Add custom error setter

This commit is contained in:
Florian Schroedl
2022-01-20 17:00:00 +01:00
parent 9d85ba8ae4
commit 194ebea0b6

View File

@@ -320,12 +320,22 @@ proc `+`*(parserFnA: Parser -> ParserResult, parserFnB: Parser -> ParserResult):
return proc(parser: Parser): ParserResult =
parserFnA(parser).flatMap(parserFnB)
proc setErrorExpectedField(err: ParserError, expected: string): ParserError =
ParserError(
kind: err.kind,
unexpected: err.unexpected,
expected: expected,
index: err.index,
parser: err.parser,
)
let newlineParser = choice(@[
ch(NewLines),
endOfStream,
])
proc newline*(parser: Parser): ParserResult = newlineParser(parser)
proc newline*(parser: Parser): ParserResult =
newlineParser(parser)
.mapErr((x: ParserError) => x.setErrorExpectedField("Newline"))
proc parseSeq*(parser: ParserResult, xs: seq[Parser -> ParserResult]): ParserResult =
xs.foldl(a.flatMap(b), parser)