src/kdl/nodes

Search:
Group by:
Source   Edit  

This module implements initializers, comparision, getters, setters, operatores and macros to manipilate KdlVal, KdlNode and KdlDoc.

Procs

proc `$`(doc: KdlDoc): string {....raises: [KdlError, Exception],
                                tags: [RootEffect], forbids: [].}
Source   Edit  
proc `$`(node: KdlNode): string {....raises: [KdlError, Exception],
                                  tags: [RootEffect], forbids: [].}
The result is always valid KDL. Source   Edit  
proc `$`(val: KdlVal): string {....raises: [KdlError], tags: [], forbids: [].}
Returns "(tag)val" Source   Edit  
func `==`(node1, node2: KdlNode): bool {....raises: [KdlError, Exception],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc `==`(val1, val2: KdlVal): bool {....raises: [KdlError], tags: [], forbids: [].}
Checks if val1 and val2 have the same value. They must be of the same kind. Source   Edit  
proc `==`[T: Value](val: KdlVal; x: T): bool
Checks if val is x, raises an error when they are not comparable.

Example:

assert initKVal("a") == "a"
assert initKVal(1) == 1
assert initKVal(true) == true
Source   Edit  
proc `[]`(node: KdlNode; key: string): KdlVal {....raises: [KeyError], tags: [],
    forbids: [].}
Source   Edit  
proc `[]`(node: var KdlNode; key: string): var KdlVal {....raises: [KeyError],
    tags: [], forbids: [].}
Source   Edit  
proc `[]=`(node: var KdlNode; key: string; val: KdlVal) {....raises: [], tags: [],
    forbids: [].}
Sets the key property to val in node. Source   Edit  
proc add(node: var KdlNode; child: KdlNode) {....raises: [], tags: [], forbids: [].}
Adds child to node's children. Source   Edit  
proc add(node: var KdlNode; val: KdlVal) {....raises: [], tags: [], forbids: [].}
Adds val to node's arguments. Source   Edit  
proc contains(doc: KdlDoc; name: string): bool {....raises: [], tags: [],
    forbids: [].}
Checks if doc has a node called name Source   Edit  
proc contains(node: KdlNode; child: KdlNode): bool {.
    ...raises: [KdlError, Exception], tags: [RootEffect], forbids: [].}
Checks if node has the child children. Source   Edit  
proc contains(node: KdlNode; key: string): bool {....raises: [], tags: [],
    forbids: [].}
Checks if node has the key property. Source   Edit  
proc contains(node: KdlNode; val: KdlVal): bool {....raises: [KdlError], tags: [],
    forbids: [].}
Checks if node has the val argument. Source   Edit  
proc find(doc: KdlDoc; name: string): seq[KdlNode] {....raises: [], tags: [],
    forbids: [].}
Returns all the nodes called name. Source   Edit  
proc findFirst(doc: KdlDoc; name: string): int {....raises: [], tags: [],
    forbids: [].}
Returns the index of the first node called name. Returns -1 when it doesn't exist Source   Edit  
proc findLast(doc: KdlDoc; name: string): int {....raises: [], tags: [],
    forbids: [].}
Returns the index of the last node called name. Returns -1 when it doesn't exist Source   Edit  
proc get[T: Value](val: KdlVal; x: typedesc[T]): T
When x is string, stringifies val using $. when x is SomeNumber, converts val to x.

Example:

let val = initKFloat(3.14)

assert val.get(int) == 3
assert val.get(uint) == 3u
assert val.get(float) == 3.14
assert val.get(float32) == 3.14f
assert val.get(range[0f .. 4f]) == 3.14f
assert val.get(string) == "3.14"
Source   Edit  
proc getBool(val: KdlVal): bool {....raises: [KdlError], tags: [], forbids: [].}
Source   Edit  
proc getFloat(val: KdlVal): float {....raises: [KdlError], tags: [], forbids: [].}
Source   Edit  
proc getInt(val: KdlVal): int64 {....raises: [KdlError], tags: [], forbids: [].}
Source   Edit  
proc getString(val: KdlVal): string {....raises: [KdlError], tags: [], forbids: [].}
Source   Edit  
proc initKBool(val = bool.default; tag = string.none): KdlVal {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc initKFloat(val: SomeFloat = float.default; tag = string.none): KdlVal
Source   Edit  
proc initKInt(val: SomeInteger = int64.default; tag = string.none): KdlVal
Source   Edit  
proc initKNode(name: string; tag = string.none;
               args: openArray[KdlVal] = newSeq[KdlVal]();
               props = initTable[string, KdlVal]();
               children: openArray[KdlNode] = newSeq[KdlNode]()): KdlNode {.
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc initKNull(tag = string.none): KdlVal {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc initKString(val = string.default; tag = string.none): KdlVal {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc initKVal(val: bool; tag = string.none): KdlVal {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc initKVal(val: KdlVal): KdlVal {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc initKVal(val: SomeFloat; tag = string.none): KdlVal
Source   Edit  
proc initKVal(val: SomeInteger; tag = string.none): KdlVal
Source   Edit  
proc initKVal(val: string; tag = string.none): KdlVal {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc initKVal(val: typeof(nil); tag = string.none): KdlVal {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc inline(doc: KdlDoc): string {....raises: [KdlError, Exception],
                                   tags: [RootEffect], forbids: [].}
Source   Edit  
proc inline(node: KdlNode): string {....raises: [KdlError, Exception],
                                     tags: [RootEffect], forbids: [].}
Returns node's single-line representation Source   Edit  
proc isBool(val: KdlVal): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc isEmpty(val: KdlVal): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc isFloat(val: KdlVal): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc isInt(val: KdlVal): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc isNull(val: KdlVal): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc isString(val: KdlVal): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc setBool(val: var KdlVal; x: bool) {....raises: [KdlError], tags: [],
    forbids: [].}
Source   Edit  
proc setFloat(val: var KdlVal; x: SomeFloat)
Source   Edit  
proc setInt(val: var KdlVal; x: SomeInteger)
Source   Edit  
proc setString(val: var KdlVal; x: string) {....raises: [KdlError], tags: [],
    forbids: [].}
Source   Edit  
proc setTo[T: Value](val: var KdlVal; x: T)
Tries to set val to x, raises an error when types are not compatible.

Example:

var val = initKFloat(3.14)

val.setTo(100u8)

assert val.getFloat() == 100

val.setTo(20.12e2f)

assert val.get(float32) == 20.12e2f
Source   Edit  

Macros

macro toKdlArgs(args: varargs[untyped]): untyped
Creates an array of KdlVals by calling initKVal through args.

Example:

assert toKdlArgs(1, 2, "a"[tag]) ==
  [1.initKVal, 2.initKVal, "a".initKVal("tag".some)]
assert initKNode("name", args = toKdlArgs(nil, true, "b")) ==
  initKNode("name", args = [initKNull(), true.initKVal, "b".initKVal])
Source   Edit  
macro toKdlDoc(body: untyped): KdlDoc

Generate a KdlDoc from Nim's AST that is somewhat similar to KDL's syntax. body has to be an statement list

See also toKdlNode.

Example:

let doc = toKdlDoc:
  node
  numbers(10[u8], 20[i32], myfloat = 1.5[f32]):
    strings(
      "123e4567-e89b-12d3-a456-426614174000"[uuid],
      "2021-02-03"[date],
      filter = r"$\d+"[regex],
    )
    person[author](name = "Alex")
  "i am also a node"
  color[RGB](r = 200, b = 100, g = 100)
Source   Edit  
macro toKdlNode(body: untyped): KdlNode
Generate a KdlNode from Nim's AST that is somewhat similar to KDL's syntax.
  • For nodes use call syntax: node(args, props).
  • For properties use an equal expression: key=val.
  • For children pass a block to a node: node(args, props): ...

Example:

let node = toKdlNode:
  numbers(10[u8], 20[i32], myfloat = 1.5[f32]):
    strings(
      "123e4567-e89b-12d3-a456-426614174000"[uuid],
      "2021-02-03"[date],
      filter = r"$\d+"[regex],
    )
    person[author](name = "Alex")
# It is the same as:
# numbers (u8)10 (i32)20 myfloat=(f32)1.5 {
#   strings (uuid)"123e4567-e89b-12d3-a456-426614174000" (date)"2021-02-03" filter=(regex)r"$\d+"
#   (author)person name="Alex"
# }
Source   Edit  
macro toKdlProps(props: untyped): Table[string, KdlVal]
Creates a Table[string, KdlVal] from a array-of-tuples/table-constructor by calling initKVal through the values.

Example:

assert toKdlProps({"a": 1[i8], "b": 2}) ==
  {"a": 1.initKVal("i8".some), "b": 2.initKVal}.toTable
assert initKNode("name", props = toKdlProps({"c": nil, "d": true})) ==
  initKNode("name", props = {"c": initKNull(), "d": true.initKVal}.toTable)
Source   Edit  
macro toKdlVal(body: untyped): KdlVal
Generate a KdlVal from Nim's AST that is somehat similar to KDL's syntax.
  • For type annotations use a bracket expresion: node[tag] instead of (tag)node.
Source   Edit