diff --git a/application.py b/application.py index 2b44c40..f0e929a 100755 --- a/application.py +++ b/application.py @@ -7,12 +7,12 @@ import util, window2 from point2 import Point # modes -# TODO: mode_c mode_nasm mode_sh mode_sql mode_javascript mode_tt +# TODO: mode_c mode_nasm mode_sql mode_javascript mode_tt import mode2 import mode_mini, mode_search, mode_replace, mode_which import mode_console, mode_consolemini import mode_blame, mode_diff -import mode_python, mode_perl, mode_xml, mode_nasm +import mode_python, mode_perl, mode_xml, mode_nasm, mode_sh import mode_life, mode_text, mode_mutt def run(buffers, jump_to_line=None, init_mode=None): @@ -93,7 +93,7 @@ class Application(object): 'python': mode_python.Python, 'replace': mode_replace.Replace, 'search': mode_search.Search, -# 'sh': mode_sh.Sh, + 'sh': mode_sh.Sh, 'text': mode_text.Text, 'which': mode_which.Which, 'xml': mode_xml.XML, @@ -107,12 +107,12 @@ class Application(object): # these are used in this order to determine which mode to open certain # kinds of files self.mode_paths = { - #'/etc/profile': 'sh', + '/etc/profile': 'sh', } self.mode_basenames = { - #'.bashrc': 'sh', - #'.bash_profile': 'sh', - #'.profile': 'sh', + '.bashrc': 'sh', + '.bash_profile': 'sh', + '.profile': 'sh', } self.mode_extensions = { '.py': 'python', @@ -122,8 +122,8 @@ class Application(object): # '.c': 'c', '.txt': 'text', '.s': 'nasm', -# '.sh': 'sh', -# '.bash': 'sh', + '.sh': 'sh', + '.bash': 'sh', '.xml': 'xml', '.xml.in': 'xml', '.html': 'xml', @@ -135,8 +135,8 @@ class Application(object): self.mode_detection = { 'python': 'python', 'perl': 'perl', -# 'sh': 'sh', -# 'bash': 'sh', + 'sh': 'sh', + 'bash': 'sh', } # initialize our methods diff --git a/lex2.py b/lex2.py index bb689a4..4ab541b 100755 --- a/lex2.py +++ b/lex2.py @@ -130,11 +130,6 @@ class RegionRule(Rule): self.end = end self.start_re = re.compile(start) - for rule in self.grammar.rules: - if not hasattr(rule, 'match'): - print 'DAMN %r' % name - raise Exception, repr(rule) - def resume(self, lexer, toresume): #raise Exception, "%r %r" % (lexer, toresume) #XYZ assert toresume, "can't resume without tokens to resume!" @@ -464,8 +459,6 @@ class Grammar: rules = [] def __init__(self): for rule in self.rules: - if type(rule) in (type(tuple()), type(list())): - print self.rules if hasattr(rule, 'grammar') and rule.grammar is None: rule.grammar = self diff --git a/mode_sh.py b/mode_sh.py index baf1c70..c2e7b33 100644 --- a/mode_sh.py +++ b/mode_sh.py @@ -1,36 +1,59 @@ -import commands, os.path, sets, sys +import color, mode2 +from lex2 import Grammar, PatternRule, RegionRule -import color, default, mode, lex, lex_sh, method, tab_sh +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 Sh(mode.Fundamental): +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): - mode.Fundamental.__init__(self, w) - - self.grammar = lex_sh.ShGrammar() - self.lexer = lex.Lexer(self.grammar) - + mode2.Fundamental.__init__(self, w) self.colors = { - 'builtin': color.build('cyan', 'default', 'bold'), - 'method': color.build('magenta', 'default', 'bold'), - 'reserved': color.build('magenta', 'default', 'bold'), - #'delimiter': color.build('magenta', 'default', 'bold'), - 'delimiter': color.build('default', 'default', 'bold'), - 'operator': color.build('magenta', 'default', 'bold'), - 'redirection': color.build('blue', 'default', 'bold'), - 'string1': color.build('green', 'default'), - 'string2': color.build('green', 'default'), - 'eval': color.build('cyan', 'default', 'bold'), - 'comment': color.build('red', 'default'), - 'continuation': color.build('red', 'default'), - 'variable0': color.build('yellow', 'default', 'bold'), - 'variable1': color.build('yellow', 'default', 'bold'), - 'variable2': color.build('yellow', 'default', 'bold'), - 'variable3': color.build('yellow', 'default', 'bold'), + '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'), } - - #self.highlighter.lex_buffer() - #self.get_regions() - self.tabber = tab_sh.ShTabber(self) - def name(self): return "Sh"