Skip to main content

Crate tabnas_json5

Crate tabnas_json5 

Source
Expand description

The JSON5 grammar plugin for the tabnas parsing engine.

JSON5 is JSON plus comments, unquoted keys, trailing commas, single quotes, hex numbers, Infinity and NaN, leading and trailing decimal points, explicit + signs, and string line continuations. The plugin layers on the relaxed-JSON base grammar of tabnas_jsonic and then TIGHTENS it toward the JSON5 specification: no implicit top-level a:1 or 1,2, no auto-close at the end of the source, no unquoted text at a value position, and only ECMAScript 5.1 IdentifierNames as unquoted keys.

let value = tabnas_json5::parse("{ a: 1, b: [2, 3,], }")?;
assert_eq!(value.to_string(), r#"{"a":1,"b":[2,3]}"#);

TypeScript is canonical: ts/src/json5.ts and the shared json5-grammar.jsonic define behaviour and option defaults. The shared fixtures in test/spec/*.tsv and the vendored test/json5-tests corpus are the parity contract across TypeScript, Go and Rust.

Structs§

Checked
The reader check_reader gives back: every byte the reader it read from gives, the ones it read first, and a read that failed there, where it failed.
Json5Error
The error a failed parse produces, re-exported so callers need not depend on the engine crate directly. The Go port returns the same *jsonic.JsonicError.
Json5Options
The plugin options: a strict JSON5 configuration by default, with the relaxations jsonic offers beyond the specification opt-in. The TypeScript Json5Options and the Go Defaults() map.
TranslationPart
One optional alchemy source and the entry point a host calls.
TranslationParts
The package-local structural translation interface.

Constants§

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/json5.ts and const VERSION in go/json5.go.

Functions§

check_reader
Apply the requireValue rule parse_with applies to a source read from reader, reading only as far as the answer needs: the leading whitespace and comments, and the first character after them. With the option on (the default), an empty source is json5_empty and one of only whitespace and comments json5_no_value, as parse_with has them; any other source comes back as a reader that gives every byte reader gives, the ones read here first, for the parse. A read that fails, or bytes that are not UTF-8, are the parse’s to report (the engine’s unreadable): the reader gives them as reader did. A parser without the option, or without the plugin, reads reader as it is.
json5
Install JSON5 on parser, which must already carry the jsonic grammar (from tabnas_jsonic::make, or tabnas_jsonic::jsonic on a bare engine). The counterpart of the TypeScript Json5 plugin function and the Go Json5(j, opts).
make
Build a JSON5 parser with the default (strict JSON5) options.
make_with
Build a JSON5 parser with caller options: Make(opts) in Go, which is jsonic.Make() followed by UseDefaults(Json5, Defaults(), opts), or make(opts) in TypeScript, which is new Tabnas().use(jsonic).use(Json5, opts).
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 JSON5 is read as and written from (tree), the root its render takes (any), the file that holds the render, and the sentences that say what the 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 JSON5 source string with the shared default parser.
parse_reader_with
Parse a JSON5 source read from reader with a parser that carries the plugin: the counterpart of parse_with for a reader, and of the Go ParseReader(j, r) and the TypeScript plugin’s tn.parseReader. The requireValue rule reads reader only as far as its answer needs (check_reader), and the parser then reads the rest through Tabnas::parse_reader: a window at a time where it declares lex.window, as make’s does, so a long document is never held whole. It gives the value and the errors parse_with gives of the same text, but for an error’s excerpt of a very long line (tabnas/parser DIVERGENCE.md).
parse_with
Parse a JSON5 source with a parser that carries the plugin: the counterpart of the Go Parse(j, src) and of the parse the TypeScript plugin wraps.
plugin
The plugin form of json5, for Tabnas::use_plugin. Its defaults are Json5Options::default, and the bag it receives is read with Json5Options::from_value, so a caller passes only the keys to change:
render_text
JSON5’s render, alchemy/render.alc, the file the manifest’s translate.render names: a library of alchemy definitions, with no export, whose entry point json5-render writes a tree’s events as one compact JSON5 document, a number that is not finite as Infinity, -Infinity or NaN. A host links it with its own program. The crate embeds its own copy, translate/render.alc, held to the file as the manifest’s is.
translate
Return JSON5’s immutable translation parts.