2008-10-01 16:58:43 -04:00
|
|
|
import commands
|
|
|
|
import color, mode, tab
|
|
|
|
from lex import Grammar, PatternRule, RegionRule
|
|
|
|
from mode.python import StringGrammar2
|
2008-10-02 09:35:28 -04:00
|
|
|
from tab import Marker
|
2008-10-01 16:58:43 -04:00
|
|
|
|
2008-10-01 18:10:11 -04:00
|
|
|
class RegexGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
PatternRule(r'escaped', r'\\.'),
|
|
|
|
PatternRule(r'data', r'[^\\/]+'),
|
|
|
|
]
|
|
|
|
|
2008-10-01 16:58:43 -04:00
|
|
|
class AwkGrammar(Grammar):
|
|
|
|
rules = [
|
2008-10-01 18:10:11 -04:00
|
|
|
PatternRule(r'comment', r'#.*\n$'),
|
|
|
|
PatternRule(r'spaces', r' +'),
|
2008-10-01 23:59:30 -04:00
|
|
|
RegionRule(r'awk_regex', r'/(?! )', RegexGrammar, r'/'),
|
2008-10-01 18:10:11 -04:00
|
|
|
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_])'),
|
2008-10-01 23:59:30 -04:00
|
|
|
PatternRule(r'delimiter', r'(?:[\{\}()\[\]?:;,]|=(?!=)|\+=|-=|\*=|/=|\%=|\^=)'),
|
2008-10-02 09:35:28 -04:00
|
|
|
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_])'),
|
2008-10-01 23:59:30 -04:00
|
|
|
|
|
|
|
PatternRule(r'awk_field', r'\$\d*'),
|
2008-10-01 18:15:52 -04:00
|
|
|
|
|
|
|
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]+'),
|
|
|
|
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r'unop', r'!(?![=~])|--|\+\+'),
|
|
|
|
PatternRule(r'binop', r'(?:&&|\|\||<=|>=|!=|!~|==|\^|%|[-~/+*<>])'),
|
|
|
|
RegionRule(r'string', r'"', StringGrammar2, r'"'),
|
2008-10-01 23:59:30 -04:00
|
|
|
PatternRule(r'awk_function', r'[a-zA-Z_][a-zA-Z0-9_]*(?=\()'),
|
|
|
|
PatternRule(r'awk_identifier', r'[a-zA-Z_][a-zA-Z0-9_]*'),
|
2008-10-01 18:15:52 -04:00
|
|
|
|
2008-10-01 23:59:30 -04:00
|
|
|
PatternRule(r'continuation', r'\\\n$'),
|
2008-10-01 18:10:11 -04:00
|
|
|
PatternRule(r'eol', r'\n'),
|
2008-10-01 16:58:43 -04:00
|
|
|
]
|
|
|
|
|
2008-10-01 23:59:30 -04:00
|
|
|
class AwkTabber(tab.StackTabber):
|
2008-10-02 09:35:28 -04:00
|
|
|
open_tokens = {'{': '}', '(': ')', '[': ']'}
|
|
|
|
close_tokens = {'}': '{', ')': '(', ']': '['}
|
2008-10-01 23:59:30 -04:00
|
|
|
def __init__(self, m):
|
|
|
|
self.mode = m
|
2008-10-02 09:35:28 -04:00
|
|
|
self.name = m.name()
|
2008-10-01 23:59:30 -04:00
|
|
|
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
|
2008-10-02 09:35:28 -04:00
|
|
|
while not self._is_base(y) and y > 0:
|
2008-10-01 23:59:30 -04:00
|
|
|
y -= 1
|
|
|
|
self._reset()
|
|
|
|
while y <= target:
|
2008-10-02 09:35:28 -04:00
|
|
|
self._save_curr_level()
|
2008-10-01 23:59:30 -04:00
|
|
|
self._handle_tokens(y)
|
2008-10-02 09:35:28 -04:00
|
|
|
y += 1
|
2008-10-01 23:59:30 -04:00
|
|
|
|
|
|
|
def _is_base(self, y):
|
2008-10-02 09:35:28 -04:00
|
|
|
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
|
|
|
|
|
2008-10-01 23:59:30 -04:00
|
|
|
def _reset(self):
|
2008-10-02 09:35:28 -04:00
|
|
|
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
|
2008-10-01 23:59:30 -04:00
|
|
|
else:
|
2008-10-02 09:35:28 -04:00
|
|
|
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))
|
|
|
|
|
2008-10-01 23:59:30 -04:00
|
|
|
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)
|
2008-10-02 09:35:28 -04:00
|
|
|
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):
|
2008-10-01 23:59:30 -04:00
|
|
|
pass
|
|
|
|
elif self._is_close_token(token):
|
2008-10-02 09:35:28 -04:00
|
|
|
self._handle_close_token(y, tokens, start, end, i, token)
|
2008-10-01 23:59:30 -04:00
|
|
|
elif self._is_open_token(token):
|
2008-10-02 09:35:28 -04:00
|
|
|
self._handle_open_token(y, tokens, start, end, i, token)
|
2008-10-01 23:59:30 -04:00
|
|
|
else:
|
2008-10-02 09:35:28 -04:00
|
|
|
self._handle_other_token(y, tokens, start, end, i, token)
|
|
|
|
self.lines[y] = self.curr_level
|
|
|
|
self.record[y] = tuple(self.stack)
|
2008-10-01 23:59:30 -04:00
|
|
|
|
|
|
|
def _is_indent(self, token):
|
2008-10-02 09:35:28 -04:00
|
|
|
return token.name == 'spaces'
|
2008-10-01 23:59:30 -04:00
|
|
|
def _is_ignored(self, token):
|
2008-10-02 09:35:28 -04:00
|
|
|
return token.name in ('spaces', 'eol', 'comment')
|
2008-10-01 23:59:30 -04:00
|
|
|
|
|
|
|
def _is_close_token(self, token):
|
2008-10-02 09:35:28 -04:00
|
|
|
return token.name == 'delimiter' and token.string in self.close_tokens
|
|
|
|
def _handle_close_token(self, y, tokens, start, end, i, token):
|
2008-10-01 23:59:30 -04:00
|
|
|
if not self.stack:
|
2008-10-02 09:35:28 -04:00
|
|
|
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())
|
2008-10-01 23:59:30 -04:00
|
|
|
|
2008-10-01 16:58:43 -04:00
|
|
|
class Awk(mode.Fundamental):
|
2008-10-02 09:35:28 -04:00
|
|
|
tabbercls = AwkTabber
|
2008-10-01 16:58:43 -04:00
|
|
|
modename = 'awk'
|
|
|
|
extensions = ['.awk']
|
|
|
|
grammar = AwkGrammar
|
|
|
|
colors = {
|
2008-10-01 23:59:30 -04:00
|
|
|
'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'),
|
2008-10-01 16:58:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
install = Awk.install
|