Revision: 17764
Author:   [email protected]
Date:     Thu Nov 14 20:51:16 2013 UTC
Log:      Experimental parser: cleanup after grammar refactor

[email protected]

BUG=

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

Modified:
 /branches/experimental/parser/tools/lexer_generator/action_test.py
 /branches/experimental/parser/tools/lexer_generator/automata_test.py
 /branches/experimental/parser/tools/lexer_generator/automaton.py
 /branches/experimental/parser/tools/lexer_generator/code_generator_test.py
 /branches/experimental/parser/tools/lexer_generator/dfa.py

=======================================
--- /branches/experimental/parser/tools/lexer_generator/action_test.py Thu Nov 14 20:25:22 2013 UTC +++ /branches/experimental/parser/tools/lexer_generator/action_test.py Thu Nov 14 20:51:16 2013 UTC
@@ -43,7 +43,7 @@
       expected_code = (expected_code, None)
       for automaton in [automata.dfa(), automata.minimal_dfa()]:
         actions = list(automaton.collect_actions(string))
-        self.assertEqual(actions[-1], Action('TERMINATE'))
+        self.assertEqual(actions[-1], Dfa.terminal_action())
         self.assertEqual(actions[-2].match_action(), expected_code)

     def test_action_precedence(self):
=======================================
--- /branches/experimental/parser/tools/lexer_generator/automata_test.py Thu Nov 14 07:25:37 2013 UTC +++ /branches/experimental/parser/tools/lexer_generator/automata_test.py Thu Nov 14 20:51:16 2013 UTC
@@ -40,6 +40,9 @@
   dfa = dfa_from_nfa(nfa)
   return (nfa, dfa, dfa.minimize())

+def simple_action(string):
+  return Action(None, (string, None))
+
 class AutomataTestCase(unittest.TestCase):

     # (pattern, should match, should not match)
@@ -81,8 +84,8 @@
           automaton.to_dot()

     def test_actions(self):
-      left_action = Action("LEFT_ACTION")
-      right_action = Action("RIGHT_ACTION")
+      left_action = simple_action("LEFT_ACTION")
+      right_action = simple_action("RIGHT_ACTION")
       left = RegexParser.parse("left")
       right = RegexParser.parse("right")
       left = NfaBuilder.add_action(left, left_action)
@@ -96,9 +99,9 @@
         for i, action in enumerate(actions):
           self.assertEqual(action, expected[i])
       def verify_miss(string, expected):
-        verify(string, expected + [Action('MISS',)])
+        verify(string, expected + [Dfa.miss_action()])
       def verify_hit(string, expected):
-        verify(string, expected + [Action('TERMINATE',)])
+        verify(string, expected + [Dfa.terminal_action()])
       (l, r) = left_action, right_action
       verify_hit("left", [l])
       verify_miss("lefta", [l])
=======================================
--- /branches/experimental/parser/tools/lexer_generator/automaton.py Thu Nov 14 20:25:22 2013 UTC +++ /branches/experimental/parser/tools/lexer_generator/automaton.py Thu Nov 14 20:51:16 2013 UTC
@@ -31,8 +31,12 @@

 class Action(object):

-  def __init__(self, entry_action, match_action = None, precedence = -1):
-    assert type
+  def __init__(self, entry_action, match_action, precedence = -1):
+    for action in [entry_action, match_action]:
+      if action == None:
+        continue
+      assert type(action) == TupleType and len(action)
+      assert action[0] != None
     self.__entry_action = entry_action
     self.__match_action = match_action
     self.__precedence = precedence
@@ -55,7 +59,15 @@
             self.__match_action == other.__match_action)

   def __str__(self):
-    return "action<%s, %s>" % (self.__entry_action, self.__match_action)
+    parts = []
+    for action in [self.__entry_action, self.__match_action]:
+      part = ""
+      if action:
+        part += action[0]
+        if action[1]:
+          part += "(%s)" % action[1]
+      parts.append(part)
+    return "action< %s >" % " | ".join(parts)

 class AutomatonState(object):

=======================================
--- /branches/experimental/parser/tools/lexer_generator/code_generator_test.py Thu Nov 14 07:25:37 2013 UTC +++ /branches/experimental/parser/tools/lexer_generator/code_generator_test.py Thu Nov 14 20:51:16 2013 UTC
@@ -33,10 +33,10 @@

   def test_simple(self):
     rules = '''
-    <default>
-    "("           { LBRACE }
-    ")"           { RBRACE }
+    <<default>>
+    "("           <|LBRACE|>
+    ")"           <|RBRACE|>

-    "foo"         { FOO }
-    eof           <<terminate>>'''
+    "foo"         <|FOO|>
+    eof           <|terminate|>'''
     CodeGenerator.rule_processor_to_code(RuleProcessor.parse(rules), False)
=======================================
--- /branches/experimental/parser/tools/lexer_generator/dfa.py Thu Nov 14 20:25:22 2013 UTC +++ /branches/experimental/parser/tools/lexer_generator/dfa.py Thu Nov 14 20:51:16 2013 UTC
@@ -117,23 +117,31 @@
     assert len(match) == 1
     return match[0]

+  @staticmethod
+  def terminal_action():
+    return Action(None, ('TERMINATE', None))
+
+  @staticmethod
+  def miss_action():
+    return Action(None, ('Miss', None))
+
   def collect_actions(self, string):
     state = self.__start
     for c in string:
       state = Dfa.__match_char(state, c)
       if not state:
-        yield Action('MISS')
+        yield self.miss_action()
         return
       if state.action():
         yield state.action()
     if state in self.__terminal_set:
-      yield Action('TERMINATE')
+      yield self.terminal_action()
     else:
-      yield Action('MISS')
+      yield self.miss_action()

   def matches(self, string):
     actions = list(self.collect_actions(string))
-    return actions and actions[-1].entry_action() == 'TERMINATE'
+    return actions and actions[-1] == self.terminal_action()

   def lex(self, string):
     state = self.__start

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