Add ignore tests

This commit is contained in:
Florian Schroedl
2022-08-26 16:42:05 +02:00
parent f60ddaf19d
commit 26f8baacfb

View File

@@ -114,19 +114,19 @@ proc str*(expectedString: string): parserFnT {.inline.} =
# -- Parsing API
proc optional*(parserFn: parserFnT): parserFnT {.inline.} =
## Creates parser function with a nested `parserFn`.
## Continue on succesful parser
## Creates parser function with a nested `parserFn`:
## Continues on succesful parser
## Ignores failing parsers
return proc(parser: Parser): ParserResult =
let newParser = parserFn(parser)
if newParser.isOk():
newParser
else:
parser.ok()
proc ignore*(parserFn: parserFnT): parserFnT {.inline.} =
## Parse characters but throw success tokens away
## Creates parser function with a nested `parserFn`:
## Parses using the `parserFn` but dont capture the resulting tokens.
return proc(parser: Parser): ParserResult =
return parserFn(parser)
.map((x: Parser) => Parser(
@@ -278,3 +278,6 @@ when isMainModule:
# Optional
assert testParser123.flatMap(optional(ch('1'))).tokensToString() == "1"
assert testParser123.flatMap(optional(ch('2'))).tokensToString() == ""
# Ignore
assert testParser123.flatMap(ignore(ch('1'))).tokensToString() == ""