Skip to content

Preserve literals of e-notation floats in parsing and reconstruction #226

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
Apr 18, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Issue parsing ellipsis in a separate line within `for` expression ([#221](https://github.com/amplify-education/python-hcl2/pull/221))
- Issue parsing inline expression as an object key; **see Limitations in README.md** ([#222](https://github.com/amplify-education/python-hcl2/pull/222))
- Preserve literals of e-notation floats in parsing and reconstruction. Thanks, @eranor ([#226](https://github.com/amplify-education/python-hcl2/pull/226))

## \[7.1.0\] - 2025-04-10

Expand Down
39 changes: 34 additions & 5 deletions hcl2/reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def _should_add_space(self, rule, current_terminal):
Terminal("STRING_LIT"),
Terminal("DECIMAL"),
Terminal("NAME"),
Terminal("NEGATIVE_DECIMAL"),
]:
return True

Expand Down Expand Up @@ -412,10 +413,7 @@ def _newline(self, level: int, count: int = 1) -> Tree:
def _is_block(self, value: Any) -> bool:
if isinstance(value, dict):
block_body = value
if (
START_LINE_KEY in block_body.keys()
or END_LINE_KEY in block_body.keys()
):
if START_LINE_KEY in block_body.keys() or END_LINE_KEY in block_body.keys():
return True

try:
Expand Down Expand Up @@ -520,7 +518,7 @@ def _transform_dict_to_body(self, hcl_dict: dict, level: int) -> Tree:

return Tree(Token("RULE", "body"), children)

# pylint: disable=too-many-branches, too-many-return-statements
# pylint: disable=too-many-branches, too-many-return-statements too-many-statements
def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]:
"""Transforms a value from a dictionary into an "expr_term" (a value in HCL2)

Expand Down Expand Up @@ -611,6 +609,37 @@ def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]:
],
)

if isinstance(value, float):
value = str(value)
literal = []

if value[0] == "-":
# pop two first chars - minus and a digit
literal.append(Token("NEGATIVE_DECIMAL", value[:2]))
value = value[2:]

while value != "":
char = value[0]

if char == ".":
# current char marks beginning of decimal part: pop all remaining chars and end the loop
literal.append(Token("DOT", char))
literal.extend(Token("DECIMAL", char) for char in value[1:])
break

if char == "e":
# current char marks beginning of e-notation: pop all remaining chars and end the loop
literal.append(Token("EXP_MARK", value))
break

literal.append(Token("DECIMAL", char))
value = value[1:]

return Tree(
Token("RULE", "expr_term"),
[Tree(Token("RULE", "float_lit"), literal)],
)

# store strings as single literals
if isinstance(value, str):
# potentially unpack a complex syntax structure
Expand Down
37 changes: 26 additions & 11 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def __init__(self, with_meta: bool = False):
super().__init__()

def float_lit(self, args: List) -> float:
return float("".join([self.to_tf_inline(arg) for arg in args]))
value = "".join([self.to_tf_inline(arg) for arg in args])
if "e" in value:
return self.to_string_dollar(value)
return float(value)

def int_lit(self, args: List) -> int:
return int("".join([self.to_tf_inline(arg) for arg in args]))
Expand Down Expand Up @@ -177,7 +180,9 @@ def conditional(self, args: List) -> str:
return f"{args[0]} ? {args[1]} : {args[2]}"

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

def unary_op(self, args: List) -> str:
args = self.process_nulls(args)
Expand Down Expand Up @@ -304,21 +309,31 @@ def strip_new_line_tokens(self, args: List) -> List:
"""
return [arg for arg in args if arg != "\n" and arg is not Discard]

def is_string_dollar(self, value: str) -> bool:
if not isinstance(value, str):
return False
return value.startswith("${") and value.endswith("}")

def to_string_dollar(self, value: Any) -> Any:
"""Wrap a string in ${ and }"""
if isinstance(value, str):
if not isinstance(value, str):
return value
# if it's already wrapped, pass it unmodified
if value.startswith("${") and value.endswith("}"):
return value
if self.is_string_dollar(value):
return value

if value.startswith('"') and value.endswith('"'):
value = str(value)[1:-1]
return self.process_escape_sequences(value)
if value.startswith('"') and value.endswith('"'):
value = str(value)[1:-1]
return self.process_escape_sequences(value)

if self.is_type_keyword(value):
return value

if self.is_type_keyword(value):
return value
return f"${{{value}}}"

return f"${{{value}}}"
def unwrap_string_dollar(self, value: str):
if self.is_string_dollar(value):
return value[2:-1]
return value

def strip_quotes(self, value: Any) -> Any:
Expand Down
30 changes: 30 additions & 0 deletions test/helpers/terraform-config-json/test_floats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"locals": [
{
"simple_float": 123.456,
"small_float": 0.123,
"large_float": 9876543.21,
"negative_float": -42.5,
"negative_small": -0.001,
"scientific_positive": "${1.23e5}",
"scientific_negative": "${9.87e-3}",
"scientific_large": "${6.022e+23}",
"integer_as_float": 100.0,
"float_calculation": "${105e+2 * 3.0 / 2.1}",
"float_comparison": "${5e1 > 2.3 ? 1.0 : 0.0}",
"float_list": [
1.1,
2.2,
3.3,
-4.4,
"${5.5e2}"
],
"float_object": {
"pi": 3.14159,
"euler": 2.71828,
"sqrt2": 1.41421,
"scientific": "${-123e+2}"
}
}
]
}
20 changes: 20 additions & 0 deletions test/helpers/terraform-config/test_floats.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
locals {
simple_float = 123.456
small_float = 0.123
large_float = 9876543.21
negative_float = -42.5
negative_small = -0.001
scientific_positive = 1.23e5
scientific_negative = 9.87e-3
scientific_large = 6.022e+23
integer_as_float = 100.0
float_calculation = 105e+2 * 3.0 / 2.1
float_comparison = 5e1 > 2.3 ? 1.0 : 0.0
float_list = [1.1, 2.2, 3.3, -4.4, 5.5e2]
float_object = {
pi = 3.14159
euler = 2.71828
sqrt2 = 1.41421
scientific = -123e+2
}
}
12 changes: 6 additions & 6 deletions test/unit/test_hcl2_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ def test_index(self):

def test_e_notation(self):
literals = {
"var = 3e4": {"var": 30000.0},
"var = 3.5e5": {"var": 350000.0},
"var = -3e6": {"var": -3e6},
"var = -2.3e4": {"var": -2.3e4},
"var = -5e-2": {"var": -5e-2},
"var = -6.1e-3": {"var": -6.1e-3},
"var = 3e4": {"var": "${3e4}"},
"var = 3.5e5": {"var": "${3.5e5}"},
"var = -3e6": {"var": "${-3e6}"},
"var = -2.3e4": {"var": "${-2.3e4}"},
"var = -5e-2": {"var": "${-5e-2}"},
"var = -6.1e-3": {"var": "${-6.1e-3}"},
}
for actual, expected in literals.items():
result = self.load_to_dict(actual)
Expand Down