Add isStreamCompleted function
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user