Add set[char] parser
This commit is contained in:
32
src/test.nim
32
src/test.nim
@@ -109,6 +109,38 @@ proc initParser(str: string): ParserResult =
|
||||
tokens: newSeq[Token](),
|
||||
).ok()
|
||||
|
||||
func ch(expectedChars: set[char]): (Parser -> ParserResult) {.inline.} =
|
||||
return func(parser: Parser): ParserResult =
|
||||
let state = parser.state
|
||||
let newIndex = state.position + 1
|
||||
|
||||
if newIndex > (state.stream.len - 1):
|
||||
return err(ParserError(
|
||||
kind: endOfStringErr,
|
||||
expected: &"{expectedChars.prettyExpectedSet()}",
|
||||
index: newIndex,
|
||||
parser: parser,
|
||||
))
|
||||
else:
|
||||
let foundChar = state.stream[newIndex]
|
||||
if foundChar in expectedChars:
|
||||
return Parser(
|
||||
state: ParserState(
|
||||
stream: state.stream,
|
||||
position: newIndex,
|
||||
lastPosition: parser.state.position,
|
||||
),
|
||||
tokens: parser.tokens & Token(value: foundChar)
|
||||
).ok()
|
||||
else:
|
||||
return err(ParserError(
|
||||
kind: charMismatchErr,
|
||||
unexpected: &"{foundChar}",
|
||||
expected: &"{expectedChars.prettyExpectedSet()}",
|
||||
index: newIndex,
|
||||
parser: parser,
|
||||
))
|
||||
|
||||
func ch(expectedChar: char): (Parser -> ParserResult) {.inline.} =
|
||||
return func(parser: Parser): ParserResult =
|
||||
let state = parser.state
|
||||
|
||||
Reference in New Issue
Block a user