Skip to content

Commit 9bdc0cb

Browse files
committed
Added support for provider functions
1 parent eb4e44f commit 9bdc0cb

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

hcl2/hcl2.lark

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ expr_term : "(" _NEW_LINE_OR_COMMENT* expression _NEW_LINE_OR_COMMENT? ")"
2727
| index_expr_term
2828
| get_attr_expr_term
2929
| identifier
30+
| provider_function_call
3031
| heredoc_template
3132
| heredoc_template_trim
3233
| attr_splat_expr_term
@@ -57,6 +58,8 @@ heredoc_template_trim : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)+
5758
function_call : identifier "(" _NEW_LINE_OR_COMMENT* arguments? _NEW_LINE_OR_COMMENT? ")"
5859
arguments : (expression (_NEW_LINE_OR_COMMENT* "," _NEW_LINE_OR_COMMENT* expression)* ("," | "...")? _NEW_LINE_OR_COMMENT*)
5960

61+
colons: "::"
62+
provider_function_call: identifier colons identifier colons identifier "(" _NEW_LINE_OR_COMMENT? arguments? _NEW_LINE_OR_COMMENT? ")"
6063
index_expr_term : expr_term index
6164
get_attr_expr_term : expr_term get_attr
6265
attr_splat_expr_term : expr_term attr_splat

hcl2/transformer.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Any, TYPE_CHECKING
77

88
from lark import Transformer, Token
9-
from lark.visitors import v_args
9+
from lark.visitors import v_args, Discard
1010

1111
if TYPE_CHECKING:
1212
from lark.tree import Meta
@@ -107,6 +107,14 @@ def function_call(self, args: list) -> str:
107107
def arguments(self, args: list) -> list:
108108
return args
109109

110+
def provider_function_call(self, args: list) -> str:
111+
args = self.strip_new_line_tokens(args)
112+
args_str = ""
113+
if len(args) > 5:
114+
args_str = ", ".join([str(arg) for arg in args[5] if arg is not Discard])
115+
provider_func = "::".join([args[0], args[2], args[4]])
116+
return f"{provider_func}({args_str})"
117+
110118
@v_args(meta=True)
111119
def block(self, meta: Meta, args: list) -> dict[str, Any]:
112120
args = self.strip_new_line_tokens(args)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"locals": [
3+
{
4+
"name2": ["${provider::test2::test(\"a\")}"],
5+
"name3": ["${test(\"a\")}"],
6+
"__end_line__": 4,
7+
"__start_line__": 1
8+
}
9+
]
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
locals {
2+
name2 = provider::test2::test("a")
3+
name3 = test("a")
4+
}

0 commit comments

Comments
 (0)