Skip to content

Commit

Permalink
Merge pull request #2 from Patitotective/devel
Browse files Browse the repository at this point in the history
v1.0
  • Loading branch information
Patitotective authored Feb 19, 2023
2 parents 0c41b40 + b762d38 commit f1adf58
Show file tree
Hide file tree
Showing 17 changed files with 2,449 additions and 271 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ nimble install https://github.com/Patitotective/kdl-nim
```

## Features
- Streams support
- Compile-time parsing support
- [Decoder/Desializer](https://patitotective.github.io/kdl-nim/kdl/decoder.html)
- [Encoder/Serializer](https://patitotective.github.io/kdl-nim/kdl/encoder.html)
- [JSON-in-KDL](https://github.com/kdl-org/kdl/blob/main/JSON-IN-KDL.md) ([JiK](https://patitotective.github.io/kdl-nim/kdl/jik.html))
- [XML-in-KDL](https://github.com/kdl-org/kdl/blob/main/XML-IN-KDL.md) ([Xik](https://patitotective.github.io/kdl-nim/kdl/xik.html))
- [Prefs](https://patitotective.github.io/kdl-nim/kdl/prefs.html)

## Overview
```nim
Expand Down Expand Up @@ -83,7 +88,6 @@ doc.writeFile("doc.kdl")
Documentation is live at https://patitotective.github.io/kdl-nim/.

## TODO
- Make the lexer and parser work with `openArray[char]` or `cstring`
- Implement [KDL schema language](https://github.com/kdl-org/kdl/blob/main/SCHEMA-SPEC.md).
- Implement [KDL query language](https://github.com/kdl-org/kdl/blob/main/QUERY-SPEC.md).

Expand Down
6 changes: 3 additions & 3 deletions kdl.nimble
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Package

version = "0.2.2"
version = "1.0.0"
author = "Patitotective"
description = "KDL document language Nim implementation"
license = "MIT"
srcDir = "src"

skipFiles = @["src/kdl/query.nim", "src/kdl/schema.nim"]

# Dependencies

requires "nim >= 1.6.6"
requires "nim >= 1.6.8"

task docs, "Generate documentation":
exec "nim doc --git.url:https://github.com/Patitotective/kdl-nim --git.commit:main --outdir:docs --project src/kdl.nim"
Expand Down
59 changes: 34 additions & 25 deletions src/kdl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
runnableExamples:
let doc = parseKdl("node 1 null {child \"abc\" true}") # You can also read files using parseKdlFile("file.kdl")

assert doc[0][0].isInt() # 1
assert doc[0][1].isNull() # null
assert doc[0].children[0][0].isString() # "abc"
assert doc[0].children[0][1].isBool() # true
assert doc[0].args[0].isInt() # 1
assert doc[0].args[1].isNull() # null
assert doc[0].children[0].args[0].isString() # "abc"
assert doc[0].children[0].args[1].isBool() # true

## ### Reading nodes
runnableExamples:
Expand All @@ -28,7 +28,7 @@ runnableExamples:
assert doc[0].name == "node"
assert doc[0].tag.isSome and doc[0].tag.get == "tag" # Tags are Option[string]
assert doc[0]["key"] == "val" # Same as doc[0].props["key"]
assert doc[0].children[0][0] == "abc" # Same as doc[0].children[0].args[0]
assert doc[0].children[0].args[0] == "abc" # Same as doc[0].children[0].args[0]

## ### Reading values
## Accessing to the inner value of any `KdlVal` can be achieved by using any of the following procedures:
Expand All @@ -39,36 +39,37 @@ runnableExamples:
runnableExamples:
let doc = parseKdl("node 1 3.14 {child \"abc\" true}")

assert doc[0][0].getInt() == 1
assert doc[0][1].getFloat() == 3.14
assert doc[0].children[0][0].getString() == "abc"
assert doc[0].children[0][1].getBool() == true
assert doc[0].args[0].getInt() == 1
assert doc[0].args[1].getFloat() == 3.14
assert doc[0].children[0].args[0].getString() == "abc"
assert doc[0].children[0].args[1].getBool() == true

## There's also a generic procedure that converts `KdlValue` to the given type, consider this example:
runnableExamples:
let doc = parseKdl("node 1 3.14 255")

assert doc[0][0].get(float32) == 1f
assert doc[0][1].get(int) == 3
assert doc[0][2].get(uint8) == 255u8
assert doc[0].args[0].get(float32) == 1f
assert doc[0].args[1].get(int) == 3
assert doc[0].args[2].get(uint8) == 255u8

## It only converts between numbers, you can't `val.get(string)` if `val.isBool()`.
##
## ### Setting values
runnableExamples:
var doc = parseKdl("node 1 3.14 {child \"abc\" true}")

doc[0][0].setInt(10)
assert doc[0][0] == 10
doc[0].args[0].setInt(10)
assert doc[0].args[0] == 10

doc[0].children[0][1].setBool(false)
assert doc[0].children[0][1] == false
doc[0].children[0].args[1].setBool(false)
assert doc[0].children[0].args[1] == false

# You can also use the generic procedure `setTo`
doc[0][0].setTo(3.14)
assert doc[0][0] == 3
doc[0].args[0].setTo(3.14)
assert doc[0].args[0] == 3

doc[0].children[0][0].setTo("def")
assert doc[0].children[0][0] == "def"
doc[0].children[0].args[0].setTo("def")
assert doc[0].children[0].args[0] == "def"

## ### Creating KDL
## To create KDL documents, nodes or values without parsing you can also use the `toKdl`, `toKdlNode` and `toKdlVal` macros which have a similar syntax to KDL:
Expand All @@ -84,13 +85,21 @@ runnableExamples:
let node = toKdlNode: numbers(1, 2.13, 3.1e-10)
assert node == parseKdl("numbers 1 2.13 3.1e-10")[0]

assert toKdlVal("abc") == parseKdl("node \"abc\"")[0][0]
assert toKdlVal("abc") == parseKdl("node \"abc\"")[0].args[0]

## ## More
## Checkout these other useful modules as well:
## - [kdl/decoder](kdl/decoder.html) for KDL deserializing
## - [kdl/encoder](kdl/encoder.html) for KDL serializing
## - [kdl/xix](kdl/xik.html) for [XML-in-KDL](https://github.com/kdl-org/kdl/blob/main/XML-IN-KDL.md)
## - [kdl/jix](kdl/jix.html) for [JSON-in-KDL](https://github.com/kdl-org/kdl/blob/main/JSON-IN-KDL.md)
## - [kdl/prefs](kdl/prefs.html) for simple preferences sytem.

import std/[algorithm, enumerate, strformat, strutils, sequtils, options, tables]
import kdl/[parser, lexer, nodes, utils, xik, jik]

export parser, nodes
export utils except quoted
import kdl/[decoder, encoder, parser, lexer, nodes, types, utils, prefs, xik, jik]

export decoder, encoder, parser, nodes, types
export scanKdl, scanKdlFile, lexer.`$` # lexer

func indent(s: string, count: Natural, padding = " ", newLine = "\n"): string =
Expand Down Expand Up @@ -168,7 +177,7 @@ proc pretty*(doc: KdlDoc, newLine = true): string =

if newLine: result.add "\p"

proc writeFile*(doc: KdlDoc, path: string, pretty = false) =
proc writeFile*(path: string, doc: KdlDoc, pretty = false) =
## Writes `doc` to path. Set `pretty` to true to use `pretty` instead of `$`.
if pretty:
writeFile(path, doc.pretty())
Expand Down
Loading

0 comments on commit f1adf58

Please sign in to comment.