[pypy-svn] r14312 - in pypy/branch/dist-2.4.1/pypy: interpreter interpreter/pyparser module/recparser

arigo at codespeak.net arigo at codespeak.net
Tue Jul 5 21:35:14 CEST 2005


Author: arigo
Date: Tue Jul  5 21:35:09 2005
New Revision: 14312

Added:
   pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/error.py   (contents, props changed)
Modified:
   pypy/branch/dist-2.4.1/pypy/interpreter/pycompiler.py
   pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonlexer.py
   pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonparse.py
   pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonutil.py
   pypy/branch/dist-2.4.1/pypy/module/recparser/pyparser.py
Log:
Unified the exceptions raised by the parser.


Modified: pypy/branch/dist-2.4.1/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/interpreter/pycompiler.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/interpreter/pycompiler.py	Tue Jul  5 21:35:09 2005
@@ -178,18 +178,30 @@
          the whole source after having only added a new '\n')
     """
     def compile(self, source, filename, mode, flags):
+        from pyparser.error import ParseError
+        from pyparser.pythonutil import ast_from_input
+        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
+        # XXX use 'flags'
+        space = self.space
+        transformer = Transformer()
+        try:
+            tree = ast_from_input(source, mode, transformer)
+        except ParseError, e:
+            raise OperationError(space.w_SyntaxError,
+                                 e.wrap_info(space, filename))
+        c = self.compile_tree(tree, filename, mode)
+        from pypy.interpreter.pycode import PyCode
+        return space.wrap(PyCode(space)._from_code(c))
+
+    def compile_tree(self, tree, filename, mode):
+        # __________
+        # XXX this uses the non-annotatable stablecompiler at interp-level
         from pypy.interpreter import stablecompiler
         from pypy.interpreter.stablecompiler.pycodegen import ModuleCodeGenerator
         from pypy.interpreter.stablecompiler.pycodegen import InteractiveCodeGenerator
         from pypy.interpreter.stablecompiler.pycodegen import ExpressionCodeGenerator
         from pypy.interpreter.stablecompiler.transformer import Transformer
-        from pyparser.pythonutil import ast_from_input
-
-        flags |= __future__.generators.compiler_flag   # always on (2.2 compat)
-        space = self.space
         try:
-            transformer = Transformer()
-            tree = ast_from_input(source, mode, transformer)
             stablecompiler.misc.set_filename(filename, tree)
             if mode == 'exec':
                 codegenerator = ModuleCodeGenerator(tree)
@@ -198,9 +210,6 @@
             else: # mode == 'eval':
                 codegenerator = ExpressionCodeGenerator(tree)
             c = codegenerator.getCode()
-        # It would be nice to propagate all exceptions to app level,
-        # but here we only propagate the 'usual' ones, until we figure
-        # out how to do it generically.
         except SyntaxError, e:
             w_synerr = space.newtuple([space.wrap(e.msg),
                                        space.newtuple([space.wrap(e.filename),
@@ -212,8 +221,9 @@
             raise OperationError(space.w_ValueError,space.wrap(str(e)))
         except TypeError,e:
             raise OperationError(space.w_TypeError,space.wrap(str(e)))
-        from pypy.interpreter.pycode import PyCode
-        return space.wrap(PyCode(space)._from_code(c))
+        # __________ end of XXX above
+        return c
+    compile_tree._annspecialcase_ = 'override:cpy_stablecompiler'
 
 
 class PyPyCompiler(CPythonCompiler):

Added: pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/error.py
==============================================================================
--- (empty file)
+++ pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/error.py	Tue Jul  5 21:35:09 2005
@@ -0,0 +1,24 @@
+from pypy.interpreter.error import OperationError
+
+
+class ParseError(Exception):
+    """Base class for exceptions raised by the parser."""
+
+    def __init__(self, msg, lineno, offset, text):
+        self.msg = msg
+        self.lineno = lineno
+        self.offset = offset
+        self.text = text
+
+    def wrap_info(self, space, filename):
+        return space.newtuple([space.wrap(self.msg),
+                               space.newtuple([space.wrap(filename),
+                                               space.wrap(self.lineno),
+                                               space.wrap(self.offset),
+                                               space.wrap(self.text)])])
+
+    def __str__(self):
+        return "%s at pos (%d, %d) in %r" % (self.__class__.__name__,
+                                             self.lineno,
+                                             self.offset,
+                                             self.text)

Modified: pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonlexer.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonlexer.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonlexer.py	Tue Jul  5 21:35:09 2005
@@ -5,6 +5,7 @@
 import symbol, sys
 
 from pypy.interpreter.pyparser.grammar import TokenSource, Token
+from pypy.interpreter.pyparser.error import ParseError
 # Don't import string for that ...
 NAMECHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'
 NUMCHARS = '0123456789'
@@ -71,15 +72,11 @@
 tokenmod.COMMENT = tokenmod.N_TOKENS 
 tokenmod.NL = tokenmod.N_TOKENS + 1
 
-class TokenError(Exception):
+class TokenError(ParseError):
     """Raised for lexer errors, e.g. when EOF is found prematurely"""
     def __init__(self, msg, line, strstart, token_stack):
-        self.lineno, self.offset = strstart
-        self.text = line
-        self.errlabel = msg
-        self.msg = "TokenError at pos (%d, %d) in %r" % (self.lineno,
-                                                         self.offset,
-                                                         line)
+        lineno, offset = strstart
+        ParseError.__init__(self, msg, lineno, offset, line)
         self.token_stack = token_stack
 
 def generate_tokens(lines):

Modified: pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonparse.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonparse.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonparse.py	Tue Jul  5 21:35:09 2005
@@ -6,6 +6,7 @@
 using file_input, single_input and eval_input targets
 """
 from pypy.interpreter.error import OperationError, debug_print
