Add plus tests
This commit is contained in:
@@ -114,6 +114,16 @@ proc str*(expectedString: string): parserFnT {.inline.} =
|
||||
|
||||
# -- Parsing API
|
||||
|
||||
proc plus*(parserFnA: parserFnT, parserFnB: parserFnT): parserFnT {.inline.} =
|
||||
## Creates parser function with combining two parser functions `parserFnA` plus `parserFnB`
|
||||
return proc(parser: Parser): ParserResult =
|
||||
parserFnA(parser).flatMap(parserFnB)
|
||||
|
||||
proc `+`*(parserFnA: parserFnT, parserFnB: parserFnT): parserFnT {.inline.} =
|
||||
## Creates infix parser function with combining two parser functions `parserFnA` plus `parserFnB`
|
||||
return proc(parser: Parser): ParserResult =
|
||||
parserFnA(parser).flatMap(parserFnB)
|
||||
|
||||
proc optional*(parserFn: parserFnT): parserFnT {.inline.} =
|
||||
## Creates parser function with a nested `parserFn`:
|
||||
## Continues on succesful parser
|
||||
@@ -179,12 +189,6 @@ proc choice*(parserFns: seq[parserFnT]): parserFnT {.inline} =
|
||||
proc(x: ParserResult): ParserResult = x,
|
||||
)
|
||||
|
||||
|
||||
proc `+`*(parserFnA: parserFnT, parserFnB: parserFnT): parserFnT {.inline.} =
|
||||
## Parse characters and ignore failure
|
||||
return proc(parser: Parser): ParserResult =
|
||||
parserFnA(parser).flatMap(parserFnB)
|
||||
|
||||
proc parseSeq*(parser: ParserResult, xs: seq[parserFnT]): ParserResult {.inline.} =
|
||||
xs.foldl(a.flatMap(b), parser)
|
||||
|
||||
@@ -258,6 +262,10 @@ when isMainModule:
|
||||
assert testParser123.flatMap(str("123")).isStreamCompleted() == true
|
||||
|
||||
block testParsingApi:
|
||||
# plus, +
|
||||
assert testAbc1Parser.flatMap(str("abc") + ch('1')).tokensToString() == "abc1"
|
||||
assert testParser123.flatMap(str("12").plus(digit)).tokensToString() == "123"
|
||||
|
||||
# optional
|
||||
assert testParser123.flatMap(optional(ch('1'))).tokensToString() == "1"
|
||||
assert testParser123.flatMap(optional(ch('2'))).tokensToString() == ""
|
||||
|
||||
Reference in New Issue
Block a user