Commit bafb857e authored by di68kap's avatar di68kap

- syntaxtree.py: Node.__len__ and .__bool__ methods added, .len property removed

parent 0a6602bf
......@@ -184,7 +184,7 @@ class HistoryRecord:
@property
def extent(self) -> slice:
return (slice(-self.remaining - self.node.len, -self.remaining) if self.node
return (slice(-self.remaining - len(self.node), -self.remaining) if self.node
else slice(-self.remaining, None))
@staticmethod
......
......@@ -220,6 +220,15 @@ class Node:
return "Node(%s, %s)" % (parg, rarg)
def __len__(self):
return self._len
def __bool__(self):
# A node that is not None is always True, even if it's empty
return True
def __eq__(self, other):
# return str(self.parser) == str(other.parser) and self.result == other.result
return self.tag_name == other.tag_name and self.result == other.result
......@@ -257,13 +266,6 @@ class Node:
if isinstance(self._result, tuple) else cast(ChildrenType, ()) # type: ChildrenType
self.error_flag = any(r.error_flag for r in self.children) # type: bool
@property
def len(self) -> int:
# DEBUGGING: print(self.tag_name, str(self.pos), str(self._len), str(self)[:10].replace('\n','.'))
return self._len
@property
def pos(self) -> int:
assert self._pos >= 0, "position value not initialized!"
......@@ -276,7 +278,7 @@ class Node:
offset = 0
for child in self.children:
child.pos = pos + offset
offset += child.len
offset += len(child)
@property
......
......@@ -87,7 +87,7 @@ def mem_profile(func):
print(stat)
if __name__ == "__main__":
cpu_profile(tst_func)
mem_profile(tst_func)
......
......@@ -115,11 +115,11 @@ class TestNode:
def test_len_and_pos(self):
"""Test len-property of Node."""
nd1 = Node(None, "123")
assert nd1.len == 3, "Expected Node.len == 3, got %i" % nd1.len
assert len(nd1) == 3, "Expected Node.len == 3, got %i" % len(nd1)
nd2 = Node(None, "456")
assert nd2.len == 3, "Expected Node.len == 3, got %i" % nd1.len
assert len(nd2) == 3, "Expected Node.len == 3, got %i" % len(nd2)
nd = Node(None, (nd1, nd2))
assert nd.len == 6, "Expected Node.len == 6, got %i" % nd.len
assert len(nd) == 6, "Expected Node.len == 6, got %i" % len(nd)
nd.pos = 0
assert nd.pos == 0, "Expected Node.pos == 0, got %i" % nd.pos
assert nd1.pos == 0, "Expected Node.pos == 0, got %i" % nd1.pos
......
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