Skip to content

Commit 4032a63

Browse files
authored
Fix dockerfile parsing issue (#7224)
1 parent bdb64b5 commit 4032a63

13 files changed

Lines changed: 922 additions & 838 deletions

File tree

rewrite-docker/src/main/antlr/DockerLexer.g4

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,14 @@ EQUALS : '=' { if (!afterHealthcheck) atLineStart = false; };
8888
// Flag with optional value: --name or --name=value
8989
// Captures the entire flag as a single token, stopping at whitespace
9090
// This avoids the greedy flagValue+ parsing issue while keeping shell commands working
91-
FLAG : '--' [a-zA-Z] [a-zA-Z0-9_-]* ('=' ~[ \t\r\n]+)? { if (!afterHealthcheck) atLineStart = false; };
91+
// Flag values can contain quoted strings (which may include spaces)
92+
FLAG : '--' [a-zA-Z] [a-zA-Z0-9_-]* ('=' FLAG_VALUE_PART+)? { if (!afterHealthcheck) atLineStart = false; };
93+
fragment FLAG_VALUE_PART
94+
: '"' ( '\\' ~[\r\n] | ~["\\\r\n] )* '"' // Double-quoted string (with escapes)
95+
| '\'' ~['\r\n]* '\'' // Single-quoted string (literal)
96+
| ~[ \t\r\n"'\\] // Unquoted character
97+
| '\\' ~[\r\n] // Escaped character
98+
;
9299
93100
// Standalone -- (double dash without flag name) - used in shell commands
94101
DASH_DASH : '--' { if (!afterHealthcheck) atLineStart = false; };
@@ -134,6 +141,10 @@ fragment COMMAND_SUBST_INNER : COMMAND_SUBST | ~[()];
134141
// Content cannot span newlines (backtick command substitution doesn't support that)
135142
BACKTICK_SUBST : '`' ~[ \t\r\n`] ~[`\r\n]* '`' { atLineStart = false; };
136143

144+
// Lone dollar sign that doesn't match ENV_VAR, SPECIAL_VAR, or COMMAND_SUBST
145+
// This handles cases like $'hello' (bash ANSI-C quoting) where $ precedes a quote
146+
DOLLAR : '$' { atLineStart = false; };
147+
137148
// Unquoted text (arguments, file paths, etc.)
138149
// This should be after more specific tokens
139150
// Note: comma is NOT excluded here - it's only special in JSON arrays

rewrite-docker/src/main/antlr/DockerParser.g4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ shellFormTextElement
169169
| COMMAND_SUBST // Allow $(command) in shell commands
170170
| BACKTICK_SUBST // Allow `command` in shell commands
171171
| SPECIAL_VAR // Allow $!, $$, $?, etc. in shell commands
172+
| DOLLAR // Allow lone $ in shell commands (e.g., $'hello' ANSI-C quoting)
172173
| EQUALS
173174
| FLAG // Allow --option or --option=value in shell commands
174175
| DASH_DASH
@@ -200,6 +201,7 @@ preambleElement
200201
| COMMAND_SUBST
201202
| BACKTICK_SUBST
202203
| SPECIAL_VAR
204+
| DOLLAR
203205
| EQUALS
204206
| FLAG
205207
| DASH_DASH
@@ -273,6 +275,7 @@ labelOldValueElement
273275
| COMMAND_SUBST
274276
| BACKTICK_SUBST
275277
| SPECIAL_VAR
278+
| DOLLAR
276279
| EQUALS
277280
| FLAG
278281
| DASH_DASH
@@ -327,6 +330,7 @@ envTextElementEquals
327330
| COMMAND_SUBST
328331
| BACKTICK_SUBST
329332
| SPECIAL_VAR
333+
| DOLLAR
330334
// NOTE: EQUALS is explicitly NOT included to allow multiple KEY=value pairs
331335
;
332336

@@ -410,6 +414,7 @@ textElement
410414
| COMMAND_SUBST // Allow $(command) in text
411415
| BACKTICK_SUBST // Allow `command` in text
412416
| SPECIAL_VAR // Allow $!, $$, $?, etc. in text
417+
| DOLLAR // Allow lone $ in text (e.g., $'hello' ANSI-C quoting)
413418
| EQUALS // Allow = in shell form text (e.g., ENV_VAR=value in RUN commands)
414419
| FLAG // Allow --option or --option=value in text
415420
| DASH_DASH // Allow -- in shell form text (e.g., --option in shell commands)

rewrite-docker/src/main/java/org/openrewrite/docker/internal/grammar/DockerLexer.interp

Lines changed: 5 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)