Add andParser
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user