Source Edit

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.

Imports

strutils, lexbase, streams, unicode, decode_helpers

Types

  1. JsonError = enum
  2. errNone, ## no error
  3. errInvalidToken, ## invalid token
  4. errStringExpected, ## string expected
  5. errColonExpected, ## `:` expected
  6. errCommaExpected, ## `,` expected
  7. errBracketRiExpected, ## `]` expected
  8. errCurlyRiExpected, ## `}` expected
  9. errQuoteExpected, ## `"` or `'` expected
  10. errEOC_Expected, ## `*/` expected
  11. errEofExpected, ## EOF expected
  12. errExprExpected ## expr expected

enumeration that lists all errors that can occur Source Edit

  1. JsonEventKind = enum
  2. jsonError, ## an error occurred during parsing
  3. jsonEof, ## end of file reached
  4. jsonString, ## a string literal
  5. jsonInt, ## an integer literal
  6. jsonFloat, ## a float literal
  7. jsonTrue, ## the value `true`
  8. jsonFalse, ## the value `false`
  9. jsonNull, ## the value `null`
  10. jsonObjectStart, ## start of an object: the `{` token
  11. jsonObjectEnd, ## end of an object: the `}` token
  12. jsonArrayStart, ## start of an array: the `[` token
  13. jsonArrayEnd ## end of an array: the `]` token

enumeration of all events that may occur when parsing Source Edit

  1. JsonKindError = object of ValueError

raised by the to macro if the JSON kind is incorrect. Source Edit

  1. JsonParser = object of BaseLexer
  2. a*: string
  3. tok*: TokKind

the parser object. Source Edit

  1. JsonParsingError = object of ValueError

is raised for a JSON error Source Edit

  1. TokKind = enum
  2. tkError, tkEof, tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull, tkCurlyLe,
  3. tkCurlyRi, tkBracketLe, tkBracketRi, tkColon, tkComma

Source Edit

Consts

  1. errorMessages: array[JsonError, string] = ["no error", "invalid token",
  2. "string expected", "\':\' expected", "\',\' expected", "\']\' expected",
  3. "\'}\' expected", "\'\"\' or \"\'\" expected", "\'*/\' expected",
  4. "EOF expected", "expression expected"]

Source Edit

Procs

  1. proc close(my: var JsonParser) {.inline, ...raises: [IOError, OSError],
  2. tags: [WriteIOEffect], forbids: [].}

closes the parser my and its associated input stream. Source Edit

  1. proc eat(p: var JsonParser; tok: TokKind) {.
  2. ...raises: [IOError, OSError, JsonParsingError, ValueError],
  3. tags: [ReadIOEffect], forbids: [].}

Source Edit

  1. proc errorMsg(my: JsonParser): string {....raises: [ValueError], tags: [],
  2. forbids: [].}

returns a helpful error message for the event jsonError Source Edit

  1. proc errorMsgExpected(my: JsonParser; e: string): string {....raises: [ValueError],
  2. tags: [], forbids: [].}

returns an error message “e expected” in the same format as the other error messages Source Edit

  1. proc getColumn(my: JsonParser): int {.inline, ...raises: [], tags: [], forbids: [].}

get the current column the parser has arrived at. Source Edit

  1. proc getFilename(my: JsonParser): string {.inline, ...raises: [], tags: [],
  2. forbids: [].}

get the filename of the file that the parser processes. Source Edit

  1. proc getFloat(my: JsonParser): float {.inline, ...raises: [ValueError], tags: [],
  2. forbids: [].}

returns the number for the event: jsonFloat Source Edit

  1. proc getInt(my: JsonParser): BiggestInt {.inline, ...raises: [ValueError],
  2. tags: [], forbids: [].}

returns the number for the event: jsonInt Source Edit

  1. proc getLine(my: JsonParser): int {.inline, ...raises: [], tags: [], forbids: [].}

get the current line the parser has arrived at. Source Edit

  1. proc getTok(my: var JsonParser): TokKind {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Source Edit

  1. proc kind(my: JsonParser): JsonEventKind {.inline, ...raises: [], tags: [],
  2. forbids: [].}

returns the current event type for the JSON parser Source Edit

  1. proc next(my: var JsonParser) {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

retrieves the first/next event. This controls the parser. Source Edit

  1. proc open(my: var JsonParser; input: Stream; filename: string;
  2. rawStringLiterals = false) {....raises: [IOError, OSError],
  3. 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

  1. proc parseEscapedUTF16(buf: cstring; pos: var int): int {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc raiseParseErr(p: JsonParser; msg: string) {.noinline, noreturn,
  2. ...raises: [JsonParsingError, ValueError], tags: [], forbids: [].}

raises an EJsonParsingError exception. Source Edit

  1. proc str(my: JsonParser): string {.inline, ...raises: [], tags: [], forbids: [].}

returns the character data for the events: jsonInt, jsonFloat, jsonString Source Edit