From 48802f0c8d84dcbdc190a4bd2848a0e749436aae Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Fri, 26 Aug 2022 16:25:40 +0200 Subject: [PATCH] Add isStreamCompleted function --- src_v2/parser/parser_internals.nim | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src_v2/parser/parser_internals.nim b/src_v2/parser/parser_internals.nim index 28dd8a2..3ef9300 100644 --- a/src_v2/parser/parser_internals.nim +++ b/src_v2/parser/parser_internals.nim @@ -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