ilusm.dev

toml

TOML parser and serialiser - parse a TOML string into an ilusm object tree covering [sections], [[array-of-tables]], inline tables, inline arrays, double- and single-quoted strings (with escape sequences), booleans, integers, floats, ISO 8601 dates and times; serialise an ilusm object tree back to well-formed TOML text.

Load with: use toml

What this module does

toml is a pure-ilusm TOML implementation. The parser walks the input line by line, building an object tree. Section headers [name] create or navigate nested objects; [[name]] append to an array of table objects. Key/value pairs handle the full TOML value grammar including dotted keys. The serialiser writes objects as TOML sections and primitive values using TOML syntax.

Quick example

use toml

src = '
[database]
host = "localhost"
port = 5432
flags = [1, 2, 3]

[[servers]]
name = "primary"
ip = "10.0.0.1"

[[servers]]
name = "replica"
ip = "10.0.0.2"
'

cfg = tomrd(src)
prn(cfg.database.host)        # "localhost"
prn(cfg.database.port)        # 5432
prn(cfg.servers[0].name)      # "primary"

# Serialise back to TOML
prn(tomwr(cfg))

Functions

Parsing

tomrd(s)

Parses a TOML string. Returns an ilusm object tree. Handles: [table] sections, [[array-of-tables]], inline tables {key = val}, inline arrays [1, 2, 3], double-quoted strings (with \n/\t/\\/\"/\uXXXX), single-quoted strings (literal), booleans, integers, floats, ISO 8601 dates, and times. Comments (#) and blank lines are ignored.

Serialisation

tomwr(obj)

Serialises an ilusm object to TOML text. Objects become [section] tables or inline tables as appropriate. Lists of objects become [[array-of-tables]]. Strings are quoted (triple-quoted if they contain newlines or quotes). Numbers, booleans, and nil use TOML literals.

Notes

  • Requires trl, txt, and jsn.