@ansi-tools/parser 1.0.11 → 1.0.13

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 (48) hide show
  1. package/dist/constants.d.ts +56 -0
  2. package/dist/constants.d.ts.map +1 -0
  3. package/dist/constants.js +55 -0
  4. package/dist/escaped.d.ts +6 -10
  5. package/dist/escaped.d.ts.map +1 -0
  6. package/dist/escaped.js +4 -279
  7. package/dist/index.d.ts +5 -7
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +3 -3
  10. package/dist/parse.d.ts +4 -0
  11. package/dist/parse.d.ts.map +1 -0
  12. package/dist/parse.escaped.d.ts +3 -0
  13. package/dist/parse.escaped.d.ts.map +1 -0
  14. package/dist/parse.escaped.js +5 -0
  15. package/dist/parse.js +109 -0
  16. package/dist/parsers/apc.d.ts +3 -0
  17. package/dist/parsers/apc.d.ts.map +1 -0
  18. package/dist/parsers/apc.js +6 -0
  19. package/dist/parsers/csi.d.ts +3 -0
  20. package/dist/parsers/csi.d.ts.map +1 -0
  21. package/dist/parsers/csi.js +63 -0
  22. package/dist/parsers/dcs.d.ts +3 -0
  23. package/dist/parsers/dcs.d.ts.map +1 -0
  24. package/dist/parsers/dcs.js +26 -0
  25. package/dist/parsers/esc.d.ts +3 -0
  26. package/dist/parsers/esc.d.ts.map +1 -0
  27. package/dist/parsers/esc.js +8 -0
  28. package/dist/parsers/osc.d.ts +3 -0
  29. package/dist/parsers/osc.d.ts.map +1 -0
  30. package/dist/parsers/osc.js +28 -0
  31. package/dist/parsers/pm.d.ts +3 -0
  32. package/dist/parsers/pm.d.ts.map +1 -0
  33. package/dist/parsers/pm.js +6 -0
  34. package/dist/parsers/sos.d.ts +3 -0
  35. package/dist/parsers/sos.d.ts.map +1 -0
  36. package/dist/parsers/sos.js +6 -0
  37. package/dist/tokenize.d.ts +4 -0
  38. package/dist/tokenize.d.ts.map +1 -0
  39. package/dist/tokenize.escaped.d.ts +4 -0
  40. package/dist/tokenize.escaped.d.ts.map +1 -0
  41. package/dist/tokenize.escaped.js +295 -0
  42. package/dist/tokenize.js +314 -0
  43. package/dist/types.d.ts +23 -0
  44. package/dist/types.d.ts.map +1 -0
  45. package/dist/types.js +1 -0
  46. package/package.json +27 -28
  47. package/dist/parse-BzJl6pBN.d.ts +0 -85
  48. package/dist/parse-DBLatt5c.js +0 -532
