Skip to content
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

Wrong Arithmetic Parse #70

Open
soasme opened this issue Jun 15, 2021 · 1 comment
Open

Wrong Arithmetic Parse #70

soasme opened this issue Jun 15, 2021 · 1 comment

Comments

@soasme
Copy link

soasme commented Jun 15, 2021

Issue

>>> from hcl2 import loads
>>> loads('a=1*(2+3)')
{'a': ['${1 * 2 + 3}']}

Expect

{'a': ['${1*(2+3)}']}
@jmunar
Copy link

jmunar commented Mar 30, 2023

In case it helps somebody, I fixed this issue by adding the parenthesis to the transformer, whenever an expression is found:

from hcl2.parser import hcl2 as parser
from hcl2.transformer import DictTransformer

class DictTransformerMod(DictTransformer):

    def expr_term(self, args: list) -> object:
        term = super().expr_term(args)
        if isinstance(term, str) and " " in term:
            return f"({term})"
        return term

transformer = DictTransformerMod()

tree = parser.parse('a=1*(2+3)')
transformer.transform(tree)

, which returns the expected:

{'a': '${1 * (2 + 3)}'}

I won't do a PR because I know too little about how this package is used to understand possible side effects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants