10.12., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 1b5915d9 authored by Eckhart Arnold's avatar Eckhart Arnold

speed optimitations for Node creation

parent 825c4c2a
...@@ -1055,7 +1055,7 @@ class RegExp(Parser): ...@@ -1055,7 +1055,7 @@ class RegExp(Parser):
match = text.match(self.regexp) match = text.match(self.regexp)
if match: if match:
end = text.index(match.end()) end = text.index(match.end())
return Node(self, text[:end]), text[end:] return Node(self, text[:end], True), text[end:]
return None, text return None, text
def __repr__(self): def __repr__(self):
......
...@@ -208,15 +208,19 @@ class Node(collections.abc.Sized): ...@@ -208,15 +208,19 @@ class Node(collections.abc.Sized):
__slots__ = ['_result', 'children', '_errors', '_len', '_pos', 'parser', 'error_flag'] __slots__ = ['_result', 'children', '_errors', '_len', '_pos', 'parser', 'error_flag']
def __init__(self, parser, result: ResultType) -> None: def __init__(self, parser, result: ResultType, leafhint: bool=False) -> 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 = 0 # type: int self.error_flag = 0 # type: int
self._errors = [] # type: List[Error] self._errors = [] # type: List[Error]
self.result = result if leafhint:
self._result = result # type: StrictResultType
self.children = NoChildren # type: ChildrenType
else:
self.result = result
self._len = len(self._result) if not self.children else \ self._len = len(self._result) if not self.children else \
sum(child._len for child in self.children) # type: int sum(child._len for child in self.children) # type: int
# self.pos: int = 0 # continuous updating of pos values wastes a lot of time # self.pos: int = 0 # continuous updating of pos values wastes a lot of time
...@@ -282,14 +286,29 @@ class Node(collections.abc.Sized): ...@@ -282,14 +286,29 @@ class Node(collections.abc.Sized):
# or isinstance(result, str)), str(result) # or isinstance(result, str)), str(result)
# Possible optimization: Do not allow single nodes as argument: # Possible optimization: Do not allow single nodes as argument:
# assert not isinstance(result, Node) # assert not isinstance(result, Node)
self._result = (result,) if isinstance(result, Node) else result or '' # type: StrictResultType
# self._result = (result,) if isinstance(result, Node) else str(result) \ if isinstance(result, Node):
# if isinstance(result, StringView) else result or '' # type: StrictResultType self.children = (result,)
self.children = cast(ChildrenType, self._result) \ self._result = self.children
if isinstance(self._result, tuple) else NoChildren # type: ChildrenType self.error_flag = result.error_flag
if self.children: else:
self.error_flag = max(self.error_flag, if isinstance(result, tuple):
max(child.error_flag for child in self.children)) # type: bool self.children = result
self._result = result or ''
if self.children and not self.error_flag:
self.error_flag = max(child.error_flag for child in self.children)
else:
self.children = NoChildren
self._result = result
# # shorter but slower:
# self._result = (result,) if isinstance(result, Node) else result or '' # type: StrictResultType
# self.children = cast(ChildrenType, self._result) \
# if isinstance(self._result, tuple) else NoChildren # type: ChildrenType
# if self.children:
# self.error_flag = max(self.error_flag,
# max(child.error_flag for child in self.children)) # type: bool
@property @property
def pos(self) -> int: def pos(self) -> int:
...@@ -313,11 +332,13 @@ class Node(collections.abc.Sized): ...@@ -313,11 +332,13 @@ class Node(collections.abc.Sized):
def errors(self) -> List[Error]: def errors(self) -> List[Error]:
return self._errors.copy() return self._errors.copy()
def add_error(self, message: str, level: int = Error.ERROR, code: Hashable = 0) -> 'Node': def add_error(self, message: str, level: int = Error.ERROR, code: Hashable = 0) -> 'Node':
self._errors.append(Error(message, level, code)) self._errors.append(Error(message, level, code))
self.error_flag = max(self.error_flag, self._errors[-1].level) self.error_flag = max(self.error_flag, self._errors[-1].level)
return self return self
def collect_errors(self, document: Union[StringView, str] = '', clear_errors=False) \ def collect_errors(self, document: Union[StringView, str] = '', clear_errors=False) \
-> List[Error]: -> List[Error]:
""" """
......
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