Fix fstring issues when error leaves are involved

This commit is contained in:
Dave Halter
2020-04-06 23:34:27 +02:00
parent 561e81df00
commit cd9c213a62
3 changed files with 25 additions and 5 deletions

View File

@@ -12,10 +12,9 @@ import logging
from parso.utils import split_lines from parso.utils import split_lines
from parso.python.parser import Parser from parso.python.parser import Parser
from parso.python.tree import EndMarker, PythonErrorLeaf from parso.python.tree import EndMarker
from parso.python.tokenize import PythonToken from parso.python.tokenize import PythonToken
from parso.python.token import PythonTokenTypes from parso.python.token import PythonTokenTypes
from parso.tree import search_ancestor
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
DEBUG_DIFF_PARSER = False DEBUG_DIFF_PARSER = False
@@ -432,9 +431,9 @@ class DiffParser(object):
) )
stack = self._active_parser.stack stack = self._active_parser.stack
self._replace_tos_indent = None self._replace_tos_indent = None
#print('start', line_offset + 1, indents) # print('start', line_offset + 1, indents)
for token in tokens: for token in tokens:
#print(token, indents) # print(token, indents)
typ = token.type typ = token.type
if typ == DEDENT: if typ == DEDENT:
if len(indents) < initial_indentation_count: if len(indents) < initial_indentation_count:

View File

@@ -532,7 +532,7 @@ def tokenize_lines(lines, version_info, start_pos=(1, 0), indents=None, is_first
if not pseudomatch: # scan for tokens if not pseudomatch: # scan for tokens
match = whitespace.match(line, pos) match = whitespace.match(line, pos)
if pos == 0 and paren_level == 0: if pos == 0 and paren_level == 0 and not fstring_stack:
for t in dedent_if_necessary(match.end()): for t in dedent_if_necessary(match.end()):
yield t yield t
pos = match.end() pos = match.end()

View File

@@ -1609,3 +1609,24 @@ def test_backslash_insertion(differ):
differ.initialize(code1) differ.initialize(code1)
differ.parse(code2, parsers=2, copies=1, expect_error_leaves=True) differ.parse(code2, parsers=2, copies=1, expect_error_leaves=True)
differ.parse(code1, parsers=2, copies=1) differ.parse(code1, parsers=2, copies=1)
def test_fstring_with_error_leaf(differ):
code1 = dedent("""\
def f():
x
def g():
y
""")
code2 = dedent("""\
def f():
x
F'''
def g():
y
{a
\x01
""")
differ.initialize(code1)
differ.parse(code2, parsers=1, copies=1, expect_error_leaves=True)