2007-07-25 16:42:24 -04:00
|
|
|
import commands
|
2007-10-21 20:55:29 -04:00
|
|
|
import color, mode, tab
|
2008-03-16 16:08:37 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule
|
2007-07-25 16:42:24 -04:00
|
|
|
from method import Method
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
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"\$(?=\()"),
|
|
|
|
]
|
|
|
|
|
2007-10-14 13:11:01 -04:00
|
|
|
class EvalGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
RegionRule(r'string', "'", Grammar, "'"),
|
|
|
|
RegionRule(r'string', '"', StringGrammar, '"'),
|
|
|
|
PatternRule(r'escaped', r'\\.'),
|
|
|
|
PatternRule(r'variable', r"\${(?:[a-zA-Z0-9_]+|\?\$)}"),
|
|
|
|
PatternRule(r"variable", r"\$[^({][a-zA-Z0-9_]*"),
|
|
|
|
PatternRule(r'variable', r"\$(?=\()"),
|
|
|
|
]
|
|
|
|
|
2007-09-24 14:42:26 -04:00
|
|
|
class CaseGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
RegionRule(r'stanza', r'.+\)', Grammar, r';;'),
|
|
|
|
]
|
|
|
|
|
2007-09-20 02:10:27 -04:00
|
|
|
class TestGrammar(Grammar):
|
|
|
|
rules = [
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'sh_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_=/])"),
|
2007-09-20 02:10:27 -04:00
|
|
|
PatternRule(r'binop', r'==|=|!=|<|>'),
|
|
|
|
PatternRule(r'binop', r'-(?:nt|ot|ef|eq|ne|lt|gt|le|ge)(?![a-zA-Z0-9_])'),
|
|
|
|
PatternRule(r'unop', r'-[a-zA-Z](?![a-zA-Z0-9_])'),
|
|
|
|
PatternRule(r'continuation', r'\\\n$'),
|
|
|
|
PatternRule(r'delimiter', r";;|[();{}|&><]"),
|
|
|
|
RegionRule(r'eval', r'`', StringGrammar, r'`'),
|
|
|
|
RegionRule(r'neval', r'\$\(', StringGrammar, 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', "'", Grammar, "'"),
|
|
|
|
RegionRule(r'string', '"', StringGrammar, '"'),
|
2007-10-17 22:44:22 -04:00
|
|
|
PatternRule(r'bareword', r'[-a-zA-Z0-9_.]+'),
|
2007-09-20 02:10:27 -04:00
|
|
|
]
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
class ShGrammar(Grammar):
|
|
|
|
rules = [
|
2008-03-16 15:59:43 -04:00
|
|
|
RegionRule(r'heredoc', r"<<(?P<heredoc>[a-zA-Z0-9_]+)", StringGrammar, r'^%(heredoc)s$'),
|
2008-03-16 01:23:14 -04:00
|
|
|
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_=/])"),
|
2007-09-24 14:42:26 -04:00
|
|
|
RegionRule(r'case', r'case', Grammar, 'in', CaseGrammar, r'esac'),
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'sh_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|times|trap|type|ulimit|umask|unalias|unset|wait)(?![a-zA-Z0-9_=/])"),
|
2007-09-20 02:10:27 -04:00
|
|
|
RegionRule(r'test', r'test', TestGrammar, r'\n'),
|
|
|
|
RegionRule(r'test2', r'\[', TestGrammar, r'\]'),
|
|
|
|
PatternRule(r'delimiter', r";;|[();{}|&><]"),
|
2007-10-14 13:11:01 -04:00
|
|
|
#RegionRule(r'eval', r'`', StringGrammar, r'`'),
|
|
|
|
#RegionRule(r'neval', r'\$\(', StringGrammar, r'\)'),
|
|
|
|
RegionRule(r'eval', r'`', EvalGrammar, r'`'),
|
|
|
|
RegionRule(r'neval', r'\$\(', EvalGrammar, r'\)'),
|
2007-07-24 12:25:15 -04:00
|
|
|
PatternRule(r'variable', r"(?:^|(?<= ))[a-zA-Z_][a-zA-Z0-9_]*(?==)"),
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'variable', r"\${(?:[a-zA-Z0-9_]+|\?\$)}"),
|
|
|
|
PatternRule(r"variable", r"\$[^({][a-zA-Z0-9_]*"),
|
|
|
|
PatternRule(r'variable', r"\$(?=\()"),
|
2007-07-24 12:25:15 -04:00
|
|
|
RegionRule(r'string', "'", Grammar, "'"),
|
2007-07-21 11:40:53 -04:00
|
|
|
RegionRule(r'string', '"', StringGrammar, '"'),
|
2008-03-16 16:08:37 -04:00
|
|
|
OverridePatternRule(r'comment', r'#@@:(?P<token>[.a-zA-Z0-9_]+):(?P<mode>[.a-zA-Z0-9_]+) *$'),
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'comment', r'#.*$'),
|
2007-10-17 22:44:22 -04:00
|
|
|
PatternRule(r'bareword', r'[-a-zA-Z0-9_.]+'),
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'continuation', r'\\\n$'),
|
|
|
|
PatternRule(r'eol', r'\n$'),
|
|
|
|
]
|
|
|
|
|
2007-10-18 17:07:35 -04:00
|
|
|
# hacks to get some circular grammar refs
|
2007-10-17 22:44:22 -04:00
|
|
|
CaseGrammar.rules[0].pairs[0] = (ShGrammar, CaseGrammar.rules[0].pairs[0][1])
|
2007-10-18 17:07:35 -04:00
|
|
|
#ShGrammar.rules[2].pairs[0] = (ShGrammar, ShGrammar.rules[0].pairs[0][1])
|
2007-10-17 22:44:22 -04:00
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class ShTabber(tab.StackTabber):
|
2007-07-21 11:40:53 -04:00
|
|
|
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]
|
2008-03-16 01:23:14 -04:00
|
|
|
return t.name == 'sh_function'
|
2007-07-21 11:40:53 -04:00
|
|
|
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:
|
2007-10-21 20:55:29 -04:00
|
|
|
return tab.StackTabber._handle_close_token(self, currlvl, y, i)
|
2007-07-21 11:40:53 -04:00
|
|
|
def _handle_other_token(self, currlvl, y, i):
|
2008-04-02 19:06:52 -04:00
|
|
|
w = self.mode.tabwidth
|
2007-07-21 11:40:53 -04:00
|
|
|
token = self.get_token(y, i)
|
|
|
|
fqname = token.fqname()
|
|
|
|
if token.name == 'continuation':
|
2008-04-02 19:06:52 -04:00
|
|
|
self._opt_append("cont", currlvl + w)
|
2008-03-16 01:23:14 -04:00
|
|
|
elif token.name == 'sh_reserved' and token.string == 'else':
|
2008-04-02 19:06:52 -04:00
|
|
|
currlvl -= w
|
2007-07-21 11:40:53 -04:00
|
|
|
elif token.name == 'eol':
|
|
|
|
self._opt_pop("cont")
|
|
|
|
return currlvl
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class Sh(mode.Fundamental):
|
2007-10-18 17:07:35 -04:00
|
|
|
modename = 'sh'
|
|
|
|
paths = ['/etc/profile']
|
|
|
|
basenames = ['.bashrc', '.bash_profile', '.profile']
|
|
|
|
extensions = ['.bash', '.sh']
|
|
|
|
detection = ['sh', 'bash']
|
2007-07-21 11:40:53 -04:00
|
|
|
grammar = ShGrammar
|
|
|
|
tabbercls = ShTabber
|
2008-03-16 01:23:14 -04:00
|
|
|
opentokens = ('delimiter', 'sh_reserved', 'case.start')
|
2007-10-18 17:07:35 -04:00
|
|
|
opentags = {'(': ')', '[': ']', '{': '}', 'do': 'done', 'then': 'fi', 'case': 'esac'}
|
2008-03-16 01:23:14 -04:00
|
|
|
closetokens = ('delimiter', 'sh_reserved', 'case.end')
|
2007-07-21 11:40:53 -04:00
|
|
|
closetags = {')': '(', ']': '[', '}': '{',
|
|
|
|
'done': 'do', 'fi': 'then', 'esac': 'case'}
|
|
|
|
colors = {
|
2008-03-16 01:23:14 -04:00
|
|
|
'sh_builtin': ('cyan', 'default', 'bold'),
|
|
|
|
'sh_function': ('magenta', 'default', 'bold'),
|
|
|
|
'sh_reserved': ('magenta', 'default', 'bold'),
|
2007-07-21 11:40:53 -04:00
|
|
|
'variable': ('yellow', 'default', 'bold'),
|
|
|
|
|
2007-09-24 14:42:26 -04:00
|
|
|
# case statements
|
|
|
|
'case.start': ('magenta', 'default', 'bold'),
|
|
|
|
'case.middle0': ('magenta', 'default', 'bold'),
|
|
|
|
'case.end': ('magenta', 'default', 'bold'),
|
|
|
|
|
2008-03-16 01:23:14 -04:00
|
|
|
#'delimiter': ('default', 'default', 'bold'),
|
2007-09-20 02:10:27 -04:00
|
|
|
'test.start': ('cyan', 'default', 'bold'),
|
|
|
|
'binop': ('magenta', 'default', 'bold'),
|
|
|
|
'unop': ('magenta', 'default', 'bold'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2008-03-16 01:23:14 -04:00
|
|
|
#'string.start': ('green', 'default'),
|
|
|
|
#'string.variable': ('yellow', 'default'),
|
|
|
|
#'string.escaped': ('magenta', 'default'),
|
|
|
|
#'string.null': ('green', 'default'),
|
|
|
|
#'string.end': ('green', 'default'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
'eval.start': ('cyan', 'default'),
|
|
|
|
'eval.variable': ('yellow', 'default'),
|
|
|
|
'eval.null': ('cyan', 'default'),
|
|
|
|
'eval.end': ('cyan', 'default'),
|
|
|
|
|
|
|
|
'neval.start': ('yellow', 'default'),
|
|
|
|
'neval.variable': ('yellow', 'default'),
|
|
|
|
'neval.null': ('cyan', 'default'),
|
|
|
|
'neval.end': ('yellow', 'default'),
|
|
|
|
|
2008-03-16 01:23:14 -04:00
|
|
|
#'comment': ('red', 'default'),
|
|
|
|
#'continuation': ('red', 'default'),
|
2007-07-21 11:40:53 -04:00
|
|
|
}
|
2007-07-25 16:42:24 -04:00
|
|
|
def __init__(self, w):
|
2007-10-21 20:55:29 -04:00
|
|
|
mode.Fundamental.__init__(self, w)
|
2007-07-25 16:42:24 -04:00
|
|
|
self.add_action_and_bindings(ShCheckSyntax(), ('C-c s',))
|
|
|
|
|
|
|
|
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)
|
2007-10-19 02:41:33 -04:00
|
|
|
|
|
|
|
install = Sh.install
|