Skip to content

@lanexio/parser-grammar-toml

This page documents @lanexio/parser-grammar-toml, the TOML grammar package supporting both TOML v1.0.0 and v1.1.0 with version selection.

  • Version: Stable
  • Module name: parser-grammar-toml
  • Package: @lanexio/parser-grammar-toml
  • Import path: @lanexio/parser-grammar-toml
  • Layer: 2 (Grammar)
  • Runtime: Universal
  • Module format: ESM
  • Stability: Stable
  • Primary use case: Parse TOML configuration files into a flat AST.
  • You need to parse TOML configuration files (v1.0.0 or v1.1.0) into a traversable AST.
  • You need strict adherence to a specific TOML version.
  • You need incremental reuse for repeated parsing.
BoundaryDescription
InputsUint8Array (source bytes) + optional ParseTomlOptions
OutputsLexTree (flat AST)
Side effectsNone
DeterminismYes
External dependencies@lanexio/parser-core
Never-throw guaranteeYes
Security surfaceNone (parse only, no output generation)
  1. Install the package.

    Terminal window
    pnpm add @lanexio/parser-grammar-toml
  2. Import the named export.

    import { parseToml } from '@lanexio/parser-grammar-toml';
RequirementRequiredLayerNotes
@lanexio/parser-coreYes1^1.0.0
import { parseToml } from '@lanexio/parser-grammar-toml';
const encoder = new TextEncoder();
const bytes = encoder.encode('[server]\nhost = "localhost"\nport = 8080');
const tree = parseToml(bytes);
console.log(tree.nodeCount);
import { parseToml10 } from '@lanexio/parser-grammar-toml';
const encoder = new TextEncoder();
const tree = parseToml10(encoder.encode('key = "value"'));
import { parseToml } from '@lanexio/parser-grammar-toml';
const encoder = new TextEncoder();
const tree = parseToml(encoder.encode('key = "value"'), { version: '1.1' });
ExportTypeDescription
parseToml(bytes: Uint8Array, options?: ParseTomlOptions) => LexTreeParse TOML (defaults to v1.1.0). Never throws.
parseToml10(bytes: Uint8Array) => LexTreeParse TOML v1.0.0 (strict).
parseToml11(bytes: Uint8Array) => LexTreeParse TOML v1.1.0.
TomlKindconst objectNumeric kind IDs for all TOML node types.
TomlFieldconst objectNumeric field IDs for TOML value slots.
TOML_FIELD_NAMES_BY_IDreadonly string[]Field name lookup by numeric field ID.
TOML_KIND_NAMES_BY_IDreadonly Record<number, string>Kind-name lookup by numeric ID.
tomlGrammarLanexioParserPureGrammarGrammar descriptor for use with parser-pure.
tomlRegistrationGrammarRegistrationRegistration for the unified grammar registry.
tomlReuseOracleReuseOracleIncremental reuse oracle.
tomlToValue(target: LexTree | LexNode, options?: ToValueOptions) => LexValueResultProject a TOML tree or node subtree into a host value. Never throws.
tomlValueExtractorLexValueExtractorThe TOML value extractor (registered under toml).

tomlToValue() reconstructs the TOML document object graph by path: dotted keys create nested objects, [table] headers create nested tables (with implicit tables on the way), [[array-of-tables]] headers append elements, and a nested [a.b] after [[a]] attaches to the most recent element. Basic and literal strings unescape, integers honor 0x/0o/0b and underscores (unsafe_integer past Number.MAX_SAFE_INTEGER), floats support inf/nan, booleans project, and date-times stay raw RFC 3339 text strings.

import { parseToml, tomlToValue } from '@lanexio/parser-grammar-toml';
const { value } = tomlToValue(parseToml(new TextEncoder().encode('[server]\nport = 8080\n')));
console.log(value); // { server: { port: 8080 } }

See the value-extraction reference for the full contract.

FieldTypeRequiredDefaultDescription
versionTomlVersionNo'1.1'TOML version dialect: '1.0' or '1.1'. Canonical key; wins over spec when both are given.
specTomlVersionNoundefinedDocumented alias for version (ADR 0048), matching the unified strict convention’s “spec” vocabulary. An explicit version wins; spec wins over the strict-mode default.
modeParseModeNo'lenient'Parse mode. Strict mode defaults the version to '1.0' when neither version nor spec is given.
strictbooleanNoundefinedConvenience alias for mode: 'strict' (ADR 0044). true picks strict, false is the explicit lenient spelling; an explicit mode wins.

parseToml with no options defaults to TOML v1.1.0. Version precedence (highest to lowest): an explicit version wins over spec, and either wins over the strict-mode default. parseToml10 and parseToml11 are unaffected.

Strict mode is mechanically enforced against the official toml-test reference suite (ADR 0034): every .toml file under corpus/toml-test/invalid/ (511 files across its 16 category subdirectories) must carry hasError: true under strict: true with zero false accepts. See TOML conformance for the ledger.

PropertyTypeDescription
rootLexNodeRoot node of the document.
nodeCountnumberTotal nodes in the tree.
sourceUint8ArrayOriginal parsed bytes.
TypePurposeNotes
ParseTomlOptionsOptions for parseToml{ version?, spec?, mode?, strict? }
TomlVersionTOML version string'1.0' | '1.1'
TomlKindTypeUnion of all TomlKind valuesType-safe kind reference
TomlFieldTypeUnion of all TomlField valuesType-safe field reference

parseToml defaults to TOML 1.1.0. Use parseToml10(bytes) for strict TOML 1.0.0. Use parseToml(bytes, { version: '1.0' }) or its spec alias parseToml(bytes, { spec: '1.0' }) for explicit version selection; an explicit version wins over spec, and either wins over the strict-mode default.

  • No direct accessibility surface. The TOML parser produces flat AST data structures.
ConcernStatus
Generated output semanticsNot applicable (data format)
ARIA attributes in serialized outputNot applicable
Semantic element round-tripNot applicable
  • parseToml never throws on any byte sequence.
ThreatMitigationStatus
Malformed input byte sequencePanic-free guarantee: all inputs accepted, errors produce LexError AST nodesImplemented
PackageRelationshipLayerNotes
@lanexio/parser-coreRequires1Provides LexTree, LexNode, LexCursor
@lanexio/parserConsumes6Unified entry point
VersionDateStatusNotable changes
1.0.02026-05-29CurrentInitial stable release. Apache-2.0.
  • None.