Skip to content

revert interpolation grammar to v5.1.1 #200

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

Merged
merged 1 commit into from
Mar 27, 2025
Merged
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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ To see all the available options, run `tox -l`.

## Releasing

To create a new releaes go to Releases page, press 'Draft a new release', create a tag
To create a new release go to Releases page, press 'Draft a new release', create a tag
with a version you want to be released, fill the release notes and press 'Publish release'.
Github actions will take care of publishing it to PyPi.

Expand All @@ -84,3 +84,23 @@ We welcome pull requests! For your pull request to be accepted smoothly, we sugg
- Create a pull request. Explain why you want to make the change and what it’s for.

We’ll try to answer any PR’s promptly.

## Limitations

### Error parsing string interpolations nested more than 2 times

- Parsing following example is expected to throw out an exception and fail:
```terraform
locals {
foo = "foo"
name = "prefix1-${"prefix2-${"${local.foo}-bar"}"}" //should interpolate into "prefix1-prefix2-foo-bar" but fails
}
```
We recommend working around this by modifying the configuration in the following manner:
```terraform
locals {
foo = "foo"
foo_bar = "${local.foo}-bar"
name = "prefix1-${"prefix2-${local.foo_bar}"}" //interpolates into "prefix1-prefix2-foo-bar"
}
```
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 | string_with_interpolation)* new_line_or_comment? "{" body "}"
block : identifier (identifier | STRING_LIT)* new_line_or_comment? "{" body "}"
new_line_or_comment: ( NL_OR_COMMENT )+
NL_OR_COMMENT: /\n[ \t]*/ | /#.*\n/ | /\/\/.*\n/ | /\/\*(.|\n)*?(\*\/)/

Expand Down Expand Up @@ -42,7 +42,6 @@ expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
| float_lit
| int_lit
| STRING_LIT
| string_with_interpolation
| tuple
| object
| function_call
Expand All @@ -57,10 +56,11 @@ expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
| for_tuple_expr
| for_object_expr

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


int_lit : NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+
!float_lit: (NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+) "." DECIMAL+ (EXP_MARK)?
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +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"}]}
7 changes: 0 additions & 7 deletions test/helpers/terraform-config/multi_level_interpolation.tf

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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"
}
14 changes: 8 additions & 6 deletions test/unit/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ def test_locals_embedded_function_tf(self):
def test_locals_embedded_interpolation_tf(self):
builder = hcl2.Builder()

embedded_interpolation = (
"(long substring without interpolation); ${module.special_constants.aws_accounts"
'["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo'
)
attributes = {
"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",
}

builder.block("locals", embedded_interpolation=embedded_interpolation)
builder.block("locals", **attributes)

self.compare_filenames(builder, "locals_embedded_interpolation.tf")
self.compare_filenames(builder, "string_interpolations.tf")

def test_provider_function_tf(self):
builder = hcl2.Builder()
Expand Down