KDL

KDL is a small, pleasant document language with XML-like node semantics that looks like you’re invoking a bunch of CLI commands! It’s meant to be used both as a serialization format and a configuration language, much like JSON, YAML, or XML. It looks like this:

package {
  name my-pkg
  version "1.2.3"
 
  dependencies {
    // Nodes can have standalone values as well as
    // key/value pairs.
    lodash "^3.2.1" optional=#true alias=underscore
  }
 
  scripts {
    // "Raw" and dedented multi-line strings are supported.
    message """
      hello
      world
      """
    build #"""
      echo "foo"
      node -c "console.log('hello, world!');"
      echo "foo" > some-file.txt
      """#
  }
 
  // `\` breaks up a single node across multiple lines.
  the-matrix 1 2 3 \
             4 5 6 \
             7 8 9
 
  // "Slashdash" comments operate at the node level,
  // with just `/-`.
  /-this-is-commented {
    this entire node {
      is gone
    }
  }
}

The current version of the KDL spec is KDL 2.0.0. For legacy KDL, please refer to the KDL 1.0.0 spec. All users are encouraged to migrate. Migration is forward-and-backward-compatible and safe, and can be automated.

In addition to a spec for KDL itself, there are specifications for a KDL Query Language based on CSS selectors, and a KDL Schema Language loosely based on JSON Schema.

The language is based on SDLang, with a number of modifications and clarifications on its syntax and behavior. We are grateful for their work as an inspiration to ours.

Used By

A lot of folks have started picking up KDL for both personal projects, and larger open source, and even proprietary projects! This section includes a list of some examples of KDL in the wild (either v1, v2, or both):

  • Zellij - Terminal workspace/multiplexer
  • Niri - Scrollable-tiling window manager for Wayland
  • Bikeshed (here and here) - Specification pre-processor used by CSS, C++, WHATWG, various W3C working groups, and others.
  • orogene - Lightning-fast JavaScript package manager
  • Onyx - An efficient, procedural, and pragmatic programming language that compiles to WASM. Used for package manifests.
  • Pop!_OS/System76 Scheduler - Scheduling service which optimizes Linux’s CPU scheduler and makes it go faster.
  • ImStyle - ImGui application styling with Nim and KDL
  • fmod-rs - Rust bindings to FMOD Core and FMOD Studio
  • mise - dev tools, env vars, task runner
  • Camping - Ruby web microframework
  • Iron Vault - VTT (Virtual Tabletop) plugin for Obsidian for the Ironsworn family of games
  • Microsoft TypeScript DOM Generator - Tool for generating DOM-related TypeScript and JavaScript library files
  • You?

Implementations

  • Dart - kdl-dart*

  • Rust - kdl-rs (Format/comment-preserving parser)*

  • VSCode

Overview

Basics

A KDL node is a node name string, followed by zero or more “arguments”, and children.

title "Hello, World"

You can also have multiple values in a single node!

bookmarks 12 15 188 1234

Nodes can have properties, with string keys.

author "Alex Monad" email=alex@example.com active=#true

And they can have nested child nodes, too!

contents {
  section "First section" {
    paragraph "This is the first paragraph"
    paragraph "This is the second paragraph"
  }
}

Nodes without children are terminated by a newline, a semicolon, or the end of a file stream:

node1; node2; node3

Values

KDL supports 4 data types:

Strings: unquoted, "hello world", or #"hello world"# Numbers: 123.45, 0xdeadbeef, #inf, #-inf, #nan Booleans: #true and #false Null: #null

Strings

It supports three different formats for string input: unquoted, quoted, and raw.

node1 this-is-a-string
node2 "this\nhas\tescapes"
node3 #"C:\Users\zkat\raw\string"#

You don’t have to quote strings unless any the following apply:

  • The string contains whitespace.
  • The string contains any of []{}()\/#";=.
  • The string is one of true, false, null, inf, -inf, or nan.
  • The strings starts with a digit, or +/-/./-.,+. and a digit. (aka “looks like a number”)

In essence, if it can get confused for other KDL or KQL syntax, it needs quotes.

Both types of quoted string can be written across multiple lines by using triple quotes (""") followed immediately by a newline. Additionally, common indentation shared with the line containing the closing quotes will be stripped/dedented:

string """
  my
    multiline
  value
  """

Raw strings, which do not support \ escapes and can be used when you want certain kinds of strings to look nicer without having to escape a lot:

exec #"""
  echo "foo"
  echo "bar"
  cd C:\path\to\dir
  """#
 
regex #"\d{3} "[^/"]+""#

You can add any number of #s before and after the opening and closing # to disambiguate literal closing #" sequences:

other-raw ##"hello#"world"##

Numbers

There are 4 ways to represent numbers in KDL. KDL does not prescribe any representation for these numbers, and it’s entirely up to individual implementations whether to represent all numbers with a single type, or to have different representations for different forms.

KDL has regular decimal-radix numbers, with optional decimal part, as well as an optional exponent.

num 1.234e-42

And using the appropriate prefix, you can also enter hexadecimal, octal, and binary literals:

my-hex 0xdeadbeef
my-octal 0o755
my-binary 0b10101101

Finally, all numbers can have underscores to help readability:

bignum 1_000_000

Comments

KDL supports C-style comments, both line-based and multiline. Multiline comments can be nested.

// C style
 
/*
C style multiline
*/
 
tag /*foo=#true*/ bar=#false
 
/*/*
hello
*/*/

On top of that, KDL supports /- “slashdash” comments, which can be used to comment out individual nodes, entries, or child blocks:

// This entire node and its children are all commented out.
/-mynode foo key=1 {
  a
  b
  c
}
 
mynode /-commented "not commented" /-key=value /-{
  a
  b
}
// The above is equivalent to:
mynode "not commented"

Type Annotations

KDL supports type annotations on both values and nodes. These can be arbitrary, but can be used by individual implementations or use-cases to constrain KDL’s basic types. A number of type names are also reserved to have specific meanings.

numbers (u8)10 (i32)20 myfloat=(f32)1.5 {
  strings (uuid)"123e4567-e89b-12d3-a456-426614174000" (date)"2021-02-03" filter=(regex)#"$\d+"#
  (author)person name=Alex
}

More Details

// Nodes can be separated into multiple lines
title \
  "Some title"
 
 
// Files must be utf8 encoded!
smile 😁
 
// Node names and property keys are just strings, so you can write them like
// quoted or raw strings, too!
"illegal(){}[]/\\=#;identifier" #"1.2.3"# "#false"=#true
 
// Identifiers are very flexible. The following is a legal bare identifier:
-<123~!$@%^&*,.:'`|?+>
 
// And you can also use non-ASCII unicode!
ノード お名前=ฅ^•ﻌ•^ฅ
 
// kdl specifically allows properties and values to be
// interspersed with each other, much like CLI commands.
foo bar=#true baz quux=#false 1 2 3