Add starOfStream function

This commit is contained in:
Florian Schroedl
2022-08-28 18:19:42 +02:00
parent 0cf8f6791d
commit bc7525e353
2 changed files with 25 additions and 0 deletions

View File

@@ -112,6 +112,21 @@ proc str*(expectedString: string): parserFnT {.inline.} =
res = res.flatMap(ch(c))
return res
proc startOfStream*(parser: Parser): ParserResult =
## Check if the following character is the end of the stream.
## Errors when the end of stream was already reached.
let position = parser.state.position
if position == 0 or position == -1:
ok(parser)
else:
err(ParserError(
kind: startOfStringErr,
expected: "startOfStream",
index: position,
parser: parser,
))
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.

View File

@@ -36,6 +36,7 @@ type
choiceMismatchErr
charMismatchErr
endOfStringErr
startOfStringErr
ParserError* = ref object
kind*: ParserErrorKind
unexpected*: string
@@ -287,6 +288,15 @@ func `$`*(x: ParserError): string =
{original}
{errSpace}^ Expected 'EndOfString' at {index} but got {original.len - 1}"""
of startOfStringErr(parser: @parser, index: @index):
let original = parser.state.stream
.deleteAfterNewline(parser.state.position)
let errSpace = " ".repeat(max(0, index))
&"""Parsing Error (StartOFString Expected):
{original}
{errSpace}^ Expected 'StartOfString' at index {index}"""
else: "ParseError"
when isMainModule: