Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow expressions in string interpolation #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 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 "=" expression
block : identifier (identifier | STRING_LIT)* new_line_or_comment? "{" body "}"
block : identifier (identifier | string_lit)* new_line_or_comment? "{" body "}"
new_line_and_or_comma: new_line_or_comment | "," | "," new_line_or_comment
new_line_or_comment: ( /\n/ | /#.*\n/ | /\/\/.*\n/ )+

Expand All @@ -20,7 +20,7 @@ binary_term : binary_operator new_line_or_comment? expression
expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
| float_lit
| int_lit
| STRING_LIT
| string_lit
| tuple
| object
| function_call
Expand All @@ -35,10 +35,9 @@ expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
| for_object_expr


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

int_lit : DECIMAL+
!float_lit: DECIMAL+ "." DECIMAL+ (EXP_MARK DECIMAL+)?
Expand Down
6 changes: 6 additions & 0 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def float_lit(self, args: List) -> float:
def int_lit(self, args: List) -> int:
return int("".join([str(arg) for arg in args]))

def string_lit(self, args: List) -> str:
return '"' + "".join([str(arg) for arg in args]) + '"'

def interpolation(self, args: List) -> str:
return "".join([self.to_string_dollar(arg) for arg in args])

def expr_term(self, args: List) -> Any:
args = self.strip_new_line_tokens(args)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"locals": [
{
"nested_interpolation": "tags: ${join(\";\", ['names:${join(\",\", [\\'name:${example}\\'])}'])}"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# both values should evaluate to the same values
locals {
multi_function = substr(split("-", "us-west-2")[0], 0, 1)
multi_function_embedded = substr(split("-", "us-west-2")[0], 0, 1)
multi_function_embedded = "${substr(split("-", "us-west-2")[0], 0, 1)}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
nested_interpolation = "tags: ${join(";", ["names:${join(",", ["name:${example}"])}"])}"
}