Fix for loop issues in the fast parser.

This commit is contained in:
Dave Halter
2015-01-29 01:36:16 +01:00
parent e412694fa2
commit dde0e9c7c6
3 changed files with 16 additions and 9 deletions

View File

@@ -12,9 +12,11 @@ from jedi.parser import Parser
from jedi.parser import tree as pr from jedi.parser import tree as pr
from jedi.parser import tokenize from jedi.parser import tokenize
from jedi import cache from jedi import cache
from jedi.parser.tokenize import (source_tokens, FLOWS, NEWLINE, from jedi.parser.tokenize import (source_tokens, NEWLINE,
ENDMARKER, INDENT, DEDENT) ENDMARKER, INDENT, DEDENT)
FLOWS = ['if', 'else', 'elif', 'while', 'with', 'try', 'except', 'finally', 'for']
class FastModule(pr.SubModule): class FastModule(pr.SubModule):
type = 'file_input' type = 'file_input'
@@ -278,7 +280,7 @@ class ParserNode(object):
class FastParser(use_metaclass(CachedFastParser)): class FastParser(use_metaclass(CachedFastParser)):
_keyword_re = re.compile('^[ \t]*(def|class|@|%s)' % '|'.join(tokenize.FLOWS)) _keyword_re = re.compile('^[ \t]*(def|class|@|%s)' % '|'.join(FLOWS))
def __init__(self, grammar, source, module_path=None): def __init__(self, grammar, source, module_path=None):
# set values like `pr.Module`. # set values like `pr.Module`.
@@ -348,7 +350,7 @@ class FastParser(use_metaclass(CachedFastParser)):
if not in_flow: if not in_flow:
m = self._keyword_re.match(l) m = self._keyword_re.match(l)
if m: if m:
in_flow = m.group(1) in tokenize.FLOWS in_flow = m.group(1) in FLOWS
if not is_decorator and not in_flow: if not is_decorator and not in_flow:
if current_lines: if current_lines:
yield gen_part() yield gen_part()
@@ -540,7 +542,6 @@ class FastTokenizer(object):
self._indent_counter += 1 self._indent_counter += 1
elif typ == DEDENT: elif typ == DEDENT:
self._indent_counter -= 1 self._indent_counter -= 1
print(self._flow_indent_counter)
if self._in_flow and self._indent_counter == self._flow_indent_counter: if self._in_flow and self._indent_counter == self._flow_indent_counter:
self._in_flow = False self._in_flow = False
self._next_dedent_noclose = True self._next_dedent_noclose = True

View File

@@ -21,11 +21,6 @@ from jedi.parser.token import (tok_name, N_TOKENS, ENDMARKER, STRING, NUMBER,
cookie_re = re.compile("coding[:=]\s*([-\w.]+)") cookie_re = re.compile("coding[:=]\s*([-\w.]+)")
# From here on we have custom stuff (everything before was originally Python
# internal code).
FLOWS = ['if', 'else', 'elif', 'while', 'with', 'try', 'except', 'finally']
namechars = string.ascii_letters + '_' namechars = string.ascii_letters + '_'

View File

@@ -131,6 +131,17 @@ def test_if_simple():
check_fp(src + "else:\n a = ''\na", 1) check_fp(src + "else:\n a = ''\na", 1)
def test_for():
src = dedent("""\
for a in [1,2]:
a
for a1 in 1,"":
a1
""")
check_fp(src, 1)
def test_incomplete_function(): def test_incomplete_function():
source = '''return ImportErr''' source = '''return ImportErr'''