@typia/utils 12.0.0-dev.20260305 → 12.0.0-dev.20260307

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.
Files changed (60) hide show
  1. package/README.md +5 -5
  2. package/lib/converters/LlmSchemaConverter.d.ts +1 -20
  3. package/lib/converters/LlmSchemaConverter.js +0 -189
  4. package/lib/converters/LlmSchemaConverter.js.map +1 -1
  5. package/lib/converters/LlmSchemaConverter.mjs +0 -227
  6. package/lib/converters/LlmSchemaConverter.mjs.map +1 -1
  7. package/lib/http/HttpLlm.d.ts +2 -32
  8. package/lib/http/HttpLlm.js +4 -27
  9. package/lib/http/HttpLlm.js.map +1 -1
  10. package/lib/http/HttpLlm.mjs +1 -24
  11. package/lib/http/HttpLlm.mjs.map +1 -1
  12. package/lib/http/internal/HttpLlmApplicationComposer.js +13 -19
  13. package/lib/http/internal/HttpLlmApplicationComposer.js.map +1 -1
  14. package/lib/http/internal/HttpLlmApplicationComposer.mjs +7 -9
  15. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  16. package/lib/index.mjs +11 -11
  17. package/lib/utils/LlmJson.d.ts +67 -0
  18. package/lib/utils/LlmJson.js +106 -0
  19. package/lib/utils/LlmJson.js.map +1 -0
  20. package/lib/utils/LlmJson.mjs +113 -0
  21. package/lib/utils/LlmJson.mjs.map +1 -0
  22. package/lib/utils/index.d.ts +1 -1
  23. package/lib/utils/index.js +1 -1
  24. package/lib/utils/index.js.map +1 -1
  25. package/lib/utils/index.mjs +1 -1
  26. package/lib/utils/internal/coerceLlmArguments.d.ts +1 -0
  27. package/lib/utils/internal/coerceLlmArguments.js +233 -0
  28. package/lib/utils/internal/coerceLlmArguments.js.map +1 -0
  29. package/lib/utils/internal/coerceLlmArguments.mjs +232 -0
  30. package/lib/utils/internal/coerceLlmArguments.mjs.map +1 -0
  31. package/lib/utils/internal/parseLenientJson.d.ts +1 -0
  32. package/lib/utils/internal/parseLenientJson.js +639 -0
  33. package/lib/utils/internal/parseLenientJson.js.map +1 -0
  34. package/lib/utils/internal/parseLenientJson.mjs +640 -0
  35. package/lib/utils/internal/parseLenientJson.mjs.map +1 -0
  36. package/lib/utils/internal/stringifyValidationFailure.d.ts +2 -0
  37. package/lib/utils/{stringifyValidationFailure.js → internal/stringifyValidationFailure.js} +55 -63
  38. package/lib/utils/internal/stringifyValidationFailure.js.map +1 -0
  39. package/lib/utils/{stringifyValidationFailure.mjs → internal/stringifyValidationFailure.mjs} +54 -63
  40. package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -0
  41. package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
  42. package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
  43. package/package.json +2 -2
  44. package/src/converters/LlmSchemaConverter.ts +0 -277
  45. package/src/http/HttpLlm.ts +1 -44
  46. package/src/http/internal/HttpLlmApplicationComposer.ts +3 -9
  47. package/src/utils/LlmJson.ts +115 -0
  48. package/src/utils/index.ts +1 -1
  49. package/src/utils/internal/coerceLlmArguments.ts +297 -0
  50. package/src/utils/internal/parseLenientJson.ts +731 -0
  51. package/src/utils/{stringifyValidationFailure.ts → internal/stringifyValidationFailure.ts} +66 -70
  52. package/lib/http/internal/LlmDataMerger.d.ts +0 -48
  53. package/lib/http/internal/LlmDataMerger.js +0 -60
  54. package/lib/http/internal/LlmDataMerger.js.map +0 -1
  55. package/lib/http/internal/LlmDataMerger.mjs +0 -59
  56. package/lib/http/internal/LlmDataMerger.mjs.map +0 -1
  57. package/lib/utils/stringifyValidationFailure.d.ts +0 -25
  58. package/lib/utils/stringifyValidationFailure.js.map +0 -1
  59. package/lib/utils/stringifyValidationFailure.mjs.map +0 -1
  60. package/src/http/internal/LlmDataMerger.ts +0 -73
