diff --git a/hcl2/hcl2.lark b/hcl2/hcl2.lark index 0f5437fc..7a9ff6cb 100644 --- a/hcl2/hcl2.lark +++ b/hcl2/hcl2.lark @@ -37,6 +37,7 @@ DOUBLE_PIPE : "||" PLUS : "+" LPAR : "(" RPAR : ")" +COMMA : "," expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR | float_lit @@ -71,7 +72,7 @@ EXP_MARK : ("e" | "E") ("+" | "-")? DECIMAL+ EQ : /[ \t]*=(?!=|>)/ tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]" -object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem ",")) new_line_or_comment*)* "}" +object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem COMMA)) new_line_or_comment*)* "}" object_elem : object_elem_key ( EQ | ":") expression object_elem_key : float_lit | int_lit | identifier | STRING_LIT diff --git a/hcl2/transformer.py b/hcl2/transformer.py index 646dfc76..e2c5f8db 100644 --- a/hcl2/transformer.py +++ b/hcl2/transformer.py @@ -5,6 +5,7 @@ from collections import namedtuple from typing import List, Dict, Any +from lark import Token from lark.tree import Meta from lark.visitors import Transformer, Discard, _DiscardType, v_args @@ -111,6 +112,11 @@ def object(self, args: List) -> Dict: args = self.strip_new_line_tokens(args) result: Dict[str, Any] = {} for arg in args: + if ( + isinstance(arg, Token) and arg.type == "COMMA" + ): # skip optional comma at the end of object element + continue + result.update(arg) return result diff --git a/test/helpers/terraform-config/locals_embedded_condition.tf b/test/helpers/terraform-config/locals_embedded_condition.tf index 621adfcf..d705a30f 100644 --- a/test/helpers/terraform-config/locals_embedded_condition.tf +++ b/test/helpers/terraform-config/locals_embedded_condition.tf @@ -1,7 +1,7 @@ locals { terraform = { - channels = (local.running_in_ci ? local.ci_channels : local.local_channels), - authentication = [], - foo = null, + channels = (local.running_in_ci ? local.ci_channels : local.local_channels) + authentication = [] + foo = null } }