Skip to content

Commit ab97c39

Browse files
authored
Merge pull request #119 from amplify-education/feature/comments-inside-the-list-or-function-args
Add feature to parse comments in function args and list elems.
2 parents 59602e3 + 511c1b7 commit ab97c39

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

hcl2/hcl2.lark

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ int_lit : DECIMAL+
4646
DECIMAL : "0".."9"
4747
EXP_MARK : ("e" | "E") ("+" | "-")?
4848

49-
tuple : "[" (new_line_or_comment? expression (new_line_or_comment? "," new_line_or_comment? expression)* new_line_or_comment? ","?)? new_line_or_comment? "]"
49+
tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"
5050
object : "{" new_line_or_comment? (object_elem (new_line_and_or_comma object_elem )* new_line_and_or_comma?)? "}"
5151
object_elem : (identifier | expression) ("=" | ":") expression
5252

5353
heredoc_template : /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc)/
5454
heredoc_template_trim : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc_trim)/
5555

5656
function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")"
57-
arguments : (expression (new_line_or_comment? "," new_line_or_comment? expression)* ("," | "...")? new_line_or_comment?)
57+
arguments : (expression (new_line_or_comment* "," new_line_or_comment* expression)* ("," | "...")? new_line_or_comment*)
5858

5959
index_expr_term : expr_term index
6060
get_attr_expr_term : expr_term get_attr

test/helpers/terraform-config-json/multiline_expressions.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@
2727
"triggers": []
2828
}
2929
}
30+
},
31+
{
32+
"null_resource": {
33+
"multiline_comment_multiple_single_element": {
34+
"triggers": [
35+
2
36+
]
37+
}
38+
}
39+
}
40+
],
41+
"variable": [
42+
{
43+
"some_var2": {
44+
"description": "description",
45+
"type": "${string}",
46+
"default": "${cidrsubnets(\"10.0.0.0/24\", 2, 2)}"
47+
}
48+
},
49+
{
50+
"some_var2": {
51+
"description": "description",
52+
"default": "${concat([{'1': '1'}], [{'2': '2'}])}"
53+
}
3054
}
3155
]
3256
}

test/helpers/terraform-config/multiline_expressions.tf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,41 @@ resource "null_resource" "multiline_comment_single_line_after_opening_bracket" {
2020
triggers = [ /* "a", "b" */
2121
]
2222
}
23+
24+
resource "null_resource" "multiline_comment_multiple_single_element" {
25+
triggers = [ /* "a",
26+
"b" */
27+
2,
28+
# "c"
29+
]
30+
}
31+
32+
variable "some_var2" {
33+
description = "description"
34+
type = string
35+
default = cidrsubnets(
36+
# comment 1
37+
# comment 2
38+
"10.0.0.0/24",
39+
# comment 3
40+
# comment 4
41+
2,
42+
# comment 5
43+
# comment 6
44+
2
45+
# comment 7
46+
# comment 8
47+
)
48+
}
49+
50+
variable "some_var2" {
51+
description = "description"
52+
default = concat(
53+
# comment 1
54+
[{"1": "1"} /* comment 2 */ ],
55+
# comment 3
56+
# comment 4
57+
[{"2": "2"},]
58+
# comment 5
59+
)
60+
}

0 commit comments

Comments
 (0)