Simple error recovery in the parser: For now just discard everything that is not a suite or file_input, if we detect an error.

This commit is contained in:
Dave Halter
2014-10-30 01:40:22 +01:00
parent f09ff04fcc
commit 1bb0eccc86
2 changed files with 14 additions and 2 deletions

View File

@@ -144,6 +144,7 @@ class Parser(object):
type, value, context)
else:
self.error_recovery(type, value, context)
break
def classify(self, type, value, context):
"""Turn a token into a label. (Internal)"""
@@ -201,5 +202,16 @@ class Parser(object):
index = i
self.stack[index:] = []
else:
# For now just discard everything that is not a suite or
# file_input, if we detect an error.
for i, (dfa, state, node) in reversed(list(enumerate(self.stack))):
symbol, _, _, _ = node
# `suite` can sometimes be only simple_stmt, not stmt.
if symbol in (self.grammar.symbol2number['file_input'],
self.grammar.symbol2number['suite']):
index = i
break
self.stack[index + 1:] = []
# No success finding a transition
raise ParseError("bad input", type, value, context)
#raise ParseError("bad input", type, value, context)

View File

@@ -534,7 +534,7 @@ class Scope(Simple, DocstringMixin):
if is_node(c, 'simple_stmt'):
names += chain.from_iterable(
[s.get_defined_names() for s in c.children
if isinstance(s, (ExprStmt, Import, KeywordStatement))])
if isinstance(s, (ExprStmt, Import))])
elif isinstance(c, (Function, Class)):
names.append(c.name)
return names