2007-07-17 07:14:21 -04:00
|
|
|
import curses, re
|
2007-07-15 15:07:36 -04:00
|
|
|
import regex, util
|
2007-10-21 20:50:11 -04:00
|
|
|
from point import Point
|
2007-07-14 16:59:38 -04:00
|
|
|
|
|
|
|
class Token(object):
|
2007-07-17 07:14:21 -04:00
|
|
|
def __init__(self, name, rule, y, x, s, color=None, parent=None, matchd={}, link=None):
|
|
|
|
self.name = name
|
|
|
|
self.rule = rule
|
|
|
|
self.y = y
|
|
|
|
self.x = x
|
|
|
|
self.string = s
|
|
|
|
self.color = color
|
|
|
|
self.parent = parent
|
|
|
|
self.matchd = matchd
|
|
|
|
self.link = link
|
2007-07-15 09:44:20 -04:00
|
|
|
assert parent is None or hasattr(parent, 'name'), 'oh no %r' % parent
|
2007-07-14 16:59:38 -04:00
|
|
|
def parents(self):
|
|
|
|
if self.parent is not None:
|
|
|
|
parents = self.parent.parents()
|
|
|
|
parents.append(self.parent)
|
|
|
|
return parents
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
def domain(self):
|
|
|
|
if self.parent is not None:
|
|
|
|
names = self.parent.domain()
|
|
|
|
else:
|
|
|
|
names = []
|
2007-07-15 15:07:36 -04:00
|
|
|
if self.link and not self.link.startswith('middle'):
|
2007-07-14 16:59:38 -04:00
|
|
|
names.append(self.rule.name)
|
|
|
|
return names
|
|
|
|
def fqlist(self):
|
|
|
|
if self.parent is not None:
|
|
|
|
names = self.parent.domain()
|
|
|
|
else:
|
|
|
|
names = []
|
2007-07-15 15:07:36 -04:00
|
|
|
if self.link == 'start':
|
2007-07-14 16:59:38 -04:00
|
|
|
names.append(self.rule.name)
|
|
|
|
names.append(self.name)
|
|
|
|
return names
|
|
|
|
def fqname(self):
|
|
|
|
names = self.fqlist()
|
|
|
|
return '.'.join(names)
|
|
|
|
def copy(self):
|
|
|
|
return Token(self.name, self.rule, self.y, self.x, self.string,
|
2007-07-17 07:14:21 -04:00
|
|
|
self.color, self.parent, self.matchd, self.link)
|
2007-07-14 16:59:38 -04:00
|
|
|
def add_to_string(self, s):
|
|
|
|
self.string += s
|
|
|
|
def end_x(self):
|
|
|
|
return self.x + len(self.string)
|
|
|
|
def __eq__(self, other):
|
|
|
|
return (self.y == other.y and self.x == other.x
|
|
|
|
and self.name == other.name and self.parent is other.parent and
|
|
|
|
self.string == other.string)
|
|
|
|
def __repr__(self):
|
|
|
|
if len(self.string) < 10:
|
|
|
|
s = self.string
|
|
|
|
else:
|
|
|
|
s = self.string[:10] + '...'
|
|
|
|
fields = (self.fqname(), self.rule, self.y, self.x, s)
|
|
|
|
return "<Token(%r, %r, %d, %d, %r)>" % fields
|
|
|
|
|
|
|
|
class Rule:
|
|
|
|
reflags = 0
|
2007-07-15 15:07:36 -04:00
|
|
|
def __init__(self, name):
|
|
|
|
assert regex.valid_token_name.match(name), 'invalid name %r' % name
|
|
|
|
assert not regex.reserved_token_names.match(name), \
|
|
|
|
"rule name %r is reserved and thus illegal" % name
|
2007-07-14 16:59:38 -04:00
|
|
|
self.name = name
|
|
|
|
def match(self, lexer, parent):
|
|
|
|
raise Exception, "not implemented"
|
|
|
|
def lex(self, lexer, parent, match):
|
|
|
|
raise Exception, "not implemented"
|
2007-07-15 15:07:36 -04:00
|
|
|
def make_token(self, lexer, s, name, parent=None, matchd={}, link=None):
|
2007-07-17 07:14:21 -04:00
|
|
|
t = Token(name, self, lexer.y, lexer.x, s, None, parent, matchd, link)
|
|
|
|
t.color = lexer.get_color(t)
|
2007-07-14 16:59:38 -04:00
|
|
|
lexer.x += len(s)
|
2007-07-24 09:05:50 -04:00
|
|
|
if lexer.x > len(lexer.lines[lexer.y]):
|
2007-07-23 15:28:27 -04:00
|
|
|
lexer.x = 0
|
|
|
|
lexer.y += 1
|
2007-07-14 16:59:38 -04:00
|
|
|
return t
|
2007-07-24 09:05:50 -04:00
|
|
|
def get_line(self, lexer, y=None):
|
|
|
|
if y is None:
|
|
|
|
return lexer.lines[lexer.y] + '\n'
|
|
|
|
else:
|
|
|
|
return lexer.lines[y] + '\n'
|
2007-07-14 16:59:38 -04:00
|
|
|
|
|
|
|
class PatternRule(Rule):
|
2007-07-15 15:07:36 -04:00
|
|
|
def __init__(self, name, pattern):
|
2007-07-14 16:59:38 -04:00
|
|
|
Rule.__init__(self, name)
|
|
|
|
self.pattern = pattern
|
|
|
|
self.re = re.compile(self.pattern, self.reflags)
|
|
|
|
def match(self, lexer, parent):
|
|
|
|
return self.re.match(self.get_line(lexer), lexer.x)
|
|
|
|
def lex(self, lexer, parent, m):
|
|
|
|
if m:
|
2007-07-15 10:26:28 -04:00
|
|
|
yield self.make_token(lexer, m.group(0), self.name, parent, m.groupdict())
|
2007-07-14 16:59:38 -04:00
|
|
|
raise StopIteration
|
|
|
|
class NocasePatternRule(PatternRule):
|
|
|
|
reflags = re.IGNORECASE
|
|
|
|
|
2007-07-17 07:14:21 -04:00
|
|
|
class OverrideError(Exception):
|
|
|
|
pass
|
2007-07-15 20:07:26 -04:00
|
|
|
class OverridePatternRule(PatternRule):
|
|
|
|
def lex(self, lexer, parent, m):
|
|
|
|
if m:
|
|
|
|
d = m.groupdict()
|
2007-07-16 18:43:11 -04:00
|
|
|
if lexer.action == 'lex':
|
|
|
|
a = lexer.mode.window.application
|
|
|
|
try:
|
2007-07-17 07:14:21 -04:00
|
|
|
names = d['mode'].split('.')
|
|
|
|
modecls = a.globals()[names.pop(0)]
|
2007-07-16 18:43:11 -04:00
|
|
|
for name in names:
|
2007-07-17 07:14:21 -04:00
|
|
|
modecls = getattr(modecls, name)
|
|
|
|
mode = modecls(None)
|
|
|
|
if hasattr(mode, 'grammar') and hasattr(mode, 'colors'):
|
|
|
|
lexer.mode.gstack['%s.start' % d['token']] = mode
|
|
|
|
else:
|
|
|
|
raise OverrideError, "argh: %r" % mode
|
|
|
|
except (KeyError, AttributeError, OverrideError):
|
2007-07-16 18:43:11 -04:00
|
|
|
pass
|
2007-07-15 20:07:26 -04:00
|
|
|
yield self.make_token(lexer, m.group(0), self.name, parent, d)
|
|
|
|
raise StopIteration
|
|
|
|
|
2007-07-14 16:59:38 -04:00
|
|
|
class ContextPatternRule(PatternRule):
|
2007-07-15 15:07:36 -04:00
|
|
|
def __init__(self, name, pattern, fallback):
|
2007-07-14 16:59:38 -04:00
|
|
|
Rule.__init__(self, name)
|
|
|
|
self.pattern = pattern
|
|
|
|
self.fallback_re = re.compile(fallback, self.reflags)
|
|
|
|
def match(self, lexer, parent):
|
|
|
|
try:
|
|
|
|
r = re.compile(self.pattern % parent.matchd)
|
|
|
|
except KeyError:
|
|
|
|
r = self.fallback_re
|
|
|
|
return r.match(self.get_line(lexer), lexer.x)
|
|
|
|
class NocaseContextPatternRule(ContextPatternRule):
|
|
|
|
reflags = re.IGNORECASE
|
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
class PatternGroupRule(PatternRule):
|
|
|
|
def __init__(self, name, *args):
|
|
|
|
assert args and len(args) % 2 == 0
|
|
|
|
i = 0
|
|
|
|
pairs = []
|
|
|
|
while i < len(args):
|
|
|
|
tokname, pattern = args[i], args[i+1]
|
2007-07-19 14:28:21 -04:00
|
|
|
pairs.append((tokname, re.compile(pattern, self.reflags)))
|
2007-07-15 15:07:36 -04:00
|
|
|
i += 2
|
|
|
|
Rule.__init__(self, name)
|
|
|
|
self.pairs = tuple(pairs)
|
|
|
|
def match(self, lexer, parent):
|
2007-07-23 15:28:27 -04:00
|
|
|
(x, y) = (lexer.x, lexer.y)
|
2007-07-15 15:07:36 -04:00
|
|
|
matches = []
|
|
|
|
for (tokname, tokre) in self.pairs:
|
2007-07-23 15:28:27 -04:00
|
|
|
if y >= len(lexer.lines):
|
|
|
|
return []
|
2007-07-24 09:05:50 -04:00
|
|
|
line = self.get_line(lexer, y)
|
2007-07-23 15:28:27 -04:00
|
|
|
m = tokre.match(line, x)
|
2007-07-15 15:07:36 -04:00
|
|
|
if m:
|
|
|
|
x += len(m.group(0))
|
2007-07-23 15:28:27 -04:00
|
|
|
if x >= len(line):
|
|
|
|
x = 0
|
|
|
|
y += 1
|
2007-07-15 15:07:36 -04:00
|
|
|
matches.append((tokname, m))
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
assert len(matches) == len(self.pairs)
|
|
|
|
return matches
|
|
|
|
def lex(self, lexer, parent, matches):
|
|
|
|
if matches:
|
|
|
|
for (tokname, m) in matches:
|
|
|
|
yield self.make_token(lexer, m.group(0), tokname, parent, m.groupdict())
|
|
|
|
raise StopIteration
|
|
|
|
|
2007-07-14 16:59:38 -04:00
|
|
|
class RegionRule(Rule):
|
2007-07-15 15:07:36 -04:00
|
|
|
def __init__(self, name, *args):
|
2007-07-14 16:59:38 -04:00
|
|
|
Rule.__init__(self, name)
|
2007-07-15 15:07:36 -04:00
|
|
|
assert len(args) > 1
|
|
|
|
args = list(args)
|
|
|
|
self.pairs = []
|
|
|
|
self.start_re = re.compile(args.pop(0), self.reflags)
|
|
|
|
while len(args) > 1:
|
|
|
|
grammar = args.pop(0)
|
|
|
|
pattern = args.pop(0)
|
|
|
|
assert hasattr(grammar, 'rules'), repr(grammar)
|
|
|
|
assert type(pattern) == type(''), repr(pattern)
|
|
|
|
self.pairs.append((grammar, pattern))
|
|
|
|
if len(args) == 1:
|
|
|
|
self.pairs.append((grammar, None))
|
2007-07-15 20:07:26 -04:00
|
|
|
|
2007-07-14 16:59:38 -04:00
|
|
|
def match(self, lexer, parent):
|
|
|
|
return self.start_re.match(self.get_line(lexer), lexer.x)
|
|
|
|
def lex(self, lexer, parent, m):
|
2007-07-15 15:07:36 -04:00
|
|
|
assert m
|
|
|
|
# ok, so since we had a match, we need to create our start token, who
|
|
|
|
# will be the ancestor to all other tokens matched in this region
|
|
|
|
matchd = m.groupdict()
|
|
|
|
parent = self.make_token(lexer, m.group(0), 'start', parent, matchd, 'start')
|
|
|
|
yield parent
|
|
|
|
|
|
|
|
# now we will loop over the different pairs of grammars/stop-patterns in
|
|
|
|
# this region, and return the resulting token; we start at 0
|
|
|
|
for tok in self._lex_loop(lexer, [parent], matchd, 0):
|
|
|
|
yield tok
|
2007-07-14 19:02:42 -04:00
|
|
|
raise StopIteration
|
2007-07-15 09:24:25 -04:00
|
|
|
def resume(self, lexer, toresume):
|
2007-07-15 15:07:36 -04:00
|
|
|
assert toresume, "can't resume without tokens to resume!"
|
|
|
|
# ok, so we need to figure out in which of the grammars of our region
|
|
|
|
# we are resuming. to do this we calculate i, a position in our list
|
|
|
|
# of grammar/stop-pattern pairs
|
|
|
|
if toresume[0].link == 'start':
|
|
|
|
i = 0
|
2007-07-14 16:59:38 -04:00
|
|
|
else:
|
2007-07-15 15:07:36 -04:00
|
|
|
m = regex.middle_token_name.match(toresume[0].link)
|
|
|
|
assert m
|
|
|
|
i = int(m.group(1)) + 1
|
|
|
|
assert i > 0 and i < len(self.pairs)
|
|
|
|
|
|
|
|
# now we will loop over the different pairs of grammars/stop-patterns in
|
|
|
|
# this region, and return the resulting token; we start at i
|
|
|
|
for tok in self._lex_loop(lexer, toresume, toresume[0].matchd, i):
|
|
|
|
yield tok
|
2007-07-15 09:24:25 -04:00
|
|
|
raise StopIteration
|
2007-07-15 15:07:36 -04:00
|
|
|
def _lex_loop(self, lexer, toresume, matchd, i):
|
|
|
|
# we need to loop over our grammar/stop-pattern pairs
|
|
|
|
while i < len(self.pairs):
|
|
|
|
# for each one, we will compile our stop-regex, and figure out the
|
|
|
|
# name of the stop token to be created if this stop-regex matches.
|
2007-07-15 20:07:26 -04:00
|
|
|
fqname = toresume[0].fqname()
|
2007-07-16 18:43:11 -04:00
|
|
|
p = Point(toresume[0].x, toresume[0].y)
|
|
|
|
if fqname in lexer.mode.ghist and p in lexer.mode.ghist[fqname]:
|
2007-07-17 07:14:21 -04:00
|
|
|
mode = lexer.mode.ghist[fqname][p]
|
|
|
|
grammar = mode.grammar
|
2007-07-16 18:43:11 -04:00
|
|
|
elif fqname in lexer.mode.gstack:
|
2007-07-17 07:14:21 -04:00
|
|
|
mode = lexer.mode.gstack[fqname]
|
|
|
|
grammar = mode.grammar
|
2007-07-16 18:43:11 -04:00
|
|
|
lexer.mode.ghist.setdefault(fqname, {})
|
2007-07-17 07:14:21 -04:00
|
|
|
lexer.mode.ghist[fqname][p] = mode
|
2007-07-16 18:43:11 -04:00
|
|
|
del lexer.mode.gstack[fqname]
|
2007-07-15 20:07:26 -04:00
|
|
|
else:
|
2007-07-17 07:14:21 -04:00
|
|
|
mode = lexer.mode
|
2007-07-15 20:07:26 -04:00
|
|
|
grammar = self.pairs[i][0]
|
2007-07-17 07:14:21 -04:00
|
|
|
lexer.mstack.append(mode)
|
2007-07-15 20:07:26 -04:00
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
if self.pairs[i][1]:
|
|
|
|
stopre = re.compile(self.pairs[i][1] % matchd, self.reflags)
|
|
|
|
else:
|
|
|
|
stopre = None
|
|
|
|
if i == len(self.pairs) - 1:
|
|
|
|
tokname = 'end'
|
|
|
|
else:
|
|
|
|
tokname = 'middle%d' % i
|
|
|
|
|
|
|
|
# ok, so now loop over all the tokens in the current grammar, until
|
|
|
|
# the stop-token (if any) is found, and return each result as we get
|
|
|
|
# it.
|
2007-07-24 17:22:35 -04:00
|
|
|
tok = None
|
2007-07-15 15:07:36 -04:00
|
|
|
for tok in self._lex(lexer, toresume, tokname, stopre, grammar):
|
|
|
|
yield tok
|
2007-07-14 16:59:38 -04:00
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
# ok, so now either we found the stop-token, and have a new parent
|
|
|
|
# for future tokens (if any), or we are done.
|
2007-07-24 17:22:35 -04:00
|
|
|
if tok is not None and tok.name == tokname:
|
2007-07-15 15:07:36 -04:00
|
|
|
toresume = [tok]
|
|
|
|
matchd.update(tok.matchd)
|
|
|
|
else:
|
|
|
|
raise StopIteration
|
2007-07-17 07:14:21 -04:00
|
|
|
# this should have already gotten done by _lex
|
|
|
|
#lexer.mstack.pop(-1)
|
2007-07-15 15:07:36 -04:00
|
|
|
i += 1
|
|
|
|
# assuming we make it through all our grammars, and find the end-token,
|
|
|
|
# then we need to signal that we are done.
|
|
|
|
raise StopIteration
|
2007-07-15 10:26:28 -04:00
|
|
|
def _lex(self, lexer, toresume, stopname, stopre, grammar):
|
2007-07-15 09:44:20 -04:00
|
|
|
assert toresume
|
2007-07-15 09:24:25 -04:00
|
|
|
parent = toresume[0]
|
|
|
|
reenter = len(toresume) > 1
|
|
|
|
null_t = None
|
2007-07-14 16:59:38 -04:00
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
# ok, so there are only two way we want to exit this loop: either we
|
|
|
|
# lex the whole document, or we encounter the stop-token.
|
2007-07-14 16:59:38 -04:00
|
|
|
done = False
|
|
|
|
while not done and lexer.y < len(lexer.lines):
|
2007-07-15 15:07:36 -04:00
|
|
|
line = self.get_line(lexer)
|
2007-07-14 16:59:38 -04:00
|
|
|
old_y = lexer.y
|
2007-07-14 19:02:42 -04:00
|
|
|
while not done and lexer.y == old_y and lexer.x < len(line):
|
2007-07-15 15:07:36 -04:00
|
|
|
# ok, so reenter gets priority, since the current input might be
|
|
|
|
# intended for nested grammar. so handle it here
|
2007-07-14 16:59:38 -04:00
|
|
|
if reenter:
|
|
|
|
reenter = False
|
|
|
|
for t in toresume[1].rule.resume(lexer, toresume[1:]):
|
|
|
|
yield t
|
2007-07-15 15:07:36 -04:00
|
|
|
# since we might have changed our x/y coordinates, we need to
|
|
|
|
# do some checks here, and maybe finish or change our coordintes
|
2007-07-14 16:59:38 -04:00
|
|
|
if lexer.y >= len(lexer.lines):
|
|
|
|
raise StopIteration
|
2007-07-14 19:02:42 -04:00
|
|
|
elif lexer.x >= len(line):
|
2007-07-14 16:59:38 -04:00
|
|
|
lexer.y += 1
|
|
|
|
lexer.x = 0
|
2007-07-15 15:07:36 -04:00
|
|
|
|
|
|
|
# ok, so get the *now* current line
|
2007-07-14 16:59:38 -04:00
|
|
|
line = self.get_line(lexer)
|
2007-07-14 19:02:42 -04:00
|
|
|
|
2007-07-15 09:24:25 -04:00
|
|
|
if stopre:
|
2007-07-15 15:07:36 -04:00
|
|
|
# if we are looking for a stop-token, do that check now
|
2007-07-15 09:24:25 -04:00
|
|
|
m = stopre.match(line, lexer.x)
|
2007-07-14 16:59:38 -04:00
|
|
|
if m:
|
|
|
|
if null_t:
|
2007-07-15 15:07:36 -04:00
|
|
|
# if we have a null token waiting, return it first.
|
2007-07-14 16:59:38 -04:00
|
|
|
yield null_t
|
|
|
|
null_t = None
|
2007-07-15 15:07:36 -04:00
|
|
|
# ok, now return the stop-token, and signal that we are
|
|
|
|
# done and no more input is to be consumed
|
2007-07-17 07:14:21 -04:00
|
|
|
lexer.mstack.pop(-1)
|
2007-07-15 15:07:36 -04:00
|
|
|
yield self.make_token(lexer, m.group(0), stopname,
|
|
|
|
parent, m.groupdict(), stopname)
|
2007-07-14 16:59:38 -04:00
|
|
|
done = True
|
|
|
|
break
|
|
|
|
|
2007-07-15 09:24:25 -04:00
|
|
|
m = None
|
2007-07-15 15:07:36 -04:00
|
|
|
# start checking our rules to see if we can match the input
|
2007-07-15 10:26:28 -04:00
|
|
|
for rule in grammar.rules:
|
2007-07-14 16:59:38 -04:00
|
|
|
m = rule.match(lexer, parent)
|
|
|
|
if m:
|
2007-07-15 15:07:36 -04:00
|
|
|
# ok great, we have a match
|
2007-07-14 16:59:38 -04:00
|
|
|
if null_t:
|
2007-07-15 15:07:36 -04:00
|
|
|
# if we have a null token waiting, return it first.
|
2007-07-14 16:59:38 -04:00
|
|
|
yield null_t
|
|
|
|
null_t = None
|
2007-07-15 15:07:36 -04:00
|
|
|
# ok, now for every token this rules has created, we
|
|
|
|
# return them, one by one.
|
2007-07-14 16:59:38 -04:00
|
|
|
for t in rule.lex(lexer, parent, m):
|
|
|
|
yield t
|
|
|
|
break
|
|
|
|
|
2007-07-15 09:24:25 -04:00
|
|
|
if not m:
|
2007-07-15 15:07:36 -04:00
|
|
|
# we didn't find a match on a rule, so add this character to
|
|
|
|
# the current null token (creating a new one if necessary);
|
|
|
|
if not null_t:
|
2007-07-17 07:14:21 -04:00
|
|
|
null_t = Token('null', None, lexer.y, lexer.x, '', None, parent)
|
|
|
|
null_t.color = lexer.get_color(null_t)
|
2007-07-15 15:07:36 -04:00
|
|
|
null_t.add_to_string(line[lexer.x])
|
|
|
|
lexer.x += 1
|
|
|
|
|
|
|
|
# ok, we are at the end of a line of input. so, if we have a null
|
|
|
|
# token waiting, now is the time to return it
|
2007-07-14 16:59:38 -04:00
|
|
|
if null_t:
|
|
|
|
yield null_t
|
|
|
|
null_t = None
|
|
|
|
if not done and old_y == lexer.y:
|
|
|
|
lexer.y += 1
|
|
|
|
lexer.x = 0
|
|
|
|
raise StopIteration
|
|
|
|
class NocaseRegionRule(RegionRule):
|
|
|
|
reflags = re.IGNORECASE
|
|
|
|
|
|
|
|
class Grammar:
|
|
|
|
rules = []
|
|
|
|
grammar = Grammar()
|
|
|
|
|
|
|
|
class Lexer:
|
2007-07-15 20:07:26 -04:00
|
|
|
def __init__(self, mode, grammar):
|
2007-07-16 18:43:11 -04:00
|
|
|
self.mode = mode
|
2007-07-17 07:14:21 -04:00
|
|
|
self.mstack = []
|
2007-07-16 18:43:11 -04:00
|
|
|
self.grammar = grammar
|
|
|
|
self.y = 0
|
|
|
|
self.x = 0
|
|
|
|
self.lines = None
|
2007-07-15 10:26:28 -04:00
|
|
|
assert self.grammar.rules
|
2007-07-15 09:24:25 -04:00
|
|
|
def get_line(self):
|
2007-07-15 09:44:20 -04:00
|
|
|
return self.lines[self.y] + '\n'
|
2007-07-14 16:59:38 -04:00
|
|
|
def lex(self, lines, y=0, x=0):
|
2007-07-16 18:43:11 -04:00
|
|
|
self.action = 'lex'
|
|
|
|
self.y = y
|
|
|
|
self.x = x
|
|
|
|
self.lines = lines
|
2007-07-17 07:14:21 -04:00
|
|
|
self.mstack = []
|
2007-07-16 18:43:11 -04:00
|
|
|
self.mode.ghist = {}
|
|
|
|
self.mode.gstack = {}
|
2007-07-15 09:24:25 -04:00
|
|
|
for t in self._lex():
|
|
|
|
yield t
|
2007-07-16 18:43:11 -04:00
|
|
|
del self.action
|
2007-07-15 09:24:25 -04:00
|
|
|
raise StopIteration
|
2007-07-14 16:59:38 -04:00
|
|
|
def resume(self, lines, y, x, token):
|
2007-07-15 20:07:26 -04:00
|
|
|
self.action = 'resume'
|
2007-07-14 16:59:38 -04:00
|
|
|
self.y = y
|
|
|
|
self.x = x
|
|
|
|
self.lines = lines
|
2007-07-17 07:14:21 -04:00
|
|
|
self.mstack = []
|
|
|
|
toresume = token.parents()
|
2007-07-14 16:59:38 -04:00
|
|
|
|
2007-07-15 09:24:25 -04:00
|
|
|
i = 1
|
2007-07-14 16:59:38 -04:00
|
|
|
while i < len(toresume):
|
2007-07-15 15:07:36 -04:00
|
|
|
if toresume[i].link and toresume[i].link != 'start':
|
2007-07-14 16:59:38 -04:00
|
|
|
del toresume[i-1]
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if toresume:
|
2007-07-14 19:02:42 -04:00
|
|
|
for t in toresume[0].rule.resume(self, toresume):
|
|
|
|
yield t
|
|
|
|
for t in self._lex():
|
|
|
|
yield t
|
2007-07-16 18:43:11 -04:00
|
|
|
del self.action
|
2007-07-14 19:02:42 -04:00
|
|
|
raise StopIteration
|
2007-07-14 16:59:38 -04:00
|
|
|
|
2007-07-15 09:24:25 -04:00
|
|
|
def _lex(self):
|
2007-07-15 09:44:20 -04:00
|
|
|
parent = None
|
2007-07-14 16:59:38 -04:00
|
|
|
while self.y < len(self.lines):
|
2007-07-15 10:26:28 -04:00
|
|
|
null_t = None
|
2007-07-15 09:24:25 -04:00
|
|
|
line = self.get_line()
|
2007-07-15 09:44:20 -04:00
|
|
|
while self.x < len(line):
|
2007-07-15 09:24:25 -04:00
|
|
|
m = None
|
2007-07-14 16:59:38 -04:00
|
|
|
for rule in self.grammar.rules:
|
2007-07-15 09:24:25 -04:00
|
|
|
m = rule.match(self, parent)
|
|
|
|
if m:
|
|
|
|
if null_t:
|
|
|
|
yield null_t
|
|
|
|
null_t = None
|
|
|
|
for t in rule.lex(self, parent, m):
|
|
|
|
yield t
|
|
|
|
break
|
|
|
|
|
2007-07-15 15:07:36 -04:00
|
|
|
if self.y >= len(self.lines):
|
|
|
|
break
|
2007-07-15 10:26:28 -04:00
|
|
|
line = self.get_line()
|
2007-07-15 09:24:25 -04:00
|
|
|
if not m:
|
|
|
|
if self.x < len(line):
|
2007-07-15 10:26:28 -04:00
|
|
|
if null_t is None:
|
2007-07-17 07:14:21 -04:00
|
|
|
null_t = Token('null', None, self.y, self.x, '', None, parent)
|
|
|
|
null_t.color = self.get_color(null_t)
|
2007-07-15 09:24:25 -04:00
|
|
|
null_t.add_to_string(line[self.x])
|
|
|
|
self.x += 1
|
|
|
|
if null_t:
|
|
|
|
yield null_t
|
2007-07-14 16:59:38 -04:00
|
|
|
self.y += 1
|
|
|
|
self.x = 0
|
2007-07-15 09:24:25 -04:00
|
|
|
raise StopIteration
|
2007-07-17 07:14:21 -04:00
|
|
|
|
|
|
|
def get_color(self, token):
|
|
|
|
fqlist = token.fqlist()
|
|
|
|
if self.mstack:
|
|
|
|
mode = self.mstack[-1]
|
|
|
|
else:
|
|
|
|
mode = self.mode
|
2007-07-18 07:22:12 -04:00
|
|
|
v = list(mode.default_color)
|
2007-07-17 07:14:21 -04:00
|
|
|
for j in range(0, len(fqlist)):
|
|
|
|
name = '.'.join(fqlist[j:])
|
|
|
|
if name in mode.colors:
|
2007-07-18 07:22:12 -04:00
|
|
|
assert type(mode.colors[name]) == type(()), repr(mode)
|
|
|
|
v = list(mode.colors[name])
|
2007-07-17 07:14:21 -04:00
|
|
|
break
|
|
|
|
#if DARK_BACKGROUND:
|
|
|
|
if True:
|
2007-07-18 07:22:12 -04:00
|
|
|
v.append('bold')
|
|
|
|
return v
|