mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-08 05:34:51 +08:00
Start adding normalizers instead of the get_code(normalize=True) function.
This commit is contained in:
13
parso/normalizer.py
Normal file
13
parso/normalizer.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
class Normalizer():
|
||||||
|
def normalize(self, leaf):
|
||||||
|
return leaf.prefix + leaf.value
|
||||||
|
|
||||||
|
def iter_errors(self, leaf):
|
||||||
|
return iter([])
|
||||||
|
|
||||||
|
|
||||||
|
class Error():
|
||||||
|
def __init__(self, leaf, code, message):
|
||||||
|
self._leaf = leaf
|
||||||
|
self.code = code
|
||||||
|
self.message = message
|
||||||
20
parso/python/normalizer.py
Normal file
20
parso/python/normalizer.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from parso.normalizer import Normalizer, Error
|
||||||
|
|
||||||
|
|
||||||
|
class CompressNormalizer(Normalizer):
|
||||||
|
"""
|
||||||
|
Removes comments and whitespace.
|
||||||
|
"""
|
||||||
|
def normalize(self, leaf):
|
||||||
|
return leaf.prefix + leaf.value
|
||||||
|
|
||||||
|
|
||||||
|
class PEP8Normalizer(Normalizer):
|
||||||
|
"""
|
||||||
|
Normalizing to PEP8. Not really implemented, yet.
|
||||||
|
"""
|
||||||
|
def normalize(self, leaf):
|
||||||
|
return leaf.prefix + leaf.value
|
||||||
|
|
||||||
|
def iter_errors(self, leaf):
|
||||||
|
return iter([])
|
||||||
@@ -28,6 +28,7 @@ Any subclasses of :class:`Scope`, including :class:`Module` has an attribute
|
|||||||
from parso._compatibility import utf8_repr, unicode
|
from parso._compatibility import utf8_repr, unicode
|
||||||
from parso.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf, \
|
from parso.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf, \
|
||||||
search_ancestor
|
search_ancestor
|
||||||
|
from parso.python import normalizer
|
||||||
|
|
||||||
|
|
||||||
class DocstringMixin(object):
|
class DocstringMixin(object):
|
||||||
@@ -63,6 +64,7 @@ class PythonMixin(object):
|
|||||||
Some Python specific utitilies.
|
Some Python specific utitilies.
|
||||||
"""
|
"""
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
default_normalizer = normalizer.PEP8Normalizer()
|
||||||
|
|
||||||
def get_definition(self):
|
def get_definition(self):
|
||||||
if self.type in ('newline', 'endmarker'):
|
if self.type in ('newline', 'endmarker'):
|
||||||
@@ -94,7 +96,7 @@ class PythonMixin(object):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class PythonLeaf(Leaf, PythonMixin):
|
class PythonLeaf(PythonMixin, Leaf):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
@@ -110,19 +112,19 @@ class _LeafWithoutNewlines(PythonLeaf):
|
|||||||
|
|
||||||
|
|
||||||
# Python base classes
|
# Python base classes
|
||||||
class PythonBaseNode(BaseNode, PythonMixin):
|
class PythonBaseNode(PythonMixin, BaseNode):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
class PythonNode(Node, PythonMixin):
|
class PythonNode(PythonMixin, Node):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
class PythonErrorNode(ErrorNode, PythonMixin):
|
class PythonErrorNode(PythonMixin, ErrorNode):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
class PythonErrorLeaf(ErrorLeaf, PythonMixin):
|
class PythonErrorLeaf(PythonMixin, ErrorLeaf):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
@@ -1010,7 +1012,7 @@ class Param(PythonBaseNode):
|
|||||||
"""
|
"""
|
||||||
return search_ancestor(self, 'funcdef', 'lambdef')
|
return search_ancestor(self, 'funcdef', 'lambdef')
|
||||||
|
|
||||||
def get_code(self, normalized=False, include_prefix=True, include_comma=True):
|
def get_code(self, include_prefix=True, include_comma=True):
|
||||||
"""
|
"""
|
||||||
Like all the other get_code functions, but includes the param
|
Like all the other get_code functions, but includes the param
|
||||||
`include_comma`.
|
`include_comma`.
|
||||||
@@ -1018,14 +1020,13 @@ class Param(PythonBaseNode):
|
|||||||
:param include_comma bool: If enabled includes the comma in the string output.
|
:param include_comma bool: If enabled includes the comma in the string output.
|
||||||
"""
|
"""
|
||||||
if include_comma:
|
if include_comma:
|
||||||
return super(Param, self).get_code(normalized, include_prefix)
|
return super(Param, self).get_code(include_prefix)
|
||||||
|
|
||||||
children = self.children
|
children = self.children
|
||||||
if children[-1] == ',':
|
if children[-1] == ',':
|
||||||
children = children[:-1]
|
children = children[:-1]
|
||||||
return self._get_code_for_children(
|
return self._get_code_for_children(
|
||||||
children,
|
children,
|
||||||
normalized=False,
|
|
||||||
include_prefix=include_prefix
|
include_prefix=include_prefix
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ class NodeOrLeaf(object):
|
|||||||
The base class for nodes and leaves.
|
The base class for nodes and leaves.
|
||||||
"""
|
"""
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
default_normalizer = None
|
||||||
|
|
||||||
def get_root_node(self):
|
def get_root_node(self):
|
||||||
"""
|
"""
|
||||||
@@ -145,17 +146,32 @@ class NodeOrLeaf(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_code(self, normalized=False, include_prefix=True):
|
def get_code(self, include_prefix=True):
|
||||||
"""
|
"""
|
||||||
Returns the code that was the input of the parser.
|
Returns the code that was the input of the parser.
|
||||||
|
|
||||||
If a normalizer is given, the returned code will be normalized and will
|
:param include_prefix: Removes the prefix (whitespace and comments) of
|
||||||
not be equal to the input.
|
e.g. a statement.
|
||||||
|
|
||||||
:param include_prefix: Removes the prefix (whitespace and comments) of e.g. a statement.
|
|
||||||
:param normalized: Deprecated. Please don't use. Will be replaced with something more powerful.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def normalize(self, normalizer=None):
|
||||||
|
"""
|
||||||
|
The returned code will be normalized, e.g. PEP8 for Python.
|
||||||
|
"""
|
||||||
|
if normalizer is None:
|
||||||
|
normalizer = self.default_normalizer
|
||||||
|
if normalizer is None:
|
||||||
|
raise ValueError("You need to specify a normalizer, because "
|
||||||
|
"there's no default normalizer for this tree.")
|
||||||
|
|
||||||
|
try:
|
||||||
|
children = self.children
|
||||||
|
except AttributeError:
|
||||||
|
return normalizer(self)
|
||||||
|
else:
|
||||||
|
return ''.join(child.normalize(normalizer) for child in children)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Leaf(NodeOrLeaf):
|
class Leaf(NodeOrLeaf):
|
||||||
__slots__ = ('value', 'parent', 'line', 'indent', 'prefix')
|
__slots__ = ('value', 'parent', 'line', 'indent', 'prefix')
|
||||||
@@ -187,9 +203,7 @@ class Leaf(NodeOrLeaf):
|
|||||||
def get_last_leaf(self):
|
def get_last_leaf(self):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get_code(self, normalized=False, include_prefix=True):
|
def get_code(self, include_prefix=True):
|
||||||
if normalized:
|
|
||||||
return self.value
|
|
||||||
if include_prefix:
|
if include_prefix:
|
||||||
return self.prefix + self.value
|
return self.prefix + self.value
|
||||||
else:
|
else:
|
||||||
@@ -238,16 +252,15 @@ class BaseNode(NodeOrLeaf):
|
|||||||
def end_pos(self):
|
def end_pos(self):
|
||||||
return self.children[-1].end_pos
|
return self.children[-1].end_pos
|
||||||
|
|
||||||
def _get_code_for_children(self, children, normalized, include_prefix):
|
def _get_code_for_children(self, children, include_prefix):
|
||||||
# TODO implement normalized (depending on context).
|
|
||||||
if include_prefix:
|
if include_prefix:
|
||||||
return "".join(c.get_code(normalized) for c in children)
|
return "".join(c.get_code() for c in children)
|
||||||
else:
|
else:
|
||||||
first = children[0].get_code(include_prefix=False)
|
first = children[0].get_code(include_prefix=False)
|
||||||
return first + "".join(c.get_code(normalized) for c in children[1:])
|
return first + "".join(c.get_code() for c in children[1:])
|
||||||
|
|
||||||
def get_code(self, normalized=False, include_prefix=True):
|
def get_code(self, include_prefix=True):
|
||||||
return self._get_code_for_children(self.children, normalized, include_prefix)
|
return self._get_code_for_children(self.children, include_prefix)
|
||||||
|
|
||||||
def get_leaf_for_position(self, position, include_prefixes=False):
|
def get_leaf_for_position(self, position, include_prefixes=False):
|
||||||
def binary_search(lower, upper):
|
def binary_search(lower, upper):
|
||||||
|
|||||||
Reference in New Issue
Block a user