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
e43fc882
Commit
e43fc882
authored
Oct 17, 2018
by
di68kap
Browse files
bug fix: transform.has_parent now recognizes the oldest parent, too
parent
9bebcbdc
Changes
4
Hide whitespace changes
Inline
Side-by-side
DHParser/testing.py
View file @
e43fc882
...
...
@@ -339,6 +339,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
try
:
cst
=
parser
(
test_code
,
parser_name
)
except
UnknownParserError
as
upe
:
cst
=
RootNode
()
cst
=
cst
.
new_error
(
Node
(
ZOMBIE_PARSER
,
""
).
init_pos
(
0
),
str
(
upe
))
clean_test_name
=
str
(
test_name
).
replace
(
'*'
,
''
)
# log_ST(cst, "match_%s_%s.cst" % (parser_name, clean_test_name))
...
...
DHParser/transform.py
View file @
e43fc882
...
...
@@ -472,7 +472,7 @@ def has_parent(context: List[Node], tag_name_set: AbstractSet[str]) -> bool:
Checks whether a node with one of the given tag names appears somewhere
in the context before the last node in the context.
"""
for
i
in
range
(
2
,
len
(
context
)):
for
i
in
range
(
2
,
len
(
context
)
+
1
):
if
context
[
-
i
].
tag_name
in
tag_name_set
:
return
True
return
False
...
...
test/test_testing.py
View file @
e43fc882
...
...
@@ -222,6 +222,7 @@ class TestGrammarTest:
# print(e)
assert
len
(
errata
)
==
3
,
str
(
errata
)
# def test_get_report(self):
# parser_fac = grammar_provider(ARITHMETIC_EBNF)
# trans_fac = lambda : ARITHMETIC_EBNFTransform
...
...
test/test_transform.py
View file @
e43fc882
...
...
@@ -28,7 +28,7 @@ from DHParser.syntaxtree import Node, parse_sxpr, flatten_sxpr, parse_xml, ZOMBI
MockParser
,
TOKEN_PTYPE
from
DHParser.transform
import
traverse
,
reduce_single_child
,
remove_whitespace
,
\
traverse_locally
,
collapse
,
collapse_if
,
lstrip
,
rstrip
,
remove_content
,
remove_tokens
,
\
transformation_factory
transformation_factory
,
has_parent
from
DHParser.toolkit
import
typing
from
typing
import
AbstractSet
,
List
,
Sequence
,
Tuple
...
...
@@ -157,6 +157,14 @@ class TestTransformationFactory:
class
TestConditionalTransformations
:
"""Tests conditional transformations."""
def
test_has_parent
(
self
):
context
=
[
Node
(
MockParser
(
'A'
),
'alpha'
),
Node
(
MockParser
(
'B'
),
'beta'
),
Node
(
MockParser
(
'C'
),
'gamma'
)]
assert
has_parent
(
context
,
{
'A'
})
assert
has_parent
(
context
,
{
'B'
})
assert
not
has_parent
(
context
,
{
'C'
})
def
test_traverse_locally
(
self
):
cst
=
parse_sxpr
(
"""
(Lemma
...
...
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