Revision: 17434
Author:   [email protected]
Date:     Wed Oct 30 14:06:48 2013 UTC
Log:      Experimental lexer generator: parse rules (in the .re file format)
http://code.google.com/p/v8/source/detail?r=17434

Added:
 /branches/experimental/parser/tools/lexer_generator/rule_lexer.py
 /branches/experimental/parser/tools/lexer_generator/rule_parser.py

=======================================
--- /dev/null
+++ /branches/experimental/parser/tools/lexer_generator/rule_lexer.py Wed Oct 30 14:06:48 2013 UTC
@@ -0,0 +1,56 @@
+# Copyright 2013 the V8 project authors. All rights reserved.
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+#       copyright notice, this list of conditions and the following
+#       disclaimer in the documentation and/or other materials provided
+#       with the distribution.
+#     * Neither the name of Google Inc. nor the names of its
+#       contributors may be used to endorse or promote products derived
+#       from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import ply.lex as lex
+
+class RuleLexer:
+
+  tokens = (
+      'ALIAS',
+      'CONDITION_TRANSITION',
+      'CONDITION'
+      )
+
+  t_ignore = " \t\n"
+
+  def t_ALIAS(self, t):
+    r'\s*(?P<name>[a-zA-Z0-9_]+)\s*=\s*(?P<regex>.+)\s*;\s*'
+    return t
+
+  def t_CONDITION_TRANSITION(self, t):
+    r'\s*<(?P<old>[a-zA-Z]+)>\s*(?P<regex>.+)\s*:=>\s*(?P<new>.+)\s*'
+    return t
+
+  def t_CONDITION(self, t):
+    r'\s*<(?P<old>[a-zA-Z]+)>\s*(?P<regex>.+)\s*{(?P<body>.+)}\s*'
+    return t
+
+  def t_error(self, t):
+    raise Exception("Illegal character '%s'" % t.value[0])
+
+  def build(self, **kwargs):
+    self.lexer = lex.lex(module=self, **kwargs)
=======================================
--- /dev/null
+++ /branches/experimental/parser/tools/lexer_generator/rule_parser.py Wed Oct 30 14:06:48 2013 UTC
@@ -0,0 +1,74 @@
+# Copyright 2013 the V8 project authors. All rights reserved.
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+#       copyright notice, this list of conditions and the following
+#       disclaimer in the documentation and/or other materials provided
+#       with the distribution.
+#     * Neither the name of Google Inc. nor the names of its
+#       contributors may be used to endorse or promote products derived
+#       from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import ply.yacc as yacc
+from rule_lexer import RuleLexer
+
+class RuleParser:
+
+  tokens = RuleLexer.tokens
+
+  aliases = dict()
+  transitions = dict()
+
+  def p_statement_alias(self, p):
+    'statement : ALIAS'
+    name = self.lexer.lexer.lexmatch.group('name')
+    regex = self.lexer.lexer.lexmatch.group('regex')
+    self.aliases[name] = regex
+
+  def p_statement_condition_transition(self, p):
+    'statement : CONDITION_TRANSITION'
+    old_condition = self.lexer.lexer.lexmatch.group('old')
+    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')
+    regex = self.lexer.lexer.lexmatch.group('regex')
+    body = self.lexer.lexer.lexmatch.group('body')
+    if old_condition not in self.transitions:
+      self.transitions[old_condition] = []
+    self.transitions[old_condition].append((regex, body))
+
+  def p_empty(self, p):
+    'empty :'
+
+  def p_error(self, p):
+    raise Exception("Syntax error in input '%s'" % p)
+
+  def build(self, **kwargs):
+    self.parser = yacc.yacc(module=self, **kwargs)
+    self.lexer = RuleLexer()
+    self.lexer.build(**kwargs)
+
+  def parse(self, data):
+    return self.parser.parse(data, lexer=self.lexer.lexer)

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