Expand description
A JSONC (JSON with comments) grammar plugin for the tabnas parsing
engine.
JSONC is the dialect of editor and tool configuration files
(tsconfig.json, settings.json): standard JSON plus single-line
(//) and block (/* */) comments, with optional trailing commas in
objects and arrays.
The plugin is not standalone. It layers on the relaxed-JSON grammar of
tabnas_jsonic (the val / map / list / pair / elem rules)
and adds no rules of its own: it installs an end-of-input alternate on
val, the trailing-comma alternates on pair and elem, and pulls
the lexer, number, string and map options back toward standard JSON.
Two plugin options switch behaviour: JsoncOptions::disallow_comments
(strict JSON, no comments) and JsoncOptions::allow_trailing_comma.
Nesting is bounded at DEPTH_LIMIT levels, which the canonical
plugin does not do; see that constant for why.
let value = tabnas_jsonc::parse(r#"{ "foo": /* hello */ true }"#)?;
assert_eq!(value.to_string(), r#"{"foo":true}"#);This is the Rust port; the TypeScript package (ts/src/jsonc.ts) is
canonical and the Go port (go/jsonc.go) is the nearer structural
model. The grammar itself is authored once, in jsonc-grammar.jsonic
at the repository root, and embedded verbatim into all three runtimes.
The shared test/spec/*.tsv fixtures are the parity contract.
Structs§
- Jsonc
Error - The error a failed parse produces, re-exported so callers need not depend on the engine crate directly. Every code jsonc raises is inherited from the engine or from jsonic; the plugin declares none of its own.
- Jsonc
Options - The plugin options. Mirrors the TypeScript
JsoncOptionstype and thepluginOptsmap the GoJsoncreads; both keys are off by default. - Translation
Part - One optional alchemy source and the entry point a host calls.
- Translation
Parts - The package-local structural translation interface.
Constants§
- DEPTH_
LIMIT - The deepest nesting of objects and arrays a document may have.
- PLUGIN_
NAME - The name the plugin registers under, and so the namespace of its
option bag on the engine. Mirrors the TypeScript plugin function name
Jsonc, which is what the engine records in its plugin stack. - VERSION
- This crate’s version. It MUST equal
ts/package.json“version”: the release orchestrator rewrites both, andtests/version_test.rsfails the build if they drift. MirrorsVERSIONints/src/jsonc.tsandconst VERSIONingo/jsonc.go.
Functions§
- grammar_
text - The embedded grammar text, exactly as
jsonc-grammar.jsonicholds it (with the leading newline the embedding adds). It is jsonic, not JSON: the plugin parses it with a jsonic instance at install time, as the TypeScript and Go plugins do, so the file at the repository root stays the single source of truth for every runtime. - jsonc
- Install JSONC on an engine that ALREADY carries the jsonic grammar:
the body of the TypeScript
Jsonc(tn, options)plugin function and the GoJsonc(j, pluginOpts). - make
- Build a JSONC parser with the default options: comments on, trailing commas off.
- make_
with - Build a JSONC parser with the given options: a jsonic instance with
the plugin installed through
Tabnas::use_plugin, which is thenew Tabnas().use(jsonic).use(Jsonc, options)of the TypeScript suite and thejsonic.Make()thenj.Use(Jsonc, opts)of the Go one. - manifest_
text - The plugin’s manifest,
tabnas.plugin.json, as the repository carries it. Itstranslateobject is what a host that translates reads: the shape JSONC is read as and written from (tree), the root the render takes (any), the render that writes it, which is thejsonrender alchemy carries rather than a file of this repository’s, and the sentences that say what that render does not keep. It also names the transducer part,alchemy/transduce.alc, the document’s JSON events from the grammar’s rule passes, through which a host that reads parser events reads a document. The crate embeds its own copies,translate/manifest.jsonandtranslate/transduce.alc, since a packaged crate holds nothing outsiders/;tests/translate_test.rsholds each copy to its file. - parse
- Parse a JSONC source string with the shared default parser.
- plugin
- The plugin form of
jsonc, forTabnas::use_plugin: the Rust counterpart oftn.use(Jsonc, options)andj.Use(Jsonc, opts). - translate
- Return JSONC’s immutable translation parts.