json-brook 0.0.2 → 0.0.4

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.
@@ -1,564 +0,0 @@
1
- // src/create-parser.ts
2
- function createParser() {
3
- const root = {
4
- type: "Root",
5
- current: null
6
- };
7
- let currentRef = root;
8
- const tryGetNextValue = (token) => {
9
- if (token.type === "Keyword") {
10
- return {
11
- type: "Literal",
12
- value: token.value
13
- };
14
- }
15
- if (token.type === "String") {
16
- return {
17
- type: "Literal",
18
- value: token.value
19
- };
20
- }
21
- if (token.type === "Number") {
22
- return {
23
- type: "Literal",
24
- value: token.value
25
- };
26
- }
27
- if (token.type === "Symbol" && token.value === "[") {
28
- return {
29
- type: "Array",
30
- children: [],
31
- state: 0 /* Start */,
32
- parent: currentRef,
33
- current: null
34
- };
35
- }
36
- if (token.type === "Symbol" && token.value === "{") {
37
- return {
38
- type: "Object",
39
- children: [],
40
- state: 0 /* Start */,
41
- parent: currentRef,
42
- current: null
43
- };
44
- }
45
- return null;
46
- };
47
- const onArrayOrObjectFinish = () => {
48
- currentRef = currentRef;
49
- switch (currentRef.parent.type) {
50
- case "Root":
51
- return;
52
- case "Array":
53
- currentRef.parent.children.push(currentRef);
54
- currentRef.parent.current = null;
55
- currentRef = currentRef.parent;
56
- return;
57
- case "Property":
58
- currentRef.parent.current = currentRef;
59
- currentRef = currentRef.parent;
60
- currentRef.parent.children.push(currentRef);
61
- currentRef.parent.current = null;
62
- currentRef = currentRef.parent;
63
- return;
64
- }
65
- };
66
- const onPropertyFinish = () => {
67
- currentRef = currentRef;
68
- currentRef.parent.children.push(currentRef);
69
- currentRef.parent.current = null;
70
- currentRef = currentRef.parent;
71
- };
72
- const getRoot = () => root.current;
73
- const write = (token) => {
74
- switch (currentRef.type) {
75
- case "Root":
76
- if (currentRef.current === null) {
77
- const next = tryGetNextValue(token);
78
- if (next) {
79
- currentRef.current = next;
80
- if (next.type !== "Literal") {
81
- currentRef = next;
82
- }
83
- return;
84
- }
85
- }
86
- throw new Error("解析错误");
87
- case "Array":
88
- switch (currentRef.state) {
89
- case 0 /* Start */: {
90
- if (token.type === "Symbol" && token.value === "]") {
91
- onArrayOrObjectFinish();
92
- return;
93
- }
94
- const next = tryGetNextValue(token);
95
- if (next) {
96
- currentRef.state = 1 /* Value */;
97
- if (next.type === "Literal") {
98
- currentRef.children.push(next);
99
- currentRef.current = null;
100
- } else {
101
- currentRef.current = next;
102
- currentRef = next;
103
- }
104
- return;
105
- }
106
- throw new Error("解析错误");
107
- }
108
- case 1 /* Value */:
109
- if (token.type === "Symbol" && token.value === "]") {
110
- onArrayOrObjectFinish();
111
- return;
112
- }
113
- if (token.type === "Symbol" && token.value === ",") {
114
- currentRef.state = 2 /* Comma */;
115
- return;
116
- }
117
- throw new Error("解析错误");
118
- case 2 /* Comma */: {
119
- const next = tryGetNextValue(token);
120
- if (next) {
121
- currentRef.state = 1 /* Value */;
122
- if (next.type === "Literal") {
123
- currentRef.children.push(next);
124
- currentRef.current = null;
125
- } else {
126
- currentRef.current = next;
127
- currentRef = next;
128
- }
129
- return;
130
- }
131
- throw new Error("解析错误");
132
- }
133
- }
134
- case "Property":
135
- switch (currentRef.state) {
136
- case 0 /* Key */:
137
- if (token.type === "Symbol" && token.value === ":") {
138
- currentRef.state = 1 /* Colon */;
139
- return;
140
- }
141
- throw new Error("解析错误");
142
- case 1 /* Colon */: {
143
- const next = tryGetNextValue(token);
144
- if (next) {
145
- currentRef.state = 2 /* Value */;
146
- currentRef.current = next;
147
- if (next.type === "Literal") {
148
- onPropertyFinish();
149
- } else {
150
- currentRef = next;
151
- }
152
- return;
153
- }
154
- throw new Error("解析错误");
155
- }
156
- }
157
- case "Object":
158
- switch (currentRef.state) {
159
- case 0 /* Start */:
160
- if (token.type === "String") {
161
- currentRef.state = 1 /* Property */;
162
- currentRef.current = {
163
- type: "Property",
164
- key: {
165
- type: "Identifier",
166
- value: token.value
167
- },
168
- current: null,
169
- state: 0 /* Key */,
170
- parent: currentRef
171
- };
172
- currentRef = currentRef.current;
173
- return;
174
- }
175
- if (token.type === "Symbol" && token.value === "}") {
176
- onArrayOrObjectFinish();
177
- return;
178
- }
179
- throw new Error("解析错误");
180
- case 1 /* Property */:
181
- if (token.type === "Symbol" && token.value === "}") {
182
- onArrayOrObjectFinish();
183
- return;
184
- }
185
- if (token.type === "Symbol" && token.value === ",") {
186
- currentRef.state = 2 /* Comma */;
187
- return;
188
- }
189
- throw new Error("解析错误");
190
- case 2 /* Comma */:
191
- if (token.type === "String") {
192
- currentRef.state = 1 /* Property */;
193
- currentRef.current = {
194
- type: "Property",
195
- key: {
196
- type: "Identifier",
197
- value: token.value
198
- },
199
- current: null,
200
- state: 0 /* Key */,
201
- parent: currentRef
202
- };
203
- currentRef = currentRef.current;
204
- return;
205
- }
206
- throw new Error("解析错误");
207
- }
208
- }
209
- };
210
- return {
211
- getRoot,
212
- write
213
- };
214
- }
215
-
216
- // src/create-tokenize.ts
217
- var whiteSpaces = [" ", " ", "\n", "\r"];
218
- var symbols = ["{", "}", "[", "]", ":", ","];
219
- var keywords = ["true", "false", "null"];
220
- var keywordsStart = keywords.map((keyword) => keyword.charAt(0));
221
- var escapes = ['"', "\\", "/", "b", "f", "n", "r", "t"];
222
- var hexEscape = "u";
223
- var flags = ["+", "-"];
224
- function isDigit(char) {
225
- return char >= "0" && char <= "9";
226
- }
227
- function isDigitNotZero(char) {
228
- return char >= "1" && char <= "9";
229
- }
230
- function isHex(char) {
231
- return isDigit(char) || char >= "a" && char <= "f" || char >= "A" && char <= "F";
232
- }
233
- function isExp(char) {
234
- return char === "e" || char === "E";
235
- }
236
- function createTokenize() {
237
- let current = null;
238
- const write = (char) => {
239
- if (current) {
240
- switch (current.type) {
241
- case "Keyword":
242
- if (char === current.value[current.matchedIndex + 1]) {
243
- current.matchedIndex++;
244
- if (current.matchedIndex === current.value.length - 1) {
245
- const token = {
246
- type: "Keyword",
247
- value: JSON.parse(current.value)
248
- };
249
- current = null;
250
- return token;
251
- }
252
- return null;
253
- }
254
- throw new Error("解析失败");
255
- case "String":
256
- switch (current.state) {
257
- case 0 /* Normal */:
258
- switch (char) {
259
- case '"': {
260
- current.value += char;
261
- const token = {
262
- type: "String",
263
- value: JSON.parse(current.value)
264
- };
265
- current = null;
266
- return token;
267
- }
268
- case "\\":
269
- current.state = 1 /* Escape */;
270
- current.value += char;
271
- current.escapeIndex = 0;
272
- return null;
273
- default:
274
- current.value += char;
275
- return null;
276
- }
277
- case 1 /* Escape */: {
278
- if (current.escapeIndex === 0) {
279
- if (escapes.includes(char)) {
280
- current.state = 0 /* Normal */;
281
- current.value += char;
282
- return null;
283
- }
284
- if (char === hexEscape) {
285
- current.value += char;
286
- current.escapeIndex++;
287
- return null;
288
- }
289
- throw new Error("解析失败");
290
- } else {
291
- if (isHex(char)) {
292
- if (current.escapeIndex === 4) {
293
- current.state = 0 /* Normal */;
294
- current.value += char;
295
- return null;
296
- } else {
297
- current.value += char;
298
- current.escapeIndex++;
299
- return null;
300
- }
301
- } else {
302
- throw new Error("解析失败");
303
- }
304
- }
305
- }
306
- }
307
- case "Number":
308
- switch (current.state) {
309
- case 1 /* Negative */:
310
- if (char === "0") {
311
- current.state = 2 /* Zero */;
312
- current.value += char;
313
- current.passed = true;
314
- return null;
315
- }
316
- if (isDigitNotZero(char)) {
317
- current.state = 3 /* Digit */;
318
- current.value += char;
319
- current.passed = true;
320
- return null;
321
- }
322
- throw new Error("解析失败");
323
- case 2 /* Zero */:
324
- if (char === ".") {
325
- current.state = 4 /* Point */;
326
- current.value += char;
327
- current.passed = false;
328
- return null;
329
- }
330
- if (isExp(char)) {
331
- current.state = 6 /* Exp */;
332
- current.value += char;
333
- current.passed = false;
334
- return null;
335
- }
336
- if (current.passed) {
337
- const token = {
338
- type: "Number",
339
- value: JSON.parse(current.value)
340
- };
341
- current = null;
342
- const next = write(char);
343
- if (next) {
344
- return Array.isArray(next) ? [token, ...next] : [token, next];
345
- } else {
346
- return token;
347
- }
348
- }
349
- throw new Error("解析失败");
350
- case 3 /* Digit */:
351
- if (isDigit(char)) {
352
- current.value += char;
353
- current.passed = true;
354
- return null;
355
- }
356
- if (char === ".") {
357
- current.state = 4 /* Point */;
358
- current.value += char;
359
- current.passed = false;
360
- return null;
361
- }
362
- if (isExp(char)) {
363
- current.state = 6 /* Exp */;
364
- current.value += char;
365
- current.passed = false;
366
- return null;
367
- }
368
- if (current.passed) {
369
- const token = {
370
- type: "Number",
371
- value: JSON.parse(current.value)
372
- };
373
- current = null;
374
- const next = write(char);
375
- if (next) {
376
- return Array.isArray(next) ? [token, ...next] : [token, next];
377
- } else {
378
- return token;
379
- }
380
- }
381
- throw new Error("解析失败");
382
- case 4 /* Point */:
383
- if (isDigit(char)) {
384
- current.state = 5 /* DigitFraction */;
385
- current.value += char;
386
- current.passed = true;
387
- return null;
388
- }
389
- if (current.passed) {
390
- const token = {
391
- type: "Number",
392
- value: JSON.parse(current.value)
393
- };
394
- current = null;
395
- const next = write(char);
396
- if (next) {
397
- return Array.isArray(next) ? [token, ...next] : [token, next];
398
- } else {
399
- return token;
400
- }
401
- }
402
- throw new Error("解析失败");
403
- case 5 /* DigitFraction */:
404
- if (isDigit(char)) {
405
- current.value += char;
406
- current.passed = true;
407
- return null;
408
- }
409
- if (isExp(char)) {
410
- current.state = 6 /* Exp */;
411
- current.value += char;
412
- current.passed = false;
413
- return null;
414
- }
415
- if (current.passed) {
416
- const token = {
417
- type: "Number",
418
- value: JSON.parse(current.value)
419
- };
420
- current = null;
421
- const next = write(char);
422
- if (next) {
423
- return Array.isArray(next) ? [token, ...next] : [token, next];
424
- } else {
425
- return token;
426
- }
427
- }
428
- throw new Error("解析失败");
429
- case 6 /* Exp */:
430
- if (flags.includes(char)) {
431
- current.state = 7 /* ExpDigitOrSign */;
432
- current.value += char;
433
- current.passed = false;
434
- return null;
435
- }
436
- if (isDigit(char)) {
437
- current.state = 7 /* ExpDigitOrSign */;
438
- current.value += char;
439
- current.passed = true;
440
- return null;
441
- }
442
- if (current.passed) {
443
- const token = {
444
- type: "Number",
445
- value: JSON.parse(current.value)
446
- };
447
- current = null;
448
- const next = write(char);
449
- if (next) {
450
- return Array.isArray(next) ? [token, ...next] : [token, next];
451
- } else {
452
- return token;
453
- }
454
- }
455
- throw new Error("解析失败");
456
- case 7 /* ExpDigitOrSign */:
457
- if (isDigit(char)) {
458
- current.value += char;
459
- current.passed = true;
460
- return null;
461
- }
462
- if (current.passed) {
463
- const token = {
464
- type: "Number",
465
- value: JSON.parse(current.value)
466
- };
467
- current = null;
468
- const next = write(char);
469
- if (next) {
470
- return Array.isArray(next) ? [token, ...next] : [token, next];
471
- } else {
472
- return token;
473
- }
474
- }
475
- throw new Error("解析失败");
476
- }
477
- }
478
- } else {
479
- if (whiteSpaces.includes(char)) {
480
- return null;
481
- }
482
- if (symbols.includes(char)) {
483
- return {
484
- type: "Symbol",
485
- value: char
486
- };
487
- }
488
- const keywordIndex = keywordsStart.indexOf(char);
489
- if (keywordIndex >= 0) {
490
- current = {
491
- type: "Keyword",
492
- value: keywords[keywordIndex],
493
- matchedIndex: 0
494
- };
495
- return null;
496
- }
497
- if (char === '"') {
498
- current = {
499
- type: "String",
500
- state: 0 /* Normal */,
501
- value: '"',
502
- escapeIndex: 0
503
- };
504
- return null;
505
- }
506
- if (char === "-") {
507
- current = {
508
- type: "Number",
509
- state: 1 /* Negative */,
510
- value: char,
511
- passed: false
512
- };
513
- return null;
514
- }
515
- if (char === "0") {
516
- current = {
517
- type: "Number",
518
- state: 2 /* Zero */,
519
- value: char,
520
- passed: true
521
- };
522
- return null;
523
- }
524
- if (isDigitNotZero(char)) {
525
- current = {
526
- type: "Number",
527
- state: 3 /* Digit */,
528
- value: char,
529
- passed: true
530
- };
531
- return null;
532
- }
533
- throw new Error("解析失败");
534
- }
535
- };
536
- const end = () => {
537
- if (current) {
538
- switch (current.type) {
539
- case "Keyword":
540
- case "String":
541
- throw new Error("解析失败");
542
- case "Number":
543
- if (current.passed) {
544
- const token = {
545
- type: "Number",
546
- value: JSON.parse(current.value)
547
- };
548
- current = null;
549
- return token;
550
- }
551
- return null;
552
- }
553
- }
554
- return null;
555
- };
556
- return {
557
- write,
558
- end
559
- };
560
- }
561
- export {
562
- createParser,
563
- createTokenize
564
- };
package/dist/es/type.js DELETED
File without changes