std/parsejson (original) (raw)
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.
Types
JsonError = enum
errNone,
errInvalidToken,
errStringExpected,
errColonExpected,
errCommaExpected,
errBracketRiExpected,
errCurlyRiExpected,
errQuoteExpected,
errEOC_Expected,
errEofExpected,
errExprExpected
enumeration that lists all errors that can occurSource Edit
JsonEventKind = enum
jsonError,
jsonEof,
jsonString,
jsonInt,
jsonFloat,
jsonTrue,
jsonFalse,
jsonNull,
jsonObjectStart,
jsonObjectEnd,
jsonArrayStart,
jsonArrayEnd
enumeration of all events that may occur when parsingSource Edit
TokKind = enum tkError, tkEof, tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull, tkCurlyLe, tkCurlyRi, tkBracketLe, tkBracketRi, tkColon, tkComma
Consts
errorMessages: array[JsonError, string] = ["no error", "invalid token", "string expected", "':' expected", "',' expected", "']' expected", "'}' expected", "'"' or "'" expected", "'*/' expected", "EOF expected", "expression expected"]
Procs
proc close(my: var JsonParser) {.inline, ...raises: [IOError, OSError], tags: [WriteIOEffect], forbids: [].}
closes the parser my and its associated input stream.Source Edit
proc eat(p: var JsonParser; tok: TokKind) {. ...raises: [IOError, OSError, JsonParsingError, ValueError], tags: [ReadIOEffect], forbids: [].}
proc errorMsg(my: JsonParser): string {....raises: [ValueError], tags: [], forbids: [].}
returns a helpful error message for the event jsonError Source Edit
proc errorMsgExpected(my: JsonParser; e: string): string {....raises: [ValueError], tags: [], forbids: [].}
returns an error message "e expected" in the same format as the other error messagesSource Edit
proc getColumn(my: JsonParser): int {.inline, ...raises: [], tags: [], forbids: [].}
get the current column the parser has arrived at.Source Edit
proc getFilename(my: JsonParser): string {.inline, ...raises: [], tags: [], forbids: [].}
get the filename of the file that the parser processes.Source Edit
proc getFloat(my: JsonParser): float {.inline, ...raises: [ValueError], tags: [], forbids: [].}
returns the number for the event: jsonFloat Source Edit
proc getLine(my: JsonParser): int {.inline, ...raises: [], tags: [], forbids: [].}
get the current line the parser has arrived at.Source Edit
proc next(my: var JsonParser) {....raises: [IOError, OSError], tags: [ReadIOEffect], forbids: [].}
retrieves the first/next event. This controls the parser.Source Edit
proc open(my: var JsonParser; input: Stream; filename: string; rawStringLiterals = false) {....raises: [IOError, OSError], tags: [ReadIOEffect], forbids: [].}
initializes the parser with an input stream. Filename is only used for nice error messages. If rawStringLiterals is true, string literals are kept with their surrounding quotes and escape sequences in them are left untouched too.Source Edit
proc raiseParseErr(p: JsonParser; msg: string) {.noinline, noreturn, ...raises: [JsonParsingError, ValueError], tags: [], forbids: [].}
raises an EJsonParsingError exception.Source Edit
proc str(my: JsonParser): string {.inline, ...raises: [], tags: [], forbids: [].}
returns the character data for the events: jsonInt, jsonFloat, jsonString Source Edit