From e2bc0b755562ef88ea55e9f78216c5dd1d31780b Mon Sep 17 00:00:00 2001 From: Lars Gohlke Date: Thu, 6 Jun 2024 20:10:13 +0200 Subject: [PATCH 1/2] adds support for provider functions --- hcl2/hcl2.lark | 8 +++++--- hcl2/transformer.py | 8 ++++++++ test/helpers/terraform-config-json/provider_function.json | 8 ++++++++ test/helpers/terraform-config/provider_function.tf | 4 ++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/helpers/terraform-config-json/provider_function.json create mode 100644 test/helpers/terraform-config/provider_function.tf diff --git a/hcl2/hcl2.lark b/hcl2/hcl2.lark index b9e453c7..7751f1a4 100644 --- a/hcl2/hcl2.lark +++ b/hcl2/hcl2.lark @@ -26,7 +26,8 @@ expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")" | function_call | index_expr_term | get_attr_expr_term - | identifier + | identifier + | provider_function_call | heredoc_template | heredoc_template_trim | attr_splat_expr_term @@ -34,7 +35,6 @@ 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 : "${" /[^}]+/ "}" @@ -53,7 +53,9 @@ object_elem : (identifier | expression) ("=" | ":") expression heredoc_template : /<<(?P[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc)/ heredoc_template_trim : /<<-(?P[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc_trim)/ -function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")" +colons: "::" +provider_function_call: identifier colons identifier colons identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")" +function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")" arguments : (expression (new_line_or_comment* "," new_line_or_comment* expression)* ("," | "...")? new_line_or_comment*) index_expr_term : expr_term index diff --git a/hcl2/transformer.py b/hcl2/transformer.py index 6f2d4f31..998a9dd1 100644 --- a/hcl2/transformer.py +++ b/hcl2/transformer.py @@ -111,6 +111,14 @@ def function_call(self, args: List) -> str: args_str = ", ".join([str(arg) for arg in args[1] if arg is not Discard]) return f"{args[0]}({args_str})" + def provider_function_call(self, args: List) -> str: + args = self.strip_new_line_tokens(args) + args_str = "" + if len(args) > 5: + args_str = ", ".join([str(arg) for arg in args[5] if arg is not Discard]) + provider_func = "::".join([args[0],args[2],args[4]]) + return f"{provider_func}({args_str})" + def arguments(self, args: List) -> List: return args diff --git a/test/helpers/terraform-config-json/provider_function.json b/test/helpers/terraform-config-json/provider_function.json new file mode 100644 index 00000000..2b749c13 --- /dev/null +++ b/test/helpers/terraform-config-json/provider_function.json @@ -0,0 +1,8 @@ +{ + "locals": [ + { + "name2": "${provider::test2::test(\"a\")}", + "name3": "${test(\"a\")}" + } + ] +} diff --git a/test/helpers/terraform-config/provider_function.tf b/test/helpers/terraform-config/provider_function.tf new file mode 100644 index 00000000..feaca9b7 --- /dev/null +++ b/test/helpers/terraform-config/provider_function.tf @@ -0,0 +1,4 @@ +locals { + name2 = provider::test2::test("a") + name3 = test("a") +} From bf030298fbd8053d6e1f849c16442b1bafbd5e8a Mon Sep 17 00:00:00 2001 From: Lars Gohlke Date: Fri, 7 Jun 2024 09:11:56 +0200 Subject: [PATCH 2/2] cleanup for pretty diff --- hcl2/hcl2.lark | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hcl2/hcl2.lark b/hcl2/hcl2.lark index 7751f1a4..cee792e8 100644 --- a/hcl2/hcl2.lark +++ b/hcl2/hcl2.lark @@ -26,7 +26,7 @@ expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")" | function_call | index_expr_term | get_attr_expr_term - | identifier + | identifier | provider_function_call | heredoc_template | heredoc_template_trim @@ -35,6 +35,7 @@ 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 : "${" /[^}]+/ "}" @@ -53,10 +54,10 @@ object_elem : (identifier | expression) ("=" | ":") expression heredoc_template : /<<(?P[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc)/ heredoc_template_trim : /<<-(?P[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc_trim)/ -colons: "::" -provider_function_call: identifier colons identifier colons identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")" -function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")" +function_call : identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")" arguments : (expression (new_line_or_comment* "," new_line_or_comment* expression)* ("," | "...")? new_line_or_comment*) +colons: "::" +provider_function_call: identifier colons identifier colons identifier "(" new_line_or_comment? arguments? new_line_or_comment? ")" index_expr_term : expr_term index get_attr_expr_term : expr_term get_attr