Commit 6dabf495 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

- syntaxtree.py: Node: __slots__ added for memory optimization; need to optimize caching

parent 79f5bbdd
...@@ -260,8 +260,7 @@ def add_parser_guard(parser_func): ...@@ -260,8 +260,7 @@ def add_parser_guard(parser_func):
node, rest = parser_func(parser, text) node, rest = parser_func(parser, text)
if node is None: if node is None:
# retrieve an earlier match result (from left recursion) # retrieve an earlier match result (from left recursion) if it exists
# if it exists
node, rest = parser.visited.get(location, (None, rest)) node, rest = parser.visited.get(location, (None, rest))
# don't overwrite any positive match (i.e. node not None) in the cache # don't overwrite any positive match (i.e. node not None) in the cache
# and don't add empty entries for parsers returning from left recursive calls! # and don't add empty entries for parsers returning from left recursive calls!
......
...@@ -187,12 +187,15 @@ class Node: ...@@ -187,12 +187,15 @@ class Node:
AST-transformation. AST-transformation.
""" """
__slots__ = ['_result', 'children', '_errors', '_len', '_pos', 'parser', 'error_flag']
def __init__(self, parser, result: ResultType) -> None: def __init__(self, parser, result: ResultType) -> None:
"""Initializes the ``Node``-object with the ``Parser``-Instance """Initializes the ``Node``-object with the ``Parser``-Instance
that generated the node and the parser's result. that generated the node and the parser's result.
""" """
# self._result = '' # type: StrictResultType # self._result = '' # type: StrictResultType
# self.children = () # type: ChildrenType # self.children = () # type: ChildrenType
# self.error_flag = False # type: bool
self._errors = [] # type: List[str] self._errors = [] # type: List[str]
self.result = result self.result = result
self._len = len(result) if not self.children else \ self._len = len(result) if not self.children else \
...@@ -481,6 +484,11 @@ class Node: ...@@ -481,6 +484,11 @@ class Node:
# return nav(path.split('/')) # return nav(path.split('/'))
def tree_size(self) -> int:
"""Recursively counts the number of nodes in the tree including the root node."""
return sum(child.tree_size() for child in self.children) + 1
def log(self, log_file_name): def log(self, log_file_name):
if is_logging(): if is_logging():
path = os.path.join(log_dir(), log_file_name) path = os.path.join(log_dir(), log_file_name)
......
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