Add starOfStream function
This commit is contained in:
@@ -112,6 +112,21 @@ proc str*(expectedString: string): parserFnT {.inline.} =
|
|||||||
res = res.flatMap(ch(c))
|
res = res.flatMap(ch(c))
|
||||||
return res
|
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 =
|
proc endOfStream*(parser: Parser): ParserResult =
|
||||||
## Check if the following character is the end of the stream.
|
## Check if the following character is the end of the stream.
|
||||||
## Errors when the end of stream was already reached.
|
## Errors when the end of stream was already reached.
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ type
|
|||||||
choiceMismatchErr
|
choiceMismatchErr
|
||||||
charMismatchErr
|
charMismatchErr
|
||||||
endOfStringErr
|
endOfStringErr
|
||||||
|
startOfStringErr
|
||||||
ParserError* = ref object
|
ParserError* = ref object
|
||||||
kind*: ParserErrorKind
|
kind*: ParserErrorKind
|
||||||
unexpected*: string
|
unexpected*: string
|
||||||
@@ -287,6 +288,15 @@ func `$`*(x: ParserError): string =
|
|||||||
{original}
|
{original}
|
||||||
{errSpace}^ Expected 'EndOfString' at {index} but got {original.len - 1}"""
|
{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"
|
else: "ParseError"
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|||||||
Reference in New Issue
Block a user