Add andParser

This commit is contained in:
Florian Schroedl
2022-01-20 17:00:00 +01:00
parent 811bf23fe7
commit b46b81de42

View File

@@ -251,6 +251,10 @@ func choice*(parsers: seq[Parser -> ParserResult]): (Parser -> ParserResult) {.i
proc(x: ParserResult): ParserResult = x,
)
proc `+`*(parserFnA: Parser -> ParserResult, parserFnB: Parser -> ParserResult): (Parser -> ParserResult) {.inline.} =
## Parse characters and ignore failure
return proc(parser: Parser): ParserResult =
parserFnA(parser).flatMap(parserFnB)
proc parseSeq*(parser: ParserResult, xs: seq[Parser -> ParserResult]): ParserResult =
xs.foldl(a.flatMap(b), parser)
@@ -325,3 +329,10 @@ when isMainModule:
echo initParserResult("_ABC").parseSeq(optionalPrefixParser)
echo initParserResult("ABC").parseSeq(optionalPrefixParser)
let andParsers = @[
(ch('A') + ch('B')),
ch('C'),
]
echo initParserResult("ABC").parseSeq(andParsers)