Repository navigation
Expand file tree
/
Copy pathshell-tokenize.ts
More file actions
346 lines (320 loc) · 9.78 KB
/
Copy pathshell-tokenize.ts
File metadata and controls
346 lines (320 loc) · 9.78 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
import { Effect } from "effect"
import { parse as shellQuoteParse } from "shell-quote"
export interface Argv {
line: number
tokens: string[]
}
export interface ParseError {
kind: "unclosed-quote" | "unsupported-operator" | "unclosed-heredoc" | "internal"
line: number
detail: string
}
// Null-byte delimited placeholder that shell-quote passes through verbatim.
function heredocPlaceholder(n: number) {
return `\x00HD${n}\x00`
}
type HeredocResult =
| { ok: true; stripped: string; bodies: string[] }
| { ok: false; error: ParseError }
function extractHeredocs(script: string): HeredocResult {
const bodies: string[] = []
let out = ""
let quote: '"' | "'" | null = null
let i = 0
let line = 1
while (i < script.length) {
const ch = script[i]
// --- Inside a quoted string: pass through verbatim ---
if (quote) {
if (ch === "\\" && quote === '"' && i + 1 < script.length) {
out += ch + script[i + 1]
if (script[i + 1] === "\n") line++
i += 2
continue
}
if (ch === quote) {
quote = null
out += ch
i++
continue
}
if (ch === "\n") line++
out += ch
i++
continue
}
// --- At top level ---
if (ch === '"' || ch === "'") {
quote = ch
out += ch
i++
continue
}
// Check for `<<` at top level (but not `<<<` herestring)
if (ch === "<" && script[i + 1] === "<") {
// `<<<` herestring: pass all three chars through so the existing operator
// check rejects them.
if (script[i + 2] === "<") {
out += "<<< "
i += 3
// Skip to newline so the rest of the line goes through normally
while (i < script.length && script[i] !== "\n") i++
continue
}
// Try to read a marker name starting at i+2
let j = i + 2
const markerStart = j
// Marker must start with letter or underscore
if (j < script.length && /[A-Za-z_]/.test(script[j])) {
while (j < script.length && /[A-Za-z0-9_]/.test(script[j])) j++
const marker = script.slice(markerStart, j)
// After marker: only optional whitespace, then newline (or EOF)
let k = j
while (k < script.length && (script[k] === " " || script[k] === "\t")) k++
if (k < script.length && script[k] !== "\n") {
// Non-whitespace after <<MARKER on the same line
return {
ok: false,
error: {
kind: "unsupported-operator",
line,
detail: "tokens after <<MARKER on the same line are not supported",
},
}
}
// Valid heredoc open: emit placeholder on the same line in output.
// Body lines and the closing marker are replaced with empty lines so
// that splitTopLevelLines sees the correct original line numbers.
const bodyIndex = bodies.length
const openLine = line
out += heredocPlaceholder(bodyIndex) + "\n"
// Advance past <<MARKER + optional whitespace + \n
i = k + 1
line++
// Collect body lines until we see a line that trims to exactly `marker`
const bodyLines: string[] = []
let closed = false
while (i < script.length) {
// Read one line (up to but not including its \n, or EOF)
let lineEnd = i
while (lineEnd < script.length && script[lineEnd] !== "\n") lineEnd++
const bodyLine = script.slice(i, lineEnd)
// Emit a blank line to preserve original line numbering
out += "\n"
if (bodyLine.trim() === marker) {
// Closing line — consume it (and trailing \n if present)
i = lineEnd + (lineEnd < script.length ? 1 : 0)
line++
closed = true
break
}
bodyLines.push(bodyLine)
i = lineEnd + (lineEnd < script.length ? 1 : 0)
line++
}
if (!closed) {
return {
ok: false,
error: {
kind: "unclosed-heredoc",
line: openLine,
detail: `unclosed heredoc <<${marker}`,
},
}
}
bodies.push(bodyLines.join("\n"))
continue
}
// `<<` without a valid marker — pass through so the existing operator
// check rejects it.
out += ch
i++
continue
}
out += ch
if (ch === "\n") line++
i++
}
return { ok: true, stripped: out, bodies }
}
// Regex matching a heredoc placeholder token produced by extractHeredocs.
const HD_RE = /^\x00HD(\d+)\x00$/
// POSIX-style comment pre-pass: operates on the post-heredoc-extraction text so
// heredoc bodies are already removed. Drops word-boundary `#` to end-of-line,
// escapes mid-token `#` so shell-quote treats it as a literal character.
function preprocessComments(input: string): string {
let out = ""
let i = 0
let quote: '"' | "'" | null = null
let prevWasBoundary = true // start of input is a word boundary
while (i < input.length) {
const ch = input[i]
if (quote) {
out += ch
if (ch === "\\" && quote === '"' && i + 1 < input.length) {
out += input[i + 1]
i += 2
continue
}
if (ch === quote) quote = null
i++
prevWasBoundary = false
continue
}
if (ch === '"' || ch === "'") {
quote = ch
out += ch
i++
prevWasBoundary = false
continue
}
if (ch === "\\") {
if (i + 1 < input.length && input[i + 1] === "#") {
// Explicit \# escape: pass through verbatim so shell-quote produces literal #
out += "\\#"
i += 2
prevWasBoundary = false
continue
}
// Other backslash: pass through
out += ch
i++
prevWasBoundary = false
continue
}
if (ch === "#") {
if (prevWasBoundary) {
// word-boundary # → real comment, drop to end of line (don't consume the \n)
while (i < input.length && input[i] !== "\n") i++
continue
}
// mid-token # → escape so shell-quote treats as literal
out += "\\#"
i++
prevWasBoundary = false
continue
}
out += ch
prevWasBoundary = ch === "\n" || /\s/.test(ch)
i++
}
return out
}
export function tokenize(script: string): Effect.Effect<Argv[], ParseError> {
return Effect.suspend(() => {
if (script.trim() === "") return Effect.succeed([] as Argv[])
const heredocResult = extractHeredocs(script)
if (!heredocResult.ok) return Effect.fail(heredocResult.error)
const { stripped, bodies } = heredocResult
const segments = splitTopLevelLines(preprocessComments(stripped)).filter((seg) => seg.text.trim() !== "")
const out: Argv[] = []
for (const seg of segments) {
const unclosed = scanUnclosedQuote(seg.text)
if (unclosed) {
return Effect.fail<ParseError>({
kind: "unclosed-quote",
line: seg.line,
detail: `unclosed ${unclosed}-quoted string`,
})
}
const segTokens = shellQuoteParse(seg.text, (name: string) => "$" + name, { escape: "\\" })
const stringTokens: string[] = []
for (const t of segTokens) {
if (typeof t === "string") {
const match = HD_RE.exec(t)
if (match) {
stringTokens.push(bodies[parseInt(match[1], 10)])
continue
}
stringTokens.push(t)
continue
}
if (typeof t === "object" && t !== null && "op" in t) {
const tok = t as { op: string; pattern?: string }
const detail =
tok.op === "glob" && tok.pattern != null
? `unsupported glob pattern: ${tok.pattern}`
: `unsupported shell operator: ${tok.op}`
return Effect.fail<ParseError>({
kind: "unsupported-operator",
line: seg.line,
detail,
})
}
}
if (stringTokens.length > 0) out.push({ line: seg.line, tokens: stringTokens })
}
return Effect.succeed(out)
})
}
function scanUnclosedQuote(segment: string): '"' | "'" | null {
let quote: '"' | "'" | null = null
for (let i = 0; i < segment.length; i++) {
const ch = segment[i]
if (quote) {
if (ch === "\\" && quote === '"' && i + 1 < segment.length) {
i++
continue
}
if (ch === quote) quote = null
continue
}
if (ch === '"' || ch === "'") quote = ch
}
return quote
}
// Split the script into segments separated by line breaks that are NOT inside
// quoted strings. Returns each segment's starting line (1-indexed) and text.
function splitTopLevelLines(script: string): Array<{ line: number; text: string }> {
const segments: Array<{ line: number; text: string }> = []
let buf = ""
let segStart = 1
let line = 1
let quote: '"' | "'" | null = null
let i = 0
while (i < script.length) {
const ch = script[i]
if (quote) {
if (ch === "\\" && quote === '"' && i + 1 < script.length) {
buf += ch + script[i + 1]
if (script[i + 1] === "\n") line++
i += 2
continue
}
if (ch === quote) {
quote = null
buf += ch
i++
continue
}
if (ch === "\n") line++
buf += ch
i++
continue
}
if (ch === '"' || ch === "'") {
quote = ch
buf += ch
i++
continue
}
if (ch === "\\" && i + 1 < script.length && script[i + 1] === "\n") {
// POSIX line continuation: \<LF> disappears, next physical line continues this command
line++
i += 2
continue
}
if (ch === "\n") {
segments.push({ line: segStart, text: buf })
buf = ""
line++
segStart = line
i++
continue
}
buf += ch
i++
}
if (buf.length > 0) segments.push({ line: segStart, text: buf })
return segments
}