Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
56c4cc43
Commit
56c4cc43
authored
Mar 10, 2018
by
eckhart
Browse files
- transform.py: added apply_unless()
parent
a9354ef2
Changes
1
Hide whitespace changes
Inline
Side-by-side
DHParser/transform.py
View file @
56c4cc43
...
...
@@ -53,6 +53,7 @@ __all__ = ('TransformationDict',
'merge_children'
,
'replace_content'
,
'apply_if'
,
'apply_unless'
,
'traverse_locally'
,
'is_anonymous'
,
'is_whitespace'
,
...
...
@@ -324,6 +325,13 @@ def apply_if(context: List[Node], transformation: Callable, condition: Callable)
transformation
(
context
)
@
transformation_factory
(
Callable
)
def
apply_unless
(
context
:
List
[
Node
],
transformation
:
Callable
,
condition
:
Callable
):
"""Applies a transformation if a certain condition is *not* met."""
if
not
condition
(
context
):
transformation
(
context
)
#######################################################################
#
# conditionals that determine whether the context (or the last node in
...
...
@@ -338,14 +346,17 @@ def apply_if(context: List[Node], transformation: Callable, condition: Callable)
def
is_single_child
(
context
:
List
[
Node
])
->
bool
:
"""Returns ``True`` if the current node does not have any siblings."""
return
len
(
context
[
-
2
].
children
)
==
1
def
is_named
(
context
:
List
[
Node
])
->
bool
:
"""Returns ``True`` if the current node's parser is a named parser."""
return
bool
(
context
[
-
1
].
parser
.
name
)
def
is_anonymous
(
context
:
List
[
Node
])
->
bool
:
"""Returns ``True`` if the current node's parser is an anonymous parser."""
return
not
context
[
-
1
].
parser
.
name
...
...
@@ -356,10 +367,13 @@ def is_whitespace(context: List[Node]) -> bool:
def
is_empty
(
context
:
List
[
Node
])
->
bool
:
"""Returns ``True`` if the current node's content is empty."""
return
not
context
[
-
1
].
result
def
is_expendable
(
context
:
List
[
Node
])
->
bool
:
"""Returns ``True`` if the current node either is a node containing
whitespace or an empty node."""
return
is_empty
(
context
)
or
is_whitespace
(
context
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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