Skip to content

fix e-notation and negative numbers literals #182

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
Jan 20, 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
9 changes: 5 additions & 4 deletions hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -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* "]"
Expand Down
15 changes: 14 additions & 1 deletion test/unit/test_hcl2_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Loading