diff --git a/baron/inner_formatting_grouper.py b/baron/inner_formatting_grouper.py index 5ec9e5f5..1bce2dda 100644 --- a/baron/inner_formatting_grouper.py +++ b/baron/inner_formatting_grouper.py @@ -153,6 +153,7 @@ def group_generator(sequence): in_grouping_mode += 1 elif current[0] in QUIT_GROUPING_MODE: in_grouping_mode -= 1 + assert in_grouping_mode >= 0 if in_grouping_mode: if current[0] in GROUP_THOSE: @@ -168,11 +169,15 @@ def group_generator(sequence): yield next(iterator) fail_on_bad_token(iterator.show_next(), debug_file_content, in_grouping_mode) + current = append_to_token_before(next(iterator), to_group) + if current[0] in ENTER_GROUPING_MODE: + in_grouping_mode += 1 # TODO test if current[0] in QUIT_GROUPING_MODE: in_grouping_mode -= 1 + assert in_grouping_mode >= 0 yield current continue diff --git a/tests/test_inner_formatting_grouper.py b/tests/test_inner_formatting_grouper.py index 4628b5c9..f866e6de 100644 --- a/tests/test_inner_formatting_grouper.py +++ b/tests/test_inner_formatting_grouper.py @@ -1,5 +1,5 @@ #!/usr/bin/python -# -*- coding:Utf-8 -*- +# -*- coding:utf-8 -*- from baron.inner_formatting_grouper import group @@ -183,3 +183,27 @@ def test_number_backslash_newline(): ('INT', '3'), ('SPACE', '\\\n'), ] + + +def test_nested_grouping_after_endl(): + """ + (b + [0]) + """ + assert group([ + ('LEFT_PARENTHESIS', '('), + ('NAME', 'b'), + ('ENDL', '\n'), + ('SPACE', ' '), + ('LEFT_SQUARE_BRACKET', '['), + ('INT', '0'), + ('RIGHT_SQUARE_BRACKET', ']'), + ('RIGHT_PARENTHESIS', ')'), + ]) == [ + ('LEFT_PARENTHESIS', '('), + ('NAME', 'b'), + ('LEFT_SQUARE_BRACKET', '[', [('ENDL', '\n'), ('SPACE', ' ')], []), + ('INT', '0'), + ('RIGHT_SQUARE_BRACKET', ']'), + ('RIGHT_PARENTHESIS', ')'), + ]