Add 'assertExpected' to Parser.oneOf() method

This commit is contained in:
Bartłomiej Pluta
2019-07-12 16:49:25 +02:00
parent 0435bc776e
commit c7e90b9fbd

View File

@@ -62,7 +62,7 @@ class Parser:
# oneOf -> a | b | c | ...
@staticmethod
def oneOf(*parsers, exception=None, name="or"):
def oneOf(*parsers, assertExpected=None, exception=None, name="or"):
def combinedParser(input):
snap = input.snapshot()
for parser in parsers:
@@ -70,6 +70,10 @@ class Parser:
if value.result:
return value
if assertExpected is not None:
found = f", found '{input.current().rawValue}'" if input.hasCurrent() else ""
raise SyntaxException(f"Expected {assertExpected}{found}", input.currentPos())
if exception is not None:
if callable(exception):
raise exception(input)