Reviewers: dcarney,

Message:
Committed patchset #1 manually as r17436.

Description:
Experimental parser generator: Saner rule parsing.

BUG=

[email protected]

Committed: https://code.google.com/p/v8/source/detail?r=17436

Please review this at https://codereview.chromium.org/47493004/

SVN Base: https://v8.googlecode.com/svn/branches/experimental/parser

Affected files (+55, -16 lines):
  M tools/lexer_generator/rule_lexer.py
  M tools/lexer_generator/rule_parser.py


Index: tools/lexer_generator/rule_lexer.py
diff --git a/tools/lexer_generator/rule_lexer.py b/tools/lexer_generator/rule_lexer.py index be7b42f01d9cbb85ff38ffa83bf2c23085726a36..5dc4936a3802eaafc7e6fe672c099380eefe10f6 100644
--- a/tools/lexer_generator/rule_lexer.py
+++ b/tools/lexer_generator/rule_lexer.py
@@ -31,22 +31,62 @@ class RuleLexer:

   tokens = (
       'ALIAS',
-      'CONDITION_TRANSITION',
-      'CONDITION'
+      'EQUALS',
+      'REGEX',
+      'CONDITION',
+      'CONDITION_BEGIN',
+      'CONDITION_END',
+      'REGEX_AND_TRANSITION',
+      'REGEX_AND_BODY',
       )

-  t_ignore = " \t\n"
+  t_ANY_ignore = " \t\n"
+
+  states = (
+    ('afterAlias', 'exclusive'),
+    ('afterAliasEquals', 'exclusive'),
+    ('inCondition', 'exclusive'),
+    ('seenCondition', 'exclusive'),
+    ('afterCondition', 'exclusive'))

   def t_ALIAS(self, t):
-    r'\s*(?P<name>[a-zA-Z0-9_]+)\s*=\s*(?P<regex>.+)\s*;\s*'
+    r'[a-zA-Z0-9_]+'
+    self.lexer.begin('afterAlias')
+    return t
+
+  def t_afterAlias_EQUALS(self, t):
+    r'='
+    self.lexer.begin('afterAliasEquals')
+    return t
+
+  def t_afterAliasEquals_REGEX(self, t):
+    r'(?P<regex>.+)\s*;'
+    self.lexer.begin('INITIAL')
+    return t
+
+  def t_CONDITION_BEGIN(self, t):
+    r'<'
+    self.lexer.begin('inCondition')
+    return t
+
+  def t_inCondition_CONDITION(self, t):
+    r'[a-zA-Z0-9_]+'
+    self.lexer.begin('seenCondition')
+    return t
+
+  def t_seenCondition_CONDITION_END(self, t):
+    r'>'
+    self.lexer.begin('afterCondition')
     return t

-  def t_CONDITION_TRANSITION(self, t):
-    r'\s*<(?P<old>[a-zA-Z]+)>\s*(?P<regex>.+)\s*:=>\s*(?P<new>.+)\s*'
+  def t_afterCondition_REGEX_AND_TRANSITION(self, t):
+    r'(?P<regex>.+)\s*:=>\s*(?P<new>.+)\s*'
+    self.lexer.begin('INITIAL')
     return t

-  def t_CONDITION(self, t):
-    r'\s*<(?P<old>[a-zA-Z]+)>\s*(?P<regex>.+)\s*{(?P<body>.+)}\s*'
+  def t_afterCondition_REGEX_AND_BODY(self, t):
+    r'(?P<regex>.+)\s*{\s*(?P<body>.+)\s*}\s*'
+    self.lexer.begin('INITIAL')
     return t

   def t_error(self, t):
Index: tools/lexer_generator/rule_parser.py
diff --git a/tools/lexer_generator/rule_parser.py b/tools/lexer_generator/rule_parser.py index 0ac92ab970c6a099f260e3ca2eb7a8f08a0bc8f2..96ed09901e6374dc7fbd6805d58d42ee5e233513 100644
--- a/tools/lexer_generator/rule_parser.py
+++ b/tools/lexer_generator/rule_parser.py
@@ -36,23 +36,22 @@ class RuleParser:
   transitions = dict()

   def p_statement_alias(self, p):
-    'statement : ALIAS'
-    name = self.lexer.lexer.lexmatch.group('name')
+    'statement : ALIAS EQUALS REGEX'
     regex = self.lexer.lexer.lexmatch.group('regex')
-    self.aliases[name] = regex
+    self.aliases[p[1]] = regex

   def p_statement_condition_transition(self, p):
-    'statement : CONDITION_TRANSITION'
-    old_condition = self.lexer.lexer.lexmatch.group('old')
+ 'statement : CONDITION_BEGIN CONDITION CONDITION_END REGEX_AND_TRANSITION'
+    old_condition = p[2]
     regex = self.lexer.lexer.lexmatch.group('regex')
     new_condition = self.lexer.lexer.lexmatch.group('new')
     if old_condition not in self.transitions:
       self.transitions[old_condition] = []
     self.transitions[old_condition].append((regex, new_condition))

-  def p_statement_condition(self, p):
-    'statement : CONDITION'
-    old_condition = self.lexer.lexer.lexmatch.group('old')
+  def p_statement_condition_body(self, p):
+    'statement : CONDITION_BEGIN CONDITION CONDITION_END REGEX_AND_BODY'
+    old_condition = p[2]
     regex = self.lexer.lexer.lexmatch.group('regex')
     body = self.lexer.lexer.lexmatch.group('body')
     if old_condition not in self.transitions:


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