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