101 lines
4.7 KiB
Python
101 lines
4.7 KiB
Python
import color, mode2, tab2
|
|
from lex3 import Grammar, PatternRule, RegionRule
|
|
|
|
class StringGrammar(Grammar):
|
|
rules = [
|
|
PatternRule(r'escaped', r'\\.'),
|
|
PatternRule(r'variable', r"\${(?:[a-zA-Z0-9_]+|\?\$)}"),
|
|
PatternRule(r"variable", r"\$[^({][a-zA-Z0-9_]*"),
|
|
PatternRule(r'variable', r"\$(?=\()"),
|
|
]
|
|
|
|
class ShGrammar(Grammar):
|
|
rules = [
|
|
PatternRule(r'function', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\(\))'),
|
|
PatternRule(r'reserved', r"(?:case|done|do|elif|else|esac|fi|for|function|if|in|select|then|until|while|time)(?![a-zA-Z0-9_=/])"),
|
|
PatternRule(r'builtin', 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(r'operator', r"(?:-eq|-ne|-gt|-lt|-ge|-le| = | != )"),
|
|
PatternRule(r'delimiter', r";;|[\[\]\(\);\{\}|&><]"),
|
|
RegionRule(r'eval', '`', StringGrammar, '`'),
|
|
#RegionRule(r'neval', r'\$\(', None, r'\)'),
|
|
RegionRule(r'neval', r'\$\(', StringGrammar, r'\)'),
|
|
PatternRule(r'variable', r"(?:^|(?<= ))[a-zA-Z_][a-zA-Z_][a-zA-Z0-9_]*(?==)"),
|
|
PatternRule(r'variable', r"\${(?:[a-zA-Z0-9_]+|\?\$)}"),
|
|
PatternRule(r"variable", r"\$[^({][a-zA-Z0-9_]*"),
|
|
PatternRule(r'variable', r"\$(?=\()"),
|
|
RegionRule(r'string', "'", Grammar(), "'"),
|
|
RegionRule(r'string', '"', StringGrammar, '"'),
|
|
PatternRule(r'comment', r'#.*$'),
|
|
PatternRule(r'bareword', r'[a-zA-Z0-9_-]+'),
|
|
PatternRule(r'continuation', r'\\\n$'),
|
|
PatternRule(r'eol', r'\n$'),
|
|
]
|
|
|
|
class ShTabber(tab2.StackTabber):
|
|
def is_base(self, y):
|
|
if y == 0:
|
|
return True
|
|
highlighter = self.mode.window.buffer.highlights[self.mode.name()]
|
|
if not highlighter.tokens[y]:
|
|
return False
|
|
t = highlighter.tokens[y][0]
|
|
return t.name == 'function'
|
|
def _handle_close_token(self, currlvl, y, i):
|
|
s = self.get_token(y, i).string
|
|
if s == ')' and self.markers and self._peek_name() == "case":
|
|
# we have to ignore ) when used in "case" statements.
|
|
return currlvl
|
|
else:
|
|
return tab2.StackTabber._handle_close_token(self, currlvl, y, i)
|
|
def _handle_other_token(self, currlvl, y, i):
|
|
token = self.get_token(y, i)
|
|
fqname = token.fqname()
|
|
if token.name == 'continuation':
|
|
self._opt_append("cont", currlvl + 4)
|
|
elif token.name == 'eol':
|
|
self._opt_pop("cont")
|
|
return currlvl
|
|
|
|
class Sh(mode2.Fundamental):
|
|
grammar = ShGrammar
|
|
tabbercls = ShTabber
|
|
opentokens = ('delimiter', 'reserved',)
|
|
opentags = {'(': ')', '[': ']', '{': '}',
|
|
'do': 'done', 'then': 'fi', 'case': 'esac'}
|
|
closetokens = ('delimiter', 'reserved',)
|
|
closetags = {')': '(', ']': '[', '}': '{',
|
|
'done': 'do', 'fi': 'then', 'esac': 'case'}
|
|
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'),
|
|
|
|
'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'),
|
|
|
|
#'neval.start': color.build('cyan', 'default'),
|
|
#'neval.end': color.build('cyan', 'default'),
|
|
'neval.start': color.build('yellow', 'default'),
|
|
'neval.variable': color.build('yellow', 'default'),
|
|
'neval.null': color.build('cyan', 'default'),
|
|
'neval.end': color.build('yellow', 'default'),
|
|
|
|
'comment': color.build('red', 'default'),
|
|
'continuation': color.build('red', 'default'),
|
|
}
|
|
def name(self):
|
|
return "Sh"
|