Commit cafc209b authored by di68kap's avatar di68kap
Browse files

- DHParser submodule: bugfix. length values for later added children

parent b3997558
...@@ -445,7 +445,6 @@ def log_ST(syntax_tree, log_file_name): ...@@ -445,7 +445,6 @@ def log_ST(syntax_tree, log_file_name):
with open(path, "w", encoding="utf-8") as f: with open(path, "w", encoding="utf-8") as f:
f.write(syntax_tree.as_sxpr()) f.write(syntax_tree.as_sxpr())
LOG_SIZE_THRESHOLD = 10000 # maximum number of history records to log LOG_SIZE_THRESHOLD = 10000 # maximum number of history records to log
LOG_TAIL_THRESHOLD = 500 # maximum number of history recors for "tail log" LOG_TAIL_THRESHOLD = 500 # maximum number of history recors for "tail log"
......
...@@ -262,7 +262,8 @@ class Parser: ...@@ -262,7 +262,8 @@ class Parser:
def __call__(self: 'Parser', text: StringView) -> Tuple[Optional[Node], StringView]: def __call__(self: 'Parser', text: StringView) -> Tuple[Optional[Node], StringView]:
"""Applies the parser to the given text. This is a wrapper method that adds """Applies the parser to the given text. This is a wrapper method that adds
the business intelligence that is common to all parsers. The actual parsing is the business intelligence that is common to all parsers. The actual parsing is
done in the overridden method `_parse()`. done in the overridden method `_parse()`. This wrapper-method can be thought of
as a "parser guard", because it guards the parsing process.
""" """
def get_error_node_id(error_node: Node, root_node: RootNode) -> int: def get_error_node_id(error_node: Node, root_node: RootNode) -> int:
if error_node: if error_node:
...@@ -715,8 +716,7 @@ class Grammar: ...@@ -715,8 +716,7 @@ class Grammar:
moving_forward__: This flag indicates that the parsing process is currently moving_forward__: This flag indicates that the parsing process is currently
moving forward . It is needed to reduce noise in history recording moving forward . It is needed to reduce noise in history recording
and should not be considered as having a valid value if history and should not be considered as having a valid value if history
recording is turned off! (See :func:`add_parser_guard` and its local recording is turned off! (See :func:`Parser.__call__`)
function :func:`guarded_call`)
recursion_locations__: Stores the locations where left recursion was recursion_locations__: Stores the locations where left recursion was
detected. Needed to provide minimal memoization for the left detected. Needed to provide minimal memoization for the left
......
...@@ -217,7 +217,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -217,7 +217,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
self._result = result # type: StrictResultType # cast(StrictResultType, result) self._result = result # type: StrictResultType # cast(StrictResultType, result)
self.children = NO_CHILDREN # type: ChildrenType self.children = NO_CHILDREN # type: ChildrenType
else: else:
self.result = result self.__set_result(result)
self.tag_name = tag_name # type: str self.tag_name = tag_name # type: str
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
...@@ -316,16 +316,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -316,16 +316,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
""" """
return self._result return self._result
@result.setter def __set_result(self, result: ResultType):
def result(self, result: ResultType):
# # made obsolete by static type checking with mypy
# assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result))
# or isinstance(result, Node)
# or isinstance(result, str)
# or isinstance(result, StringView)), "%s (%s)" % (str(result), str(type(result)))
# Possible optimization: Do not allow single nodes as argument:
# assert not isinstance(result, Node)
# self._content = None
if isinstance(result, Node): if isinstance(result, Node):
self.children = (result,) self.children = (result,)
self._result = self.children self._result = self.children
...@@ -337,6 +328,17 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -337,6 +328,17 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
self.children = NO_CHILDREN self.children = NO_CHILDREN
self._result = result # cast(StrictResultType, result) self._result = result # cast(StrictResultType, result)
@result.setter
def result(self, result: ResultType):
self.__set_result(result)
# fix position values for children that are added after the parsing process
if self._pos >= 0 and self.children:
p = self._pos
for child in self.children:
if child._pos < 0:
child.with_pos(p)
p = child._pos + len(child)
def _content(self) -> List[str]: def _content(self) -> List[str]:
""" """
Returns string content as list of string fragments Returns string content as list of string fragments
...@@ -380,7 +382,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -380,7 +382,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
def with_pos(self, pos: int) -> 'Node': def with_pos(self, pos: int) -> 'Node':
""" """
Initialize position value. Usually, the parser guard Initialize position value. Usually, the parser guard
(`parsers.add_parser_guard()`) takes care of assigning the (`parse.Parser.__call__`) takes care of assigning the
position in the document to newly created nodes. However, position in the document to newly created nodes. However,
when Nodes are created outside the reach of the parser when Nodes are created outside the reach of the parser
guard, their document-position must be assigned manually. guard, their document-position must be assigned manually.
...@@ -393,6 +395,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -393,6 +395,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
# % (self._pos, pos, repr(self))) # % (self._pos, pos, repr(self)))
if pos != self._pos >= 0: if pos != self._pos >= 0:
raise AssertionError("Position value cannot be reassigned to a different value!") raise AssertionError("Position value cannot be reassigned to a different value!")
assert pos >= 0, "Negative value %i not allowed!"
if self._pos < 0: if self._pos < 0:
self._pos = pos self._pos = pos
# recursively adjust pos-values of all children # recursively adjust pos-values of all children
......
Supports Markdown
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