In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

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: ...@@ -184,7 +184,7 @@ class HistoryRecord:
@property @property
def extent(self) -> slice: 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)) else slice(-self.remaining, None))
@staticmethod @staticmethod
......
...@@ -220,6 +220,15 @@ class Node: ...@@ -220,6 +220,15 @@ class Node:
return "Node(%s, %s)" % (parg, rarg) 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): def __eq__(self, other):
# return str(self.parser) == str(other.parser) and self.result == other.result # return str(self.parser) == str(other.parser) and self.result == other.result
return self.tag_name == other.tag_name and self.result == other.result return self.tag_name == other.tag_name and self.result == other.result
...@@ -257,13 +266,6 @@ class Node: ...@@ -257,13 +266,6 @@ class Node:
if isinstance(self._result, tuple) else cast(ChildrenType, ()) # type: ChildrenType if isinstance(self._result, tuple) else cast(ChildrenType, ()) # type: ChildrenType
self.error_flag = any(r.error_flag for r in self.children) # type: bool 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 @property
def pos(self) -> int: def pos(self) -> int:
assert self._pos >= 0, "position value not initialized!" assert self._pos >= 0, "position value not initialized!"
...@@ -276,7 +278,7 @@ class Node: ...@@ -276,7 +278,7 @@ class Node:
offset = 0 offset = 0
for child in self.children: for child in self.children:
child.pos = pos + offset child.pos = pos + offset
offset += child.len offset += len(child)
@property @property
......
...@@ -87,7 +87,7 @@ def mem_profile(func): ...@@ -87,7 +87,7 @@ def mem_profile(func):
print(stat) print(stat)
if __name__ == "__main__": if __name__ == "__main__":
cpu_profile(tst_func) mem_profile(tst_func)
......
...@@ -115,11 +115,11 @@ class TestNode: ...@@ -115,11 +115,11 @@ class TestNode:
def test_len_and_pos(self): def test_len_and_pos(self):
"""Test len-property of Node.""" """Test len-property of Node."""
nd1 = Node(None, "123") 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") 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)) 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 nd.pos = 0
assert nd.pos == 0, "Expected Node.pos == 0, got %i" % nd.pos assert nd.pos == 0, "Expected Node.pos == 0, got %i" % nd.pos
assert nd1.pos == 0, "Expected Node.pos == 0, got %i" % nd1.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