json-brook 0.0.4 → 0.1.0-alpha.1
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/dist/generate.d.ts +2 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +619 -0
- package/dist/index.umd.cjs +2 -0
- package/dist/parse.d.ts +65 -0
- package/dist/tokenize.d.ts +92 -0
- package/package.json +60 -65
- package/dist/es/index.js +0 -623
- package/dist/lib/index.js +0 -652
- package/dist/types/index.d.ts +0 -114
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as parse from "./parse";
|
|
2
|
+
import * as tokenize from "./tokenize";
|
|
3
|
+
export type * from './parse';
|
|
4
|
+
export type * from './tokenize';
|
|
5
|
+
export { parse, tokenize };
|
|
6
|
+
export type JsonBrook = {
|
|
7
|
+
getRoot: () => parse.RootNode;
|
|
8
|
+
getCurrent: () => parse.Node;
|
|
9
|
+
parse: (char: string) => void;
|
|
10
|
+
};
|
|
11
|
+
export declare const createJsonBrook: () => {
|
|
12
|
+
getRoot: () => parse.RootNode;
|
|
13
|
+
getCurrent: () => parse.Node;
|
|
14
|
+
parse: (char: string) => void;
|
|
15
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
const p = {
|
|
2
|
+
Normal: 1,
|
|
3
|
+
Escape: 2
|
|
4
|
+
}, l = {
|
|
5
|
+
Negative: 1,
|
|
6
|
+
Zero: 2,
|
|
7
|
+
Digit: 3,
|
|
8
|
+
Point: 4,
|
|
9
|
+
DigitFraction: 5,
|
|
10
|
+
Exp: 6,
|
|
11
|
+
ExpDigitOrSign: 7
|
|
12
|
+
}, T = [" ", " ", `
|
|
13
|
+
`, "\r"], O = ["{", "}", "[", "]", ":", ","], y = ["true", "false", "null"], D = y.map((e) => e[0]), P = ['"', "\\", "/", "b", "f", "n", "r", "t"], j = ["+", "-"], v = (e) => e >= "0" && e <= "9", g = (e) => e >= "1" && e <= "9", C = (e) => v(e) || e >= "a" && e <= "f" || e >= "A" && e <= "F", c = (e) => e === "e" || e === "E", o = (e) => {
|
|
14
|
+
if (T.includes(e))
|
|
15
|
+
return {
|
|
16
|
+
token: null,
|
|
17
|
+
current: null
|
|
18
|
+
};
|
|
19
|
+
if (O.includes(e))
|
|
20
|
+
return {
|
|
21
|
+
token: {
|
|
22
|
+
type: "symbol",
|
|
23
|
+
value: e
|
|
24
|
+
},
|
|
25
|
+
current: null
|
|
26
|
+
};
|
|
27
|
+
const t = D.indexOf(e);
|
|
28
|
+
if (t !== -1)
|
|
29
|
+
return {
|
|
30
|
+
token: null,
|
|
31
|
+
current: {
|
|
32
|
+
type: "keyword",
|
|
33
|
+
value: y[t],
|
|
34
|
+
matchLength: 1
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
if (e === '"')
|
|
38
|
+
return {
|
|
39
|
+
token: null,
|
|
40
|
+
current: {
|
|
41
|
+
type: "string",
|
|
42
|
+
state: p.Normal,
|
|
43
|
+
value: e,
|
|
44
|
+
escapeLength: 0
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
if (e === "-")
|
|
48
|
+
return {
|
|
49
|
+
token: null,
|
|
50
|
+
current: {
|
|
51
|
+
type: "number",
|
|
52
|
+
state: l.Negative,
|
|
53
|
+
value: e,
|
|
54
|
+
validLength: 0
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
if (e === "0")
|
|
58
|
+
return {
|
|
59
|
+
token: null,
|
|
60
|
+
current: {
|
|
61
|
+
type: "number",
|
|
62
|
+
state: l.Zero,
|
|
63
|
+
value: e,
|
|
64
|
+
validLength: 1
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
if (g(e))
|
|
68
|
+
return {
|
|
69
|
+
token: null,
|
|
70
|
+
current: {
|
|
71
|
+
type: "number",
|
|
72
|
+
state: l.Digit,
|
|
73
|
+
value: e,
|
|
74
|
+
validLength: 1
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
throw new Error("解析失败");
|
|
78
|
+
}, E = (e, t) => {
|
|
79
|
+
if (e === t.value[t.matchLength])
|
|
80
|
+
return t.matchLength++, t.matchLength === t.value.length ? {
|
|
81
|
+
token: {
|
|
82
|
+
type: "keyword",
|
|
83
|
+
value: JSON.parse(t.value)
|
|
84
|
+
},
|
|
85
|
+
current: null
|
|
86
|
+
} : {
|
|
87
|
+
token: null,
|
|
88
|
+
current: t
|
|
89
|
+
};
|
|
90
|
+
throw new Error("解析失败");
|
|
91
|
+
}, w = (e, t) => {
|
|
92
|
+
switch (t.state) {
|
|
93
|
+
case p.Normal:
|
|
94
|
+
switch (e) {
|
|
95
|
+
case '"':
|
|
96
|
+
return t.value += e, {
|
|
97
|
+
token: {
|
|
98
|
+
type: "string",
|
|
99
|
+
value: t.value
|
|
100
|
+
},
|
|
101
|
+
current: null
|
|
102
|
+
};
|
|
103
|
+
case "\\":
|
|
104
|
+
return t.state = p.Escape, t.value += e, t.escapeLength = 1, {
|
|
105
|
+
token: null,
|
|
106
|
+
current: t
|
|
107
|
+
};
|
|
108
|
+
default:
|
|
109
|
+
return t.value += e, {
|
|
110
|
+
token: null,
|
|
111
|
+
current: t
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
case p.Escape:
|
|
115
|
+
if (t.escapeLength === 1) {
|
|
116
|
+
if (P.includes(e))
|
|
117
|
+
return t.state = p.Normal, t.value += e, t.escapeLength = 0, {
|
|
118
|
+
token: null,
|
|
119
|
+
current: t
|
|
120
|
+
};
|
|
121
|
+
if (e === "u")
|
|
122
|
+
return t.value += e, t.escapeLength++, {
|
|
123
|
+
token: null,
|
|
124
|
+
current: t
|
|
125
|
+
};
|
|
126
|
+
throw new Error("解析失败");
|
|
127
|
+
}
|
|
128
|
+
if (C(e))
|
|
129
|
+
return t.escapeLength === 6 ? (t.state = p.Normal, t.value += e, t.escapeLength = 0, {
|
|
130
|
+
token: null,
|
|
131
|
+
current: t
|
|
132
|
+
}) : (t.value += e, t.escapeLength++, {
|
|
133
|
+
token: null,
|
|
134
|
+
current: t
|
|
135
|
+
});
|
|
136
|
+
throw new Error("解析失败");
|
|
137
|
+
}
|
|
138
|
+
}, h = (e, t) => {
|
|
139
|
+
switch (t.state) {
|
|
140
|
+
case l.Negative:
|
|
141
|
+
if (e === "0")
|
|
142
|
+
return t.state = l.Zero, t.value += e, t.validLength = t.value.length, {
|
|
143
|
+
token: null,
|
|
144
|
+
current: t
|
|
145
|
+
};
|
|
146
|
+
if (g(e))
|
|
147
|
+
return t.state = l.Digit, t.value += e, t.validLength = t.value.length, {
|
|
148
|
+
token: null,
|
|
149
|
+
current: t
|
|
150
|
+
};
|
|
151
|
+
throw new Error("解析失败");
|
|
152
|
+
case l.Zero:
|
|
153
|
+
return e === "." ? (t.state = l.Point, t.value += e, {
|
|
154
|
+
token: null,
|
|
155
|
+
current: t
|
|
156
|
+
}) : c(e) ? (t.state = l.Exp, t.value += e, {
|
|
157
|
+
token: null,
|
|
158
|
+
current: t
|
|
159
|
+
}) : {
|
|
160
|
+
token: {
|
|
161
|
+
type: "number",
|
|
162
|
+
value: JSON.parse(t.value)
|
|
163
|
+
},
|
|
164
|
+
current: null,
|
|
165
|
+
char: e
|
|
166
|
+
};
|
|
167
|
+
case l.Digit:
|
|
168
|
+
return v(e) ? (t.value += e, t.validLength = t.value.length, {
|
|
169
|
+
token: null,
|
|
170
|
+
current: t
|
|
171
|
+
}) : e === "." ? (t.state = l.Point, t.value += e, {
|
|
172
|
+
token: null,
|
|
173
|
+
current: t
|
|
174
|
+
}) : c(e) ? (t.state = l.Exp, t.value += e, {
|
|
175
|
+
token: null,
|
|
176
|
+
current: t
|
|
177
|
+
}) : {
|
|
178
|
+
token: {
|
|
179
|
+
type: "number",
|
|
180
|
+
value: JSON.parse(t.value)
|
|
181
|
+
},
|
|
182
|
+
current: null,
|
|
183
|
+
char: e
|
|
184
|
+
};
|
|
185
|
+
case l.Point:
|
|
186
|
+
if (v(e))
|
|
187
|
+
return t.state = l.DigitFraction, t.value += e, t.validLength = t.value.length, {
|
|
188
|
+
token: null,
|
|
189
|
+
current: t
|
|
190
|
+
};
|
|
191
|
+
throw new Error("解析失败");
|
|
192
|
+
case l.DigitFraction:
|
|
193
|
+
return v(e) ? (t.value += e, t.validLength = t.value.length, {
|
|
194
|
+
token: null,
|
|
195
|
+
current: t
|
|
196
|
+
}) : c(e) ? (t.state = l.Exp, t.value += e, {
|
|
197
|
+
token: null,
|
|
198
|
+
current: t
|
|
199
|
+
}) : {
|
|
200
|
+
token: {
|
|
201
|
+
type: "number",
|
|
202
|
+
value: JSON.parse(t.value)
|
|
203
|
+
},
|
|
204
|
+
current: null,
|
|
205
|
+
char: e
|
|
206
|
+
};
|
|
207
|
+
case l.Exp:
|
|
208
|
+
if (j.includes(e))
|
|
209
|
+
return t.state = l.ExpDigitOrSign, t.value += e, {
|
|
210
|
+
token: null,
|
|
211
|
+
current: t
|
|
212
|
+
};
|
|
213
|
+
if (v(e))
|
|
214
|
+
return t.state = l.ExpDigitOrSign, t.value += e, t.validLength = t.value.length, {
|
|
215
|
+
token: null,
|
|
216
|
+
current: t
|
|
217
|
+
};
|
|
218
|
+
throw new Error("解析失败");
|
|
219
|
+
case l.ExpDigitOrSign:
|
|
220
|
+
if (v(e))
|
|
221
|
+
return t.value += e, t.validLength = t.value.length, {
|
|
222
|
+
token: null,
|
|
223
|
+
current: t
|
|
224
|
+
};
|
|
225
|
+
if (t.validLength === t.value.length)
|
|
226
|
+
return {
|
|
227
|
+
token: {
|
|
228
|
+
type: "number",
|
|
229
|
+
value: JSON.parse(t.value)
|
|
230
|
+
},
|
|
231
|
+
current: null,
|
|
232
|
+
char: e
|
|
233
|
+
};
|
|
234
|
+
throw new Error("解析失败");
|
|
235
|
+
}
|
|
236
|
+
}, m = (e) => {
|
|
237
|
+
if (e.validLength === e.value.length)
|
|
238
|
+
return {
|
|
239
|
+
type: "number",
|
|
240
|
+
value: JSON.parse(e.value)
|
|
241
|
+
};
|
|
242
|
+
throw new Error("解析失败");
|
|
243
|
+
}, _ = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
244
|
+
__proto__: null,
|
|
245
|
+
NumberTokenState: l,
|
|
246
|
+
StringTokenState: p,
|
|
247
|
+
endNumberTokenCurrent: m,
|
|
248
|
+
parseNextToken: o,
|
|
249
|
+
parseNextTokenWithKeyword: E,
|
|
250
|
+
parseNextTokenWithNumber: h,
|
|
251
|
+
parseNextTokenWithString: w
|
|
252
|
+
}, Symbol.toStringTag, { value: "Module" })), a = {
|
|
253
|
+
Start: 1,
|
|
254
|
+
Value: 2,
|
|
255
|
+
Comma: 3,
|
|
256
|
+
End: 4
|
|
257
|
+
}, u = {
|
|
258
|
+
Key: 1,
|
|
259
|
+
Colon: 2,
|
|
260
|
+
Value: 3,
|
|
261
|
+
End: 4
|
|
262
|
+
}, s = {
|
|
263
|
+
Start: 1,
|
|
264
|
+
Property: 2,
|
|
265
|
+
Comma: 3,
|
|
266
|
+
End: 4
|
|
267
|
+
}, i = (e, t) => {
|
|
268
|
+
switch (e.type) {
|
|
269
|
+
case "root":
|
|
270
|
+
if (t)
|
|
271
|
+
throw new Error("解析失败");
|
|
272
|
+
return e;
|
|
273
|
+
case "array":
|
|
274
|
+
if (e.state === a.Value)
|
|
275
|
+
return t ? f(e, t) : e;
|
|
276
|
+
throw new Error("解析失败");
|
|
277
|
+
case "property":
|
|
278
|
+
if (e.state === u.Value)
|
|
279
|
+
return e.state = u.End, e.parent;
|
|
280
|
+
throw new Error("解析失败");
|
|
281
|
+
}
|
|
282
|
+
}, S = (e, t) => {
|
|
283
|
+
if (!e.value) {
|
|
284
|
+
const r = o(t);
|
|
285
|
+
if (r.current)
|
|
286
|
+
return e.value = {
|
|
287
|
+
type: "literal",
|
|
288
|
+
value: null,
|
|
289
|
+
current: r.current,
|
|
290
|
+
parent: e
|
|
291
|
+
}, e.value;
|
|
292
|
+
if (!r.token)
|
|
293
|
+
return e;
|
|
294
|
+
switch (r.token.value) {
|
|
295
|
+
case "[":
|
|
296
|
+
return e.value = {
|
|
297
|
+
type: "array",
|
|
298
|
+
state: a.Start,
|
|
299
|
+
children: [],
|
|
300
|
+
parent: e
|
|
301
|
+
}, e.value;
|
|
302
|
+
case "{":
|
|
303
|
+
return e.value = {
|
|
304
|
+
type: "object",
|
|
305
|
+
state: s.Start,
|
|
306
|
+
children: [],
|
|
307
|
+
parent: e
|
|
308
|
+
}, e.value;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
throw new Error("解析失败");
|
|
312
|
+
}, b = (e, t) => {
|
|
313
|
+
if (e.current)
|
|
314
|
+
switch (e.current.type) {
|
|
315
|
+
case "keyword": {
|
|
316
|
+
const r = E(t, e.current);
|
|
317
|
+
return r.token ? (e.value = r.token.value, e.current = null, i(e.parent)) : (e.current = r.current, e);
|
|
318
|
+
}
|
|
319
|
+
case "string": {
|
|
320
|
+
const r = w(t, e.current);
|
|
321
|
+
return r.token ? (e.value = r.token.value, e.current = null, i(e.parent)) : (e.current = r.current, e);
|
|
322
|
+
}
|
|
323
|
+
case "number": {
|
|
324
|
+
const r = h(t, e.current);
|
|
325
|
+
return r.token ? (e.value = r.token.value, e.current = null, i(e.parent, r.char)) : (e.current = r.current, e);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
throw new Error("解析失败");
|
|
329
|
+
}, f = (e, t) => {
|
|
330
|
+
switch (e.state) {
|
|
331
|
+
case a.Start: {
|
|
332
|
+
const r = o(t);
|
|
333
|
+
if (r.current) {
|
|
334
|
+
e.state = a.Value;
|
|
335
|
+
const n = {
|
|
336
|
+
type: "literal",
|
|
337
|
+
value: null,
|
|
338
|
+
current: r.current,
|
|
339
|
+
parent: e
|
|
340
|
+
};
|
|
341
|
+
return e.children.push(n), n;
|
|
342
|
+
}
|
|
343
|
+
if (!r.token)
|
|
344
|
+
return e;
|
|
345
|
+
switch (r.token.value) {
|
|
346
|
+
case "]":
|
|
347
|
+
return e.state = a.End, i(e.parent);
|
|
348
|
+
case "[": {
|
|
349
|
+
e.state = a.Value;
|
|
350
|
+
const n = {
|
|
351
|
+
type: "array",
|
|
352
|
+
state: a.Start,
|
|
353
|
+
children: [],
|
|
354
|
+
parent: e
|
|
355
|
+
};
|
|
356
|
+
return e.children.push(n), n;
|
|
357
|
+
}
|
|
358
|
+
case "{": {
|
|
359
|
+
e.state = a.Start;
|
|
360
|
+
const n = {
|
|
361
|
+
type: "object",
|
|
362
|
+
state: s.Start,
|
|
363
|
+
children: [],
|
|
364
|
+
parent: e
|
|
365
|
+
};
|
|
366
|
+
return e.children.push(n), n;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
throw new Error("解析失败");
|
|
370
|
+
}
|
|
371
|
+
case a.Comma: {
|
|
372
|
+
const r = o(t);
|
|
373
|
+
if (r.current) {
|
|
374
|
+
e.state = a.Value;
|
|
375
|
+
const n = {
|
|
376
|
+
type: "literal",
|
|
377
|
+
value: null,
|
|
378
|
+
current: r.current,
|
|
379
|
+
parent: e
|
|
380
|
+
};
|
|
381
|
+
return e.children.push(n), n;
|
|
382
|
+
}
|
|
383
|
+
if (!r.token)
|
|
384
|
+
return e;
|
|
385
|
+
switch (r.token.value) {
|
|
386
|
+
case "[": {
|
|
387
|
+
e.state = a.Value;
|
|
388
|
+
const n = {
|
|
389
|
+
type: "array",
|
|
390
|
+
state: a.Start,
|
|
391
|
+
children: [],
|
|
392
|
+
parent: e
|
|
393
|
+
};
|
|
394
|
+
return e.children.push(n), n;
|
|
395
|
+
}
|
|
396
|
+
case "{": {
|
|
397
|
+
e.state = a.Start;
|
|
398
|
+
const n = {
|
|
399
|
+
type: "object",
|
|
400
|
+
state: s.Start,
|
|
401
|
+
children: [],
|
|
402
|
+
parent: e
|
|
403
|
+
};
|
|
404
|
+
return e.children.push(n), n;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
throw new Error("解析失败");
|
|
408
|
+
}
|
|
409
|
+
case a.Value: {
|
|
410
|
+
const r = o(t);
|
|
411
|
+
if (r.current)
|
|
412
|
+
throw new Error("解析失败");
|
|
413
|
+
if (!r.token)
|
|
414
|
+
return e;
|
|
415
|
+
switch (r.token.value) {
|
|
416
|
+
case ",":
|
|
417
|
+
return e.state = a.Comma, e;
|
|
418
|
+
case "]":
|
|
419
|
+
return e.state = a.End, i(e.parent);
|
|
420
|
+
}
|
|
421
|
+
throw new Error("解析失败");
|
|
422
|
+
}
|
|
423
|
+
case a.End:
|
|
424
|
+
throw new Error("解析失败");
|
|
425
|
+
}
|
|
426
|
+
}, x = (e, t) => {
|
|
427
|
+
if (e.current) {
|
|
428
|
+
const r = w(t, e.current);
|
|
429
|
+
return r.current ? (e.current = r.current, e) : (e.value = r.token.value, e.current = null, e.parent.state = u.Colon, e.parent);
|
|
430
|
+
}
|
|
431
|
+
throw new Error("解析失败");
|
|
432
|
+
}, N = (e, t) => {
|
|
433
|
+
switch (e.state) {
|
|
434
|
+
case u.Colon: {
|
|
435
|
+
const r = o(t);
|
|
436
|
+
if (r.current)
|
|
437
|
+
throw new Error("解析失败");
|
|
438
|
+
if (!r.token)
|
|
439
|
+
return e;
|
|
440
|
+
if (r.token.value === ":")
|
|
441
|
+
return e.state = u.Value, e;
|
|
442
|
+
throw new Error("解析失败");
|
|
443
|
+
}
|
|
444
|
+
case u.Value: {
|
|
445
|
+
const r = o(t);
|
|
446
|
+
if (r.current) {
|
|
447
|
+
const n = {
|
|
448
|
+
type: "literal",
|
|
449
|
+
value: null,
|
|
450
|
+
current: r.current,
|
|
451
|
+
parent: e
|
|
452
|
+
};
|
|
453
|
+
return e.value = n, n;
|
|
454
|
+
}
|
|
455
|
+
if (!r.token)
|
|
456
|
+
return e;
|
|
457
|
+
switch (r.token.value) {
|
|
458
|
+
case "[": {
|
|
459
|
+
const n = {
|
|
460
|
+
type: "array",
|
|
461
|
+
state: a.Start,
|
|
462
|
+
children: [],
|
|
463
|
+
parent: e
|
|
464
|
+
};
|
|
465
|
+
return e.value = n, n;
|
|
466
|
+
}
|
|
467
|
+
case "{": {
|
|
468
|
+
const n = {
|
|
469
|
+
type: "object",
|
|
470
|
+
state: s.Start,
|
|
471
|
+
children: [],
|
|
472
|
+
parent: e
|
|
473
|
+
};
|
|
474
|
+
return e.value = n, n;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
throw new Error("解析失败");
|
|
478
|
+
}
|
|
479
|
+
case u.Key:
|
|
480
|
+
case u.End:
|
|
481
|
+
throw new Error("解析失败");
|
|
482
|
+
}
|
|
483
|
+
}, L = (e, t) => {
|
|
484
|
+
switch (e.state) {
|
|
485
|
+
case s.Start: {
|
|
486
|
+
const r = o(t);
|
|
487
|
+
if (r.current) {
|
|
488
|
+
if (r.current.type === "string") {
|
|
489
|
+
e.state = s.Property;
|
|
490
|
+
const n = {
|
|
491
|
+
type: "property",
|
|
492
|
+
state: u.Key,
|
|
493
|
+
key: null,
|
|
494
|
+
value: null,
|
|
495
|
+
parent: e
|
|
496
|
+
}, k = {
|
|
497
|
+
type: "identifier",
|
|
498
|
+
value: "",
|
|
499
|
+
current: r.current,
|
|
500
|
+
parent: n
|
|
501
|
+
};
|
|
502
|
+
return n.key = k, e.children.push(n), k;
|
|
503
|
+
}
|
|
504
|
+
throw new Error("解析失败");
|
|
505
|
+
}
|
|
506
|
+
if (!r.token)
|
|
507
|
+
return e;
|
|
508
|
+
if (r.token.value === "}")
|
|
509
|
+
return e.state = s.End, i(e.parent);
|
|
510
|
+
throw new Error("解析失败");
|
|
511
|
+
}
|
|
512
|
+
case s.Comma: {
|
|
513
|
+
const r = o(t);
|
|
514
|
+
if (r.current) {
|
|
515
|
+
if (r.current.type === "string") {
|
|
516
|
+
e.state = s.Property;
|
|
517
|
+
const n = {
|
|
518
|
+
type: "property",
|
|
519
|
+
state: u.Key,
|
|
520
|
+
key: null,
|
|
521
|
+
value: null,
|
|
522
|
+
parent: e
|
|
523
|
+
}, k = {
|
|
524
|
+
type: "identifier",
|
|
525
|
+
value: "",
|
|
526
|
+
current: r.current,
|
|
527
|
+
parent: n
|
|
528
|
+
};
|
|
529
|
+
return n.key = k, e.children.push(n), k;
|
|
530
|
+
}
|
|
531
|
+
throw new Error("解析失败");
|
|
532
|
+
}
|
|
533
|
+
if (!r.token)
|
|
534
|
+
return e;
|
|
535
|
+
throw new Error("解析失败");
|
|
536
|
+
}
|
|
537
|
+
case s.Property: {
|
|
538
|
+
const r = o(t);
|
|
539
|
+
if (r.current)
|
|
540
|
+
throw new Error("解析失败");
|
|
541
|
+
if (!r.token)
|
|
542
|
+
return e;
|
|
543
|
+
switch (r.token.value) {
|
|
544
|
+
case ",":
|
|
545
|
+
return e.state = s.Comma, e;
|
|
546
|
+
case "}":
|
|
547
|
+
return e.state = s.End, i(e.parent);
|
|
548
|
+
}
|
|
549
|
+
throw new Error("解析失败");
|
|
550
|
+
}
|
|
551
|
+
case s.End:
|
|
552
|
+
throw new Error("解析失败");
|
|
553
|
+
}
|
|
554
|
+
}, V = (e) => {
|
|
555
|
+
switch (e.type) {
|
|
556
|
+
case "root":
|
|
557
|
+
return e;
|
|
558
|
+
case "literal":
|
|
559
|
+
if (e.current && e.current.type === "number") {
|
|
560
|
+
const t = m(e.current);
|
|
561
|
+
return e.value = t.value, e.current = null, i(e.parent);
|
|
562
|
+
}
|
|
563
|
+
throw new Error("解析失败");
|
|
564
|
+
case "array":
|
|
565
|
+
case "object":
|
|
566
|
+
case "property":
|
|
567
|
+
case "identifier":
|
|
568
|
+
throw new Error("解析失败");
|
|
569
|
+
}
|
|
570
|
+
}, J = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
571
|
+
__proto__: null,
|
|
572
|
+
ArrayNodeState: a,
|
|
573
|
+
ObjectNodeState: s,
|
|
574
|
+
PropertyNodeState: u,
|
|
575
|
+
endAst: V,
|
|
576
|
+
parseArrayNode: f,
|
|
577
|
+
parseIdentifierNode: x,
|
|
578
|
+
parseLiteralNode: b,
|
|
579
|
+
parseObjectNode: L,
|
|
580
|
+
parsePropertyNode: N,
|
|
581
|
+
parseRootNode: S
|
|
582
|
+
}, Symbol.toStringTag, { value: "Module" })), K = () => {
|
|
583
|
+
const e = {
|
|
584
|
+
type: "root",
|
|
585
|
+
value: null
|
|
586
|
+
};
|
|
587
|
+
let t = e;
|
|
588
|
+
return {
|
|
589
|
+
getRoot: () => e,
|
|
590
|
+
getCurrent: () => t,
|
|
591
|
+
parse: (r) => {
|
|
592
|
+
switch (t.type) {
|
|
593
|
+
case "root":
|
|
594
|
+
t = S(t, r);
|
|
595
|
+
break;
|
|
596
|
+
case "literal":
|
|
597
|
+
t = b(t, r);
|
|
598
|
+
break;
|
|
599
|
+
case "array":
|
|
600
|
+
t = f(t, r);
|
|
601
|
+
break;
|
|
602
|
+
case "identifier":
|
|
603
|
+
t = x(t, r);
|
|
604
|
+
break;
|
|
605
|
+
case "property":
|
|
606
|
+
t = N(t, r);
|
|
607
|
+
break;
|
|
608
|
+
case "object":
|
|
609
|
+
t = L(t, r);
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
};
|
|
615
|
+
export {
|
|
616
|
+
K as createJsonBrook,
|
|
617
|
+
J as parse,
|
|
618
|
+
_ as tokenize
|
|
619
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
(function(p,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(p=typeof globalThis<"u"?globalThis:p||self,u(p.JsonBrook={}))})(this,(function(p){"use strict";const u={Normal:1,Escape:2},l={Negative:1,Zero:2,Digit:3,Point:4,DigitFraction:5,Exp:6,ExpDigitOrSign:7},O=[" "," ",`
|
|
2
|
+
`,"\r"],j=["{","}","[","]",":",","],g=["true","false","null"],P=g.map(e=>e[0]),D=['"',"\\","/","b","f","n","r","t"],d=["+","-"],c=e=>e>="0"&&e<="9",h=e=>e>="1"&&e<="9",C=e=>c(e)||e>="a"&&e<="f"||e>="A"&&e<="F",w=e=>e==="e"||e==="E",i=e=>{if(O.includes(e))return{token:null,current:null};if(j.includes(e))return{token:{type:"symbol",value:e},current:null};const t=P.indexOf(e);if(t!==-1)return{token:null,current:{type:"keyword",value:g[t],matchLength:1}};if(e==='"')return{token:null,current:{type:"string",state:u.Normal,value:e,escapeLength:0}};if(e==="-")return{token:null,current:{type:"number",state:l.Negative,value:e,validLength:0}};if(e==="0")return{token:null,current:{type:"number",state:l.Zero,value:e,validLength:1}};if(h(e))return{token:null,current:{type:"number",state:l.Digit,value:e,validLength:1}};throw new Error("解析失败")},E=(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("解析失败")},f=(e,t)=>{switch(t.state){case u.Normal:switch(e){case'"':return t.value+=e,{token:{type:"string",value:t.value},current:null};case"\\":return t.state=u.Escape,t.value+=e,t.escapeLength=1,{token:null,current:t};default:return t.value+=e,{token:null,current:t}}case u.Escape:if(t.escapeLength===1){if(D.includes(e))return t.state=u.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(C(e))return t.escapeLength===6?(t.state=u.Normal,t.value+=e,t.escapeLength=0,{token:null,current:t}):(t.value+=e,t.escapeLength++,{token:null,current:t});throw new Error("解析失败")}},m=(e,t)=>{switch(t.state){case l.Negative:if(e==="0")return t.state=l.Zero,t.value+=e,t.validLength=t.value.length,{token:null,current:t};if(h(e))return t.state=l.Digit,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case l.Zero:return e==="."?(t.state=l.Point,t.value+=e,{token:null,current:t}):w(e)?(t.state=l.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case l.Digit:return c(e)?(t.value+=e,t.validLength=t.value.length,{token:null,current:t}):e==="."?(t.state=l.Point,t.value+=e,{token:null,current:t}):w(e)?(t.state=l.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case l.Point:if(c(e))return t.state=l.DigitFraction,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case l.DigitFraction:return c(e)?(t.value+=e,t.validLength=t.value.length,{token:null,current:t}):w(e)?(t.state=l.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case l.Exp:if(d.includes(e))return t.state=l.ExpDigitOrSign,t.value+=e,{token:null,current:t};if(c(e))return t.state=l.ExpDigitOrSign,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case l.ExpDigitOrSign:if(c(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("解析失败")}},b=e=>{if(e.validLength===e.value.length)return{type:"number",value:JSON.parse(e.value)};throw new Error("解析失败")},V=Object.freeze(Object.defineProperty({__proto__:null,NumberTokenState:l,StringTokenState:u,endNumberTokenCurrent:b,parseNextToken:i,parseNextTokenWithKeyword:E,parseNextTokenWithNumber:m,parseNextTokenWithString:f},Symbol.toStringTag,{value:"Module"})),a={Start:1,Value:2,Comma:3,End:4},o={Key:1,Colon:2,Value:3,End:4},s={Start:1,Property:2,Comma:3,End:4},v=(e,t)=>{switch(e.type){case"root":if(t)throw new Error("解析失败");return e;case"array":if(e.state===a.Value)return t?y(e,t):e;throw new Error("解析失败");case"property":if(e.state===o.Value)return e.state=o.End,e.parent;throw new Error("解析失败")}},S=(e,t)=>{if(!e.value){const r=i(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:a.Start,children:[],parent:e},e.value;case"{":return e.value={type:"object",state:s.Start,children:[],parent:e},e.value}}throw new Error("解析失败")},N=(e,t)=>{if(e.current)switch(e.current.type){case"keyword":{const r=E(t,e.current);return r.token?(e.value=r.token.value,e.current=null,v(e.parent)):(e.current=r.current,e)}case"string":{const r=f(t,e.current);return r.token?(e.value=r.token.value,e.current=null,v(e.parent)):(e.current=r.current,e)}case"number":{const r=m(t,e.current);return r.token?(e.value=r.token.value,e.current=null,v(e.parent,r.char)):(e.current=r.current,e)}}throw new Error("解析失败")},y=(e,t)=>{switch(e.state){case a.Start:{const r=i(t);if(r.current){e.state=a.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=a.End,v(e.parent);case"[":{e.state=a.Value;const n={type:"array",state:a.Start,children:[],parent:e};return e.children.push(n),n}case"{":{e.state=a.Start;const n={type:"object",state:s.Start,children:[],parent:e};return e.children.push(n),n}}throw new Error("解析失败")}case a.Comma:{const r=i(t);if(r.current){e.state=a.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=a.Value;const n={type:"array",state:a.Start,children:[],parent:e};return e.children.push(n),n}case"{":{e.state=a.Start;const n={type:"object",state:s.Start,children:[],parent:e};return e.children.push(n),n}}throw new Error("解析失败")}case a.Value:{const r=i(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;switch(r.token.value){case",":return e.state=a.Comma,e;case"]":return e.state=a.End,v(e.parent)}throw new Error("解析失败")}case a.End:throw new Error("解析失败")}},x=(e,t)=>{if(e.current){const r=f(t,e.current);return r.current?(e.current=r.current,e):(e.value=r.token.value,e.current=null,e.parent.state=o.Colon,e.parent)}throw new Error("解析失败")},T=(e,t)=>{switch(e.state){case o.Colon:{const r=i(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;if(r.token.value===":")return e.state=o.Value,e;throw new Error("解析失败")}case o.Value:{const r=i(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:a.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 o.Key:case o.End:throw new Error("解析失败")}},L=(e,t)=>{switch(e.state){case s.Start:{const r=i(t);if(r.current){if(r.current.type==="string"){e.state=s.Property;const n={type:"property",state:o.Key,key:null,value:null,parent:e},k={type:"identifier",value:"",current:r.current,parent:n};return n.key=k,e.children.push(n),k}throw new Error("解析失败")}if(!r.token)return e;if(r.token.value==="}")return e.state=s.End,v(e.parent);throw new Error("解析失败")}case s.Comma:{const r=i(t);if(r.current){if(r.current.type==="string"){e.state=s.Property;const n={type:"property",state:o.Key,key:null,value:null,parent:e},k={type:"identifier",value:"",current:r.current,parent:n};return n.key=k,e.children.push(n),k}throw new Error("解析失败")}if(!r.token)return e;throw new Error("解析失败")}case s.Property:{const r=i(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,v(e.parent)}throw new Error("解析失败")}case s.End:throw new Error("解析失败")}},J=Object.freeze(Object.defineProperty({__proto__:null,ArrayNodeState:a,ObjectNodeState:s,PropertyNodeState:o,endAst:e=>{switch(e.type){case"root":return e;case"literal":if(e.current&&e.current.type==="number"){const t=b(e.current);return e.value=t.value,e.current=null,v(e.parent)}throw new Error("解析失败");case"array":case"object":case"property":case"identifier":throw new Error("解析失败")}},parseArrayNode:y,parseIdentifierNode:x,parseLiteralNode:N,parseObjectNode:L,parsePropertyNode:T,parseRootNode:S},Symbol.toStringTag,{value:"Module"})),_=()=>{const e={type:"root",value:null};let t=e;return{getRoot:()=>e,getCurrent:()=>t,parse:r=>{switch(t.type){case"root":t=S(t,r);break;case"literal":t=N(t,r);break;case"array":t=y(t,r);break;case"identifier":t=x(t,r);break;case"property":t=T(t,r);break;case"object":t=L(t,r);break}}}};p.createJsonBrook=_,p.parse=J,p.tokenize=V,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
|