Add endOfStream overflow, move to parserFns, add test

This commit is contained in:
Florian Schroedl
2022-08-26 17:36:43 +02:00
parent 93aba40e00
commit c3389abb59

View File

@@ -112,6 +112,31 @@ proc str*(expectedString: string): parserFnT {.inline.} =
res = res.flatMap(ch(c))
return res
proc endOfStream*(parser: Parser): ParserResult =
## Check if the following character is the end of the stream.
## Errors when the end of stream was already reached.
let state = parser.state
let newIndex = state.position + 1
if newIndex == parser.state.stream.len:
ok(parser)
elif newIndex > parser.state.stream.len:
return err(ParserError(
kind: endOfStringErr,
expected: "endOfStream",
index: newIndex,
parser: parser,
))
else:
let foundChar = state.stream[newIndex]
err(ParserError(
kind: charMismatchErr,
unexpected: $foundChar,
expected: "endOfStream",
index: newIndex,
parser: parser,
))
# -- Parsing API
proc plus*(parserFnA: parserFnT, parserFnB: parserFnT): parserFnT {.inline.} =
@@ -197,18 +222,6 @@ proc following*(parserFns: seq[parserFnT]): parserFnT {.inline.} =
# -- Parsing Aliases
proc endOfStream*(parser: Parser): ParserResult =
let index = parser.state.position + 1
if index == parser.state.stream.len:
ok(parser)
else:
err(ParserError(
kind: endOfStringErr,
expected: &"EndOfString",
index: index,
parser: parser,
))
let newlineParser = choice(@[
ch(NewLines),
endOfStream,
@@ -264,6 +277,9 @@ when isMainModule:
assert initParserResult("").flatMap(ch1).isStreamCompleted() == false
assert testParser123.flatMap(str("123")).isStreamCompleted() == true
# endOfStream
assert testParser123.flatMap(str("123") + endOfStream).tokensToString() == "123"
block testParsingApi:
# plus, +
assert testAbc1Parser.flatMap(str("abc") + ch('1')).tokensToString() == "abc1"