tabnas-zon (Rust)
The Zig Object Notation (ZON) grammar plugin for the
tabnas parsing engine, crate
tabnas_zon.
ZON is the data format of Zig build.zig.zon manifests, built on Zig
anonymous struct literals:
.{
.name = "example",
.version = "0.0.1",
.dependencies = .{ .foo = .{ .url = "https://..." } },
.paths = .{ "build.zig", "src" },
}
This is a jsonic plugin: it layers on the relaxed-JSON grammar of
tabnas-jsonic and reshapes it into
ZON. It switches the jsonic extensions off, remaps the fixed tokens (.{
opens both a struct and a tuple, } closes both, = separates a field
from its value), adds six lex matchers for Zig syntax (dot tokens,
multi-line strings, character literals, Zig number literals, the
rejection of doc comments, and double-quoted strings with Zig's escape
set), and prepends the grammar alternates in
../zon-grammar.jsonic, which every runtime
embeds.
This is the Rust port of the canonical TypeScript implementation in
../ts; the TypeScript version is authoritative and this crate
tracks it. The Go port is in ../go.
Use
parse reuses one shared instance. Build your own with make, or with
make_with and typed options, and reuse it: building the grammar
dominates a parse.
To layer the plugin on an instance you configure yourself, install it on
a jsonic-enabled engine directly, or through use_plugin with an option
bag:
Parse errors are the engine's TabnasError, re-exported as ZonError,
with code, row, col and a report that shows the offending source.
The five codes this plugin declares are zon_number, zon_ident,
zon_char, zon_doc_comment and zon_dup_field:
Install
Neither the engine nor the grammars it builds on are published to a
registry, so all of them are consumed as sibling checkouts, the
standard tabnas development model. Clone
https://github.com/tabnas/parser, https://github.com/tabnas/json and
https://github.com/tabnas/jsonic next to this repository and point at
them:
[]
= { = "../zon/rs" }
= { = "../jsonic/rs" }
= { = "../parser/rs" }
All three entries are needed. A crate's dependencies are not passed on to
its dependents, so tabnas-zon alone does not put tabnas or
tabnas-jsonic in your extern prelude, and the examples above that name
tabnas_jsonic::make would not resolve. Only ZonError is re-exported.
The json checkout is needed because tabnas-jsonic takes it by path.
The test suite additionally needs https://github.com/tabnas/support,
for the shared fixture runner, and https://github.com/tabnas/debug,
for the composition test, beside the repository.
Differences from the canonical TypeScript
On every row of the shared fixtures in ../test/spec
and every document in the two Zig reference corpora, this crate gives
the TypeScript verdict and the TypeScript value, and the suite fails if
it stops. That is what is measured, and it is not a claim about every
document ZON can express: the inputs outside those sets on which the
runtimes are known to differ are measured, one table per input, in
../DIVERGENCE.md, and the two below that change a
verdict or a value are summarised there and here. The rest of what
differs is the shape of the API and the spelling of values the host
language has no type for:
- Options are a struct.
ZonOptionshaschar_as_numberandenum_tagas typed fields;to_valueandfrom_valueconvert to and from the option baguse_plugintakes, with the same defaults.from_valuereads each field on its own and by JavaScript truthiness, as the canonical plugin does, and reads the bag as the engine value it is rather than throughto_json, which renders a non-finite number asnull. The conversion is lossless: an emptyenum_tagsurvives the round trip, andtagtreats it as unset at the point of use, exactly asoptions.enumTag || nulldoes. An array or an object asenumTag, outside the option's declared type in every runtime, keeps its JSON spelling here rather than the JavaScript one. - A big integer is an object. An integer literal whose exact value no
IEEE-754 double holds is a
bigintin TypeScript and a*big.Intin Go. The engine'sValuehas no such variant, so this crate returns{ "$big": "<decimal digits>" }(tabnas_zon::BIG_KEY) rather than rounding, the spelling the Zig reference corpora already use for one. Every integer a double holds exactly,2^64included, is a plain number. inf,-infandnanaref64values, as in both other runtimes;to_jsonrenders them asnull, which is what a JSON round-trip does to them everywhere.- Key order is document order, and a struct is an
IndexMap, so a parsed manifest prints its fields in the order they were written. - The duplicate-field guard hands back an error token, as the
TypeScript hook does; the Go port signals the same code through the
parse context. The result is the same
zon_dup_fieldcode in all three, which../test/spec/errors.tsvpins. - No token descriptions are attached. The canonical plugin hangs a
table of human token descriptions off
cfg.tokenDescthrough aconfig.modifyhook, which@tabnas/railroadreads for a diagram legend. This engine's config has no such field, so this plugin attaches none, and the Rust railroad crate takes the descriptions from its ownExtractOptions::token_descinstead. No parse result depends on them. - A surrogate character literal folds to U+FFFD.
'\u{D800}'is an integer in Zig and a one-character string here by default; a RustStringcannot hold an unpaired surrogate, so the character is U+FFFD, where the canonical runtime keeps the UTF-16 code unit. Underchar_as_numberthe value is the number 55296 in every runtime. A surrogate escape in a"..."string or a.@"..."identifier is rejected in every runtime, as the Zig oracle rejects it. - A document nested more than 127 containers deep fails with the
engine's
cancelcode. The engine walks a value with the call stack to display, convert or drop it, so an unbounded one ends the process rather than failing; the budget is the onetabnas-jsonicalready applies, and TypeScript and Go set no limit. Abuild.zig.zonmanifest comes nowhere near it, and the deepest document in either Zig corpus nests 7 levels. - The column after a multi-line string is the true column. The
canonical runtime advances the column of a
\\string run by the token's whole length, newlines included, so it names a column too far right for a later error on that line; this port counts the rows the token spans. The row, the message and the quoted source line are the same in all three, and this is the one position difference the register records. - An exponent past what an integer holds saturates to an infinity
or a zero, the answer the Zig reference implementation gives on every
row measured. The canonical runtime spells the exponent back into the
literal it rebuilds and reads only a prefix once that spelling needs
exponent form itself (from
1e21), so1e999999999999999999999is10there; the Go port reads the exponent into a hostintand rejects what overflows it, from1e9223372036854775808on a 64-bit host. Neither boundary is a digit count. This is the one input class where this crate's answer is the reference's and the canonical one is not.
Build and test
The engine, the JSON core, the relaxed-JSON grammar, the fixture runner and the debug plugin are path dependencies on sibling checkouts, so there is nothing to fetch by hand:
&&
Or, from the repository root, make test-rs. For what CI would say,
including formatting and the lockfile check, run ci/rust/run.sh.
The suite runs every shared ../test/spec/*.tsv fixture, the same files
the TypeScript and Go suites run, building a fresh parser for each row's
opts column. It also grades the two Zig reference corpora
(test/zigzon/cases.json and test/strictness/cases.json), generating
them with scripts/fetch-zigzon.sh first when they are absent, exactly
as the other two runtimes do: a corpus that is still missing afterwards
fails the suite, it never skips. Beside them are the in-language tests
for what a fixture cannot express: big integers, infinities, NaN and
negative zero, the error messages, plugin layering and re-use, the
embedded grammar against its source, the shared default parser under
threads, that parse reuses its instance, and that the grammar composes
with tabnas-debug and reads back as the same structured model the
TypeScript suite asserts.
License
MIT.