Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
9.2.2023: Due to updates GitLab will be unavailable for some minutes between 9:00 and 11:00.
Open sidebar
badw-it
DHParser
Commits
1cea8f60
Commit
1cea8f60
authored
May 18, 2018
by
di68kap
Browse files
transform.py: remove_first, -last, -brackets now ignore whitespace
parent
69c8be53
Changes
1
Hide whitespace changes
Inline
Side-by-side
DHParser/transform.py
View file @
1cea8f60
...
...
@@ -462,11 +462,15 @@ def _replace_by(node: Node, child: Node):
node
.
parser
=
child
.
parser
node
.
errors
.
extend
(
child
.
errors
)
node
.
result
=
child
.
result
if
hasattr
(
child
,
'_xml_attr'
):
node
.
attributes
.
update
(
child
.
attributes
)
def
_reduce_child
(
node
:
Node
,
child
:
Node
):
node
.
errors
.
extend
(
child
.
errors
)
node
.
result
=
child
.
result
if
hasattr
(
child
,
'_xml_attr'
):
node
.
attributes
.
update
(
child
.
attributes
)
# def _pick_child(context: List[Node], criteria: CriteriaType):
...
...
@@ -826,13 +830,44 @@ remove_expendables = remove_children_if(is_expendable)
remove_anonymous_expendables
=
remove_children_if
(
lambda
ctx
:
is_anonymous
(
ctx
)
and
is_expendable
(
ctx
))
remove_anonymous_tokens
=
remove_children_if
(
lambda
ctx
:
is_token
(
ctx
)
and
is_anonymous
(
ctx
))
remove_first
=
apply_if
(
keep_children
(
slice
(
1
,
None
)),
lambda
ctx
:
len
(
ctx
[
-
1
].
children
)
>
1
)
remove_last
=
apply_if
(
keep_children
(
slice
(
None
,
-
1
)),
lambda
ctx
:
len
(
ctx
[
-
1
].
children
)
>
1
)
remove_brackets
=
apply_if
(
keep_children
(
slice
(
1
,
-
1
)),
lambda
ctx
:
len
(
ctx
[
-
1
].
children
)
>=
2
)
#
remove_first = apply_if(keep_children(slice(1, None)), lambda ctx: len(ctx[-1].children) > 1)
#
remove_last = apply_if(keep_children(slice(None, -1)), lambda ctx: len(ctx[-1].children) > 1)
#
remove_brackets = apply_if(keep_children(slice(1, -1)), lambda ctx: len(ctx[-1].children) >= 2)
remove_infix_operator
=
keep_children
(
slice
(
0
,
None
,
2
))
remove_single_child
=
apply_if
(
keep_children
(
slice
(
0
)),
lambda
ctx
:
len
(
ctx
[
-
1
].
children
)
==
1
)
def
remove_first
(
context
:
List
[
Node
]):
"""Removes the first non-whitespace child."""
node
=
context
[
-
1
]
if
node
.
children
:
for
i
,
child
in
enumerate
(
node
.
children
):
if
child
.
parser
.
ptype
!=
WHITESPACE_PTYPE
:
break
else
:
return
node
.
result
=
node
.
children
[:
i
]
+
node
.
children
[
i
+
1
:]
def
remove_last
(
context
:
List
[
Node
]):
"""Removes the last non-whitespace child."""
node
=
context
[
-
1
]
if
node
.
children
:
for
i
,
child
in
enumerate
(
reversed
(
node
.
children
)):
if
child
.
parser
.
ptype
!=
WHITESPACE_PTYPE
:
break
else
:
return
i
=
len
(
node
.
children
)
-
i
-
1
node
.
result
=
node
.
children
[:
i
]
+
node
.
children
[
i
+
1
:]
def
remove_brackets
(
context
:
List
[
Node
]):
"""Removes the first and the last non-whitespace child."""
remove_first
(
context
)
remove_last
(
context
)
@
transformation_factory
(
collections
.
abc
.
Set
)
def
remove_tokens
(
context
:
List
[
Node
],
tokens
:
AbstractSet
[
str
]
=
frozenset
()):
"""Removes any among a particular set of tokens from the immediate
...
...
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