In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

Commit 8d615f2c authored by eckhart's avatar eckhart

- some refinements

parent 3558cc35
......@@ -639,7 +639,7 @@ class EBNFCompiler(Compiler):
rule = node.children[0].content
if rule in self.rules:
first = self.rules[rule][0]
if not first._errors:
if not first.errors:
self.tree.add_error(first, 'First definition of rule "%s" '
'followed by illegal redefinitions.' % rule)
self.tree.add_error(node, 'A rule "%s" has already been defined earlier.' % rule)
......
......@@ -54,7 +54,7 @@ __all__ = ('Error',
class Error:
__slots__ = ['message', 'level', 'code', '_pos', 'orig_pos', 'line', 'column', '_node']
__slots__ = ['message', 'level', 'code', '_pos', 'orig_pos', 'line', 'column', '_node_keep']
# error levels
......@@ -87,9 +87,9 @@ class Error:
if node is not None and node._pos >= 0:
assert self._pos < 0 or self._pos == node._pos
self._pos = node._pos
self._node = None # if node is not needed, if pos has been set
self._node_keep = None # if node is not needed, if pos has been set
else:
self._node = node
self._node_keep = node # redundant: consider removing, see RootNode.collect_errors
def __str__(self):
prefix = ''
......@@ -104,9 +104,9 @@ class Error:
@property
def pos(self):
if self._pos < 0:
assert self._node and self._node.pos >= 0, "pos value not ready yet"
self._pos = self._node.pos # lazy evaluation of position
self._node = None # forget node to allow GC to free memory
assert self._node_keep and self._node_keep.pos >= 0, "pos value not ready yet"
self._pos = self._node_keep.pos # lazy evaluation of position
self._node_keep = None # forget node to allow GC to free memory
return self._pos
@property
......
......@@ -156,7 +156,7 @@ def add_parser_guard(parser_func):
if grammar.history_tracking__:
# don't track returning parsers except in case an error has occurred
# remaining = len(rest)
if grammar.moving_forward__ or (node and node._errors):
if grammar.moving_forward__ or (node and node.errors):
record = HistoryRecord(grammar.call_stack__, node, text)
grammar.history__.append(record)
# print(record.stack, record.status, rest[:20].replace('\n', '|'))
......
......@@ -236,14 +236,14 @@ class Node(collections.abc.Sized):
S-Expression-output.
"""
__slots__ = ['_result', 'children', '_len', '_pos', 'parser', '_errors', '_xml_attr', '_content']
__slots__ = ['_result', 'children', '_len', '_pos', 'parser', 'errors', '_xml_attr', '_content']
def __init__(self, parser, result: ResultType, leafhint: bool = False) -> None:
"""
Initializes the ``Node``-object with the ``Parser``-Instance
that generated the node and the parser's result.
"""
self._errors = [] # type: List[Error]
self.errors = [] # type: List[Error]
self._pos = -1 # type: int
# 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
......@@ -259,9 +259,9 @@ class Node(collections.abc.Sized):
def __str__(self):
s = "".join(str(child) for child in self.children) if self.children else self.content
if self._errors:
if self.errors:
return ' <<< Error on "%s" | %s >>> ' % \
(s, '; '.join(e.message for e in self._errors))
(s, '; '.join(e.message for e in self.errors))
return s
......@@ -452,14 +452,6 @@ class Node(collections.abc.Sized):
return self
@property
def errors(self) -> List[Error]:
"""
Returns the errors that occurred at this Node, not including any
errors from child nodes.
"""
return self._errors
@property
def attributes(self):
"""
......@@ -686,7 +678,7 @@ class RootNode(Node):
def __init__(self, node: Optional[Node] = None) -> 'RootNode':
super().__init__(ZOMBIE_PARSER, '')
self.all_errors = []
self.err_nodes = []
self.err_nodes_keep = []
self.error_flag = 0
if node is not None:
self.swallow(node)
......@@ -739,25 +731,25 @@ class RootNode(Node):
error = Error(message, code, node=node)
self.all_errors.append(error)
self.error_flag = max(self.error_flag, code)
node._errors.append(error)
self.err_nodes.append(node)
node.errors.append(error)
self.err_nodes_keep.append(node)
return self
def collect_errors(self, clear_errors=False) -> List[Error]:
def collect_errors(self) -> List[Error]:
"""Returns the list of errors, ordered bv their position.
"""
# for node in self.err_nodes: # lazy evaluation of positions
# for err in node.errors: # moved to error.Error.pos
# err.pos = node.pos
self.all_errors.sort(key=lambda e: e.pos)
for node in self.err_nodes_keep: # redundant: consider removing Error.Error._node_keep
for error in node.errors:
assert error._pos < 0 or error._pos == node.pos
error._pos = node.pos
self.err_nodes_keep = []
errors = self.all_errors
for error in self.all_errors:
_ = error.pos
if clear_errors:
self.all_errors = []
self.error_flag = 0
for node in self.select(lambda nd: True, True):
node._errors = []
# for error in self.all_errors:
# _ = error.pos
return errors
......
......@@ -448,12 +448,12 @@ def _replace_by(node: Node, child: Node):
child.parser = MockParser(node.parser.name, child.parser.ptype)
# parser names must not be overwritten, else: child.parser.name = node.parser.name
node.parser = child.parser
node._errors.extend(child._errors)
node.errors.extend(child.errors)
node.result = child.result
def _reduce_child(node: Node, child: Node):
node._errors.extend(child._errors)
node.errors.extend(child.errors)
node.result = child.result
......
......@@ -137,10 +137,10 @@ class TestRootNode:
root.add_error(tree.children[0], "error B")
root.swallow(tree)
assert root.error_flag
errors = root.collect_errors(False)
errors = root.collect_errors()
assert root.error_flag
assert errors == root.collect_errors(True)
assert not root.error_flag and not root.collect_errors()
# assert errors == root.collect_errors(True)
# assert not root.error_flag and not root.collect_errors()
error_str = "\n".join(str(e) for e in errors)
assert error_str.find("A") < error_str.find("B")
......
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