Skip to content

Commit fa8953b

Browse files
fix e-notation and negative numbers literals; add unit test for e-notation (#182)
1 parent ff06e94 commit fa8953b

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

hcl2/hcl2.lark

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/ // any character except '"'
6161
string_with_interpolation: "\"" (STRING_CHARS)* interpolation_maybe_nested (STRING_CHARS | interpolation_maybe_nested)* "\""
6262
interpolation_maybe_nested: "${" expression "}"
6363

64-
int_lit : DECIMAL+
65-
!float_lit: DECIMAL+ "." DECIMAL+ (EXP_MARK DECIMAL+)?
66-
| DECIMAL+ ("." DECIMAL+)? EXP_MARK DECIMAL+
64+
int_lit : NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+
65+
!float_lit: (NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+) "." DECIMAL+ (EXP_MARK)?
66+
| (NEGATIVE_DECIMAL? DECIMAL+ | NEGATIVE_DECIMAL+) ("." DECIMAL+)? (EXP_MARK)
67+
NEGATIVE_DECIMAL : "-" DECIMAL
6768
DECIMAL : "0".."9"
68-
EXP_MARK : ("e" | "E") ("+" | "-")?
69+
EXP_MARK : ("e" | "E") ("+" | "-")? DECIMAL+
6970
EQ : /[ \t]*=(?!=|>)/
7071

7172
tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"

test/unit/test_hcl2_syntax.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_block_multiple_labels(self):
7272

7373
def test_unary_operation(self):
7474
operations = [
75-
("identifier = -10", {"identifier": "${-10}"}),
75+
("identifier = -10", {"identifier": -10}),
7676
("identifier = !true", {"identifier": "${!true}"}),
7777
]
7878
for hcl, dict_ in operations:
@@ -151,3 +151,16 @@ def test_index(self):
151151
for call, expected in indexes.items():
152152
result = self.load_to_dict(call)
153153
self.assertDictEqual(result, expected)
154+
155+
def test_e_notation(self):
156+
literals = {
157+
"var = 3e4": {"var": 30000.0},
158+
"var = 3.5e5": {"var": 350000.0},
159+
"var = -3e6": {"var": -3e6},
160+
"var = -2.3e4": {"var": -2.3e4},
161+
"var = -5e-2": {"var": -5e-2},
162+
"var = -6.1e-3": {"var": -6.1e-3},
163+
}
164+
for actual, expected in literals.items():
165+
result = self.load_to_dict(actual)
166+
self.assertDictEqual(result, expected)

0 commit comments

Comments
 (0)