Fix some sh highlighting bugs.

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2012-08-04 12:51:46 -04:00
parent 8c8eea6328
commit fba8d89ea5
1 changed files with 109 additions and 64 deletions

View File

@ -27,17 +27,19 @@ StringGrammar1.rules = [
StringGrammar2.rules = [
PatternRule(r'escaped', r'\\.'),
PatternRule(r'variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"variable", r"\$" + word),
PatternRule(r'variable', r"\$(?=\()"),
PatternRule(r'sh.variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"sh.variable", r"\$" + word),
PatternRule(r"sh.variable", r"\$[*@#?\-\$!_\[\]]"),
PatternRule(r'sh.variable', r"\$(?=\()"),
PatternRule(r'data', r'[^\\$"]+'),
]
HereGrammar.rules = [
PatternRule(r'escaped', r'\\.'),
PatternRule(r'variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"variable", r"\$" + word),
PatternRule(r'variable', r"\$(?=\()"),
PatternRule(r'sh.variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"sh.variable", r"\$" + word),
PatternRule(r"sh.variable", r"\$[*@#?\-\$!_\[\]]"),
PatternRule(r'sh.variable', r"\$(?=\()"),
PatternRule(r'data', r'[^\\$]+'),
]
@ -45,20 +47,23 @@ EvalGrammar.rules = [
RegionRule(r'string', "'", StringGrammar1, "'"),
RegionRule(r'string', '"', StringGrammar2, '"'),
PatternRule(r'escaped', r'\\.'),
PatternRule(r'variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"variable", r"\$" + word),
PatternRule(r'variable', r"\$(?=\()"),
PatternRule(r'sh.variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"sh.variable", r"\$" + word),
PatternRule(r"sh.variable", r"\$[*@#?\-\$!_\[\]]"),
PatternRule(r'sh.variable', r"\$(?=\()"),
PatternRule(r'data', r'[^\\`$\'"]+'),
]
NevalGrammar.rules = [
RegionRule(r'string', "'", Grammar, "'"),
RegionRule(r'string', '"', StringGrammar2, '"'),
PatternRule(r'continuation', r'\\\n$'),
PatternRule(r'escaped', r'\\.'),
PatternRule(r'variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"variable", r"\$" + word),
PatternRule(r'variable', r"\$(?=\()"),
PatternRule(r'data', r'[^\\)$]+'),
PatternRule(r'sh.variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"sh.variable", r"\$" + word),
PatternRule(r"sh.variable", r"\$[*@#?\-\$!_\[\]]"),
PatternRule(r'sh.variable', r"\$(?=\()"),
PatternRule(r'data', r'[^\\)$"\']+'),
]
StanzaGrammar.rules = [
@ -77,8 +82,8 @@ CaseGrammar.rules = [
TestGrammar.rules = [
PatternRule(r'spaces', r' +'),
PatternRule(r'sh_builtin', r"(?<![-a-zA-Z0-9_=/])(?: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|times|trap|type|ulimit|umask|unalias|unset|wait)(?![a-zA-Z0-9_=/])"),
PatternRule(r'sh_reserved', r"(?:done|do|elif|else|esac|fi|for|function|if|in|select|then|until|while|time)(?![a-zA-Z0-9_=/])"),
PatternRule(r'sh.builtin', r"(?<![-a-zA-Z0-9_=/])(?: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|times|trap|type|ulimit|umask|unalias|unset|wait)(?![a-zA-Z0-9_=/])"),
PatternRule(r'sh.reserved', r"(?:done|do|elif|else|esac|fi|for|function|if|in|select|then|until|while|time)(?![a-zA-Z0-9_=/])"),
PatternRule(r'binop', r'==|=|!='),
PatternRule(r'binop', r'-(?:nt|ot|ef|eq|ne|lt|gt|le|ge)(?!' + char + ')'),
PatternRule(r'unop', r'-[a-zA-Z](?!' + char + ')'),
@ -88,49 +93,52 @@ TestGrammar.rules = [
RegionRule(r'test', r'(?<![^ \t])test(?![a-zA-Z0-9_=/])', None, r';|\n'),
RegionRule(r'test2', r'\[\[', None, r'\]\]'),
RegionRule(r'test3', r'\[', None, r'\]'),
RegionRule(r'eval', r'`', EvalGrammar, r'`'),
RegionRule(r'neval', r'\$\(', NevalGrammar, r'\)'),
PatternRule(r'variable', r"(?:^|(?<= ))" + word + "(?==)"),
PatternRule(r'variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"variable", r"\$" + word),
PatternRule(r'variable', r"\$(?=\()"),
RegionRule(r'sh.eval', r'`', EvalGrammar, r'`'),
RegionRule(r'sh.neval', r'\$\(', NevalGrammar, r'\)'),
PatternRule(r'sh.variable', r"(?:^|(?<= ))" + word + "(?==)"),
PatternRule(r'sh.variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"sh.variable", r"\$" + word),
PatternRule(r"sh.variable", r"\$[*@#?\-\$!_\[\]]"),
PatternRule(r'sh.variable', r"\$(?=\()"),
PatternRule(r'escaped', r'\\.'),
RegionRule(r'string', "'", StringGrammar1, "'"),
RegionRule(r'string', '"', StringGrammar2, '"'),
PatternRule(r'sh_bareword', r'[-a-zA-Z0-9_.]+'),
PatternRule(r'sh.bareword', r'[-a-zA-Z0-9_.]+'),
]
ShGrammar.rules = [
PatternMatchRule('x', '( *)(' + word + ')(=)',
'spaces', 'variable', 'delimiter'),
'spaces', 'sh.variable', 'delimiter'),
PatternMatchRule('x', '(alias|export)( +)(' + word + ')(=)',
'sh_builtin', 'spaces', 'variable', 'delimiter'),
'sh.builtin', 'spaces', 'sh.variable', 'delimiter'),
PatternMatchRule('x', '(unset)( +)(' + word + ')',
'sh_builtin', 'spaces', 'variable'),
'sh.builtin', 'spaces', 'sh.variable'),
PatternRule(r'spaces', r' +'),
RegionRule(r'heredoc', r"<<-'(?P<heredoc>" + word + ")'", None, "\n", HereGrammar, r'^[ \t]*%(heredoc)s$'),
RegionRule(r'heredoc', r"<<-(?P<heredoc>" + word + ")", None, "\n", HereGrammar, r'^[ \t]*%(heredoc)s$'),
RegionRule(r'heredoc', r"<<[<\\]?(?P<heredoc>" + word + ")", None, "\n", HereGrammar, r'^%(heredoc)s$'),
RegionRule(r'heredoc', r"<<-(?P<heredoc>" + word + ")", None, "\n", HereGrammar, r'^ *%(heredoc)s$'),
PatternRule(r'sh_function', word + r'(?= *\(\))'),
PatternRule(r'sh_reserved', r"(?:done|do|elif|else|esac|fi|for|function|if|in|select|then|until|while|time)(?![a-zA-Z0-9_=/])"),
RegionRule(r'case', r'case', None, 'in', CaseGrammar, r'esac'),
PatternRule(r'sh_builtin', r"(?<![-a-zA-Z0-9_])(?: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|times|trap|type|ulimit|umask|unalias|unset|wait)(?![a-zA-Z0-9_=/])(?![-a-zA-Z0-9_])"),
PatternRule(r'sh.function', word + r'(?= *\(\))'),
PatternRule(r'sh.reserved', r"(?:done|do|elif|else|esac|fi|for|function|if|in|select|then|until|while|time)(?![a-zA-Z0-9_=/])"),
RegionRule(r'sh.case', r'case', None, 'in', CaseGrammar, r'esac'),
PatternRule(r'sh.builtin', r"(?<![-a-zA-Z0-9_])(?: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|times|trap|type|ulimit|umask|unalias|unset|wait)(?![a-zA-Z0-9_=/])(?![-a-zA-Z0-9_])"),
RegionRule(r'test', r'(?<![^ \t])test(?![a-zA-Z0-9_=/])', TestGrammar, r';|\n'),
RegionRule(r'test2', r'\[\[', TestGrammar, r'\]\]'),
RegionRule(r'test3', r'\[', TestGrammar, r'\]'),
PatternRule(r'redirect', r'<|>'),
PatternRule(r'delimiter', r";;|[();{}|&><:=/]"),
RegionRule(r'eval', r'`', EvalGrammar, r'`'),
RegionRule(r'neval', r'\$\(', NevalGrammar, r'\)'),
PatternRule(r'variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"variable", r"\$" + word),
#PatternRule(r'variable', r"\$(?=\()"),
RegionRule(r'sh.eval', r'`', EvalGrammar, r'`'),
RegionRule(r'sh.neval', r'\$\(', NevalGrammar, r'\)'),
PatternRule(r'sh.variable', r"\${(?:" + word + "|\?\$)}"),
PatternRule(r"sh.variable", r"\$" + word),
PatternRule(r"sh.variable", r"\$[*@#?\-\$!_\[\]]"),
RegionRule(r'sh.param', r"\${", Grammar, "}"),
PatternRule('escaped', r'\\.'),
RegionRule(r'string', "'", StringGrammar1, "'"),
RegionRule(r'string', '"', StringGrammar2, '"'),
OverridePatternRule(r'comment', r'#@@:(?P<token>' + pname + '):(?P<mode>' + pname + ') *$'),
PatternRule(r'comment', r'#.*$'),
PatternRule(r'sh_bareword', r'(?:[-a-zA-Z0-9_.]|\\.)+'),
PatternRule(r'sh.bareword', r'(?:[-a-zA-Z0-9_.]|\\.)+'),
PatternRule(r'continuation', r'\\\n$'),
PatternRule(r'eol', r'\n$'),
]
@ -143,10 +151,10 @@ class ShTabber(StackTabber):
if not highlighter.tokens[y]:
return False
t = highlighter.tokens[y][0]
return t.name == 'sh_function'
return t.name == 'sh.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":
if s == ')' and self.markers and self._peek_name() == "sh.case":
# we have to ignore ) when used in "case" statements.
return currlvl
else:
@ -157,7 +165,7 @@ class ShTabber(StackTabber):
fqname = token.fqname()
if token.name == 'continuation':
self._opt_append("cont", currlvl + w)
elif token.name == 'sh_reserved' and token.string == 'else':
elif token.name == 'sh.reserved' and token.string == 'else':
currlvl -= w
elif token.name == 'eol':
self._opt_pop("cont")
@ -189,6 +197,37 @@ class BashLoadFile(Interact):
Interact._execute(self, w, bname='*Bash*', cmd=cmd)
b = w.application.get_buffer_by_name('*Bash*')
# white is for delimiters, operators, numbers
c_default = ('default', 'default')
# magenta is for keywords/builtins, translation, globs
lo_magenta = ('magenta202', 'default')
hi_magenta = ('magenta505', 'default')
# red is for comments, pods, endblocks
lo_red = ('red300', 'default')
hi_red = ('red511', 'default')
# orange are for arrays and hashes
hi_orange = ('yellow531', 'default')
lo_orange = ('yellow520', 'default')
# yellow is for scalars and prototypes
hi_yellow = ('yellow551', 'default')
lo_yellow = ('yellow330', 'default')
# green is for strings and hash keys
lo_green = ('green030', 'default')
hi_green = ('green050', 'default')
# cyan is for quotes, evals, regexes, subs
lo_cyan = ('cyan033', 'default')
hi_cyan = ('cyan155', 'default')
# blue is unused
lo_blue = ('blue113', 'default')
hi_blue = ('blue225', 'default')
class Sh(Fundamental):
name = 'sh'
paths = ['/etc/profile']
@ -198,40 +237,46 @@ class Sh(Fundamental):
detection = [re.compile('^#!(?:.+/)sh'), re.compile('^#!(?:.+/)bash')]
grammar = ShGrammar
tabbercls = ShTabber
opentokens = ('delimiter', 'sh_reserved', 'case.start')
opentokens = ('delimiter', 'sh.reserved', 'sh.case.start')
opentags = {'(': ')', '[': ']', '{': '}', 'do': 'done', 'then': 'fi',
'case': 'esac'}
closetokens = ('delimiter', 'sh_reserved', 'case.end')
closetokens = ('delimiter', 'sh.reserved', 'sh.case.end')
closetags = {')': '(', ']': '[', '}': '{', 'done': 'do', 'fi': 'then',
'esac': 'case'}
colors = {
'sh_builtin': ('cyan', 'default', 'bold'),
'sh_function': ('magenta', 'default', 'bold'),
'sh_reserved': ('magenta', 'default', 'bold'),
'variable': ('yellow', 'default', 'bold'),
'sh.builtin': hi_cyan,
'sh.function': hi_magenta,
'sh.reserved': hi_magenta,
'sh.variable': hi_yellow,
'string.sh.variable': lo_yellow,
'sh.param.start': lo_yellow,
'sh.param.null': lo_yellow,
'sh.param.end': lo_yellow,
# case statements
'case.start': ('magenta', 'default', 'bold'),
'case.stanza.start': ('cyan', 'default', 'bold'),
'case.stanza.start_cont': ('cyan', 'default', 'bold'),
'case.stanza.middle0': ('cyan', 'default', 'bold'),
'case.middle0': ('magenta', 'default', 'bold'),
'case.end': ('magenta', 'default', 'bold'),
'sh.case.start': hi_magenta,
'sh.case.stanza.start': lo_cyan,
'sh.case.stanza.start_cont': lo_cyan,
'sh.case.stanza.middle0': lo_cyan,
'sh.case.middle0': hi_magenta,
'sh.case.end': hi_magenta,
'test.start': ('cyan', 'default', 'bold'),
'binop': ('magenta', 'default', 'bold'),
'unop': ('magenta', 'default', 'bold'),
'test.start': hi_cyan,
'binop': hi_magenta,
'unop': hi_magenta,
'eval.start': ('cyan', 'default', 'bold'),
'eval.variable': ('yellow', 'default', 'bold'),
'eval.data': ('cyan', 'default', 'bold'),
'eval.null': ('cyan', 'default', 'bold'),
'eval.end': ('cyan', 'default', 'bold'),
'neval.start': ('yellow', 'default', 'bold'),
'neval.variable': ('yellow', 'default', 'bold'),
'neval.data': ('cyan', 'default', 'bold'),
'neval.null': ('cyan', 'default', 'bold'),
'neval.end': ('yellow', 'default', 'bold'),
'sh.eval.start': lo_cyan,
'sh.eval.sh.variable': hi_yellow,
'sh.eval.data': hi_cyan,
'sh.eval.null': hi_cyan,
'sh.eval.end': lo_cyan,
'sh.neval.start': lo_cyan,
'sh.neval.sh.variable': hi_yellow,
'sh.neval.data': hi_cyan,
'sh.neval.null': hi_cyan,
'sh.neval.end': lo_cyan,
}
actions = [ShCheckSyntax, BashStart, BashLoadFile]
_bindings = {