branch : pmacs2
This commit is contained in:
moculus 2007-07-16 22:43:11 +00:00
parent a8ecc35701
commit 68d68b51b9
4 changed files with 93 additions and 40 deletions

View File

@ -7,7 +7,11 @@ sub foo {
unless 9 && 3; unless 9 && 3;
} }
#@@:string:mode_sql.SqlGrammar #@@:heredoc:mode_sql.SqlGrammar
my $s = <<EOT;
drop table foog;
EOT
my $foo = { my $foo = {
'drop table ', 'drop table ',
'bar', 'bar',

70
lex3.py
View File

@ -1,5 +1,6 @@
import re import re
import regex, util import regex, util
from point2 import Point
class Token(object): class Token(object):
def __init__(self, name, rule, y, x, s, parent=None, matchd={}, link=None): def __init__(self, name, rule, y, x, s, parent=None, matchd={}, link=None):
@ -11,8 +12,7 @@ class Token(object):
self.parent = parent self.parent = parent
self.matchd = matchd self.matchd = matchd
self.link = link self.link = link
self.loverride = None #self.loverride = None
self.goverride = None
assert parent is None or hasattr(parent, 'name'), 'oh no %r' % parent assert parent is None or hasattr(parent, 'name'), 'oh no %r' % parent
def parents(self): def parents(self):
if self.parent is not None: if self.parent is not None:
@ -95,17 +95,18 @@ class NocasePatternRule(PatternRule):
class OverridePatternRule(PatternRule): class OverridePatternRule(PatternRule):
def lex(self, lexer, parent, m): def lex(self, lexer, parent, m):
if m: if m:
a = lexer.mode.window.application
d = m.groupdict() d = m.groupdict()
try: if lexer.action == 'lex':
names = d['grammar'].split('.') a = lexer.mode.window.application
grammar = a.globals()[names.pop(0)] try:
for name in names: names = d['grammar'].split('.')
grammar = getattr(grammar, name) grammar = a.globals()[names.pop(0)]
lexer.loverride['%s.start' % d['token']] = grammar for name in names:
except Exception: grammar = getattr(grammar, name)
#raise lexer.mode.gstack['%s.start' % d['token']] = grammar
pass except Exception:
#raise
pass
yield self.make_token(lexer, m.group(0), self.name, parent, d) yield self.make_token(lexer, m.group(0), self.name, parent, d)
raise StopIteration raise StopIteration
@ -208,17 +209,14 @@ class RegionRule(Rule):
# for each one, we will compile our stop-regex, and figure out the # 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. # name of the stop token to be created if this stop-regex matches.
fqname = toresume[0].fqname() fqname = toresume[0].fqname()
if fqname in lexer.loverride: p = Point(toresume[0].x, toresume[0].y)
grammar = lexer.loverride[fqname] if fqname in lexer.mode.ghist and p in lexer.mode.ghist[fqname]:
del lexer.loverride[fqname] grammar = lexer.mode.ghist[fqname][p]
#toresume[0].loverride = grammar elif fqname in lexer.mode.gstack:
elif fqname in lexer.goverride: grammar = lexer.mode.gstack[fqname]
grammar = lexer.goverride[fqname] lexer.mode.ghist.setdefault(fqname, {})
#toresume[0].goverride = grammar lexer.mode.ghist[fqname][p] = grammar
#elif toresume[0].loverride: del lexer.mode.gstack[fqname]
# grammar = toresume[0].loverride
#elif toresume[0].goverride:
# grammar = toresume[0].goverride
else: else:
grammar = self.pairs[i][0] grammar = self.pairs[i][0]
@ -335,24 +333,24 @@ grammar = Grammar()
class Lexer: class Lexer:
def __init__(self, mode, grammar): def __init__(self, mode, grammar):
self.mode = mode self.mode = mode
self.grammar = grammar self.grammar = grammar
self.y = 0 self.y = 0
self.x = 0 self.x = 0
self.lines = None self.lines = None
self.loverride = {}
self.goverride = {}
assert self.grammar.rules assert self.grammar.rules
def get_line(self): def get_line(self):
return self.lines[self.y] + '\n' return self.lines[self.y] + '\n'
def lex(self, lines, y=0, x=0): def lex(self, lines, y=0, x=0):
self.action = 'lex' self.action = 'lex'
self.y = y self.y = y
self.x = x self.x = x
self.lines = lines self.lines = lines
self.loverride = {} self.mode.ghist = {}
self.mode.gstack = {}
for t in self._lex(): for t in self._lex():
yield t yield t
del self.action
raise StopIteration raise StopIteration
def resume(self, lines, y, x, token): def resume(self, lines, y, x, token):
self.action = 'resume' self.action = 'resume'
@ -360,7 +358,6 @@ class Lexer:
self.x = x self.x = x
self.lines = lines self.lines = lines
self.tokens = [] self.tokens = []
self.loverride = {}
toresume = token.parents() toresume = token.parents()
i = 1 i = 1
@ -375,6 +372,7 @@ class Lexer:
yield t yield t
for t in self._lex(): for t in self._lex():
yield t yield t
del self.action
raise StopIteration raise StopIteration
def _lex(self): def _lex(self):

View File

@ -1,6 +1,7 @@
import os, sets, string import os, sets, string
import color, method import color, method
from lex3 import Lexer from lex3 import Lexer
from point2 import Point
DEBUG = False DEBUG = False
@ -157,7 +158,9 @@ class Fundamental(Handler):
# lexing for highlighting, etc. # lexing for highlighting, etc.
if self.grammar: if self.grammar:
self.lexer = Lexer(self, self.grammar) self.lexer = Lexer(self, self.grammar)
self.gstack = {}
self.ghist = {}
# tab handling # tab handling
if self.tabbercls: if self.tabbercls:
@ -196,6 +199,44 @@ class Fundamental(Handler):
def region_added(self, p, newlines): def region_added(self, p, newlines):
if self.tabber is not None: if self.tabber is not None:
self.tabber.region_added(p, newlines) self.tabber.region_added(p, newlines)
if self.lexer:
ydelta = len(newlines) - 1
xdelta = len(newlines[-1])
ghist = {}
for name in self.ghist:
for p2 in self.ghist[name]:
if p2.y > p.y:
newp = Point(p2.x, p2.y + ydelta)
elif ydelta == 0:
newp = Point(p2.x + xdelta, p2.y)
else:
newp = Point(p2.x, p2.y + ydelta)
ghist.setdefault(name, {})
ghist[name][newp] = self.ghist[name][p2]
self.ghist = ghist
def region_removed(self, p1, p2): def region_removed(self, p1, p2):
if self.tabber is not None: if self.tabber is not None:
self.tabber.region_removed(p1, p2) self.tabber.region_removed(p1, p2)
if self.lexer and False:
ydelta = p2.y - p1.y
xdelta = p2.x - p1.x
ghist = {}
for name in self.ghist:
for p in self.ghist[name]:
if p < p1:
newp = p
elif p1 <= p and p < p2:
continue
elif ydelta == 0:
if p.y == p2.y:
newp = Point(p.x - xdelta, p.y)
else:
newp = p
else:
if p.y == p2.y:
newp = Point(p.x - xdelta, p.y - ydelta)
else:
newp = Point(p.x, p.y - ydelta)
ghist.setdefault(name, {})
ghist[name][newp] = self.ghist[name][p]
self.ghist = ghist

View File

@ -21,12 +21,22 @@ class Point(tuple):
'''Returns a tuple (x,y)''' '''Returns a tuple (x,y)'''
return tuple(self) return tuple(self)
def add(self, xdelta, ydelta): def add(self, xdelta, ydelta):
'''Returns a new point, applying xdelta and ydelta''' '''Returns a new point, adding xdelta and ydelta'''
return Point(self[1] + xdelta, self[0] + ydelta) return Point(self[1] + xdelta, self[0] + ydelta)
def vadd(self, xdelta, ydelta): def vadd(self, xdelta, ydelta):
'''Returns a new point. If ydelta > 0, xdelta is absolute; otherwise, xdelta is relative''' '''Returns a new point. If ydelta != 0, xdelta is absolute; otherwise, xdelta is relative'''
assert xdelta >= 0 and ydelta >= 0, str((xdelta, ydelta)) assert xdelta >= 0 and ydelta >= 0, str((xdelta, ydelta))
if ydelta != 0: if ydelta != 0:
return Point(xdelta, self[0] + ydelta) return Point(xdelta, self[0] + ydelta)
else: else:
return Point(self[1] + xdelta, self[0]) return Point(self[1] + xdelta, self[0])
#def sub(self, xdelta, ydelta):
# '''Returns a new point, subtracing xdelta and ydelta'''
# return Point(self[1] - xdelta, self[0] - ydelta)
#def vsub(self, xdelta, ydelta):
# '''Returns a new point. If ydelta != 0, xdelta is absolute; otherwise, xdelta is relative'''
# assert xdelta >= 0 and ydelta >= 0, str((xdelta, ydelta))
# if ydelta != 0:
# return Point(self[1], self[0] - ydelta)
# else:
# return Point(self[1] - xdelta, self[0])