@ansi-tools/parser 1.0.10 → 1.0.12

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.
package/dist/escaped.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE, CODE_TYPES, CONTROL_CODE, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TEXT, TOKEN, TOKEN_TYPES, parser } from "./parse-DV2rRazL.js";
1
+ import { APC, APC_CODE, APC_OPEN, BACKSLASH, BACKSLASH_CODE, BELL, BELL_CODE, C0_INTERRUPTERS, CAN, CAN_CODE, CODE, CODE_TYPES, CONTROL_CODE, 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, TEXT, TOKEN, TOKEN_TYPES, parser } from "./parse-BzJl6pBN.js";
2
2
 
3
3
  //#region src/parse.escaped.d.ts
4
4
  declare function parse(input: string): CODE[];
@@ -7,4 +7,4 @@ declare function parse(input: string): CODE[];
7
7
  declare function tokenizer(input: string): IterableIterator<TOKEN>;
8
8
  declare function tokenize(input: string): TOKEN[];
9
9
  //#endregion
10
- export { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE, CODE_TYPES, CONTROL_CODE, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TEXT, TOKEN, TOKEN_TYPES, parse, parser, tokenize, tokenizer };
10
+ export { APC, APC_CODE, APC_OPEN, BACKSLASH, BACKSLASH_CODE, BELL, BELL_CODE, C0_INTERRUPTERS, CAN, CAN_CODE, type CODE, CODE_TYPES, type CONTROL_CODE, 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, type TEXT, type TOKEN, TOKEN_TYPES, parse, parser, tokenize, tokenizer };
package/dist/escaped.js CHANGED
@@ -1,4 +1,4 @@
1
- import { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE_TYPES, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TOKEN_TYPES, parser } from "./parse-DmFIqYg6.js";
1
+ import { 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, parser } from "./parse-BCjxFvJh.js";
2
2
 
3
3
  //#region src/tokenize.escaped.ts
4
4
  const CSI_ESCAPED = "\\u009b";
