Improved error reporting facilities.

parent 260359e3
...@@ -278,7 +278,7 @@ cdef class ParserEngine: ...@@ -278,7 +278,7 @@ cdef class ParserEngine:
#s = s + ' if ($$ && $$ != Py_None && PyObject_HasAttrString($$, "_pyBisonError"))\n' #s = s + ' if ($$ && $$ != Py_None && PyObject_HasAttrString($$, "_pyBisonError"))\n'
#s = s + ' {\n' #s = s + ' {\n'
#s = s + ' yyerror(PyString_AsString(PyObject_GetAttrString(py_parser, "lasterror")));\n' #s = s + ' yyerror(PyString_AsString(PyObject_GetAttrString(py_parser, "last_error")));\n'
#s = s + ' Py_INCREF(Py_None);\n' #s = s + ' Py_INCREF(Py_None);\n'
#s = s + ' YYERROR;\n' #s = s + ' YYERROR;\n'
#s = s + ' }\n' #s = s + ' }\n'
...@@ -287,11 +287,11 @@ cdef class ParserEngine: ...@@ -287,11 +287,11 @@ cdef class ParserEngine:
s += ' {\n' s += ' {\n'
s += ' if (PyObject_HasAttrString($$, "_pyBisonError"))\n' s += ' if (PyObject_HasAttrString($$, "_pyBisonError"))\n'
s += ' {\n' s += ' {\n'
s += ' //PyObject* lasterror = PyObject_GetAttrString(py_parser, "lasterror");\n' s += ' //PyObject* last_error = PyObject_GetAttrString(py_parser, "last_error");\n'
s += ' //if (lasterror && PyString_Check(lasterror))\n' s += ' //if (last_error && PyString_Check(last_error))\n'
s += ' // yyerror(PyString_AsString(lasterror));\n' s += ' // yyerror(PyString_AsString(last_error));\n'
s += ' //else\n' s += ' //else\n'
s += ' // yyerror("No \\"lasterror\\" attribute set in BisonError or not a string");\n' s += ' // yyerror("No \\"last_error\\" attribute set in BisonError or not a string");\n'
s += ' Py_INCREF(Py_None);\n' s += ' Py_INCREF(Py_None);\n'
s += ' YYERROR;\n' s += ' YYERROR;\n'
s += ' }\n' s += ' }\n'
...@@ -459,7 +459,7 @@ cdef class ParserEngine: ...@@ -459,7 +459,7 @@ cdef class ParserEngine:
action = action + ",\n ".join(args) + "\n );\n" action = action + ",\n ".join(args) + "\n );\n"
if 'error' in option: if 'error' in option:
action = action + " PyObject_SetAttrString(py_parser, \"lasterror\", Py_None);\n" action = action + " PyObject_SetAttrString(py_parser, \"last_error\", Py_None);\n"
action = action + " Py_INCREF(Py_None);\n" action = action + " Py_INCREF(Py_None);\n"
action = action + " yyclearin;\n" action = action + " yyclearin;\n"
...@@ -504,7 +504,7 @@ cdef class ParserEngine: ...@@ -504,7 +504,7 @@ cdef class ParserEngine:
' PyTuple_SetItem(args, 1, PyString_FromString(mesg));', ' PyTuple_SetItem(args, 1, PyString_FromString(mesg));',
' PyTuple_SetItem(args, 2, PyString_FromString(yytext));', ' PyTuple_SetItem(args, 2, PyString_FromString(yytext));',
'', '',
' ret = PyObject_SetAttrString((PyObject *)py_parser, "lasterror", args);', ' ret = PyObject_SetAttrString((PyObject *)py_parser, "last_error", args);',
' //printf("PyObject_SetAttrString: %d\\n", ret);', ' //printf("PyObject_SetAttrString: %d\\n", ret);',
'', '',
' //printf("line %d: %s before %s\\n", yylineno+1, mesg, yytext);', ' //printf("line %d: %s before %s\\n", yylineno+1, mesg, yytext);',
......
...@@ -45,8 +45,9 @@ class BisonError(object): ...@@ -45,8 +45,9 @@ class BisonError(object):
""" """
_pyBisonError = 1 _pyBisonError = 1
def __init__(self, value='syntax error'): def __init__(self, error, traceback_info):
self.value = value self.value = error
self.traceback_info = traceback_info
class BisonException(Exception): class BisonException(Exception):
...@@ -244,7 +245,7 @@ class BisonParser(object): ...@@ -244,7 +245,7 @@ class BisonParser(object):
last = None # last parsed target, top of parse tree last = None # last parsed target, top of parse tree
lasterror = None # gets set if there was an error last_error = None # gets set if there was an error
keepfiles = 0 # set to 1 to keep temporary engine build files keepfiles = 0 # set to 1 to keep temporary engine build files
...@@ -328,9 +329,9 @@ class BisonParser(object): ...@@ -328,9 +329,9 @@ class BisonParser(object):
try: try:
self.last = handler(target=targetname, option=option, self.last = handler(target=targetname, option=option,
names=names, values=values) names=names, values=values)
except: except Exception as e:
#traceback.print_exception(*sys.exc_info()) #traceback.print_exception(*sys.exc_info())
return self.error(sys.exc_info()) return self.error(e, sys.exc_info())
# raise # raise
#if self.verbose: #if self.verbose:
...@@ -342,7 +343,7 @@ class BisonParser(object): ...@@ -342,7 +343,7 @@ class BisonParser(object):
self.last = BisonNode(targetname, option=option, names=names, values=values) self.last = BisonNode(targetname, option=option, names=names, values=values)
# reset any resulting errors (assume they've been handled) # reset any resulting errors (assume they've been handled)
#self.lasterror = None #self.last_error = None
# assumedly the last thing parsed is at the top of the tree # assumedly the last thing parsed is at the top of the tree
return self.last return self.last
...@@ -395,11 +396,11 @@ class BisonParser(object): ...@@ -395,11 +396,11 @@ class BisonParser(object):
while not self.file.closed: while not self.file.closed:
# do the parsing job, spew if error # do the parsing job, spew if error
self.last = None self.last = None
self.lasterror = None self.last_error = None
self.engine.runEngine(debug) self.engine.runEngine(debug)
if self.lasterror: if self.last_error:
self.report_last_error(filename, self.lasterror) self.report_last_error(filename, self.last_error)
if self.verbose: if self.verbose:
print 'Parser.run: back from engine' print 'Parser.run: back from engine'
...@@ -446,13 +447,14 @@ class BisonParser(object): ...@@ -446,13 +447,14 @@ class BisonParser(object):
print 'Parser: line %s: syntax error "%s" before "%s"' \ print 'Parser: line %s: syntax error "%s" before "%s"' \
% (linenum, msg, tok) % (linenum, msg, tok)
def error(self, value): def error(self, exception, traceback_info):
""" """
Return the result of this method from a handler to notify a syntax error Return the result of this method from a handler to notify a syntax error
""" """
# TODO: should this function be removed? # TODO: should this function be removed?
self.lasterror = value self.last_error = BisonError(exception, traceback_info)
return BisonError(value)
return self.last_error
def exception(self, exception): def exception(self, exception):
# TODO: should this function be removed? # TODO: should this function be removed?
...@@ -476,7 +478,11 @@ class BisonParser(object): ...@@ -476,7 +478,11 @@ class BisonParser(object):
print >>sys.stderr, msg print >>sys.stderr, msg
else: else:
traceback.print_exception(*error) print error
if not self.interactive:
raise error[3]
traceback.print_exception(*error[:2])
def toxml(self): def toxml(self):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment