@ansi-tools/parser 1.0.12 → 1.0.14
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 +1 -1
- package/dist/escaped.js +1 -275
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -3
- package/dist/{parse-BzJl6pBN.d.ts → parse-BXzmEfiO.d.ts} +1 -1
- package/dist/parse-DOrPQ1A8.js +1 -0
- package/package.json +26 -25
- package/dist/parse-BCjxFvJh.js +0 -530
package/dist/escaped.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as OSC_CODE, B as SOS_OPEN, C as DCS_CODE, D as ESC_CODE, E as ESC, F as PM_CODE, G as SUB_CODE, H as STRING_OPENERS, I as PM_OPEN, K as TOKEN_TYPES, L as PRIVATE_OPENERS, M as OSC_OPEN_CODE, N as PARAM_SEPARATOR, O as INTERRUPTERS, P as PM, R as SOS, S as DCS, T as DEC_OPEN, U as ST_CODE, V as ST, W as SUB, _ as CODE_TYPES, a as TEXT, b as CSI_OPEN, c as APC_CODE, d as BACKSLASH_CODE, f as BELL, g as CAN_CODE, h as CAN, i as CONTROL_CODE, j as OSC_OPEN, k as OSC, l as APC_OPEN, m as C0_INTERRUPTERS, n as parser, o as TOKEN, p as BELL_CODE, r as CODE, s as APC, u as BACKSLASH, v as CSI, w as DCS_OPEN, x as CSI_OPEN_CODE, y as CSI_CODE, z as SOS_CODE } from "./parse-BXzmEfiO.js";
|
|
2
2
|
|
|
3
3
|
//#region src/parse.escaped.d.ts
|
|
4
4
|
declare function parse(input: string): CODE[];
|
package/dist/escaped.js
CHANGED
|
@@ -1,275 +1 @@
|
|
|
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
|
|
2
|
-
|
|
3
|
-
//#region src/tokenize.escaped.ts
|
|
4
|
-
const CSI_ESCAPED = "\\u009b";
|
|
5
|
-
const CSI_ESCAPED_HEX = "\\x9b";
|
|
6
|
-
const ABANDONED = "ABANDONED";
|
|
7
|
-
const INTRODUCERS = [
|
|
8
|
-
["\\u001b", 6],
|
|
9
|
-
[CSI_ESCAPED, 6],
|
|
10
|
-
[CSI_ESCAPED_HEX, 4],
|
|
11
|
-
["\\x1b", 4],
|
|
12
|
-
["\\033", 4],
|
|
13
|
-
["\\e", 2]
|
|
14
|
-
];
|
|
15
|
-
const INTERRUPTERS_ESCAPED = [
|
|
16
|
-
["\\x18", 4],
|
|
17
|
-
["\\x1a", 4],
|
|
18
|
-
["\\u0018", 6],
|
|
19
|
-
["\\u001a", 6]
|
|
20
|
-
];
|
|
21
|
-
const INTERRUPTER_LOOKUP = /* @__PURE__ */ new Map();
|
|
22
|
-
for (const [sequence, len] of INTERRUPTERS_ESCAPED) {
|
|
23
|
-
const secondChar = sequence[1];
|
|
24
|
-
if (!INTERRUPTER_LOOKUP.has(secondChar)) INTERRUPTER_LOOKUP.set(secondChar, []);
|
|
25
|
-
INTERRUPTER_LOOKUP.get(secondChar)?.push([sequence, len]);
|
|
26
|
-
}
|
|
27
|
-
const INTRODUCER_LOOKUP = /* @__PURE__ */ new Map();
|
|
28
|
-
const INTRODUCER_FIRST_CHAR_CACHE = /* @__PURE__ */ new Map();
|
|
29
|
-
for (const [sequence, len] of INTRODUCERS) {
|
|
30
|
-
const secondChar = sequence[1];
|
|
31
|
-
if (!INTRODUCER_LOOKUP.has(secondChar)) INTRODUCER_LOOKUP.set(secondChar, []);
|
|
32
|
-
INTRODUCER_LOOKUP.get(secondChar)?.push([sequence, len]);
|
|
33
|
-
INTRODUCER_FIRST_CHAR_CACHE.set(sequence, true);
|
|
34
|
-
}
|
|
35
|
-
function emit(token) {
|
|
36
|
-
return token;
|
|
37
|
-
}
|
|
38
|
-
function* tokenizer(input) {
|
|
39
|
-
const l = input.length;
|
|
40
|
-
let i = 0;
|
|
41
|
-
let state = "GROUND";
|
|
42
|
-
let currentCode;
|
|
43
|
-
let backslashIndex = input.indexOf(BACKSLASH_CODE);
|
|
44
|
-
function setState(next, code) {
|
|
45
|
-
state = next;
|
|
46
|
-
currentCode = code;
|
|
47
|
-
}
|
|
48
|
-
while (i < l) if (state === "GROUND") {
|
|
49
|
-
const textStart = i;
|
|
50
|
-
while (i < l) {
|
|
51
|
-
if (backslashIndex === -1) {
|
|
52
|
-
i = l;
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
if (backslashIndex < i) backslashIndex = input.indexOf(BACKSLASH_CODE, i);
|
|
56
|
-
if (backslashIndex === -1) {
|
|
57
|
-
i = l;
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
let isIntroducer = false;
|
|
61
|
-
const candidates = INTRODUCER_LOOKUP.get(input[backslashIndex + 1]);
|
|
62
|
-
if (candidates) for (const [seq, len] of candidates) {
|
|
63
|
-
if (backslashIndex + len > l) continue;
|
|
64
|
-
if (input.startsWith(seq, backslashIndex)) {
|
|
65
|
-
isIntroducer = true;
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (isIntroducer) {
|
|
70
|
-
i = backslashIndex;
|
|
71
|
-
break;
|
|
72
|
-
} else i = backslashIndex + 1;
|
|
73
|
-
}
|
|
74
|
-
if (i > textStart) yield emit({
|
|
75
|
-
type: TOKEN_TYPES.TEXT,
|
|
76
|
-
pos: textStart,
|
|
77
|
-
raw: input.substring(textStart, i)
|
|
78
|
-
});
|
|
79
|
-
if (i < l) {
|
|
80
|
-
const candidates = INTRODUCER_LOOKUP.get(input[i + 1]);
|
|
81
|
-
if (candidates) {
|
|
82
|
-
let isMatch = false;
|
|
83
|
-
for (const [seq, len] of candidates) {
|
|
84
|
-
if (i + len > l) continue;
|
|
85
|
-
if (input.startsWith(seq, i)) {
|
|
86
|
-
isMatch = true;
|
|
87
|
-
if (seq === CSI_ESCAPED || seq === CSI_ESCAPED_HEX) {
|
|
88
|
-
yield emit({
|
|
89
|
-
type: TOKEN_TYPES.INTRODUCER,
|
|
90
|
-
pos: i,
|
|
91
|
-
raw: seq,
|
|
92
|
-
code: CSI_CODE
|
|
93
|
-
});
|
|
94
|
-
i += len;
|
|
95
|
-
setState("SEQUENCE", CSI);
|
|
96
|
-
} else {
|
|
97
|
-
const next = input[i + len];
|
|
98
|
-
if (next === CSI_OPEN_CODE) {
|
|
99
|
-
yield emit({
|
|
100
|
-
type: TOKEN_TYPES.INTRODUCER,
|
|
101
|
-
pos: i,
|
|
102
|
-
raw: seq + next,
|
|
103
|
-
code: CSI_CODE
|
|
104
|
-
});
|
|
105
|
-
i += len + 1;
|
|
106
|
-
setState("SEQUENCE", CSI);
|
|
107
|
-
} else if (next === OSC_OPEN_CODE) {
|
|
108
|
-
yield emit({
|
|
109
|
-
type: TOKEN_TYPES.INTRODUCER,
|
|
110
|
-
pos: i,
|
|
111
|
-
raw: seq + next,
|
|
112
|
-
code: OSC_CODE
|
|
113
|
-
});
|
|
114
|
-
i += len + 1;
|
|
115
|
-
setState("SEQUENCE", OSC);
|
|
116
|
-
} else if (STRING_OPENERS.has(next)) {
|
|
117
|
-
yield emit({
|
|
118
|
-
type: TOKEN_TYPES.INTRODUCER,
|
|
119
|
-
pos: i,
|
|
120
|
-
raw: seq + next,
|
|
121
|
-
code: next
|
|
122
|
-
});
|
|
123
|
-
i += len + 1;
|
|
124
|
-
setState("SEQUENCE", next.charCodeAt(0));
|
|
125
|
-
} else if (next) {
|
|
126
|
-
let j = i + len;
|
|
127
|
-
while (j < l && input.charCodeAt(j) >= 32 && input.charCodeAt(j) <= 47) j++;
|
|
128
|
-
if (j < l) {
|
|
129
|
-
const is = input.slice(i + len, j);
|
|
130
|
-
if (is) yield emit({
|
|
131
|
-
type: TOKEN_TYPES.INTRODUCER,
|
|
132
|
-
pos: i,
|
|
133
|
-
raw: seq + is,
|
|
134
|
-
code: ESC_CODE,
|
|
135
|
-
intermediate: is
|
|
136
|
-
});
|
|
137
|
-
else yield emit({
|
|
138
|
-
type: TOKEN_TYPES.INTRODUCER,
|
|
139
|
-
pos: i,
|
|
140
|
-
raw: seq,
|
|
141
|
-
code: ESC_CODE
|
|
142
|
-
});
|
|
143
|
-
i = j;
|
|
144
|
-
setState("SEQUENCE", ESC);
|
|
145
|
-
} else i = j;
|
|
146
|
-
} else i += len;
|
|
147
|
-
}
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (!isMatch) i++;
|
|
152
|
-
} else i++;
|
|
153
|
-
}
|
|
154
|
-
} else if (state === "SEQUENCE") {
|
|
155
|
-
let terminator = "";
|
|
156
|
-
let terminatorPos = -1;
|
|
157
|
-
const pos = i;
|
|
158
|
-
while (!terminator && i < l) {
|
|
159
|
-
if (input.charCodeAt(i) === BACKSLASH) {
|
|
160
|
-
const next = input[i + 1];
|
|
161
|
-
if (next) {
|
|
162
|
-
const interrupters = INTERRUPTER_LOOKUP.get(next);
|
|
163
|
-
if (interrupters) {
|
|
164
|
-
for (const [seq, len] of interrupters) if (i + len <= l) {
|
|
165
|
-
let matched = true;
|
|
166
|
-
for (let k = 0; k < len; k++) if (input[i + k] !== seq[k]) {
|
|
167
|
-
matched = false;
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
if (matched) {
|
|
171
|
-
terminator = ABANDONED;
|
|
172
|
-
terminatorPos = i;
|
|
173
|
-
i += len;
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
if (terminator) break;
|
|
180
|
-
if (currentCode !== CSI && currentCode !== ESC) {
|
|
181
|
-
if (next === "a" && i + 2 <= l) {
|
|
182
|
-
if (currentCode === OSC && input[i + 1] === "a") {
|
|
183
|
-
terminator = "\\a";
|
|
184
|
-
terminatorPos = i;
|
|
185
|
-
i += 2;
|
|
186
|
-
}
|
|
187
|
-
} else if (next === "x") {
|
|
188
|
-
if (i + 4 <= l) {
|
|
189
|
-
const char3 = input[i + 2];
|
|
190
|
-
const char4 = input[i + 3];
|
|
191
|
-
if (char3 === "0" && char4 === "7" && currentCode === OSC) {
|
|
192
|
-
terminator = "\\x07";
|
|
193
|
-
terminatorPos = i;
|
|
194
|
-
i += 4;
|
|
195
|
-
} else if (char3 === "9" && char4 === "c") {
|
|
196
|
-
terminator = "\\x9c";
|
|
197
|
-
terminatorPos = i;
|
|
198
|
-
i += 4;
|
|
199
|
-
} else if (char3 === "1" && char4 === "b" && i + 6 <= l && input.charCodeAt(i + 4) === BACKSLASH && input.charCodeAt(i + 5) === BACKSLASH) {
|
|
200
|
-
terminator = "\\x1b\\\\";
|
|
201
|
-
terminatorPos = i;
|
|
202
|
-
i += 6;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
} else if (next === "u" && currentCode === OSC && i + 6 <= l) {
|
|
206
|
-
if (input[i + 2] === "0" && input[i + 3] === "0" && input[i + 4] === "0" && input[i + 5] === "7") {
|
|
207
|
-
terminator = "\\u0007";
|
|
208
|
-
terminatorPos = i;
|
|
209
|
-
i += 6;
|
|
210
|
-
}
|
|
211
|
-
} else if (next === "e" && i + 4 <= l) {
|
|
212
|
-
if (input.charCodeAt(i + 2) === BACKSLASH && input.charCodeAt(i + 3) === BACKSLASH) {
|
|
213
|
-
terminator = "\\e\\\\";
|
|
214
|
-
terminatorPos = i;
|
|
215
|
-
i += 4;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
if (!terminator) {
|
|
220
|
-
if (next) {
|
|
221
|
-
const candidates = INTRODUCER_LOOKUP.get(next);
|
|
222
|
-
if (candidates) for (const [seq, len] of candidates) {
|
|
223
|
-
if (i + len > l) continue;
|
|
224
|
-
let matched = true;
|
|
225
|
-
for (let k = 0; k < len && matched; k += 2) {
|
|
226
|
-
matched = input[i + k] === seq[k];
|
|
227
|
-
if (matched && k + 1 < len) matched = input[i + k + 1] === seq[k + 1];
|
|
228
|
-
}
|
|
229
|
-
if (matched) {
|
|
230
|
-
terminator = ABANDONED;
|
|
231
|
-
terminatorPos = i;
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
} else if (currentCode === CSI) {
|
|
238
|
-
const charCode = input.charCodeAt(i);
|
|
239
|
-
if (charCode >= 64 && charCode <= 126) {
|
|
240
|
-
terminator = input[i];
|
|
241
|
-
terminatorPos = i;
|
|
242
|
-
i++;
|
|
243
|
-
}
|
|
244
|
-
} else if (currentCode === ESC) {
|
|
245
|
-
terminator = input[i];
|
|
246
|
-
terminatorPos = i;
|
|
247
|
-
i++;
|
|
248
|
-
}
|
|
249
|
-
if (!terminator) i++;
|
|
250
|
-
}
|
|
251
|
-
if (terminatorPos > pos) yield emit({
|
|
252
|
-
type: TOKEN_TYPES.DATA,
|
|
253
|
-
pos,
|
|
254
|
-
raw: input.substring(pos, terminatorPos)
|
|
255
|
-
});
|
|
256
|
-
if (terminator && terminator !== ABANDONED) yield emit({
|
|
257
|
-
type: TOKEN_TYPES.FINAL,
|
|
258
|
-
pos: terminatorPos,
|
|
259
|
-
raw: terminator
|
|
260
|
-
});
|
|
261
|
-
setState("GROUND");
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
function tokenize(input) {
|
|
265
|
-
return Array.from(tokenizer(input));
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
//#endregion
|
|
269
|
-
//#region src/parse.escaped.ts
|
|
270
|
-
function parse(input) {
|
|
271
|
-
return Array.from(parser(tokenizer(input)));
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
//#endregion
|
|
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 };
|
|
1
|
+
import{A as e,B as t,C as n,D as r,E as i,F as a,H as o,I as s,L as c,M as l,N as u,O as d,P as f,R as p,S as m,T as h,U as g,V as _,W as v,_ as y,a as b,b as x,c as S,d as C,f as w,g as T,h as E,j as D,k as O,l as k,m as A,n as j,o as M,p as N,s as P,u as F,v as I,w as L,x as R,y as z,z as B}from"./parse-DOrPQ1A8.js";const V=`\\u009b`,H=`\\x9b`,U=`ABANDONED`,W=[[`\\u001b`,6],[V,6],[H,4],[`\\x1b`,4],[`\\033`,4],[`\\e`,2]],G=[[`\\x18`,4],[`\\x1a`,4],[`\\u0018`,6],[`\\u001a`,6]],K=new Map;for(let[e,t]of G){let n=e[1];K.has(n)||K.set(n,[]),K.get(n)?.push([e,t])}const q=new Map,J=new Map;for(let[e,t]of W){let n=e[1];q.has(n)||q.set(n,[]),q.get(n)?.push([e,t]),J.set(e,!0)}function Y(e){return e}function*X(n){let i=n.length,a=0,o=`GROUND`,s,c=n.indexOf(k);function l(e,t){o=e,s=t}for(;a<i;)if(o===`GROUND`){let o=a;for(;a<i;){if(c===-1){a=i;break}if(c<a&&(c=n.indexOf(k,a)),c===-1){a=i;break}let e=!1,t=q.get(n[c+1]);if(t){for(let[r,a]of t)if(!(c+a>i)&&n.startsWith(r,c)){e=!0;break}}if(e){a=c;break}else a=c+1}if(a>o&&(yield Y({type:v.TEXT,pos:o,raw:n.substring(o,a)})),a<i){let o=q.get(n[a+1]);if(o){let s=!1;for(let[c,u]of o)if(!(a+u>i)&&n.startsWith(c,a)){if(s=!0,c===V||c===H)yield Y({type:v.INTRODUCER,pos:a,raw:c,code:y}),a+=u,l(`SEQUENCE`,T);else{let o=n[a+u];if(o===z)yield Y({type:v.INTRODUCER,pos:a,raw:c+o,code:y}),a+=u+1,l(`SEQUENCE`,T);else if(o===e)yield Y({type:v.INTRODUCER,pos:a,raw:c+o,code:d}),a+=u+1,l(`SEQUENCE`,r);else if(t.has(o))yield Y({type:v.INTRODUCER,pos:a,raw:c+o,code:o}),a+=u+1,l(`SEQUENCE`,o.charCodeAt(0));else if(o){let e=a+u;for(;e<i&&n.charCodeAt(e)>=32&&n.charCodeAt(e)<=47;)e++;if(e<i){let t=n.slice(a+u,e);t?yield Y({type:v.INTRODUCER,pos:a,raw:c+t,code:h,intermediate:t}):yield Y({type:v.INTRODUCER,pos:a,raw:c,code:h}),a=e,l(`SEQUENCE`,L)}else a=e}else a+=u}break}s||a++}else a++}}else if(o===`SEQUENCE`){let e=``,t=-1,o=a;for(;!e&&a<i;){if(n.charCodeAt(a)===S){let o=n[a+1];if(o){let r=K.get(o);if(r){for(let[o,s]of r)if(a+s<=i){let r=!0;for(let e=0;e<s;e++)if(n[a+e]!==o[e]){r=!1;break}if(r){e=U,t=a,a+=s;break}}}}if(e)break;if(s!==T&&s!==L)if(o===`a`&&a+2<=i)s===r&&n[a+1]===`a`&&(e=`\\a`,t=a,a+=2);else if(o===`x`){if(a+4<=i){let o=n[a+2],c=n[a+3];o===`0`&&c===`7`&&s===r?(e=`\\x07`,t=a,a+=4):o===`9`&&c===`c`?(e=`\\x9c`,t=a,a+=4):o===`1`&&c===`b`&&a+6<=i&&n.charCodeAt(a+4)===S&&n.charCodeAt(a+5)===S&&(e=`\\x1b\\\\`,t=a,a+=6)}}else o===`u`&&s===r&&a+6<=i?n[a+2]===`0`&&n[a+3]===`0`&&n[a+4]===`0`&&n[a+5]===`7`&&(e=`\\u0007`,t=a,a+=6):o===`e`&&a+4<=i&&n.charCodeAt(a+2)===S&&n.charCodeAt(a+3)===S&&(e=`\\e\\\\`,t=a,a+=4);if(!e&&o){let r=q.get(o);if(r)for(let[o,s]of r){if(a+s>i)continue;let r=!0;for(let e=0;e<s&&r;e+=2)r=n[a+e]===o[e],r&&e+1<s&&(r=n[a+e+1]===o[e+1]);if(r){e=U,t=a;break}}}}else if(s===T){let r=n.charCodeAt(a);r>=64&&r<=126&&(e=n[a],t=a,a++)}else s===L&&(e=n[a],t=a,a++);e||a++}t>o&&(yield Y({type:v.DATA,pos:o,raw:n.substring(o,t)})),e&&e!==U&&(yield Y({type:v.FINAL,pos:t,raw:e})),l(`GROUND`)}}function Z(e){return Array.from(X(e))}function Q(e){return Array.from(j(X(e)))}export{b as APC,M as APC_CODE,P as APC_OPEN,S as BACKSLASH,k as BACKSLASH_CODE,F as BELL,C as BELL_CODE,w as C0_INTERRUPTERS,N as CAN,A as CAN_CODE,E as CODE_TYPES,T as CSI,y as CSI_CODE,I as CSI_OPEN,z as CSI_OPEN_CODE,x as DCS,R as DCS_CODE,m as DCS_OPEN,n as DEC_OPEN,L as ESC,h as ESC_CODE,i as INTERRUPTERS,r as OSC,d as OSC_CODE,O as OSC_OPEN,e as OSC_OPEN_CODE,D as PARAM_SEPARATOR,l as PM,u as PM_CODE,f as PM_OPEN,a as PRIVATE_OPENERS,s as SOS,c as SOS_CODE,p as SOS_OPEN,B as ST,t as STRING_OPENERS,_ as ST_CODE,o as SUB,g as SUB_CODE,v as TOKEN_TYPES,Q as parse,j as parser,Z as tokenize,X as tokenizer};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as OSC_CODE, B as SOS_OPEN, C as DCS_CODE, D as ESC_CODE, E as ESC, F as PM_CODE, G as SUB_CODE, H as STRING_OPENERS, I as PM_OPEN, K as TOKEN_TYPES, L as PRIVATE_OPENERS, M as OSC_OPEN_CODE, N as PARAM_SEPARATOR, O as INTERRUPTERS, P as PM, R as SOS, S as DCS, T as DEC_OPEN, U as ST_CODE, V as ST, W as SUB, _ as CODE_TYPES, a as TEXT, b as CSI_OPEN, c as APC_CODE, d as BACKSLASH_CODE, f as BELL, g as CAN_CODE, h as CAN, i as CONTROL_CODE, j as OSC_OPEN, k as OSC, l as APC_OPEN, m as C0_INTERRUPTERS, n as parser, o as TOKEN, p as BELL_CODE, r as CODE, s as APC, t as parse, u as BACKSLASH, v as CSI, w as DCS_OPEN, x as CSI_OPEN_CODE, y as CSI_CODE, z as SOS_CODE } from "./parse-BXzmEfiO.js";
|
|
2
2
|
|
|
3
3
|
//#region src/tokenize.d.ts
|
|
4
4
|
declare function tokenizer(input: string): IterableIterator<TOKEN>;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1 @@
|
|
|
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
|
|
2
|
-
|
|
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
|
+
import{A as e,B as t,C as n,D as r,E as i,F as a,H as o,I as s,L as c,M as l,N as u,O as d,P as f,R as p,S as m,T as h,U as g,V as _,W as v,_ as y,a as b,b as x,c as S,d as C,f as w,g as T,h as E,i as D,j as O,k,l as A,m as j,n as M,o as N,p as P,r as F,s as I,t as L,u as R,v as z,w as B,x as V,y as H,z as U}from"./parse-DOrPQ1A8.js";export{b as APC,N as APC_CODE,I as APC_OPEN,S as BACKSLASH,A as BACKSLASH_CODE,R as BELL,C as BELL_CODE,w as C0_INTERRUPTERS,P as CAN,j as CAN_CODE,E as CODE_TYPES,T as CSI,y as CSI_CODE,z as CSI_OPEN,H as CSI_OPEN_CODE,x as DCS,V as DCS_CODE,m as DCS_OPEN,n as DEC_OPEN,B as ESC,h as ESC_CODE,i as INTERRUPTERS,r as OSC,d as OSC_CODE,k as OSC_OPEN,e as OSC_OPEN_CODE,O as PARAM_SEPARATOR,l as PM,u as PM_CODE,f as PM_OPEN,a as PRIVATE_OPENERS,s as SOS,c as SOS_CODE,p as SOS_OPEN,U as ST,t as STRING_OPENERS,_ as ST_CODE,o as SUB,g as SUB_CODE,v as TOKEN_TYPES,L as parse,M as parser,F as tokenize,D as tokenizer};
|
|
@@ -82,4 +82,4 @@ type CODE = CONTROL_CODE | TEXT;
|
|
|
82
82
|
declare function parser(tokens: IterableIterator<TOKEN>): IterableIterator<CODE>;
|
|
83
83
|
declare function parse(input: string): CODE[];
|
|
84
84
|
//#endregion
|
|
85
|
-
export {
|
|
85
|
+
export { OSC_CODE as A, SOS_OPEN as B, DCS_CODE as C, ESC_CODE as D, ESC as E, PM_CODE as F, SUB_CODE as G, STRING_OPENERS as H, PM_OPEN as I, TOKEN_TYPES as K, PRIVATE_OPENERS as L, OSC_OPEN_CODE as M, PARAM_SEPARATOR as N, INTERRUPTERS as O, PM as P, SOS as R, DCS as S, DEC_OPEN as T, ST_CODE as U, ST as V, SUB as W, CODE_TYPES as _, TEXT as a, CSI_OPEN as b, APC_CODE as c, BACKSLASH_CODE as d, BELL as f, CAN_CODE as g, CAN as h, CONTROL_CODE as i, OSC_OPEN as j, OSC as k, APC_OPEN as l, C0_INTERRUPTERS as m, parser as n, TOKEN as o, BELL_CODE as p, CODE as r, APC as s, parse as t, BACKSLASH as u, CSI as v, DCS_OPEN as w, CSI_OPEN_CODE as x, CSI_CODE as y, SOS_CODE as z };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=7,t=`\x07`,n=24,r=``,i=26,a=``,o=27,s=`\x1B`,c=92,l=`\\`,u=144,d=``,f=152,ee=``,p=155,m=``,h=156,g=``,_=157,v=``,y=158,b=``,x=159,S=``,C=91,w=`[`,T=93,E=`]`,D=63,O=new Set([`<`,`=`,`>`]),k=`P`,A=`_`,j=`^`,M=`X`,N=new Set([`P`,`_`,`^`,`X`]),P=new Set([24,26,27,155,157,144,159,158,152]),F=new Set([24,26]),I=/[;:]/,L={TEXT:`TEXT`,INTRODUCER:`INTRODUCER`,DATA:`DATA`,FINAL:`FINAL`},R={CSI:`CSI`,DCS:`DCS`,DEC:`DEC`,ESC:`ESC`,OSC:`OSC`,PRIVATE:`PRIVATE`,SGR:`SGR`,STRING:`STRING`,TEXT:`TEXT`};function z(e,t,n){let r=t.map(e=>e.raw).join(``),i=e.raw+r+(n?.raw||``);return{type:R.STRING,pos:e.pos,raw:i,command:`APC`,params:r?[r]:[]}}function B(e,t,n){let r=t.length===1?t[0].raw:t.length===0?``:t.map(e=>e.raw).join(``),i=n?.raw??``,a=e.raw+r+i,o=[],s=R.CSI,c=0;if(r)for(;c<r.length;){let e=r.charCodeAt(c);if(e<48||e>63)break;c++}let l=c>0?r.substring(0,c):``,u=c<r.length?r.substring(c):``;if(l){let e=0;for(let t=0;t<=l.length;t++)(t===l.length||l.charCodeAt(t)===59||l.charCodeAt(t)===58)&&(o.push(t>e?l.substring(e,t):`0`),e=t+1)}let d=u+i,f=o[0];if(f!==void 0&&f.charCodeAt(0)===63)return s=R.DEC,f.length>1?o[0]=f.substring(1):o.shift(),{type:s,pos:e.pos,raw:a,command:d,params:o};for(let t of o)if(t.length>0&&O.has(t[0])){s=R.PRIVATE;let n=t[0]+d;for(let e=0;e<o.length;e++)if(o[e].length>0&&O.has(o[e][0])){o[e].length>1?o[e]=o[e].substring(1):o.splice(e,1);break}return{type:s,pos:e.pos,raw:a,command:n,params:o}}return d===`m`&&o.length===5&&o[1]===`2`&&(f===`38`||f===`48`)&&o.splice(2,0,`0`),d===`r`&&o.length===2&&o[1]===`0`&&(o[1]=`-1`),{type:s,pos:e.pos,raw:a,command:d,params:o}}const V=new Map([[`$q`,2],[`+q`,2],[`+p`,2],[`|`,1],[`{`,1]]);function H(e,t,n){let r=t.map(e=>e.raw).join(``),i=e.raw+r+(n?.raw??``);if(!r)return{type:R.DCS,pos:e.pos,raw:i,command:``,params:[]};for(let[t,n]of V)if(r.startsWith(t)){let a=r.slice(n),o=[];if(a)for(let e of a.split(I))o.push(e||`-1`);return{type:R.DCS,pos:e.pos,raw:i,command:t,params:o}}return{type:R.DCS,pos:e.pos,raw:i,command:``,params:[r]}}function U(e,t,n){let r=t.map(e=>e.raw).join(``),i=e.intermediate||(t[0]?.raw??n?.raw??``),a=e.intermediate&&n?.raw?[n.raw]:[],o=e.raw+r+(n?.raw??``);return{type:R.ESC,pos:e.pos,raw:o,command:i,params:a}}function W(e,t,n){let r=t.map(e=>e.raw).join(``),i=e.raw+r+(n?.raw||``),a=r.indexOf(`;`);if(a===-1)return{type:R.OSC,pos:e.pos,raw:i,command:r,params:[]};let o=r.slice(0,a),s=r.slice(a+1);if(o===`1337`)return{type:R.OSC,pos:e.pos,raw:i,command:o,params:[s]};let c=[];if(s){let e=``;for(let t=0;t<s.length;t++)s[t]===`;`?(c.push(e),e=``):e+=s[t];c.push(e)}return{type:R.OSC,pos:e.pos,raw:i,command:o,params:c}}function G(e,t,n){let r=t.map(e=>e.raw).join(``),i=e.raw+r+(n?.raw||``);return{type:R.STRING,pos:e.pos,raw:i,command:`PM`,params:r?[r]:[]}}function K(e,t,n){let r=t.map(e=>e.raw).join(``),i=e.raw+r+(n?.raw||``);return{type:R.STRING,pos:e.pos,raw:i,command:`SOS`,params:r?[r]:[]}}function q(e){return e===24||e===26||e===27||e===155||e===157||e===144||e===159||e===158||e===152}function J(e){return e===24||e===26}function Y(e){return e===27||e===155||e===157||e===144||e===159||e===158||e===152}function X(e){return e===155||e===157||e===144||e===159||e===158||e===152}function*Z(e){let t=0,n=0,r=0,i=e.length;for(;t<i;)if(n===0){let a=t,o=e.charCodeAt(t);for(;t<i&&!Y(o);)o=e.charCodeAt(++t);if(t>a&&(yield{type:L.TEXT,pos:a,raw:e.substring(a,t)}),t>=i)break;if(X(o))yield{type:L.INTRODUCER,pos:t,raw:e[t],code:e[t]},t++,n=1,r=o;else{let a=e.charCodeAt(t+1);if(a===91)yield{type:L.INTRODUCER,pos:t,raw:e.substring(t,t+2),code:``},t+=2,n=1,r=155;else if(a===93)yield{type:L.INTRODUCER,pos:t,raw:e.substring(t,t+2),code:``},t+=2,n=1,r=157;else if(t+1<i&&N.has(e[t+1]))yield{type:L.INTRODUCER,pos:t,raw:e.substring(t,t+2),code:e[t+1]},t+=2,n=1,r=a;else if(t+1<i){let a=t+1;for(;a<i&&e.charCodeAt(a)>=32&&e.charCodeAt(a)<=47;)a++;if(a<i){if(a>t+1){let n=e.substring(t+1,a);yield{type:L.INTRODUCER,pos:t,raw:e.substring(t,a),code:`\x1B`,intermediate:n}}else yield{type:L.INTRODUCER,pos:t,raw:e[t],code:`\x1B`};t=a,n=1,r=27}else t=a}else t++}}else{let a=t;if(r===155){let r=t;for(;t<i;){let i=e.charCodeAt(t);if(q(i)){t>r&&(yield{type:L.DATA,pos:a,raw:e.substring(r,t)}),n=0,J(i)&&t++;break}if(i>=64&&i<=126){t>r&&(yield{type:L.DATA,pos:a,raw:e.substring(r,t)}),yield{type:L.FINAL,pos:t,raw:e[t]},t++,n=0;break}t++}}else if(r===27){if(t<i){let r=e.charCodeAt(t);q(r)?(n=0,J(r)&&t++):(yield{type:L.FINAL,pos:t,raw:e[t]},t++,n=0)}}else{let o=t;for(;t<i;){let i=e.charCodeAt(t),s;if(i===27&&e.charCodeAt(t+1)===92?s=`\x1B\\`:i===156?s=``:i===7&&r===157&&(s=`\x07`),s){t>o&&(yield{type:L.DATA,pos:a,raw:e.substring(o,t)}),yield{type:L.FINAL,pos:t,raw:s},t+=s.length,n=0;break}if(q(i)){t>o&&(yield{type:L.DATA,pos:a,raw:e.substring(o,t)}),n=0,J(i)&&t++;break}t++}}n===1&&(n=0)}}function Q(e){return Array.from(Z(e))}function*$(e){let t=e.next();for(;!t.done;){let n=t.value;if(n.type===L.TEXT){yield{type:R.TEXT,pos:n.pos,raw:n.raw},t=e.next();continue}if(n.type===L.INTRODUCER){let r=n,i=[],a;for(t=e.next();!t.done;){let n=t.value;if(n.type===L.DATA)i.push(n);else if(n.type===L.FINAL){a=n,t=e.next();break}else break;t=e.next()}yield te(r,i,a)}else t=e.next()}}function te(e,t,n){switch(e.code){case``:return B(e,t,n);case``:return W(e,t,n);case``:case`P`:return H(e,t,n);case``:case`_`:return z(e,t,n);case``:case`X`:return G(e,t,n);case``:case`^`:return K(e,t,n);case`\x1B`:return U(e,t,n);default:return{type:R.TEXT,pos:e.pos,raw:e.raw}}}function ne(e){return Array.from($(Z(e)))}export{E as A,N as B,D as C,_ as D,P as E,O as F,i as H,f as I,ee as L,y as M,b as N,v as O,M as P,j as R,k as S,s as T,a as U,g as V,L as W,m as _,x as a,u as b,c,t as d,F as f,p as g,R as h,Z as i,I as j,T as k,l,r as m,$ as n,S as o,n as p,Q as r,A as s,ne as t,e as u,C as v,o as w,d as x,w as y,h as z};
|
package/package.json
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ansi-tools/parser",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "Tokenize and parse strings containing ANSI escape sequences and control codes",
|
|
5
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ansi",
|
|
7
|
+
"escape codes",
|
|
8
|
+
"web application"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/webpro/ANSI.tools/tree/main/packages/parser",
|
|
11
|
+
"bugs": "https://github.com/webpro/ANSI.tools/issues",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"author": "Lars Kappert <lars@webpro.nl>",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "github:webpro/ANSI.tools",
|
|
17
|
+
"directory": "packages/parser"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
6
22
|
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
7
24
|
"exports": {
|
|
8
25
|
".": {
|
|
9
26
|
"types": "./dist/index.d.ts",
|
|
@@ -16,36 +33,20 @@
|
|
|
16
33
|
"default": "./dist/escaped.js"
|
|
17
34
|
}
|
|
18
35
|
},
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
22
39
|
"scripts": {
|
|
23
40
|
"prebuild": "pnpm type-check && pnpm test",
|
|
24
|
-
"build": "tsdown --dts src/index.ts src/escaped.ts",
|
|
41
|
+
"build": "tsdown --dts --minify src/index.ts src/escaped.ts",
|
|
25
42
|
"dev": "tsdown --dts src/index.ts src/escaped.ts --watch",
|
|
26
43
|
"test": "node --import ./test/assertions.ts --test",
|
|
27
|
-
"type-check": "tsc",
|
|
44
|
+
"type-check": "tsc --noEmit",
|
|
28
45
|
"prepack": "pnpm build"
|
|
29
46
|
},
|
|
30
|
-
"keywords": [
|
|
31
|
-
"ansi",
|
|
32
|
-
"escape codes",
|
|
33
|
-
"web application"
|
|
34
|
-
],
|
|
35
|
-
"author": "Lars Kappert <lars@webpro.nl>",
|
|
36
|
-
"license": "ISC",
|
|
37
|
-
"publishConfig": {
|
|
38
|
-
"access": "public"
|
|
39
|
-
},
|
|
40
|
-
"homepage": "https://github.com/webpro/ANSI.tools/tree/main/packages/parser",
|
|
41
|
-
"bugs": "https://github.com/webpro/ANSI.tools/issues",
|
|
42
|
-
"repository": {
|
|
43
|
-
"type": "git",
|
|
44
|
-
"url": "github:webpro/ANSI.tools",
|
|
45
|
-
"directory": "packages/parser"
|
|
46
|
-
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@types/node": "^24.
|
|
48
|
+
"@types/node": "^24.12.0",
|
|
49
|
+
"mitata": "^1.0.34",
|
|
49
50
|
"tsdown": "^0.15.7",
|
|
50
51
|
"typescript": "^5.9.3"
|
|
51
52
|
}
|
package/dist/parse-BCjxFvJh.js
DELETED
|
@@ -1,530 +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/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
|
-
|
|
91
|
-
//#endregion
|
|
92
|
-
//#region src/parsers/csi.ts
|
|
93
|
-
function parseCSI(introducer, dataTokens, final) {
|
|
94
|
-
const data = dataTokens.map((t) => t.raw).join("");
|
|
95
|
-
const raw = introducer.raw + data + (final?.raw || "");
|
|
96
|
-
const params = [];
|
|
97
|
-
let type = CODE_TYPES.CSI;
|
|
98
|
-
let intermediates = "";
|
|
99
|
-
let paramSection = "";
|
|
100
|
-
if (data) {
|
|
101
|
-
let i = 0;
|
|
102
|
-
while (i < data.length) {
|
|
103
|
-
const charCode = data.charCodeAt(i);
|
|
104
|
-
if (charCode >= 48 && charCode <= 63) {
|
|
105
|
-
paramSection += data[i];
|
|
106
|
-
i++;
|
|
107
|
-
} else break;
|
|
108
|
-
}
|
|
109
|
-
intermediates = data.slice(i);
|
|
110
|
-
}
|
|
111
|
-
if (paramSection) for (const part of paramSection.split(PARAM_SEPARATOR)) params.push(part || "0");
|
|
112
|
-
const command = intermediates + (final?.raw ?? "");
|
|
113
|
-
const start = params[0];
|
|
114
|
-
if (start?.startsWith("?")) {
|
|
115
|
-
type = CODE_TYPES.DEC;
|
|
116
|
-
if (start.length > 1) params[0] = start.slice(1);
|
|
117
|
-
else params.shift();
|
|
118
|
-
return {
|
|
119
|
-
type,
|
|
120
|
-
pos: introducer.pos,
|
|
121
|
-
raw,
|
|
122
|
-
command,
|
|
123
|
-
params
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
for (const param of params) if (param && param.length > 0 && PRIVATE_OPENERS.has(param[0])) {
|
|
127
|
-
type = CODE_TYPES.PRIVATE;
|
|
128
|
-
const privateCommand = param[0] + command;
|
|
129
|
-
for (let i = 0; i < params.length; i++) if (params[i] && params[i].length > 0 && PRIVATE_OPENERS.has(params[i][0])) {
|
|
130
|
-
if (params[i].length > 1) params[i] = params[i].slice(1);
|
|
131
|
-
else params.splice(i, 1);
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
return {
|
|
135
|
-
type,
|
|
136
|
-
pos: introducer.pos,
|
|
137
|
-
raw,
|
|
138
|
-
command: privateCommand,
|
|
139
|
-
params
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
if (command === "m" && params.length === 5 && params[1] === "2" && (start === "38" || start === "48")) params.splice(2, 0, "0");
|
|
143
|
-
if (command === "r" && params.length === 2 && params[1] === "0") params[1] = "-1";
|
|
144
|
-
return {
|
|
145
|
-
type,
|
|
146
|
-
pos: introducer.pos,
|
|
147
|
-
raw,
|
|
148
|
-
command,
|
|
149
|
-
params
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
//#endregion
|
|
154
|
-
//#region src/parsers/dcs.ts
|
|
155
|
-
const DCS_PATTERNS = new Map([
|
|
156
|
-
["$q", 2],
|
|
157
|
-
["+q", 2],
|
|
158
|
-
["+p", 2],
|
|
159
|
-
["|", 1],
|
|
160
|
-
["{", 1]
|
|
161
|
-
]);
|
|
162
|
-
function parseDCS(introducer, dataTokens, final) {
|
|
163
|
-
const data = dataTokens.map((t) => t.raw).join("");
|
|
164
|
-
const raw = introducer.raw + data + (final?.raw ?? "");
|
|
165
|
-
if (!data) return {
|
|
166
|
-
type: CODE_TYPES.DCS,
|
|
167
|
-
pos: introducer.pos,
|
|
168
|
-
raw,
|
|
169
|
-
command: "",
|
|
170
|
-
params: []
|
|
171
|
-
};
|
|
172
|
-
for (const [pattern, length] of DCS_PATTERNS) if (data.startsWith(pattern)) {
|
|
173
|
-
const remainder = data.slice(length);
|
|
174
|
-
const params = [];
|
|
175
|
-
if (remainder) for (const part of remainder.split(PARAM_SEPARATOR)) params.push(part || "-1");
|
|
176
|
-
return {
|
|
177
|
-
type: CODE_TYPES.DCS,
|
|
178
|
-
pos: introducer.pos,
|
|
179
|
-
raw,
|
|
180
|
-
command: pattern,
|
|
181
|
-
params
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
return {
|
|
185
|
-
type: CODE_TYPES.DCS,
|
|
186
|
-
pos: introducer.pos,
|
|
187
|
-
raw,
|
|
188
|
-
command: "",
|
|
189
|
-
params: [data]
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
//#endregion
|
|
194
|
-
//#region src/parsers/esc.ts
|
|
195
|
-
function parseESC(introducer, dataTokens, final) {
|
|
196
|
-
const data = dataTokens.map((t) => t.raw).join("");
|
|
197
|
-
const command = introducer.intermediate || (dataTokens[0]?.raw ?? final?.raw ?? "");
|
|
198
|
-
const params = introducer.intermediate ? final?.raw ? [final.raw] : [] : [];
|
|
199
|
-
const raw = introducer.raw + data + (final?.raw ?? "");
|
|
200
|
-
return {
|
|
201
|
-
type: CODE_TYPES.ESC,
|
|
202
|
-
pos: introducer.pos,
|
|
203
|
-
raw,
|
|
204
|
-
command,
|
|
205
|
-
params
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
//#endregion
|
|
210
|
-
//#region src/parsers/osc.ts
|
|
211
|
-
function parseOSC(introducer, dataTokens, final) {
|
|
212
|
-
const data = dataTokens.map((t) => t.raw).join("");
|
|
213
|
-
const raw = introducer.raw + data + (final?.raw || "");
|
|
214
|
-
const semicolonIndex = data.indexOf(";");
|
|
215
|
-
if (semicolonIndex === -1) return {
|
|
216
|
-
type: CODE_TYPES.OSC,
|
|
217
|
-
pos: introducer.pos,
|
|
218
|
-
raw,
|
|
219
|
-
command: data,
|
|
220
|
-
params: []
|
|
221
|
-
};
|
|
222
|
-
const command = data.slice(0, semicolonIndex);
|
|
223
|
-
const remainder = data.slice(semicolonIndex + 1);
|
|
224
|
-
if (command === "1337") return {
|
|
225
|
-
type: CODE_TYPES.OSC,
|
|
226
|
-
pos: introducer.pos,
|
|
227
|
-
raw,
|
|
228
|
-
command,
|
|
229
|
-
params: [remainder]
|
|
230
|
-
};
|
|
231
|
-
const params = [];
|
|
232
|
-
if (remainder) {
|
|
233
|
-
let current = "";
|
|
234
|
-
for (let i = 0; i < remainder.length; i++) if (remainder[i] === ";") {
|
|
235
|
-
params.push(current);
|
|
236
|
-
current = "";
|
|
237
|
-
} else current += remainder[i];
|
|
238
|
-
params.push(current);
|
|
239
|
-
}
|
|
240
|
-
return {
|
|
241
|
-
type: CODE_TYPES.OSC,
|
|
242
|
-
pos: introducer.pos,
|
|
243
|
-
raw,
|
|
244
|
-
command,
|
|
245
|
-
params
|
|
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
|
-
let data = "";
|
|
372
|
-
if (currentCode === CSI) while (i < input.length) {
|
|
373
|
-
const charCode = input.charCodeAt(i);
|
|
374
|
-
const char = input[i];
|
|
375
|
-
if (INTERRUPTERS.has(charCode)) {
|
|
376
|
-
if (data) yield emit$1({
|
|
377
|
-
type: TOKEN_TYPES.DATA,
|
|
378
|
-
pos,
|
|
379
|
-
raw: data
|
|
380
|
-
});
|
|
381
|
-
setState("GROUND");
|
|
382
|
-
if (C0_INTERRUPTERS.has(charCode)) i++;
|
|
383
|
-
break;
|
|
384
|
-
}
|
|
385
|
-
if (charCode >= 64 && charCode <= 126) {
|
|
386
|
-
if (data) yield emit$1({
|
|
387
|
-
type: TOKEN_TYPES.DATA,
|
|
388
|
-
pos,
|
|
389
|
-
raw: data
|
|
390
|
-
});
|
|
391
|
-
yield emit$1({
|
|
392
|
-
type: TOKEN_TYPES.FINAL,
|
|
393
|
-
pos: i,
|
|
394
|
-
raw: char
|
|
395
|
-
});
|
|
396
|
-
i++;
|
|
397
|
-
setState("GROUND");
|
|
398
|
-
break;
|
|
399
|
-
}
|
|
400
|
-
data += char;
|
|
401
|
-
i++;
|
|
402
|
-
}
|
|
403
|
-
else if (currentCode === ESC) {
|
|
404
|
-
if (i < input.length) {
|
|
405
|
-
const charCode = input.charCodeAt(i);
|
|
406
|
-
if (INTERRUPTERS.has(charCode)) {
|
|
407
|
-
setState("GROUND");
|
|
408
|
-
if (C0_INTERRUPTERS.has(charCode)) i++;
|
|
409
|
-
} else {
|
|
410
|
-
yield emit$1({
|
|
411
|
-
type: TOKEN_TYPES.FINAL,
|
|
412
|
-
pos: i,
|
|
413
|
-
raw: input[i]
|
|
414
|
-
});
|
|
415
|
-
i++;
|
|
416
|
-
setState("GROUND");
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
} else if (currentCode) while (i < input.length) {
|
|
420
|
-
const char = input[i];
|
|
421
|
-
const charCode = char.charCodeAt(0);
|
|
422
|
-
let terminator;
|
|
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;
|
|
426
|
-
if (terminator) {
|
|
427
|
-
if (data) yield emit$1({
|
|
428
|
-
type: TOKEN_TYPES.DATA,
|
|
429
|
-
pos,
|
|
430
|
-
raw: data
|
|
431
|
-
});
|
|
432
|
-
yield emit$1({
|
|
433
|
-
type: TOKEN_TYPES.FINAL,
|
|
434
|
-
pos: i,
|
|
435
|
-
raw: terminator
|
|
436
|
-
});
|
|
437
|
-
i += terminator.length;
|
|
438
|
-
setState("GROUND");
|
|
439
|
-
break;
|
|
440
|
-
}
|
|
441
|
-
if (INTERRUPTERS.has(charCode)) {
|
|
442
|
-
if (data) yield emit$1({
|
|
443
|
-
type: TOKEN_TYPES.DATA,
|
|
444
|
-
pos,
|
|
445
|
-
raw: data
|
|
446
|
-
});
|
|
447
|
-
setState("GROUND");
|
|
448
|
-
if (C0_INTERRUPTERS.has(charCode)) i++;
|
|
449
|
-
break;
|
|
450
|
-
}
|
|
451
|
-
data += char;
|
|
452
|
-
i++;
|
|
453
|
-
}
|
|
454
|
-
if (state === "SEQUENCE") setState("GROUND");
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
function tokenize(input) {
|
|
458
|
-
return Array.from(tokenizer(input));
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
//#endregion
|
|
462
|
-
//#region src/parse.ts
|
|
463
|
-
function emit(token) {
|
|
464
|
-
return token;
|
|
465
|
-
}
|
|
466
|
-
function* parser(tokens) {
|
|
467
|
-
let current = tokens.next();
|
|
468
|
-
while (!current.done) {
|
|
469
|
-
const token = current.value;
|
|
470
|
-
if (token.type === TOKEN_TYPES.TEXT) {
|
|
471
|
-
yield emit({
|
|
472
|
-
type: CODE_TYPES.TEXT,
|
|
473
|
-
pos: token.pos,
|
|
474
|
-
raw: token.raw
|
|
475
|
-
});
|
|
476
|
-
current = tokens.next();
|
|
477
|
-
continue;
|
|
478
|
-
}
|
|
479
|
-
if (token.type === TOKEN_TYPES.INTRODUCER) {
|
|
480
|
-
const introducer = token;
|
|
481
|
-
const data = [];
|
|
482
|
-
let final;
|
|
483
|
-
current = tokens.next();
|
|
484
|
-
while (!current.done) {
|
|
485
|
-
const nextToken = current.value;
|
|
486
|
-
if (nextToken.type === TOKEN_TYPES.DATA) data.push(nextToken);
|
|
487
|
-
else if (nextToken.type === TOKEN_TYPES.FINAL) {
|
|
488
|
-
final = nextToken;
|
|
489
|
-
current = tokens.next();
|
|
490
|
-
break;
|
|
491
|
-
} else if (nextToken.type === TOKEN_TYPES.INTRODUCER) break;
|
|
492
|
-
else if (nextToken.type === TOKEN_TYPES.TEXT) break;
|
|
493
|
-
current = tokens.next();
|
|
494
|
-
}
|
|
495
|
-
switch (introducer.code) {
|
|
496
|
-
case CSI_CODE:
|
|
497
|
-
yield emit(parseCSI(introducer, data, final));
|
|
498
|
-
break;
|
|
499
|
-
case OSC_CODE:
|
|
500
|
-
yield emit(parseOSC(introducer, data, final));
|
|
501
|
-
break;
|
|
502
|
-
case DCS_CODE:
|
|
503
|
-
case DCS_OPEN:
|
|
504
|
-
yield emit(parseDCS(introducer, data, final));
|
|
505
|
-
break;
|
|
506
|
-
case APC_CODE:
|
|
507
|
-
case APC_OPEN:
|
|
508
|
-
yield emit(parseAPC(introducer, data, final));
|
|
509
|
-
break;
|
|
510
|
-
case PM_CODE:
|
|
511
|
-
case PM_OPEN:
|
|
512
|
-
yield emit(parsePM(introducer, data, final));
|
|
513
|
-
break;
|
|
514
|
-
case SOS_CODE:
|
|
515
|
-
case SOS_OPEN:
|
|
516
|
-
yield emit(parseSOS(introducer, data, final));
|
|
517
|
-
break;
|
|
518
|
-
case ESC_CODE:
|
|
519
|
-
yield emit(parseESC(introducer, data, final));
|
|
520
|
-
break;
|
|
521
|
-
}
|
|
522
|
-
} else current = tokens.next();
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
function parse(input) {
|
|
526
|
-
return Array.from(parser(tokenizer(input)));
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
//#endregion
|
|
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 };
|