ok maybe i got this right finally

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2009-07-27 12:06:11 -04:00
parent 619ca7538e
commit f7936a4910
2 changed files with 27 additions and 3 deletions

17
lex.py
View File

@ -201,6 +201,23 @@ class ContextPatternRule(PatternRule):
class NocaseContextPatternRule(ContextPatternRule): class NocaseContextPatternRule(ContextPatternRule):
reflags = re.IGNORECASE reflags = re.IGNORECASE
class LazyPatternRule(Rule):
def __init__(self, name, pattern):
Rule.__init__(self, name)
self.pattern = pattern
self.d = {}
def _compile(self, d):
self.re = re.compile(self.pattern % d, self.reflags)
self.d = d
def match(self, lexer, parent):
if self.d != parent.matchd:
self._compile(parent.matchd)
return self.re.match(self.get_line(lexer), lexer.x)
def lex(self, lexer, parent, m):
if m:
yield self.make_token(lexer, m.group(0), self.name, parent, m.groupdict())
raise StopIteration
class PatternGroupRule(PatternRule): class PatternGroupRule(PatternRule):
def __init__(self, name, *args): def __init__(self, name, *args):
assert args and len(args) % 2 == 0 assert args and len(args) % 2 == 0

View File

@ -8,7 +8,7 @@ from mode import Fundamental
import regex import regex
from buffer import IperlBuffer from buffer import IperlBuffer
from point import Point from point import Point
from lex import Grammar, PatternRule, ContextPatternRule, RegionRule from lex import Grammar, LazyPatternRule, PatternRule, ContextPatternRule, RegionRule
from lex import OverridePatternRule, PatternMatchRule from lex import OverridePatternRule, PatternMatchRule
from method import Argument, Method, WrapParagraph, arg from method import Argument, Method, WrapParagraph, arg
from method.introspect import TokenComplete from method.introspect import TokenComplete
@ -60,7 +60,7 @@ scalar_rules = [
PatternRule('perl.hash', r"\%\$*" + word2), PatternRule('perl.hash', r"\%\$*" + word2),
PatternRule('perl.hash', r'\$' + word2 + '(?= *\{)'), PatternRule('perl.hash', r'\$' + word2 + '(?= *\{)'),
PatternRule('perl.scalar', r'\$[_ab&`\'\+\*\.|,\\";#\%=\-~\^:\?!@\$<>()\[\]](?!' + wchr2 + ')'), PatternRule('perl.scalar', r'\$[_&`\'\+\*\.|,\\";#\%=\-~\^:\?!@\$<>()\[\]](?!' + wchr2 + ')'),
PatternRule('perl.scalar', r'\$\d+(?!' + wchr2 +')'), PatternRule('perl.scalar', r'\$\d+(?!' + wchr2 +')'),
PatternRule('perl.scalar', r'\$\^(?:' + word1 + '|' + wchr1 + ')'), PatternRule('perl.scalar', r'\$\^(?:' + word1 + '|' + wchr1 + ')'),
PatternRule('perl.scalar', r'\$\^O'), PatternRule('perl.scalar', r'\$\^O'),
@ -78,6 +78,13 @@ def _make_string_rules(forbidden):
length, length,
] + scalar_rules ] + scalar_rules
if not forbidden:
rules.insert(0, LazyPatternRule('data', "\$(?=%(delim)s)"))
elif forbidden in '()[].+*|?^':
rules.insert(0, PatternRule('data', "\\$(?=\\" + forbidden + ')'))
else:
rules.insert(0, PatternRule('data', "\\$(?=" + forbidden + ')'))
if forbidden == ')': if forbidden == ')':
return rules + [PatternRule('data', r"[^$\%@\(\)]+")] return rules + [PatternRule('data', r"[^$\%@\(\)]+")]
elif forbidden == '}': elif forbidden == '}':
@ -160,7 +167,7 @@ PerlGrammar.rules = [
PatternRule('perl.function', r"\$\$*" + word2 + "(?=-> *\()"), PatternRule('perl.function', r"\$\$*" + word2 + "(?=-> *\()"),
# special scalar; doesn't interpolate well # special scalar; doesn't interpolate well
PatternRule('perl.scalar', r'\$/'), #PatternRule('perl.scalar', r'\$/'),
] + scalar_rules + [ ] + scalar_rules + [
# match regexes; paired delimiters # match regexes; paired delimiters