mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-18 18:36:02 +08:00
Start scanning for indentation errors.
This commit is contained in:
@@ -67,10 +67,21 @@ class ErrorFinder(Normalizer):
|
|||||||
|
|
||||||
def visit_leaf(self, leaf):
|
def visit_leaf(self, leaf):
|
||||||
if leaf.type == 'error_leaf':
|
if leaf.type == 'error_leaf':
|
||||||
self._add_syntax_error("Syntax Error", leaf)
|
if leaf.original_type == 'indent':
|
||||||
|
# Indents/Dedents itself never have a prefix. They are just
|
||||||
|
# "pseudo" tokens that get removed by the syntax tree later.
|
||||||
|
# Therefore in case of an error we also have to check for this.
|
||||||
|
spacing = list(leaf.get_next_leaf()._split_prefix())[-1]
|
||||||
|
self._add_indentation_error("Indentation Error", spacing)
|
||||||
|
print(leaf, repr(leaf.prefix), repr(leaf.value), leaf.get_previous_leaf())
|
||||||
|
else:
|
||||||
|
self._add_syntax_error("Syntax Error", leaf)
|
||||||
|
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
def _add_indentation_error(self, message, spacing):
|
||||||
|
self._add_error(903, message, spacing)
|
||||||
|
|
||||||
def _add_syntax_error(self, message, node):
|
def _add_syntax_error(self, message, node):
|
||||||
self._add_error(901, message, node)
|
self._add_error(901, message, node)
|
||||||
|
|
||||||
|
|||||||
@@ -103,6 +103,19 @@ class PythonLeaf(PythonMixin, Leaf):
|
|||||||
def _split_prefix(self):
|
def _split_prefix(self):
|
||||||
return split_prefix(self, self.get_start_pos_of_prefix())
|
return split_prefix(self, self.get_start_pos_of_prefix())
|
||||||
|
|
||||||
|
def get_start_pos_of_prefix(self):
|
||||||
|
# TODO it is really ugly that we have to override it. Maybe change
|
||||||
|
# indent error leafs somehow? No idea how, though.
|
||||||
|
previous_leaf = self.get_previous_leaf()
|
||||||
|
if previous_leaf is not None and previous_leaf.type == 'error_leaf' \
|
||||||
|
and previous_leaf.original_type == 'indent':
|
||||||
|
previous_leaf = previous_leaf.get_previous_leaf()
|
||||||
|
|
||||||
|
if previous_leaf is None:
|
||||||
|
return self.line - self.prefix.count('\n'), 0 # It's the first leaf.
|
||||||
|
return previous_leaf.end_pos
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class _LeafWithoutNewlines(PythonLeaf):
|
class _LeafWithoutNewlines(PythonLeaf):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ if False:
|
|||||||
pass
|
pass
|
||||||
print
|
print
|
||||||
print
|
print
|
||||||
#: E901:4
|
#: E903:0
|
||||||
print
|
print
|
||||||
mimetype = 'application/x-directory'
|
mimetype = 'application/x-directory'
|
||||||
#: E111:5
|
#: E111:5
|
||||||
|
|||||||
@@ -14,6 +14,11 @@ def _get_error_list(code, version=None):
|
|||||||
return list(tree._get_normalizer_issues(config))
|
return list(tree._get_normalizer_issues(config))
|
||||||
|
|
||||||
|
|
||||||
|
def assert_comparison(code, error_code, positions):
|
||||||
|
errors = [(error.start_pos, error.code) for error in _get_error_list(code)]
|
||||||
|
assert [(pos, error_code) for pos in positions] == errors
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('code', 'positions'), [
|
('code', 'positions'), [
|
||||||
('1 +', [(1, 3)]),
|
('1 +', [(1, 3)]),
|
||||||
@@ -34,9 +39,13 @@ def _get_error_list(code, version=None):
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_syntax_errors(code, positions):
|
def test_syntax_errors(code, positions):
|
||||||
errors = [(error.start_pos, error.code) for error in _get_error_list(code)]
|
assert_comparison(code, 901, positions)
|
||||||
assert [(pos, 901) for pos in positions] == errors
|
|
||||||
|
|
||||||
|
|
||||||
def test_indentation_errors():
|
@pytest.mark.parametrize(
|
||||||
pass
|
('code', 'positions'), [
|
||||||
|
(' 1', [(1, 0)]),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_indentation_errors(code, positions):
|
||||||
|
assert_comparison(code, 903, positions)
|
||||||
|
|||||||
Reference in New Issue
Block a user