Document & Test optional

This commit is contained in:
Florian Schroedl
2022-08-26 16:39:20 +02:00
parent 3bbf6834c3
commit f60ddaf19d

View File

@@ -114,7 +114,9 @@ proc str*(expectedString: string): parserFnT {.inline.} =
# -- Parsing API
proc optional*(parserFn: parserFnT): parserFnT {.inline.} =
## Create a
## Creates parser function with a nested `parserFn`.
## Continue on succesful parser
## Ignores failing parsers
return proc(parser: Parser): ParserResult =
let newParser = parserFn(parser)
@@ -271,3 +273,8 @@ when isMainModule:
assert initParserResult("12").flatMap(ch1).isStreamCompleted() == false
assert initParserResult("").flatMap(ch1).isStreamCompleted() == false
assert testParser123.flatMap(str("123")).isStreamCompleted() == true
block testParsingApi:
# Optional
assert testParser123.flatMap(optional(ch('1'))).tokensToString() == "1"
assert testParser123.flatMap(optional(ch('2'))).tokensToString() == ""