mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-10 22:51:59 +08:00
Make a proper recovery for only error statements in a suite.
This commit is contained in:
@@ -82,6 +82,7 @@ class Grammar(object):
|
|||||||
self.keywords = {}
|
self.keywords = {}
|
||||||
self.tokens = {}
|
self.tokens = {}
|
||||||
self.symbol2label = {}
|
self.symbol2label = {}
|
||||||
|
self.label2symbol = {}
|
||||||
self.start = 256
|
self.start = 256
|
||||||
|
|
||||||
def dump(self, filename):
|
def dump(self, filename):
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ class ParserGenerator(object):
|
|||||||
else:
|
else:
|
||||||
c.labels.append((c.symbol2number[label], None))
|
c.labels.append((c.symbol2number[label], None))
|
||||||
c.symbol2label[label] = ilabel
|
c.symbol2label[label] = ilabel
|
||||||
|
c.label2symbol[ilabel] = label
|
||||||
return ilabel
|
return ilabel
|
||||||
else:
|
else:
|
||||||
# A named token (NAME, NUMBER, STRING)
|
# A named token (NAME, NUMBER, STRING)
|
||||||
|
|||||||
@@ -75,9 +75,7 @@ with_stmt: 'with' with_item (',' with_item)* ':' suite
|
|||||||
with_item: test ['as' expr]
|
with_item: test ['as' expr]
|
||||||
# NB compile.c makes sure that the default except clause is last
|
# NB compile.c makes sure that the default except clause is last
|
||||||
except_clause: 'except' [test [('as' | ',') test]]
|
except_clause: 'except' [test [('as' | ',') test]]
|
||||||
# Edit by David Halter: The stmt is now optional. This reflects how Jedi allows
|
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
|
||||||
# classes and functions to be empty, which is beneficial for autocompletion.
|
|
||||||
suite: simple_stmt | NEWLINE INDENT stmt* DEDENT
|
|
||||||
|
|
||||||
# Backward compatibility cruft to support:
|
# Backward compatibility cruft to support:
|
||||||
# [ x for x in lambda: True, lambda: False if x() ]
|
# [ x for x in lambda: True, lambda: False if x() ]
|
||||||
|
|||||||
@@ -78,9 +78,7 @@ with_stmt: 'with' with_item (',' with_item)* ':' suite
|
|||||||
with_item: test ['as' expr]
|
with_item: test ['as' expr]
|
||||||
# NB compile.c makes sure that the default except clause is last
|
# NB compile.c makes sure that the default except clause is last
|
||||||
except_clause: 'except' [test ['as' NAME]]
|
except_clause: 'except' [test ['as' NAME]]
|
||||||
# Edit by David Halter: The stmt is now optional. This reflects how Jedi allows
|
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
|
||||||
# classes and functions to be empty, which is beneficial for autocompletion.
|
|
||||||
suite: simple_stmt | NEWLINE INDENT stmt* DEDENT
|
|
||||||
|
|
||||||
test: or_test ['if' or_test 'else' test] | lambdef
|
test: or_test ['if' or_test 'else' test] | lambdef
|
||||||
test_nocond: or_test | lambdef_nocond
|
test_nocond: or_test | lambdef_nocond
|
||||||
|
|||||||
@@ -84,9 +84,7 @@ with_stmt: 'with' with_item (',' with_item)* ':' suite
|
|||||||
with_item: test ['as' expr]
|
with_item: test ['as' expr]
|
||||||
# NB compile.c makes sure that the default except clause is last
|
# NB compile.c makes sure that the default except clause is last
|
||||||
except_clause: 'except' [test ['as' NAME]]
|
except_clause: 'except' [test ['as' NAME]]
|
||||||
# Edit by David Halter: The stmt is now optional. This reflects how Jedi allows
|
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
|
||||||
# classes and functions to be empty, which is beneficial for autocompletion.
|
|
||||||
suite: simple_stmt | NEWLINE INDENT stmt* DEDENT
|
|
||||||
|
|
||||||
test: or_test ['if' or_test 'else' test] | lambdef
|
test: or_test ['if' or_test 'else' test] | lambdef
|
||||||
test_nocond: or_test | lambdef_nocond
|
test_nocond: or_test | lambdef_nocond
|
||||||
|
|||||||
@@ -83,10 +83,7 @@ with_stmt: 'with' with_item (',' with_item)* ':' suite
|
|||||||
with_item: test ['as' expr]
|
with_item: test ['as' expr]
|
||||||
# NB compile.c makes sure that the default except clause is last
|
# NB compile.c makes sure that the default except clause is last
|
||||||
except_clause: 'except' [test ['as' NAME]]
|
except_clause: 'except' [test ['as' NAME]]
|
||||||
# Edit by Francisco Souza/David Halter: The stmt is now optional. This reflects
|
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
|
||||||
# how Jedi allows classes and functions to be empty, which is beneficial for
|
|
||||||
# autocompletion.
|
|
||||||
suite: simple_stmt | NEWLINE INDENT stmt* DEDENT
|
|
||||||
|
|
||||||
test: or_test ['if' or_test 'else' test] | lambdef
|
test: or_test ['if' or_test 'else' test] | lambdef
|
||||||
test_nocond: or_test | lambdef_nocond
|
test_nocond: or_test | lambdef_nocond
|
||||||
|
|||||||
@@ -221,6 +221,17 @@ class Parser(BaseParser):
|
|||||||
error_leaf = tree.PythonErrorLeaf(tok_name[typ].lower(), value, start_pos, prefix)
|
error_leaf = tree.PythonErrorLeaf(tok_name[typ].lower(), value, start_pos, prefix)
|
||||||
stack[-1][2][1].append(error_leaf)
|
stack[-1][2][1].append(error_leaf)
|
||||||
|
|
||||||
|
if symbol == 'suite':
|
||||||
|
dfa, state, node = stack[-1]
|
||||||
|
states, first = dfa
|
||||||
|
arcs = states[state]
|
||||||
|
intended_label = pgen_grammar.symbol2label['stmt']
|
||||||
|
# Introduce a proper state transition. We're basically allowing
|
||||||
|
# there to be no valid statements inside a suite.
|
||||||
|
if [x[0] for x in arcs] == [intended_label]:
|
||||||
|
new_state = arcs[0][1]
|
||||||
|
stack[-1] = dfa, new_state, node
|
||||||
|
|
||||||
def _stack_removal(self, pgen_grammar, stack, arcs, start_index, value, start_pos):
|
def _stack_removal(self, pgen_grammar, stack, arcs, start_index, value, start_pos):
|
||||||
failed_stack = False
|
failed_stack = False
|
||||||
found = False
|
found = False
|
||||||
|
|||||||
Reference in New Issue
Block a user