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/parser.js ADDED
@@ -0,0 +1,948 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Parser = void 0;
4
+ exports.parse = parse;
5
+ const bash_ast_1 = require("bash-ast");
6
+ const lexer_1 = require("./lexer");
7
+ /**
8
+ * Dockerfile Parser
9
+ */
10
+ class Parser {
11
+ tokens = [];
12
+ pos = 0;
13
+ escapeChar = '\\';
14
+ options;
15
+ constructor(options = {}) {
16
+ this.options = {
17
+ includeComments: true,
18
+ ...options
19
+ };
20
+ }
21
+ /**
22
+ * Parse Dockerfile source into AST
23
+ */
24
+ parse(source) {
25
+ const lexer = new lexer_1.Lexer(source);
26
+ // First pass: look for escape directive
27
+ const tempTokens = lexer.tokenize();
28
+ for (const token of tempTokens) {
29
+ if (token.type === lexer_1.TokenType.DIRECTIVE) {
30
+ const [directive, value] = token.value.split('=');
31
+ if (directive === 'escape' && (value === '\\' || value === '`')) {
32
+ this.escapeChar = value;
33
+ break;
34
+ }
35
+ }
36
+ if (token.type !== lexer_1.TokenType.NEWLINE && token.type !== lexer_1.TokenType.WHITESPACE && token.type !== lexer_1.TokenType.COMMENT) {
37
+ break;
38
+ }
39
+ }
40
+ // Second pass with correct escape character
41
+ const lexer2 = new lexer_1.Lexer(source);
42
+ lexer2.setEscapeChar(this.escapeChar);
43
+ this.tokens = lexer2.tokenize();
44
+ this.pos = 0;
45
+ const dockerfile = {
46
+ type: 'Dockerfile',
47
+ directives: [],
48
+ stages: [],
49
+ comments: []
50
+ };
51
+ // Parse directives first
52
+ while (!this.isAtEnd()) {
53
+ this.skipWhitespaceAndNewlines();
54
+ if (this.isAtEnd())
55
+ break;
56
+ const token = this.peek();
57
+ if (token.type === lexer_1.TokenType.DIRECTIVE) {
58
+ dockerfile.directives.push(this.parseDirective());
59
+ }
60
+ else if (token.type === lexer_1.TokenType.COMMENT) {
61
+ if (this.options.includeComments) {
62
+ dockerfile.comments.push(this.parseComment());
63
+ }
64
+ else {
65
+ this.advance();
66
+ }
67
+ }
68
+ else {
69
+ break;
70
+ }
71
+ }
72
+ // Parse stages and instructions
73
+ let currentStage = null;
74
+ while (!this.isAtEnd()) {
75
+ this.skipWhitespaceAndNewlines();
76
+ if (this.isAtEnd())
77
+ break;
78
+ const token = this.peek();
79
+ if (token.type === lexer_1.TokenType.COMMENT) {
80
+ if (this.options.includeComments) {
81
+ dockerfile.comments.push(this.parseComment());
82
+ }
83
+ else {
84
+ this.advance();
85
+ }
86
+ continue;
87
+ }
88
+ if (token.type === lexer_1.TokenType.FROM) {
89
+ const from = this.parseFrom();
90
+ currentStage = {
91
+ type: 'Stage',
92
+ from,
93
+ instructions: [],
94
+ name: from.name,
95
+ range: from.range
96
+ };
97
+ dockerfile.stages.push(currentStage);
98
+ continue;
99
+ }
100
+ // Parse other instructions
101
+ const instruction = this.parseInstruction();
102
+ if (instruction) {
103
+ if (currentStage) {
104
+ currentStage.instructions.push(instruction);
105
+ }
106
+ else {
107
+ // Instructions before first FROM (like ARG)
108
+ if (!dockerfile.stages.length) {
109
+ // Create a virtual stage for pre-FROM instructions
110
+ currentStage = {
111
+ type: 'Stage',
112
+ from: {
113
+ type: 'FromInstruction',
114
+ instruction: 'FROM',
115
+ image: 'scratch'
116
+ },
117
+ instructions: [instruction]
118
+ };
119
+ dockerfile.stages.push(currentStage);
120
+ }
121
+ }
122
+ }
123
+ }
124
+ return dockerfile;
125
+ }
126
+ /**
127
+ * Check if at end of tokens
128
+ */
129
+ isAtEnd() {
130
+ return this.pos >= this.tokens.length || this.peek().type === lexer_1.TokenType.EOF;
131
+ }
132
+ /**
133
+ * Peek at current token
134
+ */
135
+ peek(offset = 0) {
136
+ const idx = this.pos + offset;
137
+ if (idx >= this.tokens.length) {
138
+ return this.tokens[this.tokens.length - 1];
139
+ }
140
+ return this.tokens[idx];
141
+ }
142
+ /**
143
+ * Advance to next token
144
+ */
145
+ advance() {
146
+ if (!this.isAtEnd()) {
147
+ this.pos++;
148
+ }
149
+ return this.tokens[this.pos - 1];
150
+ }
151
+ /**
152
+ * Skip whitespace and newlines
153
+ */
154
+ skipWhitespaceAndNewlines() {
155
+ while (!this.isAtEnd()) {
156
+ const token = this.peek();
157
+ if (token.type === lexer_1.TokenType.WHITESPACE || token.type === lexer_1.TokenType.NEWLINE) {
158
+ this.advance();
159
+ }
160
+ else {
161
+ break;
162
+ }
163
+ }
164
+ }
165
+ /**
166
+ * Skip whitespace only
167
+ */
168
+ skipWhitespace() {
169
+ while (!this.isAtEnd() && this.peek().type === lexer_1.TokenType.WHITESPACE) {
170
+ this.advance();
171
+ }
172
+ }
173
+ /**
174
+ * Collect arguments until newline
175
+ * Combines key=value tokens into single arguments
176
+ */
177
+ collectArguments() {
178
+ const args = [];
179
+ this.skipWhitespace();
180
+ while (!this.isAtEnd()) {
181
+ const token = this.peek();
182
+ if (token.type === lexer_1.TokenType.NEWLINE || token.type === lexer_1.TokenType.EOF) {
183
+ break;
184
+ }
185
+ if (token.type === lexer_1.TokenType.WHITESPACE) {
186
+ this.advance();
187
+ continue;
188
+ }
189
+ // Check if this is a key=value pattern (STRING EQUALS STRING)
190
+ if (token.type === lexer_1.TokenType.STRING && this.peek(1).type === lexer_1.TokenType.EQUALS) {
191
+ let combined = token.value;
192
+ this.advance(); // STRING
193
+ combined += this.advance().value; // EQUALS
194
+ if (!this.isAtEnd() && this.peek().type !== lexer_1.TokenType.WHITESPACE &&
195
+ this.peek().type !== lexer_1.TokenType.NEWLINE && this.peek().type !== lexer_1.TokenType.EOF) {
196
+ combined += this.advance().value; // value
197
+ }
198
+ args.push(combined);
199
+ }
200
+ else {
201
+ args.push(token.value);
202
+ this.advance();
203
+ }
204
+ }
205
+ return args;
206
+ }
207
+ /**
208
+ * Collect rest of line as single string
209
+ */
210
+ collectRestOfLine() {
211
+ const parts = [];
212
+ this.skipWhitespace();
213
+ while (!this.isAtEnd()) {
214
+ const token = this.peek();
215
+ if (token.type === lexer_1.TokenType.NEWLINE || token.type === lexer_1.TokenType.EOF) {
216
+ break;
217
+ }
218
+ parts.push(token.value);
219
+ this.advance();
220
+ }
221
+ return parts.join('').trim();
222
+ }
223
+ /**
224
+ * Parse parser directive
225
+ */
226
+ parseDirective() {
227
+ const token = this.advance();
228
+ const [directive, value] = token.value.split('=');
229
+ return {
230
+ type: 'ParserDirective',
231
+ directive,
232
+ value,
233
+ range: token.range
234
+ };
235
+ }
236
+ /**
237
+ * Parse comment
238
+ */
239
+ parseComment() {
240
+ const token = this.advance();
241
+ return {
242
+ type: 'Comment',
243
+ value: token.value,
244
+ range: token.range
245
+ };
246
+ }
247
+ /**
248
+ * Parse FROM instruction
249
+ */
250
+ parseFrom() {
251
+ const startToken = this.advance(); // FROM
252
+ this.skipWhitespace();
253
+ let platform;
254
+ let image = '';
255
+ let tag;
256
+ let digest;
257
+ let name;
258
+ // Check for --platform flag
259
+ if (this.peek().type === lexer_1.TokenType.FLAG && this.peek().value.startsWith('--platform')) {
260
+ const flag = this.advance().value;
261
+ if (flag.includes('=')) {
262
+ platform = flag.split('=')[1];
263
+ }
264
+ else {
265
+ this.skipWhitespace();
266
+ if (this.peek().type === lexer_1.TokenType.EQUALS) {
267
+ this.advance();
268
+ }
269
+ this.skipWhitespace();
270
+ platform = this.advance().value;
271
+ }
272
+ this.skipWhitespace();
273
+ }
274
+ // Parse image reference
275
+ const imageToken = this.advance();
276
+ const imageRef = imageToken.value;
277
+ // Parse image:tag@digest
278
+ if (imageRef.includes('@')) {
279
+ const [beforeDigest, digestPart] = imageRef.split('@');
280
+ digest = digestPart;
281
+ if (beforeDigest.includes(':')) {
282
+ const [imagePart, tagPart] = beforeDigest.split(':');
283
+ image = imagePart;
284
+ tag = tagPart;
285
+ }
286
+ else {
287
+ image = beforeDigest;
288
+ }
289
+ }
290
+ else if (imageRef.includes(':')) {
291
+ const [imagePart, tagPart] = imageRef.split(':');
292
+ image = imagePart;
293
+ tag = tagPart;
294
+ }
295
+ else {
296
+ image = imageRef;
297
+ }
298
+ this.skipWhitespace();
299
+ // Check for AS alias
300
+ if (!this.isAtEnd() && this.peek().type === lexer_1.TokenType.STRING) {
301
+ const maybeAs = this.peek().value.toUpperCase();
302
+ if (maybeAs === 'AS') {
303
+ this.advance(); // AS
304
+ this.skipWhitespace();
305
+ name = this.advance().value;
306
+ }
307
+ }
308
+ return {
309
+ type: 'FromInstruction',
310
+ instruction: 'FROM',
311
+ image,
312
+ tag,
313
+ digest,
314
+ name,
315
+ platform,
316
+ range: startToken.range
317
+ };
318
+ }
319
+ /**
320
+ * Parse instruction based on type
321
+ */
322
+ parseInstruction() {
323
+ const token = this.peek();
324
+ switch (token.type) {
325
+ case lexer_1.TokenType.RUN:
326
+ return this.parseRun();
327
+ case lexer_1.TokenType.CMD:
328
+ return this.parseCmd();
329
+ case lexer_1.TokenType.ENTRYPOINT:
330
+ return this.parseEntrypoint();
331
+ case lexer_1.TokenType.COPY:
332
+ return this.parseCopy();
333
+ case lexer_1.TokenType.ADD:
334
+ return this.parseAdd();
335
+ case lexer_1.TokenType.ENV:
336
+ return this.parseEnv();
337
+ case lexer_1.TokenType.ARG:
338
+ return this.parseArg();
339
+ case lexer_1.TokenType.WORKDIR:
340
+ return this.parseWorkdir();
341
+ case lexer_1.TokenType.USER:
342
+ return this.parseUser();
343
+ case lexer_1.TokenType.EXPOSE:
344
+ return this.parseExpose();
345
+ case lexer_1.TokenType.VOLUME:
346
+ return this.parseVolume();
347
+ case lexer_1.TokenType.LABEL:
348
+ return this.parseLabel();
349
+ case lexer_1.TokenType.SHELL:
350
+ return this.parseShell();
351
+ case lexer_1.TokenType.HEALTHCHECK:
352
+ return this.parseHealthcheck();
353
+ case lexer_1.TokenType.STOPSIGNAL:
354
+ return this.parseStopsignal();
355
+ case lexer_1.TokenType.ONBUILD:
356
+ return this.parseOnbuild();
357
+ case lexer_1.TokenType.MAINTAINER:
358
+ return this.parseMaintainer();
359
+ default:
360
+ // Skip unknown tokens
361
+ this.advance();
362
+ return null;
363
+ }
364
+ }
365
+ /**
366
+ * Parse RUN instruction
367
+ */
368
+ parseRun() {
369
+ const startToken = this.advance(); // RUN
370
+ this.skipWhitespace();
371
+ const mounts = [];
372
+ let network;
373
+ let security;
374
+ // Parse flags
375
+ while (this.peek().type === lexer_1.TokenType.FLAG) {
376
+ const flag = this.advance().value;
377
+ if (flag.startsWith('--mount')) {
378
+ mounts.push(this.parseMountFlag(flag));
379
+ }
380
+ else if (flag.startsWith('--network')) {
381
+ network = this.extractFlagValue(flag, 'network');
382
+ }
383
+ else if (flag.startsWith('--security')) {
384
+ security = this.extractFlagValue(flag, 'security');
385
+ }
386
+ this.skipWhitespace();
387
+ }
388
+ // Check for JSON array (exec form)
389
+ if (this.peek().type === lexer_1.TokenType.JSON_ARRAY) {
390
+ const jsonStr = this.advance().value;
391
+ const command = JSON.parse(jsonStr);
392
+ return {
393
+ type: 'RunInstruction',
394
+ instruction: 'RUN',
395
+ command,
396
+ isExec: true,
397
+ mount: mounts.length > 0 ? mounts : undefined,
398
+ network,
399
+ security,
400
+ range: startToken.range
401
+ };
402
+ }
403
+ // Shell form - also parse with bash-ast for heterogeneous AST
404
+ const command = this.collectRestOfLine();
405
+ let bashAst;
406
+ try {
407
+ bashAst = (0, bash_ast_1.parse)(command);
408
+ }
409
+ catch {
410
+ // If bash parsing fails, leave bashAst undefined
411
+ }
412
+ return {
413
+ type: 'RunInstruction',
414
+ instruction: 'RUN',
415
+ command,
416
+ isExec: false,
417
+ mount: mounts.length > 0 ? mounts : undefined,
418
+ network,
419
+ security,
420
+ bashAst,
421
+ range: startToken.range
422
+ };
423
+ }
424
+ /**
425
+ * Parse mount flag
426
+ */
427
+ parseMountFlag(flag) {
428
+ const mount = { type: 'bind' };
429
+ const value = flag.replace('--mount=', '').replace('--mount', '');
430
+ if (value) {
431
+ const parts = value.split(',');
432
+ for (const part of parts) {
433
+ const [key, val] = part.split('=');
434
+ switch (key) {
435
+ case 'type':
436
+ mount.type = val;
437
+ break;
438
+ case 'target':
439
+ case 'dst':
440
+ case 'destination':
441
+ mount.target = val;
442
+ break;
443
+ case 'source':
444
+ case 'src':
445
+ mount.source = val;
446
+ break;
447
+ case 'from':
448
+ mount.from = val;
449
+ break;
450
+ case 'id':
451
+ mount.id = val;
452
+ break;
453
+ case 'sharing':
454
+ mount.sharing = val;
455
+ break;
456
+ case 'readonly':
457
+ case 'ro':
458
+ mount.readonly = val !== 'false';
459
+ break;
460
+ case 'required':
461
+ mount.required = val !== 'false';
462
+ break;
463
+ case 'mode':
464
+ mount.mode = val;
465
+ break;
466
+ case 'uid':
467
+ mount.uid = val;
468
+ break;
469
+ case 'gid':
470
+ mount.gid = val;
471
+ break;
472
+ }
473
+ }
474
+ }
475
+ return mount;
476
+ }
477
+ /**
478
+ * Extract value from flag
479
+ */
480
+ extractFlagValue(flag, _name) {
481
+ if (flag.includes('=')) {
482
+ return flag.split('=')[1];
483
+ }
484
+ this.skipWhitespace();
485
+ if (this.peek().type === lexer_1.TokenType.EQUALS) {
486
+ this.advance();
487
+ this.skipWhitespace();
488
+ }
489
+ return this.advance().value;
490
+ }
491
+ /**
492
+ * Parse CMD instruction
493
+ */
494
+ parseCmd() {
495
+ const startToken = this.advance(); // CMD
496
+ this.skipWhitespace();
497
+ if (this.peek().type === lexer_1.TokenType.JSON_ARRAY) {
498
+ const jsonStr = this.advance().value;
499
+ const command = JSON.parse(jsonStr);
500
+ return {
501
+ type: 'CmdInstruction',
502
+ instruction: 'CMD',
503
+ command,
504
+ isExec: true,
505
+ range: startToken.range
506
+ };
507
+ }
508
+ const command = this.collectRestOfLine();
509
+ return {
510
+ type: 'CmdInstruction',
511
+ instruction: 'CMD',
512
+ command,
513
+ isExec: false,
514
+ range: startToken.range
515
+ };
516
+ }
517
+ /**
518
+ * Parse ENTRYPOINT instruction
519
+ */
520
+ parseEntrypoint() {
521
+ const startToken = this.advance(); // ENTRYPOINT
522
+ this.skipWhitespace();
523
+ if (this.peek().type === lexer_1.TokenType.JSON_ARRAY) {
524
+ const jsonStr = this.advance().value;
525
+ const command = JSON.parse(jsonStr);
526
+ return {
527
+ type: 'EntrypointInstruction',
528
+ instruction: 'ENTRYPOINT',
529
+ command,
530
+ isExec: true,
531
+ range: startToken.range
532
+ };
533
+ }
534
+ const command = this.collectRestOfLine();
535
+ return {
536
+ type: 'EntrypointInstruction',
537
+ instruction: 'ENTRYPOINT',
538
+ command,
539
+ isExec: false,
540
+ range: startToken.range
541
+ };
542
+ }
543
+ /**
544
+ * Parse COPY instruction
545
+ */
546
+ parseCopy() {
547
+ const startToken = this.advance(); // COPY
548
+ this.skipWhitespace();
549
+ let from;
550
+ let chown;
551
+ let chmod;
552
+ let link;
553
+ let parents;
554
+ const exclude = [];
555
+ // Parse flags
556
+ while (this.peek().type === lexer_1.TokenType.FLAG) {
557
+ const flag = this.advance().value;
558
+ if (flag.startsWith('--from')) {
559
+ from = this.extractFlagValue(flag, 'from');
560
+ }
561
+ else if (flag.startsWith('--chown')) {
562
+ chown = this.extractFlagValue(flag, 'chown');
563
+ }
564
+ else if (flag.startsWith('--chmod')) {
565
+ chmod = this.extractFlagValue(flag, 'chmod');
566
+ }
567
+ else if (flag === '--link') {
568
+ link = true;
569
+ }
570
+ else if (flag === '--parents') {
571
+ parents = true;
572
+ }
573
+ else if (flag.startsWith('--exclude')) {
574
+ exclude.push(this.extractFlagValue(flag, 'exclude'));
575
+ }
576
+ this.skipWhitespace();
577
+ }
578
+ const args = this.collectArguments();
579
+ const destination = args.pop() || '';
580
+ const sources = args;
581
+ return {
582
+ type: 'CopyInstruction',
583
+ instruction: 'COPY',
584
+ sources,
585
+ destination,
586
+ from,
587
+ chown,
588
+ chmod,
589
+ link,
590
+ parents,
591
+ exclude: exclude.length > 0 ? exclude : undefined,
592
+ range: startToken.range
593
+ };
594
+ }
595
+ /**
596
+ * Parse ADD instruction
597
+ */
598
+ parseAdd() {
599
+ const startToken = this.advance(); // ADD
600
+ this.skipWhitespace();
601
+ let chown;
602
+ let chmod;
603
+ let link;
604
+ let checksum;
605
+ let keepGitDir;
606
+ const exclude = [];
607
+ // Parse flags
608
+ while (this.peek().type === lexer_1.TokenType.FLAG) {
609
+ const flag = this.advance().value;
610
+ if (flag.startsWith('--chown')) {
611
+ chown = this.extractFlagValue(flag, 'chown');
612
+ }
613
+ else if (flag.startsWith('--chmod')) {
614
+ chmod = this.extractFlagValue(flag, 'chmod');
615
+ }
616
+ else if (flag === '--link') {
617
+ link = true;
618
+ }
619
+ else if (flag.startsWith('--checksum')) {
620
+ checksum = this.extractFlagValue(flag, 'checksum');
621
+ }
622
+ else if (flag === '--keep-git-dir') {
623
+ keepGitDir = true;
624
+ }
625
+ else if (flag.startsWith('--exclude')) {
626
+ exclude.push(this.extractFlagValue(flag, 'exclude'));
627
+ }
628
+ this.skipWhitespace();
629
+ }
630
+ const args = this.collectArguments();
631
+ const destination = args.pop() || '';
632
+ const sources = args;
633
+ return {
634
+ type: 'AddInstruction',
635
+ instruction: 'ADD',
636
+ sources,
637
+ destination,
638
+ chown,
639
+ chmod,
640
+ link,
641
+ checksum,
642
+ keepGitDir,
643
+ exclude: exclude.length > 0 ? exclude : undefined,
644
+ range: startToken.range
645
+ };
646
+ }
647
+ /**
648
+ * Parse ENV instruction
649
+ */
650
+ parseEnv() {
651
+ const startToken = this.advance(); // ENV
652
+ this.skipWhitespace();
653
+ const variables = [];
654
+ const args = this.collectArguments();
655
+ // Check for key=value or key value format
656
+ if (args.length === 2 && !args[0].includes('=')) {
657
+ // Old format: ENV key value
658
+ variables.push({ key: args[0], value: args[1] });
659
+ }
660
+ else {
661
+ // New format: ENV key=value key2=value2
662
+ for (const arg of args) {
663
+ if (arg.includes('=')) {
664
+ const eqIdx = arg.indexOf('=');
665
+ const key = arg.substring(0, eqIdx);
666
+ const value = arg.substring(eqIdx + 1);
667
+ variables.push({ key, value: this.unquote(value) });
668
+ }
669
+ }
670
+ }
671
+ return {
672
+ type: 'EnvInstruction',
673
+ instruction: 'ENV',
674
+ variables,
675
+ range: startToken.range
676
+ };
677
+ }
678
+ /**
679
+ * Parse ARG instruction
680
+ */
681
+ parseArg() {
682
+ const startToken = this.advance(); // ARG
683
+ this.skipWhitespace();
684
+ const arg = this.collectRestOfLine();
685
+ let name;
686
+ let defaultValue;
687
+ if (arg.includes('=')) {
688
+ const eqIdx = arg.indexOf('=');
689
+ name = arg.substring(0, eqIdx);
690
+ defaultValue = arg.substring(eqIdx + 1);
691
+ }
692
+ else {
693
+ name = arg;
694
+ }
695
+ return {
696
+ type: 'ArgInstruction',
697
+ instruction: 'ARG',
698
+ name,
699
+ defaultValue,
700
+ range: startToken.range
701
+ };
702
+ }
703
+ /**
704
+ * Parse WORKDIR instruction
705
+ */
706
+ parseWorkdir() {
707
+ const startToken = this.advance(); // WORKDIR
708
+ this.skipWhitespace();
709
+ const path = this.collectRestOfLine();
710
+ return {
711
+ type: 'WorkdirInstruction',
712
+ instruction: 'WORKDIR',
713
+ path,
714
+ range: startToken.range
715
+ };
716
+ }
717
+ /**
718
+ * Parse USER instruction
719
+ */
720
+ parseUser() {
721
+ const startToken = this.advance(); // USER
722
+ this.skipWhitespace();
723
+ const userSpec = this.collectRestOfLine();
724
+ let user;
725
+ let group;
726
+ if (userSpec.includes(':')) {
727
+ [user, group] = userSpec.split(':');
728
+ }
729
+ else {
730
+ user = userSpec;
731
+ }
732
+ return {
733
+ type: 'UserInstruction',
734
+ instruction: 'USER',
735
+ user,
736
+ group,
737
+ range: startToken.range
738
+ };
739
+ }
740
+ /**
741
+ * Parse EXPOSE instruction
742
+ */
743
+ parseExpose() {
744
+ const startToken = this.advance(); // EXPOSE
745
+ this.skipWhitespace();
746
+ const args = this.collectArguments();
747
+ const ports = [];
748
+ for (const arg of args) {
749
+ if (arg.includes('/')) {
750
+ const [port, protocol] = arg.split('/');
751
+ ports.push({ port, protocol: protocol });
752
+ }
753
+ else {
754
+ ports.push({ port: arg });
755
+ }
756
+ }
757
+ return {
758
+ type: 'ExposeInstruction',
759
+ instruction: 'EXPOSE',
760
+ ports,
761
+ range: startToken.range
762
+ };
763
+ }
764
+ /**
765
+ * Parse VOLUME instruction
766
+ */
767
+ parseVolume() {
768
+ const startToken = this.advance(); // VOLUME
769
+ this.skipWhitespace();
770
+ let paths;
771
+ if (this.peek().type === lexer_1.TokenType.JSON_ARRAY) {
772
+ const jsonStr = this.advance().value;
773
+ paths = JSON.parse(jsonStr);
774
+ }
775
+ else {
776
+ paths = this.collectArguments();
777
+ }
778
+ return {
779
+ type: 'VolumeInstruction',
780
+ instruction: 'VOLUME',
781
+ paths,
782
+ range: startToken.range
783
+ };
784
+ }
785
+ /**
786
+ * Parse LABEL instruction
787
+ */
788
+ parseLabel() {
789
+ const startToken = this.advance(); // LABEL
790
+ this.skipWhitespace();
791
+ const labels = [];
792
+ const args = this.collectArguments();
793
+ for (const arg of args) {
794
+ if (arg.includes('=')) {
795
+ const eqIdx = arg.indexOf('=');
796
+ const key = arg.substring(0, eqIdx);
797
+ const value = arg.substring(eqIdx + 1);
798
+ labels.push({ key, value: this.unquote(value) });
799
+ }
800
+ }
801
+ return {
802
+ type: 'LabelInstruction',
803
+ instruction: 'LABEL',
804
+ labels,
805
+ range: startToken.range
806
+ };
807
+ }
808
+ /**
809
+ * Parse SHELL instruction
810
+ */
811
+ parseShell() {
812
+ const startToken = this.advance(); // SHELL
813
+ this.skipWhitespace();
814
+ let shell;
815
+ if (this.peek().type === lexer_1.TokenType.JSON_ARRAY) {
816
+ const jsonStr = this.advance().value;
817
+ shell = JSON.parse(jsonStr);
818
+ }
819
+ else {
820
+ shell = this.collectArguments();
821
+ }
822
+ return {
823
+ type: 'ShellInstruction',
824
+ instruction: 'SHELL',
825
+ shell,
826
+ range: startToken.range
827
+ };
828
+ }
829
+ /**
830
+ * Parse HEALTHCHECK instruction
831
+ */
832
+ parseHealthcheck() {
833
+ const startToken = this.advance(); // HEALTHCHECK
834
+ this.skipWhitespace();
835
+ // Check for NONE
836
+ if (this.peek().type === lexer_1.TokenType.STRING && this.peek().value.toUpperCase() === 'NONE') {
837
+ this.advance();
838
+ return {
839
+ type: 'HealthcheckInstruction',
840
+ instruction: 'HEALTHCHECK',
841
+ none: true,
842
+ range: startToken.range
843
+ };
844
+ }
845
+ let interval;
846
+ let timeout;
847
+ let startPeriod;
848
+ let startInterval;
849
+ let retries;
850
+ // Parse flags
851
+ while (this.peek().type === lexer_1.TokenType.FLAG) {
852
+ const flag = this.advance().value;
853
+ if (flag.startsWith('--interval')) {
854
+ interval = this.extractFlagValue(flag, 'interval');
855
+ }
856
+ else if (flag.startsWith('--timeout')) {
857
+ timeout = this.extractFlagValue(flag, 'timeout');
858
+ }
859
+ else if (flag.startsWith('--start-period')) {
860
+ startPeriod = this.extractFlagValue(flag, 'start-period');
861
+ }
862
+ else if (flag.startsWith('--start-interval')) {
863
+ startInterval = this.extractFlagValue(flag, 'start-interval');
864
+ }
865
+ else if (flag.startsWith('--retries')) {
866
+ retries = parseInt(this.extractFlagValue(flag, 'retries'), 10);
867
+ }
868
+ this.skipWhitespace();
869
+ }
870
+ // Parse CMD (may be tokenized as STRING since it's not at line start)
871
+ let command;
872
+ if (this.peek().type === lexer_1.TokenType.CMD ||
873
+ (this.peek().type === lexer_1.TokenType.STRING && this.peek().value.toUpperCase() === 'CMD')) {
874
+ command = this.parseCmd();
875
+ }
876
+ return {
877
+ type: 'HealthcheckInstruction',
878
+ instruction: 'HEALTHCHECK',
879
+ interval,
880
+ timeout,
881
+ startPeriod,
882
+ startInterval,
883
+ retries,
884
+ command,
885
+ range: startToken.range
886
+ };
887
+ }
888
+ /**
889
+ * Parse STOPSIGNAL instruction
890
+ */
891
+ parseStopsignal() {
892
+ const startToken = this.advance(); // STOPSIGNAL
893
+ this.skipWhitespace();
894
+ const signal = this.collectRestOfLine();
895
+ return {
896
+ type: 'StopsignalInstruction',
897
+ instruction: 'STOPSIGNAL',
898
+ signal,
899
+ range: startToken.range
900
+ };
901
+ }
902
+ /**
903
+ * Parse ONBUILD instruction
904
+ */
905
+ parseOnbuild() {
906
+ const startToken = this.advance(); // ONBUILD
907
+ this.skipWhitespace();
908
+ const trigger = this.parseInstruction();
909
+ return {
910
+ type: 'OnbuildInstruction',
911
+ instruction: 'ONBUILD',
912
+ trigger: trigger,
913
+ range: startToken.range
914
+ };
915
+ }
916
+ /**
917
+ * Parse MAINTAINER instruction (deprecated)
918
+ */
919
+ parseMaintainer() {
920
+ const startToken = this.advance(); // MAINTAINER
921
+ this.skipWhitespace();
922
+ const maintainer = this.collectRestOfLine();
923
+ return {
924
+ type: 'MaintainerInstruction',
925
+ instruction: 'MAINTAINER',
926
+ maintainer,
927
+ range: startToken.range
928
+ };
929
+ }
930
+ /**
931
+ * Remove quotes from string
932
+ */
933
+ unquote(str) {
934
+ if ((str.startsWith('"') && str.endsWith('"')) ||
935
+ (str.startsWith("'") && str.endsWith("'"))) {
936
+ return str.slice(1, -1);
937
+ }
938
+ return str;
939
+ }
940
+ }
941
+ exports.Parser = Parser;
942
+ /**
943
+ * Parse Dockerfile source into AST
944
+ */
945
+ function parse(source, options) {
946
+ const parser = new Parser(options);
947
+ return parser.parse(source);
948
+ }