From ab0a0717a3e9ebc0e52e7d981f41af3c531c0be5 Mon Sep 17 00:00:00 2001 From: moculus Date: Thu, 2 Oct 2008 03:59:30 +0000 Subject: [PATCH] --HG-- branch : pmacs2 --- mode/awk.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 7 deletions(-) diff --git a/mode/awk.py b/mode/awk.py index 24a453b..e7e6adb 100644 --- a/mode/awk.py +++ b/mode/awk.py @@ -13,10 +13,13 @@ class AwkGrammar(Grammar): rules = [ PatternRule(r'comment', r'#.*\n$'), PatternRule(r'spaces', r' +'), - RegionRule(r'regex', r'/(?! )', RegexGrammar, r'/'), + RegionRule(r'awk_regex', r'/(?! )', RegexGrammar, r'/'), PatternRule(r'awk_global', r'(?:TEXTDOMAIN|SUBSEP|RLENGTH|RSTART|RT|RS|PROCINFO|ORS|OFS|OFMT|NR|NF|LINT|IGNORECASE|FS|FNR|FILENAME|FIELDWIDTHS|ERRNO|ENVIRON|CONVFMT|BINMODE|ARGV|ARGIND|ARGC)(?![a-zA-Z0-9_])'), - PatternRule(r'delimiter', r'(?:[\{\}()\[\]?:;,\$]|=(?!=)|\+=|-=|\*=|/=|\%=|\^=)'), - PatternRule(r'keyword', r'(?:if|else|while|do|for|break|continue|delete|exit|close|getline|nextfile|next|printf|print|system|fflush|atan2|cos|exp|int|log|rand|sin|sqrt|srand|asorti|asort|gensub|gsub|index|length|match|split|sprintf|strtonum|substr|sub|tolower|toupper|mktime|strftime|systime|and|compl|lshift|or|xor|rshift|bindtextdomain|dcgettext|dcngettext|function|extension)(?![a-zA-Z0-9_])'), + PatternRule(r'delimiter', r'(?:[\{\}()\[\]?:;,]|=(?!=)|\+=|-=|\*=|/=|\%=|\^=)'), + PatternRule(r'keyword', r'(?:if|else|while|do|for|break|continue|delete|exit)(?![a-zA-Z0-9_])'), + PatternRule(r'builtin', r'(?:BEGIN|END|close|getline|nextfile|next|printf|print|system|fflush|atan2|cos|exp|int|log|rand|sin|sqrt|srand|asorti|asort|gensub|gsub|index|length|match|split|sprintf|strtonum|substr|sub|tolower|toupper|mktime|strftime|systime|and|compl|lshift|or|xor|rshift|bindtextdomain|dcgettext|dcngettext|function|extension)(?![a-zA-Z0-9_])'), + + PatternRule(r'awk_field', r'\$\d*'), PatternRule(r'number', r'-?0x[0-9A-Fa-f]+'), PatternRule(r'number', r'-?0[0-7]*'), @@ -26,19 +29,98 @@ class AwkGrammar(Grammar): PatternRule(r'unop', r'!(?![=~])|--|\+\+'), PatternRule(r'binop', r'(?:&&|\|\||<=|>=|!=|!~|==|\^|%|[-~/+*<>])'), RegionRule(r'string', r'"', StringGrammar2, r'"'), - PatternRule(r'function', r'[a-zA-Z_][a-zA-Z0-9_]*(?=\()'), - PatternRule(r'identifier', r'[a-zA-Z_][a-zA-Z0-9_]*'), + PatternRule(r'awk_function', r'[a-zA-Z_][a-zA-Z0-9_]*(?=\()'), + PatternRule(r'awk_identifier', r'[a-zA-Z_][a-zA-Z0-9_]*'), - PatternRule(r'cont', r'\\(?=\n)'), + PatternRule(r'continuation', r'\\\n$'), PatternRule(r'eol', r'\n'), ] +class AwkTabber(tab.StackTabber): + def __init__(self, m): + self.mode = m + self.name = m.name() + self.lines = {} + self._reset() + def region_added(self, p, newlines): + self.lines = {} + def region_removed(self, p1, p2): + self.lines = {} + def get_level(self, y): + if y not in self.lines: + self._calc_level(y) + return self.lines.get(y) + + def _calc_level(self, y): + target = y + while not self.is_base(y) and y > 0: + y -= 1 + self._reset() + while y <= target: + self._set_currlvl() + self._handle_tokens(y) + + def _is_base(self, y): + return False + def _reset(self): + self.stack = [] + self.curr = 0 + def _set_currlvl(self): + if self.markers: + self.curr = self.markers[-1].level + else: + self.curr = 0 + def _get_tokens(self, y): + return self.mode.window.buffer.highlights[self.name].tokens[y] + + def _handle_tokens(self, y): + tokens = self._get_tokens(y) + for i in range(0, len(tokens)): + token = tokens[i] + if self._is_indent(token): + pass + elif self._is_ignored(token): + pass + elif self._is_close_token(token): + self._handle_close_token(y, tokens, i, token) + elif self._is_open_token(token): + self._handle_open_token(y, tokens, i, token) + else: + self._handle_other_token(y, tokens, i, token) + self.lines[y] = self.curr + + def _is_indent(self, token): + return False + def _is_ignored(self, token): + return False + + def _is_close_token(self, token): + return True + def _close_match(self, opentoken, closetoken): + return True + def _handle_close_token(self, y, tokens, i, token): + if not self.stack: + raise Exception, "unmatched %r on line %d" % (token.string, y) + token2 = self.stack.pop() + if not self._close_match(token2, token): + raise Exception, "mismatched %r on line %d (expected %r)" % \ + (token.string, y, token2.string) + if False: + self._set_currlvl() + class Awk(mode.Fundamental): modename = 'awk' extensions = ['.awk'] grammar = AwkGrammar colors = { - 'awk_global': ('cyan', 'default', 'bold'), + 'awk_global': ('yellow', 'default', 'bold'), + 'awk_function': ('magenta', 'default', 'bold'), + 'awk_field': ('yellow', 'default', 'bold'), + 'awk_identifier': ('yellow', 'default', 'bold'), + 'awk_regex.start': ('cyan', 'default', 'bold'), + 'awk_regex.null': ('cyan', 'default', 'bold'), + 'awk_regex.data': ('cyan', 'default', 'bold'), + 'awk_regex.end': ('cyan', 'default', 'bold'), } install = Awk.install