Commit 2721ad5f authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

- bugfix history logging: now also logs cached matches

parent 35f247ac
...@@ -161,6 +161,12 @@ def add_parser_guard(parser_func): ...@@ -161,6 +161,12 @@ def add_parser_guard(parser_func):
def guarded_call(parser: 'Parser', text: str) -> Tuple[Node, str]: def guarded_call(parser: 'Parser', text: str) -> Tuple[Node, str]:
try: try:
location = len(text) location = len(text)
grammar = parser.grammar # grammar may be 'None' for unconnected parsers!
if grammar.history_tracking:
grammar.call_stack.append(parser)
grammar.moving_forward = True
# if location has already been visited by the current parser, # if location has already been visited by the current parser,
# return saved result # return saved result
if location in parser.visited: if location in parser.visited:
...@@ -170,24 +176,10 @@ def add_parser_guard(parser_func): ...@@ -170,24 +176,10 @@ def add_parser_guard(parser_func):
return None, text return None, text
parser.recursion_counter[location] += 1 parser.recursion_counter[location] += 1
grammar = parser.grammar # grammar may be 'None' for unconnected parsers!
if grammar.history_tracking:
grammar.call_stack.append(parser)
grammar.moving_forward = True
# run original __call__ method # run original __call__ method
node, rest = parser_func(parser, text) node, rest = parser_func(parser, text)
if grammar.history_tracking:
# don't track returning parsers except in case an error has occurred
if grammar.moving_forward or (node and node._errors):
grammar.moving_forward = False
record = HistoryRecord(grammar.call_stack.copy(), node, len(rest))
grammar.history.append(record)
# print(record.stack, record.status, rest[:20].replace('\n', '|'))
grammar.call_stack.pop()
if node is not None: if node is not None:
# in case of a recursive call saves the result of the first # in case of a recursive call saves the result of the first
# (or left-most) call that matches # (or left-most) call that matches
...@@ -200,6 +192,15 @@ def add_parser_guard(parser_func): ...@@ -200,6 +192,15 @@ def add_parser_guard(parser_func):
parser.recursion_counter[location] -= 1 parser.recursion_counter[location] -= 1
if grammar.history_tracking:
# don't track returning parsers except in case an error has occurred
if grammar.moving_forward or (node and node._errors):
grammar.moving_forward = False
record = HistoryRecord(grammar.call_stack.copy(), node, len(rest))
grammar.history.append(record)
# print(record.stack, record.status, rest[:20].replace('\n', '|'))
grammar.call_stack.pop()
except RecursionError: except RecursionError:
node = Node(None, text[:min(10, max(1, text.find("\n")))] + " ...") node = Node(None, text[:min(10, max(1, text.find("\n")))] + " ...")
node.add_error("maximum recursion depth of parser reached; " node.add_error("maximum recursion depth of parser reached; "
......
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