Turn "parseSeq" into api function "following", add test
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user