+from pypy.interpreter.pyparser.error import ParseError
 from pypy.tool.option import Options
 
 from pythonlexer import Source
@@ -44,10 +45,9 @@
         builder.source_encoding = src.encoding
         # </HACK>
         if not result:
-            # raising a SyntaxError here is not annotable, and it can
-            # probably be handled in an other way
             line, lineno = src.debug()
-            raise BuilderError(line, lineno)
+            # XXX needs better error messages
+            raise ParseError("error", lineno, -1, line)
             # return None
         return builder
 
@@ -108,14 +108,6 @@
 
 
 
-class BuilderError(SyntaxError):
-    def __init__(self, line, lineno):
-        self.filename = 'XXX.py'
-        self.line = self.text = line
-        self.lineno = lineno
-        self.offset = -1
-        self.msg = "SyntaxError at line %d: %r" % (self.lineno, self.line)
-
 def parse_file_input(pyf, gram, builder=None):
     """Parse a python file"""
     return gram.parse_source( pyf.read(), "file_input", builder )

Modified: pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonutil.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonutil.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/interpreter/pyparser/pythonutil.py	Tue Jul  5 21:35:09 2005
@@ -5,6 +5,7 @@
 
 import pythonparse
 from tuplebuilder import TupleBuilder
+from pypy.interpreter.pyparser.error import ParseError
 
 PYTHON_PARSER = pythonparse.PYTHON_PARSER
 TARGET_DICT = {

Modified: pypy/branch/dist-2.4.1/pypy/module/recparser/pyparser.py
==============================================================================
--- pypy/branch/dist-2.4.1/pypy/module/recparser/pyparser.py	(original)
+++ pypy/branch/dist-2.4.1/pypy/module/recparser/pyparser.py	Tue Jul  5 21:35:09 2005
@@ -8,7 +8,7 @@
 from pypy.interpreter.typedef import interp_attrproperty, GetSetProperty
 from pypy.interpreter.pycode import PyCode 
 from pypy.interpreter.pyparser.syntaxtree import SyntaxNode
-from pypy.interpreter.pyparser.pythonutil import PYTHON_PARSER
+from pypy.interpreter.pyparser.pythonutil import PYTHON_PARSER, ParseError
 
 __all__ = [ "ASTType", "STType", "suite", "expr" ]
 
@@ -98,16 +98,23 @@
     totuple = interp2app(STType.descr_totuple),
 )
 
+def parse_python_source(space, source, goal):
+    try:
+        return PYTHON_PARSER.parse_source(source, goal)
+    except ParseError, e:
+        raise OperationError(space.w_SyntaxError,
+                             e.wrap_info(space, '<string>'))
+
 def suite( space, source ):
     # make the annotator life easier (don't use str.splitlines())
-    builder = PYTHON_PARSER.parse_source( source, "file_input" )
+    builder = parse_python_source( space, source, "file_input" )
     return space.wrap( STType(space, builder.stack[-1]) )    
 
 suite.unwrap_spec = [ObjSpace, str]
 
 def expr( space, source ):
     # make the annotator life easier (don't use str.splitlines())
-    builder = PYTHON_PARSER.parse_source( source, "eval_input" )
+    builder = parse_python_source( space, source, "eval_input" )
     return space.wrap( STType(space, builder.stack[-1]) )    
 
 expr.unwrap_spec = [ObjSpace, str]



More information about the Pypy-commit mailing list