Better recovery for online classes and functions

This commit is contained in:
Dave Halter
2018-06-12 13:23:49 +02:00
parent cef9f1bdbd
commit 1e18163402
2 changed files with 20 additions and 10 deletions

View File

@@ -197,23 +197,19 @@ class Parser(BaseParser):
def current_suite(stack):
# For now just discard everything that is not a suite or
# file_input, if we detect an error.
suite_with_newline = False
one_line_suite = False
for index, (symbol, nodes) in reversed(list(enumerate(get_symbol_and_nodes(stack)))):
# `suite` can sometimes be only simple_stmt, not stmt.
if symbol == 'file_input':
if one_line_suite:
break
elif symbol == 'file_input':
break
elif symbol == 'suite':
if len(nodes) > 1:
break
elif nodes:
suite_with_newline = True
elif not nodes:
one_line_suite = True
# `suite` without an indent are error nodes.
continue
elif symbol in ('with_stmt', 'if_stmt', 'while_stmt',
# 'funcdef', 'classdef',
'try_stmt') \
and nodes[-1] == ':' and not suite_with_newline:
break
return index, symbol, nodes
index, symbol, nodes = current_suite(stack)