@@ -40,6 +40,7 @@ function* tokenizer(input) {
40
40
  let i = 0;
41
41
  let state = "GROUND";
42
42
  let currentCode;
43
+ let backslashIndex = input.indexOf(BACKSLASH_CODE);
43
44
  function setState(next, code) {
44
45
  state = next;
45
46
  currentCode = code;
@@ -47,7 +48,11 @@ function* tokenizer(input) {
47
48
  while (i < l) if (state === "GROUND") {
48
49
  const textStart = i;
49
50
  while (i < l) {
50
- const backslashIndex = input.indexOf(BACKSLASH, i);
51
+ if (backslashIndex === -1) {
52
+ i = l;
53
+ break;
54
+ }
55
+ if (backslashIndex < i) backslashIndex = input.indexOf(BACKSLASH_CODE, i);
51
56
  if (backslashIndex === -1) {
52
57
  i = l;
53
58
  break;
@@ -56,12 +61,7 @@ function* tokenizer(input) {
56
61
  const candidates = INTRODUCER_LOOKUP.get(input[backslashIndex + 1]);
57
62
  if (candidates) for (const [seq, len] of candidates) {
58
63
  if (backslashIndex + len > l) continue;
59
- let matched = true;
60
- for (let k = 0; k < len && matched; k += 2) {
61
- matched = input[backslashIndex + k] === seq[k];
62
- if (matched && k + 1 < len) matched = input[backslashIndex + k + 1] === seq[k + 1];
63
- }
64
- if (matched) {
64
+ if (input.startsWith(seq, backslashIndex)) {
65
65
  isIntroducer = true;
66
66
  break;
67
67
  }
@@ -82,39 +82,34 @@ function* tokenizer(input) {
82
82
  let isMatch = false;
83
83
  for (const [seq, len] of candidates) {
84
84
  if (i + len > l) continue;
85
- let isSeqMatch = true;
86
- for (let k = 0; k < len && isSeqMatch; k += 2) {
87
- isSeqMatch = input[i + k] === seq[k];
88
- if (isSeqMatch && k + 1 < len) isSeqMatch = input[i + k + 1] === seq[k + 1];
89
- }
90
- if (isSeqMatch) {
85
+ if (input.startsWith(seq, i)) {
91
86
  isMatch = true;
92
87
  if (seq === CSI_ESCAPED || seq === CSI_ESCAPED_HEX) {
93
88
  yield emit({
94
89
  type: TOKEN_TYPES.INTRODUCER,
95
90
  pos: i,
96
91
  raw: seq,
97
- code: CSI
92
+ code: CSI_CODE
98
93
  });
99
94
  i += len;
100
95
  setState("SEQUENCE", CSI);
101
96
  } else {
102
97
  const next = input[i + len];
103
- if (next === CSI_OPEN) {
98
+ if (next === CSI_OPEN_CODE) {
104
99
  yield emit({
105
100
  type: TOKEN_TYPES.INTRODUCER,
106
101
  pos: i,
107
102
  raw: seq + next,
108
- code: CSI
103
+ code: CSI_CODE
109
104
  });
110
105
  i += len + 1;
111
106
  setState("SEQUENCE", CSI);
112
- } else if (next === OSC_OPEN) {
107
+ } else if (next === OSC_OPEN_CODE) {
113
108
  yield emit({
114
109
  type: TOKEN_TYPES.INTRODUCER,
115
110
  pos: i,
116
111
  raw: seq + next,
117
- code: OSC
112
+ code: OSC_CODE
118
113
  });
119
114
  i += len + 1;
120
115
  setState("SEQUENCE", OSC);
@@ -126,7 +121,7 @@ function* tokenizer(input) {
126
121
  code: next
127
122
  });
128
123
  i += len + 1;
129
- setState("SEQUENCE", next);
124
+ setState("SEQUENCE", next.charCodeAt(0));
130
125
  } else if (next) {
131
126
  let j = i + len;
132
127
  while (j < l && input.charCodeAt(j) >= 32 && input.charCodeAt(j) <= 47) j++;
@@ -136,14 +131,14 @@ function* tokenizer(input) {
136
131
  type: TOKEN_TYPES.INTRODUCER,
137
132
  pos: i,
138
133
  raw: seq + is,
139
- code: ESC,
134
+ code: ESC_CODE,
140
135
  intermediate: is
141
136
  });
142
137
  else yield emit({
143
138
  type: TOKEN_TYPES.INTRODUCER,
144
139
  pos: i,
145
140
  raw: seq,
146
- code: ESC
141
+ code: ESC_CODE
147
142
  });
148
143
  i = j;
149
144
  setState("SEQUENCE", ESC);
@@ -160,10 +155,8 @@ function* tokenizer(input) {
160
155
  let terminator = "";
161
156
  let terminatorPos = -1;
162
157
  const pos = i;
163
- const code = currentCode;
164
158
  while (!terminator && i < l) {
165
- const char = input[i];
166
- if (char === BACKSLASH) {
159
+ if (input.charCodeAt(i) === BACKSLASH) {
167
160
  const next = input[i + 1];
168
161
  if (next) {
169
162
  const interrupters = INTERRUPTER_LOOKUP.get(next);
@@ -184,9 +177,9 @@ function* tokenizer(input) {
184
177
  }
185
178
  }
186
179
  if (terminator) break;
187
- if (code !== CSI && code !== ESC) {
180
+ if (currentCode !== CSI && currentCode !== ESC) {
188
181
  if (next === "a" && i + 2 <= l) {
189
- if (code === OSC && input[i + 1] === "a") {
182
+ if (currentCode === OSC && input[i + 1] === "a") {
190
183
  terminator = "\\a";
191
184
  terminatorPos = i;
192
185
  i += 2;
@@ -195,7 +188,7 @@ function* tokenizer(input) {
195
188
  if (i + 4 <= l) {
196
189
  const char3 = input[i + 2];
197
190
  const char4 = input[i + 3];
198
- if (char3 === "0" && char4 === "7" && code === OSC) {
191
+ if (char3 === "0" && char4 === "7" && currentCode === OSC) {
199
192
  terminator = "\\x07";
200
193
  terminatorPos = i;
201
194
  i += 4;
@@ -203,20 +196,20 @@ function* tokenizer(input) {
203
196
  terminator = "\\x9c";
204
197
  terminatorPos = i;
205
198
  i += 4;
206
- } else if (char3 === "1" && char4 === "b" && i + 6 <= l && input[i + 4] === BACKSLASH && input[i + 5] === BACKSLASH) {
199
+ } else if (char3 === "1" && char4 === "b" && i + 6 <= l && input.charCodeAt(i + 4) === BACKSLASH && input.charCodeAt(i + 5) === BACKSLASH) {
207
200
  terminator = "\\x1b\\\\";
208
201
  terminatorPos = i;
209
202
  i += 6;
210
203
  }
211
204
  }
212
- } else if (next === "u" && code === OSC && i + 6 <= l) {
205
+ } else if (next === "u" && currentCode === OSC && i + 6 <= l) {
213
206
  if (input[i + 2] === "0" && input[i + 3] === "0" && input[i + 4] === "0" && input[i + 5] === "7") {
214
207
  terminator = "\\u0007";
215
208
  terminatorPos = i;
216
209
  i += 6;
217
210
  }
218
211
  } else if (next === "e" && i + 4 <= l) {
219
- if (input[i + 2] === BACKSLASH && input[i + 3] === BACKSLASH) {
212
+ if (input.charCodeAt(i + 2) === BACKSLASH && input.charCodeAt(i + 3) === BACKSLASH) {
220
213
  terminator = "\\e\\\\";
221
214
  terminatorPos = i;
222
215
  i += 4;
@@ -241,28 +234,25 @@ function* tokenizer(input) {
241
234
  }
242
235
  }
243
236
  }
244
- } else if (code === CSI) {
237
+ } else if (currentCode === CSI) {
245
238
  const charCode = input.charCodeAt(i);
246
239
  if (charCode >= 64 && charCode <= 126) {
247
- terminator = char;
240
+ terminator = input[i];
248
241
  terminatorPos = i;
249
242
  i++;
250
243
  }
251
- } else if (code === ESC) {
252
- terminator = char;
244
+ } else if (currentCode === ESC) {
245
+ terminator = input[i];
253
246
  terminatorPos = i;
254
247
  i++;
255
248
  }
256
249
  if (!terminator) i++;
257
250
  }
258
- if (terminatorPos > pos) {
259
- const data = input.substring(pos, terminatorPos);
260
- yield emit({
261
- type: TOKEN_TYPES.DATA,
262
- pos,
263
- raw: data
264
- });
265
- }
251
+ if (terminatorPos > pos) yield emit({
252
+ type: TOKEN_TYPES.DATA,
253
+ pos,
254
+ raw: input.substring(pos, terminatorPos)
255
+ });
266
256
  if (terminator && terminator !== ABANDONED) yield emit({
267
257
  type: TOKEN_TYPES.FINAL,
268
258
  pos: terminatorPos,
@@ -282,4 +272,4 @@ function parse(input) {
282
272
  }
283
273
 
284
274
  //#endregion
285
- export { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE_TYPES, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TOKEN_TYPES, parse, parser, tokenize, tokenizer };
275
+ 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 };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE, CODE_TYPES, CONTROL_CODE, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TEXT, TOKEN, TOKEN_TYPES, parse, parser } from "./parse-DV2rRazL.js";
1
+ import { APC, APC_CODE, APC_OPEN, BACKSLASH, BACKSLASH_CODE, BELL, BELL_CODE, C0_INTERRUPTERS, CAN, CAN_CODE, CODE, CODE_TYPES, CONTROL_CODE, 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, TEXT, TOKEN, TOKEN_TYPES, parse, parser } from "./parse-BzJl6pBN.js";
2
2
 
3
3
  //#region src/tokenize.d.ts
4
4
  declare function tokenizer(input: string): IterableIterator<TOKEN>;
5
5
  declare function tokenize(input: string): TOKEN[];
6
6
  //#endregion
7
- export { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE, CODE_TYPES, CONTROL_CODE, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TEXT, TOKEN, TOKEN_TYPES, parse, parser, tokenize, tokenizer };
7
+ export { APC, APC_CODE, APC_OPEN, BACKSLASH, BACKSLASH_CODE, BELL, BELL_CODE, C0_INTERRUPTERS, CAN, CAN_CODE, type CODE, CODE_TYPES, type CONTROL_CODE, 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, type TEXT, type TOKEN, TOKEN_TYPES, parse, parser, tokenize, tokenizer };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE_TYPES, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TOKEN_TYPES, parse, parser, tokenize, tokenizer } from "./parse-DmFIqYg6.js";
1
+ import { 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 } from "./parse-BCjxFvJh.js";
2
2
 
3
- export { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE_TYPES, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TOKEN_TYPES, parse, parser, tokenize, tokenizer };
3
+ 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 };
@@ -1,19 +1,33 @@
1
1
  //#region src/constants.ts
2
- const BELL = String.fromCharCode(7);
3
- const CAN = String.fromCharCode(24);
4
- const SUB = String.fromCharCode(26);
5
- const ESC = String.fromCharCode(27);
6
- const BACKSLASH = String.fromCharCode(92);
7
- const DCS = String.fromCharCode(144);
8
- const SOS = String.fromCharCode(152);
9
- const CSI = String.fromCharCode(155);
10
- const ST = String.fromCharCode(156);
11
- const OSC = String.fromCharCode(157);
12
- const PM = String.fromCharCode(158);
13
- const APC = String.fromCharCode(159);
14
- const CSI_OPEN = "[";
15
- const OSC_OPEN = "]";
16
- const DEC_OPEN = "?";
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);
17
31
  const PRIVATE_OPENERS = new Set([
18
32
  "<",
19
33
  "=",
@@ -60,6 +74,20 @@ const CODE_TYPES = {
60
74
  TEXT: "TEXT"
61
75
  };
62
76
 
77
+ //#endregion
78
+ //#region src/parsers/apc.ts
79
+ function parseAPC(introducer, dataTokens, final) {
80
+ const data = dataTokens.map((t) => t.raw).join("");
81
+ const raw = introducer.raw + data + (final?.raw || "");
82
+ return {
83
+ type: CODE_TYPES.STRING,
84
+ pos: introducer.pos,
85
+ raw,
86
+ command: "APC",
87
+ params: data ? [data] : []
88
+ };
89
+ }
90
+
63
91
  //#endregion
64
92
  //#region src/parsers/csi.ts
65
93
  function parseCSI(introducer, dataTokens, final) {
@@ -218,20 +246,6 @@ function parseOSC(introducer, dataTokens, final) {
218
246
  };
219
247
  }
220
248
 
221
- //#endregion
222
- //#region src/parsers/apc.ts
223
- function parseAPC(introducer, dataTokens, final) {
224
- const data = dataTokens.map((t) => t.raw).join("");
225
- const raw = introducer.raw + data + (final?.raw || "");
226
- return {
227
- type: CODE_TYPES.STRING,
228
- pos: introducer.pos,
229
- raw,
230
- command: "APC",
231
- params: data ? [data] : []
232
- };
233
- }
234
-
235
249
  //#endregion
236
250
  //#region src/parsers/pm.ts
237
251
  function parsePM(introducer, dataTokens, final) {
@@ -262,15 +276,6 @@ function parseSOS(introducer, dataTokens, final) {
262
276
 
263
277
  //#endregion
264
278
  //#region src/tokenize.ts
265
- const INTRODUCERS = new Set([
266
- ESC,
267
- CSI,
268
- OSC,
269
- DCS,
270
- APC,
271
- PM,
272
- SOS
273
- ]);
274
279
  function emit$1(token) {
275
280
  return token;
276
281
  }
@@ -284,10 +289,13 @@ function* tokenizer(input) {
284
289
  }
285
290
  while (i < input.length) if (state === "GROUND") {
286
291
  const textStart = i;
292
+ let charCode = input.charCodeAt(i);
293
+ let char = input[i];
287
294
  while (i < input.length) {
288
- const char = input[i];
289
- if (INTRODUCERS.has(char)) break;
295
+ if (charCode === ESC || charCode === CSI || charCode === OSC || charCode === DCS || charCode === APC || charCode === PM || charCode === SOS) break;
290
296
  i++;
297
+ charCode = input.charCodeAt(i);
298
+ char = input[i];
291
299
  }
292
300
  if (i > textStart) yield emit$1({
293
301
  type: TOKEN_TYPES.TEXT,
@@ -295,8 +303,7 @@ function* tokenizer(input) {
295
303
  raw: input.substring(textStart, i)
296
304
  });
297
305
  if (i < input.length) {
298
- const char = input[i];
299
- if (char === CSI || char === OSC || char === DCS || char === APC || char === PM || char === SOS) {
306
+ if (charCode === CSI || charCode === OSC || charCode === DCS || charCode === APC || charCode === PM || charCode === SOS) {
300
307
  yield emit$1({
301
308
  type: TOKEN_TYPES.INTRODUCER,
302
309
  pos: i,
@@ -304,24 +311,25 @@ function* tokenizer(input) {
304
311
  code: char
305
312
  });
306
313
  i++;
307
- setState("SEQUENCE", char);
308
- } else if (char === ESC) {
314
+ setState("SEQUENCE", charCode);
315
+ } else if (charCode === ESC) {
309
316
  const next = input[i + 1];
310
- if (next === CSI_OPEN) {
317
+ const nextCode = input.charCodeAt(i + 1);
318
+ if (nextCode === CSI_OPEN) {
311
319
  yield emit$1({
312
320
  type: TOKEN_TYPES.INTRODUCER,
313
321
  pos: i,
314
322
  raw: char + next,
315
- code: CSI
323
+ code: CSI_CODE
316
324
  });
317
325
  i += 2;
318
326
  setState("SEQUENCE", CSI);
319
- } else if (next === OSC_OPEN) {
327
+ } else if (nextCode === OSC_OPEN) {
320
328
  yield emit$1({
321
329
  type: TOKEN_TYPES.INTRODUCER,
322
330
  pos: i,
323
331
  raw: char + next,
324
- code: OSC
332
+ code: OSC_CODE
325
333
  });
326
334
  i += 2;
327
335
  setState("SEQUENCE", OSC);
@@ -333,7 +341,7 @@ function* tokenizer(input) {
333
341
  code: next
334
342
  });
335
343
  i += 2;
336
- setState("SEQUENCE", next);
344
+ setState("SEQUENCE", nextCode);
337
345
  } else if (next) {
338
346
  let j = i + 1;
339
347
  while (j < input.length && input.charCodeAt(j) >= 32 && input.charCodeAt(j) <= 47) j++;
@@ -343,14 +351,14 @@ function* tokenizer(input) {
343
351
  type: TOKEN_TYPES.INTRODUCER,
344
352
  pos: i,
345
353
  raw: char + is,
346
- code: ESC,
354
+ code: ESC_CODE,
347
355
  intermediate: is
348
356
  });
349
357
  else yield emit$1({
350
358
  type: TOKEN_TYPES.INTRODUCER,
351
359
  pos: i,
352
360
  raw: char,
353
- code: ESC
361
+ code: ESC_CODE
354
362
  });
355
363
  i = j;
356
364
  setState("SEQUENCE", ESC);
@@ -360,21 +368,20 @@ function* tokenizer(input) {
360
368
  }
361
369
  } else if (state === "SEQUENCE") {
362
370
  const pos = i;
363
- const code = currentCode;
364
371
  let data = "";
365
- if (code === CSI) while (i < input.length) {
372
+ if (currentCode === CSI) while (i < input.length) {
373
+ const charCode = input.charCodeAt(i);
366
374
  const char = input[i];
367
- if (INTERRUPTERS.has(char)) {
375
+ if (INTERRUPTERS.has(charCode)) {
368
376
  if (data) yield emit$1({
369
377
  type: TOKEN_TYPES.DATA,
370
378
  pos,
371
379
  raw: data
372
380
  });
373
381
  setState("GROUND");
374
- if (C0_INTERRUPTERS.has(char)) i++;
382
+ if (C0_INTERRUPTERS.has(charCode)) i++;
375
383
  break;
376
384
  }
377
- const charCode = char.charCodeAt(0);
378
385
  if (charCode >= 64 && charCode <= 126) {
379
386
  if (data) yield emit$1({
380
387
  type: TOKEN_TYPES.DATA,
@@ -393,28 +400,29 @@ function* tokenizer(input) {
393
400
  data += char;
394
401
  i++;
395
402
  }
396
- else if (code === ESC) {
403
+ else if (currentCode === ESC) {
397
404
  if (i < input.length) {
398
- const char = input[i];
399
- if (INTERRUPTERS.has(char)) {
405
+ const charCode = input.charCodeAt(i);
406
+ if (INTERRUPTERS.has(charCode)) {
400
407
  setState("GROUND");
401
- if (C0_INTERRUPTERS.has(char)) i++;
408
+ if (C0_INTERRUPTERS.has(charCode)) i++;
402
409
  } else {
403
410
  yield emit$1({
404
411
  type: TOKEN_TYPES.FINAL,
405
412
  pos: i,
406
- raw: char
413
+ raw: input[i]
407
414
  });
408
415
  i++;
409
416
  setState("GROUND");
410
417
  }
411
418
  }
412
- } else if (code) while (i < input.length) {
419
+ } else if (currentCode) while (i < input.length) {
413
420
  const char = input[i];
421
+ const charCode = char.charCodeAt(0);
414
422
  let terminator;
415
- if (char === ESC && input[i + 1] === BACKSLASH) terminator = ESC + BACKSLASH;
416
- else if (char === ST) terminator = ST;
417
- else if (char === BELL && code === OSC) terminator = BELL;
423
+ if (charCode === ESC && input.charCodeAt(i + 1) === BACKSLASH) terminator = ESC_CODE + BACKSLASH_CODE;
424
+ else if (charCode === ST) terminator = ST_CODE;
425
+ else if (charCode === BELL && currentCode === OSC) terminator = BELL_CODE;
418
426
  if (terminator) {
419
427
  if (data) yield emit$1({
420
428
  type: TOKEN_TYPES.DATA,
@@ -430,14 +438,14 @@ function* tokenizer(input) {
430
438
  setState("GROUND");
431
439
  break;
432
440
  }
433
- if (INTERRUPTERS.has(char)) {
441
+ if (INTERRUPTERS.has(charCode)) {
434
442
  if (data) yield emit$1({
435
443
  type: TOKEN_TYPES.DATA,
436
444
  pos,
437
445
  raw: data
438
446
  });
439
447
  setState("GROUND");
440
- if (C0_INTERRUPTERS.has(char)) i++;
448
+ if (C0_INTERRUPTERS.has(charCode)) i++;
441
449
  break;
442
450
  }
443
451
  data += char;
@@ -485,29 +493,29 @@ function* parser(tokens) {
485
493
  current = tokens.next();
486
494
  }
487
495
  switch (introducer.code) {
488
- case CSI:
496
+ case CSI_CODE:
489
497
  yield emit(parseCSI(introducer, data, final));
490
498
  break;
491
- case OSC:
499
+ case OSC_CODE:
492
500
  yield emit(parseOSC(introducer, data, final));
493
501
  break;
494
- case DCS:
502
+ case DCS_CODE:
495
503
  case DCS_OPEN:
496
504
  yield emit(parseDCS(introducer, data, final));
497
505
  break;
498
- case APC:
506
+ case APC_CODE:
499
507
  case APC_OPEN:
500
508
  yield emit(parseAPC(introducer, data, final));
501
509
  break;
502
- case PM:
510
+ case PM_CODE:
503
511
  case PM_OPEN:
504
512
  yield emit(parsePM(introducer, data, final));
505
513
  break;
506
- case SOS:
514
+ case SOS_CODE:
507
515
  case SOS_OPEN:
508
516
  yield emit(parseSOS(introducer, data, final));
509
517
  break;
510
- case ESC:
518
+ case ESC_CODE:
511
519
  yield emit(parseESC(introducer, data, final));
512
520
  break;
513
521
  }
@@ -519,4 +527,4 @@ function parse(input) {
519
527
  }
520
528
 
521
529
  //#endregion
522
- export { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE_TYPES, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TOKEN_TYPES, parse, parser, tokenize, tokenizer };
530
+ 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 };
@@ -0,0 +1,85 @@
1
+ //#region src/constants.d.ts
2
+ declare const BELL = 7;
3
+ declare const BELL_CODE: string;
4
+ declare const CAN = 24;
5
+ declare const CAN_CODE: string;
6
+ declare const SUB = 26;
7
+ declare const SUB_CODE: string;
8
+ declare const ESC = 27;
9
+ declare const ESC_CODE: string;
10
+ declare const BACKSLASH = 92;
11
+ declare const BACKSLASH_CODE: string;
12
+ declare const DCS = 144;
13
+ declare const DCS_CODE: string;
14
+ declare const SOS = 152;
15
+ declare const SOS_CODE: string;
16
+ declare const CSI = 155;
17
+ declare const CSI_CODE: string;
18
+ declare const ST = 156;
19
+ declare const ST_CODE: string;
20
+ declare const OSC = 157;
21
+ declare const OSC_CODE: string;
22
+ declare const PM = 158;
23
+ declare const PM_CODE: string;
24
+ declare const APC = 159;
25
+ declare const APC_CODE: string;
26
+ declare const CSI_OPEN: number;
27
+ declare const CSI_OPEN_CODE: string;
28
+ declare const OSC_OPEN: number;
29
+ declare const OSC_OPEN_CODE: string;
30
+ declare const DEC_OPEN: number;
31
+ declare const PRIVATE_OPENERS: Set<string>;
32
+ declare const DCS_OPEN = "P";
33
+ declare const APC_OPEN = "_";
34
+ declare const SOS_OPEN = "^";
35
+ declare const PM_OPEN = "X";
36
+ declare const STRING_OPENERS: Set<string>;
37
+ declare const INTERRUPTERS: Set<number>;
38
+ declare const C0_INTERRUPTERS: Set<number>;
39
+ declare const PARAM_SEPARATOR: RegExp;
40
+ declare const TOKEN_TYPES: {
41
+ readonly TEXT: "TEXT";
42
+ readonly INTRODUCER: "INTRODUCER";
43
+ readonly DATA: "DATA";
44
+ readonly FINAL: "FINAL";
45
+ };
46
+ declare const CODE_TYPES: {
47
+ readonly CSI: "CSI";
48
+ readonly DCS: "DCS";
49
+ readonly DEC: "DEC";
50
+ readonly ESC: "ESC";
51
+ readonly OSC: "OSC";
52
+ readonly PRIVATE: "PRIVATE";
53
+ readonly SGR: "SGR";
54
+ readonly STRING: "STRING";
55
+ readonly TEXT: "TEXT";
56
+ };
57
+ //#endregion
58
+ //#region src/types.d.ts
59
+ type TOKEN = {
60
+ type: keyof typeof TOKEN_TYPES;
61
+ pos: number;
62
+ raw: string;
63
+ code?: string;
64
+ intermediate?: string;
65
+ };
66
+ type CONTROL_CODE_TYPE = Exclude<keyof typeof CODE_TYPES, TEXT["type"]>;
67
+ type CONTROL_CODE = {
68
+ type: CONTROL_CODE_TYPE;
69
+ command: string;
70
+ raw: string;
71
+ params: string[];
72
+ pos: number;
73
+ };
74
+ type TEXT = {
75
+ type: "TEXT";
76
+ raw: string;
77
+ pos: number;
78
+ };
79
+ type CODE = CONTROL_CODE | TEXT;
80
+ //#endregion
81
+ //#region src/parse.d.ts
82
+ declare function parser(tokens: IterableIterator<TOKEN>): IterableIterator<CODE>;
83
+ declare function parse(input: string): CODE[];
84
+ //#endregion
85
+ export { APC, APC_CODE, APC_OPEN, BACKSLASH, BACKSLASH_CODE, BELL, BELL_CODE, C0_INTERRUPTERS, CAN, CAN_CODE, CODE, CODE_TYPES, CONTROL_CODE, 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, TEXT, TOKEN, TOKEN_TYPES, parse, parser };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ansi-tools/parser",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Tokenize and parse strings containing ANSI escape sequences and control codes",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
@@ -23,7 +23,7 @@
23
23
  "prebuild": "pnpm type-check && pnpm test",
24
24
  "build": "tsdown --dts src/index.ts src/escaped.ts",
25
25
  "dev": "tsdown --dts src/index.ts src/escaped.ts --watch",
26
- "test": "node --test",
26
+ "test": "node --import ./test/assertions.ts --test",
27
27
  "type-check": "tsc",
28
28
  "prepack": "pnpm build"
29
29
  },
@@ -45,8 +45,8 @@
45
45
  "directory": "packages/parser"
46
46
  },
47
47
  "devDependencies": {
48
- "@types/node": "^24.0.13",
49
- "tsdown": "^0.12.9",
50
- "typescript": "^5.8.3"
48
+ "@types/node": "^24.7.2",
49
+ "tsdown": "^0.15.7",
50
+ "typescript": "^5.9.3"
51
51
  }
52
52
  }
@@ -1,71 +0,0 @@
1
- //#region src/constants.d.ts
2
- declare const BELL: string;
3
- declare const CAN: string;
4
- declare const SUB: string;
5
- declare const ESC: string;
6
- declare const BACKSLASH: string;
7
- declare const DCS: string;
8
- declare const SOS: string;
9
- declare const CSI: string;
10
- declare const ST: string;
11
- declare const OSC: string;
12
- declare const PM: string;
13
- declare const APC: string;
14
- declare const CSI_OPEN = "[";
15
- declare const OSC_OPEN = "]";
16
- declare const DEC_OPEN = "?";
17
- declare const PRIVATE_OPENERS: Set<string>;
18
- declare const DCS_OPEN = "P";
19
- declare const APC_OPEN = "_";
20
- declare const SOS_OPEN = "^";
21
- declare const PM_OPEN = "X";
22
- declare const STRING_OPENERS: Set<string>;
23
- declare const INTERRUPTERS: Set<string>;
24
- declare const C0_INTERRUPTERS: Set<string>;
25
- declare const PARAM_SEPARATOR: RegExp;
26
- declare const TOKEN_TYPES: {
27
- readonly TEXT: "TEXT";
28
- readonly INTRODUCER: "INTRODUCER";
29
- readonly DATA: "DATA";
30
- readonly FINAL: "FINAL";
31
- };
32
- declare const CODE_TYPES: {
33
- readonly CSI: "CSI";
34
- readonly DCS: "DCS";
35
- readonly DEC: "DEC";
36
- readonly ESC: "ESC";
37
- readonly OSC: "OSC";
38
- readonly PRIVATE: "PRIVATE";
39
- readonly SGR: "SGR";
40
- readonly STRING: "STRING";
41
- readonly TEXT: "TEXT";
42
- };
43
- //#endregion
44
- //#region src/types.d.ts
45
- type TOKEN = {
46
- type: keyof typeof TOKEN_TYPES;
47
- pos: number;
48
- raw: string;
49
- code?: string;
50
- intermediate?: string;
51
- };
52
- type CONTROL_CODE_TYPE = Exclude<keyof typeof CODE_TYPES, TEXT["type"]>;
53
- type CONTROL_CODE = {
54
- type: CONTROL_CODE_TYPE;
55
- command: string;
56
- raw: string;
57
- params: string[];
58
- pos: number;
59
- };
60
- type TEXT = {
61
- type: "TEXT";
62
- raw: string;
63
- pos: number;
64
- };
65
- type CODE = CONTROL_CODE | TEXT;
66
- //#endregion
67
- //#region src/parse.d.ts
68
- declare function parser(tokens: IterableIterator<TOKEN>): IterableIterator<CODE>;
69
- declare function parse(input: string): CODE[];
70
- //#endregion
71
- export { APC, APC_OPEN, BACKSLASH, BELL, C0_INTERRUPTERS, CAN, CODE, CODE_TYPES, CONTROL_CODE, CSI, CSI_OPEN, DCS, DCS_OPEN, DEC_OPEN, ESC, INTERRUPTERS, OSC, OSC_OPEN, PARAM_SEPARATOR, PM, PM_OPEN, PRIVATE_OPENERS, SOS, SOS_OPEN, ST, STRING_OPENERS, SUB, TEXT, TOKEN, TOKEN_TYPES, parse, parser };