amplify-education / python-hcl2

MIT License
240 stars 53 forks source link

Unexpected tokens on cross line statements #16

Closed SumGR closed 4 years ago

SumGR commented 4 years ago

I have the following block:

locals {
    route53_forwarding_rule_shares = {
        for forwarding_rule_key in keys(var.route53_resolver_forwarding_rule_shares) :
            "${forwarding_rule_key}" => {
                aws_account_ids = [
                    for account_name in var.route53_resolver_forwarding_rule_shares[
                        forwarding_rule_key
                    ].aws_account_names:
                        module.remote_state_subaccounts.map[account_name].outputs["aws_account_id"]
                ]
            }
    }
}

and get the following stack trace:

Error parsing /home/****/terraform-aws-jenkins/.terraform/modules/platform_remote_state/route53.tf
Traceback (most recent call last):
  File "/home/***/.local/lib64/python3.6/site-packages/lark/parsers/lalr_parser.py", line 62, in get_action
    return states[state][token.type]
KeyError: '__ANON_0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/****/builder/gtflinter/__main__.py", line 67, in <module>
    main()
  File "/home/****/.local/lib64/python3.6/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/****/.local/lib64/python3.6/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/****/.local/lib64/python3.6/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/****/.local/lib64/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/****/ratespace/devops-builder-image/builder/gtflinter/__main__.py", line 51, in main
    results = remote_source_check(scan_directory)
  File "/home/****/ratespace/devops-builder-image/builder/gtflinter/tfchecks.py", line 27, in remote_source_check
    raise e
  File "/home/****/ratespace/devops-builder-image/builder/gtflinter/tfchecks.py", line 24, in remote_source_check
    cur_hcl = hcl2.load(fd)
  File "/home/****/.local/lib64/python3.6/site-packages/hcl2/api.py", line 9, in load
    return loads(file.read())
  File "/home/****/.local/lib64/python3.6/site-packages/hcl2/api.py", line 18, in loads
    return hcl2.parse(text + "\n")
  File "/home/****/.local/lib64/python3.6/site-packages/lark/lark.py", line 311, in parse
    return self.parser.parse(text, start=start)
  File "/home/****/.local/lib64/python3.6/site-packages/lark/parser_frontends.py", line 89, in parse
    return self._parse(token_stream, start, *[sps] if sps is not NotImplemented else [])
  File "/home/****/.local/lib64/python3.6/site-packages/lark/parser_frontends.py", line 54, in _parse
    return self.parser.parse(input, start, *args)
  File "/home/****/.local/lib64/python3.6/site-packages/lark/parsers/lalr_parser.py", line 36, in parse
    return self.parser.parse(*args)
  File "/home/****/.local/lib64/python3.6/site-packages/lark/parsers/lalr_parser.py", line 86, in parse
    action, arg = get_action(token)
  File "/home/****/.local/lib64/python3.6/site-packages/lark/parsers/lalr_parser.py", line 65, in get_action
    raise UnexpectedToken(token, expected, state=state)
lark.exceptions.UnexpectedToken: Unexpected token Token(__ANON_0, '\n') at line 50, column 85.
Expected one of: 
    * LBRACE
    * LSQB
    * NULL
    * IDENTIFIER
    * TRUE
    * MINUS
    * STRING_LIT
    * __ANON_9
    * DECIMAL
    * LPAR
    * BANG
    * STAR
    * __ANON_10
    * FALSE

In this case, line 50 is for account_name in var.route53_resolver_forwarding_rule_shares[

I think it's having trouble with the arrays split across lines. If I place that entire statement on line line, the parse error goes away. This is valid tf file, at lest with terraform v0.12.18.

SumGR commented 4 years ago

I found another instance of this error in one of our locals.tf file:

locals {
  /**
   * Endpoint mode
   */
  has_valid_forwarding_rules_template_inputs = (
    length(keys(var.forwarding_rules_template.copy_resolver_rules)) > 0 &&
    length(var.forwarding_rules_template.replace_with_target_ips) > 0 &&
    length(var.forwarding_rules_template.exclude_cidrs) > 0
  )

I get the following:

lark.exceptions.UnexpectedToken: Unexpected token Token(__ANON_0, '\n') at line 5, column 49.
Expected one of: 

with the parse error being at the end of has_valid_forwarding_rules_template_inputs = (

The parser has trouble with those statements within the = () being stretched over multiple lines. I assume it's the same issue as the other one above?

aoskotsky-amplify commented 4 years ago

Good catch. This PR should take care of it. Let me know if it fixes it for you https://github.com/amplify-education/python-hcl2/pull/24

SumGR commented 4 years ago

Sorry for the late reply. Yes this fixed our issue. I bumped our project to use python-hcl2==0.2.5 and it works great. Thanks!