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