Repository navigation
Expand file tree
/
Copy pathgrammar.js
More file actions
959 lines (862 loc) · 28.4 KB
/
Copy pathgrammar.js
File metadata and controls
959 lines (862 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
/// <reference types="tree-sitter-cli/dsl"/>
// @ts-check
/**
* @param {RuleOrLiteral} atom
* @param {RuleOrLiteral} continuation
* @returns {RuleOrLiteral}
*/
const spacedValue = (atom, continuation) =>
seq(repeat1(atom), repeat(continuation));
/**
* @param {RuleOrLiteral} continuation
* @returns {RuleOrLiteral}
*/
const continuedSpacedValue = (continuation) =>
seq(continuation, repeat(continuation));
/**
* An instruction keyword (case-insensitive regex) followed by the external
* scanner's zero-width word-boundary token, which succeeds only when the
* keyword is followed by whitespace, a line end, or EOF. This rejects glued
* forms like `FROMalpine` or `RUNecho`, which regex tokens alone cannot do
* (tree-sitter regexes have no lookahead, and `word:`-based keyword
* extraction only applies to string-literal keywords).
*
* @param {GrammarSymbols<string>} $
* @param {RegExp} pattern
* @param {string} name
* @returns {RuleOrLiteral}
*/
const keyword = ($, pattern, name) =>
seq(alias(pattern, name), $._keyword_terminator);
/**
* @param {GrammarSymbols<string>} $
* @param {RuleOrLiteral} atom
* @returns {RuleOrLiteral}
*/
const spacedValueContinuation = ($, atom) =>
seq(
alias($.required_line_continuation, $.line_continuation),
repeat1(atom),
);
/**
* The inner content of a double-quoted string, shared by the standalone rule
* and the immediate variant used for adjacent-segment concatenation.
*
* @param {GrammarSymbols<string>} $
* @returns {RuleOrLiteral}
*/
const doubleQuotedBody = ($) =>
repeat(
choice(
token.immediate(/[^"\n\\`\$]+/),
alias($.double_quoted_escape_sequence, $.escape_sequence),
token.immediate(/\$\(/),
'\\',
'`',
$._immediate_expansion,
// A `$` not starting a valid expansion is literal inside double quotes
// too (e.g. "$5", "cost$"); handled by the external scanner's lookahead.
$._literal_dollar,
),
);
/**
* The inner content of a single-quoted string (fully literal).
*
* @returns {RuleOrLiteral}
*/
const singleQuotedBody = () =>
repeat(
choice(
token.immediate(/[^'\n\\`]+/),
token.immediate(/[\\`]/),
),
);
/**
* A `KEY=` value made of one or more directly-adjacent segments that
* concatenate, mirroring shell word parsing (K=pre"mid"post -> premidpost).
* The first segment may lead with a quoted string (optionally trailed by an
* unquoted run) or a bare unquoted run; each subsequent quoted segment must
* abut the previous one and may itself be trailed by an unquoted run.
*
* @param {GrammarSymbols<string>} $
* @param {RuleOrLiteral} leadingQuoted
* @returns {RuleOrLiteral}
*/
const adjacentValue = ($, leadingQuoted) =>
seq(
choice(
seq(leadingQuoted, optional($.unquoted_string)),
$.unquoted_string,
),
repeat(
seq(
choice(
alias($._immediate_double_quoted_string, $.double_quoted_string),
alias($._immediate_single_quoted_string, $.single_quoted_string),
),
optional($.unquoted_string),
),
),
);
export default grammar({
name: 'containerfile',
extras: ($) => [/[ \t\r\f]+/, $.line_continuation, $.comment],
externals: ($) => [
$.comment,
$.line_continuation,
$.required_line_continuation,
$._newline,
$._invalid_json_array_shell_command,
$.heredoc_marker,
$._heredoc_line,
$.heredoc_end,
$._heredoc_nl,
$._literal_dollar,
$._keyword_terminator,
$.error_sentinel,
],
rules: {
source_file: ($) =>
seq(
repeat($._newline),
optional(
seq(
$._instruction,
repeat(seq(repeat1($._newline), $._instruction)),
repeat($._newline),
),
),
),
_instruction: ($) =>
choice(
$.from_instruction,
$.run_instruction,
$.cmd_instruction,
$.label_instruction,
$.expose_instruction,
$.env_instruction,
$.add_instruction,
$.copy_instruction,
$.entrypoint_instruction,
$.volume_instruction,
$.user_instruction,
$.workdir_instruction,
$.arg_instruction,
$.onbuild_instruction,
$.stopsignal_instruction,
$.healthcheck_instruction,
$.shell_instruction,
$.maintainer_instruction,
$.cross_build_instruction,
),
from_instruction: ($) =>
seq(
keyword($, /[fF][rR][oO][mM]/, 'FROM'),
optional($.param),
$.image_spec,
optional(seq(keyword($, /[aA][sS]/, 'AS'), field('as', $.image_alias))),
),
run_instruction: ($) =>
seq(
keyword($, /[rR][uU][nN]/, 'RUN'),
repeat(
choice(
$.param,
$.mount_param,
),
),
$._json_or_shell_command,
repeat($.heredoc_block),
),
cmd_instruction: ($) =>
seq(
keyword($, /[cC][mM][dD]/, 'CMD'),
$._json_or_shell_command,
),
label_instruction: ($) =>
seq(
keyword($, /[lL][aA][bB][eE][lL]/, 'LABEL'),
choice(repeat1($.label_pair), alias($._spaced_label_pair, $.label_pair)),
),
expose_instruction: ($) =>
seq(
keyword($, /[eE][xX][pP][oO][sS][eE]/, 'EXPOSE'),
repeat1($.expose_port),
),
env_instruction: ($) =>
seq(
keyword($, /[eE][nN][vV]/, 'ENV'),
choice(repeat1($.env_pair), alias($._spaced_env_pair, $.env_pair)),
),
add_instruction: ($) =>
seq(
keyword($, /[aA][dD][dD]/, 'ADD'),
repeat($.param),
choice(
$.json_string_array,
// After a bare `--` end-of-options separator, source paths may
// begin with `-`.
seq(
$.end_of_options,
repeat1(
seq(alias($.path_with_heredoc_or_dash, $.path), $._non_newline_whitespace),
),
alias($.path_with_heredoc_or_dash, $.path),
repeat($.heredoc_block),
),
seq(
repeat1(
seq(alias($.path_with_heredoc, $.path), $._non_newline_whitespace),
),
alias($.path_with_heredoc, $.path),
repeat($.heredoc_block),
),
),
),
copy_instruction: ($) =>
seq(
keyword($, /[cC][oO][pP][yY]/, 'COPY'),
repeat($.param),
choice(
$.json_string_array,
seq(
$.end_of_options,
repeat1(
seq(alias($.path_with_heredoc_or_dash, $.path), $._non_newline_whitespace),
),
alias($.path_with_heredoc_or_dash, $.path),
repeat($.heredoc_block),
),
seq(
repeat1(
seq(alias($.path_with_heredoc, $.path), $._non_newline_whitespace),
),
alias($.path_with_heredoc, $.path),
repeat($.heredoc_block),
),
),
),
entrypoint_instruction: ($) =>
seq(
keyword($, /[eE][nN][tT][rR][yY][pP][oO][iI][nN][tT]/, 'ENTRYPOINT'),
$._json_or_shell_command,
),
volume_instruction: ($) =>
seq(
keyword($, /[vV][oO][lL][uU][mM][eE]/, 'VOLUME'),
choice(
$.json_string_array,
seq($.path, repeat(seq($._non_newline_whitespace, $.path))),
),
),
user_instruction: ($) =>
seq(
keyword($, /[uU][sS][eE][rR]/, 'USER'),
field('user', alias($._user_name_or_group, $.unquoted_string)),
optional(
seq(
token.immediate(':'),
field('group',
alias($._immediate_user_name_or_group, $.unquoted_string)),
),
),
),
_user_name_or_group: ($) =>
seq(
choice(/([a-zA-Z_][-A-Za-z0-9_.]*|[0-9]+)/, $.expansion),
repeat($._immediate_user_name_or_group_fragment),
),
// same as _user_name_or_group but sticks to previous token
_immediate_user_name_or_group: ($) =>
repeat1($._immediate_user_name_or_group_fragment),
_immediate_user_name_or_group_fragment: ($) =>
choice(
token.immediate(/([a-zA-Z_][-a-zA-Z0-9_.]*|[0-9]+)/),
$._immediate_expansion,
),
workdir_instruction: ($) =>
seq(keyword($, /[wW][oO][rR][kK][dD][iI][rR]/, 'WORKDIR'), $.path),
arg_instruction: ($) =>
seq(
keyword($, /[aA][rR][gG]/, 'ARG'),
repeat1(
seq(
$._non_newline_whitespace,
$.arg_pair,
),
),
),
arg_pair: ($) =>
seq(
field('name', alias(/[a-zA-Z0-9_]+/, $.unquoted_string)),
optional(
seq(
token.immediate('='),
// The default is optional so an empty default (ARG X=) parses.
optional(
field('default',
adjacentValue(
$,
choice(
$.double_quoted_string,
$.single_quoted_string,
),
)),
),
),
),
),
onbuild_instruction: ($) =>
seq(keyword($, /[oO][nN][bB][uU][iI][lL][dD]/, 'ONBUILD'), $._instruction),
stopsignal_instruction: ($) =>
seq(
keyword($, /[sS][tT][oO][pP][sS][iI][gG][nN][aA][lL]/, 'STOPSIGNAL'),
$._stopsignal_value,
),
_stopsignal_value: ($) =>
choice(
$.double_quoted_string,
seq(
choice(/[A-Z0-9+-]+/, $.expansion),
repeat(choice(token.immediate(/[A-Z0-9+-]+/), $._immediate_expansion)),
),
),
healthcheck_instruction: ($) =>
seq(
keyword($, /[hH][eE][aA][lL][tT][hH][cC][hH][eE][cC][kK]/, 'HEALTHCHECK'),
optional(
choice(
'NONE',
seq(
repeat($.param),
choice(
alias($._healthcheck_cmd_instruction, $.cmd_instruction),
alias($._healthcheck_raw_command, $.shell_command),
),
),
),
),
),
_healthcheck_cmd_instruction: ($) =>
seq(
keyword($, /[cC][mM][dD]/, 'CMD'),
optional(choice($.json_string_array, $.shell_command)),
),
_healthcheck_raw_command: ($) =>
seq(
$._healthcheck_raw_fragment,
repeat(
seq(
alias($.required_line_continuation, $.line_continuation),
repeat($._newline),
$._shell_fragment,
),
),
),
_healthcheck_raw_fragment: ($) =>
seq(
$._healthcheck_raw_fragment_atom,
repeat($._shell_fragment_atom),
),
_healthcheck_raw_fragment_atom: ($) =>
choice(
seq($.heredoc_marker, /[ \t]*/),
/"([^"\\`\n]|[\\`].)*"/,
/'([^'\\`\n]|[\\`].)*'/,
$._shell_double_quoted_fragment,
$._shell_single_quoted_fragment,
/[,=-]/,
/[^cC\\`\[\n#\s,=\-"']([^\\`\n<"']|[\\`][^ \t\n\r])*/,
/[cC]([^mM\\`\n<"']|[\\`][^ \t\n\r])([^\\`\n<"']|[\\`][^ \t\n\r])*/,
/[cC][mM]([^dD\\`\n<"']|[\\`][^ \t\n\r])([^\\`\n<"']|[\\`][^ \t\n\r])*/,
/[cC][mM][dD][^\s\\`\n<"']([^\\`\n<"']|[\\`][^ \t\n\r])*/,
/[cC][mM]?/,
/[\\`][^\r\n,=-]/,
/[\\`]/,
/<[^<]/,
),
shell_instruction: ($) =>
seq(keyword($, /[sS][hH][eE][lL][lL]/, 'SHELL'), $.json_string_array),
maintainer_instruction: ($) =>
seq(
keyword($, /[mM][aA][iI][nN][tT][aA][iI][nN][eE][rR]/, 'MAINTAINER'),
/.*/,
),
cross_build_instruction: ($) =>
seq(
keyword(
$,
/[cC][rR][oO][sS][sS]_[bB][uU][iI][lL][dD][a-zA-Z_]*/,
'CROSS_BUILD',
),
/.*/,
),
heredoc_block: ($) =>
seq(
// A heredoc block starts with a line break after the instruction it
// belongs to. The herdoc_nl token is a special token that only matches
// \n if there's at least one open heredoc to avoid conflicts.
// We also alias this token to hide it from the output like all other
// whitespace.
$._heredoc_nl,
// The body (everything before the closing delimiter) is wrapped in a
// named node so language injection can target exactly the redirected
// content, excluding the heredoc_end delimiter line.
optional($.heredoc_content),
$.heredoc_end,
),
heredoc_content: ($) =>
repeat1(seq($._heredoc_line, '\n')),
path: ($) =>
seq(
choice(
/[^-\s\$<]/, // cannot start with a '-' to avoid conflicts with params
/<[^<]/, // cannot start with a '<<' to avoid conflicts with heredocs (a single < is fine, though)
$.expansion,
),
repeat(choice(token.immediate(/[^\s\$]+/), $._immediate_expansion)),
),
path_with_heredoc: ($) =>
choice(
$.heredoc_marker,
seq(
choice(
/[^-\s\$<]/, // cannot start with a '-' to avoid conflicts with params
/<[^-\s\$<]/,
$.expansion,
),
repeat(choice(token.immediate(/[^\s\$]+/), $._immediate_expansion)),
),
),
// Same as path_with_heredoc but the leading `-` restriction is lifted:
// used only after an end_of_options `--`, where a source path may begin
// with a dash (COPY -- -src /dst).
path_with_heredoc_or_dash: ($) =>
choice(
$.heredoc_marker,
seq(
choice(
/[^\s\$<]/,
/<[^<]/,
$.expansion,
),
repeat(choice(token.immediate(/[^\s\$]+/), $._immediate_expansion)),
),
),
expansion: $ =>
seq('$', $._expansion_body),
// we have 2 rules b/c aliases don't work as expected on seq() directly
_immediate_expansion: $ => alias($._imm_expansion, $.expansion),
_imm_expansion: $ =>
seq(token.immediate('$'), $._expansion_body),
_expansion_body: $ =>
choice(
$.variable,
seq(
token.immediate('{'),
alias(token.immediate(/[a-zA-Z_][a-zA-Z0-9_]*/), $.variable),
optional($.expansion_modifier),
token.immediate('}'),
),
),
// Shell-style parameter expansion modifier inside ${...}: a default/
// alternate/error operator (:- - :+ + :? ?) followed by a word that may
// itself contain nested expansions. The word runs to the matching brace.
// The pattern-manipulation operators (# ## % %% / //) are omitted: they
// are pre-release in BuildKit and `#` collides with comment lexing.
expansion_modifier: $ =>
seq(
$.expansion_operator,
repeat(
choice(
// Exclude quote chars from the literal run so a quoted word
// (e.g. ${var:-"blue}"}) is parsed as a string and its `}`/`$`
// do not terminate the expansion prematurely.
token.immediate(/[^}$"']+/),
alias($._immediate_double_quoted_string, $.double_quoted_string),
alias($._immediate_single_quoted_string, $.single_quoted_string),
$._immediate_expansion,
// A `$` not starting a valid expansion is literal inside a modifier
// word too (e.g. ${foo:-$5}, ${foo:-cost$)); handled by the scanner.
$._literal_dollar,
),
),
),
// A real (non-alias) rule so the node is a public `sym_` symbol that older
// tree-sitter query compilers (e.g. go-tree-sitter) accept in queries; an
// alias of an inline token compiles to an unqueryable `aux_sym`.
expansion_operator: () => token.immediate(/:?[-+?]/),
variable: () => token.immediate(/[a-zA-Z_][a-zA-Z0-9_]*/),
env_pair: ($) =>
seq(
field('name', $._env_key),
token.immediate('='),
optional(
field('value', $._env_assignment_value),
),
),
// A value is one or more directly-adjacent segments (quoted or unquoted)
// that concatenate, mirroring shell word parsing (ENV K=pre"mid"post ->
// premidpost). A quoted segment is the boundary between unquoted runs; an
// unquoted run immediately after a closing quote abuts it. unquoted_string
// is itself greedy, so it only ever appears once between quoted segments.
_env_assignment_value: ($) =>
adjacentValue(
$,
choice(
$.double_quoted_string,
$._env_single_quoted_string_with_trailing_quote,
),
),
_env_single_quoted_string_with_trailing_quote: ($) =>
seq($.single_quoted_string, optional($._env_trailing_single_quotes)),
_env_trailing_single_quotes: () => token.immediate(/'+/),
_spaced_env_pair: ($) =>
seq(
field('name', $._env_key),
choice(
seq(
token.immediate(/\s+/),
field('value',
choice(
$.double_quoted_string,
$.single_quoted_string,
alias($._spaced_env_value, $.unquoted_string),
)),
),
field('value', alias($._continued_spaced_env_value, $.unquoted_string)),
),
),
_env_key: ($) =>
alias(/[a-zA-Z_][a-zA-Z0-9_]*/, $.unquoted_string),
expose_port: ($) =>
seq(
choice(/\d+(-\d+)?/, $.expansion),
// Protocol is optional and case-insensitive; Docker accepts only tcp,
// udp, and sctp (and rejects anything else). It must abut the port.
optional(token.immediate(/\/([tT][cC][pP]|[uU][dD][pP]|[sS][cC][tT][pP])/)),
),
label_pair: ($) =>
seq(
field('key', $._label_key),
token.immediate('='),
field('value',
adjacentValue(
$,
choice(
$.double_quoted_string,
$.single_quoted_string,
),
)),
),
_spaced_label_pair: ($) =>
seq(
field('key', $._label_key),
// Legacy LABEL treats the first word as the key and the rest of the
// line as the value, so quotes remain literal value text.
choice(
seq(
token.immediate(/\s+/),
field('value', alias($._spaced_label_value, $.unquoted_string)),
),
field('value', alias($._continued_spaced_label_value, $.unquoted_string)),
),
),
_label_key: ($) =>
choice(
alias(/[-a-zA-Z0-9\._]+/, $.unquoted_string),
$.double_quoted_string,
$.single_quoted_string,
),
image_spec: ($) =>
seq(
field('name', $.image_name),
field('tag', optional($.image_tag)),
field('digest', optional($.image_digest)),
),
image_name: ($) =>
seq(
choice(/[^@:\s\$-]/, $.expansion),
repeat(
choice(
token.immediate(/[^@:\s\$]+/),
// A registry host may include a port: the `:PORT/` before the
// repository path is part of the name, not the tag (which is the
// colon after the final path segment). The port may be literal
// digits or a build-arg expansion (registry:${PORT}/img). Matched
// as a single token so it does not conflict with image_tag's `:`.
token.immediate(/:(\d+|\$[a-zA-Z_][a-zA-Z0-9_]*|\$\{[^}]+\})\//),
$._immediate_expansion,
),
),
),
image_tag: ($) =>
seq(
token.immediate(':'),
repeat1(choice(token.immediate(/[^@\s\$]+/), $._immediate_expansion)),
),
image_digest: ($) =>
seq(
token.immediate('@'),
repeat1(choice(token.immediate(/[a-zA-Z0-9:]+/), $._immediate_expansion)),
),
// Generic parsing of options passed right after an instruction name.
// The value surfaces $-expansions (e.g. --platform=$BUILDPLATFORM,
// --chown=$USER:$GROUP) as expansion nodes so they can be highlighted.
param: ($) =>
seq(
'--',
field('name', token.immediate(/[a-z][-a-z]*/)),
optional(
seq(
token.immediate('='),
field('value', $._param_value),
),
),
),
_param_value: ($) =>
repeat1(
choice(
token.immediate(/[^\s\$]+/),
$._immediate_expansion,
// A `$` not starting a valid expansion is literal text
// (--exclude=$5, a value ending in `$`); handled by the external
// scanner's lookahead. A real $ident / ${...} is left to the grammar.
$._literal_dollar,
),
),
// POSIX end-of-options separator (`COPY -- src dst`). BuildKit stops flag
// collection here; it's the documented way to pass a source path that
// begins with `-`. A bare `--` not followed by a flag name.
end_of_options: () => '--',
// Specific parsing of the --mount option e.g.
//
// --mount=type=cache,target=/root/.cache/go-build
//
mount_param: ($) => seq(
'--',
field('name', token.immediate('mount')),
token.immediate('='),
field(
'value',
seq(
$.mount_param_param,
repeat(
seq(token.immediate(','), $.mount_param_param),
),
),
),
),
mount_param_param: () => seq(
token.immediate(/[^\s=,]+/),
optional(seq(
token.immediate('='),
// The value is captured as a single literal run. (Unlike generic
// --flag values, mount option values do not surface $-expansions as
// nodes: the comma-separated mount grammar makes interleaving literal
// text with expansions ambiguous, so we keep the whole value literal
// to guarantee complete, non-truncating capture.)
token.immediate(/[^\s,]+/),
)),
),
image_alias: ($) => seq(
choice(/[-a-zA-Z0-9_.]+/, $.expansion),
repeat(choice(token.immediate(/[-a-zA-Z0-9_.]+/), $._immediate_expansion)),
),
_json_or_shell_command: ($) =>
choice(
alias($._invalid_json_array_shell_command, $.shell_command),
prec(1, $.json_string_array),
$.shell_command,
),
shell_command: ($) =>
seq(
$._shell_fragment,
repeat(
seq(
alias($.required_line_continuation, $.line_continuation),
repeat($._newline),
$._shell_fragment,
),
),
),
_shell_fragment: ($) => repeat1(
$._shell_fragment_atom,
),
_shell_fragment_atom: ($) =>
choice(
// A shell fragment is broken into the same tokens as other
// constructs because the lexer prefers the longer tokens
// when it has a choice. The example below shows the tokenization
// of the --mount parameter.
//
// RUN --mount=foo=bar,baz=42 ls --all
// ^^ ^ ^ ^ ^
// ^^^^^ ^^^ ^^^ ^^^ ^^
// |--------param-------|
// |--shell_command--|
//
seq($.heredoc_marker, /[ \t]*/),
/"([^"\\`\n]|[\\`].)*"/,
/'([^'\\`\n]|[\\`].)*'/,
$._shell_double_quoted_fragment,
$._shell_single_quoted_fragment,
/[,=-]/,
/[^\\`\[\n#\s,=\-"']([^\\`\n<"']|[\\`][^ \t\n\r])*/,
/[\\`][^\r\n,=-]/,
/[\\`]/,
/<[^<]/,
),
_shell_double_quoted_fragment: ($) =>
seq(
'"',
repeat(
choice(
token.immediate(prec(1, /[^"\\`\n]+/)),
token.immediate(/[\\`][^\r\n]/),
alias($.required_line_continuation, $.line_continuation),
$.line_continuation,
),
),
token.immediate('"'),
),
_shell_single_quoted_fragment: ($) =>
seq(
'\'',
repeat(
choice(
token.immediate(prec(1, /[^'\\`\n]+/)),
token.immediate(/[\\`][^\r\n]/),
alias($.required_line_continuation, $.line_continuation),
$.line_continuation,
),
),
token.immediate('\''),
),
json_string_array: ($) =>
seq(
'[',
optional(
seq($.json_string, repeat(seq(',', $.json_string))),
),
']',
),
// Note that JSON strings are different from the other double-quoted
// strings. They don't support $-expansions.
// Convenient reference: https://www.json.org/
json_string: ($) => seq(
'"',
repeat(
choice(
token.immediate(/[^"\\`\n]+/),
alias($.json_escape_sequence, $.escape_sequence),
token.immediate('`'),
),
),
'"',
),
json_escape_sequence: () => token.immediate(
/\\(?:["\\/bfnrt]|u[0-9A-Fa-f]{4})/,
),
double_quoted_string: ($) =>
seq('"', doubleQuotedBody($), '"'),
// Same as double_quoted_string but the opening quote must abut the
// preceding token, so it can concatenate onto an adjacent value segment.
_immediate_double_quoted_string: ($) =>
seq(token.immediate('"'), doubleQuotedBody($), '"'),
// Single-quoted strings are fully literal in Dockerfile shell parsing:
// there is no escape processing (a backslash or backtick before the
// closing quote does NOT escape it) and no $-expansions. A backslash/
// backtick before a newline is still a line continuation (BuildKit joins
// physical lines before quote processing), which is handled by the
// external line_continuation extra; an isolated backslash/backtick is
// literal text.
single_quoted_string: () =>
seq('\'', singleQuotedBody(), '\''),
_immediate_single_quoted_string: () =>
seq(token.immediate('\''), singleQuotedBody(), '\''),
unquoted_string: ($) =>
repeat1(
choice(
token.immediate(/[^\s\n\"'\\`\$]+/),
token.immediate(/[\\`] /),
token.immediate(/[\\`][^\s\n]/),
$._immediate_expansion,
// A `$` not starting a valid expansion is literal text. The external
// scanner emits _literal_dollar for a `$` that is NOT followed by an
// identifier char or `{` — the lookahead a regex token cannot do —
// covering $5, $$, cost:$5.00, a trailing `cost$`, and a bare `$`,
// while a real $ident / ${...} expansion is left to the grammar.
$._literal_dollar,
),
),
_spaced_label_value: ($) =>
spacedValue(
$._spaced_label_value_atom,
$._spaced_label_value_continuation,
),
_continued_spaced_label_value: ($) =>
continuedSpacedValue(
$._spaced_label_value_continuation,
),
_spaced_label_value_continuation: ($) =>
spacedValueContinuation(
$,
$._spaced_label_value_atom,
),
_spaced_label_value_atom: ($) =>
choice($._spaced_label_value_fragment, $._non_newline_whitespace),
_spaced_label_value_fragment: ($) =>
choice(
token.immediate(/[^\s\n\\`\$]+/),
token.immediate(/[\\`] /),
token.immediate(/[\\`][^\s\n]/),
$._immediate_expansion,
),
// The legacy `ENV KEY value` value must not START with a quote (a leading
// quote is parsed as a proper quoted string by _spaced_env_pair), so the
// first fragment excludes quotes. Later atoms keep quotes as literal text
// so a mid-line quote (ENV GREETING hello "world") does not error.
_spaced_env_value: ($) =>
seq(
$._spaced_env_value_first_fragment,
repeat($._spaced_env_value_atom),
repeat($._spaced_env_value_continuation),
),
_continued_spaced_env_value: ($) =>
continuedSpacedValue(
$._spaced_env_value_continuation,
),
_spaced_env_value_continuation: ($) =>
spacedValueContinuation(
$,
$._spaced_env_value_atom,
),
_spaced_env_value_atom: ($) =>
choice($._spaced_env_value_fragment, $._non_newline_whitespace),
// First fragment of a spaced value: excludes a leading quote.
_spaced_env_value_first_fragment: ($) =>
choice(
token.immediate(/[^\s\n\"'\\`\$]+/),
token.immediate(/[\\`] /),
token.immediate(/[\\`][^\s\n]/),
$._immediate_expansion,
),
// Legacy `ENV KEY value` takes the rest of the line as the value, with
// quotes kept as literal text (matching _spaced_label_value_fragment).
_spaced_env_value_fragment: ($) =>
choice(
token.immediate(/[^\s\n\\`\$]+/),
token.immediate(/[\\`] /),
token.immediate(/[\\`][^\s\n]/),
$._immediate_expansion,
),
// The active escape char (\ by default, or ` via `# escape=`) escapes
// ", \, `, and $ inside double-quoted strings. A \$ is literal text (the
// following identifier is NOT expanded).
double_quoted_escape_sequence: () => token.immediate(
/[\\`][\\`"]/,
),
_non_newline_whitespace: () => token.immediate(/[\t ]+/),
},
});