Add endOfStream overflow, move to parserFns, add test
This commit is contained in:
@@ -112,6 +112,31 @@ proc str*(expectedString: string): parserFnT {.inline.} =
|
|||||||
res = res.flatMap(ch(c))
|
res = res.flatMap(ch(c))
|
||||||
return res
|
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
|
# -- Parsing API
|
||||||
|
|
||||||
proc plus*(parserFnA: parserFnT, parserFnB: parserFnT): parserFnT {.inline.} =
|
proc plus*(parserFnA: parserFnT, parserFnB: parserFnT): parserFnT {.inline.} =
|
||||||
@@ -197,18 +222,6 @@ proc following*(parserFns: seq[parserFnT]): parserFnT {.inline.} =
|
|||||||
|
|
||||||
# -- Parsing Aliases
|
# -- 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(@[
|
let newlineParser = choice(@[
|
||||||
ch(NewLines),
|
ch(NewLines),
|
||||||
endOfStream,
|
endOfStream,
|
||||||
@@ -264,6 +277,9 @@ when isMainModule:
|
|||||||
assert initParserResult("").flatMap(ch1).isStreamCompleted() == false
|
assert initParserResult("").flatMap(ch1).isStreamCompleted() == false
|
||||||
assert testParser123.flatMap(str("123")).isStreamCompleted() == true
|
assert testParser123.flatMap(str("123")).isStreamCompleted() == true
|
||||||
|
|
||||||
|
# endOfStream
|
||||||
|
assert testParser123.flatMap(str("123") + endOfStream).tokensToString() == "123"
|
||||||
|
|
||||||
block testParsingApi:
|
block testParsingApi:
|
||||||
# plus, +
|
# plus, +
|
||||||
assert testAbc1Parser.flatMap(str("abc") + ch('1')).tokensToString() == "abc1"
|
assert testAbc1Parser.flatMap(str("abc") + ch('1')).tokensToString() == "abc1"
|
||||||
|
|||||||
Reference in New Issue
Block a user