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