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_readergives back: every byte the reader it read from gives, the ones it read first, and a read that failed there, where it failed. - Json5
Error - 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. - Json5
Options - The plugin options: a strict JSON5 configuration by default, with the
relaxations jsonic offers beyond the specification opt-in. The
TypeScript
Json5Optionsand the GoDefaults()map. - Translation
Part - One optional alchemy source and the entry point a host calls.
- Translation
Parts - The package-local structural translation interface.
Constants§
- 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/json5.tsandconst VERSIONingo/json5.go.
Functions§
- check_
reader - Apply the
requireValueruleparse_withapplies to a source read fromreader, 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 isjson5_emptyand one of only whitespace and commentsjson5_no_value, asparse_withhas them; any other source comes back as a reader that gives every bytereadergives, 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’sunreadable): the reader gives them asreaderdid. A parser without the option, or without the plugin, readsreaderas it is. - json5
- Install JSON5 on
parser, which must already carry the jsonic grammar (fromtabnas_jsonic::make, ortabnas_jsonic::jsonicon a bare engine). The counterpart of the TypeScriptJson5plugin function and the GoJson5(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 isjsonic.Make()followed byUseDefaults(Json5, Defaults(), opts), ormake(opts)in TypeScript, which isnew Tabnas().use(jsonic).use(Json5, opts). - manifest_
text - The plugin’s manifest,
tabnas.plugin.json, as the repository carries it. Itstranslateobject 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.jsonandtranslate/transduce.alc, since a packaged crate holds nothing outsiders/;tests/translate_test.rsholds 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
readerwith a parser that carries the plugin: the counterpart ofparse_withfor a reader, and of the GoParseReader(j, r)and the TypeScript plugin’stn.parseReader. TherequireValuerule readsreaderonly as far as its answer needs (check_reader), and the parser then reads the rest throughTabnas::parse_reader: a window at a time where it declareslex.window, asmake’s does, so a long document is never held whole. It gives the value and the errorsparse_withgives of the same text, but for an error’s excerpt of a very long line (tabnas/parserDIVERGENCE.md). - parse_
with - Parse a JSON5 source with a parser that carries the plugin: the
counterpart of the Go
Parse(j, src)and of theparsethe TypeScript plugin wraps. - plugin
- The plugin form of
json5, forTabnas::use_plugin. Its defaults areJson5Options::default, and the bag it receives is read withJson5Options::from_value, so a caller passes only the keys to change: - render_
text - JSON5’s render,
alchemy/render.alc, the file the manifest’stranslate.rendernames: a library of alchemy definitions, with noexport, whose entry pointjson5-renderwrites a tree’s events as one compact JSON5 document, a number that is not finite asInfinity,-InfinityorNaN. 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.