Commit e43fc882 authored by di68kap's avatar di68kap
Browse files

bug fix: transform.has_parent now recognizes the oldest parent, too

parent 9bebcbdc
......@@ -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))
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
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