This module implements a json parser. It is used and exported by the json
standard library module, but can also be used in its own right.
JsonEventKind = enum jsonError, ## an error occurred during parsing jsonEof, ## end of file reached jsonString, ## a string literal jsonInt, ## an integer literal jsonFloat, ## a float literal jsonTrue, ## the value ``true`` jsonFalse, ## the value ``false`` jsonNull, ## the value ``null`` jsonObjectStart, ## start of an object: the ``{`` token jsonObjectEnd, ## end of an object: the ``}`` token jsonArrayStart, ## start of an array: the ``[`` token jsonArrayEnd ## start of an array: the ``]`` token
TokKind = enum tkError, tkEof, tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull, tkCurlyLe, tkCurlyRi, tkBracketLe, tkBracketRi, tkColon, tkComma
JsonError = enum errNone, ## no error errInvalidToken, ## invalid token errStringExpected, ## string expected errColonExpected, ## ``:`` expected errCommaExpected, ## ``,`` expected errBracketRiExpected, ## ``]`` expected errCurlyRiExpected, ## ``}`` expected errQuoteExpected, ## ``"`` or ``'`` expected errEOC_Expected, ## ``*/`` expected errEofExpected, ## EOF expected errExprExpected ## expr expected
JsonParser = object of BaseLexer a*: string tok*: TokKind kind: JsonEventKind err: JsonError state: seq[ParserState] filename: string rawStringLiterals: bool
JsonKindError = object of ValueError
to
macro if the JSON kind is incorrect. JsonParsingError = object of ValueError
errorMessages: array[JsonError, string] = ["no error", "invalid token", "string expected", "\':\' expected", "\',\' expected", "\']\' expected", "\'}\' expected", "\'\"\' or \"\'\" expected", "\'*/\' expected", "EOF expected", "expression expected"]
proc open(my: var JsonParser; input: Stream; filename: string; rawStringLiterals = false) {...}{. raises: [Exception], tags: [ReadIOEffect].}
proc close(my: var JsonParser) {...}{.inline, raises: [Exception], tags: [].}
proc str(my: JsonParser): string {...}{.inline, raises: [], tags: [].}
jsonInt
, jsonFloat
, jsonString
proc getInt(my: JsonParser): BiggestInt {...}{.inline, raises: [ValueError], tags: [].}
jsonInt
proc getFloat(my: JsonParser): float {...}{.inline, raises: [ValueError], tags: [].}
jsonFloat
proc kind(my: JsonParser): JsonEventKind {...}{.inline, raises: [], tags: [].}
proc getColumn(my: JsonParser): int {...}{.inline, raises: [], tags: [].}
proc getLine(my: JsonParser): int {...}{.inline, raises: [], tags: [].}
proc getFilename(my: JsonParser): string {...}{.inline, raises: [], tags: [].}
proc errorMsg(my: JsonParser): string {...}{.raises: [ValueError], tags: [].}
jsonError
proc errorMsgExpected(my: JsonParser; e: string): string {...}{.raises: [ValueError], tags: [].}
proc parseEscapedUTF16(buf: cstring; pos: var int): int {...}{.raises: [], tags: [].}
proc getTok(my: var JsonParser): TokKind {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc next(my: var JsonParser) {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc raiseParseErr(p: JsonParser; msg: string) {...}{.noinline, noreturn, raises: [JsonParsingError, ValueError], tags: [].}
proc eat(p: var JsonParser; tok: TokKind) {...}{.raises: [Exception, JsonParsingError, ValueError], tags: [ReadIOEffect].}
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/parsejson.html