From 669388d85cf3d890fbb9f11598ac75074f7491a3 Mon Sep 17 00:00:00 2001 From: Kamil Kozik Date: Fri, 17 Jan 2025 12:18:54 +0100 Subject: [PATCH] fix e-notation and negative numbers literals; add unit test for e-notation --- hcl2/hcl2.lark | 9 +++++---- test/unit/test_hcl2_syntax.py | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/hcl2/hcl2.lark b/hcl2/hcl2.lark index a1e7f26a..eef3e0c0 100644 --- a/hcl2/hcl2.lark +++ b/hcl2/hcl2.lark @@ -61,11 +61,12 @@ STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/ // any character except '"' string_with_interpolation: "\"" (STRING_CHARS)* interpolation_maybe_nested (STRING_CHARS | interpolation_maybe_nested)* "\"" interpolation_maybe_nested: "${" expression "}" -int_lit : DECIMAL+ -!float_lit: DECIMAL+ "." DECIMAL+ (EXP_MARK DECIMAL+)? - | DECIMAL+ ("." DECIMAL+)? EXP_MARK DECIMAL+ +int_lit : NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+ +!float_lit: (NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+) "." DECIMAL+ (EXP_MARK)? + | (NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+) ("." DECIMAL+)? (EXP_MARK) +NEGATIVE_DECIMAL : "-" DECIMAL DECIMAL : "0".."9" -EXP_MARK : ("e" | "E") ("+" | "-")? +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* "]" diff --git a/test/unit/test_hcl2_syntax.py b/test/unit/test_hcl2_syntax.py index 50b44997..132291b2 100644 --- a/test/unit/test_hcl2_syntax.py +++ b/test/unit/test_hcl2_syntax.py @@ -72,7 +72,7 @@ def test_block_multiple_labels(self): def test_unary_operation(self): operations = [ - ("identifier = -10", {"identifier": "${-10}"}), + ("identifier = -10", {"identifier": -10}), ("identifier = !true", {"identifier": "${!true}"}), ] for hcl, dict_ in operations: @@ -151,3 +151,16 @@ def test_index(self): for call, expected in indexes.items(): result = self.load_to_dict(call) self.assertDictEqual(result, expected) + + 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}, + } + for actual, expected in literals.items(): + result = self.load_to_dict(actual) + self.assertDictEqual(result, expected)