From f7936a4910b3df27cb14cf858b7acbc030d82aed Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Mon, 27 Jul 2009 12:06:11 -0400 Subject: [PATCH] ok maybe i got this right finally --HG-- branch : pmacs2 --- lex.py | 17 +++++++++++++++++ mode/perl.py | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lex.py b/lex.py index 5608658..a25d4a5 100755 --- a/lex.py +++ b/lex.py @@ -201,6 +201,23 @@ class ContextPatternRule(PatternRule): class NocaseContextPatternRule(ContextPatternRule): 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): def __init__(self, name, *args): assert args and len(args) % 2 == 0 diff --git a/mode/perl.py b/mode/perl.py index cc83d94..03ab7f5 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -8,7 +8,7 @@ from mode import Fundamental import regex from buffer import IperlBuffer 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 method import Argument, Method, WrapParagraph, arg from method.introspect import TokenComplete @@ -60,7 +60,7 @@ scalar_rules = [ 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'\$\^(?:' + word1 + '|' + wchr1 + ')'), PatternRule('perl.scalar', r'\$\^O'), @@ -78,6 +78,13 @@ def _make_string_rules(forbidden): length, ] + 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 == ')': return rules + [PatternRule('data', r"[^$\%@\(\)]+")] elif forbidden == '}': @@ -160,7 +167,7 @@ PerlGrammar.rules = [ PatternRule('perl.function', r"\$\$*" + word2 + "(?=-> *\()"), # special scalar; doesn't interpolate well - PatternRule('perl.scalar', r'\$/'), + #PatternRule('perl.scalar', r'\$/'), ] + scalar_rules + [ # match regexes; paired delimiters