Skip to content

Parsing TOML

Lanexio Parser supports both TOML v1.0.0 and v1.1.0 through a single package. Choose your version explicitly, or use the default (v1.1.0).

  1. Install the package.

    Terminal window
    pnpm add @lanexio/parser-grammar-toml
  2. Parse a TOML document.

    import { parseToml } from '@lanexio/parser-grammar-toml';
    const encoder = new TextEncoder();
    const tree = parseToml(encoder.encode(`
    [server]
    host = "0.0.0.0"
    port = 8080
    [database]
    url = "postgres://localhost"
    pool = 10
    `));

Use parseToml10 for TOML v1.0.0 syntax and parseToml11 for v1.1.0 constructs like dotted keys in inline tables and hex float literals. parseToml defaults to v1.1.0.

import { parseToml10, parseToml11 } from '@lanexio/parser-grammar-toml';
const v1 = parseToml10(encoder.encode('[table]\nkey = "value"'));
const v2 = parseToml11(encoder.encode('a.b.c = 42'));

Tables become TomlTable nodes, key-value pairs become TomlKvPair nodes, and arrays become TomlArray nodes. Values carry type flags for string, integer, float, boolean, datetime, and array-of-types.

import { TomlKind } from '@lanexio/parser-grammar-toml';
const cursor = tree.cursor();
cursor.gotoFirstChild();
console.log(cursor.current.kind === TomlKind.TomlTable); // true

TOML 1.1.0 adds the \e escape (ESC character, U+001B) and \xHH byte escape to the existing \b, \t, \n, \f, \r, \", \\, \uXXXX, and \UXXXXXXXX escapes from TOML 1.0.0.

# All valid escape sequences
string_val = "Tab:\t Newline:\n Quote:\" Backslash:\\"
hex_val = "\x48\x65\x6C\x6C\x6F" # "Hello" (TOML 1.1.0 only)
esc_val = "\e" # ESC character (TOML 1.1.0 only)
unicode = "Hello" # "Hello"
big_unicode = "\U0001F600" # Unicode beyond BMP

The \xHH escape is only valid in TOML 1.1.0 mode. In TOML 1.0.0 mode, \x is a reserved escape sequence and produces an Error node.

The \e escape is valid in both TOML 1.0.0 and 1.1.0 per the toml-test reference suite.

import { parseToml10, parseToml11 } from '@lanexio/parser-grammar-toml';
const bytes = new TextEncoder();
// \xHH accepted in 1.1.0, rejected in 1.0.0
const v11 = parseToml11(bytes.encode('val = "\\x48"'));
console.log(v11.root.hasError); // false
const v10 = parseToml10(bytes.encode('val = "\\x48"'));
console.log(v10.root.hasError); // true
// \e accepted in both versions
const ev11 = parseToml11(bytes.encode('val = "\\e"'));
console.log(ev11.root.hasError); // false
const ev10 = parseToml10(bytes.encode('val = "\\e"'));
console.log(ev10.root.hasError); // false
EscapeCode PointDescriptionTOML Version
\bU+0008Backspace1.0.0+
\tU+0009Tab1.0.0+
\nU+000ALine Feed1.0.0+
\fU+000CForm Feed1.0.0+
\rU+000DCarriage Return1.0.0+
\"U+0022Quotation Mark1.0.0+
\\U+005CBackslash1.0.0+
\eU+001BEscape (ESC)1.0.0+
\xHHvariesByte escape (2 hex digits)1.1.0+
\uXXXXU+0000-U+FFFFUnicode (4 hex digits)1.0.0+
\UXXXXXXXXU+0000-U+10FFFFUnicode (8 hex digits)1.0.0+