Commit 54722cc2 authored by eckhart's avatar eckhart
Browse files

- minor adjustments

parent acc52924
...@@ -164,7 +164,7 @@ ZOMBIE_PARSER = ZombieParser() ...@@ -164,7 +164,7 @@ ZOMBIE_PARSER = ZombieParser()
ChildrenType = Tuple['Node', ...] ChildrenType = Tuple['Node', ...]
NoChildren = cast(ChildrenType, ()) # type: ChildrenType NoChildren = cast(ChildrenType, ()) # type: ChildrenType
StrictResultType = Union[ChildrenType, str] StrictResultType = Union[ChildrenType, StringView, str]
ResultType = Union[ChildrenType, 'Node', StringView, str, None] ResultType = Union[ChildrenType, 'Node', StringView, str, None]
...@@ -194,6 +194,10 @@ class Node(collections.abc.Sized): ...@@ -194,6 +194,10 @@ class Node(collections.abc.Sized):
children (tuple): The tuple of child nodes or an empty tuple children (tuple): The tuple of child nodes or an empty tuple
if there are no child nodes. READ ONLY! if there are no child nodes. READ ONLY!
content (str): Yields the contents of the tree as string. The
difference to ``str(node)`` is that ``node.content`` does
not add the error messages to the returned string.
parser (Parser): The parser which generated this node. parser (Parser): The parser which generated this node.
WARNING: In case you use mock syntax trees for testing or WARNING: In case you use mock syntax trees for testing or
parser replacement during the AST-transformation: DO NOT parser replacement during the AST-transformation: DO NOT
...@@ -232,10 +236,13 @@ class Node(collections.abc.Sized): ...@@ -232,10 +236,13 @@ class Node(collections.abc.Sized):
dictionary is created lazily upon first usage. The attributes dictionary is created lazily upon first usage. The attributes
will only be shown in the XML-Representation, not in the will only be shown in the XML-Representation, not in the
S-Expression-output. S-Expression-output.
_parent (Node): SLOT RESERVED FOR FUTURE USE!
""" """
__slots__ = ['_result', 'children', '_errors', '_len', '_pos', 'parser', 'error_flag', __slots__ = ['_result', 'children', '_errors', '_len', '_pos', 'parser', 'error_flag',
'_xml_attr'] '_xml_attr', '_parent']
def __init__(self, parser, result: ResultType, leafhint: bool = False) -> None: def __init__(self, parser, result: ResultType, leafhint: bool = False) -> None:
""" """
...@@ -248,7 +255,7 @@ class Node(collections.abc.Sized): ...@@ -248,7 +255,7 @@ class Node(collections.abc.Sized):
# Assignment to self.result initializes the attributes _result, children and _len # Assignment to self.result initializes the attributes _result, children and _len
# The following if-clause is merely an optimization, i.e. a fast-path for leaf-Nodes # The following if-clause is merely an optimization, i.e. a fast-path for leaf-Nodes
if leafhint: if leafhint:
self._result = str(result) # type: StrictResultType self._result = result # type: StrictResultType
self.children = NoChildren # type: ChildrenType self.children = NoChildren # type: ChildrenType
self._len = -1 # type: int # lazy evaluation self._len = -1 # type: int # lazy evaluation
else: else:
...@@ -412,7 +419,6 @@ class Node(collections.abc.Sized): ...@@ -412,7 +419,6 @@ class Node(collections.abc.Sized):
""" """
Returns content as string, omitting error messages. Returns content as string, omitting error messages.
""" """
# TODO: Optimize with caching and StringViews
if self.children: if self.children:
return "".join(child.content for child in self.children) return "".join(child.content for child in self.children)
return cast(str, self._result) return cast(str, self._result)
......
...@@ -11,7 +11,7 @@ Match-test "1" ...@@ -11,7 +11,7 @@ Match-test "1"
### CST ### AST
(LB (LB
"" ""
"" ""
......
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