pmacs3/mode/awk.py

208 lines
7.7 KiB
Python

import commands
import color, mode, tab
from lex import Grammar, PatternRule, RegionRule
from mode.python import StringGrammar2
from tab import Marker
class RegexGrammar(Grammar):
rules = [
PatternRule(r'escaped', r'\\.'),
PatternRule(r'data', r'[^\\/]+'),
]
class AwkGrammar(Grammar):
rules = [
PatternRule(r'comment', r'#.*\n$'),
PatternRule(r'spaces', 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'(?:BEGIN|END|if|else|while|do|for|break|continue|delete|exit)(?![a-zA-Z0-9_])'),
PatternRule(r'builtin', r'(?: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]*'),
PatternRule(r'number', r'-?[0-9]+\.?[0-9]*'),
PatternRule(r'number', r'-?\.[0-9]+'),
PatternRule(r'unop', r'!(?![=~])|--|\+\+'),
PatternRule(r'binop', r'(?:&&|\|\||<=|>=|!=|!~|==|\^|%|[-~/+*<>])'),
RegionRule(r'string', r'"', StringGrammar2, r'"'),
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'continuation', r'\\\n$'),
PatternRule(r'eol', r'\n'),
]
class AwkTabber(tab.StackTabber):
open_tokens = {'{': '}', '(': ')', '[': ']'}
close_tokens = {'}': '{', ')': '(', ']': '['}
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._save_curr_level()
self._handle_tokens(y)
y += 1
def _is_base(self, y):
if y == 0:
return True
t = self._get_tokens(y)[0]
if t.fqname() == 'awk_regex.start':
return True
elif t.name in ('awk_field', 'awk_global'):
return True
elif t.name == 'keyword' and t.string in ('BEGIN', 'END'):
return True
else:
return False
def _reset(self):
self.record = {}
self.stack = []
self.markers = self.stack
self.curr_level = 0
def _get_curr_level(self):
if self.stack:
return self.stack[-1].level
else:
return 0
def _get_next_level(self):
return self._get_curr_level() + self.mode.tabwidth
def _save_curr_level(self):
self.curr_level = self._get_curr_level()
def _match(self, *names):
return self.stack and self.stack[-1].name in names
def _nomatch(self, *names):
return self.stack and self.stack[-1].name not in names
def _pop(self, *names):
if self._match(*names):
self.stack.pop()
def _pop_while(self, *names):
while self._match(*names):
self.stack.pop()
def _pop_until(self, *names):
while self._nomatch(*names):
self.stack.pop()
def _append(self, name, level):
self.stack.append(Marker(name, level))
def _append_unless(self, name, level):
if self._nomatch(name):
self.stack.append(Marker(name, level))
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)
assert tokens
start = int(self._is_indent(tokens[0]))
end = len(tokens) - 1
while end > 0 and self._is_ignored(tokens[end]):
end -= 1
for i in range(0, end + 1 - start):
token = tokens[start + i]
if self._is_ignored(token):
pass
elif self._is_close_token(token):
self._handle_close_token(y, tokens, start, end, i, token)
elif self._is_open_token(token):
self._handle_open_token(y, tokens, start, end, i, token)
else:
self._handle_other_token(y, tokens, start, end, i, token)
self.lines[y] = self.curr_level
self.record[y] = tuple(self.stack)
def _is_indent(self, token):
return token.name == 'spaces'
def _is_ignored(self, token):
return token.name in ('spaces', 'eol', 'comment')
def _is_close_token(self, token):
return token.name == 'delimiter' and token.string in self.close_tokens
def _handle_close_token(self, y, tokens, start, end, i, token):
if not self.stack:
raise Exception, "unmatched %r, line %d" % (token.string, y)
while True:
marker = self.stack[-1]
if marker.name in ('control', 'continue'):
self.stack.pop()
elif marker.name in self.open_tokens:
if self.open_tokens[marker.name] == token.string:
self.stack.pop()
break
else:
raise Exception, "mismatched %r, line %d (expected %r)" % \
(token.string, y, d[marker.name])
else:
raise Exception, "what? %r" % marker.name
if i == 0:
self._save_curr_level()
def _is_open_token(self, token):
return token.name == 'delimiter' and token.string in self.open_tokens
def _handle_open_token(self, y, tokens, start, end, i, token):
if i == 0 and self.stack and self.stack[-1].name == 'continue':
self.stack.pop()
if token.string == '{':
self._pop_while('continue', 'control')
if i == end - start:
level = self._get_next_level()
else:
level = tokens[i + 1].x
self._append(token.string, level)
def _handle_other_token(self, y, tokens, start, end, i, token):
name, s = token.name, token.string
if i + start == end:
self._pop_while('continue', 'control')
if name == 'continuation':
self._append_unless('continue', self._get_next_level())
elif name == 'keyword' and s in ('if', 'else', 'while', 'do', 'for'):
if i == start:
self._save_curr_level()
self._append_unless('control', self._get_next_level())
class Awk(mode.Fundamental):
tabbercls = AwkTabber
modename = 'awk'
extensions = ['.awk']
grammar = AwkGrammar
colors = {
'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