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/esm/lexer.js ADDED
@@ -0,0 +1,447 @@
1
+ /**
2
+ * Token types for bash lexer
3
+ */
4
+ export var TokenType;
5
+ (function (TokenType) {
6
+ // Words and identifiers
7
+ TokenType["WORD"] = "WORD";
8
+ TokenType["NAME"] = "NAME";
9
+ TokenType["ASSIGNMENT_WORD"] = "ASSIGNMENT_WORD";
10
+ // Operators
11
+ TokenType["PIPE"] = "PIPE";
12
+ TokenType["AND_IF"] = "AND_IF";
13
+ TokenType["OR_IF"] = "OR_IF";
14
+ TokenType["DSEMI"] = "DSEMI";
15
+ TokenType["SEMI"] = "SEMI";
16
+ TokenType["AMP"] = "AMP";
17
+ TokenType["NEWLINE"] = "NEWLINE";
18
+ // Redirections
19
+ TokenType["LESS"] = "LESS";
20
+ TokenType["GREAT"] = "GREAT";
21
+ TokenType["DLESS"] = "DLESS";
22
+ TokenType["DGREAT"] = "DGREAT";
23
+ TokenType["LESSAND"] = "LESSAND";
24
+ TokenType["GREATAND"] = "GREATAND";
25
+ TokenType["LESSGREAT"] = "LESSGREAT";
26
+ TokenType["DLESSDASH"] = "DLESSDASH";
27
+ TokenType["CLOBBER"] = "CLOBBER";
28
+ TokenType["TLESS"] = "TLESS";
29
+ // Reserved words
30
+ TokenType["IF"] = "IF";
31
+ TokenType["THEN"] = "THEN";
32
+ TokenType["ELSE"] = "ELSE";
33
+ TokenType["ELIF"] = "ELIF";
34
+ TokenType["FI"] = "FI";
35
+ TokenType["DO"] = "DO";
36
+ TokenType["DONE"] = "DONE";
37
+ TokenType["CASE"] = "CASE";
38
+ TokenType["ESAC"] = "ESAC";
39
+ TokenType["WHILE"] = "WHILE";
40
+ TokenType["UNTIL"] = "UNTIL";
41
+ TokenType["FOR"] = "FOR";
42
+ TokenType["IN"] = "IN";
43
+ TokenType["FUNCTION"] = "FUNCTION";
44
+ TokenType["BANG"] = "BANG";
45
+ // Grouping
46
+ TokenType["LPAREN"] = "LPAREN";
47
+ TokenType["RPAREN"] = "RPAREN";
48
+ TokenType["LBRACE"] = "LBRACE";
49
+ TokenType["RBRACE"] = "RBRACE";
50
+ // Special
51
+ TokenType["IO_NUMBER"] = "IO_NUMBER";
52
+ TokenType["EOF"] = "EOF";
53
+ TokenType["WHITESPACE"] = "WHITESPACE";
54
+ })(TokenType || (TokenType = {}));
55
+ /**
56
+ * Reserved words mapping
57
+ */
58
+ const RESERVED_WORDS = {
59
+ if: TokenType.IF,
60
+ then: TokenType.THEN,
61
+ else: TokenType.ELSE,
62
+ elif: TokenType.ELIF,
63
+ fi: TokenType.FI,
64
+ do: TokenType.DO,
65
+ done: TokenType.DONE,
66
+ case: TokenType.CASE,
67
+ esac: TokenType.ESAC,
68
+ while: TokenType.WHILE,
69
+ until: TokenType.UNTIL,
70
+ for: TokenType.FOR,
71
+ in: TokenType.IN,
72
+ function: TokenType.FUNCTION,
73
+ '!': TokenType.BANG,
74
+ };
75
+ /**
76
+ * Bash Lexer
77
+ */
78
+ export class Lexer {
79
+ input;
80
+ pos = 0;
81
+ line = 1;
82
+ column = 0;
83
+ constructor(input) {
84
+ this.input = input;
85
+ }
86
+ /**
87
+ * Get current position
88
+ */
89
+ getPosition() {
90
+ return {
91
+ line: this.line,
92
+ column: this.column,
93
+ offset: this.pos
94
+ };
95
+ }
96
+ /**
97
+ * Peek at current character
98
+ */
99
+ peek(offset = 0) {
100
+ return this.input[this.pos + offset] || '';
101
+ }
102
+ /**
103
+ * Advance position by one character
104
+ */
105
+ advance() {
106
+ const char = this.input[this.pos];
107
+ this.pos++;
108
+ if (char === '\n') {
109
+ this.line++;
110
+ this.column = 0;
111
+ }
112
+ else {
113
+ this.column++;
114
+ }
115
+ return char;
116
+ }
117
+ /**
118
+ * Check if at end of input
119
+ */
120
+ isAtEnd() {
121
+ return this.pos >= this.input.length;
122
+ }
123
+ /**
124
+ * Check if character is whitespace (not newline)
125
+ */
126
+ isWhitespace(char) {
127
+ return char === ' ' || char === '\t';
128
+ }
129
+ /**
130
+ * Check if character is a digit
131
+ */
132
+ isDigit(char) {
133
+ return char >= '0' && char <= '9';
134
+ }
135
+ /**
136
+ * Check if character can start a word
137
+ */
138
+ isWordChar(char) {
139
+ return char !== '' &&
140
+ !this.isWhitespace(char) &&
141
+ char !== '\n' &&
142
+ char !== '|' &&
143
+ char !== '&' &&
144
+ char !== ';' &&
145
+ char !== '(' &&
146
+ char !== ')' &&
147
+ char !== '<' &&
148
+ char !== '>' &&
149
+ char !== '{' &&
150
+ char !== '}';
151
+ }
152
+ /**
153
+ * Skip whitespace (not newlines)
154
+ */
155
+ skipWhitespace() {
156
+ while (!this.isAtEnd() && this.isWhitespace(this.peek())) {
157
+ this.advance();
158
+ }
159
+ }
160
+ /**
161
+ * Read a single-quoted string
162
+ */
163
+ readSingleQuoted() {
164
+ let result = "'";
165
+ this.advance(); // opening quote
166
+ while (!this.isAtEnd() && this.peek() !== "'") {
167
+ result += this.advance();
168
+ }
169
+ if (this.peek() === "'") {
170
+ result += this.advance();
171
+ }
172
+ return result;
173
+ }
174
+ /**
175
+ * Read a double-quoted string
176
+ */
177
+ readDoubleQuoted() {
178
+ let result = '"';
179
+ this.advance(); // opening quote
180
+ while (!this.isAtEnd() && this.peek() !== '"') {
181
+ if (this.peek() === '\\' && (this.peek(1) === '"' || this.peek(1) === '\\' || this.peek(1) === '$' || this.peek(1) === '`')) {
182
+ result += this.advance(); // backslash
183
+ result += this.advance(); // escaped char
184
+ }
185
+ else {
186
+ result += this.advance();
187
+ }
188
+ }
189
+ if (this.peek() === '"') {
190
+ result += this.advance();
191
+ }
192
+ return result;
193
+ }
194
+ /**
195
+ * Read a word token
196
+ */
197
+ readWord() {
198
+ let result = '';
199
+ while (!this.isAtEnd() && this.isWordChar(this.peek())) {
200
+ const char = this.peek();
201
+ if (char === "'") {
202
+ result += this.readSingleQuoted();
203
+ }
204
+ else if (char === '"') {
205
+ result += this.readDoubleQuoted();
206
+ }
207
+ else if (char === '\\') {
208
+ result += this.advance(); // backslash
209
+ if (!this.isAtEnd()) {
210
+ result += this.advance(); // escaped char
211
+ }
212
+ }
213
+ else if (char === '$') {
214
+ result += this.readExpansion();
215
+ }
216
+ else if (char === '`') {
217
+ result += this.readBacktickSubstitution();
218
+ }
219
+ else {
220
+ result += this.advance();
221
+ }
222
+ }
223
+ return result;
224
+ }
225
+ /**
226
+ * Read an expansion ($VAR, ${VAR}, $(cmd), $((expr)))
227
+ */
228
+ readExpansion() {
229
+ let result = this.advance(); // $
230
+ if (this.peek() === '(') {
231
+ if (this.peek(1) === '(') {
232
+ // Arithmetic expansion $((expr))
233
+ result += this.advance(); // (
234
+ result += this.advance(); // (
235
+ let depth = 2;
236
+ while (!this.isAtEnd() && depth > 0) {
237
+ if (this.peek() === '(')
238
+ depth++;
239
+ if (this.peek() === ')')
240
+ depth--;
241
+ result += this.advance();
242
+ }
243
+ }
244
+ else {
245
+ // Command substitution $(cmd)
246
+ result += this.advance(); // (
247
+ let depth = 1;
248
+ while (!this.isAtEnd() && depth > 0) {
249
+ if (this.peek() === '(')
250
+ depth++;
251
+ if (this.peek() === ')')
252
+ depth--;
253
+ result += this.advance();
254
+ }
255
+ }
256
+ }
257
+ else if (this.peek() === '{') {
258
+ // Parameter expansion ${VAR}
259
+ result += this.advance(); // {
260
+ let depth = 1;
261
+ while (!this.isAtEnd() && depth > 0) {
262
+ if (this.peek() === '{')
263
+ depth++;
264
+ if (this.peek() === '}')
265
+ depth--;
266
+ result += this.advance();
267
+ }
268
+ }
269
+ else {
270
+ // Simple variable $VAR
271
+ while (!this.isAtEnd() && /[a-zA-Z0-9_]/.test(this.peek())) {
272
+ result += this.advance();
273
+ }
274
+ }
275
+ return result;
276
+ }
277
+ /**
278
+ * Read backtick command substitution
279
+ */
280
+ readBacktickSubstitution() {
281
+ let result = this.advance(); // `
282
+ while (!this.isAtEnd() && this.peek() !== '`') {
283
+ if (this.peek() === '\\') {
284
+ result += this.advance();
285
+ if (!this.isAtEnd()) {
286
+ result += this.advance();
287
+ }
288
+ }
289
+ else {
290
+ result += this.advance();
291
+ }
292
+ }
293
+ if (this.peek() === '`') {
294
+ result += this.advance();
295
+ }
296
+ return result;
297
+ }
298
+ /**
299
+ * Read a comment
300
+ */
301
+ readComment() {
302
+ while (!this.isAtEnd() && this.peek() !== '\n') {
303
+ this.advance();
304
+ }
305
+ }
306
+ /**
307
+ * Get next token
308
+ */
309
+ nextToken() {
310
+ this.skipWhitespace();
311
+ if (this.isAtEnd()) {
312
+ return {
313
+ type: TokenType.EOF,
314
+ value: '',
315
+ range: { start: this.getPosition(), end: this.getPosition() }
316
+ };
317
+ }
318
+ const start = this.getPosition();
319
+ const char = this.peek();
320
+ // Comment
321
+ if (char === '#') {
322
+ this.readComment();
323
+ return this.nextToken();
324
+ }
325
+ // Newline
326
+ if (char === '\n') {
327
+ this.advance();
328
+ return {
329
+ type: TokenType.NEWLINE,
330
+ value: '\n',
331
+ range: { start, end: this.getPosition() }
332
+ };
333
+ }
334
+ // Operators
335
+ if (char === '|') {
336
+ this.advance();
337
+ if (this.peek() === '|') {
338
+ this.advance();
339
+ return { type: TokenType.OR_IF, value: '||', range: { start, end: this.getPosition() } };
340
+ }
341
+ return { type: TokenType.PIPE, value: '|', range: { start, end: this.getPosition() } };
342
+ }
343
+ if (char === '&') {
344
+ this.advance();
345
+ if (this.peek() === '&') {
346
+ this.advance();
347
+ return { type: TokenType.AND_IF, value: '&&', range: { start, end: this.getPosition() } };
348
+ }
349
+ return { type: TokenType.AMP, value: '&', range: { start, end: this.getPosition() } };
350
+ }
351
+ if (char === ';') {
352
+ this.advance();
353
+ if (this.peek() === ';') {
354
+ this.advance();
355
+ return { type: TokenType.DSEMI, value: ';;', range: { start, end: this.getPosition() } };
356
+ }
357
+ return { type: TokenType.SEMI, value: ';', range: { start, end: this.getPosition() } };
358
+ }
359
+ // Redirections
360
+ if (char === '<') {
361
+ this.advance();
362
+ if (this.peek() === '<') {
363
+ this.advance();
364
+ if (this.peek() === '-') {
365
+ this.advance();
366
+ return { type: TokenType.DLESSDASH, value: '<<-', range: { start, end: this.getPosition() } };
367
+ }
368
+ if (this.peek() === '<') {
369
+ this.advance();
370
+ return { type: TokenType.TLESS, value: '<<<', range: { start, end: this.getPosition() } };
371
+ }
372
+ return { type: TokenType.DLESS, value: '<<', range: { start, end: this.getPosition() } };
373
+ }
374
+ if (this.peek() === '&') {
375
+ this.advance();
376
+ return { type: TokenType.LESSAND, value: '<&', range: { start, end: this.getPosition() } };
377
+ }
378
+ if (this.peek() === '>') {
379
+ this.advance();
380
+ return { type: TokenType.LESSGREAT, value: '<>', range: { start, end: this.getPosition() } };
381
+ }
382
+ return { type: TokenType.LESS, value: '<', range: { start, end: this.getPosition() } };
383
+ }
384
+ if (char === '>') {
385
+ this.advance();
386
+ if (this.peek() === '>') {
387
+ this.advance();
388
+ return { type: TokenType.DGREAT, value: '>>', range: { start, end: this.getPosition() } };
389
+ }
390
+ if (this.peek() === '&') {
391
+ this.advance();
392
+ return { type: TokenType.GREATAND, value: '>&', range: { start, end: this.getPosition() } };
393
+ }
394
+ if (this.peek() === '|') {
395
+ this.advance();
396
+ return { type: TokenType.CLOBBER, value: '>|', range: { start, end: this.getPosition() } };
397
+ }
398
+ return { type: TokenType.GREAT, value: '>', range: { start, end: this.getPosition() } };
399
+ }
400
+ // Grouping
401
+ if (char === '(') {
402
+ this.advance();
403
+ return { type: TokenType.LPAREN, value: '(', range: { start, end: this.getPosition() } };
404
+ }
405
+ if (char === ')') {
406
+ this.advance();
407
+ return { type: TokenType.RPAREN, value: ')', range: { start, end: this.getPosition() } };
408
+ }
409
+ if (char === '{') {
410
+ this.advance();
411
+ return { type: TokenType.LBRACE, value: '{', range: { start, end: this.getPosition() } };
412
+ }
413
+ if (char === '}') {
414
+ this.advance();
415
+ return { type: TokenType.RBRACE, value: '}', range: { start, end: this.getPosition() } };
416
+ }
417
+ // IO number (digit followed by < or >)
418
+ if (this.isDigit(char) && (this.peek(1) === '<' || this.peek(1) === '>')) {
419
+ const num = this.advance();
420
+ return { type: TokenType.IO_NUMBER, value: num, range: { start, end: this.getPosition() } };
421
+ }
422
+ // Word
423
+ const word = this.readWord();
424
+ const end = this.getPosition();
425
+ // Check for reserved words
426
+ if (word in RESERVED_WORDS) {
427
+ return { type: RESERVED_WORDS[word], value: word, range: { start, end } };
428
+ }
429
+ // Check for assignment word (NAME=VALUE)
430
+ if (word.includes('=') && /^[a-zA-Z_][a-zA-Z0-9_]*=/.test(word)) {
431
+ return { type: TokenType.ASSIGNMENT_WORD, value: word, range: { start, end } };
432
+ }
433
+ return { type: TokenType.WORD, value: word, range: { start, end } };
434
+ }
435
+ /**
436
+ * Tokenize entire input
437
+ */
438
+ tokenize() {
439
+ const tokens = [];
440
+ let token;
441
+ do {
442
+ token = this.nextToken();
443
+ tokens.push(token);
444
+ } while (token.type !== TokenType.EOF);
445
+ return tokens;
446
+ }
447
+ }