mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 05:14:29 +08:00
Use a bit better warnings for tokenizer errors.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import codecs
|
import codecs
|
||||||
|
import re
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from parso.normalizer import Normalizer, NormalizerConfig, Issue
|
from parso.normalizer import Normalizer, NormalizerConfig, Issue
|
||||||
@@ -512,7 +513,16 @@ class ErrorFinder(Normalizer):
|
|||||||
message = 'unindent does not match any outer indentation level'
|
message = 'unindent does not match any outer indentation level'
|
||||||
self._add_indentation_error(message, spacing)
|
self._add_indentation_error(message, spacing)
|
||||||
else:
|
else:
|
||||||
self._add_syntax_error('invalid syntax', leaf)
|
match = re.match('\\w{,2}("{1,3}|\'{1,3})', leaf.value)
|
||||||
|
if match is None:
|
||||||
|
message = 'invalid syntax'
|
||||||
|
else:
|
||||||
|
if len(match.group(1)) == 1:
|
||||||
|
print(match.group(1))
|
||||||
|
message = 'EOL while scanning string literal'
|
||||||
|
else:
|
||||||
|
message = 'EOF while scanning triple-quoted string literal'
|
||||||
|
self._add_syntax_error(message, leaf, overwrite=True)
|
||||||
elif leaf.type == 'name':
|
elif leaf.type == 'name':
|
||||||
if leaf.value == '__debug__' and leaf.is_definition():
|
if leaf.value == '__debug__' and leaf.is_definition():
|
||||||
if self._version < (3, 0):
|
if self._version < (3, 0):
|
||||||
@@ -672,13 +682,17 @@ class ErrorFinder(Normalizer):
|
|||||||
def _add_indentation_error(self, message, spacing):
|
def _add_indentation_error(self, message, spacing):
|
||||||
self._add_error(903, "IndentationError: " + message, spacing)
|
self._add_error(903, "IndentationError: " + message, spacing)
|
||||||
|
|
||||||
def _add_syntax_error(self, message, node):
|
def _add_syntax_error(self, message, node, overwrite=False):
|
||||||
self._add_error(901, "SyntaxError: " + message, node)
|
self._add_error(901, "SyntaxError: " + message, node, overwrite)
|
||||||
|
|
||||||
def _add_error(self, code, message, node):
|
def _add_error(self, code, message, node, overwrite=False):
|
||||||
# Check if the issues are on the same line.
|
# Check if the issues are on the same line.
|
||||||
line = node.start_pos[0]
|
line = node.start_pos[0]
|
||||||
self._error_dict.setdefault(line, (code, message, node))
|
args = (code, message, node)
|
||||||
|
if overwrite:
|
||||||
|
self._error_dict[line] = args
|
||||||
|
else:
|
||||||
|
self._error_dict.setdefault(line, args)
|
||||||
|
|
||||||
def finalize(self):
|
def finalize(self):
|
||||||
self._context.finalize()
|
self._context.finalize()
|
||||||
|
|||||||
@@ -106,6 +106,12 @@ FAILING_EXAMPLES = [
|
|||||||
r'b"\x"',
|
r'b"\x"',
|
||||||
r'b"\"',
|
r'b"\"',
|
||||||
|
|
||||||
|
# Parser/tokenize.c
|
||||||
|
r'"""',
|
||||||
|
r'"',
|
||||||
|
r"'''",
|
||||||
|
r"'",
|
||||||
|
|
||||||
# SyntaxErrors from Python/symtable.c
|
# SyntaxErrors from Python/symtable.c
|
||||||
'def f(x, x): pass',
|
'def f(x, x): pass',
|
||||||
'nonlocal a',
|
'nonlocal a',
|
||||||
@@ -319,10 +325,6 @@ def _get_actual_exception(code):
|
|||||||
if sys.version_info[:2] == (2, 6) and wanted == 'SyntaxError: unexpected EOF while parsing':
|
if sys.version_info[:2] == (2, 6) and wanted == 'SyntaxError: unexpected EOF while parsing':
|
||||||
wanted = 'SyntaxError: invalid syntax'
|
wanted = 'SyntaxError: invalid syntax'
|
||||||
|
|
||||||
if wanted == 'SyntaxError: EOL while scanning string literal':
|
|
||||||
# TODO This is not what we want in the future. Remove this.
|
|
||||||
wanted = 'SyntaxError: invalid syntax'
|
|
||||||
|
|
||||||
if wanted == 'SyntaxError: non-keyword arg after keyword arg':
|
if wanted == 'SyntaxError: non-keyword arg after keyword arg':
|
||||||
# The python 3.5+ way, a bit nicer.
|
# The python 3.5+ way, a bit nicer.
|
||||||
wanted = 'SyntaxError: positional argument follows keyword argument'
|
wanted = 'SyntaxError: positional argument follows keyword argument'
|
||||||
|
|||||||
Reference in New Issue
Block a user