@tabnas/json 0.5.7 → 0.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/dist/json.d.ts +5 -2
- package/dist/json.js +20 -6
- package/dist/json.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @tabnas/json
|
|
2
2
|
|
|
3
3
|
A standard JSON parser (RFC 8259 / ECMA-404) for TypeScript and
|
|
4
|
-
JavaScript
|
|
4
|
+
JavaScript: the standard-JSON **grammar plugin** for the
|
|
5
5
|
[`tabnas`](https://github.com/tabnas/parser) parsing engine.
|
|
6
6
|
|
|
7
7
|
Available for [TypeScript/JavaScript](#install) and [Go](../go/).
|
|
@@ -36,12 +36,13 @@ parse('"hello"') // => "hello"
|
|
|
36
36
|
|
|
37
37
|
Full [Diátaxis](https://diataxis.fr) docs:
|
|
38
38
|
|
|
39
|
-
- [`doc/tutorial.md`](doc/tutorial.md)
|
|
40
|
-
- [`doc/guide.md`](doc/guide.md)
|
|
41
|
-
- [`doc/reference.md`](doc/reference.md)
|
|
42
|
-
- [`doc/concepts.md`](doc/concepts.md)
|
|
39
|
+
- [`doc/tutorial.md`](doc/tutorial.md). Learn it step by step.
|
|
40
|
+
- [`doc/guide.md`](doc/guide.md). Task-focused recipes.
|
|
41
|
+
- [`doc/reference.md`](doc/reference.md). The exact API and CLI surface.
|
|
42
|
+
- [`doc/concepts.md`](doc/concepts.md). How it works and why.
|
|
43
43
|
|
|
44
|
-
The Go port has the [equivalent docs](../go/doc/)
|
|
44
|
+
The Go port has the [equivalent docs](../go/doc/), and so does the Rust
|
|
45
|
+
port in [`../rs/doc/`](../rs/doc/).
|
|
45
46
|
|
|
46
47
|
## Use it as a plugin
|
|
47
48
|
|
|
@@ -62,9 +63,9 @@ options, for plugins that want to build on the JSON rule set.
|
|
|
62
63
|
|
|
63
64
|
## Reuse and options
|
|
64
65
|
|
|
65
|
-
`parse` reuses a single lazily
|
|
66
|
+
`parse` reuses a single lazily created instance, so you don't pay to
|
|
66
67
|
rebuild the grammar on every call. To customize, build your own instance
|
|
67
|
-
with `make(opts?)
|
|
68
|
+
with `make(opts?)`; extra options (for example the `info` metadata options) are
|
|
68
69
|
applied on top of the strict JSON config:
|
|
69
70
|
|
|
70
71
|
```ts
|
|
@@ -90,17 +91,16 @@ Exactly standard JSON:
|
|
|
90
91
|
- `true`, `false`, `null`
|
|
91
92
|
- insignificant whitespace: space, tab, line feed, carriage return
|
|
92
93
|
|
|
93
|
-
It **rejects** everything outside that grammar
|
|
94
|
+
It **rejects** everything outside that grammar: comments, trailing
|
|
94
95
|
commas, unquoted keys, single-quoted or backtick strings, multiline
|
|
95
96
|
strings, implicit objects/arrays, hex/octal/binary numbers, leading
|
|
96
97
|
zeros, a leading `+`, a bare `.5` or trailing `1.`, and empty input.
|
|
97
98
|
This matches the platform `JSON.parse`.
|
|
98
99
|
|
|
99
100
|
Parsed objects use a **null prototype** (`Object.create(null)`): this is
|
|
100
|
-
deliberate and prototype-pollution-safe
|
|
101
|
+
deliberate and prototype-pollution-safe: a `"__proto__"` key becomes a
|
|
101
102
|
normal own property rather than mutating the prototype. The only visible
|
|
102
|
-
difference from `JSON.parse` is the missing `Object.prototype` (so
|
|
103
|
-
`obj.hasOwnProperty` is `undefined`; use `Object.hasOwn(obj, k)`).
|
|
103
|
+
difference from `JSON.parse` is the missing `Object.prototype` (so for example `obj.hasOwnProperty` is `undefined`; use `Object.hasOwn(obj, k)`).
|
|
104
104
|
|
|
105
105
|
## Extending the grammar
|
|
106
106
|
|
package/dist/json.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { Tabnas, TabnasError, type Plugin } from '@tabnas/parser';
|
|
2
|
-
export declare const VERSION = "0.5.
|
|
3
|
-
export
|
|
2
|
+
export declare const VERSION = "0.5.9";
|
|
3
|
+
export interface JsonGrammarOptions {
|
|
4
|
+
chainOff?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function registerJsonGrammar(tn: Tabnas, opts?: JsonGrammarOptions): void;
|
|
4
7
|
export declare const json: Plugin;
|
|
5
8
|
export declare function make(opts?: Record<string, any>): Tabnas;
|
|
6
9
|
export declare function parse(src: string): any;
|
package/dist/json.js
CHANGED
|
@@ -29,7 +29,7 @@ Object.defineProperty(exports, "JsonError", { enumerable: true, get: function ()
|
|
|
29
29
|
// VERSION is this package's version. It MUST equal package.json "version":
|
|
30
30
|
// the release orchestrator rewrites both, and test/version.test.js fails the
|
|
31
31
|
// build if they drift. Mirrors `const VERSION` in go/json.go.
|
|
32
|
-
exports.VERSION = '0.5.
|
|
32
|
+
exports.VERSION = '0.5.9';
|
|
33
33
|
// JSON-only lexer/parser options. Restrictive enough to mirror
|
|
34
34
|
// JSON.parse: double-quoted strings only, plain decimal numbers, quoted
|
|
35
35
|
// keys only, no comments, no trailing-comma map extension, no empty
|
|
@@ -72,7 +72,13 @@ const JSON_OPTIONS = {
|
|
|
72
72
|
// given engine instance. Exposed separately from the options so other
|
|
73
73
|
// grammar plugins can layer their extensions on top without re-declaring
|
|
74
74
|
// the JSON core. This is jsonic's "Plain JSON" grammar.
|
|
75
|
-
function registerJsonGrammar(tn) {
|
|
75
|
+
function registerJsonGrammar(tn, opts) {
|
|
76
|
+
// Spread onto the two `elem` close alts. Nothing is emitted unless
|
|
77
|
+
// asked for, and an absent `k` is the engine default: the walk runs.
|
|
78
|
+
// Called per alt so each gets its OWN object, as the two literals it
|
|
79
|
+
// replaces did: alt config is the engine's to read, and two alts
|
|
80
|
+
// sharing one object would make that an assumption rather than a fact.
|
|
81
|
+
const pushChain = () => opts?.chainOff ? { k: { push$: { chain: false } } } : {};
|
|
76
82
|
tn.grammar({
|
|
77
83
|
// The schema version of the native-value builtins this grammar binds
|
|
78
84
|
// to (object/array/reset/key/setval/push/value).
|
|
@@ -173,10 +179,15 @@ function registerJsonGrammar(tn) {
|
|
|
173
179
|
{ p: 'val', g: 'list,elem,val,json' },
|
|
174
180
|
],
|
|
175
181
|
close: [
|
|
176
|
-
// Next element. @push$ appends the element.
|
|
177
|
-
|
|
182
|
+
// Next element. @push$ appends the element. `r: 'elem'`
|
|
183
|
+
// REPLACES this rule, so the rule it displaces keeps its own
|
|
184
|
+
// view of the list -- which is why `chainOff` is a claim only
|
|
185
|
+
// the assembled grammar can make. See JsonGrammarOptions.
|
|
186
|
+
{ s: '#CA', r: 'elem', a: '@push$',
|
|
187
|
+
...pushChain(), g: 'list,elem,comma,json' },
|
|
178
188
|
// End of list.
|
|
179
|
-
{ s: '#CS', b: 1, a: '@push$',
|
|
189
|
+
{ s: '#CS', b: 1, a: '@push$',
|
|
190
|
+
...pushChain(), g: 'list,elem,close,json' },
|
|
180
191
|
],
|
|
181
192
|
},
|
|
182
193
|
},
|
|
@@ -186,7 +197,10 @@ function registerJsonGrammar(tn) {
|
|
|
186
197
|
// the JSON grammar. `use` this on a bare engine, or pass it to `make`.
|
|
187
198
|
const json = function json(tn, _options) {
|
|
188
199
|
tn.options(JSON_OPTIONS);
|
|
189
|
-
|
|
200
|
+
// The complete parser: these rules are the whole grammar, and none of
|
|
201
|
+
// them reads a replaced rule. That is what makes `chainOff` true here
|
|
202
|
+
// and false in the rules-only installer above.
|
|
203
|
+
registerJsonGrammar(tn, { chainOff: true });
|
|
190
204
|
};
|
|
191
205
|
exports.json = json;
|
|
192
206
|
// Create a standard-JSON parser instance: a tabnas engine with the json
|
package/dist/json.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";AAAA,4CAA4C;;;
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";AAAA,4CAA4C;;;AAuF5C,kDA0IC;AAeD,oBAMC;AASD,sBAEC;AA/PD;;;;;;;;;;;;;;;;GAgBG;AAEH,2CAAiE;AA+OxD,uFA/OA,eAAM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