docker-parser 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 +97 -0
- package/deparser.d.ts +127 -0
- package/deparser.js +441 -0
- package/esm/clean.js +91 -0
- package/esm/deparser.js +436 -0
- package/esm/index.js +10 -0
- package/esm/lexer.js +412 -0
- package/esm/parser.js +943 -0
- package/esm/types.js +1 -0
- package/index.d.ts +5 -0
- package/index.js +37 -0
- package/lexer.d.ts +117 -0
- package/lexer.js +416 -0
- package/package.json +45 -0
- package/parser.d.ts +150 -0
- package/parser.js +948 -0
- package/types.d.ts +284 -0
- package/types.js +2 -0
package/esm/lexer.js
ADDED
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token types for Dockerfile lexer
|
|
3
|
+
*/
|
|
4
|
+
export var TokenType;
|
|
5
|
+
(function (TokenType) {
|
|
6
|
+
// Instructions
|
|
7
|
+
TokenType["FROM"] = "FROM";
|
|
8
|
+
TokenType["RUN"] = "RUN";
|
|
9
|
+
TokenType["CMD"] = "CMD";
|
|
10
|
+
TokenType["LABEL"] = "LABEL";
|
|
11
|
+
TokenType["MAINTAINER"] = "MAINTAINER";
|
|
12
|
+
TokenType["EXPOSE"] = "EXPOSE";
|
|
13
|
+
TokenType["ENV"] = "ENV";
|
|
14
|
+
TokenType["ADD"] = "ADD";
|
|
15
|
+
TokenType["COPY"] = "COPY";
|
|
16
|
+
TokenType["ENTRYPOINT"] = "ENTRYPOINT";
|
|
17
|
+
TokenType["VOLUME"] = "VOLUME";
|
|
18
|
+
TokenType["USER"] = "USER";
|
|
19
|
+
TokenType["WORKDIR"] = "WORKDIR";
|
|
20
|
+
TokenType["ARG"] = "ARG";
|
|
21
|
+
TokenType["ONBUILD"] = "ONBUILD";
|
|
22
|
+
TokenType["STOPSIGNAL"] = "STOPSIGNAL";
|
|
23
|
+
TokenType["HEALTHCHECK"] = "HEALTHCHECK";
|
|
24
|
+
TokenType["SHELL"] = "SHELL";
|
|
25
|
+
// Other tokens
|
|
26
|
+
TokenType["COMMENT"] = "COMMENT";
|
|
27
|
+
TokenType["DIRECTIVE"] = "DIRECTIVE";
|
|
28
|
+
TokenType["STRING"] = "STRING";
|
|
29
|
+
TokenType["QUOTED_STRING"] = "QUOTED_STRING";
|
|
30
|
+
TokenType["JSON_ARRAY"] = "JSON_ARRAY";
|
|
31
|
+
TokenType["NEWLINE"] = "NEWLINE";
|
|
32
|
+
TokenType["WHITESPACE"] = "WHITESPACE";
|
|
33
|
+
TokenType["CONTINUATION"] = "CONTINUATION";
|
|
34
|
+
TokenType["FLAG"] = "FLAG";
|
|
35
|
+
TokenType["EQUALS"] = "EQUALS";
|
|
36
|
+
TokenType["EOF"] = "EOF";
|
|
37
|
+
TokenType["UNKNOWN"] = "UNKNOWN";
|
|
38
|
+
})(TokenType || (TokenType = {}));
|
|
39
|
+
/**
|
|
40
|
+
* Instruction keywords
|
|
41
|
+
*/
|
|
42
|
+
const INSTRUCTIONS = new Set([
|
|
43
|
+
'FROM', 'RUN', 'CMD', 'LABEL', 'MAINTAINER', 'EXPOSE', 'ENV',
|
|
44
|
+
'ADD', 'COPY', 'ENTRYPOINT', 'VOLUME', 'USER', 'WORKDIR',
|
|
45
|
+
'ARG', 'ONBUILD', 'STOPSIGNAL', 'HEALTHCHECK', 'SHELL'
|
|
46
|
+
]);
|
|
47
|
+
/**
|
|
48
|
+
* Dockerfile Lexer
|
|
49
|
+
*/
|
|
50
|
+
export class Lexer {
|
|
51
|
+
input;
|
|
52
|
+
pos = 0;
|
|
53
|
+
line = 1;
|
|
54
|
+
column = 0;
|
|
55
|
+
escapeChar = '\\';
|
|
56
|
+
atLineStart = true;
|
|
57
|
+
constructor(input) {
|
|
58
|
+
this.input = input;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Set the escape character (from parser directive)
|
|
62
|
+
*/
|
|
63
|
+
setEscapeChar(char) {
|
|
64
|
+
if (char === '\\' || char === '`') {
|
|
65
|
+
this.escapeChar = char;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get current position
|
|
70
|
+
*/
|
|
71
|
+
getPosition() {
|
|
72
|
+
return {
|
|
73
|
+
line: this.line,
|
|
74
|
+
column: this.column,
|
|
75
|
+
offset: this.pos
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Peek at current character
|
|
80
|
+
*/
|
|
81
|
+
peek(offset = 0) {
|
|
82
|
+
return this.input[this.pos + offset] || '';
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Advance position by one character
|
|
86
|
+
*/
|
|
87
|
+
advance() {
|
|
88
|
+
const char = this.input[this.pos];
|
|
89
|
+
this.pos++;
|
|
90
|
+
if (char === '\n') {
|
|
91
|
+
this.line++;
|
|
92
|
+
this.column = 0;
|
|
93
|
+
this.atLineStart = true;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.column++;
|
|
97
|
+
if (char !== ' ' && char !== '\t') {
|
|
98
|
+
this.atLineStart = false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return char;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Check if at end of input
|
|
105
|
+
*/
|
|
106
|
+
isAtEnd() {
|
|
107
|
+
return this.pos >= this.input.length;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Skip whitespace (not newlines)
|
|
111
|
+
*/
|
|
112
|
+
skipWhitespace() {
|
|
113
|
+
while (!this.isAtEnd() && (this.peek() === ' ' || this.peek() === '\t')) {
|
|
114
|
+
this.advance();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Read until end of line, handling continuations
|
|
119
|
+
*/
|
|
120
|
+
readToEndOfLine() {
|
|
121
|
+
let result = '';
|
|
122
|
+
while (!this.isAtEnd()) {
|
|
123
|
+
const char = this.peek();
|
|
124
|
+
if (char === '\n') {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
if (char === '\r') {
|
|
128
|
+
this.advance();
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (char === this.escapeChar && (this.peek(1) === '\n' || this.peek(1) === '\r')) {
|
|
132
|
+
// Line continuation
|
|
133
|
+
this.advance(); // escape char
|
|
134
|
+
if (this.peek() === '\r')
|
|
135
|
+
this.advance();
|
|
136
|
+
if (this.peek() === '\n')
|
|
137
|
+
this.advance();
|
|
138
|
+
result += ' ';
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
result += this.advance();
|
|
142
|
+
}
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Read a quoted string
|
|
147
|
+
*/
|
|
148
|
+
readQuotedString(quote) {
|
|
149
|
+
let result = quote;
|
|
150
|
+
this.advance(); // opening quote
|
|
151
|
+
while (!this.isAtEnd()) {
|
|
152
|
+
const char = this.peek();
|
|
153
|
+
if (char === quote) {
|
|
154
|
+
result += this.advance();
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (char === '\\' && this.peek(1) === quote) {
|
|
158
|
+
result += this.advance(); // backslash
|
|
159
|
+
result += this.advance(); // escaped quote
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (char === '\n') {
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
result += this.advance();
|
|
166
|
+
}
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Read a JSON array
|
|
171
|
+
*/
|
|
172
|
+
readJsonArray() {
|
|
173
|
+
let result = '';
|
|
174
|
+
let depth = 0;
|
|
175
|
+
while (!this.isAtEnd()) {
|
|
176
|
+
const char = this.peek();
|
|
177
|
+
if (char === '[') {
|
|
178
|
+
depth++;
|
|
179
|
+
result += this.advance();
|
|
180
|
+
}
|
|
181
|
+
else if (char === ']') {
|
|
182
|
+
depth--;
|
|
183
|
+
result += this.advance();
|
|
184
|
+
if (depth === 0)
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
else if (char === '"') {
|
|
188
|
+
result += this.readQuotedString('"');
|
|
189
|
+
}
|
|
190
|
+
else if (char === '\n' && depth === 0) {
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
result += this.advance();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Read an identifier/word
|
|
201
|
+
*/
|
|
202
|
+
readWord() {
|
|
203
|
+
let result = '';
|
|
204
|
+
while (!this.isAtEnd()) {
|
|
205
|
+
const char = this.peek();
|
|
206
|
+
if (char === ' ' || char === '\t' || char === '\n' || char === '\r' || char === '=') {
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
if (char === this.escapeChar && (this.peek(1) === '\n' || this.peek(1) === '\r')) {
|
|
210
|
+
// Line continuation
|
|
211
|
+
this.advance();
|
|
212
|
+
if (this.peek() === '\r')
|
|
213
|
+
this.advance();
|
|
214
|
+
if (this.peek() === '\n')
|
|
215
|
+
this.advance();
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
result += this.advance();
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Check if this looks like a parser directive
|
|
224
|
+
*/
|
|
225
|
+
isDirective() {
|
|
226
|
+
if (this.peek() !== '#')
|
|
227
|
+
return false;
|
|
228
|
+
// Look ahead for directive pattern: # directive=value
|
|
229
|
+
let i = 1;
|
|
230
|
+
while (this.peek(i) === ' ' || this.peek(i) === '\t')
|
|
231
|
+
i++;
|
|
232
|
+
let word = '';
|
|
233
|
+
while (this.peek(i) && /[a-zA-Z]/.test(this.peek(i))) {
|
|
234
|
+
word += this.peek(i);
|
|
235
|
+
i++;
|
|
236
|
+
}
|
|
237
|
+
while (this.peek(i) === ' ' || this.peek(i) === '\t')
|
|
238
|
+
i++;
|
|
239
|
+
return this.peek(i) === '=' && (word.toLowerCase() === 'escape' || word.toLowerCase() === 'syntax');
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Read until end of line without handling continuations
|
|
243
|
+
* Used for directive values where we don't want escape handling
|
|
244
|
+
*/
|
|
245
|
+
readToEndOfLineRaw() {
|
|
246
|
+
let result = '';
|
|
247
|
+
while (!this.isAtEnd()) {
|
|
248
|
+
const char = this.peek();
|
|
249
|
+
if (char === '\n' || char === '\r') {
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
result += this.advance();
|
|
253
|
+
}
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Read a comment or directive
|
|
258
|
+
*/
|
|
259
|
+
readComment() {
|
|
260
|
+
const start = this.getPosition();
|
|
261
|
+
this.advance(); // #
|
|
262
|
+
// Check for directive
|
|
263
|
+
this.skipWhitespace();
|
|
264
|
+
const wordStart = this.pos;
|
|
265
|
+
let word = '';
|
|
266
|
+
while (!this.isAtEnd() && /[a-zA-Z]/.test(this.peek())) {
|
|
267
|
+
word += this.advance();
|
|
268
|
+
}
|
|
269
|
+
this.skipWhitespace();
|
|
270
|
+
if (this.peek() === '=' && (word.toLowerCase() === 'escape' || word.toLowerCase() === 'syntax')) {
|
|
271
|
+
this.advance(); // =
|
|
272
|
+
this.skipWhitespace();
|
|
273
|
+
// Use raw read for directive values to avoid escape character issues
|
|
274
|
+
const value = this.readToEndOfLineRaw().trim();
|
|
275
|
+
const end = this.getPosition();
|
|
276
|
+
return {
|
|
277
|
+
type: TokenType.DIRECTIVE,
|
|
278
|
+
value: `${word.toLowerCase()}=${value}`,
|
|
279
|
+
range: { start, end }
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
// Regular comment - reset and read the whole line
|
|
283
|
+
this.pos = wordStart;
|
|
284
|
+
const value = this.readToEndOfLine();
|
|
285
|
+
const end = this.getPosition();
|
|
286
|
+
return {
|
|
287
|
+
type: TokenType.COMMENT,
|
|
288
|
+
value: value.trim(),
|
|
289
|
+
range: { start, end }
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Get next token
|
|
294
|
+
*/
|
|
295
|
+
nextToken() {
|
|
296
|
+
// Skip whitespace at start of line
|
|
297
|
+
if (this.atLineStart) {
|
|
298
|
+
this.skipWhitespace();
|
|
299
|
+
}
|
|
300
|
+
if (this.isAtEnd()) {
|
|
301
|
+
return {
|
|
302
|
+
type: TokenType.EOF,
|
|
303
|
+
value: '',
|
|
304
|
+
range: { start: this.getPosition(), end: this.getPosition() }
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
const start = this.getPosition();
|
|
308
|
+
const char = this.peek();
|
|
309
|
+
// Newline
|
|
310
|
+
if (char === '\n') {
|
|
311
|
+
this.advance();
|
|
312
|
+
return {
|
|
313
|
+
type: TokenType.NEWLINE,
|
|
314
|
+
value: '\n',
|
|
315
|
+
range: { start, end: this.getPosition() }
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
// Carriage return
|
|
319
|
+
if (char === '\r') {
|
|
320
|
+
this.advance();
|
|
321
|
+
if (this.peek() === '\n') {
|
|
322
|
+
this.advance();
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
type: TokenType.NEWLINE,
|
|
326
|
+
value: '\n',
|
|
327
|
+
range: { start, end: this.getPosition() }
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
// Comment or directive (only at line start)
|
|
331
|
+
if (char === '#' && this.atLineStart) {
|
|
332
|
+
return this.readComment();
|
|
333
|
+
}
|
|
334
|
+
// Whitespace
|
|
335
|
+
if (char === ' ' || char === '\t') {
|
|
336
|
+
let value = '';
|
|
337
|
+
while (!this.isAtEnd() && (this.peek() === ' ' || this.peek() === '\t')) {
|
|
338
|
+
value += this.advance();
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
type: TokenType.WHITESPACE,
|
|
342
|
+
value,
|
|
343
|
+
range: { start, end: this.getPosition() }
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
// JSON array
|
|
347
|
+
if (char === '[') {
|
|
348
|
+
const value = this.readJsonArray();
|
|
349
|
+
return {
|
|
350
|
+
type: TokenType.JSON_ARRAY,
|
|
351
|
+
value,
|
|
352
|
+
range: { start, end: this.getPosition() }
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
// Quoted string
|
|
356
|
+
if (char === '"' || char === "'") {
|
|
357
|
+
const value = this.readQuotedString(char);
|
|
358
|
+
return {
|
|
359
|
+
type: TokenType.QUOTED_STRING,
|
|
360
|
+
value,
|
|
361
|
+
range: { start, end: this.getPosition() }
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
// Flag (--something)
|
|
365
|
+
if (char === '-' && this.peek(1) === '-') {
|
|
366
|
+
const value = this.readWord();
|
|
367
|
+
return {
|
|
368
|
+
type: TokenType.FLAG,
|
|
369
|
+
value,
|
|
370
|
+
range: { start, end: this.getPosition() }
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
// Equals
|
|
374
|
+
if (char === '=') {
|
|
375
|
+
this.advance();
|
|
376
|
+
return {
|
|
377
|
+
type: TokenType.EQUALS,
|
|
378
|
+
value: '=',
|
|
379
|
+
range: { start, end: this.getPosition() }
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
// Word (instruction or string)
|
|
383
|
+
// Save atLineStart before reading the word since readWord will change it
|
|
384
|
+
const wasAtLineStart = this.atLineStart;
|
|
385
|
+
const word = this.readWord();
|
|
386
|
+
const upperWord = word.toUpperCase();
|
|
387
|
+
if (INSTRUCTIONS.has(upperWord) && wasAtLineStart) {
|
|
388
|
+
return {
|
|
389
|
+
type: TokenType[upperWord] || TokenType.STRING,
|
|
390
|
+
value: word,
|
|
391
|
+
range: { start, end: this.getPosition() }
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
type: TokenType.STRING,
|
|
396
|
+
value: word,
|
|
397
|
+
range: { start, end: this.getPosition() }
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Tokenize entire input
|
|
402
|
+
*/
|
|
403
|
+
tokenize() {
|
|
404
|
+
const tokens = [];
|
|
405
|
+
let token;
|
|
406
|
+
do {
|
|
407
|
+
token = this.nextToken();
|
|
408
|
+
tokens.push(token);
|
|
409
|
+
} while (token.type !== TokenType.EOF);
|
|
410
|
+
return tokens;
|
|
411
|
+
}
|
|
412
|
+
}
|