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