Turn "parseSeq" into api function "following", add test

This commit is contained in:
Florian Schroedl
2022-08-26 17:26:27 +02:00
parent 447a3dcefc
commit 93aba40e00

View File

@@ -189,8 +189,11 @@ proc choice*(parserFns: seq[parserFnT]): parserFnT {.inline} =
proc(x: ParserResult): ParserResult = x,
)
proc parseSeq*(parser: ParserResult, xs: seq[parserFnT]): ParserResult {.inline.} =
xs.foldl(a.flatMap(b), parser)
proc following*(parserFns: seq[parserFnT]): parserFnT {.inline.} =
## Checks a sequence of `parserFns`.
## All of them need to be `ParserResult.ok`
return proc(parser: Parser): ParserResult {.closure.} =
parserFns.foldl(a.flatMap(b), parser.ok)
# -- Parsing Aliases
@@ -282,3 +285,6 @@ when isMainModule:
# choice
assert testAbc1Parser.flatMap(choice(@[digit, ch('a')])).tokensToString() == "a"
assert testAbc1Parser.flatMap(choice(@[digit])).error().kind == choiceMismatchErr
# parse
assert testAbc1Parser.flatMap(following(@[ch('a'), str("bc"), digit])).tokensToString() == "abc1"