Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Unified Diff: tools/lexer_generator/regex_parser.py

Issue 149113010: Experimental parser: unify parser construction (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/lexer_generator/rule_parser.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/lexer_generator/regex_parser.py
diff --git a/tools/lexer_generator/regex_parser.py b/tools/lexer_generator/regex_parser.py
index b624584d2d849b91e1ea6dce645e87a50febf7fa..dc00afb15aba51b286159e026dfb9aa5abccc9ca 100644
--- a/tools/lexer_generator/regex_parser.py
+++ b/tools/lexer_generator/regex_parser.py
@@ -31,6 +31,47 @@ from types import ListType, TupleType
from regex_lexer import RegexLexer
from action import Term
+class ParserBuilder:
+
+ class Logger(object):
+ def debug(self,msg,*args,**kwargs):
+ pass
+
+ def info(self,msg,*args,**kwargs):
+ pass
+
+ def warning(self,msg,*args,**kwargs):
+ pass
+ # assert False, "warning: "+ (msg % args) + "\n"
+
+ def error(self,msg,*args,**kwargs):
+ assert False, "error: "+ (msg % args) + "\n"
+
+ __static_instances = {}
+ @staticmethod
+ def parse(
+ string, name, new_lexer, new_parser, preparse = None, postparse = None):
+ if not name in ParserBuilder.__static_instances:
+ logger = ParserBuilder.Logger()
+ lexer_instance = new_lexer()
+ lexer_instance.lex = lex.lex(module=lexer_instance)
+ instance = new_parser()
+ instance.yacc = yacc.yacc(
+ module=instance, debug=True, write_tables=0,
+ debuglog=logger, errorlog=logger)
+ ParserBuilder.__static_instances[name] = (lexer_instance, instance)
+ (lexer_instance, instance) = ParserBuilder.__static_instances[name]
+ if preparse:
+ preparse(instance)
+ try:
+ return_value = instance.yacc.parse(string, lexer=lexer_instance.lex)
+ except Exception:
+ del ParserBuilder.__static_instances[name]
+ raise
+ if postparse:
+ postparse(instance)
+ return return_value
+
def build_escape_map(chars):
def add_escape(d, char):
d['\\' + char] = char
@@ -95,12 +136,12 @@ class RegexLexer:
def t_CLASS_BEGIN(self, t):
r'\['
- self.lexer.push_state('class')
+ self.lex.push_state('class')
return t
def t_class_CLASS_END(self, t):
r'\]'
- self.lexer.pop_state()
+ self.lex.pop_state()
return t
t_class_RANGE = '-'
@@ -123,12 +164,12 @@ class RegexLexer:
def t_REPEAT_BEGIN(self, t):
r'\{'
- self.lexer.push_state('repeat')
+ self.lex.push_state('repeat')
return t
def t_repeat_REPEAT_END(self, t):
r'\}'
- self.lexer.pop_state()
+ self.lex.pop_state()
return t
t_repeat_NUMBER = r'[0-9]+'
@@ -139,9 +180,6 @@ class RegexLexer:
def t_ANY_error(self, t):
raise Exception("Illegal character '%s'" % t.value[0])
- def build(self, **kwargs):
- self.lexer = lex.lex(module=self, **kwargs)
-
class RegexParser:
tokens = RegexLexer.tokens
@@ -268,21 +306,8 @@ class RegexParser:
assert left
return left if not right else Term('CAT', left, right)
- def build(self, **kwargs):
- self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs)
- self.lexer = RegexLexer()
- self.lexer.build(**kwargs)
-
- __static_instance = None
@staticmethod
- def parse(data):
- parser = RegexParser.__static_instance
- if not parser:
- parser = RegexParser()
- parser.build()
- RegexParser.__static_instance = parser
- try:
- return parser.parser.parse(data, lexer=parser.lexer.lexer)
- except Exception:
- RegexParser.__static_instance = None
- raise
+ def parse(string):
+ new_lexer = lambda: RegexLexer()
+ new_parser = lambda: RegexParser()
+ return ParserBuilder.parse(string, "RegexParser", new_lexer, new_parser)
« no previous file with comments | « no previous file | tools/lexer_generator/rule_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698