json-brook 0.1.0-alpha.0 → 0.1.0-alpha.2
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/CHANGELOG.md +23 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +124 -90
- package/dist/index.umd.cjs +2 -2
- package/package.json +60 -54
package/CHANGELOG.md
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import * as generate from "./generate";
|
|
1
2
|
import * as parse from "./parse";
|
|
2
3
|
import * as tokenize from "./tokenize";
|
|
4
|
+
export type * from './generate';
|
|
3
5
|
export type * from './parse';
|
|
4
6
|
export type * from './tokenize';
|
|
5
|
-
export { parse, tokenize };
|
|
7
|
+
export { generate, parse, tokenize };
|
|
6
8
|
export type JsonBrook = {
|
|
7
9
|
getRoot: () => parse.RootNode;
|
|
8
10
|
getCurrent: () => parse.Node;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,40 @@
|
|
|
1
|
-
const
|
|
1
|
+
const O = (e) => {
|
|
2
|
+
if (e.current)
|
|
3
|
+
switch (e.current.type) {
|
|
4
|
+
case "keyword":
|
|
5
|
+
return e.current.value;
|
|
6
|
+
case "string":
|
|
7
|
+
return JSON.parse(
|
|
8
|
+
e.current.escapeLength > 0 ? e.current.value.slice(0, -e.current.escapeLength) : e.current.value
|
|
9
|
+
);
|
|
10
|
+
case "number":
|
|
11
|
+
return JSON.parse(
|
|
12
|
+
e.current.value.slice(0, e.current.validLength)
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
return e.value;
|
|
16
|
+
}, j = (e) => e.children.map((t) => w(t)), P = (e) => e.children.reduce(
|
|
17
|
+
(t, r) => (r.value && (t[r.key.value] = w(r.value)), t),
|
|
18
|
+
{}
|
|
19
|
+
), w = (e) => {
|
|
20
|
+
switch (e.type) {
|
|
21
|
+
case "literal":
|
|
22
|
+
return O(e);
|
|
23
|
+
case "array":
|
|
24
|
+
return j(e);
|
|
25
|
+
case "object":
|
|
26
|
+
return P(e);
|
|
27
|
+
}
|
|
28
|
+
}, D = (e) => {
|
|
29
|
+
if (e.value)
|
|
30
|
+
return w(e.value);
|
|
31
|
+
}, _ = (e) => D(e.getRoot()), A = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
32
|
+
__proto__: null,
|
|
33
|
+
simpleGenerator: _
|
|
34
|
+
}, Symbol.toStringTag, { value: "Module" })), p = {
|
|
2
35
|
Normal: 1,
|
|
3
36
|
Escape: 2
|
|
4
|
-
},
|
|
37
|
+
}, a = {
|
|
5
38
|
Negative: 1,
|
|
6
39
|
Zero: 2,
|
|
7
40
|
Digit: 3,
|
|
@@ -9,14 +42,14 @@ const p = {
|
|
|
9
42
|
DigitFraction: 5,
|
|
10
43
|
Exp: 6,
|
|
11
44
|
ExpDigitOrSign: 7
|
|
12
|
-
},
|
|
13
|
-
`, "\r"],
|
|
14
|
-
if (
|
|
45
|
+
}, C = [" ", " ", `
|
|
46
|
+
`, "\r"], V = ["{", "}", "[", "]", ":", ","], g = ["true", "false", "null"], J = g.map((e) => e[0]), G = ['"', "\\", "/", "b", "f", "n", "r", "t"], K = ["+", "-"], c = (e) => e >= "0" && e <= "9", h = (e) => e >= "1" && e <= "9", Z = (e) => c(e) || e >= "a" && e <= "f" || e >= "A" && e <= "F", k = (e) => e === "e" || e === "E", o = (e) => {
|
|
47
|
+
if (C.includes(e))
|
|
15
48
|
return {
|
|
16
49
|
token: null,
|
|
17
50
|
current: null
|
|
18
51
|
};
|
|
19
|
-
if (
|
|
52
|
+
if (V.includes(e))
|
|
20
53
|
return {
|
|
21
54
|
token: {
|
|
22
55
|
type: "symbol",
|
|
@@ -24,13 +57,13 @@ const p = {
|
|
|
24
57
|
},
|
|
25
58
|
current: null
|
|
26
59
|
};
|
|
27
|
-
const t =
|
|
60
|
+
const t = J.indexOf(e);
|
|
28
61
|
if (t !== -1)
|
|
29
62
|
return {
|
|
30
63
|
token: null,
|
|
31
64
|
current: {
|
|
32
65
|
type: "keyword",
|
|
33
|
-
value:
|
|
66
|
+
value: g[t],
|
|
34
67
|
matchLength: 1
|
|
35
68
|
}
|
|
36
69
|
};
|
|
@@ -49,7 +82,7 @@ const p = {
|
|
|
49
82
|
token: null,
|
|
50
83
|
current: {
|
|
51
84
|
type: "number",
|
|
52
|
-
state:
|
|
85
|
+
state: a.Negative,
|
|
53
86
|
value: e,
|
|
54
87
|
validLength: 0
|
|
55
88
|
}
|
|
@@ -59,17 +92,17 @@ const p = {
|
|
|
59
92
|
token: null,
|
|
60
93
|
current: {
|
|
61
94
|
type: "number",
|
|
62
|
-
state:
|
|
95
|
+
state: a.Zero,
|
|
63
96
|
value: e,
|
|
64
97
|
validLength: 1
|
|
65
98
|
}
|
|
66
99
|
};
|
|
67
|
-
if (
|
|
100
|
+
if (h(e))
|
|
68
101
|
return {
|
|
69
102
|
token: null,
|
|
70
103
|
current: {
|
|
71
104
|
type: "number",
|
|
72
|
-
state:
|
|
105
|
+
state: a.Digit,
|
|
73
106
|
value: e,
|
|
74
107
|
validLength: 1
|
|
75
108
|
}
|
|
@@ -88,7 +121,7 @@ const p = {
|
|
|
88
121
|
current: t
|
|
89
122
|
};
|
|
90
123
|
throw new Error("解析失败");
|
|
91
|
-
},
|
|
124
|
+
}, y = (e, t) => {
|
|
92
125
|
switch (t.state) {
|
|
93
126
|
case p.Normal:
|
|
94
127
|
switch (e) {
|
|
@@ -113,7 +146,7 @@ const p = {
|
|
|
113
146
|
}
|
|
114
147
|
case p.Escape:
|
|
115
148
|
if (t.escapeLength === 1) {
|
|
116
|
-
if (
|
|
149
|
+
if (G.includes(e))
|
|
117
150
|
return t.state = p.Normal, t.value += e, t.escapeLength = 0, {
|
|
118
151
|
token: null,
|
|
119
152
|
current: t
|
|
@@ -125,7 +158,7 @@ const p = {
|
|
|
125
158
|
};
|
|
126
159
|
throw new Error("解析失败");
|
|
127
160
|
}
|
|
128
|
-
if (
|
|
161
|
+
if (Z(e))
|
|
129
162
|
return t.escapeLength === 6 ? (t.state = p.Normal, t.value += e, t.escapeLength = 0, {
|
|
130
163
|
token: null,
|
|
131
164
|
current: t
|
|
@@ -135,25 +168,25 @@ const p = {
|
|
|
135
168
|
});
|
|
136
169
|
throw new Error("解析失败");
|
|
137
170
|
}
|
|
138
|
-
},
|
|
171
|
+
}, m = (e, t) => {
|
|
139
172
|
switch (t.state) {
|
|
140
|
-
case
|
|
173
|
+
case a.Negative:
|
|
141
174
|
if (e === "0")
|
|
142
|
-
return t.state =
|
|
175
|
+
return t.state = a.Zero, t.value += e, t.validLength = t.value.length, {
|
|
143
176
|
token: null,
|
|
144
177
|
current: t
|
|
145
178
|
};
|
|
146
|
-
if (
|
|
147
|
-
return t.state =
|
|
179
|
+
if (h(e))
|
|
180
|
+
return t.state = a.Digit, t.value += e, t.validLength = t.value.length, {
|
|
148
181
|
token: null,
|
|
149
182
|
current: t
|
|
150
183
|
};
|
|
151
184
|
throw new Error("解析失败");
|
|
152
|
-
case
|
|
153
|
-
return e === "." ? (t.state =
|
|
185
|
+
case a.Zero:
|
|
186
|
+
return e === "." ? (t.state = a.Point, t.value += e, {
|
|
154
187
|
token: null,
|
|
155
188
|
current: t
|
|
156
|
-
}) :
|
|
189
|
+
}) : k(e) ? (t.state = a.Exp, t.value += e, {
|
|
157
190
|
token: null,
|
|
158
191
|
current: t
|
|
159
192
|
}) : {
|
|
@@ -164,14 +197,14 @@ const p = {
|
|
|
164
197
|
current: null,
|
|
165
198
|
char: e
|
|
166
199
|
};
|
|
167
|
-
case
|
|
168
|
-
return
|
|
200
|
+
case a.Digit:
|
|
201
|
+
return c(e) ? (t.value += e, t.validLength = t.value.length, {
|
|
169
202
|
token: null,
|
|
170
203
|
current: t
|
|
171
|
-
}) : e === "." ? (t.state =
|
|
204
|
+
}) : e === "." ? (t.state = a.Point, t.value += e, {
|
|
172
205
|
token: null,
|
|
173
206
|
current: t
|
|
174
|
-
}) :
|
|
207
|
+
}) : k(e) ? (t.state = a.Exp, t.value += e, {
|
|
175
208
|
token: null,
|
|
176
209
|
current: t
|
|
177
210
|
}) : {
|
|
@@ -182,18 +215,18 @@ const p = {
|
|
|
182
215
|
current: null,
|
|
183
216
|
char: e
|
|
184
217
|
};
|
|
185
|
-
case
|
|
186
|
-
if (
|
|
187
|
-
return t.state =
|
|
218
|
+
case a.Point:
|
|
219
|
+
if (c(e))
|
|
220
|
+
return t.state = a.DigitFraction, t.value += e, t.validLength = t.value.length, {
|
|
188
221
|
token: null,
|
|
189
222
|
current: t
|
|
190
223
|
};
|
|
191
224
|
throw new Error("解析失败");
|
|
192
|
-
case
|
|
193
|
-
return
|
|
225
|
+
case a.DigitFraction:
|
|
226
|
+
return c(e) ? (t.value += e, t.validLength = t.value.length, {
|
|
194
227
|
token: null,
|
|
195
228
|
current: t
|
|
196
|
-
}) :
|
|
229
|
+
}) : k(e) ? (t.state = a.Exp, t.value += e, {
|
|
197
230
|
token: null,
|
|
198
231
|
current: t
|
|
199
232
|
}) : {
|
|
@@ -204,20 +237,20 @@ const p = {
|
|
|
204
237
|
current: null,
|
|
205
238
|
char: e
|
|
206
239
|
};
|
|
207
|
-
case
|
|
208
|
-
if (
|
|
209
|
-
return t.state =
|
|
240
|
+
case a.Exp:
|
|
241
|
+
if (K.includes(e))
|
|
242
|
+
return t.state = a.ExpDigitOrSign, t.value += e, {
|
|
210
243
|
token: null,
|
|
211
244
|
current: t
|
|
212
245
|
};
|
|
213
|
-
if (
|
|
214
|
-
return t.state =
|
|
246
|
+
if (c(e))
|
|
247
|
+
return t.state = a.ExpDigitOrSign, t.value += e, t.validLength = t.value.length, {
|
|
215
248
|
token: null,
|
|
216
249
|
current: t
|
|
217
250
|
};
|
|
218
251
|
throw new Error("解析失败");
|
|
219
|
-
case
|
|
220
|
-
if (
|
|
252
|
+
case a.ExpDigitOrSign:
|
|
253
|
+
if (c(e))
|
|
221
254
|
return t.value += e, t.validLength = t.value.length, {
|
|
222
255
|
token: null,
|
|
223
256
|
current: t
|
|
@@ -233,23 +266,23 @@ const p = {
|
|
|
233
266
|
};
|
|
234
267
|
throw new Error("解析失败");
|
|
235
268
|
}
|
|
236
|
-
},
|
|
269
|
+
}, b = (e) => {
|
|
237
270
|
if (e.validLength === e.value.length)
|
|
238
271
|
return {
|
|
239
272
|
type: "number",
|
|
240
273
|
value: JSON.parse(e.value)
|
|
241
274
|
};
|
|
242
275
|
throw new Error("解析失败");
|
|
243
|
-
},
|
|
276
|
+
}, F = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
244
277
|
__proto__: null,
|
|
245
|
-
NumberTokenState:
|
|
278
|
+
NumberTokenState: a,
|
|
246
279
|
StringTokenState: p,
|
|
247
|
-
endNumberTokenCurrent:
|
|
280
|
+
endNumberTokenCurrent: b,
|
|
248
281
|
parseNextToken: o,
|
|
249
282
|
parseNextTokenWithKeyword: E,
|
|
250
|
-
parseNextTokenWithNumber:
|
|
251
|
-
parseNextTokenWithString:
|
|
252
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
283
|
+
parseNextTokenWithNumber: m,
|
|
284
|
+
parseNextTokenWithString: y
|
|
285
|
+
}, Symbol.toStringTag, { value: "Module" })), l = {
|
|
253
286
|
Start: 1,
|
|
254
287
|
Value: 2,
|
|
255
288
|
Comma: 3,
|
|
@@ -271,7 +304,7 @@ const p = {
|
|
|
271
304
|
throw new Error("解析失败");
|
|
272
305
|
return e;
|
|
273
306
|
case "array":
|
|
274
|
-
if (e.state ===
|
|
307
|
+
if (e.state === l.Value)
|
|
275
308
|
return t ? f(e, t) : e;
|
|
276
309
|
throw new Error("解析失败");
|
|
277
310
|
case "property":
|
|
@@ -295,7 +328,7 @@ const p = {
|
|
|
295
328
|
case "[":
|
|
296
329
|
return e.value = {
|
|
297
330
|
type: "array",
|
|
298
|
-
state:
|
|
331
|
+
state: l.Start,
|
|
299
332
|
children: [],
|
|
300
333
|
parent: e
|
|
301
334
|
}, e.value;
|
|
@@ -309,7 +342,7 @@ const p = {
|
|
|
309
342
|
}
|
|
310
343
|
}
|
|
311
344
|
throw new Error("解析失败");
|
|
312
|
-
},
|
|
345
|
+
}, N = (e, t) => {
|
|
313
346
|
if (e.current)
|
|
314
347
|
switch (e.current.type) {
|
|
315
348
|
case "keyword": {
|
|
@@ -317,21 +350,21 @@ const p = {
|
|
|
317
350
|
return r.token ? (e.value = r.token.value, e.current = null, i(e.parent)) : (e.current = r.current, e);
|
|
318
351
|
}
|
|
319
352
|
case "string": {
|
|
320
|
-
const r =
|
|
353
|
+
const r = y(t, e.current);
|
|
321
354
|
return r.token ? (e.value = r.token.value, e.current = null, i(e.parent)) : (e.current = r.current, e);
|
|
322
355
|
}
|
|
323
356
|
case "number": {
|
|
324
|
-
const r =
|
|
357
|
+
const r = m(t, e.current);
|
|
325
358
|
return r.token ? (e.value = r.token.value, e.current = null, i(e.parent, r.char)) : (e.current = r.current, e);
|
|
326
359
|
}
|
|
327
360
|
}
|
|
328
361
|
throw new Error("解析失败");
|
|
329
362
|
}, f = (e, t) => {
|
|
330
363
|
switch (e.state) {
|
|
331
|
-
case
|
|
364
|
+
case l.Start: {
|
|
332
365
|
const r = o(t);
|
|
333
366
|
if (r.current) {
|
|
334
|
-
e.state =
|
|
367
|
+
e.state = l.Value;
|
|
335
368
|
const n = {
|
|
336
369
|
type: "literal",
|
|
337
370
|
value: null,
|
|
@@ -344,19 +377,19 @@ const p = {
|
|
|
344
377
|
return e;
|
|
345
378
|
switch (r.token.value) {
|
|
346
379
|
case "]":
|
|
347
|
-
return e.state =
|
|
380
|
+
return e.state = l.End, i(e.parent);
|
|
348
381
|
case "[": {
|
|
349
|
-
e.state =
|
|
382
|
+
e.state = l.Value;
|
|
350
383
|
const n = {
|
|
351
384
|
type: "array",
|
|
352
|
-
state:
|
|
385
|
+
state: l.Start,
|
|
353
386
|
children: [],
|
|
354
387
|
parent: e
|
|
355
388
|
};
|
|
356
389
|
return e.children.push(n), n;
|
|
357
390
|
}
|
|
358
391
|
case "{": {
|
|
359
|
-
e.state =
|
|
392
|
+
e.state = l.Start;
|
|
360
393
|
const n = {
|
|
361
394
|
type: "object",
|
|
362
395
|
state: s.Start,
|
|
@@ -368,10 +401,10 @@ const p = {
|
|
|
368
401
|
}
|
|
369
402
|
throw new Error("解析失败");
|
|
370
403
|
}
|
|
371
|
-
case
|
|
404
|
+
case l.Comma: {
|
|
372
405
|
const r = o(t);
|
|
373
406
|
if (r.current) {
|
|
374
|
-
e.state =
|
|
407
|
+
e.state = l.Value;
|
|
375
408
|
const n = {
|
|
376
409
|
type: "literal",
|
|
377
410
|
value: null,
|
|
@@ -384,17 +417,17 @@ const p = {
|
|
|
384
417
|
return e;
|
|
385
418
|
switch (r.token.value) {
|
|
386
419
|
case "[": {
|
|
387
|
-
e.state =
|
|
420
|
+
e.state = l.Value;
|
|
388
421
|
const n = {
|
|
389
422
|
type: "array",
|
|
390
|
-
state:
|
|
423
|
+
state: l.Start,
|
|
391
424
|
children: [],
|
|
392
425
|
parent: e
|
|
393
426
|
};
|
|
394
427
|
return e.children.push(n), n;
|
|
395
428
|
}
|
|
396
429
|
case "{": {
|
|
397
|
-
e.state =
|
|
430
|
+
e.state = l.Start;
|
|
398
431
|
const n = {
|
|
399
432
|
type: "object",
|
|
400
433
|
state: s.Start,
|
|
@@ -406,7 +439,7 @@ const p = {
|
|
|
406
439
|
}
|
|
407
440
|
throw new Error("解析失败");
|
|
408
441
|
}
|
|
409
|
-
case
|
|
442
|
+
case l.Value: {
|
|
410
443
|
const r = o(t);
|
|
411
444
|
if (r.current)
|
|
412
445
|
throw new Error("解析失败");
|
|
@@ -414,22 +447,22 @@ const p = {
|
|
|
414
447
|
return e;
|
|
415
448
|
switch (r.token.value) {
|
|
416
449
|
case ",":
|
|
417
|
-
return e.state =
|
|
450
|
+
return e.state = l.Comma, e;
|
|
418
451
|
case "]":
|
|
419
|
-
return e.state =
|
|
452
|
+
return e.state = l.End, i(e.parent);
|
|
420
453
|
}
|
|
421
454
|
throw new Error("解析失败");
|
|
422
455
|
}
|
|
423
|
-
case
|
|
456
|
+
case l.End:
|
|
424
457
|
throw new Error("解析失败");
|
|
425
458
|
}
|
|
426
459
|
}, x = (e, t) => {
|
|
427
460
|
if (e.current) {
|
|
428
|
-
const r =
|
|
461
|
+
const r = y(t, e.current);
|
|
429
462
|
return r.current ? (e.current = r.current, e) : (e.value = r.token.value, e.current = null, e.parent.state = u.Colon, e.parent);
|
|
430
463
|
}
|
|
431
464
|
throw new Error("解析失败");
|
|
432
|
-
},
|
|
465
|
+
}, L = (e, t) => {
|
|
433
466
|
switch (e.state) {
|
|
434
467
|
case u.Colon: {
|
|
435
468
|
const r = o(t);
|
|
@@ -458,7 +491,7 @@ const p = {
|
|
|
458
491
|
case "[": {
|
|
459
492
|
const n = {
|
|
460
493
|
type: "array",
|
|
461
|
-
state:
|
|
494
|
+
state: l.Start,
|
|
462
495
|
children: [],
|
|
463
496
|
parent: e
|
|
464
497
|
};
|
|
@@ -480,7 +513,7 @@ const p = {
|
|
|
480
513
|
case u.End:
|
|
481
514
|
throw new Error("解析失败");
|
|
482
515
|
}
|
|
483
|
-
},
|
|
516
|
+
}, T = (e, t) => {
|
|
484
517
|
switch (e.state) {
|
|
485
518
|
case s.Start: {
|
|
486
519
|
const r = o(t);
|
|
@@ -493,13 +526,13 @@ const p = {
|
|
|
493
526
|
key: null,
|
|
494
527
|
value: null,
|
|
495
528
|
parent: e
|
|
496
|
-
},
|
|
529
|
+
}, v = {
|
|
497
530
|
type: "identifier",
|
|
498
531
|
value: "",
|
|
499
532
|
current: r.current,
|
|
500
533
|
parent: n
|
|
501
534
|
};
|
|
502
|
-
return n.key =
|
|
535
|
+
return n.key = v, e.children.push(n), v;
|
|
503
536
|
}
|
|
504
537
|
throw new Error("解析失败");
|
|
505
538
|
}
|
|
@@ -520,13 +553,13 @@ const p = {
|
|
|
520
553
|
key: null,
|
|
521
554
|
value: null,
|
|
522
555
|
parent: e
|
|
523
|
-
},
|
|
556
|
+
}, v = {
|
|
524
557
|
type: "identifier",
|
|
525
558
|
value: "",
|
|
526
559
|
current: r.current,
|
|
527
560
|
parent: n
|
|
528
561
|
};
|
|
529
|
-
return n.key =
|
|
562
|
+
return n.key = v, e.children.push(n), v;
|
|
530
563
|
}
|
|
531
564
|
throw new Error("解析失败");
|
|
532
565
|
}
|
|
@@ -551,13 +584,13 @@ const p = {
|
|
|
551
584
|
case s.End:
|
|
552
585
|
throw new Error("解析失败");
|
|
553
586
|
}
|
|
554
|
-
},
|
|
587
|
+
}, z = (e) => {
|
|
555
588
|
switch (e.type) {
|
|
556
589
|
case "root":
|
|
557
590
|
return e;
|
|
558
591
|
case "literal":
|
|
559
592
|
if (e.current && e.current.type === "number") {
|
|
560
|
-
const t =
|
|
593
|
+
const t = b(e.current);
|
|
561
594
|
return e.value = t.value, e.current = null, i(e.parent);
|
|
562
595
|
}
|
|
563
596
|
throw new Error("解析失败");
|
|
@@ -567,19 +600,19 @@ const p = {
|
|
|
567
600
|
case "identifier":
|
|
568
601
|
throw new Error("解析失败");
|
|
569
602
|
}
|
|
570
|
-
},
|
|
603
|
+
}, M = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
571
604
|
__proto__: null,
|
|
572
|
-
ArrayNodeState:
|
|
605
|
+
ArrayNodeState: l,
|
|
573
606
|
ObjectNodeState: s,
|
|
574
607
|
PropertyNodeState: u,
|
|
575
|
-
endAst:
|
|
608
|
+
endAst: z,
|
|
576
609
|
parseArrayNode: f,
|
|
577
610
|
parseIdentifierNode: x,
|
|
578
|
-
parseLiteralNode:
|
|
579
|
-
parseObjectNode:
|
|
580
|
-
parsePropertyNode:
|
|
611
|
+
parseLiteralNode: N,
|
|
612
|
+
parseObjectNode: T,
|
|
613
|
+
parsePropertyNode: L,
|
|
581
614
|
parseRootNode: S
|
|
582
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
615
|
+
}, Symbol.toStringTag, { value: "Module" })), R = () => {
|
|
583
616
|
const e = {
|
|
584
617
|
type: "root",
|
|
585
618
|
value: null
|
|
@@ -594,7 +627,7 @@ const p = {
|
|
|
594
627
|
t = S(t, r);
|
|
595
628
|
break;
|
|
596
629
|
case "literal":
|
|
597
|
-
t =
|
|
630
|
+
t = N(t, r);
|
|
598
631
|
break;
|
|
599
632
|
case "array":
|
|
600
633
|
t = f(t, r);
|
|
@@ -603,17 +636,18 @@ const p = {
|
|
|
603
636
|
t = x(t, r);
|
|
604
637
|
break;
|
|
605
638
|
case "property":
|
|
606
|
-
t =
|
|
639
|
+
t = L(t, r);
|
|
607
640
|
break;
|
|
608
641
|
case "object":
|
|
609
|
-
t =
|
|
642
|
+
t = T(t, r);
|
|
610
643
|
break;
|
|
611
644
|
}
|
|
612
645
|
}
|
|
613
646
|
};
|
|
614
647
|
};
|
|
615
648
|
export {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
649
|
+
R as createJsonBrook,
|
|
650
|
+
A as generate,
|
|
651
|
+
M as parse,
|
|
652
|
+
F as tokenize
|
|
619
653
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`,"\r"],
|
|
1
|
+
(function(i,k){typeof exports=="object"&&typeof module<"u"?k(exports):typeof define=="function"&&define.amd?define(["exports"],k):(i=typeof globalThis<"u"?globalThis:i||self,k(i.JsonBrook={}))})(this,(function(i){"use strict";const k=e=>{if(e.current)switch(e.current.type){case"keyword":return e.current.value;case"string":return JSON.parse(e.current.escapeLength>0?e.current.value.slice(0,-e.current.escapeLength):e.current.value);case"number":return JSON.parse(e.current.value.slice(0,e.current.validLength))}return e.value},P=e=>e.children.map(t=>w(t)),D=e=>e.children.reduce((t,r)=>(r.value&&(t[r.key.value]=w(r.value)),t),{}),w=e=>{switch(e.type){case"literal":return k(e);case"array":return P(e);case"object":return D(e)}},_=e=>{if(e.value)return w(e.value)},C=Object.freeze(Object.defineProperty({__proto__:null,simpleGenerator:e=>_(e.getRoot())},Symbol.toStringTag,{value:"Module"})),c={Normal:1,Escape:2},a={Negative:1,Zero:2,Digit:3,Point:4,DigitFraction:5,Exp:6,ExpDigitOrSign:7},J=[" "," ",`
|
|
2
|
+
`,"\r"],V=["{","}","[","]",":",","],E=["true","false","null"],G=E.map(e=>e[0]),z=['"',"\\","/","b","f","n","r","t"],A=["+","-"],v=e=>e>="0"&&e<="9",m=e=>e>="1"&&e<="9",K=e=>v(e)||e>="a"&&e<="f"||e>="A"&&e<="F",y=e=>e==="e"||e==="E",o=e=>{if(J.includes(e))return{token:null,current:null};if(V.includes(e))return{token:{type:"symbol",value:e},current:null};const t=G.indexOf(e);if(t!==-1)return{token:null,current:{type:"keyword",value:E[t],matchLength:1}};if(e==='"')return{token:null,current:{type:"string",state:c.Normal,value:e,escapeLength:0}};if(e==="-")return{token:null,current:{type:"number",state:a.Negative,value:e,validLength:0}};if(e==="0")return{token:null,current:{type:"number",state:a.Zero,value:e,validLength:1}};if(m(e))return{token:null,current:{type:"number",state:a.Digit,value:e,validLength:1}};throw new Error("解析失败")},b=(e,t)=>{if(e===t.value[t.matchLength])return t.matchLength++,t.matchLength===t.value.length?{token:{type:"keyword",value:JSON.parse(t.value)},current:null}:{token:null,current:t};throw new Error("解析失败")},g=(e,t)=>{switch(t.state){case c.Normal:switch(e){case'"':return t.value+=e,{token:{type:"string",value:t.value},current:null};case"\\":return t.state=c.Escape,t.value+=e,t.escapeLength=1,{token:null,current:t};default:return t.value+=e,{token:null,current:t}}case c.Escape:if(t.escapeLength===1){if(z.includes(e))return t.state=c.Normal,t.value+=e,t.escapeLength=0,{token:null,current:t};if(e==="u")return t.value+=e,t.escapeLength++,{token:null,current:t};throw new Error("解析失败")}if(K(e))return t.escapeLength===6?(t.state=c.Normal,t.value+=e,t.escapeLength=0,{token:null,current:t}):(t.value+=e,t.escapeLength++,{token:null,current:t});throw new Error("解析失败")}},S=(e,t)=>{switch(t.state){case a.Negative:if(e==="0")return t.state=a.Zero,t.value+=e,t.validLength=t.value.length,{token:null,current:t};if(m(e))return t.state=a.Digit,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case a.Zero:return e==="."?(t.state=a.Point,t.value+=e,{token:null,current:t}):y(e)?(t.state=a.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case a.Digit:return v(e)?(t.value+=e,t.validLength=t.value.length,{token:null,current:t}):e==="."?(t.state=a.Point,t.value+=e,{token:null,current:t}):y(e)?(t.state=a.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case a.Point:if(v(e))return t.state=a.DigitFraction,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case a.DigitFraction:return v(e)?(t.value+=e,t.validLength=t.value.length,{token:null,current:t}):y(e)?(t.state=a.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case a.Exp:if(A.includes(e))return t.state=a.ExpDigitOrSign,t.value+=e,{token:null,current:t};if(v(e))return t.state=a.ExpDigitOrSign,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case a.ExpDigitOrSign:if(v(e))return t.value+=e,t.validLength=t.value.length,{token:null,current:t};if(t.validLength===t.value.length)return{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};throw new Error("解析失败")}},N=e=>{if(e.validLength===e.value.length)return{type:"number",value:JSON.parse(e.value)};throw new Error("解析失败")},Z=Object.freeze(Object.defineProperty({__proto__:null,NumberTokenState:a,StringTokenState:c,endNumberTokenCurrent:N,parseNextToken:o,parseNextTokenWithKeyword:b,parseNextTokenWithNumber:S,parseNextTokenWithString:g},Symbol.toStringTag,{value:"Module"})),l={Start:1,Value:2,Comma:3,End:4},u={Key:1,Colon:2,Value:3,End:4},s={Start:1,Property:2,Comma:3,End:4},p=(e,t)=>{switch(e.type){case"root":if(t)throw new Error("解析失败");return e;case"array":if(e.state===l.Value)return t?h(e,t):e;throw new Error("解析失败");case"property":if(e.state===u.Value)return e.state=u.End,e.parent;throw new Error("解析失败")}},x=(e,t)=>{if(!e.value){const r=o(t);if(r.current)return e.value={type:"literal",value:null,current:r.current,parent:e},e.value;if(!r.token)return e;switch(r.token.value){case"[":return e.value={type:"array",state:l.Start,children:[],parent:e},e.value;case"{":return e.value={type:"object",state:s.Start,children:[],parent:e},e.value}}throw new Error("解析失败")},L=(e,t)=>{if(e.current)switch(e.current.type){case"keyword":{const r=b(t,e.current);return r.token?(e.value=r.token.value,e.current=null,p(e.parent)):(e.current=r.current,e)}case"string":{const r=g(t,e.current);return r.token?(e.value=r.token.value,e.current=null,p(e.parent)):(e.current=r.current,e)}case"number":{const r=S(t,e.current);return r.token?(e.value=r.token.value,e.current=null,p(e.parent,r.char)):(e.current=r.current,e)}}throw new Error("解析失败")},h=(e,t)=>{switch(e.state){case l.Start:{const r=o(t);if(r.current){e.state=l.Value;const n={type:"literal",value:null,current:r.current,parent:e};return e.children.push(n),n}if(!r.token)return e;switch(r.token.value){case"]":return e.state=l.End,p(e.parent);case"[":{e.state=l.Value;const n={type:"array",state:l.Start,children:[],parent:e};return e.children.push(n),n}case"{":{e.state=l.Start;const n={type:"object",state:s.Start,children:[],parent:e};return e.children.push(n),n}}throw new Error("解析失败")}case l.Comma:{const r=o(t);if(r.current){e.state=l.Value;const n={type:"literal",value:null,current:r.current,parent:e};return e.children.push(n),n}if(!r.token)return e;switch(r.token.value){case"[":{e.state=l.Value;const n={type:"array",state:l.Start,children:[],parent:e};return e.children.push(n),n}case"{":{e.state=l.Start;const n={type:"object",state:s.Start,children:[],parent:e};return e.children.push(n),n}}throw new Error("解析失败")}case l.Value:{const r=o(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;switch(r.token.value){case",":return e.state=l.Comma,e;case"]":return e.state=l.End,p(e.parent)}throw new Error("解析失败")}case l.End:throw new Error("解析失败")}},T=(e,t)=>{if(e.current){const r=g(t,e.current);return r.current?(e.current=r.current,e):(e.value=r.token.value,e.current=null,e.parent.state=u.Colon,e.parent)}throw new Error("解析失败")},O=(e,t)=>{switch(e.state){case u.Colon:{const r=o(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;if(r.token.value===":")return e.state=u.Value,e;throw new Error("解析失败")}case u.Value:{const r=o(t);if(r.current){const n={type:"literal",value:null,current:r.current,parent:e};return e.value=n,n}if(!r.token)return e;switch(r.token.value){case"[":{const n={type:"array",state:l.Start,children:[],parent:e};return e.value=n,n}case"{":{const n={type:"object",state:s.Start,children:[],parent:e};return e.value=n,n}}throw new Error("解析失败")}case u.Key:case u.End:throw new Error("解析失败")}},j=(e,t)=>{switch(e.state){case s.Start:{const r=o(t);if(r.current){if(r.current.type==="string"){e.state=s.Property;const n={type:"property",state:u.Key,key:null,value:null,parent:e},f={type:"identifier",value:"",current:r.current,parent:n};return n.key=f,e.children.push(n),f}throw new Error("解析失败")}if(!r.token)return e;if(r.token.value==="}")return e.state=s.End,p(e.parent);throw new Error("解析失败")}case s.Comma:{const r=o(t);if(r.current){if(r.current.type==="string"){e.state=s.Property;const n={type:"property",state:u.Key,key:null,value:null,parent:e},f={type:"identifier",value:"",current:r.current,parent:n};return n.key=f,e.children.push(n),f}throw new Error("解析失败")}if(!r.token)return e;throw new Error("解析失败")}case s.Property:{const r=o(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;switch(r.token.value){case",":return e.state=s.Comma,e;case"}":return e.state=s.End,p(e.parent)}throw new Error("解析失败")}case s.End:throw new Error("解析失败")}},d=Object.freeze(Object.defineProperty({__proto__:null,ArrayNodeState:l,ObjectNodeState:s,PropertyNodeState:u,endAst:e=>{switch(e.type){case"root":return e;case"literal":if(e.current&&e.current.type==="number"){const t=N(e.current);return e.value=t.value,e.current=null,p(e.parent)}throw new Error("解析失败");case"array":case"object":case"property":case"identifier":throw new Error("解析失败")}},parseArrayNode:h,parseIdentifierNode:T,parseLiteralNode:L,parseObjectNode:j,parsePropertyNode:O,parseRootNode:x},Symbol.toStringTag,{value:"Module"})),F=()=>{const e={type:"root",value:null};let t=e;return{getRoot:()=>e,getCurrent:()=>t,parse:r=>{switch(t.type){case"root":t=x(t,r);break;case"literal":t=L(t,r);break;case"array":t=h(t,r);break;case"identifier":t=T(t,r);break;case"property":t=O(t,r);break;case"object":t=j(t,r);break}}}};i.createJsonBrook=F,i.generate=C,i.parse=d,i.tokenize=Z,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,55 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
2
|
+
"name": "json-brook",
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"description": "parse json data streamly",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"json",
|
|
7
|
+
"stream"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/asurance/json-brook",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/asurance/json-brook/issues"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "asurance",
|
|
15
|
+
"sideEffects": [],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"CHANGELOG.md"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.umd.cjs",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"require": "./dist/index.umd.cjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"dev": "vite --config vite.doc.config.ts",
|
|
32
|
+
"build": "tsc && vite build",
|
|
33
|
+
"build:doc": "tsc && vite build --config vite.doc.config.ts",
|
|
34
|
+
"preview": "vite preview",
|
|
35
|
+
"lint": "biome check --fix"
|
|
36
|
+
},
|
|
37
|
+
"simple-git-hooks": {
|
|
38
|
+
"pre-commit": "npx lint-staged"
|
|
39
|
+
},
|
|
40
|
+
"lint-staged": {
|
|
41
|
+
"*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}": [
|
|
42
|
+
"pnpm lint"
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@biomejs/biome": "2.4.4",
|
|
47
|
+
"@tailwindcss/vite": "^4.1.13",
|
|
48
|
+
"@types/node": "^25.3.0",
|
|
49
|
+
"lint-staged": "~16.2.7",
|
|
50
|
+
"shiki": "^3.22.0",
|
|
51
|
+
"simple-git-hooks": "^2.13.1",
|
|
52
|
+
"solid-devtools": "^0.34.3",
|
|
53
|
+
"solid-js": "^1.9.11",
|
|
54
|
+
"tailwindcss": "^4.1.13",
|
|
55
|
+
"typescript": "~5.9.3",
|
|
56
|
+
"vite": "^7.3.1",
|
|
57
|
+
"vite-plugin-dts": "^4.5.4",
|
|
58
|
+
"vite-plugin-solid": "^2.11.8"
|
|
59
|
+
},
|
|
60
|
+
"packageManager": "pnpm@9.1.2"
|
|
61
|
+
}
|