Ifs in two directions.

This commit is contained in:
Dave Halter
2016-09-11 22:42:47 +02:00
parent dfdda4a2f1
commit 994e6615b1
2 changed files with 18 additions and 8 deletions

View File

@@ -42,9 +42,14 @@ def _merge_names_dicts(base_dict, other_dict):
def _flows_finished(grammar, stack): def _flows_finished(grammar, stack):
for dfa, newstate, (symbol_number, nodes) in reversed(stack): """
print('symbol', grammar.number2symbol[symbol_number], nodes) if, while, for and try might not be finished, because another part might
#if symbol_number == symbol2number['suite']: still be parsed.
"""
for dfa, newstate, (symbol_number, nodes) in stack:
if grammar.number2symbol[symbol_number] in ('if_stmt', 'while_stmt',
'for_stmt', 'try_stmt'):
return False
return True return True
@@ -479,8 +484,7 @@ class DiffParser(object):
if typ == tokenize.DEDENT: if typ == tokenize.DEDENT:
indents.pop() indents.pop()
if omitted_first_indent and not indents and \ if omitted_first_indent and not indents:
_flows_finished(self._grammar, stack):
# We are done here, only thing that can come now is an # We are done here, only thing that can come now is an
# endmarker or another dedented code block. # endmarker or another dedented code block.
yield tokenize.TokenInfo(tokenize.ENDMARKER, '', start_pos, '') yield tokenize.TokenInfo(tokenize.ENDMARKER, '', start_pos, '')

View File

@@ -60,7 +60,7 @@ class Differ(object):
self.parser = ParserWithRecovery(grammar, source) self.parser = ParserWithRecovery(grammar, source)
return self.parser.module return self.parser.module
def parse(self, source, copies=0, parsers=0): def parse(self, source, copies=0, parsers=0, allow_error_leafs=False):
lines = splitlines(source, keepends=True) lines = splitlines(source, keepends=True)
diff_parser = DiffParser(self.parser) diff_parser = DiffParser(self.parser)
new_module = diff_parser.update(lines) new_module = diff_parser.update(lines)
@@ -69,6 +69,7 @@ class Differ(object):
assert diff_parser._parser_count == parsers assert diff_parser._parser_count == parsers
self.parser.module = new_module self.parser.module = new_module
self.parser._parsed = new_module self.parser._parsed = new_module
if not allow_error_leafs:
assert not _check_error_leafs(new_module) assert not _check_error_leafs(new_module)
return new_module return new_module
@@ -125,8 +126,13 @@ def test_if_simple(differ):
if 1: if 1:
a = 3 a = 3
''') ''')
else_ = "else:\n a = ''\n"
differ.initialize(src + 'a') differ.initialize(src + 'a')
differ.parse(src + "else:\n a = ''\na", copies=1, parsers=1) differ.parse(src + else_ + "a", copies=0, parsers=1)
differ.parse(else_, parsers=1, allow_error_leafs=True)
differ.parse(src + else_, parsers=1)
def test_func_with_for_and_comment(): def test_func_with_for_and_comment():