import commands import color, mode, tab from lex import Grammar, PatternRule, RegionRule, PatternGroupRule, OverridePatternRule from method import Method class StringGrammar1(Grammar): rules = [ PatternRule(r'data', r'[^\']+'), ] class StringGrammar2(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"\$(?=\()"), PatternRule(r'data', r'[^\\$"]+'), ] class HereGrammar(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"\$(?=\()"), PatternRule(r'data', r'[^\\$]+'), ] class EvalGrammar(Grammar): rules = [ RegionRule(r'string', "'", StringGrammar1, "'"), RegionRule(r'string', '"', StringGrammar2, '"'), PatternRule(r'escaped', r'\\.'), PatternRule(r'variable', r"\${(?:[a-zA-Z0-9_]+|\?\$)}"), PatternRule(r"variable", r"\$[a-zA-Z0-9_]*"), PatternRule(r'variable', r"\$(?=\()"), PatternRule(r'data', r'[^\\`$]+'), ] class NevalGrammar(Grammar): rules = [ RegionRule(r'string', "'", Grammar, "'"), RegionRule(r'string', '"', StringGrammar2, '"'), PatternRule(r'escaped', r'\\.'), PatternRule(r'variable', r"\${(?:[a-zA-Z0-9_]+|\?\$)}"), PatternRule(r"variable", r"\$[a-zA-Z0-9_]*"), PatternRule(r'variable', r"\$(?=\()"), PatternRule(r'data', r'[^\\)$]+'), ] class StanzaGrammar(Grammar): rules = [ PatternRule(r'spaces', r' +'), PatternRule(r'start_cont', r'.+\\'), PatternRule(r'eol', r'\n'), ] class CaseGrammar(Grammar): rules = [ PatternRule(r'comment', r'#.*$'), PatternRule(r'spaces', r' +'), RegionRule(r'stanza', r'.+\\\n$', StanzaGrammar, r'.+\)', Grammar, r';;'), RegionRule(r'stanza', r'.+?\)', Grammar, r';;'), PatternRule(r'eol', r'\n'), ] class TestGrammar(Grammar): rules = [ PatternRule(r'spaces', r' +'), PatternRule(r'sh_builtin', r"(?'), PatternRule(r'delimiter', r";;|[();{}|&><]"), RegionRule(r'test', r'test', 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"(?:^|(?<= ))[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', "'", StringGrammar1, "'"), RegionRule(r'string', '"', StringGrammar2, '"'), PatternRule(r'bareword', r'[-a-zA-Z0-9_.]+'), ] class ShGrammar(Grammar): rules = [ PatternGroupRule(r'vardecl', r'spaces', r'^ +', r'variable', r'[a-zA-Z_][a-zA-Z0-9_]*', r'delimiter', r'='), PatternGroupRule(r'vardecl', r'sh_builtin', r'(?:alias|export)', r'spaces', r' +', r'variable', r'[a-zA-Z_][a-zA-Z0-9_]*', r'delimiter', r'='), PatternRule(r'variable', r"^[a-zA-Z_][a-zA-Z0-9_]*(?==)"), PatternRule(r'spaces', r' +'), RegionRule(r'heredoc', r"<<[<\\]?(?P[a-zA-Z_][a-zA-Z0-9_]*)", None, "\n", HereGrammar, r'^%(heredoc)s$'), RegionRule(r'heredoc', r"<<-(?P[a-zA-Z_][a-zA-Z0-9_]*)", None, "\n", HereGrammar, r'^ *%(heredoc)s$'), PatternRule(r'sh_function', r'[a-zA-Z_][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_=/])"), RegionRule(r'case', r'case', None, 'in', CaseGrammar, r'esac'), PatternRule(r'sh_builtin', r"(?'), PatternRule(r'delimiter', r";;|[();{}|&><:=/]"), RegionRule(r'eval', r'`', EvalGrammar, r'`'), RegionRule(r'neval', r'\$\(', NevalGrammar, r'\)'), PatternRule(r'variable', r"^[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', "'", StringGrammar1, "'"), RegionRule(r'string', '"', StringGrammar2, '"'), OverridePatternRule(r'comment', r'#@@:(?P[.a-zA-Z0-9_]+):(?P[.a-zA-Z0-9_]+) *$'), PatternRule(r'comment', r'#.*$'), PatternRule(r'bareword', r'(?:[-a-zA-Z0-9_.]|\\.)+'), PatternRule(r'continuation', r'\\\n$'), PatternRule(r'eol', r'\n$'), ] # hacks to get some circular grammar refs rule = CaseGrammar.rules[2] rule.pairs[1] = (ShGrammar, rule.pairs[1][1]) rule = CaseGrammar.rules[3] rule.pairs[0] = (ShGrammar, rule.pairs[0][1]) class ShTabber(tab.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 == '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": # we have to ignore ) when used in "case" statements. return currlvl else: return tab.StackTabber._handle_close_token(self, currlvl, y, i) def _handle_other_token(self, currlvl, y, i): w = self.mode.tabwidth token = self.get_token(y, i) fqname = token.fqname() if token.name == 'continuation': self._opt_append("cont", currlvl + w) elif token.name == 'sh_reserved' and token.string == 'else': currlvl -= w elif token.name == 'eol': self._opt_pop("cont") return currlvl class ShCheckSyntax(Method): '''Check the syntax of a shell script''' def _execute(self, w, **vargs): app = w.application cmd = "bash -n %r" % w.buffer.path (status, output) = commands.getstatusoutput(cmd) if status == 0: app.set_error("Syntax OK") app.data_buffer("*Sh-Check-Syntax*", output, switch_to=False) else: app.data_buffer("*Sh-Check-Syntax*", output) class Sh(mode.Fundamental): modename = 'sh' paths = ['/etc/profile'] basenames = ['.bashrc', '.bash_profile', '.profile'] extensions = ['.bash', '.sh'] detection = ['sh', 'bash'] grammar = ShGrammar tabbercls = ShTabber opentokens = ('delimiter', 'sh_reserved', 'case.start') opentags = {'(': ')', '[': ']', '{': '}', 'do': 'done', 'then': 'fi', 'case': 'esac'} closetokens = ('delimiter', 'sh_reserved', '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'), # 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'), 'test.start': ('cyan', 'default', 'bold'), 'binop': ('magenta', 'default', 'bold'), 'unop': ('magenta', 'default', 'bold'), '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'), } actions = [ShCheckSyntax] def __init__(self, w): mode.Fundamental.__init__(self, w) self.add_bindings('sh-check-syntax', ('C-c s',)) install = Sh.install