Add test for chars

This commit is contained in:
Florian Schroedl
2022-08-26 16:08:26 +02:00
parent e5bf85a551
commit 1f4bd5d60e
2 changed files with 18 additions and 0 deletions

View File

@@ -248,6 +248,17 @@ when isMainModule:
block testBlockChar:
let ch1 = ch('1')
let ch2 = ch('2')
let ch3 = ch('3')
let chDigits = ch(Digits)
assert testParser123.flatMap(ch1).tokensToString() == "1"
assert testParser123.flatMap(chDigits).tokensToString() == "1"
# Mismatch
assert testParser123.flatMap(ch('2')).error().kind == charMismatchErr
assert testParser123.flatMap(ch(Letters)).error().kind == charMismatchErr
# Out of bounds
assert initParserResult("").flatMap(ch1).error().kind == endOfStringErr
assert initParserResult("1").flatMap(ch1).flatMap(ch1).error().kind == endOfStringErr

View File

@@ -92,6 +92,7 @@ func tokenStringValue*(x: ParserToken): string =
func tokensToString*(tokens: seq[ParserToken]): string =
tokens.foldl(a & b.tokenStringValue(), "")
# -- Modifiers
func flattenParserTokens*(parser: Parser): ParserResult =
@@ -124,6 +125,12 @@ func foldTokens*[T](
let err = parserResult.error()
onError(err)
func tokensToString*(parserResult: ParserResult, fallback = ""): string =
parserResult.foldTokens(
err => fallback,
xs => xs.tokensToString(),
)
func setErrorExpectedField*(err: ParserError, expected: string): ParserError =
ParserError(
kind: err.kind,