pmacs3/mode_sh.py

60 lines
3.5 KiB
Python

import color, mode2
from lex2 import Grammar, PatternRule, RegionRule
class StringGrammar(Grammar):
rules = [
PatternRule(name=r'escaped', pattern=r'\\.'),
PatternRule(name=r'variable', pattern=r"(?:^|(?<= ))[a-zA-Z_][a-zA-Z_][a-zA-Z0-9_]*(?==)"),
PatternRule(name=r'variable', pattern=r"\${(?:[a-zA-Z0-9_]+|\?\$)}"),
PatternRule(name=r"variable", pattern=r"\$[^({][a-zA-Z0-9_]*"),
PatternRule(name=r'variable', pattern=r"\$(?=\()"),
]
class ShGrammar(Grammar):
rules = [
PatternRule(name=r'function', pattern=r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\(\))'),
PatternRule(name=r'reserved', pattern=r"(?:case|done|do|elif|else|esac|fi|for|function|if|in|select|then|until|while|time)(?![a-zA-Z0-9_=/])"),
PatternRule(name=r'builtin', pattern=r"(?:source|alias|bg|bind|break|builtin|cd|command|compgen|complete|declare|dirs|disown|echo|enable|eval|exec|exit|export|fc|fg|getops|hash|help|history|jobs|kill|let|local|logout|popd|printf|pushd|pwd|readonly|read|return|set|shift|shopt|suspend|test|times|trap|type|ulimit|umask|unalias|unset|wait)(?![a-zA-Z0-9_=/])"),
PatternRule(name=r'operator', pattern=r"(?:-eq|-ne|-gt|-lt|-ge|-le| = | != )"),
PatternRule(name=r'delimiter', pattern=r"[][\(\);\{\}|&><]"),
RegionRule(name=r'eval2', start=r'\$\(', grammar=None, end=r'\)'),
PatternRule(name=r'variable', pattern=r"(?:^|(?<= ))[a-zA-Z_][a-zA-Z_][a-zA-Z0-9_]*(?==)"),
PatternRule(name=r'variable', pattern=r"\${(?:[a-zA-Z0-9_]+|\?\$)}"),
PatternRule(name=r"variable", pattern=r"\$[^({][a-zA-Z0-9_]*"),
PatternRule(name=r'variable', pattern=r"\$(?=\()"),
RegionRule(name=r'eval', start='`', grammar=StringGrammar(), end='`'),
RegionRule(name=r'string', start="'", grammar=Grammar(), end="'"),
RegionRule(name=r'string', start='"', grammar=StringGrammar(), end='"'),
PatternRule(name=r'continuation', pattern=r'\\(?= *$)'),
PatternRule(name=r'comment', pattern=r'#.*$'),
PatternRule(name=r'bareword', pattern=r'[a-zA-Z0-9_-]+'),
]
class Sh(mode2.Fundamental):
grammar = ShGrammar()
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.colors = {
'builtin': color.build('cyan', 'default', 'bold'),
'function': color.build('magenta', 'default', 'bold'),
'reserved': color.build('magenta', 'default', 'bold'),
'variable': color.build('yellow', 'default', 'bold'),
'delimiter': color.build('default', 'default', 'bold'),
'operator': color.build('magenta', 'default', 'bold'),
'redirection': color.build('blue', 'default', 'bold'),
'string.start': color.build('green', 'default'),
'string.variable': color.build('yellow', 'default'),
'string.null': color.build('green', 'default'),
'string.end': color.build('green', 'default'),
'eval.start': color.build('cyan', 'default'),
'eval.variable': color.build('yellow', 'default'),
'eval.null': color.build('cyan', 'default'),
'eval.end': color.build('cyan', 'default'),
'eval2.start': color.build('cyan', 'default'),
'eval2.end': color.build('cyan', 'default'),
'comment': color.build('red', 'default'),
'continuation': color.build('red', 'default'),
}
def name(self):
return "Sh"