@@ -1,532 +0,0 @@
1
- //#region src/constants.ts
2
- const BELL = 7;
3
- const BELL_CODE = String.fromCharCode(BELL);
4
- const CAN = 24;
5
- const CAN_CODE = String.fromCharCode(CAN);
6
- const SUB = 26;
7
- const SUB_CODE = String.fromCharCode(SUB);
8
- const ESC = 27;
9
- const ESC_CODE = String.fromCharCode(ESC);
10
- const BACKSLASH = 92;
11
- const BACKSLASH_CODE = String.fromCharCode(BACKSLASH);
12
- const DCS = 144;
13
- const DCS_CODE = String.fromCharCode(DCS);
14
- const SOS = 152;
15
- const SOS_CODE = String.fromCharCode(SOS);
16
- const CSI = 155;
17
- const CSI_CODE = String.fromCharCode(CSI);
18
- const ST = 156;
19
- const ST_CODE = String.fromCharCode(ST);
20
- const OSC = 157;
21
- const OSC_CODE = String.fromCharCode(OSC);
22
- const PM = 158;
23
- const PM_CODE = String.fromCharCode(PM);
24
- const APC = 159;
25
- const APC_CODE = String.fromCharCode(APC);
26
- const CSI_OPEN = "[".charCodeAt(0);
27
- const CSI_OPEN_CODE = String.fromCharCode(CSI_OPEN);
28
- const OSC_OPEN = "]".charCodeAt(0);
29
- const OSC_OPEN_CODE = String.fromCharCode(OSC_OPEN);
30
- const DEC_OPEN = "?".charCodeAt(0);
31
- const PRIVATE_OPENERS = new Set([
32
- "<",
33
- "=",
34
- ">"
35
- ]);
36
- const DCS_OPEN = "P";
37
- const APC_OPEN = "_";
38
- const SOS_OPEN = "^";
39
- const PM_OPEN = "X";
40
- const STRING_OPENERS = new Set([
41
- DCS_OPEN,
42
- APC_OPEN,
43
- SOS_OPEN,
44
- PM_OPEN
45
- ]);
46
- const INTERRUPTERS = new Set([
47
- CAN,
48
- SUB,
49
- ESC,
50
- CSI,
51
- OSC,
52
- DCS,
53
- APC,
54
- PM,
55
- SOS
56
- ]);
57
- const C0_INTERRUPTERS = new Set([CAN, SUB]);
58
- const PARAM_SEPARATOR = /[;:]/;
59
- const TOKEN_TYPES = {
60
- TEXT: "TEXT",
61
- INTRODUCER: "INTRODUCER",
62
- DATA: "DATA",
63
- FINAL: "FINAL"
64
- };
65
- const CODE_TYPES = {
66
- CSI: "CSI",
67
- DCS: "DCS",
68
- DEC: "DEC",
69
- ESC: "ESC",
70
- OSC: "OSC",
71
- PRIVATE: "PRIVATE",
72
- SGR: "SGR",
73
- STRING: "STRING",
74
- TEXT: "TEXT"
75
- };
76
-
77
- //#endregion
78
- //#region src/parsers/csi.ts
79
- function parseCSI(introducer, dataTokens, final) {
80
- const data = dataTokens.map((t) => t.raw).join("");
81
- const raw = introducer.raw + data + (final?.raw || "");
82
- const params = [];
83
- let type = CODE_TYPES.CSI;
84
- let intermediates = "";
85
- let paramSection = "";
86
- if (data) {
87
- let i = 0;
88
- while (i < data.length) {
89
- const charCode = data.charCodeAt(i);
90
- if (charCode >= 48 && charCode <= 63) {
91
- paramSection += data[i];
92
- i++;
93
- } else break;
94
- }
95
- intermediates = data.slice(i);
96
- }
97
- if (paramSection) for (const part of paramSection.split(PARAM_SEPARATOR)) params.push(part || "0");
98
- const command = intermediates + (final?.raw ?? "");
99
- const start = params[0];
100
- if (start?.startsWith("?")) {
101
- type = CODE_TYPES.DEC;
102
- if (start.length > 1) params[0] = start.slice(1);
103
- else params.shift();
104
- return {
105
- type,
106
- pos: introducer.pos,
107
- raw,
108
- command,
109
- params
110
- };
111
- }
112
- for (const param of params) if (param && param.length > 0 && PRIVATE_OPENERS.has(param[0])) {
113
- type = CODE_TYPES.PRIVATE;
114
- const privateCommand = param[0] + command;
115
- for (let i = 0; i < params.length; i++) if (params[i] && params[i].length > 0 && PRIVATE_OPENERS.has(params[i][0])) {
116
- if (params[i].length > 1) params[i] = params[i].slice(1);
117
- else params.splice(i, 1);
118
- break;
119
- }
120
- return {
121
- type,
122
- pos: introducer.pos,
123
- raw,
124
- command: privateCommand,
125
- params
126
- };
127
- }
128
- if (command === "m" && params.length === 5 && params[1] === "2" && (start === "38" || start === "48")) params.splice(2, 0, "0");
129
- if (command === "r" && params.length === 2 && params[1] === "0") params[1] = "-1";
130
- return {
131
- type,
132
- pos: introducer.pos,
133
- raw,
134
- command,
135
- params
136
- };
137
- }
138
-
139
- //#endregion
140
- //#region src/parsers/dcs.ts
141
- const DCS_PATTERNS = new Map([
142
- ["$q", 2],
143
- ["+q", 2],
144
- ["+p", 2],
145
- ["|", 1],
146
- ["{", 1]
147
- ]);
148
- function parseDCS(introducer, dataTokens, final) {
149
- const data = dataTokens.map((t) => t.raw).join("");
150
- const raw = introducer.raw + data + (final?.raw ?? "");
151
- if (!data) return {
152
- type: CODE_TYPES.DCS,
153
- pos: introducer.pos,
154
- raw,
155
- command: "",
156
- params: []
157
- };
158
- for (const [pattern, length] of DCS_PATTERNS) if (data.startsWith(pattern)) {
159
- const remainder = data.slice(length);
160
- const params = [];
161
- if (remainder) for (const part of remainder.split(PARAM_SEPARATOR)) params.push(part || "-1");
162
- return {
163
- type: CODE_TYPES.DCS,
164
- pos: introducer.pos,
165
- raw,
166
- command: pattern,
167
- params
168
- };
169
- }
170
- return {
171
- type: CODE_TYPES.DCS,
172
- pos: introducer.pos,
173
- raw,
174
- command: "",
175
- params: [data]
176
- };
177
- }
178
-
179
- //#endregion
180
- //#region src/parsers/esc.ts
181
- function parseESC(introducer, dataTokens, final) {
182
- const data = dataTokens.map((t) => t.raw).join("");
183
- const command = introducer.intermediate || (dataTokens[0]?.raw ?? final?.raw ?? "");
184
- const params = introducer.intermediate ? final?.raw ? [final.raw] : [] : [];
185
- const raw = introducer.raw + data + (final?.raw ?? "");
186
- return {
187
- type: CODE_TYPES.ESC,
188
- pos: introducer.pos,
189
- raw,
190
- command,
191
- params
192
- };
193
- }
194
-
195
- //#endregion
196
- //#region src/parsers/osc.ts
197
- function parseOSC(introducer, dataTokens, final) {
198
- const data = dataTokens.map((t) => t.raw).join("");
199
- const raw = introducer.raw + data + (final?.raw || "");
200
- const semicolonIndex = data.indexOf(";");
201
- if (semicolonIndex === -1) return {
202
- type: CODE_TYPES.OSC,
203
- pos: introducer.pos,
204
- raw,
205
- command: data,
206
- params: []
207
- };
208
- const command = data.slice(0, semicolonIndex);
209
- const remainder = data.slice(semicolonIndex + 1);
210
- if (command === "1337") return {
211
- type: CODE_TYPES.OSC,
212
- pos: introducer.pos,
213
- raw,
214
- command,
215
- params: [remainder]
216
- };
217
- const params = [];
218
- if (remainder) {
219
- let current = "";
220
- for (let i = 0; i < remainder.length; i++) if (remainder[i] === ";") {
221
- params.push(current);
222
- current = "";
223
- } else current += remainder[i];
224
- params.push(current);
225
- }
226
- return {
227
- type: CODE_TYPES.OSC,
228
- pos: introducer.pos,
229
- raw,
230
- command,
231
- params
232
- };
233
- }
234
-
235
- //#endregion
236
- //#region src/parsers/apc.ts
237
- function parseAPC(introducer, dataTokens, final) {
238
- const data = dataTokens.map((t) => t.raw).join("");
239
- const raw = introducer.raw + data + (final?.raw || "");
240
- return {
241
- type: CODE_TYPES.STRING,
242
- pos: introducer.pos,
243
- raw,
244
- command: "APC",
245
- params: data ? [data] : []
246
- };
247
- }
248
-
249
- //#endregion
250
- //#region src/parsers/pm.ts
251
- function parsePM(introducer, dataTokens, final) {
252
- const data = dataTokens.map((t) => t.raw).join("");
253
- const raw = introducer.raw + data + (final?.raw || "");
254
- return {
255
- type: CODE_TYPES.STRING,
256
- pos: introducer.pos,
257
- raw,
258
- command: "PM",
259
- params: data ? [data] : []
260
- };
261
- }
262
-
263
- //#endregion
264
- //#region src/parsers/sos.ts
265
- function parseSOS(introducer, dataTokens, final) {
266
- const data = dataTokens.map((t) => t.raw).join("");
267
- const raw = introducer.raw + data + (final?.raw || "");
268
- return {
269
- type: CODE_TYPES.STRING,
270
- pos: introducer.pos,
271
- raw,
272
- command: "SOS",
273
- params: data ? [data] : []
274
- };
275
- }
276
-
277
- //#endregion
278
- //#region src/tokenize.ts
279
- function emit$1(token) {
280
- return token;
281
- }
282
- function* tokenizer(input) {
283
- let i = 0;
284
- let state = "GROUND";
285
- let currentCode;
286
- function setState(next, code) {
287
- state = next;
288
- currentCode = code;
289
- }
290
- while (i < input.length) if (state === "GROUND") {
291
- const textStart = i;
292
- let charCode = input.charCodeAt(i);
293
- let char = input[i];
294
- while (i < input.length) {
295
- if (charCode === ESC || charCode === CSI || charCode === OSC || charCode === DCS || charCode === APC || charCode === PM || charCode === SOS) break;
296
- i++;
297
- charCode = input.charCodeAt(i);
298
- char = input[i];
299
- }
300
- if (i > textStart) yield emit$1({
301
- type: TOKEN_TYPES.TEXT,
302
- pos: textStart,
303
- raw: input.substring(textStart, i)
304
- });
305
- if (i < input.length) {
306
- if (charCode === CSI || charCode === OSC || charCode === DCS || charCode === APC || charCode === PM || charCode === SOS) {
307
- yield emit$1({
308
- type: TOKEN_TYPES.INTRODUCER,
309
- pos: i,
310
- raw: char,
311
- code: char
312
- });
313
- i++;
314
- setState("SEQUENCE", charCode);
315
- } else if (charCode === ESC) {
316
- const next = input[i + 1];
317
- const nextCode = input.charCodeAt(i + 1);
318
- if (nextCode === CSI_OPEN) {
319
- yield emit$1({
320
- type: TOKEN_TYPES.INTRODUCER,
321
- pos: i,
322
- raw: char + next,
323
- code: CSI_CODE
324
- });
325
- i += 2;
326
- setState("SEQUENCE", CSI);
327
- } else if (nextCode === OSC_OPEN) {
328
- yield emit$1({
329
- type: TOKEN_TYPES.INTRODUCER,
330
- pos: i,
331
- raw: char + next,
332
- code: OSC_CODE
333
- });
334
- i += 2;
335
- setState("SEQUENCE", OSC);
336
- } else if (STRING_OPENERS.has(next)) {
337
- yield emit$1({
338
- type: TOKEN_TYPES.INTRODUCER,
339
- pos: i,
340
- raw: char + next,
341
- code: next
342
- });
343
- i += 2;
344
- setState("SEQUENCE", nextCode);
345
- } else if (next) {
346
- let j = i + 1;
347
- while (j < input.length && input.charCodeAt(j) >= 32 && input.charCodeAt(j) <= 47) j++;
348
- if (j < input.length) {
349
- const is = input.slice(i + 1, j);
350
- if (is) yield emit$1({
351
- type: TOKEN_TYPES.INTRODUCER,
352
- pos: i,
353
- raw: char + is,
354
- code: ESC_CODE,
355
- intermediate: is
356
- });
357
- else yield emit$1({
358
- type: TOKEN_TYPES.INTRODUCER,
359
- pos: i,
360
- raw: char,
361
- code: ESC_CODE
362
- });
363
- i = j;
364
- setState("SEQUENCE", ESC);
365
- } else i = j;
366
- } else i++;
367
- }
368
- }
369
- } else if (state === "SEQUENCE") {
370
- const pos = i;
371
- const code = currentCode;
372
- let data = "";
373
- if (code === CSI) while (i < input.length) {
374
- const charCode = input.charCodeAt(i);
375
- const char = input[i];
376
- if (INTERRUPTERS.has(charCode)) {
377
- if (data) yield emit$1({
378
- type: TOKEN_TYPES.DATA,
379
- pos,
380
- raw: data
381
- });
382
- setState("GROUND");
383
- if (C0_INTERRUPTERS.has(charCode)) i++;
384
- break;
385
- }
386
- if (charCode >= 64 && charCode <= 126) {
387
- if (data) yield emit$1({
388
- type: TOKEN_TYPES.DATA,
389
- pos,
390
- raw: data
391
- });
392
- yield emit$1({
393
- type: TOKEN_TYPES.FINAL,
394
- pos: i,
395
- raw: char
396
- });
397
- i++;
398
- setState("GROUND");
399
- break;
400
- }
401
- data += char;
402
- i++;
403
- }
404
- else if (code === ESC) {
405
- if (i < input.length) {
406
- const charCode = input.charCodeAt(i);
407
- const char = input[i];
408
- if (INTERRUPTERS.has(charCode)) {
409
- setState("GROUND");
410
- if (C0_INTERRUPTERS.has(charCode)) i++;
411
- } else {
412
- yield emit$1({
413
- type: TOKEN_TYPES.FINAL,
414
- pos: i,
415
- raw: char
416
- });
417
- i++;
418
- setState("GROUND");
419
- }
420
- }
421
- } else if (code) while (i < input.length) {
422
- const char = input[i];
423
- const charCode = char.charCodeAt(0);
424
- let terminator;
425
- if (charCode === ESC && input.charCodeAt(i + 1) === BACKSLASH) terminator = ESC_CODE + BACKSLASH_CODE;
426
- else if (charCode === ST) terminator = ST_CODE;
427
- else if (charCode === BELL && code === OSC) terminator = BELL_CODE;
428
- if (terminator) {
429
- if (data) yield emit$1({
430
- type: TOKEN_TYPES.DATA,
431
- pos,
432
- raw: data
433
- });
434
- yield emit$1({
435
- type: TOKEN_TYPES.FINAL,
436
- pos: i,
437
- raw: terminator
438
- });
439
- i += terminator.length;
440
- setState("GROUND");
441
- break;
442
- }
443
- if (INTERRUPTERS.has(charCode)) {
444
- if (data) yield emit$1({
445
- type: TOKEN_TYPES.DATA,
446
- pos,
447
- raw: data
448
- });
449
- setState("GROUND");
450
- if (C0_INTERRUPTERS.has(charCode)) i++;
451
- break;
452
- }
453
- data += char;
454
- i++;
455
- }
456
- if (state === "SEQUENCE") setState("GROUND");
457
- }
458
- }
459
- function tokenize(input) {
460
- return Array.from(tokenizer(input));
461
- }
462
-
463
- //#endregion
464
- //#region src/parse.ts
465
- function emit(token) {
466
- return token;
467
- }
468
- function* parser(tokens) {
469
- let current = tokens.next();
470
- while (!current.done) {
471
- const token = current.value;
472
- if (token.type === TOKEN_TYPES.TEXT) {
473
- yield emit({
474
- type: CODE_TYPES.TEXT,
475
- pos: token.pos,
476
- raw: token.raw
477
- });
478
- current = tokens.next();
479
- continue;
480
- }
481
- if (token.type === TOKEN_TYPES.INTRODUCER) {
482
- const introducer = token;
483
- const data = [];
484
- let final;
485
- current = tokens.next();
486
- while (!current.done) {
487
- const nextToken = current.value;
488
- if (nextToken.type === TOKEN_TYPES.DATA) data.push(nextToken);
489
- else if (nextToken.type === TOKEN_TYPES.FINAL) {
490
- final = nextToken;
491
- current = tokens.next();
492
- break;
493
- } else if (nextToken.type === TOKEN_TYPES.INTRODUCER) break;
494
- else if (nextToken.type === TOKEN_TYPES.TEXT) break;
495
- current = tokens.next();
496
- }
497
- switch (introducer.code) {
498
- case CSI_CODE:
499
- yield emit(parseCSI(introducer, data, final));
500
- break;
501
- case OSC_CODE:
502
- yield emit(parseOSC(introducer, data, final));
503
- break;
504
- case DCS_CODE:
505
- case DCS_OPEN:
506
- yield emit(parseDCS(introducer, data, final));
507
- break;
508
- case APC_CODE:
509
- case APC_OPEN:
510
- yield emit(parseAPC(introducer, data, final));
511
- break;
512
- case PM_CODE:
513
- case PM_OPEN:
514
- yield emit(parsePM(introducer, data, final));
515
- break;
516
- case SOS_CODE:
517
- case SOS_OPEN:
518
- yield emit(parseSOS(introducer, data, final));
519
- break;
520
- case ESC_CODE:
521
- yield emit(parseESC(introducer, data, final));
522
- break;
523
- }
524
- } else current = tokens.next();
525
- }
526
- }
527
- function parse(input) {
528
- return Array.from(parser(tokenizer(input)));
529
- }
530
-
531
- //#endregion
532
- export { APC, APC_CODE, APC_OPEN, BACKSLASH, BACKSLASH_CODE, BELL, BELL_CODE, C0_INTERRUPTERS, CAN, CAN_CODE, CODE_TYPES, CSI, CSI_CODE, CSI_OPEN, CSI_OPEN_CODE, DCS, DCS_CODE, DCS_OPEN, DEC_OPEN, ESC, ESC_CODE, INTERRUPTERS, OSC, OSC_CODE, OSC_OPEN, OSC_OPEN_CODE, PARAM_SEPARATOR, PM, PM_CODE, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_CODE, SOS_OPEN, ST, STRING_OPENERS, ST_CODE, SUB, SUB_CODE, TOKEN_TYPES, parse, parser, tokenize, tokenizer };