From b46b81de4234d2ad7f7849fd5ebc1171a6c0c178 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Thu, 20 Jan 2022 17:00:00 +0100 Subject: [PATCH] Add andParser --- src/parser/parser.nim | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/parser/parser.nim b/src/parser/parser.nim index aefa906..10dc27d 100644 --- a/src/parser/parser.nim +++ b/src/parser/parser.nim @@ -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)