Use string as error for ease of use
This commit is contained in:
24
src/test.nim
24
src/test.nim
@@ -28,8 +28,8 @@ type
|
|||||||
endOfStringErr
|
endOfStringErr
|
||||||
ParserError = ref object
|
ParserError = ref object
|
||||||
kind: ParseErrorKind
|
kind: ParseErrorKind
|
||||||
unexpected: char
|
unexpected: string
|
||||||
expected: char
|
expected: string
|
||||||
index: int
|
index: int
|
||||||
parser: Parser
|
parser: Parser
|
||||||
ParserResult* = Result[Parser, ParserError]
|
ParserResult* = Result[Parser, ParserError]
|
||||||
@@ -92,7 +92,7 @@ proc initParser(str: string): ParserResult =
|
|||||||
tokens: newSeq[Token](),
|
tokens: newSeq[Token](),
|
||||||
).ok()
|
).ok()
|
||||||
|
|
||||||
func ch(c1: char): (Parser -> ParserResult) {.inline.} =
|
func ch(expectedChar: char): (Parser -> ParserResult) {.inline.} =
|
||||||
return func(parser: Parser): ParserResult =
|
return func(parser: Parser): ParserResult =
|
||||||
let state = parser.state
|
let state = parser.state
|
||||||
let newIndex = state.position + 1
|
let newIndex = state.position + 1
|
||||||
@@ -100,26 +100,26 @@ func ch(c1: char): (Parser -> ParserResult) {.inline.} =
|
|||||||
if newIndex > (state.stream.len - 1):
|
if newIndex > (state.stream.len - 1):
|
||||||
return err(ParserError(
|
return err(ParserError(
|
||||||
kind: endOfStringErr,
|
kind: endOfStringErr,
|
||||||
expected: c1,
|
expected: &"{expectedChar}",
|
||||||
index: newIndex,
|
index: newIndex,
|
||||||
parser: parser,
|
parser: parser,
|
||||||
))
|
))
|
||||||
else:
|
else:
|
||||||
let c2 = state.stream[newIndex]
|
let foundChar = state.stream[newIndex]
|
||||||
if c1 == c2:
|
if expectedChar == foundChar:
|
||||||
return Parser(
|
return Parser(
|
||||||
state: ParserState(
|
state: ParserState(
|
||||||
stream: state.stream,
|
stream: state.stream,
|
||||||
position: newIndex,
|
position: newIndex,
|
||||||
lastPosition: parser.state.position,
|
lastPosition: parser.state.position,
|
||||||
),
|
),
|
||||||
tokens: parser.tokens & Token(value: c2)
|
tokens: parser.tokens & Token(value: foundChar)
|
||||||
).ok()
|
).ok()
|
||||||
else:
|
else:
|
||||||
return err(ParserError(
|
return err(ParserError(
|
||||||
kind: charMismatchErr,
|
kind: charMismatchErr,
|
||||||
unexpected: c2,
|
unexpected: &"{foundChar}",
|
||||||
expected: c1,
|
expected: &"{expectedChar}",
|
||||||
index: newIndex,
|
index: newIndex,
|
||||||
parser: parser,
|
parser: parser,
|
||||||
))
|
))
|
||||||
@@ -165,11 +165,11 @@ proc foldTokens[T](
|
|||||||
onError(err)
|
onError(err)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
let fooParser = initParser("FOO_BAR")
|
let fooParser = initParser("FOO___BAR")
|
||||||
.parseSeq(@[
|
.parseSeq(@[
|
||||||
str("FOO"),
|
str("FOO"),
|
||||||
ignore(ch('_')),
|
ignore(str("___")),
|
||||||
ch('B'),
|
ignore(str("_")),
|
||||||
ch('B'),
|
ch('B'),
|
||||||
ch('A'),
|
ch('A'),
|
||||||
ch('R'),
|
ch('R'),
|
||||||
|
|||||||
Reference in New Issue
Block a user