diff --git a/parso/python/parser.py b/parso/python/parser.py index 5298212..b99053b 100644 --- a/parso/python/parser.py +++ b/parso/python/parser.py @@ -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) diff --git a/test/test_error_recovery.py b/test/test_error_recovery.py index ae72383..0d90907 100644 --- a/test/test_error_recovery.py +++ b/test/test_error_recovery.py @@ -11,6 +11,18 @@ def test_with_stmt(): 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(): module = parse('if x: f.')# \nelse: g( if_stmt = module.children[0] @@ -19,3 +31,5 @@ def test_if_stmt(): assert f.type == 'error_node' assert f.children[0].value == 'f' assert f.children[1].value == '.' + #assert g.children[0].value == 'g' + #assert g.children[1].value == '('