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

Commit 8aa3ba60 authored by di68kap's avatar di68kap

- DHParser/parse.py: fix error reporting in case of lookahead

- DHParser/stringview.py, log.py: better object representation
parent 3286cce7
......@@ -257,6 +257,12 @@ class HistoryRecord:
def __str__(self):
return '%4i, %2i: %s; %s; "%s"' % self.as_tuple()
def __repr__(self):
return 'HistoryRecord(%s, %s, %s, %s, %s)' % \
(repr(self.call_stack), repr(self.node), repr(self.text),
repr(self.line_col), repr(self.errors))
def as_tuple(self) -> 'Snapshot':
"""
Returns history record formatted as a snapshot tuple.
......
......@@ -997,8 +997,8 @@ class Grammar:
error_code = Error.PARSER_DID_NOT_MATCH
else:
stitches.append(result)
h = self.history__[-1] if self.history__ else \
HistoryRecord([], None, StringView(''), (0, 0))
h = HistoryRecord.most_advanced_match(self.history__) \
if self.history__ else HistoryRecord([], None, StringView(''), (0, 0))
if h.status == h.MATCH and (h.node.pos + len(h.node) == len(self.document__)):
# TODO: this case still needs unit-tests and support in testing.py
error_msg = "Parser stopped before end, but matched with lookahead."
......@@ -1022,6 +1022,7 @@ class Grammar:
# for record in self.history__:
# if record.node and record.node._pos < 0:
# record.node.with_pos(0)
# print(self.call_stack__)
record = HistoryRecord(self.call_stack__.copy(), stitches[-1], rest,
self.line_col__(rest))
self.history__.append(record)
......
......@@ -131,6 +131,9 @@ class StringView: # collections.abc.Sized
self._fullstring = self._text[self._begin:self._end]
return self._fullstring
def __repr__(self) -> str:
return repr(str(self))
def __eq__(self, other) -> bool:
# PERFORMANCE WARNING: This creates copies of the strings
return len(other) == len(self) and str(self) == str(other)
......
......@@ -351,11 +351,10 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report='REPORT'
operator at the end. See test_testing.TestLookahead.
"""
raw_errors = syntax_tree.errors_sorted
is_artifact = ((2 <= len(raw_errors) == 3 # case 1: superfluous data for lookahead
and {e.code for e in raw_errors} <=
is_artifact = ({e.code for e in raw_errors} <=
{Error.PARSER_LOOKAHEAD_FAILURE_ONLY,
# Error.PARSER_STOPPED_BEFORE_END,
Error.PARSER_LOOKAHEAD_MATCH_ONLY})
Error.PARSER_LOOKAHEAD_MATCH_ONLY}
or (len(raw_errors) == 1
and (raw_errors[-1].code == Error.PARSER_LOOKAHEAD_MATCH_ONLY
# case 2: mandatory lookahead failure at end of text
......
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