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