Simple and boring human readable data format for Zig.
zzz syntax describes a tree of strings. It has little syntactic noise and is really easy to implement. The spec does not force any specific rules for escaping or number parsing by design. Nodes in the tree are string slices.
zzz's focus is to be a simple and lightweight format. This library implements two trees: a static tree which has zero allocations, and a dynamic tree which manages slice and node allocations. Here's an example using the static tree:
// 100 is the max number of nodes.
var tree = zzz.StaticTree(100){};
try zzz.appendText(&tree, "foo:bar");
// There is always a root, and its value is empty.
assert(tree.root.value == "");
assert(tree.root.findNthChild(0, "foo") != null);
assert(tree.root.findNthDescendant(0, "bar") != null);
// Print the tree to standard out.
tree.root.show();
Note: This implementation is being iterated on while I use this for another project. API stability isn't a guarantee quite yet, and will also be subject to changes as Zig changes.
- Configuration files
- Game object descriptions
- Embedded devices
- Simple serialization format
D&D Kobold stat block. Raw text here.
(YAML highlighting used)
# Comments begin with a hash symbol.
# : describes a parent child relationship
name: Kobold
# , describes a sibling relationship
tags: small, humanoid, lawful evil
armor class: 12
# : can appear on the same line, here "(2d6 - 2)" is a child of "5"
hit points: 5 : (2d6 - 2)
# This can be used for meta tagging
speed: 30 : ft
# Continuing on a newline. The indentation is exactly 2 spaces to describe a parent/child relationship
stats:
str: 7: -2
dex: 15: 2
con: 9: -1
int: 8: -1
wis: 7: -2
cha: 8: -1
# ; is used to go up in the tree. Here we ascend up from the "ft" node
senses: darkvision:60:ft;; passive perception:8
languages: common, draconic
challenge: 1/8
# Multline strings follow the same rules as Lua's. The first newline on an empty line is skipped
abilities:
sunlight sensitivity: [[
While in sunlight, the kobold has disadvantage on attack
rolls, as well as on Wisdom (Perception) checks that rely on sight.]]
pack tactics: [[
The kobold has advantage on an attack roll against a
creature if at least one of the kobold's allies is within
5 feet of the creature and the ally isn't incapacitated.]]
Translated from http://www.json.org/example.html
menu:
id: file
value: File
popup:
menuitem:
: value: New
onclick: CreateNewDoc()
: value: Open
onclick: OpenDoc()
: value: Close
oneclick: CloseDoc()
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
For more examples see the source comments and tests.
zig build test
zig build examples
zzz text describes a tree of strings. Special characters (and spaces) are used to go up and down the tree. The tree has an implicit empty root node.
grandparent:parent:child:grandchild
Output:
null -> "grandparent" -> "parent" -> "child" -> "grandchild"
sibling1,sibling2,sibling3
Output:
null -> "sibling1"
-> "sibling2"
-> "sibling3"
parent:child;anotherparent
Output:
null -> "parent" -> "child"
-> "anotherparent"
parent:child
anotherparent
Output:
null -> "parent" -> "child"
-> "anotherparent"
parent:child
sibling
Output:
null -> "parent" -> "child"
-> "sibling"
parent:child
sibling
Output:
Error!
parent
child
sibling
Output:
null -> "parent" -> "child"
-> "sibling"
parent: child: grand child ;
Output:
null -> "parent" -> "child" -> "grand child"
"parent":[[ child ]]:[==[grand child]=]]==]:'great grand child'
Output:
null -> "parent" -> " child " -> "grand child]=]" -> "great grand child"
[[
some text]]
Output:
null -> "some text"
"\n\t\r"
Output:
null -> "\n\t\r"
Comments begin with # and run up to the end of the line. Their indentation follows the same rules as nodes.
# A comment
a node
# Another comment
a child
Output:
null -> "a node" -> "a child"