Add isStreamCompleted function

This commit is contained in:
Florian Schroedl
2022-08-26 16:25:40 +02:00
parent ccd1e612ae
commit 48802f0c8d

View File

@@ -32,6 +32,15 @@ proc lookBack*(count: int): (parserFnT) =
tokens: parser.tokens,
).ok()
proc isStreamCompleted*(parser: Parser): bool =
parser.state.position >= parser.state.stream.len - 1
proc isStreamCompleted*(parserResult: ParserResult): bool =
parserResult.fold(
err => false,
isStreamCompleted,
)
# -- Parsing Proctions
proc ch*(expectedChars: set[char]): parserFnT {.inline.} =
@@ -248,10 +257,9 @@ when isMainModule:
block testBlockChar:
let ch1 = ch('1')
let ch2 = ch('2')
let ch3 = ch('3')
let chDigits = ch(Digits)
# Success
assert testParser123.flatMap(ch1).tokensToString() == "1"
assert testParser123.flatMap(anyCh).tokensToString() == "1"
assert testParser123.flatMap(chDigits).tokensToString() == "1"
@@ -263,3 +271,8 @@ when isMainModule:
# Out of bounds
assert initParserResult("").flatMap(ch1).error().kind == endOfStringErr
assert initParserResult("1").flatMap(ch1).flatMap(ch1).error().kind == endOfStringErr
# Stream end reached
assert initParserResult("1").flatMap(ch1).isStreamCompleted() == true
assert initParserResult("12").flatMap(ch1).isStreamCompleted() == false
assert initParserResult("").flatMap(ch1).isStreamCompleted() == false