Anonymous avatar Anonymous committed fbe54d3

added a hook to modify the grammar rules at applevel

Comments (0)

Files changed (2)


         warnings.warn_explicit = old_warn_explicit
 class PythonAstCompiler(PyCodeCompiler):
     """Uses the stdlib's python implementation of compiler
          of incomplete inputs (e.g. we shouldn't re-compile from sracth
          the whole source after having only added a new '\n')
+    def __init__(self, space):
+        from pyparser.pythonparse import get_pyparser_for_version
+        from pypy.tool.option import Options
+        PyCodeCompiler.__init__(self, space)
+        self.parser = get_pyparser_for_version(Options.version)
+        self.additional_rules = {}
     def compile(self, source, filename, mode, flags):
         from pyparser.error import SyntaxError
         from pypy.interpreter import astcompiler
         from pypy.interpreter.astcompiler.pycodegen import ExpressionCodeGenerator
         from pypy.interpreter.astcompiler.ast import Node
         from pyparser.astbuilder import AstBuilder
-        from pyparser.pythonparse import PYTHON_PARSER
         from pypy.interpreter.pycode import PyCode
         flags |= stdlib___future__.generators.compiler_flag   # always on (2.2 compat)
         space =
-            builder = AstBuilder(space=space)
-            PYTHON_PARSER.parse_source(source, mode, builder, flags)
+            builder = AstBuilder(self.parser, space=space)
+            for rulename, buildfunc in self.additional_rules.iteritems():
+                builder.build_rules[rulename] = buildfunc
+            self.parser.parse_source(source, mode, builder, flags)
             ast_tree = builder.rule_stack[-1]
             encoding = builder.source_encoding
         except SyntaxError, e:
 #       if not space.get( w_callable ):
 #           raise OperationError( space.w_TypeError( space.wrap( "must have a callable" ) )
         space.default_compiler.w_compile_hook = w_callable
+def insert_grammar_rule(space, w_rule, w_buildfuncs):
+    """inserts new grammar rules to the default compiler"""
+    rule = space.str_w(w_rule)
+    buildfuncs_w = w_buildfuncs.content
+    buildfuncs = {}
+    for w_name, w_func in buildfuncs_w.iteritems():
+        buildfuncs[space.str_w(w_name)] = space.unwrap(w_func)
+    space.default_compiler.additional_rules = buildfuncs
+    space.default_compiler.parser.insert_rule(rule)


          'source2ast' : "pyparser.source2ast",
          'decode_string_literal': 'pyparser.decode_string_literal',
          'install_compiler_hook' : 'pypy.interpreter.pycompiler.install_compiler_hook',
+         'insert_grammar_rule' : 'pypy.interpreter.pycompiler.insert_grammar_rule',
          'rules' : 'pypy.interpreter.pyparser.pythonparse.grammar_rules',
          'parse_grammar' : 'pypy.interpreter.pyparser.pythonparse.parse_grammar',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.