Revision: 19174
Author:   [email protected]
Date:     Thu Feb  6 19:35:17 2014 UTC
Log:      Experimental parser: unify parser construction

[email protected]

BUG=

Review URL: https://codereview.chromium.org/149113010
http://code.google.com/p/v8/source/detail?r=19174

Modified:
 /branches/experimental/parser/tools/lexer_generator/regex_parser.py
 /branches/experimental/parser/tools/lexer_generator/rule_parser.py

=======================================
--- /branches/experimental/parser/tools/lexer_generator/regex_parser.py Thu Feb 6 16:38:00 2014 UTC +++ /branches/experimental/parser/tools/lexer_generator/regex_parser.py Thu Feb 6 19:35:17 2014 UTC
@@ -31,6 +31,47 @@
 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 @@

   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 @@

   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 @@
   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 @@
     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)
=======================================
--- /branches/experimental/parser/tools/lexer_generator/rule_parser.py Thu Feb 6 11:40:10 2014 UTC +++ /branches/experimental/parser/tools/lexer_generator/rule_parser.py Thu Feb 6 19:35:17 2014 UTC
@@ -28,7 +28,7 @@
 import ply.lex as lex
 import ply.yacc as yacc
 from action import Term, Action
-from regex_parser import RegexParser
+from regex_parser import RegexParser, ParserBuilder
 from nfa_builder import NfaBuilder
 from dfa import Dfa
 from dfa_optimizer import DfaOptimizer
@@ -96,18 +96,9 @@
   t_ACTION_CLOSE = '>'
   t_COMMA = ','

-  def t_LEFT_BRACKET(self, t):
-    r'{'
-    self.lexer.push_state('code')
-    self.nesting = 1
-    return t
-
   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 RuleParserState:

   def __init__(self, encoding):
@@ -297,6 +288,7 @@
     escape_char = lambda string, char: string.replace(char, "\\" + char)
string = reduce(escape_char, "+?*|.[](){}", string).replace("\\\"", "\"")
     p[0] = RegexParser.parse(string)
+
   def p_regex(self, p):
     'regex : REGEX'
     string = p[1][1:-1].replace("\\/", "/")
@@ -323,26 +315,16 @@
   def p_error(self, p):
     raise Exception("Syntax error in input '%s'" % str(p))

-  def build(self, **kwargs):
-    self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs)
-    self.lexer = RuleLexer()
-    self.lexer.build(**kwargs)
-
-  __static_instance = None
   @staticmethod
-  def parse(data, parser_state):
-    parser = RuleParser.__static_instance
-    if not parser:
-      parser = RuleParser()
-      parser.build()
-      RuleParser.__static_instance = parser
-    parser.__state = parser_state
-    try:
-      parser.parser.parse(data, lexer=parser.lexer.lexer)
-    except Exception:
-      RuleParser.__static_instance = None
-      raise
-    parser.__state = None
+  def parse(string, parser_state):
+    new_lexer = lambda: RuleLexer()
+    new_parser = lambda: RuleParser()
+    def preparse(parser):
+      parser.__state = parser_state
+    def postparse(parser):
+      parser.__state = None
+    return ParserBuilder.parse(
+      string, "RuleParser", new_lexer, new_parser, preparse, postparse)

 class RuleProcessor(object):

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to