NAME
std/data/kdl - KDL parsing and serialization for ZuzuScript.
SYNOPSIS
from std/data/kdl import KDL;
let kdl := new KDL();
let doc := kdl.decode("""
package name="zuzu" { version "0.1.0" }
""");
let text := kdl.encode(doc);
IMPLEMENTATION SUPPORT
This module is supported by zuzu.pl, zuzu-rust, and zuzu-js on Node and Electron. It is partially supported by zuzu-js in the browser: in-memory KDL parsing and serialization coverage passes, but fixture and load/dump coverage is unsupported because browser filesystem capability is unavailable.
DESCRIPTION
This module provides a pure-Zuzu implementation of the KDL document model and parser, with a user-facing API modelled on std/data/json.
EXPORTS
The parser returns explicit KDL model objects rather than generic Dict and Array structures.
KDLDocument
KDLDocument represents a complete parsed KDL document.
Constructor fields:
nodesAn Array of top-level
KDLNodeobjects. Defaults to an empty Array.
Methods:
nodes()Parameters: none. Returns:
Array. Returns the Array of top-levelKDLNodeobjects.to_Iterator()Parameters: none. Returns:
Function. Returns an iterator over the document's top-level nodes.
KDLNode
KDLNode represents one KDL node.
Constructor fields:
nameThe node name as a String.
type_annotationThe node type annotation as a String, or
nullwhen unannotated.argsAn Array of unnamed argument
KDLValueobjects. Defaults to an empty Array.propsA
PairListmapping property names toKDLValueobjects. Defaults to an emptyPairList, and preserves duplicate property names.childrenAn Array of child
KDLNodeobjects. Defaults to an empty Array.
Methods:
name()Parameters: none. Returns:
String. Returns the node name.type_annotation()Parameters: none. Returns:
Stringornull. Returns the node type annotation.args()Parameters: none. Returns:
Array. Returns the unnamed argumentKDLValueobjects.props()Parameters: none. Returns:
PairList. Returns the named propertyKDLValueobjects.children()Parameters: none. Returns:
Array. Returns the childKDLNodeobjects.to_Iterator()Parameters: none. Returns:
Function. Returns an iterator over the node's arguments followed by its child nodes.
KDLValue
KDLValue represents an unnamed argument or named property value.
Constructor fields:
typeThe KDL value type:
null,boolean,number, orstring. Some conversion helpers may create opaqueKDLValueobjects with other type names; those are not KDL primitives.kindFor numbers, one of
integer,float, orstring.stringis used for KDL number keywords such as#infthat do not have a native JSON-like numeric value.valueThe raw native value.
type_annotationThe value type annotation as a String, or
nullwhen unannotated.canonical_numberThe canonical numeric spelling used by canonical serialization, or
null.
Methods:
type()Parameters: none. Returns:
String. Returns the value type.kind()Parameters: none. Returns:
Stringornull. Returns the numeric kind for number values.value()Parameters: none. Returns: value. Returns the raw native value.
type_annotation()Parameters: none. Returns:
Stringornull. Returns the value type annotation.canonical_number()Parameters: none. Returns:
Stringornull. Returns the canonical numeric spelling.is_null(),is_boolean(),is_number(),is_string()Parameters: none. Returns:
Boolean. Predicate methods for the four KDL primitive value types.is_true(),is_false()Parameters: none. Returns:
Boolean. Predicate methods for boolean#trueand#falsevalues.to_Number()Parameters: none. Returns:
Number. Returns the native number, or throws when the value is not a finite native number.to_String()Parameters: none. Returns:
String. Coerces the value to a string.to_Boolean()Parameters: none. Returns:
Boolean. Coerces the value to a boolean.native_value()Parameters: none. Returns: value. Returns the raw native value, with KDL
#nullrepresented asnull.
KDL
KDL is the codec class for KDL text.
codec.decode(String text)Parameters:
textis KDL text. Returns:KDLDocument. Parses KDL text.codec.decode_binarystring(BinaryString bytes)Parameters:
bytesis UTF-8 KDL bytes. Returns:KDLDocument. Parses KDL bytes.codec.encode(value)Parameters:
valueis aKDLDocument,KDLNode, or compatible value. Returns:String. Serializes KDL data to text.codec.encode_binarystring(value)Parameters:
valueis aKDLDocument,KDLNode, or compatible value. Returns:BinaryString. Serializes KDL data to UTF-8 bytes.codec.load(path)Parameters:
pathis astd/ioPath. Returns:KDLDocument. Reads and parses a KDL file.codec.dump(path, value)Parameters:
pathis astd/ioPathandvalueis KDL data. Returns:null. Serializes and writes a KDL file.
ZPATH
KDLDocument, KDLNode, and KDLValue objects can be queried with std/path/z. A KDL document's top-level nodes are exposed as children. A KDL node exposes its arguments first, then its child nodes, as ZPath children. KDL properties are exposed as ZPath attributes.
from std/data/kdl import KDL;
from std/path/z import ZPath;
let doc := ( new KDL() ).decode( """
(pkg)package "zuzu" (email)"dev@example.test" version="0.1.0" {
foo "first"
bar "middle"
foo "second"
foo "third"
}
""" );
// The package node's first child is its first argument.
say( ( new ZPath( path: "/package/#0" ) ).first(doc) ); // zuzu
// Properties are attributes.
say( ( new ZPath( path: "/package/@version" ) ).first(doc) ); // 0.1.0
// Child nodes can be selected by name and zero-based occurrence.
say( ( new ZPath( path: "/package/foo#2/#0" ) ).first(doc) ); // third
// tag() exposes type annotations on nodes and values.
say( ( new ZPath( path: "tag(/package)" ) ).first(doc) ); // pkg
say( ( new ZPath( path: "tag(/package/#1)" ) ).first(doc) ); // email
// local-name() exposes the node name, like it does for XML elements.
say( ( new ZPath( path: "local-name(/package)" ) ).first(doc) );
say( ( new ZPath( path: "/package/foo#2/local-name()" ) ).first(doc) );
COMPATIBILITY
The parser is recursive descent and covers the usual KDL 2.0 document model: nodes, arguments, properties, annotations, comments, slashdash, strings, booleans, nulls, numbers, and children.
COPYRIGHT AND LICENCE
std/data/kdl is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.