bash-ast 0.1.0
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/LICENSE +21 -0
- package/README.md +122 -0
- package/clean.d.ts +14 -0
- package/clean.js +66 -0
- package/deparser.d.ts +100 -0
- package/deparser.js +299 -0
- package/esm/clean.js +60 -0
- package/esm/deparser.js +294 -0
- package/esm/index.js +10 -0
- package/esm/lexer.js +447 -0
- package/esm/parser.js +662 -0
- package/esm/types.js +1 -0
- package/index.d.ts +5 -0
- package/index.js +37 -0
- package/lexer.d.ts +130 -0
- package/lexer.js +451 -0
- package/package.json +42 -0
- package/parser.d.ts +135 -0
- package/parser.js +667 -0
- package/types.d.ts +203 -0
- package/types.js +2 -0
package/esm/parser.js
ADDED
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
import { Lexer, TokenType } from './lexer';
|
|
2
|
+
/**
|
|
3
|
+
* Bash Parser
|
|
4
|
+
*/
|
|
5
|
+
export class Parser {
|
|
6
|
+
tokens = [];
|
|
7
|
+
pos = 0;
|
|
8
|
+
options;
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parse bash source into AST
|
|
14
|
+
*/
|
|
15
|
+
parse(source) {
|
|
16
|
+
const lexer = new Lexer(source);
|
|
17
|
+
this.tokens = lexer.tokenize();
|
|
18
|
+
this.pos = 0;
|
|
19
|
+
const commands = [];
|
|
20
|
+
this.skipNewlines();
|
|
21
|
+
while (!this.isAtEnd()) {
|
|
22
|
+
const command = this.parseCommand();
|
|
23
|
+
if (command) {
|
|
24
|
+
commands.push(command);
|
|
25
|
+
}
|
|
26
|
+
this.skipNewlinesAndSeparators();
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
type: 'Script',
|
|
30
|
+
commands
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if at end of tokens
|
|
35
|
+
*/
|
|
36
|
+
isAtEnd() {
|
|
37
|
+
return this.pos >= this.tokens.length || this.peek().type === TokenType.EOF;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Peek at current token
|
|
41
|
+
*/
|
|
42
|
+
peek(offset = 0) {
|
|
43
|
+
const idx = this.pos + offset;
|
|
44
|
+
if (idx >= this.tokens.length) {
|
|
45
|
+
return this.tokens[this.tokens.length - 1];
|
|
46
|
+
}
|
|
47
|
+
return this.tokens[idx];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Advance to next token
|
|
51
|
+
*/
|
|
52
|
+
advance() {
|
|
53
|
+
if (!this.isAtEnd()) {
|
|
54
|
+
this.pos++;
|
|
55
|
+
}
|
|
56
|
+
return this.tokens[this.pos - 1];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Check if current token matches type
|
|
60
|
+
*/
|
|
61
|
+
check(type) {
|
|
62
|
+
return this.peek().type === type;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Consume token if it matches, otherwise error
|
|
66
|
+
*/
|
|
67
|
+
expect(type) {
|
|
68
|
+
if (this.check(type)) {
|
|
69
|
+
return this.advance();
|
|
70
|
+
}
|
|
71
|
+
throw new Error(`Expected ${type}, got ${this.peek().type} at position ${this.pos}`);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Skip newlines
|
|
75
|
+
*/
|
|
76
|
+
skipNewlines() {
|
|
77
|
+
while (this.check(TokenType.NEWLINE)) {
|
|
78
|
+
this.advance();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Skip newlines and separators
|
|
83
|
+
*/
|
|
84
|
+
skipNewlinesAndSeparators() {
|
|
85
|
+
while (this.check(TokenType.NEWLINE) || this.check(TokenType.SEMI) || this.check(TokenType.AMP)) {
|
|
86
|
+
this.advance();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Parse a command (handles logical operators)
|
|
91
|
+
*/
|
|
92
|
+
parseCommand() {
|
|
93
|
+
return this.parseLogicalOr();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse logical OR (||)
|
|
97
|
+
*/
|
|
98
|
+
parseLogicalOr() {
|
|
99
|
+
let left = this.parseLogicalAnd();
|
|
100
|
+
if (!left)
|
|
101
|
+
return null;
|
|
102
|
+
while (this.check(TokenType.OR_IF)) {
|
|
103
|
+
this.advance();
|
|
104
|
+
this.skipNewlines();
|
|
105
|
+
const right = this.parseLogicalAnd();
|
|
106
|
+
if (!right) {
|
|
107
|
+
throw new Error('Expected command after ||');
|
|
108
|
+
}
|
|
109
|
+
left = {
|
|
110
|
+
type: 'LogicalExpression',
|
|
111
|
+
op: '||',
|
|
112
|
+
left,
|
|
113
|
+
right
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return left;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Parse logical AND (&&)
|
|
120
|
+
*/
|
|
121
|
+
parseLogicalAnd() {
|
|
122
|
+
let left = this.parsePipeline();
|
|
123
|
+
if (!left)
|
|
124
|
+
return null;
|
|
125
|
+
while (this.check(TokenType.AND_IF)) {
|
|
126
|
+
this.advance();
|
|
127
|
+
this.skipNewlines();
|
|
128
|
+
const right = this.parsePipeline();
|
|
129
|
+
if (!right) {
|
|
130
|
+
throw new Error('Expected command after &&');
|
|
131
|
+
}
|
|
132
|
+
left = {
|
|
133
|
+
type: 'LogicalExpression',
|
|
134
|
+
op: '&&',
|
|
135
|
+
left,
|
|
136
|
+
right
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return left;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Parse pipeline (|)
|
|
143
|
+
*/
|
|
144
|
+
parsePipeline() {
|
|
145
|
+
let bang = false;
|
|
146
|
+
if (this.check(TokenType.BANG)) {
|
|
147
|
+
bang = true;
|
|
148
|
+
this.advance();
|
|
149
|
+
}
|
|
150
|
+
const first = this.parseCompoundCommand();
|
|
151
|
+
if (!first)
|
|
152
|
+
return null;
|
|
153
|
+
const commands = [first];
|
|
154
|
+
while (this.check(TokenType.PIPE)) {
|
|
155
|
+
this.advance();
|
|
156
|
+
this.skipNewlines();
|
|
157
|
+
const next = this.parseCompoundCommand();
|
|
158
|
+
if (!next) {
|
|
159
|
+
throw new Error('Expected command after |');
|
|
160
|
+
}
|
|
161
|
+
commands.push(next);
|
|
162
|
+
}
|
|
163
|
+
if (commands.length === 1 && !bang) {
|
|
164
|
+
return first;
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
type: 'Pipeline',
|
|
168
|
+
commands,
|
|
169
|
+
bang: bang || undefined
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Parse compound command or simple command
|
|
174
|
+
*/
|
|
175
|
+
parseCompoundCommand() {
|
|
176
|
+
const token = this.peek();
|
|
177
|
+
switch (token.type) {
|
|
178
|
+
case TokenType.IF:
|
|
179
|
+
return this.parseIf();
|
|
180
|
+
case TokenType.WHILE:
|
|
181
|
+
return this.parseWhile();
|
|
182
|
+
case TokenType.UNTIL:
|
|
183
|
+
return this.parseUntil();
|
|
184
|
+
case TokenType.FOR:
|
|
185
|
+
return this.parseFor();
|
|
186
|
+
case TokenType.CASE:
|
|
187
|
+
return this.parseCase();
|
|
188
|
+
case TokenType.LPAREN:
|
|
189
|
+
return this.parseSubshell();
|
|
190
|
+
case TokenType.LBRACE:
|
|
191
|
+
return this.parseBraceGroup();
|
|
192
|
+
case TokenType.FUNCTION:
|
|
193
|
+
return this.parseFunction();
|
|
194
|
+
default:
|
|
195
|
+
// Check for function definition (name () { ... })
|
|
196
|
+
if (token.type === TokenType.WORD && this.peek(1).type === TokenType.LPAREN && this.peek(2).type === TokenType.RPAREN) {
|
|
197
|
+
return this.parseFunctionShorthand();
|
|
198
|
+
}
|
|
199
|
+
return this.parseSimpleCommand();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Parse simple command
|
|
204
|
+
*/
|
|
205
|
+
parseSimpleCommand() {
|
|
206
|
+
const prefix = [];
|
|
207
|
+
const suffix = [];
|
|
208
|
+
let name;
|
|
209
|
+
// Parse prefix (assignments before command name)
|
|
210
|
+
while (this.check(TokenType.ASSIGNMENT_WORD)) {
|
|
211
|
+
const token = this.advance();
|
|
212
|
+
prefix.push({
|
|
213
|
+
type: 'AssignmentWord',
|
|
214
|
+
text: token.value,
|
|
215
|
+
range: token.range
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
// Parse redirections before command name
|
|
219
|
+
while (this.isRedirectOp()) {
|
|
220
|
+
const redirect = this.parseRedirect();
|
|
221
|
+
if (redirect)
|
|
222
|
+
suffix.push(redirect);
|
|
223
|
+
}
|
|
224
|
+
// Parse command name
|
|
225
|
+
if (this.check(TokenType.WORD)) {
|
|
226
|
+
const token = this.advance();
|
|
227
|
+
name = {
|
|
228
|
+
type: 'Word',
|
|
229
|
+
text: token.value,
|
|
230
|
+
range: token.range
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
// Parse suffix (arguments and redirections)
|
|
234
|
+
while (true) {
|
|
235
|
+
if (this.isRedirectOp()) {
|
|
236
|
+
const redirect = this.parseRedirect();
|
|
237
|
+
if (redirect)
|
|
238
|
+
suffix.push(redirect);
|
|
239
|
+
}
|
|
240
|
+
else if (this.check(TokenType.WORD)) {
|
|
241
|
+
const token = this.advance();
|
|
242
|
+
suffix.push({
|
|
243
|
+
type: 'Word',
|
|
244
|
+
text: token.value,
|
|
245
|
+
range: token.range
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (!name && prefix.length === 0 && suffix.length === 0) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
type: 'SimpleCommand',
|
|
257
|
+
name,
|
|
258
|
+
prefix: prefix.length > 0 ? prefix : undefined,
|
|
259
|
+
suffix: suffix.length > 0 ? suffix : undefined
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Check if current token is a redirect operator
|
|
264
|
+
*/
|
|
265
|
+
isRedirectOp() {
|
|
266
|
+
const type = this.peek().type;
|
|
267
|
+
return type === TokenType.LESS ||
|
|
268
|
+
type === TokenType.GREAT ||
|
|
269
|
+
type === TokenType.DLESS ||
|
|
270
|
+
type === TokenType.DGREAT ||
|
|
271
|
+
type === TokenType.LESSAND ||
|
|
272
|
+
type === TokenType.GREATAND ||
|
|
273
|
+
type === TokenType.LESSGREAT ||
|
|
274
|
+
type === TokenType.DLESSDASH ||
|
|
275
|
+
type === TokenType.CLOBBER ||
|
|
276
|
+
type === TokenType.TLESS ||
|
|
277
|
+
type === TokenType.IO_NUMBER;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Parse redirect
|
|
281
|
+
*/
|
|
282
|
+
parseRedirect() {
|
|
283
|
+
let numberIo;
|
|
284
|
+
// Check for IO number
|
|
285
|
+
if (this.check(TokenType.IO_NUMBER)) {
|
|
286
|
+
numberIo = parseInt(this.advance().value, 10);
|
|
287
|
+
}
|
|
288
|
+
const opToken = this.peek();
|
|
289
|
+
let op;
|
|
290
|
+
switch (opToken.type) {
|
|
291
|
+
case TokenType.LESS:
|
|
292
|
+
op = '<';
|
|
293
|
+
break;
|
|
294
|
+
case TokenType.GREAT:
|
|
295
|
+
op = '>';
|
|
296
|
+
break;
|
|
297
|
+
case TokenType.DLESS:
|
|
298
|
+
op = '<<';
|
|
299
|
+
break;
|
|
300
|
+
case TokenType.DGREAT:
|
|
301
|
+
op = '>>';
|
|
302
|
+
break;
|
|
303
|
+
case TokenType.LESSAND:
|
|
304
|
+
op = '<&';
|
|
305
|
+
break;
|
|
306
|
+
case TokenType.GREATAND:
|
|
307
|
+
op = '>&';
|
|
308
|
+
break;
|
|
309
|
+
case TokenType.LESSGREAT:
|
|
310
|
+
op = '<>';
|
|
311
|
+
break;
|
|
312
|
+
case TokenType.DLESSDASH:
|
|
313
|
+
op = '<<-';
|
|
314
|
+
break;
|
|
315
|
+
case TokenType.CLOBBER:
|
|
316
|
+
op = '>|';
|
|
317
|
+
break;
|
|
318
|
+
case TokenType.TLESS:
|
|
319
|
+
op = '<<<';
|
|
320
|
+
break;
|
|
321
|
+
default:
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
this.advance();
|
|
325
|
+
// Parse file/word
|
|
326
|
+
if (this.check(TokenType.WORD)) {
|
|
327
|
+
const fileToken = this.advance();
|
|
328
|
+
return {
|
|
329
|
+
type: 'Redirect',
|
|
330
|
+
op,
|
|
331
|
+
file: {
|
|
332
|
+
type: 'Word',
|
|
333
|
+
text: fileToken.value,
|
|
334
|
+
range: fileToken.range
|
|
335
|
+
},
|
|
336
|
+
numberIo,
|
|
337
|
+
range: opToken.range
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
type: 'Redirect',
|
|
342
|
+
op,
|
|
343
|
+
numberIo,
|
|
344
|
+
range: opToken.range
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Parse if clause
|
|
349
|
+
*/
|
|
350
|
+
parseIf() {
|
|
351
|
+
const start = this.expect(TokenType.IF);
|
|
352
|
+
this.skipNewlines();
|
|
353
|
+
const condition = this.parseCompoundList();
|
|
354
|
+
this.expect(TokenType.THEN);
|
|
355
|
+
this.skipNewlines();
|
|
356
|
+
const then = this.parseCompoundList();
|
|
357
|
+
let elseClause;
|
|
358
|
+
if (this.check(TokenType.ELIF)) {
|
|
359
|
+
elseClause = this.parseElif();
|
|
360
|
+
}
|
|
361
|
+
else if (this.check(TokenType.ELSE)) {
|
|
362
|
+
this.advance();
|
|
363
|
+
this.skipNewlines();
|
|
364
|
+
elseClause = this.parseCompoundList();
|
|
365
|
+
}
|
|
366
|
+
this.expect(TokenType.FI);
|
|
367
|
+
return {
|
|
368
|
+
type: 'IfClause',
|
|
369
|
+
condition,
|
|
370
|
+
then,
|
|
371
|
+
else: elseClause,
|
|
372
|
+
range: start.range
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Parse elif as nested if
|
|
377
|
+
*/
|
|
378
|
+
parseElif() {
|
|
379
|
+
this.expect(TokenType.ELIF);
|
|
380
|
+
this.skipNewlines();
|
|
381
|
+
const condition = this.parseCompoundList();
|
|
382
|
+
this.expect(TokenType.THEN);
|
|
383
|
+
this.skipNewlines();
|
|
384
|
+
const then = this.parseCompoundList();
|
|
385
|
+
let elseClause;
|
|
386
|
+
if (this.check(TokenType.ELIF)) {
|
|
387
|
+
elseClause = this.parseElif();
|
|
388
|
+
}
|
|
389
|
+
else if (this.check(TokenType.ELSE)) {
|
|
390
|
+
this.advance();
|
|
391
|
+
this.skipNewlines();
|
|
392
|
+
elseClause = this.parseCompoundList();
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
type: 'IfClause',
|
|
396
|
+
condition,
|
|
397
|
+
then,
|
|
398
|
+
else: elseClause
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Parse while clause
|
|
403
|
+
*/
|
|
404
|
+
parseWhile() {
|
|
405
|
+
const start = this.expect(TokenType.WHILE);
|
|
406
|
+
this.skipNewlines();
|
|
407
|
+
const condition = this.parseCompoundList();
|
|
408
|
+
this.expect(TokenType.DO);
|
|
409
|
+
this.skipNewlines();
|
|
410
|
+
const body = this.parseCompoundList();
|
|
411
|
+
this.expect(TokenType.DONE);
|
|
412
|
+
return {
|
|
413
|
+
type: 'WhileClause',
|
|
414
|
+
condition,
|
|
415
|
+
body,
|
|
416
|
+
range: start.range
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Parse until clause
|
|
421
|
+
*/
|
|
422
|
+
parseUntil() {
|
|
423
|
+
const start = this.expect(TokenType.UNTIL);
|
|
424
|
+
this.skipNewlines();
|
|
425
|
+
const condition = this.parseCompoundList();
|
|
426
|
+
this.expect(TokenType.DO);
|
|
427
|
+
this.skipNewlines();
|
|
428
|
+
const body = this.parseCompoundList();
|
|
429
|
+
this.expect(TokenType.DONE);
|
|
430
|
+
return {
|
|
431
|
+
type: 'UntilClause',
|
|
432
|
+
condition,
|
|
433
|
+
body,
|
|
434
|
+
range: start.range
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Parse for clause
|
|
439
|
+
*/
|
|
440
|
+
parseFor() {
|
|
441
|
+
const start = this.expect(TokenType.FOR);
|
|
442
|
+
const nameToken = this.expect(TokenType.WORD);
|
|
443
|
+
const name = nameToken.value;
|
|
444
|
+
this.skipNewlines();
|
|
445
|
+
let wordlist;
|
|
446
|
+
if (this.check(TokenType.IN)) {
|
|
447
|
+
this.advance();
|
|
448
|
+
wordlist = [];
|
|
449
|
+
while (this.check(TokenType.WORD)) {
|
|
450
|
+
const token = this.advance();
|
|
451
|
+
wordlist.push({
|
|
452
|
+
type: 'Word',
|
|
453
|
+
text: token.value,
|
|
454
|
+
range: token.range
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
this.skipNewlinesAndSeparators();
|
|
459
|
+
this.expect(TokenType.DO);
|
|
460
|
+
this.skipNewlines();
|
|
461
|
+
const body = this.parseCompoundList();
|
|
462
|
+
this.expect(TokenType.DONE);
|
|
463
|
+
return {
|
|
464
|
+
type: 'ForClause',
|
|
465
|
+
name,
|
|
466
|
+
wordlist,
|
|
467
|
+
body,
|
|
468
|
+
range: start.range
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Parse case clause
|
|
473
|
+
*/
|
|
474
|
+
parseCase() {
|
|
475
|
+
const start = this.expect(TokenType.CASE);
|
|
476
|
+
const wordToken = this.expect(TokenType.WORD);
|
|
477
|
+
const word = {
|
|
478
|
+
type: 'Word',
|
|
479
|
+
text: wordToken.value,
|
|
480
|
+
range: wordToken.range
|
|
481
|
+
};
|
|
482
|
+
this.skipNewlines();
|
|
483
|
+
this.expect(TokenType.IN);
|
|
484
|
+
this.skipNewlines();
|
|
485
|
+
const cases = [];
|
|
486
|
+
while (!this.check(TokenType.ESAC)) {
|
|
487
|
+
const caseItem = this.parseCaseItem();
|
|
488
|
+
if (caseItem) {
|
|
489
|
+
cases.push(caseItem);
|
|
490
|
+
}
|
|
491
|
+
this.skipNewlines();
|
|
492
|
+
}
|
|
493
|
+
this.expect(TokenType.ESAC);
|
|
494
|
+
return {
|
|
495
|
+
type: 'CaseClause',
|
|
496
|
+
word,
|
|
497
|
+
cases,
|
|
498
|
+
range: start.range
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Parse case item
|
|
503
|
+
*/
|
|
504
|
+
parseCaseItem() {
|
|
505
|
+
// Skip optional (
|
|
506
|
+
if (this.check(TokenType.LPAREN)) {
|
|
507
|
+
this.advance();
|
|
508
|
+
}
|
|
509
|
+
const pattern = [];
|
|
510
|
+
// Parse pattern
|
|
511
|
+
while (this.check(TokenType.WORD)) {
|
|
512
|
+
const token = this.advance();
|
|
513
|
+
pattern.push({
|
|
514
|
+
type: 'Word',
|
|
515
|
+
text: token.value,
|
|
516
|
+
range: token.range
|
|
517
|
+
});
|
|
518
|
+
if (this.check(TokenType.PIPE)) {
|
|
519
|
+
this.advance();
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (pattern.length === 0) {
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
this.expect(TokenType.RPAREN);
|
|
529
|
+
this.skipNewlines();
|
|
530
|
+
let body;
|
|
531
|
+
if (!this.check(TokenType.DSEMI) && !this.check(TokenType.ESAC)) {
|
|
532
|
+
body = this.parseCompoundList();
|
|
533
|
+
}
|
|
534
|
+
if (this.check(TokenType.DSEMI)) {
|
|
535
|
+
this.advance();
|
|
536
|
+
}
|
|
537
|
+
return {
|
|
538
|
+
type: 'CaseItem',
|
|
539
|
+
pattern,
|
|
540
|
+
body
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Parse subshell
|
|
545
|
+
*/
|
|
546
|
+
parseSubshell() {
|
|
547
|
+
const start = this.expect(TokenType.LPAREN);
|
|
548
|
+
this.skipNewlines();
|
|
549
|
+
const list = this.parseCompoundList();
|
|
550
|
+
this.expect(TokenType.RPAREN);
|
|
551
|
+
return {
|
|
552
|
+
type: 'Subshell',
|
|
553
|
+
list,
|
|
554
|
+
range: start.range
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Parse brace group
|
|
559
|
+
*/
|
|
560
|
+
parseBraceGroup() {
|
|
561
|
+
this.expect(TokenType.LBRACE);
|
|
562
|
+
this.skipNewlines();
|
|
563
|
+
const list = this.parseCompoundList();
|
|
564
|
+
this.expect(TokenType.RBRACE);
|
|
565
|
+
return list;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Parse function definition (function name { ... })
|
|
569
|
+
*/
|
|
570
|
+
parseFunction() {
|
|
571
|
+
const start = this.expect(TokenType.FUNCTION);
|
|
572
|
+
const nameToken = this.expect(TokenType.WORD);
|
|
573
|
+
const name = nameToken.value;
|
|
574
|
+
// Optional ()
|
|
575
|
+
if (this.check(TokenType.LPAREN)) {
|
|
576
|
+
this.advance();
|
|
577
|
+
this.expect(TokenType.RPAREN);
|
|
578
|
+
}
|
|
579
|
+
this.skipNewlines();
|
|
580
|
+
const body = this.parseFunctionBody();
|
|
581
|
+
return {
|
|
582
|
+
type: 'FunctionDefinition',
|
|
583
|
+
name,
|
|
584
|
+
body,
|
|
585
|
+
range: start.range
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Parse function shorthand (name () { ... })
|
|
590
|
+
*/
|
|
591
|
+
parseFunctionShorthand() {
|
|
592
|
+
const nameToken = this.expect(TokenType.WORD);
|
|
593
|
+
const name = nameToken.value;
|
|
594
|
+
this.expect(TokenType.LPAREN);
|
|
595
|
+
this.expect(TokenType.RPAREN);
|
|
596
|
+
this.skipNewlines();
|
|
597
|
+
const body = this.parseFunctionBody();
|
|
598
|
+
return {
|
|
599
|
+
type: 'FunctionDefinition',
|
|
600
|
+
name,
|
|
601
|
+
body,
|
|
602
|
+
range: nameToken.range
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Parse function body
|
|
607
|
+
*/
|
|
608
|
+
parseFunctionBody() {
|
|
609
|
+
if (this.check(TokenType.LPAREN)) {
|
|
610
|
+
return this.parseSubshell();
|
|
611
|
+
}
|
|
612
|
+
return this.parseBraceGroup();
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Parse compound list (list of commands)
|
|
616
|
+
*/
|
|
617
|
+
parseCompoundList() {
|
|
618
|
+
const commands = [];
|
|
619
|
+
while (true) {
|
|
620
|
+
this.skipNewlines();
|
|
621
|
+
// Check for terminators
|
|
622
|
+
if (this.isAtEnd() ||
|
|
623
|
+
this.check(TokenType.THEN) ||
|
|
624
|
+
this.check(TokenType.ELSE) ||
|
|
625
|
+
this.check(TokenType.ELIF) ||
|
|
626
|
+
this.check(TokenType.FI) ||
|
|
627
|
+
this.check(TokenType.DO) ||
|
|
628
|
+
this.check(TokenType.DONE) ||
|
|
629
|
+
this.check(TokenType.ESAC) ||
|
|
630
|
+
this.check(TokenType.RPAREN) ||
|
|
631
|
+
this.check(TokenType.RBRACE) ||
|
|
632
|
+
this.check(TokenType.DSEMI)) {
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
const command = this.parseCommand();
|
|
636
|
+
if (command) {
|
|
637
|
+
commands.push(command);
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
break;
|
|
641
|
+
}
|
|
642
|
+
// Check for separator
|
|
643
|
+
if (this.check(TokenType.SEMI) || this.check(TokenType.AMP) || this.check(TokenType.NEWLINE)) {
|
|
644
|
+
this.advance();
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return {
|
|
651
|
+
type: 'CompoundList',
|
|
652
|
+
commands
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Parse bash source into AST
|
|
658
|
+
*/
|
|
659
|
+
export function parse(source, options) {
|
|
660
|
+
const parser = new Parser(options);
|
|
661
|
+
return parser.parse(source);
|
|
662
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from './types';
|
|
2
|
+
export { Lexer, Token, TokenType } from './lexer';
|
|
3
|
+
export { parse, Parser, ParserOptions } from './parser';
|
|
4
|
+
export { deparse, Deparser, DeparserOptions, deparseSync } from './deparser';
|
|
5
|
+
export { cleanTree, compareAst, noop } from './clean';
|
package/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.noop = exports.compareAst = exports.cleanTree = exports.deparseSync = exports.Deparser = exports.deparse = exports.Parser = exports.parse = exports.TokenType = exports.Lexer = void 0;
|
|
18
|
+
// Types
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
20
|
+
// Lexer
|
|
21
|
+
var lexer_1 = require("./lexer");
|
|
22
|
+
Object.defineProperty(exports, "Lexer", { enumerable: true, get: function () { return lexer_1.Lexer; } });
|
|
23
|
+
Object.defineProperty(exports, "TokenType", { enumerable: true, get: function () { return lexer_1.TokenType; } });
|
|
24
|
+
// Parser
|
|
25
|
+
var parser_1 = require("./parser");
|
|
26
|
+
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parser_1.parse; } });
|
|
27
|
+
Object.defineProperty(exports, "Parser", { enumerable: true, get: function () { return parser_1.Parser; } });
|
|
28
|
+
// Deparser
|
|
29
|
+
var deparser_1 = require("./deparser");
|
|
30
|
+
Object.defineProperty(exports, "deparse", { enumerable: true, get: function () { return deparser_1.deparse; } });
|
|
31
|
+
Object.defineProperty(exports, "Deparser", { enumerable: true, get: function () { return deparser_1.Deparser; } });
|
|
32
|
+
Object.defineProperty(exports, "deparseSync", { enumerable: true, get: function () { return deparser_1.deparseSync; } });
|
|
33
|
+
// Clean utilities
|
|
34
|
+
var clean_1 = require("./clean");
|
|
35
|
+
Object.defineProperty(exports, "cleanTree", { enumerable: true, get: function () { return clean_1.cleanTree; } });
|
|
36
|
+
Object.defineProperty(exports, "compareAst", { enumerable: true, get: function () { return clean_1.compareAst; } });
|
|
37
|
+
Object.defineProperty(exports, "noop", { enumerable: true, get: function () { return clean_1.noop; } });
|