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/deparser.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bash Deparser - converts AST back to source
|
|
3
|
+
*/
|
|
4
|
+
export class Deparser {
|
|
5
|
+
options;
|
|
6
|
+
indentLevel = 0;
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.options = {
|
|
9
|
+
newline: '\n',
|
|
10
|
+
indent: ' ',
|
|
11
|
+
...options
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Deparse AST to source string
|
|
16
|
+
*/
|
|
17
|
+
deparse(node) {
|
|
18
|
+
if ('type' in node) {
|
|
19
|
+
switch (node.type) {
|
|
20
|
+
case 'Script':
|
|
21
|
+
return this.deparseScript(node);
|
|
22
|
+
case 'SimpleCommand':
|
|
23
|
+
return this.deparseSimpleCommand(node);
|
|
24
|
+
case 'Pipeline':
|
|
25
|
+
return this.deparsePipeline(node);
|
|
26
|
+
case 'LogicalExpression':
|
|
27
|
+
return this.deparseLogicalExpression(node);
|
|
28
|
+
case 'Subshell':
|
|
29
|
+
return this.deparseSubshell(node);
|
|
30
|
+
case 'CompoundList':
|
|
31
|
+
return this.deparseCompoundList(node);
|
|
32
|
+
case 'IfClause':
|
|
33
|
+
return this.deparseIf(node);
|
|
34
|
+
case 'WhileClause':
|
|
35
|
+
return this.deparseWhile(node);
|
|
36
|
+
case 'UntilClause':
|
|
37
|
+
return this.deparseUntil(node);
|
|
38
|
+
case 'ForClause':
|
|
39
|
+
return this.deparseFor(node);
|
|
40
|
+
case 'CaseClause':
|
|
41
|
+
return this.deparseCase(node);
|
|
42
|
+
case 'FunctionDefinition':
|
|
43
|
+
return this.deparseFunction(node);
|
|
44
|
+
case 'Word':
|
|
45
|
+
return this.deparseWord(node);
|
|
46
|
+
case 'AssignmentWord':
|
|
47
|
+
return node.text;
|
|
48
|
+
case 'Redirect':
|
|
49
|
+
return this.deparseRedirect(node);
|
|
50
|
+
default:
|
|
51
|
+
throw new Error(`Unknown node type: ${node.type}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
throw new Error('Invalid node');
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get current indentation
|
|
58
|
+
*/
|
|
59
|
+
getIndent() {
|
|
60
|
+
return this.options.indent.repeat(this.indentLevel);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Deparse script
|
|
64
|
+
*/
|
|
65
|
+
deparseScript(script) {
|
|
66
|
+
return script.commands
|
|
67
|
+
.map(cmd => this.deparseCommand(cmd))
|
|
68
|
+
.join(this.options.newline);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Deparse any command
|
|
72
|
+
*/
|
|
73
|
+
deparseCommand(command) {
|
|
74
|
+
switch (command.type) {
|
|
75
|
+
case 'SimpleCommand':
|
|
76
|
+
return this.deparseSimpleCommand(command);
|
|
77
|
+
case 'Pipeline':
|
|
78
|
+
return this.deparsePipeline(command);
|
|
79
|
+
case 'LogicalExpression':
|
|
80
|
+
return this.deparseLogicalExpression(command);
|
|
81
|
+
case 'Subshell':
|
|
82
|
+
return this.deparseSubshell(command);
|
|
83
|
+
case 'CompoundList':
|
|
84
|
+
return this.deparseCompoundList(command);
|
|
85
|
+
case 'IfClause':
|
|
86
|
+
return this.deparseIf(command);
|
|
87
|
+
case 'WhileClause':
|
|
88
|
+
return this.deparseWhile(command);
|
|
89
|
+
case 'UntilClause':
|
|
90
|
+
return this.deparseUntil(command);
|
|
91
|
+
case 'ForClause':
|
|
92
|
+
return this.deparseFor(command);
|
|
93
|
+
case 'CaseClause':
|
|
94
|
+
return this.deparseCase(command);
|
|
95
|
+
case 'FunctionDefinition':
|
|
96
|
+
return this.deparseFunction(command);
|
|
97
|
+
default:
|
|
98
|
+
throw new Error(`Unknown command type: ${command.type}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Deparse simple command
|
|
103
|
+
*/
|
|
104
|
+
deparseSimpleCommand(cmd) {
|
|
105
|
+
const parts = [];
|
|
106
|
+
// Prefix (assignments)
|
|
107
|
+
if (cmd.prefix) {
|
|
108
|
+
for (const assignment of cmd.prefix) {
|
|
109
|
+
parts.push(assignment.text);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Command name
|
|
113
|
+
if (cmd.name) {
|
|
114
|
+
parts.push(this.deparseWord(cmd.name));
|
|
115
|
+
}
|
|
116
|
+
// Suffix (arguments and redirections)
|
|
117
|
+
if (cmd.suffix) {
|
|
118
|
+
for (const item of cmd.suffix) {
|
|
119
|
+
if (item.type === 'Word') {
|
|
120
|
+
parts.push(this.deparseWord(item));
|
|
121
|
+
}
|
|
122
|
+
else if (item.type === 'Redirect') {
|
|
123
|
+
parts.push(this.deparseRedirect(item));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return parts.join(' ');
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Deparse word
|
|
131
|
+
*/
|
|
132
|
+
deparseWord(word) {
|
|
133
|
+
return word.text;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Deparse redirect
|
|
137
|
+
*/
|
|
138
|
+
deparseRedirect(redirect) {
|
|
139
|
+
let result = '';
|
|
140
|
+
if (redirect.numberIo !== undefined) {
|
|
141
|
+
result += redirect.numberIo;
|
|
142
|
+
}
|
|
143
|
+
result += redirect.op;
|
|
144
|
+
if (redirect.file) {
|
|
145
|
+
result += this.deparseWord(redirect.file);
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Deparse pipeline
|
|
151
|
+
*/
|
|
152
|
+
deparsePipeline(pipeline) {
|
|
153
|
+
const parts = [];
|
|
154
|
+
if (pipeline.bang) {
|
|
155
|
+
parts.push('!');
|
|
156
|
+
}
|
|
157
|
+
parts.push(pipeline.commands
|
|
158
|
+
.map(cmd => this.deparseCommand(cmd))
|
|
159
|
+
.join(' | '));
|
|
160
|
+
return parts.join(' ');
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Deparse logical expression
|
|
164
|
+
*/
|
|
165
|
+
deparseLogicalExpression(expr) {
|
|
166
|
+
const left = this.deparseCommand(expr.left);
|
|
167
|
+
const right = this.deparseCommand(expr.right);
|
|
168
|
+
return `${left} ${expr.op} ${right}`;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Deparse subshell
|
|
172
|
+
*/
|
|
173
|
+
deparseSubshell(subshell) {
|
|
174
|
+
const list = this.deparseCompoundListInline(subshell.list);
|
|
175
|
+
return `(${list})`;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Deparse compound list
|
|
179
|
+
*/
|
|
180
|
+
deparseCompoundList(list) {
|
|
181
|
+
return list.commands
|
|
182
|
+
.map(cmd => this.deparseCommand(cmd))
|
|
183
|
+
.join('; ');
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Deparse compound list inline (for subshells, etc.)
|
|
187
|
+
*/
|
|
188
|
+
deparseCompoundListInline(list) {
|
|
189
|
+
return list.commands
|
|
190
|
+
.map(cmd => this.deparseCommand(cmd))
|
|
191
|
+
.join('; ');
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Deparse if clause
|
|
195
|
+
*/
|
|
196
|
+
deparseIf(ifClause) {
|
|
197
|
+
const parts = [];
|
|
198
|
+
parts.push('if');
|
|
199
|
+
parts.push(this.deparseCompoundListInline(ifClause.condition));
|
|
200
|
+
parts.push('; then');
|
|
201
|
+
parts.push(this.deparseCompoundListInline(ifClause.then));
|
|
202
|
+
if (ifClause.else) {
|
|
203
|
+
if (ifClause.else.type === 'IfClause') {
|
|
204
|
+
// elif
|
|
205
|
+
parts.push('; el' + this.deparseIf(ifClause.else).substring(0));
|
|
206
|
+
return parts.join(' ').replace('; elif', '; elif').replace('; fi; fi', '; fi');
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
parts.push('; else');
|
|
210
|
+
parts.push(this.deparseCompoundListInline(ifClause.else));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
parts.push('; fi');
|
|
214
|
+
return parts.join(' ');
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Deparse while clause
|
|
218
|
+
*/
|
|
219
|
+
deparseWhile(whileClause) {
|
|
220
|
+
const condition = this.deparseCompoundListInline(whileClause.condition);
|
|
221
|
+
const body = this.deparseCompoundListInline(whileClause.body);
|
|
222
|
+
return `while ${condition}; do ${body}; done`;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Deparse until clause
|
|
226
|
+
*/
|
|
227
|
+
deparseUntil(untilClause) {
|
|
228
|
+
const condition = this.deparseCompoundListInline(untilClause.condition);
|
|
229
|
+
const body = this.deparseCompoundListInline(untilClause.body);
|
|
230
|
+
return `until ${condition}; do ${body}; done`;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Deparse for clause
|
|
234
|
+
*/
|
|
235
|
+
deparseFor(forClause) {
|
|
236
|
+
const parts = ['for', forClause.name];
|
|
237
|
+
if (forClause.wordlist && forClause.wordlist.length > 0) {
|
|
238
|
+
parts.push('in');
|
|
239
|
+
parts.push(forClause.wordlist.map(w => this.deparseWord(w)).join(' '));
|
|
240
|
+
}
|
|
241
|
+
parts.push(';');
|
|
242
|
+
parts.push('do');
|
|
243
|
+
parts.push(this.deparseCompoundListInline(forClause.body));
|
|
244
|
+
parts.push(';');
|
|
245
|
+
parts.push('done');
|
|
246
|
+
return parts.join(' ');
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Deparse case clause
|
|
250
|
+
*/
|
|
251
|
+
deparseCase(caseClause) {
|
|
252
|
+
const parts = [];
|
|
253
|
+
parts.push('case');
|
|
254
|
+
parts.push(this.deparseWord(caseClause.word));
|
|
255
|
+
parts.push('in');
|
|
256
|
+
for (const caseItem of caseClause.cases) {
|
|
257
|
+
parts.push(this.deparseCaseItem(caseItem));
|
|
258
|
+
}
|
|
259
|
+
parts.push('esac');
|
|
260
|
+
return parts.join(' ');
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Deparse case item
|
|
264
|
+
*/
|
|
265
|
+
deparseCaseItem(caseItem) {
|
|
266
|
+
const pattern = caseItem.pattern.map(w => this.deparseWord(w)).join('|');
|
|
267
|
+
let result = `${pattern})`;
|
|
268
|
+
if (caseItem.body) {
|
|
269
|
+
result += ' ' + this.deparseCompoundListInline(caseItem.body);
|
|
270
|
+
}
|
|
271
|
+
result += ' ;;';
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Deparse function definition
|
|
276
|
+
*/
|
|
277
|
+
deparseFunction(func) {
|
|
278
|
+
const body = func.body.type === 'Subshell'
|
|
279
|
+
? this.deparseSubshell(func.body)
|
|
280
|
+
: `{ ${this.deparseCompoundListInline(func.body)}; }`;
|
|
281
|
+
return `${func.name}() ${body}`;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Deparse AST to source string
|
|
286
|
+
*/
|
|
287
|
+
export function deparse(node, options) {
|
|
288
|
+
const deparser = new Deparser(options);
|
|
289
|
+
return deparser.deparse(node);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Synchronous deparse (same as deparse, for API consistency)
|
|
293
|
+
*/
|
|
294
|
+
export const deparseSync = deparse;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Types
|
|
2
|
+
export * from './types';
|
|
3
|
+
// Lexer
|
|
4
|
+
export { Lexer, TokenType } from './lexer';
|
|
5
|
+
// Parser
|
|
6
|
+
export { parse, Parser } from './parser';
|
|
7
|
+
// Deparser
|
|
8
|
+
export { deparse, Deparser, deparseSync } from './deparser';
|
|
9
|
+
// Clean utilities
|
|
10
|
+
export { cleanTree, compareAst, noop } from './clean';
|