tabnas-jsonc (Rust)
The JSONC (JSON with comments) grammar plugin for the
tabnas parsing engine, crate
tabnas_jsonc.
JSONC is the dialect of editor and tool configuration files such as
tsconfig.json and settings.json: standard JSON plus single-line
(//) and block (/* */) comments, with optional trailing commas in
objects and arrays. The plugin layers on the relaxed-JSON grammar of
tabnas-jsonic and adds no rules of
its own: it installs an end-of-input alternate, the trailing-comma
alternates, and pulls the lexer, number, string, and map options back
toward standard JSON.
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. The grammar itself is
authored once, in ../jsonc-grammar.jsonic,
and embedded into all three runtimes by ts/embed-grammar.js.
Use
The two plugin options are a typed struct. allow_trailing_comma
accepts a comma before a closing bracket, and disallow_comments
turns the plugin into a strict JSON parser:
A parser from make() or make_with() also reads a source from a reader, such as a file,
with parse_reader. It holds a window of the text as it parses, never the
whole document, and gives what parse gives for the same text.
To layer the plugin on an instance of your own, install it through
use_plugin, the tn.use(jsonic).use(Jsonc, options) of the TypeScript
package, with the option bag under the TypeScript key names; or call
jsonc directly on an engine that already carries the jsonic grammar:
Parse errors are the engine's TabnasError, re-exported as
JsoncError, with code, row, col and a report that shows the
offending source with a caret. The plugin declares no codes of its own;
the ones a JSONC document meets are unexpected, unprintable,
unterminated_comment, unterminated_string, invalid_unicode and
end_of_source, every one of them pinned by an ERROR:<code> row in the
shared fixtures and so identical in all three runtimes. One more,
cancel, is reached only past the nesting limit described below.
Install
The engine and the grammars it builds on are on crates.io, the engine as
tabnas-parser, whose library is named tabnas in code:
All three entries are needed. A crate's dependencies are not passed on
to its dependents, so tabnas-jsonc alone does not put tabnas or
tabnas-jsonic in your extern prelude, and the examples above that name
tabnas::Tabnas and tabnas_jsonic::make would not resolve. Only
JsoncError is re-exported.
In this repository the engine and jsonic are taken by path from sibling
checkouts instead (jsonic takes json the same way), so clone
https://github.com/tabnas/parser, https://github.com/tabnas/jsonic
and https://github.com/tabnas/json next to it. The test suite
additionally needs https://github.com/tabnas/support beside the
repository, for the shared fixture runner. The release workflow swaps
those paths for crates.io versions when it publishes this crate.
Differences from the canonical TypeScript
Within the two limits recorded in ../DIVERGENCE.md
and listed first below, every document the TypeScript plugin accepts or
rejects, this crate accepts or rejects with the same error code, and the
shared fixtures in ../test/spec and the RFC 8259
corpus in ../test/JSONTestSuite hold all
three runtimes to that. What differs is the shape of the API and those
two points:
- Nesting is bounded. Objects and arrays may nest
DEPTH_LIMIT(512) levels; the next level is rejected with thecancelcode. The TypeScript plugin sets no limit. The Rust engine's cost grows with the square of the nesting depth, and walking a parsed value costs one stack frame per level, so the limit is what keeps a hostile document from running for hours or from exhausting the stack of the thread that reads the result. It stands above the 500 levels the conformance pin requires to be accepted. - A trivia-only document is
null. Whitespace and comments alone have no value, which TypeScript returns asundefined. The Rust engine folds a top-level undefined intoValue::Nullat the end of every parse, soparse("// only")isNullhere, as it isnilin Go. The fixtures spell the caseUNDEFINED, and the Rust runner reads that cell asnull, exactly as the Go runner does. - Options are a struct, not an object.
JsoncOptionscarries the two switches with builder methods;to_valueandfrom_valuemove them through the engine's plugin option bag under the TypeScript key names (allowTrailingComma,disallowComments) foruse_plugin. parseis a convenience the other runtimes lack. The TypeScript package and the Go module are installed on an instance the caller builds; this crate also offersparseover one shared default instance, built on first use.- Key order is document order. Inherited from
tabnas-jsonic: anIndexMapkeeps every key where it arrived. - Lone surrogates fold to U+FFFD, and the regular expression
dialect is the
regexcrate's. Both come from the engine, and both are recorded there.
Build and test
The engine, the grammars and the fixture runner are path dependencies on sibling checkouts, so there is nothing to fetch:
&&
Or, from the repository root, make test-rs. For what CI would say,
including formatting and the Cargo.lock check, run ci/rust/run.sh.
The suite runs every shared ../test/spec/*.tsv fixture, the same files
the TypeScript and Go suites run, through the shared runner with a
fresh parser per row for the opts column; the vendored RFC 8259
corpus in all three option modes against the cross-runtime pin in
../test/known-lenient.json, failing rather than skipping when the
corpus is missing; the version test that holds Cargo.toml, VERSION
and ts/package.json together; and the in-language tests for what a
fixture cannot express: the port of the Go unit suite, the plugin's
group tags, option round-trips, layering on jsonic, the nesting
boundary, the embedded grammar against the file on disk, the shared
default parser under threads, and that reusing one instance beats
rebuilding it per parse.
License
MIT.