Skip to content

Commit

Permalink
Better handle nested interpolation (fixes amplify-education#173)
Browse files Browse the repository at this point in the history
(breaks some reconstruction tests, tackling that next)
  • Loading branch information
weaversam8 committed Oct 14, 2024
1 parent 3d7ae42 commit 76804b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
12 changes: 6 additions & 6 deletions hcl2/hcl2.lark
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
start : body
body : (new_line_or_comment? (attribute | block))* new_line_or_comment?
attribute : identifier EQ expression
block : identifier (identifier | STRING_LIT)* new_line_or_comment? "{" body "}"
block : identifier (identifier | STRING_LIT | string_with_interpolation)* new_line_or_comment? "{" body "}"
new_line_and_or_comma: new_line_or_comment | "," | "," new_line_or_comment
new_line_or_comment: ( NL_OR_COMMENT )+
NL_OR_COMMENT: /\n[ \t]*/ | /#.*\n/ | /\/\/.*\n/ | /\/\*(.|\n)*?(\*\/)/
Expand All @@ -28,6 +28,7 @@ expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
| float_lit
| int_lit
| STRING_LIT
| string_with_interpolation
| tuple
| object
| function_call
Expand All @@ -42,11 +43,10 @@ expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
| for_tuple_expr
| for_object_expr


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

int_lit : DECIMAL+
!float_lit: DECIMAL+ "." DECIMAL+ (EXP_MARK DECIMAL+)?
Expand Down
11 changes: 11 additions & 0 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ def for_object_expr(self, args: List) -> str:
# e.g. f"{2 + 2} {{2 + 2}}" == "4 {2 + 2}"
return f"{{{for_expr}}}"

def string_with_interpolation(self, args: List) -> str:
return '"' + ("".join(args)) + '"'

def interpolation_maybe_nested(self, args: List) -> str:
# return "".join(args)
return "${" + ("".join(args)) + "}"

def strip_new_line_tokens(self, args: List) -> List:
"""
Remove new line and Discard tokens.
Expand All @@ -287,6 +294,10 @@ def strip_new_line_tokens(self, args: List) -> List:
def to_string_dollar(self, value: Any) -> Any:
"""Wrap a string in ${ and }"""
if isinstance(value, str):
# if it's already wrapped, pass it unmodified
if value.startswith("${") and value.endswith("}"):
return value

if value.startswith('"') and value.endswith('"'):
value = str(value)[1:-1]
return self.process_escape_sequences(value)
Expand Down
10 changes: 10 additions & 0 deletions test/helpers/terraform-config-json/multi_level_interpolation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"block": [
{
"a": "${\"${\"${\"a\"}\"}\"}"
},
{
"b": "${var.b}"
}
]
}
7 changes: 7 additions & 0 deletions test/helpers/terraform-config/multi_level_interpolation.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
block {
a = "${"${"${"a"}"}"}"
}

block {
b = "${var.b}"
}

0 comments on commit 76804b3

Please sign in to comment.