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

Commit 506cd427 authored by Eckhart Arnold's avatar Eckhart Arnold

- some cleanups

parent 909142bd
...@@ -425,10 +425,14 @@ class Parser(ParserBase): ...@@ -425,10 +425,14 @@ class Parser(ParserBase):
Applies function `func(parser)` recursively to this parser and all Applies function `func(parser)` recursively to this parser and all
descendant parsers if any exist. The same function can never descendant parsers if any exist. The same function can never
be applied twice between calls of the ``reset()``-method! be applied twice between calls of the ``reset()``-method!
Returns `True`, if function has been applied, `False` if function
had been applied earlier already and thus has not been applied again.
""" """
if func in self.cycle_detection: if func in self.cycle_detection:
return False return False
else: else:
assert not self.visited, "No calls to Parser.apply() during or " \
"after ongoing parsing process. (Call Parser.reset() first.)"
self.cycle_detection.add(func) self.cycle_detection.add(func)
func(self) func(self)
return True return True
...@@ -1147,7 +1151,7 @@ class RE(Parser): ...@@ -1147,7 +1151,7 @@ class RE(Parser):
self.wspRight = self.grammar.wsp_right_parser__ self.wspRight = self.grammar.wsp_right_parser__
def apply(self, func: Parser.ApplyFunc) -> bool: def apply(self, func: Parser.ApplyFunc) -> bool:
if super(RE, self).apply(func): if super().apply(func):
if self.wL: if self.wL:
self.wspLeft.apply(func) self.wspLeft.apply(func)
if self.wR: if self.wR:
...@@ -1208,7 +1212,7 @@ class UnaryOperator(Parser): ...@@ -1208,7 +1212,7 @@ class UnaryOperator(Parser):
return self.__class__(parser, self.name) return self.__class__(parser, self.name)
def apply(self, func: Parser.ApplyFunc) -> bool: def apply(self, func: Parser.ApplyFunc) -> bool:
if super(UnaryOperator, self).apply(func): if super().apply(func):
self.parser.apply(func) self.parser.apply(func)
return True return True
return False return False
...@@ -1235,7 +1239,7 @@ class NaryOperator(Parser): ...@@ -1235,7 +1239,7 @@ class NaryOperator(Parser):
return self.__class__(*parsers, name=self.name) return self.__class__(*parsers, name=self.name)
def apply(self, func: Parser.ApplyFunc) -> bool: def apply(self, func: Parser.ApplyFunc) -> bool:
if super(NaryOperator, self).apply(func): if super().apply(func):
for parser in self.parsers: for parser in self.parsers:
parser.apply(func) parser.apply(func)
return True return True
...@@ -1906,8 +1910,7 @@ class Forward(Parser): ...@@ -1906,8 +1910,7 @@ class Forward(Parser):
self.parser = parser self.parser = parser
def apply(self, func: Parser.ApplyFunc) -> bool: def apply(self, func: Parser.ApplyFunc) -> bool:
if super(Forward, self).apply(func): if super().apply(func):
assert not self.visited
self.parser.apply(func) self.parser.apply(func)
return True return True
return False return False
......
...@@ -145,6 +145,7 @@ ZOMBIE_PARSER = ZombieParser() ...@@ -145,6 +145,7 @@ ZOMBIE_PARSER = ZombieParser()
ChildrenType = Tuple['Node', ...] ChildrenType = Tuple['Node', ...]
NoChildren = cast(ChildrenType, ()) # type: ChildrenType
StrictResultType = Union[ChildrenType, StringView, str] StrictResultType = Union[ChildrenType, StringView, str]
ResultType = Union[ChildrenType, 'Node', StringView, str, None] ResultType = Union[ChildrenType, 'Node', StringView, str, None]
...@@ -172,11 +173,11 @@ class Node(collections.abc.Sized): ...@@ -172,11 +173,11 @@ class Node(collections.abc.Sized):
tuple of child nodes. tuple of child nodes.
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!
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
rely on this being a real parser object in any phase after rely on this being a real parser object in any phase after
parsing (i.e. AST-transformation and compiling), for parsing (i.e. AST-transformation and compiling), for
example by calling ``isinstance(node.parer, ...)``. example by calling ``isinstance(node.parer, ...)``.
errors (list): A list of parser- or compiler-errors: errors (list): A list of parser- or compiler-errors:
tuple(position, string) attached to this node tuple(position, string) attached to this node
...@@ -190,15 +191,15 @@ class Node(collections.abc.Sized): ...@@ -190,15 +191,15 @@ class Node(collections.abc.Sized):
through AST-transformation. READ ONLY! through AST-transformation. READ ONLY!
pos (int): the position of the node within the parsed text. pos (int): the position of the node within the parsed text.
The value of ``pos`` is -1 meaning invalid by default. The value of ``pos`` is -1 meaning invalid by default.
Setting this value will set the positions of all child Setting this value will set the positions of all child
nodes relative to this value. nodes relative to this value.
To set the pos values of all nodes in a syntax tree, the To set the pos values of all nodes in a syntax tree, the
pos value of the root node should be set to 0 right pos value of the root node should be set to 0 right
after parsing. after parsing.
Other than that, this value should be considered READ ONLY. Other than that, this value should be considered READ ONLY.
At any rate, it should only be reassigned only during At any rate, it should only be reassigned only during
parsing stage and never during or after the parsing stage and never during or after the
AST-transformation. AST-transformation.
...@@ -285,7 +286,7 @@ class Node(collections.abc.Sized): ...@@ -285,7 +286,7 @@ class Node(collections.abc.Sized):
# self._result = (result,) if isinstance(result, Node) else str(result) \ # self._result = (result,) if isinstance(result, Node) else str(result) \
# if isinstance(result, StringView) else result or '' # type: StrictResultType # if isinstance(result, StringView) else result or '' # type: StrictResultType
self.children = cast(ChildrenType, self._result) \ self.children = cast(ChildrenType, self._result) \
if isinstance(self._result, tuple) else cast(ChildrenType, ()) # type: ChildrenType if isinstance(self._result, tuple) else NoChildren # type: ChildrenType
if self.children: if self.children:
self.error_flag = max(self.error_flag, self.error_flag = max(self.error_flag,
max(child.error_flag for child in self.children)) # type: bool max(child.error_flag for child in self.children)) # type: bool
......
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