src/parsetoml

Search:
Group by:

License:MIT

Introduction

This module implements a TOML parser that is compliant with v0.5.0 of its spec.

Source

Repo link

Types

Sign = enum
  None, Pos, Neg
TomlDate = object
  year*: int
  month*: int
  day*: int
TomlDateTime = object
  date*: TomlDate
  time*: TomlTime
  case shift*: bool
  of true:
      isShiftPositive*: bool
      zoneHourShift*: int
      zoneMinuteShift*: int

  of false:
    nil
  
TomlError = object of ValueError
  location*: ParserState
TomlTable = OrderedTable[string, TomlValueRef]
TomlTime = object
  hour*: int
  minute*: int
  second*: int
  subsecond*: int
TomlValue = object
  case kind*: TomlValueKind
  of TomlValueKind.None:
    nil
  of TomlValueKind.Int:
    intVal*: int64
  of TomlValueKind.Float:
      floatVal*: float64
      forcedSign*: Sign

  of TomlValueKind.Bool:
    boolVal*: bool
  of TomlValueKind.Datetime:
    dateTimeVal*: TomlDateTime
  of TomlValueKind.Date:
    dateVal*: TomlDate
  of TomlValueKind.Time:
    timeVal*: TomlTime
  of TomlValueKind.String:
    stringVal*: string
  of TomlValueKind.Array:
    arrayVal*: seq[TomlValueRef]
  of TomlValueKind.Table:
    tableVal*: TomlTableRef
  
TomlValueKind {.pure.} = enum
  None, Int, Float, Bool, Datetime, Date, Time, String, Array, Table

Procs

proc `$`(val: TomlDate): string {....raises: [], tags: [], forbids: [].}
Converts the TOML date object into the ISO format read by the parser
proc `$`(val: TomlDateTime): string {....raises: [], tags: [], forbids: [].}
Converts the TOML date-time object into the ISO format read by the parser
proc `$`(val: TomlTime): string {....raises: [], tags: [], forbids: [].}
Converts the TOML time object into the ISO format read by the parser
proc `$`(val: TomlValue): string {....raises: [], tags: [], forbids: [].}
Turns whatever value into a type and value representation, used by dump
proc `$`(val: TomlValueRef): string {....raises: [Exception], tags: [RootEffect],
                                      forbids: [].}
Turns whatever value into a regular Nim value representtation
func `==`(a, b: TomlValueRef): bool {....raises: [Exception, KeyError],
                                      tags: [RootEffect], forbids: [].}
Check two nodes for equality
proc `?`(b: bool): TomlValueRef {....raises: [], tags: [], forbids: [].}
Generic constructor for TOML data. Creates a new TomlValueKind.Bool TomlValueRef.
proc `?`(keyVals: openArray[tuple[key: string, val: TomlValueRef]]): TomlValueRef {.
    ...raises: [], tags: [], forbids: [].}
Generic constructor for TOML data. Creates a new TomlValueKind.Table TomlValueRef
proc `?`(n: float): TomlValueRef {....raises: [], tags: [], forbids: [].}
Generic constructor for TOML data. Creates a new TomlValueKind.Float TomlValueRef.
proc `?`(n: int64): TomlValueRef {....raises: [], tags: [], forbids: [].}
Generic constructor for TOML data. Creates a new TomlValueKind.Int TomlValueRef.
proc `?`(o: enum): TomlValueRef
Construct a TomlValueRef that represents the specified enum value as a string. Creates a new TomlValueKind.String TomlValueRef.
proc `?`(o: object): TomlValueRef
Generic constructor for TOML data. Creates a new TomlValueKind.Table TomlValueRef
proc `?`(o: ref object): TomlValueRef
Generic constructor for TOML data. Creates a new TomlValueKind.Table TomlValueRef
proc `?`(s: string): TomlValueRef {....raises: [], tags: [], forbids: [].}
Generic constructor for TOML data. Creates a new TomlValueKind.String TomlValueRef.
proc `?`[T](elements: openArray[T]): TomlValueRef
Generic constructor for TOML data. Creates a new TomlValueKind.Array TomlValueRef
proc `[]`(node: TomlValueRef; index: int): TomlValueRef {.inline, ...raises: [],
    tags: [], forbids: [].}
