mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-21 03:42:00 +08:00
Generate error tokens in the tokenizer if the indentation is dedented wrongly.
This commit is contained in:
@@ -277,6 +277,9 @@ def tokenize_lines(lines):
|
|||||||
yield TokenInfo(INDENT, '', spos, '')
|
yield TokenInfo(INDENT, '', spos, '')
|
||||||
indents.append(start)
|
indents.append(start)
|
||||||
while start < indents[-1]:
|
while start < indents[-1]:
|
||||||
|
if start > indents[-2]:
|
||||||
|
yield TokenInfo(ERRORTOKEN, '', spos, '')
|
||||||
|
break
|
||||||
yield TokenInfo(DEDENT, '', spos, '')
|
yield TokenInfo(DEDENT, '', spos, '')
|
||||||
indents.pop()
|
indents.pop()
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
for a in 'abc':
|
for a in 'abc':
|
||||||
for b in 'xyz':
|
for b in 'xyz':
|
||||||
print a # indented with 8 spaces
|
print a # indented with 8 spaces
|
||||||
#: W101:0
|
# TODO currently not an error, because the indentation matches.
|
||||||
print(b) # indented with 1 tab
|
print(b) # indented with 1 tab
|
||||||
if True:
|
if True:
|
||||||
#: W101:0
|
#: E101:0
|
||||||
pass
|
pass
|
||||||
|
|
||||||
#: E122+1
|
#: E122+1
|
||||||
|
|||||||
@@ -2,13 +2,17 @@
|
|||||||
|
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from parso._compatibility import py_version
|
from parso._compatibility import py_version
|
||||||
from parso.utils import splitlines
|
from parso.utils import splitlines
|
||||||
from parso.python.token import NAME, NEWLINE, STRING, INDENT, ERRORTOKEN, ENDMARKER
|
from parso.python.token import (
|
||||||
|
NAME, NEWLINE, STRING, INDENT, DEDENT, ERRORTOKEN, ENDMARKER)
|
||||||
from parso.python import tokenize
|
from parso.python import tokenize
|
||||||
from parso import parse
|
from parso import parse
|
||||||
from parso.python.tokenize import TokenInfo
|
from parso.python.tokenize import TokenInfo
|
||||||
|
|
||||||
|
|
||||||
def _get_token_list(string):
|
def _get_token_list(string):
|
||||||
return list(tokenize.tokenize(string))
|
return list(tokenize.tokenize(string))
|
||||||
|
|
||||||
@@ -211,3 +215,28 @@ def test_endmarker_end_pos():
|
|||||||
check('a')
|
check('a')
|
||||||
check(r'a\\n')
|
check(r'a\\n')
|
||||||
check('a\\')
|
check('a\\')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
('code', 'types'), [
|
||||||
|
(' foo', ['error_leaf', 'name'])
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_indentation(code, types):
|
||||||
|
return
|
||||||
|
actual_types = [t.type for t in _get_token_list(code)]
|
||||||
|
print(actual_types)
|
||||||
|
assert False
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
('code', 'types'), [
|
||||||
|
(' foo', [INDENT, NAME, DEDENT]),
|
||||||
|
(' foo\n bar', [INDENT, NAME, NEWLINE, ERRORTOKEN, NAME, DEDENT]),
|
||||||
|
(' foo\n bar \n baz', [INDENT, NAME, NEWLINE, ERRORTOKEN, NAME,
|
||||||
|
NEWLINE, ERRORTOKEN, NAME, DEDENT]),
|
||||||
|
(' foo\nbar', [INDENT, NAME, NEWLINE, DEDENT, NAME]),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_indentation(code, types):
|
||||||
|
actual_types = [t.type for t in _get_token_list(code)]
|
||||||
|
assert actual_types == types + [ENDMARKER]
|
||||||
|
|||||||
Reference in New Issue
Block a user