Skip to main content

Crate tabnas_jsonc

Crate tabnas_jsonc 

Source
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§

JsoncError
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.
JsoncOptions
The plugin options. Mirrors the TypeScript JsoncOptions type and the pluginOpts map the Go Jsonc reads; both keys are off by default.
TranslationPart
One optional alchemy source and the entry point a host calls.
TranslationParts
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, and tests/version_test.rs fails the build if they drift. Mirrors VERSION in ts/src/jsonc.ts and const VERSION in go/jsonc.go.

Functions§

grammar_text
The embedded grammar text, exactly as jsonc-grammar.jsonic holds 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 Go Jsonc(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 the new Tabnas().use(jsonic).use(Jsonc, options) of the TypeScript suite and the jsonic.Make() then j.Use(Jsonc, opts) of the Go one.
manifest_text
The plugin’s manifest, tabnas.plugin.json, as the repository carries it. Its translate object 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 the json render 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.json and translate/transduce.alc, since a packaged crate holds nothing outside rs/; tests/translate_test.rs holds each copy to its file.
parse
Parse a JSONC source string with the shared default parser.
plugin
The plugin form of jsonc, for Tabnas::use_plugin: the Rust counterpart of tn.use(Jsonc, options) and j.Use(Jsonc, opts).
translate
Return JSONC’s immutable translation parts.