Add testing setup
This commit is contained in:
@@ -194,6 +194,11 @@ proc endOfStream*(parser: Parser): ParserResult =
|
|||||||
parser: parser,
|
parser: parser,
|
||||||
))
|
))
|
||||||
|
|
||||||
|
proc newline*(parser: Parser): ParserResult =
|
||||||
|
parser
|
||||||
|
.endOfStream()
|
||||||
|
.flatMap(ch(NewLines))
|
||||||
|
|
||||||
func ignore*(parserFn: Parser -> ParserResult): (Parser -> ParserResult) {.inline.} =
|
func ignore*(parserFn: Parser -> ParserResult): (Parser -> ParserResult) {.inline.} =
|
||||||
## Parse characters but throw success tokens away
|
## Parse characters but throw success tokens away
|
||||||
return proc(parser: Parser): ParserResult =
|
return proc(parser: Parser): ParserResult =
|
||||||
@@ -321,18 +326,32 @@ proc foldBuilder*[T, T2](
|
|||||||
let err = builderResult.error()
|
let err = builderResult.error()
|
||||||
onError(err[1])
|
onError(err[1])
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
proc getTokens(x: ParserResult): seq[char] =
|
||||||
|
x.foldTokens(
|
||||||
|
proc(err: ParserError): seq[char] = @[],
|
||||||
|
proc(xs: seq[Token]): seq[char] = xs.map((x: Token) => x.value),
|
||||||
|
)
|
||||||
|
|
||||||
|
proc testParser(x: string, ps: seq[Parser -> ParserResult]): seq[char] =
|
||||||
|
initParserResult(x).parseSeq(ps).getTokens()
|
||||||
|
|
||||||
let optionalPrefixParser = @[
|
let optionalPrefixParser = @[
|
||||||
optional(ch('_')),
|
optional(ch('_')),
|
||||||
str("ABC")
|
str("ABC")
|
||||||
]
|
]
|
||||||
|
assert: "_ABC".testParser(optionalPrefixParser) == @['_', 'A', 'B', 'C']
|
||||||
|
assert: "ABC".testParser(optionalPrefixParser) == @['A', 'B', 'C']
|
||||||
|
|
||||||
echo initParserResult("_ABC").parseSeq(optionalPrefixParser)
|
let andParser = @[
|
||||||
echo initParserResult("ABC").parseSeq(optionalPrefixParser)
|
|
||||||
|
|
||||||
let andParsers = @[
|
|
||||||
(ch('A') + ch('B')),
|
(ch('A') + ch('B')),
|
||||||
ch('C'),
|
ch('C'),
|
||||||
]
|
]
|
||||||
|
assert: "ABC".testParser(andParser) == @['A', 'B', 'C']
|
||||||
|
|
||||||
echo initParserResult("ABC").parseSeq(andParsers)
|
# let newlineParser = @[
|
||||||
|
# str("ABC"),
|
||||||
|
# newline
|
||||||
|
# ]
|
||||||
|
# echo initParserResult("ABC").parseSeq(newLineParser)
|
||||||
|
|||||||
Reference in New Issue
Block a user