Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
DHParser
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
badw-it
DHParser
Commits
6eb43c46
Commit
6eb43c46
authored
Feb 13, 2019
by
di68kap
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- DHParser/parse.py/class Alternative: now uses return value optimization
parent
63217a52
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
6 deletions
+14
-6
DHParser/transform.py
DHParser/transform.py
+6
-5
test/test_transform.py
test/test_transform.py
+8
-1
No files found.
DHParser/transform.py
View file @
6eb43c46
...
...
@@ -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_expendabl
e
):
def
lstrip
(
context
:
List
[
Node
],
condition
:
Callable
=
contains_only_whitespac
e
):
"""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_expendabl
e
):
def
rstrip
(
context
:
List
[
Node
],
condition
:
Callable
=
contains_only_whitespac
e
):
"""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_expendabl
e
):
def
strip
(
context
:
List
[
Node
],
condition
:
Callable
=
contains_only_whitespac
e
):
"""Removes leading and trailing child-nodes that fulfill a given condition."""
lstrip
(
context
,
condition
)
rstrip
(
context
,
condition
)
...
...
test/test_transform.py
View file @
6eb43c46
...
...
@@ -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
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment