pmacs3/mode_sh.py

101 lines
4.7 KiB
Python
Raw Normal View History

import color, mode2, tab2
from lex3 import Grammar, PatternRule, RegionRule
2007-03-06 10:05:38 -05:00
class StringGrammar(Grammar):
rules = [
2007-07-12 17:42:31 -04:00
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-03-06 10:05:38 -05:00
class ShGrammar(Grammar):
rules = [
2007-07-12 17:42:31 -04:00
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";;|[\[\]\(\);\{\}|&><]"),
2007-07-12 17:42:31 -04:00
RegionRule(r'eval', '`', StringGrammar, '`'),
#RegionRule(r'neval', r'\$\(', None, r'\)'),
RegionRule(r'neval', r'\$\(', StringGrammar, r'\)'),
2007-07-12 17:42:31 -04:00
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$'),
]
2007-03-06 10:05:38 -05:00
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)
2007-03-06 10:05:38 -05:00
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'),
2007-07-12 17:42:31 -04:00
'delimiter': color.build('default', 'default', 'bold'),
'operator': color.build('magenta', 'default', 'bold'),
2007-07-12 17:42:31 -04:00
'string.start': color.build('green', 'default'),
'string.variable': color.build('yellow', 'default'),
'string.null': color.build('green', 'default'),
'string.end': color.build('green', 'default'),
2007-07-12 17:42:31 -04:00
'eval.start': color.build('cyan', 'default'),
'eval.variable': color.build('yellow', 'default'),
'eval.null': color.build('cyan', 'default'),
'eval.end': color.build('cyan', 'default'),
2007-07-12 17:42:31 -04:00
#'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'),
2007-07-12 17:42:31 -04:00
'comment': color.build('red', 'default'),
'continuation': color.build('red', 'default'),
2007-03-06 10:05:38 -05:00
}
def name(self):
return "Sh"