Repository navigation
Expand file tree
/
Copy pathparser.ts
More file actions
405 lines (354 loc) · 9.41 KB
/
Copy pathparser.ts
File metadata and controls
405 lines (354 loc) · 9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Shell Parser
*
* State-machine tokenizer + single-pass parser for shell command strings.
* No regex. Produces a flat list of SimpleCommands for security classification.
*
* Handles: single/double quoting, backslash escaping, pipes, &&, ||, ;,
* redirects (>, >>, 2>, &>, &>>), command substitution $(), backtick
* substitution, parenthesized subshells, variable assignments.
*/
// ─── Tokens ──────────────────────────────────────────────────
export type RedirectOp = '>' | '>>' | '2>' | '&>' | '&>>'
export type Token =
| { type: 'Word'; value: string }
| { type: 'Pipe' }
| { type: 'And' }
| { type: 'Or' }
| { type: 'Semi' }
| { type: 'Redirect'; op: RedirectOp }
// ─── AST ─────────────────────────────────────────────────────
export interface Assignment {
name: string
value: string
}
export interface Redirect {
op: RedirectOp
target: string
}
export interface SimpleCommand {
assignments: Assignment[]
name: string | null
args: string[]
redirects: Redirect[]
}
// ─── Tokenizer ───────────────────────────────────────────────
export function tokenize(input: string): Token[] {
const tokens: Token[] = []
const len = input.length
let word = ''
let i = 0
// Top-level quoting state
let singleQ = false
let doubleQ = false
let escaped = false
// $() tracking (with inner quoting for paren balancing)
let subDepth = 0
let subSingleQ = false
let subDoubleQ = false
let subEscaped = false
// Backtick tracking
let backtick = false
// Subshell (bare parens) depth
let parenDepth = 0
function flush() {
if (word.length > 0) {
tokens.push({ type: 'Word', value: word })
word = ''
}
}
while (i < len) {
const ch = input[i]
// ── Escape (top level) ──────────────────────
if (escaped) {
word += ch
escaped = false
i++
continue
}
// ── Backtick mode ───────────────────────────
if (backtick) {
word += ch
if (ch === '`') backtick = false
i++
continue
}
// ── $() mode ────────────────────────────────
if (subDepth > 0) {
word += ch
if (subEscaped) {
subEscaped = false
} else if (subSingleQ) {
if (ch === "'") subSingleQ = false
} else if (subDoubleQ) {
if (ch === '\\') {
subEscaped = true
} else if (ch === '"') {
subDoubleQ = false
} else if (ch === '$' && i + 1 < len && input[i + 1] === '(') {
subDepth++
word += input[i + 1]
i += 2
continue
}
} else {
if (ch === '\\') subEscaped = true
else if (ch === "'") subSingleQ = true
else if (ch === '"') subDoubleQ = true
else if (ch === '`') {
// Backtick inside $() — just track for balancing
// We accumulate as-is (already done above)
}
else if (ch === '(') subDepth++
else if (ch === ')') subDepth--
}
i++
continue
}
// ── Single-quote mode ───────────────────────
if (singleQ) {
if (ch === "'") {
singleQ = false
} else {
word += ch
}
i++
continue
}
// ── Double-quote mode ───────────────────────
if (doubleQ) {
if (ch === '\\') {
escaped = true
i++
continue
}
if (ch === '"') {
doubleQ = false
i++
continue
}
if (ch === '`') {
word += ch
backtick = true
i++
continue
}
if (ch === '$' && i + 1 < len && input[i + 1] === '(') {
word += '$('
subDepth = 1
subSingleQ = false
subDoubleQ = false
subEscaped = false
i += 2
continue
}
word += ch
i++
continue
}
// ── Normal mode ─────────────────────────────
// Backslash
if (ch === '\\') {
escaped = true
i++
continue
}
// Quote starts
if (ch === "'") {
singleQ = true
i++
continue
}
if (ch === '"') {
doubleQ = true
i++
continue
}
if (ch === '`') {
word += ch
backtick = true
i++
continue
}
// $() start
if (ch === '$' && i + 1 < len && input[i + 1] === '(') {
word += '$('
subDepth = 1
subSingleQ = false
subDoubleQ = false
subEscaped = false
i += 2
continue
}
// Whitespace
if (ch === ' ' || ch === '\t') {
flush()
i++
continue
}
// Newline
if (ch === '\n') {
flush()
tokens.push({ type: 'Semi' })
i++
continue
}
// Semicolon
if (ch === ';') {
flush()
tokens.push({ type: 'Semi' })
i++
continue
}
// Pipe or Or
if (ch === '|') {
flush()
if (i + 1 < len && input[i + 1] === '|') {
tokens.push({ type: 'Or' })
i += 2
} else {
tokens.push({ type: 'Pipe' })
i++
}
continue
}
// And, &>, &>>, or background &
if (ch === '&') {
flush()
if (i + 1 < len && input[i + 1] === '&') {
tokens.push({ type: 'And' })
i += 2
} else if (i + 1 < len && input[i + 1] === '>') {
if (i + 2 < len && input[i + 2] === '>') {
tokens.push({ type: 'Redirect', op: '&>>' })
i += 3
} else {
tokens.push({ type: 'Redirect', op: '&>' })
i += 2
}
} else {
// Background & — treat as separator
tokens.push({ type: 'Semi' })
i++
}
continue
}
// Redirect: >, >>, 2>, 2>>
if (ch === '>') {
const isFd2 = word === '2'
if (isFd2) {
word = ''
} else {
flush()
}
if (i + 1 < len && input[i + 1] === '>') {
// >> or 2>> (both are append writes, map 2>> to >>)
tokens.push({ type: 'Redirect', op: '>>' })
i += 2
} else {
tokens.push({ type: 'Redirect', op: isFd2 ? '2>' : '>' })
i++
}
continue
}
// Subshell open paren
if (ch === '(') {
flush()
tokens.push({ type: 'Semi' })
parenDepth++
i++
continue
}
// Subshell close paren
if (ch === ')' && parenDepth > 0) {
flush()
tokens.push({ type: 'Semi' })
parenDepth--
i++
continue
}
// Default: accumulate into word
word += ch
i++
}
flush()
return tokens
}
// ─── Parser ──────────────────────────────────────────────────
export function parse(tokens: Token[]): SimpleCommand[] {
const commands: SimpleCommand[] = []
let start = 0
for (let i = 0; i <= tokens.length; i++) {
if (i === tokens.length || isSeparator(tokens[i])) {
if (i > start) {
const segment = tokens.slice(start, i)
commands.push(parseSegment(segment))
}
start = i + 1
}
}
return commands
}
function isSeparator(token: Token): boolean {
const t = token.type
return t === 'Pipe' || t === 'And' || t === 'Or' || t === 'Semi'
}
function parseSegment(tokens: Token[]): SimpleCommand {
const assignments: Assignment[] = []
const args: string[] = []
const redirects: Redirect[] = []
let name: string | null = null
let foundCommand = false
let i = 0
while (i < tokens.length) {
const tok = tokens[i]
if (tok.type === 'Redirect') {
const next = tokens[i + 1]
if (next && next.type === 'Word') {
redirects.push({ op: tok.op, target: next.value })
i += 2
} else {
i++
}
continue
}
if (tok.type === 'Word') {
if (!foundCommand && isAssignment(tok.value)) {
const eq = tok.value.indexOf('=')
assignments.push({
name: tok.value.slice(0, eq),
value: tok.value.slice(eq + 1)
})
} else if (!foundCommand) {
name = tok.value
foundCommand = true
} else {
args.push(tok.value)
}
i++
continue
}
i++
}
return { assignments, name, args, redirects }
}
function isAssignment(word: string): boolean {
const eq = word.indexOf('=')
if (eq <= 0) return false
for (let j = 0; j < eq; j++) {
const c = word.charCodeAt(j)
const isLetter = (c >= 65 && c <= 90) || (c >= 97 && c <= 122)
const isUnderscore = c === 95
if (j === 0) {
if (!isLetter && !isUnderscore) return false
} else {
const isDigit = c >= 48 && c <= 57
if (!isLetter && !isDigit && !isUnderscore) return false
}
}
return true
}
// ─── Public API ──────────────────────────────────────────────
export function parseShellCommand(input: string): SimpleCommand[] {
return parse(tokenize(input))
}