Skip to content

Commit c6402b6

Browse files
* revert interpolation grammar to v5.1.1 ( disable support for interpolations deeper than 2 levels)
1 parent 9693d42 commit c6402b6

7 files changed

Lines changed: 17 additions & 36 deletions

File tree

hcl2/hcl2.lark

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
start : body
22
body : (new_line_or_comment? (attribute | block))* new_line_or_comment?
33
attribute : identifier EQ expression
4-
block : identifier (identifier | STRING_LIT | string_with_interpolation)* new_line_or_comment? "{" body "}"
4+
block : identifier (identifier | STRING_LIT)* new_line_or_comment? "{" body "}"
55
new_line_or_comment: ( NL_OR_COMMENT )+
66
NL_OR_COMMENT: /\n[ \t]*/ | /#.*\n/ | /\/\/.*\n/ | /\/\*(.|\n)*?(\*\/)/
77

@@ -42,7 +42,6 @@ expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
4242
| float_lit
4343
| int_lit
4444
| STRING_LIT
45-
| string_with_interpolation
4645
| tuple
4746
| object
4847
| function_call
@@ -57,10 +56,11 @@ expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
5756
| for_tuple_expr
5857
| for_object_expr
5958

60-
STRING_LIT : "\"" STRING_CHARS? "\""
61-
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/ // any character except '"'
62-
string_with_interpolation: "\"" (STRING_CHARS)* interpolation_maybe_nested (STRING_CHARS | interpolation_maybe_nested)* "\""
63-
interpolation_maybe_nested: "${" expression "}"
59+
STRING_LIT : "\"" (STRING_CHARS | INTERPOLATION)* "\""
60+
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/+ // any character except '"" unless inside a interpolation string
61+
NESTED_INTERPOLATION : "${" /[^}]+/ "}"
62+
INTERPOLATION : "${" (/(?:(?!\${)([^}]))+/ | NESTED_INTERPOLATION)+ "}"
63+
6464

6565
int_lit : NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+
6666
!float_lit: (NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+) "." DECIMAL+ (EXP_MARK)?

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"locals": [{"simple_interpolation": "prefix:${var.foo}-suffix", "embedded_interpolation": "(long substring without interpolation); ${module.special_constants.aws_accounts[\"aaa-${local.foo}-${local.bar}\"]}/us-west-2/key_foo", "escaped_interpolation": "prefix:$${aws:username}-suffix"}]}

test/helpers/terraform-config/multi_level_interpolation.tf

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/helpers/terraform-config/locals_embedded_interpolation.tf renamed to test/helpers/terraform-config/string_interpolations.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
locals {
2+
simple_interpolation = "prefix:${var.foo}-suffix"
23
embedded_interpolation = "(long substring without interpolation); ${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo"
4+
escaped_interpolation = "prefix:$${aws:username}-suffix"
35
}

test/unit/test_builder.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ def test_locals_embedded_function_tf(self):
6464
def test_locals_embedded_interpolation_tf(self):
6565
builder = hcl2.Builder()
6666

67-
embedded_interpolation = (
68-
"(long substring without interpolation); ${module.special_constants.aws_accounts"
69-
'["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo'
70-
)
67+
attributes = {
68+
"simple_interpolation": "prefix:${var.foo}-suffix",
69+
"embedded_interpolation": "(long substring without interpolation); "
70+
'${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo',
71+
"escaped_interpolation": "prefix:$${aws:username}-suffix",
72+
}
7173

72-
builder.block("locals", embedded_interpolation=embedded_interpolation)
74+
builder.block("locals", **attributes)
7375

74-
self.compare_filenames(builder, "locals_embedded_interpolation.tf")
76+
self.compare_filenames(builder, "string_interpolations.tf")
7577

7678
def test_provider_function_tf(self):
7779
builder = hcl2.Builder()

0 commit comments

Comments
 (0)