@ai-sdk-tool/parser 3.2.0 → 3.3.0

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,671 @@
1
+ // src/rjson/index.ts
2
+ var WHITESPACE_TEST_REGEX = /\s/;
3
+ var WHITESPACE_REGEX = /^\s+/;
4
+ var OBJECT_START_REGEX = /^\{/;
5
+ var OBJECT_END_REGEX = /^\}/;
6
+ var ARRAY_START_REGEX = /^\[/;
7
+ var ARRAY_END_REGEX = /^\]/;
8
+ var COMMA_REGEX = /^,/;
9
+ var COLON_REGEX = /^:/;
10
+ var KEYWORD_REGEX = /^(?:true|false|null)/;
11
+ var NUMBER_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/;
12
+ var STRING_DOUBLE_REGEX = /^"(?:[^"\\]|\\["bnrtf\\/]|\\u[0-9a-fA-F]{4})*"/;
13
+ var STRING_SINGLE_REGEX = /^'((?:[^'\\]|\\['bnrtf\\/]|\\u[0-9a-fA-F]{4})*)'/;
14
+ var COMMENT_SINGLE_REGEX = /^\/\/.*?(?:\r\n|\r|\n)/;
15
+ var COMMENT_MULTI_REGEX = /^\/\*[\s\S]*?\*\//;
16
+ var IDENTIFIER_REGEX = /^[$a-zA-Z0-9_\-+.*?!|&%^/#\\]+/;
17
+ function some(array, f) {
18
+ let acc = false;
19
+ for (let i = 0; i < array.length; i += 1) {
20
+ const result = f(array[i], i, array);
21
+ acc = result === void 0 ? false : result;
22
+ if (acc) {
23
+ return acc;
24
+ }
25
+ }
26
+ return acc;
27
+ }
28
+ function makeLexer(tokenSpecs) {
29
+ return (contents) => {
30
+ const tokens = [];
31
+ let line = 1;
32
+ let remainingContents = contents;
33
+ function findToken() {
34
+ const result = some(tokenSpecs, (tokenSpec) => {
35
+ const m = tokenSpec.re.exec(remainingContents);
36
+ if (m) {
37
+ const raw = m[0];
38
+ remainingContents = remainingContents.slice(raw.length);
39
+ return {
40
+ raw,
41
+ matched: tokenSpec.f(m)
42
+ // Process the match using the spec's function
43
+ };
44
+ }
45
+ return;
46
+ });
47
+ return result === false ? void 0 : result;
48
+ }
49
+ while (remainingContents !== "") {
50
+ const matched = findToken();
51
+ if (!matched) {
52
+ const err = new SyntaxError(
53
+ `Unexpected character: ${remainingContents[0]}; input: ${remainingContents.substr(
54
+ 0,
55
+ 100
56
+ )}`
57
+ );
58
+ err.line = line;
59
+ throw err;
60
+ }
61
+ const tokenWithLine = matched.matched;
62
+ tokenWithLine.line = line;
63
+ line += matched.raw.replace(/[^\n]/g, "").length;
64
+ tokens.push(tokenWithLine);
65
+ }
66
+ return tokens;
67
+ };
68
+ }
69
+ function fStringSingle(m) {
70
+ const content = m[1].replace(
71
+ /([^'\\]|\\['bnrtf\\]|\\u[0-9a-fA-F]{4})/g,
72
+ (mm) => {
73
+ if (mm === '"') {
74
+ return '\\"';
75
+ }
76
+ if (mm === "\\'") {
77
+ return "'";
78
+ }
79
+ return mm;
80
+ }
81
+ );
82
+ const match = `"${content}"`;
83
+ return {
84
+ type: "string",
85
+ match,
86
+ // The transformed, double-quoted string representation
87
+ // Use JSON.parse on the transformed string to handle escape sequences correctly
88
+ value: JSON.parse(match)
89
+ };
90
+ }
91
+ function fStringDouble(m) {
92
+ return {
93
+ type: "string",
94
+ match: m[0],
95
+ // The raw matched string (including quotes)
96
+ value: JSON.parse(m[0])
97
+ // Use JSON.parse to handle escapes and get the value
98
+ };
99
+ }
100
+ function fIdentifier(m) {
101
+ const value = m[0];
102
+ const match = '"' + value.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + // Escape backslashes and quotes
103
+ '"';
104
+ return {
105
+ type: "string",
106
+ // Treat identifiers as strings
107
+ value,
108
+ // The original identifier name
109
+ match
110
+ // The double-quoted string representation
111
+ };
112
+ }
113
+ function fComment(m) {
114
+ const match = m[0].replace(
115
+ /./g,
116
+ (c) => WHITESPACE_TEST_REGEX.test(c) ? c : " "
117
+ );
118
+ return {
119
+ type: " ",
120
+ // Represent comments as whitespace tokens
121
+ match,
122
+ // String containing original newlines and spaces for other chars
123
+ value: void 0
124
+ // Comments don't have a semantic value
125
+ };
126
+ }
127
+ function fNumber(m) {
128
+ return {
129
+ type: "number",
130
+ match: m[0],
131
+ // The raw matched number string
132
+ value: Number.parseFloat(m[0])
133
+ // Convert string to number
134
+ };
135
+ }
136
+ function fKeyword(m) {
137
+ let value;
138
+ switch (m[0]) {
139
+ case "null":
140
+ value = null;
141
+ break;
142
+ case "true":
143
+ value = true;
144
+ break;
145
+ case "false":
146
+ value = false;
147
+ break;
148
+ default:
149
+ throw new Error(`Unexpected keyword: ${m[0]}`);
150
+ }
151
+ return {
152
+ type: "atom",
153
+ // Use 'atom' type for these literals
154
+ match: m[0],
155
+ // The raw matched keyword
156
+ value
157
+ // The corresponding JavaScript value
158
+ };
159
+ }
160
+ function makeTokenSpecs(relaxed) {
161
+ function f(type) {
162
+ return (m) => {
163
+ return { type, match: m[0], value: void 0 };
164
+ };
165
+ }
166
+ let tokenSpecs = [
167
+ { re: WHITESPACE_REGEX, f: f(" ") },
168
+ // Whitespace
169
+ { re: OBJECT_START_REGEX, f: f("{") },
170
+ // Object start
171
+ { re: OBJECT_END_REGEX, f: f("}") },
172
+ // Object end
173
+ { re: ARRAY_START_REGEX, f: f("[") },
174
+ // Array start
175
+ { re: ARRAY_END_REGEX, f: f("]") },
176
+ // Array end
177
+ { re: COMMA_REGEX, f: f(",") },
178
+ // Comma separator
179
+ { re: COLON_REGEX, f: f(":") },
180
+ // Key-value separator
181
+ { re: KEYWORD_REGEX, f: fKeyword },
182
+ // Keywords
183
+ // Number: optional sign, digits, optional decimal part, optional exponent
184
+ { re: NUMBER_REGEX, f: fNumber },
185
+ // String: double-quoted, handles escapes
186
+ { re: STRING_DOUBLE_REGEX, f: fStringDouble }
187
+ ];
188
+ if (relaxed) {
189
+ tokenSpecs = tokenSpecs.concat([
190
+ // Single-quoted strings
191
+ {
192
+ re: STRING_SINGLE_REGEX,
193
+ f: fStringSingle
194
+ },
195
+ // Single-line comments (// ...)
196
+ { re: COMMENT_SINGLE_REGEX, f: fComment },
197
+ // Multi-line comments (/* ... */)
198
+ { re: COMMENT_MULTI_REGEX, f: fComment },
199
+ // Unquoted identifiers (treated as strings)
200
+ // Allows letters, numbers, _, -, +, ., *, ?, !, |, &, %, ^, /, #, \
201
+ { re: IDENTIFIER_REGEX, f: fIdentifier }
202
+ // Note: The order matters here. Identifiers are checked after keywords/numbers.
203
+ ]);
204
+ }
205
+ return tokenSpecs;
206
+ }
207
+ var lexer = makeLexer(makeTokenSpecs(true));
208
+ var strictLexer = makeLexer(makeTokenSpecs(false));
209
+ function previousNWSToken(tokens, index) {
210
+ let currentIndex = index;
211
+ for (; currentIndex >= 0; currentIndex -= 1) {
212
+ if (tokens[currentIndex].type !== " ") {
213
+ return currentIndex;
214
+ }
215
+ }
216
+ return;
217
+ }
218
+ function stripTrailingComma(tokens) {
219
+ const res = [];
220
+ tokens.forEach((token, index) => {
221
+ if (index > 0 && (token.type === "]" || token.type === "}")) {
222
+ const prevNWSTokenIndex = previousNWSToken(res, res.length - 1);
223
+ if (prevNWSTokenIndex !== void 0 && res[prevNWSTokenIndex].type === ",") {
224
+ const preCommaIndex = previousNWSToken(res, prevNWSTokenIndex - 1);
225
+ if (preCommaIndex !== void 0 && res[preCommaIndex].type !== "[" && res[preCommaIndex].type !== "{") {
226
+ res[prevNWSTokenIndex] = {
227
+ type: " ",
228
+ match: " ",
229
+ // Represent as a single space
230
+ value: void 0,
231
+ // Whitespace has no value
232
+ line: res[prevNWSTokenIndex].line
233
+ // Preserve original line number
234
+ };
235
+ }
236
+ }
237
+ }
238
+ res.push(token);
239
+ });
240
+ return res;
241
+ }
242
+ function transform(text) {
243
+ let tokens = lexer(text);
244
+ tokens = stripTrailingComma(tokens);
245
+ return tokens.reduce((str, token) => str + token.match, "");
246
+ }
247
+ function popToken(tokens, state) {
248
+ var _a, _b;
249
+ const token = tokens[state.pos];
250
+ state.pos += 1;
251
+ if (!token) {
252
+ const lastLine = tokens.length !== 0 ? (_b = (_a = tokens.at(-1)) == null ? void 0 : _a.line) != null ? _b : 1 : 1;
253
+ return { type: "eof", match: "", value: void 0, line: lastLine };
254
+ }
255
+ return token;
256
+ }
257
+ function strToken(token) {
258
+ switch (token.type) {
259
+ case "atom":
260
+ case "string":
261
+ case "number":
262
+ return `${token.type} ${token.match}`;
263
+ case "eof":
264
+ return "end-of-file";
265
+ default:
266
+ return `'${token.type}'`;
267
+ }
268
+ }
269
+ function skipColon(tokens, state) {
270
+ const colon = popToken(tokens, state);
271
+ if (colon.type !== ":") {
272
+ const message = `Unexpected token: ${strToken(colon)}, expected ':'`;
273
+ if (state.tolerant) {
274
+ state.warnings.push({
275
+ message,
276
+ line: colon.line
277
+ });
278
+ state.pos -= 1;
279
+ } else {
280
+ const err = new SyntaxError(message);
281
+ err.line = colon.line;
282
+ throw err;
283
+ }
284
+ }
285
+ }
286
+ function skipPunctuation(tokens, state, valid) {
287
+ const punctuation = [",", ":", "]", "}"];
288
+ let token = popToken(tokens, state);
289
+ while (true) {
290
+ if (valid == null ? void 0 : valid.includes(token.type)) {
291
+ return token;
292
+ }
293
+ if (token.type === "eof") {
294
+ return token;
295
+ }
296
+ if (punctuation.includes(token.type)) {
297
+ const message = `Unexpected token: ${strToken(
298
+ token
299
+ )}, expected '[', '{', number, string or atom`;
300
+ if (state.tolerant) {
301
+ state.warnings.push({
302
+ message,
303
+ line: token.line
304
+ });
305
+ token = popToken(tokens, state);
306
+ } else {
307
+ const err = new SyntaxError(message);
308
+ err.line = token.line;
309
+ throw err;
310
+ }
311
+ } else {
312
+ return token;
313
+ }
314
+ }
315
+ }
316
+ function raiseError(state, token, message) {
317
+ if (state.tolerant) {
318
+ state.warnings.push({
319
+ message,
320
+ line: token.line
321
+ });
322
+ } else {
323
+ const err = new SyntaxError(message);
324
+ err.line = token.line;
325
+ throw err;
326
+ }
327
+ }
328
+ function raiseUnexpected(state, token, expected) {
329
+ raiseError(
330
+ state,
331
+ token,
332
+ `Unexpected token: ${strToken(token)}, expected ${expected}`
333
+ );
334
+ }
335
+ function checkDuplicates(state, obj, token) {
336
+ const key = String(token.value);
337
+ if (!state.duplicate && Object.hasOwn(obj, key)) {
338
+ raiseError(state, token, `Duplicate key: ${key}`);
339
+ }
340
+ }
341
+ function appendPair(state, obj, key, value) {
342
+ const finalValue = state.reviver ? state.reviver(key, value) : value;
343
+ if (finalValue !== void 0) {
344
+ obj[key] = finalValue;
345
+ }
346
+ }
347
+ function parsePair(tokens, state, obj) {
348
+ let token = skipPunctuation(tokens, state, [":", "string", "number", "atom"]);
349
+ let value;
350
+ if (token.type !== "string") {
351
+ raiseUnexpected(state, token, "string key");
352
+ if (state.tolerant) {
353
+ switch (token.type) {
354
+ case ":":
355
+ token = {
356
+ type: "string",
357
+ value: "null",
358
+ match: '"null"',
359
+ line: token.line
360
+ };
361
+ state.pos -= 1;
362
+ break;
363
+ case "number":
364
+ // Use number as string key
365
+ case "atom":
366
+ token = {
367
+ type: "string",
368
+ value: String(token.value),
369
+ match: `"${token.value}"`,
370
+ line: token.line
371
+ };
372
+ break;
373
+ case "[":
374
+ // Assume missing key before an array
375
+ case "{":
376
+ state.pos -= 1;
377
+ value = parseAny(tokens, state);
378
+ checkDuplicates(state, obj, {
379
+ type: "string",
380
+ value: "null",
381
+ match: '"null"',
382
+ line: token.line
383
+ });
384
+ appendPair(state, obj, "null", value);
385
+ return;
386
+ // Finished parsing this "pair"
387
+ case "eof":
388
+ return;
389
+ // Cannot recover
390
+ default:
391
+ return;
392
+ }
393
+ } else {
394
+ return;
395
+ }
396
+ }
397
+ checkDuplicates(state, obj, token);
398
+ const key = String(token.value);
399
+ skipColon(tokens, state);
400
+ value = parseAny(tokens, state);
401
+ appendPair(state, obj, key, value);
402
+ }
403
+ function parseElement(tokens, state, arr) {
404
+ const key = arr.length;
405
+ const value = parseAny(tokens, state);
406
+ arr[key] = state.reviver ? state.reviver(String(key), value) : value;
407
+ }
408
+ function parseObject(tokens, state) {
409
+ const obj = {};
410
+ return parseMany(tokens, state, obj, {
411
+ skip: [":", "}"],
412
+ // Initially skip over colon or closing brace (for empty/tolerant cases)
413
+ elementParser: parsePair,
414
+ // Use parsePair to parse each key-value element
415
+ elementName: "string key",
416
+ // Expected element type for errors
417
+ endSymbol: "}"
418
+ // The closing token for an object
419
+ });
420
+ }
421
+ function parseArray(tokens, state) {
422
+ const arr = [];
423
+ return parseMany(tokens, state, arr, {
424
+ skip: ["]"],
425
+ // Initially skip over closing bracket (for empty/tolerant cases)
426
+ elementParser: parseElement,
427
+ // Use parseElement to parse each array item
428
+ elementName: "json value",
429
+ // Expected element type for errors
430
+ endSymbol: "]"
431
+ // The closing token for an array
432
+ });
433
+ }
434
+ function handleInvalidToken(token, state, opts, result) {
435
+ raiseUnexpected(state, token, `',' or '${opts.endSymbol}'`);
436
+ if (state.tolerant) {
437
+ if (token.type === "eof") {
438
+ return result;
439
+ }
440
+ state.pos -= 1;
441
+ return null;
442
+ }
443
+ return result;
444
+ }
445
+ function handleCommaToken(params) {
446
+ const { token, tokens, state, opts, result } = params;
447
+ const nextToken = tokens[state.pos];
448
+ if (state.tolerant && nextToken && nextToken.type === opts.endSymbol) {
449
+ raiseError(state, token, `Trailing comma before '${opts.endSymbol}'`);
450
+ popToken(tokens, state);
451
+ return result;
452
+ }
453
+ opts.elementParser(tokens, state, result);
454
+ return null;
455
+ }
456
+ function parseManyInitialElement(tokens, state, result, opts) {
457
+ const token = skipPunctuation(tokens, state, opts.skip);
458
+ if (token.type === "eof") {
459
+ raiseUnexpected(state, token, `'${opts.endSymbol}' or ${opts.elementName}`);
460
+ return result;
461
+ }
462
+ if (token.type === opts.endSymbol) {
463
+ return result;
464
+ }
465
+ state.pos -= 1;
466
+ opts.elementParser(tokens, state, result);
467
+ return;
468
+ }
469
+ function parseManyProcessToken(params) {
470
+ const { token, tokens, state, opts, result } = params;
471
+ if (token.type !== opts.endSymbol && token.type !== ",") {
472
+ const handledResult = handleInvalidToken(token, state, opts, result);
473
+ if (handledResult !== null) {
474
+ return handledResult;
475
+ }
476
+ }
477
+ if (token.type === opts.endSymbol) {
478
+ return result;
479
+ }
480
+ if (token.type === ",") {
481
+ const handledResult = handleCommaToken({
482
+ token,
483
+ tokens,
484
+ state,
485
+ opts,
486
+ result
487
+ });
488
+ if (handledResult !== null) {
489
+ return handledResult;
490
+ }
491
+ return;
492
+ }
493
+ opts.elementParser(tokens, state, result);
494
+ return;
495
+ }
496
+ function parseMany(tokens, state, result, opts) {
497
+ const initialResult = parseManyInitialElement(tokens, state, result, opts);
498
+ if (initialResult !== void 0) {
499
+ return initialResult;
500
+ }
501
+ while (true) {
502
+ const token = popToken(tokens, state);
503
+ const processedResult = parseManyProcessToken({
504
+ token,
505
+ tokens,
506
+ state,
507
+ opts,
508
+ result
509
+ });
510
+ if (processedResult !== void 0) {
511
+ return processedResult;
512
+ }
513
+ }
514
+ }
515
+ function endChecks(tokens, state, ret) {
516
+ if (state.pos < tokens.length) {
517
+ if (state.tolerant) {
518
+ skipPunctuation(tokens, state);
519
+ }
520
+ if (state.pos < tokens.length) {
521
+ raiseError(
522
+ state,
523
+ tokens[state.pos],
524
+ `Unexpected token: ${strToken(tokens[state.pos])}, expected end-of-input`
525
+ );
526
+ }
527
+ }
528
+ if (state.tolerant && state.warnings.length > 0) {
529
+ const message = state.warnings.length === 1 ? state.warnings[0].message : `${state.warnings.length} parse warnings`;
530
+ const err = new SyntaxError(message);
531
+ err.line = state.warnings[0].line;
532
+ err.warnings = state.warnings;
533
+ err.obj = ret;
534
+ throw err;
535
+ }
536
+ }
537
+ function parseAny(tokens, state, end = false) {
538
+ const token = skipPunctuation(tokens, state);
539
+ let ret;
540
+ if (token.type === "eof") {
541
+ if (end) {
542
+ raiseUnexpected(state, token, "json value");
543
+ }
544
+ raiseUnexpected(state, token, "json value");
545
+ return;
546
+ }
547
+ switch (token.type) {
548
+ case "{":
549
+ ret = parseObject(tokens, state);
550
+ break;
551
+ case "[":
552
+ ret = parseArray(tokens, state);
553
+ break;
554
+ case "string":
555
+ // String literal
556
+ case "number":
557
+ // Number literal
558
+ case "atom":
559
+ ret = token.value;
560
+ break;
561
+ default:
562
+ raiseUnexpected(state, token, "json value");
563
+ if (state.tolerant) {
564
+ ret = null;
565
+ } else {
566
+ return;
567
+ }
568
+ }
569
+ if (end) {
570
+ ret = state.reviver ? state.reviver("", ret) : ret;
571
+ endChecks(tokens, state, ret);
572
+ }
573
+ return ret;
574
+ }
575
+ function normalizeParseOptions(optsOrReviver) {
576
+ var _a;
577
+ let options = {};
578
+ if (typeof optsOrReviver === "function") {
579
+ options.reviver = optsOrReviver;
580
+ } else if (optsOrReviver !== null && typeof optsOrReviver === "object") {
581
+ options = { ...optsOrReviver };
582
+ } else if (optsOrReviver !== void 0) {
583
+ throw new TypeError(
584
+ "Second argument must be a reviver function or an options object."
585
+ );
586
+ }
587
+ if (options.relaxed === void 0) {
588
+ if (options.warnings === true || options.tolerant === true) {
589
+ options.relaxed = true;
590
+ } else if (options.warnings === false && options.tolerant === false) {
591
+ options.relaxed = false;
592
+ } else {
593
+ options.relaxed = true;
594
+ }
595
+ }
596
+ options.tolerant = options.tolerant || options.warnings;
597
+ options.duplicate = (_a = options.duplicate) != null ? _a : false;
598
+ return options;
599
+ }
600
+ function createParseState(options) {
601
+ var _a, _b;
602
+ return {
603
+ pos: 0,
604
+ reviver: options.reviver,
605
+ tolerant: (_a = options.tolerant) != null ? _a : false,
606
+ duplicate: (_b = options.duplicate) != null ? _b : false,
607
+ warnings: []
608
+ };
609
+ }
610
+ function parseWithCustomParser(text, options) {
611
+ const lexerToUse = options.relaxed ? lexer : strictLexer;
612
+ let tokens = lexerToUse(text);
613
+ if (options.relaxed) {
614
+ tokens = stripTrailingComma(tokens);
615
+ }
616
+ tokens = tokens.filter((token) => token.type !== " ");
617
+ const state = createParseState(options);
618
+ return parseAny(tokens, state, true);
619
+ }
620
+ function parseWithTransform(text, options) {
621
+ let tokens = lexer(text);
622
+ tokens = stripTrailingComma(tokens);
623
+ const newtext = tokens.reduce((str, token) => str + token.match, "");
624
+ return JSON.parse(
625
+ newtext,
626
+ options.reviver
627
+ );
628
+ }
629
+ function parse(text, optsOrReviver) {
630
+ const options = normalizeParseOptions(optsOrReviver);
631
+ if (!(options.relaxed || options.warnings || options.tolerant) && options.duplicate) {
632
+ return JSON.parse(
633
+ text,
634
+ options.reviver
635
+ );
636
+ }
637
+ if (options.warnings || options.tolerant || !options.duplicate) {
638
+ return parseWithCustomParser(text, options);
639
+ }
640
+ return parseWithTransform(text, options);
641
+ }
642
+ function stringifyPair(obj, key) {
643
+ return `${JSON.stringify(key)}:${stringify(obj[key])}`;
644
+ }
645
+ function stringify(obj) {
646
+ const type = typeof obj;
647
+ if (type === "string" || type === "number" || type === "boolean" || obj === null) {
648
+ return JSON.stringify(obj);
649
+ }
650
+ if (type === "undefined") {
651
+ return "null";
652
+ }
653
+ if (Array.isArray(obj)) {
654
+ const elements = obj.map(stringify).join(",");
655
+ return `[${elements}]`;
656
+ }
657
+ if (type === "object") {
658
+ const keys = Object.keys(obj);
659
+ keys.sort();
660
+ const pairs = keys.map((key) => stringifyPair(obj, key)).join(",");
661
+ return `{${pairs}}`;
662
+ }
663
+ return "null";
664
+ }
665
+
666
+ export {
667
+ transform,
668
+ parse,
669
+ stringify
670
+ };
671
+ //# sourceMappingURL=chunk-IX4FJELL.js.map