mirror of
https://github.com/davidhalter/parso.git
synced 2026-04-14 03:39:49 +08:00
Move the iter_errors and normalizer methods to grammar.py.
This commit is contained in:
@@ -9,6 +9,8 @@ from parso.python.tokenize import tokenize_lines, tokenize
|
||||
from parso.cache import parser_cache, load_module, save_module
|
||||
from parso.parser import BaseParser
|
||||
from parso.python.parser import Parser as PythonParser
|
||||
from parso.python.normalizer import ErrorFinderConfig
|
||||
from parso.python import pep8
|
||||
|
||||
_loaded_grammars = {}
|
||||
|
||||
@@ -19,6 +21,9 @@ class Grammar(object):
|
||||
|
||||
:param text: A BNF representation of your grammar.
|
||||
"""
|
||||
_error_finder_config = None
|
||||
_default_normalizer_config = pep8.PEP8NormalizerConfig()
|
||||
|
||||
def __init__(self, text, tokenizer, parser=BaseParser, diff_parser=None):
|
||||
self._pgen_grammar = generate_grammar(text)
|
||||
self._parser = parser
|
||||
@@ -120,6 +125,34 @@ class Grammar(object):
|
||||
cache_path=cache_path)
|
||||
return root_node
|
||||
|
||||
def iter_errors(self, node):
|
||||
if self._error_normalizer_config is None:
|
||||
raise ValueError("No error normalizer specified for this grammar.")
|
||||
|
||||
return self._get_normalizer_issues(node, self._error_normalizer_config)
|
||||
|
||||
def _get_normalizer(self, normalizer_config):
|
||||
if normalizer_config is None:
|
||||
normalizer_config = self._default_normalizer_config
|
||||
if normalizer_config is None:
|
||||
raise ValueError("You need to specify a normalizer, because "
|
||||
"there's no default normalizer for this tree.")
|
||||
return normalizer_config.create_normalizer(self)
|
||||
|
||||
def _normalize(self, node, normalizer_config=None):
|
||||
"""
|
||||
TODO this is not public, yet.
|
||||
The returned code will be normalized, e.g. PEP8 for Python.
|
||||
"""
|
||||
normalizer = self._get_normalizer(normalizer_config)
|
||||
return normalizer.walk(node)
|
||||
|
||||
def _get_normalizer_issues(self, node, normalizer_config=None):
|
||||
normalizer = self._get_normalizer(normalizer_config)
|
||||
normalizer.walk(node)
|
||||
return normalizer.issues
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
labels = self._pgen_grammar.symbol2number.values()
|
||||
txt = ' '.join(list(labels)[:3]) + ' ...'
|
||||
@@ -127,6 +160,9 @@ class Grammar(object):
|
||||
|
||||
|
||||
class PythonGrammar(Grammar):
|
||||
_error_normalizer_config = ErrorFinderConfig()
|
||||
_parser_cls = PythonParser
|
||||
|
||||
def __init__(self, version_info, bnf_text):
|
||||
super(PythonGrammar, self).__init__(
|
||||
bnf_text,
|
||||
@@ -134,14 +170,14 @@ class PythonGrammar(Grammar):
|
||||
parser=PythonParser,
|
||||
diff_parser=DiffParser
|
||||
)
|
||||
self._version_int = version_info
|
||||
self.version_info = version_info
|
||||
|
||||
def _tokenize_lines(self, lines):
|
||||
return tokenize_lines(lines, self._version_int)
|
||||
return tokenize_lines(lines, self.version_info)
|
||||
|
||||
def _tokenize(self, code):
|
||||
# Used by Jedi.
|
||||
return tokenize(code, self._version_int)
|
||||
return tokenize(code, self.version_info)
|
||||
|
||||
|
||||
def load_grammar(**kwargs):
|
||||
|
||||
@@ -257,7 +257,7 @@ class ErrorFinder(Normalizer):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ErrorFinder, self).__init__(*args, **kwargs)
|
||||
self._error_dict = {}
|
||||
self._version = self._grammar._version_int
|
||||
self._version = self._grammar.version_info
|
||||
|
||||
def initialize(self, node):
|
||||
from parso.python.tree import search_ancestor
|
||||
|
||||
@@ -30,8 +30,6 @@ import re
|
||||
from parso._compatibility import utf8_repr, unicode
|
||||
from parso.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf, \
|
||||
search_ancestor
|
||||
from parso.python import normalizer
|
||||
from parso.python import pep8
|
||||
from parso.python.prefix import split_prefix
|
||||
|
||||
|
||||
@@ -68,7 +66,6 @@ class PythonMixin(object):
|
||||
Some Python specific utitilies.
|
||||
"""
|
||||
__slots__ = ()
|
||||
default_normalizer_config = pep8.PEP8NormalizerConfig()
|
||||
|
||||
def get_definition(self):
|
||||
if self.type in ('newline', 'endmarker'):
|
||||
@@ -99,10 +96,6 @@ class PythonMixin(object):
|
||||
return result
|
||||
return None
|
||||
|
||||
def _iter_errors(self, grammar):
|
||||
config = normalizer.ErrorFinderConfig()
|
||||
return self._get_normalizer_issues(grammar, config)
|
||||
|
||||
|
||||
class PythonLeaf(PythonMixin, Leaf):
|
||||
__slots__ = ()
|
||||
|
||||
@@ -22,7 +22,6 @@ class NodeOrLeaf(object):
|
||||
The base class for nodes and leaves.
|
||||
"""
|
||||
__slots__ = ()
|
||||
default_normalizer_config = None
|
||||
|
||||
def get_root_node(self):
|
||||
"""
|
||||
@@ -154,28 +153,6 @@ class NodeOrLeaf(object):
|
||||
e.g. a statement.
|
||||
"""
|
||||
|
||||
def _get_normalizer(self, normalizer_config, grammar):
|
||||
if normalizer_config is None:
|
||||
normalizer_config = self.default_normalizer_config
|
||||
if normalizer_config is None:
|
||||
raise ValueError("You need to specify a normalizer, because "
|
||||
"there's no default normalizer for this tree.")
|
||||
return normalizer_config.create_normalizer(grammar)
|
||||
|
||||
def _normalize(self, grammar, normalizer_config=None):
|
||||
"""
|
||||
TODO this is not public, yet.
|
||||
The returned code will be normalized, e.g. PEP8 for Python.
|
||||
"""
|
||||
normalizer = self._get_normalizer(normalizer_config, grammar)
|
||||
return normalizer.walk(self)
|
||||
|
||||
def _get_normalizer_issues(self, grammar, normalizer_config=None):
|
||||
normalizer = self._get_normalizer(normalizer_config, grammar)
|
||||
normalizer.walk(self)
|
||||
return normalizer.issues
|
||||
|
||||
|
||||
|
||||
class Leaf(NodeOrLeaf):
|
||||
__slots__ = ('value', 'parent', 'line', 'indent', 'prefix')
|
||||
|
||||
Reference in New Issue
Block a user