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

Commit 24b39899 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

- removal of dead code and streamlining of parse.Series.__call__

parent fcb93994
......@@ -1264,9 +1264,8 @@ class Series(NaryOperator):
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
results = () # type: Tuple[Node, ...]
text_ = text # type: StringView
pos = 0
mandatory_violation = None
for parser in self.parsers:
for pos, parser in enumerate(self.parsers):
node, text_ = parser(text_)
if not node:
if pos < self.mandatory:
......@@ -1277,25 +1276,29 @@ class Series(NaryOperator):
i = max(1, text.index(match.regs[1][0])) if match else 1
location = self.grammar.document_length__ - len(text_)
node = Node(self, text_[:i]).init_pos(location)
node.errors.append(Error("§ %s violation" % parser.repr,
location, Error.MESSAGE))
if not mandatory_violation:
found = text_[:10].replace('\n', '\\n ')
if self.errmsg:
msg = self.errmsg % found if self.errmsg.find("%s") >= 0 \
else self.errmsg
else:
msg = '%s expected, "%s" found!' % (parser.repr, found)
mandatory_violation = Error(msg, location, Error.MANDATORY_CONTINUATION)
# self.grammar.tree__.add_error(
# node, Error("§ %s violation" % parser.repr, location, Error.MESSAGE))
# # node.errors.append(Error("§ %s violation" % parser.repr,
# # location, Error.MESSAGE))
found = text_[:10].replace('\n', '\\n ')
if self.errmsg:
msg = self.errmsg % found if self.errmsg.find("%s") >= 0 \
else self.errmsg
else:
msg = '%s expected, "%s" found!' % (parser.repr, found)
mandatory_violation = Error(msg, location, Error.MANDATORY_CONTINUATION)
self.grammar.tree__.add_error(node, mandatory_violation)
text_ = text_[i:]
results += (node,)
break
results += (node,)
# if node.error_flag: # break on first error
# break
pos += 1
assert len(results) <= len(self.parsers)
node = Node(self, results)
if mandatory_violation:
self.grammar.tree__.add_error(node, mandatory_violation)
pass
# self.grammar.tree__.add_error(node, mandatory_violation)
return node, text_
def __repr__(self):
......
......@@ -298,6 +298,7 @@ class TestSeries:
st = parser("ABCD"); assert not st.error_flag
st = parser("A_CD"); assert not st.error_flag
st = parser("AB_D"); assert st.error_flag
# print(st.collect_errors())
assert st.collect_errors()[0].code == Error.MANDATORY_CONTINUATION
# transitivity of mandatory-operator
st = parser("ABC_"); assert st.error_flag
......@@ -583,9 +584,9 @@ class TestBorderlineCases:
cst = gr('X', 'parser')
assert not cst.error_flag
cst = gr(' ', 'parser')
assert cst.error_flag
assert cst.error_flag and cst.collect_errors()[0].code == Error.PARSER_DID_NOT_MATCH
cst = gr('', 'parser')
assert cst.error_flag
assert cst.error_flag and cst.collect_errors()[0].code == Error.PARSER_DID_NOT_MATCH
def test_matching(self):
minilang = """parser = /.?/"""
......@@ -593,11 +594,29 @@ class TestBorderlineCases:
cst = gr(' ', 'parser')
assert not cst.error_flag
cst = gr(' ', 'parser')
assert cst.error_flag
assert cst.error_flag and cst.collect_errors()[0].code == Error.PARSER_STOPPED_BEFORE_END
cst = gr('', 'parser')
assert not cst.error_flag
# class TestReentryAfterError:
# def test_reentry_after_mandatory_error(self):
# lang = """
# document = alpha [beta] gamma "."
# alpha = "ALPHA" abc
# abc = §"a" "b" "c"
# beta = "BETA" (bac | bca)
# bac = "b" "a" § "c"
# bca = "b" "c" § "a"
# gamma = "GAMMA" §(cab | cba)
# cab = "c" "a" §"b"
# cba = "c" "b" §"a"
# """
# gr = grammar_provider(lang)()
# cst = gr('ALPHA abc BETA bac GAMMA cab .')
# assert not cst.error_flag
class TestUnknownParserError:
def test_unknown_parser_error(self):
gr = Grammar()
......
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