Commit 6eb43c46 authored by di68kap's avatar di68kap

- DHParser/parse.py/class Alternative: now uses return value optimization

parent 63217a52
......@@ -408,7 +408,7 @@ def is_insignificant_whitespace(context: List[Node]) -> bool:
return context[-1].tag_name == WHITESPACE_PTYPE
RX_WHITESPACE = re.compile(r'\s*')
RX_WHITESPACE = re.compile(r'\s+')
def contains_only_whitespace(context: List[Node]) -> bool:
......@@ -416,7 +416,8 @@ def contains_only_whitespace(context: List[Node]) -> bool:
of the tag_name, i.e. nodes the content of which matches the regular
expression /\s*/, including empty nodes. Note, that this is not true
for anonymous whitespace nodes that contain comments."""
return bool(RX_WHITESPACE.match(context[-1].content))
content = context[-1].content
return bool(not content or RX_WHITESPACE.match(context[-1].content))
def is_any_kind_of_whitespace(context: List[Node]) -> bool:
......@@ -869,7 +870,7 @@ def move_adjacent(context, condition: Callable = is_insignificant_whitespace):
@transformation_factory(collections.abc.Callable)
def lstrip(context: List[Node], condition: Callable = is_expendable):
def lstrip(context: List[Node], condition: Callable = contains_only_whitespace):
"""Recursively removes all leading child-nodes that fulfill a given condition."""
node = context[-1]
i = 1
......@@ -883,7 +884,7 @@ def lstrip(context: List[Node], condition: Callable = is_expendable):
@transformation_factory(collections.abc.Callable)
def rstrip(context: List[Node], condition: Callable = is_expendable):
def rstrip(context: List[Node], condition: Callable = contains_only_whitespace):
"""Recursively removes all leading nodes that fulfill a given condition."""
node = context[-1]
i, L = 0, len(node.children)
......@@ -898,7 +899,7 @@ def rstrip(context: List[Node], condition: Callable = is_expendable):
@transformation_factory(collections.abc.Callable)
def strip(context: List[Node], condition: Callable = is_expendable):
def strip(context: List[Node], condition: Callable = contains_only_whitespace):
"""Removes leading and trailing child-nodes that fulfill a given condition."""
lstrip(context, condition)
rstrip(context, condition)
......
......@@ -28,7 +28,7 @@ from DHParser.syntaxtree import Node, parse_sxpr, flatten_sxpr, parse_xml, PLACE
tree_sanity_check, TOKEN_PTYPE
from DHParser.transform import traverse, reduce_single_child, remove_whitespace, move_adjacent, \
traverse_locally, collapse, collapse_if, lstrip, rstrip, remove_content, remove_tokens, \
transformation_factory, has_parent
transformation_factory, has_parent, contains_only_whitespace
from DHParser.toolkit import typing
from typing import AbstractSet, List, Sequence, Tuple
......@@ -36,6 +36,13 @@ from typing import AbstractSet, List, Sequence, Tuple
class TestRemoval:
"""Tests removing transformations."""
def test_contains_only_whitespace(self):
assert contains_only_whitespace([Node('test', ' ')])
assert contains_only_whitespace([Node('test', '')])
assert contains_only_whitespace([Node('test', '\n')])
assert not contains_only_whitespace([Node('test', 'Katze')])
assert contains_only_whitespace([Node('test', ' tag ')])
def test_lstrip(self):
cst = parse_sxpr('(_Token (:Whitespace " ") (:Re test))')
lstrip([cst])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment