Add ignore tests
This commit is contained in:
@@ -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() == ""
|
||||
|
||||
Reference in New Issue
Block a user