@@ -0,0 +1,731 @@
1
+ import { DeepPartial, IJsonParseResult } from "@typia/interface";
2
+
3
+ /**
4
+ * Maximum nesting depth to prevent stack overflow attacks.
5
+ *
6
+ * @internal
7
+ */
8
+ const MAX_DEPTH: number = 512;
9
+
10
+ /**
11
+ * Check if a string is a valid 4-character hexadecimal string.
12
+ *
13
+ * @internal
14
+ */
15
+ function isHexString(s: string): boolean {
16
+ if (s.length !== 4) return false;
17
+ for (let i = 0; i < 4; i++) {
18
+ const c: number = s.charCodeAt(i);
19
+ if (
20
+ !((c >= 48 && c <= 57) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102))
21
+ ) {
22
+ return false;
23
+ }
24
+ }
25
+ return true;
26
+ }
27
+
28
+ /**
29
+ * Extract JSON content from markdown code block if present.
30
+ *
31
+ * LLM outputs often wrap JSON in markdown code blocks like:
32
+ *
33
+ * Here is your result:
34
+ *
35
+ * ```json
36
+ * { "name": "test" }
37
+ * ```
38
+ *
39
+ * This function extracts the content between the backticks.
40
+ *
41
+ * IMPORTANT: Only extracts if the input doesn't already start with JSON. If
42
+ * input (after trim) starts with `{`, `[`, or `"`, it's already JSON and any
43
+ * markdown inside is part of a string value.
44
+ *
45
+ * @param input Text that may contain markdown code block
46
+ * @returns Extracted content or null if no code block found
47
+ * @internal
48
+ */
49
+ function extractMarkdownCodeBlock(input: string): string | null {
50
+ // Must be ```json specifically, not just ```
51
+ const codeBlockStart: number = input.indexOf("```json");
52
+ if (codeBlockStart === -1) return null;
53
+
54
+ // Check if input already starts with JSON (after trimming whitespace)
55
+ // If so, don't extract - the markdown is inside a JSON string value
56
+ const trimmed: string = input.trimStart();
57
+ if (trimmed.length > 0) {
58
+ const firstChar: string = trimmed[0]!;
59
+ if (firstChar === "{" || firstChar === "[" || firstChar === '"') {
60
+ return null;
61
+ }
62
+ }
63
+
64
+ // Find the end of the opening line (after ```json)
65
+ let contentStart: number = codeBlockStart + 7; // length of "```json"
66
+ while (contentStart < input.length && input[contentStart] !== "\n") {
67
+ contentStart++;
68
+ }
69
+ if (contentStart >= input.length) return null;
70
+ contentStart++; // skip the newline
71
+
72
+ // Find the closing ```
73
+ const codeBlockEnd: number = input.indexOf("```", contentStart);
74
+ if (codeBlockEnd === -1) {
75
+ // No closing ``` - return everything after opening
76
+ return input.slice(contentStart);
77
+ }
78
+
79
+ return input.slice(contentStart, codeBlockEnd);
80
+ }
81
+
82
+ /**
83
+ * Find the start position of JSON object/array content in text that may have
84
+ * junk prefix.
85
+ *
86
+ * LLM outputs often contain text before JSON like:
87
+ *
88
+ * - "Here is your JSON: {"name": "test"}"
89
+ * - "Sure! [1, 2, 3]"
90
+ *
91
+ * This function only looks for `{` or `[` to skip junk prefix. Primitive values
92
+ * (strings, numbers, booleans) are handled directly by the parser.
93
+ *
94
+ * @param input Text that may contain JSON with junk prefix
95
+ * @returns Index of first `{` or `[`, or -1 if not found
96
+ * @internal
97
+ */
98
+ function findJsonStart(input: string): number {
99
+ const objStart: number = input.indexOf("{");
100
+ const arrStart: number = input.indexOf("[");
101
+
102
+ if (objStart === -1 && arrStart === -1) return -1;
103
+ if (objStart === -1) return arrStart;
104
+ if (arrStart === -1) return objStart;
105
+ return Math.min(objStart, arrStart);
106
+ }
107
+
108
+ /**
109
+ * Check if input starts with a valid JSON primitive token.
110
+ *
111
+ * @param input Trimmed input string
112
+ * @returns True if input starts with a primitive value
113
+ * @internal
114
+ */
115
+ function startsWithPrimitive(input: string): boolean {
116
+ if (input.length === 0) return false;
117
+ const ch: string = input[0]!;
118
+ // String
119
+ if (ch === '"') return true;
120
+ // Number (digit or minus)
121
+ if ((ch >= "0" && ch <= "9") || ch === "-") return true;
122
+ // Keywords
123
+ if (
124
+ input.startsWith("true") ||
125
+ input.startsWith("false") ||
126
+ input.startsWith("null")
127
+ )
128
+ return true;
129
+ // Partial keywords
130
+ if (
131
+ "true".startsWith(input) ||
132
+ "false".startsWith(input) ||
133
+ "null".startsWith(input)
134
+ )
135
+ return true;
136
+ return false;
137
+ }
138
+
139
+ /**
140
+ * Parse lenient JSON that may be incomplete or malformed.
141
+ *
142
+ * Handles:
143
+ *
144
+ * - Unclosed brackets `{`, `[` - parses as much as possible
145
+ * - Trailing commas `[1, 2, ]` - ignores them
146
+ * - Unclosed strings `"hello` - returns partial string
147
+ * - Junk text before JSON (LLM often adds explanatory text)
148
+ * - Markdown code blocks (extracts content from `json ... `)
149
+ * - Incomplete keywords like `tru`, `fal`, `nul`
150
+ * - Unicode escape sequences including surrogate pairs (emoji)
151
+ * - JavaScript-style comments (single-line and multi-line)
152
+ * - Unquoted object keys (JavaScript identifier style)
153
+ *
154
+ * @param input Raw JSON string (potentially incomplete)
155
+ * @returns Parse result with data, original input, and any errors
156
+ * @internal
157
+ */
158
+ export function parseLenientJson<T>(input: string): IJsonParseResult<T> {
159
+ // Try native JSON.parse first (faster for valid JSON)
160
+ try {
161
+ return {
162
+ success: true,
163
+ data: JSON.parse(input) as T,
164
+ };
165
+ } catch {
166
+ // Fall back to lenient parser
167
+ }
168
+
169
+ // Extract markdown code block if present
170
+ const codeBlockContent: string | null = extractMarkdownCodeBlock(input);
171
+ const jsonSource: string =
172
+ codeBlockContent !== null ? codeBlockContent : input;
173
+
174
+ // Check if input is empty or whitespace-only
175
+ const trimmed: string = jsonSource.trim();
176
+ if (trimmed.length === 0) {
177
+ const errors: IJsonParseResult.IError[] = [];
178
+ const parser: LenientJsonParser = new LenientJsonParser(jsonSource, errors);
179
+ const data: unknown = parser.parse();
180
+ if (errors.length > 0) {
181
+ return { success: false, data: data as DeepPartial<T>, input, errors };
182
+ }
183
+ return { success: true, data: data as T };
184
+ }
185
+
186
+ // Check if input starts with a primitive value (no junk prefix skipping needed)
187
+ if (startsWithPrimitive(trimmed)) {
188
+ const errors: IJsonParseResult.IError[] = [];
189
+ const parser: LenientJsonParser = new LenientJsonParser(jsonSource, errors);
190
+ const data: unknown = parser.parse();
191
+ if (errors.length > 0) {
192
+ return { success: false, data: data as DeepPartial<T>, input, errors };
193
+ }
194
+ return { success: true, data: data as T };
195
+ }
196
+
197
+ // Find JSON start position (skip junk prefix from LLM)
198
+ const jsonStart: number = findJsonStart(jsonSource);
199
+ if (jsonStart === -1) {
200
+ // No JSON found - return empty object for lenient behavior
201
+ return {
202
+ success: true,
203
+ data: {} as T,
204
+ };
205
+ }
206
+
207
+ // Extract JSON portion (skip junk prefix)
208
+ const jsonInput: string =
209
+ jsonStart > 0 ? jsonSource.slice(jsonStart) : jsonSource;
210
+
211
+ const errors: IJsonParseResult.IError[] = [];
212
+ const parser: LenientJsonParser = new LenientJsonParser(jsonInput, errors);
213
+ const data: unknown = parser.parse();
214
+
215
+ if (errors.length > 0) {
216
+ return {
217
+ success: false,
218
+ data: data as DeepPartial<T>,
219
+ input,
220
+ errors,
221
+ };
222
+ }
223
+ return {
224
+ success: true,
225
+ data: data as T,
226
+ };
227
+ }
228
+
229
+ /**
230
+ * Lenient JSON parser that handles incomplete JSON.
231
+ *
232
+ * @internal
233
+ */
234
+ class LenientJsonParser {
235
+ private pos: number = 0;
236
+ private depth: number = 0;
237
+ private readonly input: string;
238
+ private readonly errors: IJsonParseResult.IError[];
239
+
240
+ constructor(input: string, errors: IJsonParseResult.IError[]) {
241
+ this.input = input;
242
+ this.errors = errors;
243
+ }
244
+
245
+ parse(): unknown {
246
+ this.skipWhitespace();
247
+ if (this.pos >= this.input.length) {
248
+ return undefined;
249
+ }
250
+ return this.parseValue("$input");
251
+ }
252
+
253
+ private parseValue(path: string): unknown {
254
+ this.skipWhitespace();
255
+
256
+ if (this.pos >= this.input.length) {
257
+ return undefined;
258
+ }
259
+
260
+ // Check for maximum depth to prevent stack overflow
261
+ if (this.depth >= MAX_DEPTH) {
262
+ this.errors.push({
263
+ path,
264
+ expected: "value (max depth exceeded)",
265
+ value: undefined,
266
+ });
267
+ return undefined;
268
+ }
269
+
270
+ const char: string = this.input[this.pos]!;
271
+
272
+ if (char === "{") return this.parseObject(path);
273
+ if (char === "[") return this.parseArray(path);
274
+ if (char === '"') return this.parseString();
275
+ if (char === "-" || (char >= "0" && char <= "9")) return this.parseNumber();
276
+
277
+ // Handle keywords (true, false, null) or invalid identifiers
278
+ if (this.isIdentifierStart(char)) {
279
+ return this.parseKeywordOrIdentifier(path);
280
+ }
281
+
282
+ this.errors.push({
283
+ path,
284
+ expected: "JSON value",
285
+ value: this.getErrorContext(),
286
+ });
287
+ // Skip the problematic character and try to continue
288
+ this.pos++;
289
+ return undefined;
290
+ }
291
+
292
+ private getErrorContext(): string {
293
+ // Get surrounding context for better error messages
294
+ const start: number = Math.max(0, this.pos - 10);
295
+ const end: number = Math.min(this.input.length, this.pos + 20);
296
+ const before: string = this.input.slice(start, this.pos);
297
+ const after: string = this.input.slice(this.pos, end);
298
+ return (
299
+ (start > 0 ? "..." : "") +
300
+ before +
301
+ "→" +
302
+ after +
303
+ (end < this.input.length ? "..." : "")
304
+ );
305
+ }
306
+
307
+ private parseKeywordOrIdentifier(path: string): unknown {
308
+ // Extract the token (sequence of identifier characters)
309
+ const start: number = this.pos;
310
+ while (
311
+ this.pos < this.input.length &&
312
+ this.isIdentifierChar(this.input[this.pos]!)
313
+ ) {
314
+ this.pos++;
315
+ }
316
+ const token: string = this.input.slice(start, this.pos);
317
+
318
+ // Check for complete or partial keyword matches
319
+ if (token === "true") return true;
320
+ if (token === "false") return false;
321
+ if (token === "null") return null;
322
+
323
+ // Partial match for lenient parsing (e.g., "tru" -> true, "fal" -> false)
324
+ if ("true".startsWith(token) && token.length > 0) return true;
325
+ if ("false".startsWith(token) && token.length > 0) return false;
326
+ if ("null".startsWith(token) && token.length > 0) return null;
327
+
328
+ // Check if this looks like a string with missing opening quote (e.g., abcdefg")
329
+ if (this.pos < this.input.length && this.input[this.pos] === '"') {
330
+ // Treat as unquoted string value - skip the errant closing quote and return as string
331
+ this.pos++; // skip the closing quote
332
+ this.errors.push({
333
+ path,
334
+ expected: "quoted string",
335
+ value: "missing opening quote for '" + token + "'",
336
+ });
337
+ return token;
338
+ }
339
+
340
+ // Invalid identifier as value - provide helpful error message
341
+ this.errors.push({
342
+ path,
343
+ expected: "JSON value (string, number, boolean, null, object, or array)",
344
+ value: "unquoted string '" + token + "' - did you forget quotes?",
345
+ });
346
+ // Skip to next comma, closing brace/bracket for recovery
347
+ this.skipToRecoveryPoint();
348
+ return undefined;
349
+ }
350
+
351
+ private skipToRecoveryPoint(): void {
352
+ while (this.pos < this.input.length) {
353
+ const ch: string = this.input[this.pos]!;
354
+ if (ch === "," || ch === "}" || ch === "]") {
355
+ return;
356
+ }
357
+ this.pos++;
358
+ }
359
+ }
360
+
361
+ private parseObject(path: string): Record<string, unknown> {
362
+ const result: Record<string, unknown> = {};
363
+ this.pos++; // skip '{'
364
+ this.depth++;
365
+ this.skipWhitespace();
366
+
367
+ while (this.pos < this.input.length) {
368
+ this.skipWhitespace();
369
+
370
+ // Handle end of object or end of input
371
+ if (this.pos >= this.input.length || this.input[this.pos] === "}") {
372
+ if (this.pos < this.input.length) this.pos++; // skip '}'
373
+ this.depth--;
374
+ return result;
375
+ }
376
+
377
+ // Skip trailing comma
378
+ if (this.input[this.pos] === ",") {
379
+ this.pos++;
380
+ this.skipWhitespace();
381
+ continue;
382
+ }
383
+
384
+ // Parse key (quoted string or unquoted identifier)
385
+ let key: string;
386
+ if (this.input[this.pos] === '"') {
387
+ key = this.parseString();
388
+ } else if (this.isIdentifierStart(this.input[this.pos]!)) {
389
+ key = this.parseIdentifier();
390
+ } else {
391
+ this.errors.push({
392
+ path,
393
+ expected: "string key",
394
+ value: this.input[this.pos],
395
+ });
396
+ // Try to recover by skipping to next meaningful character
397
+ this.depth--;
398
+ return result;
399
+ }
400
+ if (typeof key !== "string") {
401
+ this.depth--;
402
+ return result;
403
+ }
404
+
405
+ this.skipWhitespace();
406
+
407
+ // Expect colon - but if we're at end of input, it's just incomplete (not an error)
408
+ if (this.pos >= this.input.length) {
409
+ this.depth--;
410
+ return result;
411
+ }
412
+ if (this.input[this.pos] !== ":") {
413
+ this.errors.push({
414
+ path: path + "." + key,
415
+ expected: "':'",
416
+ value: this.input[this.pos],
417
+ });
418
+ this.depth--;
419
+ return result;
420
+ }
421
+ this.pos++; // skip ':'
422
+
423
+ this.skipWhitespace();
424
+
425
+ // Parse value
426
+ if (this.pos >= this.input.length) {
427
+ // No value - incomplete but not an error for lenient parsing
428
+ this.depth--;
429
+ return result;
430
+ }
431
+
432
+ const value: unknown = this.parseValue(path + "." + key);
433
+ result[key] = value;
434
+
435
+ this.skipWhitespace();
436
+
437
+ // Handle comma or end
438
+ if (this.pos < this.input.length && this.input[this.pos] === ",") {
439
+ this.pos++;
440
+ }
441
+ }
442
+
443
+ this.depth--;
444
+ return result;
445
+ }
446
+
447
+ private parseArray(path: string): unknown[] {
448
+ const result: unknown[] = [];
449
+ this.pos++; // skip '['
450
+ this.depth++;
451
+ this.skipWhitespace();
452
+
453
+ let index: number = 0;
454
+ while (this.pos < this.input.length) {
455
+ this.skipWhitespace();
456
+
457
+ // Handle end of array or end of input
458
+ if (this.pos >= this.input.length || this.input[this.pos] === "]") {
459
+ if (this.pos < this.input.length) this.pos++; // skip ']'
460
+ this.depth--;
461
+ return result;
462
+ }
463
+
464
+ // Skip trailing comma
465
+ if (this.input[this.pos] === ",") {
466
+ this.pos++;
467
+ this.skipWhitespace();
468
+ continue;
469
+ }
470
+
471
+ // Parse value
472
+ const value: unknown = this.parseValue(path + "[" + index + "]");
473
+ result.push(value);
474
+ index++;
475
+
476
+ this.skipWhitespace();
477
+
478
+ // Handle comma or end
479
+ if (this.pos < this.input.length && this.input[this.pos] === ",") {
480
+ this.pos++;
481
+ }
482
+ }
483
+
484
+ this.depth--;
485
+ return result;
486
+ }
487
+
488
+ private parseString(): string {
489
+ this.pos++; // skip opening '"'
490
+ let result: string = "";
491
+ let escaped: boolean = false;
492
+
493
+ while (this.pos < this.input.length) {
494
+ const char: string = this.input[this.pos]!;
495
+
496
+ if (escaped) {
497
+ switch (char) {
498
+ case '"':
499
+ result += '"';
500
+ break;
501
+ case "\\":
502
+ result += "\\";
503
+ break;
504
+ case "/":
505
+ result += "/";
506
+ break;
507
+ case "b":
508
+ result += "\b";
509
+ break;
510
+ case "f":
511
+ result += "\f";
512
+ break;
513
+ case "n":
514
+ result += "\n";
515
+ break;
516
+ case "r":
517
+ result += "\r";
518
+ break;
519
+ case "t":
520
+ result += "\t";
521
+ break;
522
+ case "u":
523
+ // Parse unicode escape
524
+ if (this.pos + 4 <= this.input.length) {
525
+ const hex: string = this.input.slice(this.pos + 1, this.pos + 5);
526
+ if (isHexString(hex)) {
527
+ const highCode: number = parseInt(hex, 16);
528
+ this.pos += 4;
529
+
530
+ // Check for surrogate pair (emoji and characters > U+FFFF)
531
+ if (
532
+ highCode >= 0xd800 &&
533
+ highCode <= 0xdbff &&
534
+ this.pos + 6 <= this.input.length &&
535
+ this.input[this.pos + 1] === "\\" &&
536
+ this.input[this.pos + 2] === "u"
537
+ ) {
538
+ const lowHex: string = this.input.slice(
539
+ this.pos + 3,
540
+ this.pos + 7,
541
+ );
542
+ if (isHexString(lowHex)) {
543
+ const lowCode: number = parseInt(lowHex, 16);
544
+ if (lowCode >= 0xdc00 && lowCode <= 0xdfff) {
545
+ result += String.fromCharCode(highCode, lowCode);
546
+ this.pos += 6;
547
+ break;
548
+ }
549
+ }
550
+ }
551
+ result += String.fromCharCode(highCode);
552
+ } else {
553
+ // Invalid hex - preserve escape sequence literally
554
+ result += "\\u" + hex;
555
+ this.pos += 4;
556
+ }
557
+ } else {
558
+ // Incomplete unicode escape - add partial sequence
559
+ const partial: string = this.input.slice(this.pos + 1);
560
+ result += "\\u" + partial;
561
+ this.pos = this.input.length - 1;
562
+ }
563
+ break;
564
+ default:
565
+ result += char;
566
+ }
567
+ escaped = false;
568
+ this.pos++;
569
+ continue;
570
+ }
571
+
572
+ if (char === "\\") {
573
+ escaped = true;
574
+ this.pos++;
575
+ continue;
576
+ }
577
+
578
+ if (char === '"') {
579
+ this.pos++; // skip closing '"'
580
+ return result;
581
+ }
582
+
583
+ result += char;
584
+ this.pos++;
585
+ }
586
+
587
+ // Unclosed string - return what we have (lenient)
588
+ return result;
589
+ }
590
+
591
+ private parseNumber(): number {
592
+ const start: number = this.pos;
593
+
594
+ // Handle negative sign
595
+ if (this.input[this.pos] === "-") {
596
+ this.pos++;
597
+ }
598
+
599
+ // Parse integer part
600
+ while (
601
+ this.pos < this.input.length &&
602
+ this.input[this.pos]! >= "0" &&
603
+ this.input[this.pos]! <= "9"
604
+ ) {
605
+ this.pos++;
606
+ }
607
+
608
+ // Parse decimal part
609
+ if (this.pos < this.input.length && this.input[this.pos] === ".") {
610
+ this.pos++;
611
+ while (
612
+ this.pos < this.input.length &&
613
+ this.input[this.pos]! >= "0" &&
614
+ this.input[this.pos]! <= "9"
615
+ ) {
616
+ this.pos++;
617
+ }
618
+ }
619
+
620
+ // Parse exponent
621
+ if (
622
+ this.pos < this.input.length &&
623
+ (this.input[this.pos] === "e" || this.input[this.pos] === "E")
624
+ ) {
625
+ this.pos++;
626
+ if (
627
+ this.pos < this.input.length &&
628
+ (this.input[this.pos] === "+" || this.input[this.pos] === "-")
629
+ ) {
630
+ this.pos++;
631
+ }
632
+ while (
633
+ this.pos < this.input.length &&
634
+ this.input[this.pos]! >= "0" &&
635
+ this.input[this.pos]! <= "9"
636
+ ) {
637
+ this.pos++;
638
+ }
639
+ }
640
+
641
+ const numStr: string = this.input.slice(start, this.pos);
642
+ const num: number = Number(numStr);
643
+ return Number.isNaN(num) ? 0 : num;
644
+ }
645
+
646
+ private isIdentifierStart(ch: string): boolean {
647
+ return (
648
+ (ch >= "a" && ch <= "z") ||
649
+ (ch >= "A" && ch <= "Z") ||
650
+ ch === "_" ||
651
+ ch === "$"
652
+ );
653
+ }
654
+
655
+ private isIdentifierChar(ch: string): boolean {
656
+ return (
657
+ (ch >= "a" && ch <= "z") ||
658
+ (ch >= "A" && ch <= "Z") ||
659
+ (ch >= "0" && ch <= "9") ||
660
+ ch === "_" ||
661
+ ch === "$"
662
+ );
663
+ }
664
+
665
+ private parseIdentifier(): string {
666
+ const start: number = this.pos;
667
+ while (
668
+ this.pos < this.input.length &&
669
+ this.isIdentifierChar(this.input[this.pos]!)
670
+ ) {
671
+ this.pos++;
672
+ }
673
+ return this.input.slice(start, this.pos);
674
+ }
675
+
676
+ private skipWhitespace(): void {
677
+ while (this.pos < this.input.length) {
678
+ const ch: string = this.input[this.pos]!;
679
+
680
+ // Skip standard whitespace
681
+ if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
682
+ this.pos++;
683
+ continue;
684
+ }
685
+
686
+ // Skip single-line comment: // ...
687
+ if (
688
+ ch === "/" &&
689
+ this.pos + 1 < this.input.length &&
690
+ this.input[this.pos + 1] === "/"
691
+ ) {
692
+ this.pos += 2;
693
+ while (
694
+ this.pos < this.input.length &&
695
+ this.input[this.pos] !== "\n" &&
696
+ this.input[this.pos] !== "\r"
697
+ ) {
698
+ this.pos++;
699
+ }
700
+ continue;
701
+ }
702
+
703
+ // Skip multi-line comment: /* ... */
704
+ if (
705
+ ch === "/" &&
706
+ this.pos + 1 < this.input.length &&
707
+ this.input[this.pos + 1] === "*"
708
+ ) {
709
+ this.pos += 2;
710
+ while (this.pos + 1 < this.input.length) {
711
+ if (
712
+ this.input[this.pos] === "*" &&
713
+ this.input[this.pos + 1] === "/"
714
+ ) {
715
+ this.pos += 2;
716
+ break;
717
+ }
718
+ this.pos++;
719
+ }
720
+ // Handle unclosed comment - move to end
721
+ if (this.pos + 1 >= this.input.length) {
722
+ this.pos = this.input.length;
723
+ }
724
+ continue;
725
+ }
726
+
727
+ // Not whitespace or comment
728
+ break;
729
+ }
730
+ }
731
+ }