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