Gets the node at index in an Array. Result is undefined if index is out of bounds, but as long as array bound checks are enabled it will result in an exception.
proc `[]`(node: TomlValueRef; name: string): TomlValueRef {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Gets a field from a TomlValueKind.Table, which must not be nil. If the value at name does not exist, raises KeyError.
proc `[]=`(obj: TomlValueRef; key: string; val: TomlValueRef) {.inline,
    ...raises: [], tags: [], forbids: [].}
Sets a field from a TomlValueKind.Table.
proc add(father, child: TomlValueRef) {....raises: [], tags: [], forbids: [].}
Adds child to a TomlValueKind.Array node father.
proc add(obj: TomlValueRef; key: string; val: TomlValueRef) {....raises: [],
    tags: [], forbids: [].}
Sets a field from a TomlValueKind.Table.
proc contains(node: TomlValueRef; key: string): bool {....raises: [], tags: [],
    forbids: [].}
Checks if key exists in node.
proc contains(node: TomlValueRef; val: TomlValueRef): bool {.
    ...raises: [Exception, KeyError], tags: [RootEffect], forbids: [].}
Checks if val exists in array node.
proc copy(p: TomlValueRef): TomlValueRef {....raises: [], tags: [], forbids: [].}
Performs a deep copy of a.
proc delete(obj: TomlValueRef; key: string) {....raises: [], tags: [], forbids: [].}
Deletes obj[key].
proc dump(table: TomlTableRef; indentLevel: int = 0) {....raises: [], tags: [],
    forbids: [].}
Dump out the entire table as it was parsed. This procedure is mostly useful for debugging purposes
proc existsKey(node: TomlValueRef; key: string): bool {....deprecated, raises: [],
    tags: [], forbids: [].}
Deprecated
Deprecated for hasKey
proc getBiggestInt(n: TomlValueRef; default: int64 = 0): int64 {....raises: [],
    tags: [], forbids: [].}

Retrieves the int64 value of a TomlValueKind.Int TomlValueRef.

Returns default if n is not a TomlValueKind.Int, or if n is nil.

proc getBool(n: TomlValueRef; default: bool = false): bool {....raises: [],
    tags: [], forbids: [].}

Retrieves the bool value of a TomlValueKind.Bool TomlValueRef.

Returns default if n is not a TomlValueKind.Bool, or if n is nil.

proc getElems(n: TomlValueRef; default: seq[TomlValueRef] = @[]): seq[
    TomlValueRef] {....raises: [], tags: [], forbids: [].}

Retrieves the int value of a TomlValueKind.Array TomlValueRef.

Returns default if n is not a TomlValueKind.Array, or if n is nil.

proc getFloat(n: TomlValueRef; default: float = 0.0): float {....raises: [],
    tags: [], forbids: [].}

Retrieves the float value of a TomlValueKind.Float TomlValueRef.

Returns default if n is not a TomlValueKind.Float or TomlValueKind.Int, or if n is nil.

proc getInt(n: TomlValueRef; default: int = 0): int {....raises: [], tags: [],
    forbids: [].}

Retrieves the int value of a TomlValueKind.Int TomlValueRef.

Returns default if n is not a TomlValueKind.Int, or if n is nil.

proc getOrDefault(node: TomlValueRef; key: string): TomlValueRef {....raises: [],
    tags: [], forbids: [].}
Gets a field from a node. If node is nil or not an object or value at key does not exist, returns nil
proc getStr(n: TomlValueRef; default: string = ""): string {....raises: [],
    tags: [], forbids: [].}

Retrieves the string value of a TomlValueKind.String TomlValueRef.

Returns default if n is not a TomlValueKind.String, or if n is nil.

proc getTable(n: TomlValueRef; default = new(TomlTableRef)): TomlTableRef {.
    ...raises: [], tags: [], forbids: [].}

Retrieves the key, value pairs of a TomlValueKind.Table TomlValueRef.

Returns default if n is not a TomlValueKind.Table, or if n is nil.

proc hash(n: OrderedTable[string, TomlValueRef]): Hash {.noSideEffect,
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
proc hash(n: TomlValueRef): Hash {.noSideEffect, ...raises: [Exception],
                                   tags: [RootEffect], forbids: [].}
Compute the hash for a TOML node
proc hasKey(node: TomlValueRef; key: string): bool {....raises: [], tags: [],
    forbids: [].}
Checks if key exists in node.
proc len(n: TomlValueRef): int {....raises: [], tags: [], forbids: [].}
If n is a TomlValueKind.Array, it returns the number of elements. If n is a TomlValueKind.Table, it returns the number of pairs. Else it returns 0.
proc newTArray(): TomlValueRef {....raises: [], tags: [], forbids: [].}
Creates a new TomlValueKind.Array TomlValueRef
proc newTBool(b: bool): TomlValueRef {....raises: [], tags: [], forbids: [].}
Creates a new TomlValueKind.Bool TomlValueRef.
proc newTFloat(n: float): TomlValueRef {....raises: [], tags: [], forbids: [].}
Creates a new TomlValueKind.Float TomlValueRef.
proc newTInt(n: int64): TomlValueRef {....raises: [], tags: [], forbids: [].}
Creates a new TomlValueKind.Int TomlValueRef.
proc newTNull(): TomlValueRef {....raises: [], tags: [], forbids: [].}
Creates a new JNull TomlValueRef.
proc newTString(s: string): TomlValueRef {....raises: [], tags: [], forbids: [].}
Creates a new TomlValueKind.String TomlValueRef.
proc newTTable(): TomlValueRef {....raises: [], tags: [], forbids: [].}
Creates a new TomlValueKind.Table TomlValueRef
proc parseFile(f: File; fileName: string = ""): TomlValueRef {.
    ...raises: [IOError, OSError, TomlError, ValueError, KeyError, Exception],
    tags: [ReadIOEffect, RootEffect, WriteIOEffect], forbids: [].}
Parses a file of TOML formatted data into a TOML table. The optional filename is used for error messages.
proc parseFile(fileName: string): TomlValueRef {.
    ...raises: [IOError, OSError, TomlError, ValueError, KeyError, Exception],
    tags: [ReadIOEffect, RootEffect, WriteIOEffect], forbids: [].}
Parses the file found at fileName with TOML formatted data into a TOML table.
proc parseStream(inputStream: streams.Stream; fileName: string = ""): TomlValueRef {.
    ...raises: [IOError, OSError, TomlError, ValueError, KeyError, Exception],
    tags: [ReadIOEffect, RootEffect], forbids: [].}
Parses a stream of TOML formatted data into a TOML table. The optional filename is used for error messages.
proc parseString(tomlStr: string; fileName: string = ""): TomlValueRef {.
    ...raises: [IOError, OSError, TomlError, ValueError, KeyError, Exception],
    tags: [ReadIOEffect, RootEffect, WriteIOEffect], forbids: [].}
Parses a string of TOML formatted data into a TOML table. The optional filename is used for error messages.
proc toJson(table: TomlTableRef): JsonNode {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
Converts a TOML table to a JSON node. This uses the format specified in the validation suite for it's output: https://github.com/BurntSushi/toml-test#example-json-encoding
proc toJson(value: TomlValueRef): JsonNode {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
proc toTomlString(value: TomlTableRef; parents = ""): string {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Converts a TOML table to a TOML formatted string for output to a file.
proc toTomlString(value: TomlValueRef): string {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
proc `{}`(node: TomlValueRef; keys: varargs[string]): TomlValueRef {....raises: [],
    tags: [], forbids: [].}
Traverses the node and gets the given value. If any of the keys do not exist, returns nil. Also returns nil if one of the intermediate data structures is not an object.
proc `{}=`(node: TomlValueRef; keys: varargs[string]; value: TomlValueRef) {.
    ...raises: [KeyError], tags: [], forbids: [].}
Traverses the node and tries to set the value at the given location to value. If any of the keys are missing, they are added.

Macros

macro `?*`(x: untyped): untyped
Convert an expression to a TomlValueRef directly, without having to specify ? for every element.
macro parseToml(x: untyped): untyped
Convert an expression to a TomlValueRef directly, without having to specify ? for every element.

Templates

template `?`(j: TomlValueRef): TomlValueRef
template simpleGetOrDefault{
  `{}`(node, [key])
}(node: TomlValueRef; key: string): TomlValueRef