Commit a36416d
Fix HCL parser for comments with CR-only line endings (#6532)
* Support CR-only line endings in HCL line comments
The HCL lexer's LINE_COMMENT rule only supported LF (\n) or CRLF (\r\n)
line endings, but not CR-only (\r) line endings (classic Mac-style).
This caused parsing failures for files with CR-only line endings after
comments, such as "# comment\rlocals { a = 1 }".
The fix changes the lexer rule from:
LINE_COMMENT : ('//' | '#') ~[\r\n]* '\r'? ('\n' | EOF) -> channel(HIDDEN);
to:
LINE_COMMENT : ('//' | '#') ~[\r\n]* ('\r\n' | '\r' | '\n' | EOF) -> channel(HIDDEN);
This now accepts all three line ending styles: CRLF, LF, and CR.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Add comprehensive tests for HCL comments at file boundaries
Add extensive test coverage for HCL comments at the start and end of
Terraform/HCL files, including:
- Comments as the only content in a file
- Comments at the very first character of files
- Comments at the end without trailing newlines
- Multiple comments at start/end
- Block comments at file boundaries
- CRLF and CR-only line endings
- Unicode and emoji in comments
- Realistic Terraform patterns with header/footer comments
- Edge cases with heredocs and comments
The CR-only line ending tests pass after the lexer grammar fix.
One test is disabled pending further investigation:
- emojiInComment: Emoji (surrogate pair) in comments at the start of
the file causes cursor tracking issues due to a mismatch between
ANTLR character indices and the parser visitor's code point tracking.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Restore original headers
* Copy the `antlrGeneration` approach from rewrite-toml
* Handle all newlines in NEWLINE
* Shorten LINE_COMMENT
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent 718b88c commit a36416d
9 files changed
Lines changed: 1087 additions & 317 deletions
File tree
- rewrite-hcl
- src
- main
- antlr
- java/org/openrewrite/hcl/internal/grammar
- test/java/org/openrewrite/hcl/tree
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | | - | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
31 | 36 | | |
32 | 37 | | |
33 | 38 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
60 | 60 | | |
61 | | - | |
62 | | - | |
| 61 | + | |
| 62 | + | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| |||
Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 303 additions & 305 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| 85 | + | |
Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 6 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | | - | |
| 78 | + | |
| 79 | + | |
78 | 80 | | |
79 | 81 | | |
80 | 82 | | |
| |||
87 | 89 | | |
88 | 90 | | |
89 | 91 | | |
90 | | - | |
| 92 | + | |
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
| |||
3511 | 3513 | | |
3512 | 3514 | | |
3513 | 3515 | | |
3514 | | - | |
| 3516 | + | |
3515 | 3517 | | |
3516 | 3518 | | |
3517 | 3519 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| 85 | + | |
0 commit comments