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): def current_suite(stack):
# For now just discard everything that is not a suite or # For now just discard everything that is not a suite or
# file_input, if we detect an error. # 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)))): for index, (symbol, nodes) in reversed(list(enumerate(get_symbol_and_nodes(stack)))):
# `suite` can sometimes be only simple_stmt, not stmt. # `suite` can sometimes be only simple_stmt, not stmt.
if symbol == 'file_input': if one_line_suite:
break
elif symbol == 'file_input':
break break
elif symbol == 'suite': elif symbol == 'suite':
if len(nodes) > 1: if len(nodes) > 1:
break break
elif nodes: elif not nodes:
suite_with_newline = True one_line_suite = True
# `suite` without an indent are error nodes. # `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 return index, symbol, nodes
index, symbol, nodes = current_suite(stack) index, symbol, nodes = current_suite(stack)

View File

@@ -11,6 +11,18 @@ def test_with_stmt():
assert module.children[2].type == 'name' assert module.children[2].type == 'name'
def test_one_line_function():
module = parse('def x(): f.')
assert module.children[0].type == 'funcdef'
def_, name, parameters, colon, f = module.children[0].children
assert f.type == 'error_node'
module = parse('def x(a:')
func = module.children[0]
assert func.type == 'error_node'
assert func.children[-1] == ':'
def test_if_stmt(): def test_if_stmt():
module = parse('if x: f.')# \nelse: g( module = parse('if x: f.')# \nelse: g(
if_stmt = module.children[0] if_stmt = module.children[0]
@@ -19,3 +31,5 @@ def test_if_stmt():
assert f.type == 'error_node' assert f.type == 'error_node'
assert f.children[0].value == 'f' assert f.children[0].value == 'f'
assert f.children[1].value == '.' assert f.children[1].value == '.'
#assert g.children[0].value == 'g'
#assert g.children[1].value == '('