Commit 4cbcfacf authored by eckhart's avatar eckhart

- added syntaxtree.Node.__deepcopy__ bugfixes!

parent 465f90f6
DHParser Version 0.8.4 (pending)
- lookahead parsers can now be unit-tested
- lookahead parsers can now be unit-tested (experimental)
- customized, i.e. faster deepcopy of syntax trees
DHParser Version 0.8.3 (20.8.2018)
......@@ -323,6 +323,10 @@ class Node(
Equality of nodes: Two nodes are considered as equal, if their tag
name is the same and if their results are equal.
Note: It is not required that two nodes have the same errors attached.
In case you need to check for error equality as well, compare a
serialization that includes error messages, as_sxpr() will do!
return self.tag_name == other.tag_name and self.result == other.result
......@@ -797,6 +801,28 @@ class RootNode(Node):
self.omit_tags = set()
self.empty_tags = set()
def __deepcopy__(self, memodict={}):
duplicate = self.__class__(None)
if self.children:
duplicate.children = copy.deepcopy(self.children)
duplicate._result = duplicate.children
duplicate.children = NoChildren
duplicate._result = self._result
duplicate.errors = copy.deepcopy(self.errors) if self.errors else []
duplicate._pos = self._pos
duplicate._len = self._len
if hasattr(self, '_xml_attr'):
duplicate._xml_attr = copy.deepcopy(self._xml_attr)
duplicate.all_errors = copy.deepcopy(self.all_errors)
duplicate.error_flag = self.error_flag
duplicate.inline_tags = self.inline_tags
duplicate.omit_tags = self.omit_tags
duplicate.empty_tags = self.empty_tags
duplicate.parser = self.parser
return duplicate
def swallow(self, node: Node) -> 'RootNode':
Put `self` in the place of `node` by copying all its data.
......@@ -29,6 +29,7 @@ from DHParser.transform import traverse, reduce_single_child, \
replace_by_single_child, flatten, remove_expendables
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler
from DHParser.dsl import grammar_provider
from DHParser.error import Error
class TestParseSxpression:
......@@ -79,6 +80,29 @@ class TestNode:
self.recurring_nodes_sexpr = '(a (b x) (c (d e) (b y)))'
self.recurr_tree = parse_sxpr(self.recurring_nodes_sexpr)
def test_deepcopy(self):
tree = RootNode(parse_sxpr('(a (b c) (d (e f) (h i)))'))
tree_copy = copy.deepcopy(tree)
assert tree == tree_copy
assert tree.as_sxpr() == parse_sxpr('(a (b c) (d (e f) (h i)))').as_sxpr()
assert tree_copy.as_sxpr() == parse_sxpr('(a (b c) (d (e f) (h i)))').as_sxpr()
tree.add_error(tree, Error('Test Error', 0))
assert not tree_copy.all_errors
assert tree.as_sxpr() != parse_sxpr('(a (b c) (d (e f) (h i)))').as_sxpr()
assert tree_copy.as_sxpr() == parse_sxpr('(a (b c) (d (e f) (h i)))').as_sxpr()
tree['d'].result = "x"
assert tree != tree_copy
assert tree_copy == parse_sxpr('(a (b c) (d (e f) (h i)))')
print(parse_sxpr('(a (b c) (d x))').as_sxpr())
assert tree == parse_sxpr('(a (b c) (d x))')
# this also checks for errors equality...
assert parse_sxpr('(a (b c) (d x))').as_sxpr() != tree.as_sxpr()
def test_str(self):
assert str(self.unique_tree) == "ceh"
assert str(self.recurr_tree) == "xey"
......@@ -157,7 +181,7 @@ class TestNode:
def test_xml_sanitizer(self):
node = Node(MockParser('tag'), '<&>')
assert node.as_xml() == '<tag>&lt;&amp;&gt;</tag>'
class TestRootNode:
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