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