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