amplify-education / python-hcl2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing issue with nested splat operators

josh-barker opened this issue · comments

Hi,

I've found an issue when parsing the below.

main.tf

variable "something" {
  description = "some object variable"

  type = list(object({
    id = string
    nested = list(object({
      id = string
    }))
  }))
  default = []

  validation {
    condition     = !contains([for v in flatten(var.something[*].nested[*].id) : can(regex("^blah-$", v))], false)
    error_message = "The property `nested.id` must be start with blah-."
  }
}

main.py

import hcl2
with open('main.tf', 'r') as file:
    dict = hcl2.load(file)

print(dict)
python main.py
Traceback (most recent call last):
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 59, in get_action
    return states[state][token.type]
KeyError: 'STAR'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/josh/github/bridgecrew.io/replication-cases/nested-variable-validation/main.py", line 3, in <module>
    dict = hcl2.load(file)
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/hcl2/api.py", line 9, in load
    return loads(file.read())
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/hcl2/api.py", line 18, in loads
    return hcl2.parse(text + "\n")
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/lark/lark.py", line 464, in parse
    return self.parser.parse(text, start=start)
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/lark/parser_frontends.py", line 115, in parse
    return self._parse(token_stream, start)
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/lark/parser_frontends.py", line 63, in _parse
    return self.parser.parse(input, start, *args)
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 35, in parse
    return self.parser.parse(*args)
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 88, in parse
    action, arg = get_action(token)
  File "/Users/josh/.local/share/virtualenvs/empty-locals-s2Z1l-Fh/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 66, in get_action
    raise UnexpectedToken(token, expected, state=state, puppet=puppet)
lark.exceptions.UnexpectedToken: Unexpected token Token('STAR', '*') at line 13, column 73.
Expected one of:
	* NULL
	* __ANON_10
	* DECIMAL
	* __ANON_3
	* __ANON_2
	* __ANON_1
	* FALSE
	* BANG
	* STRING_LIT
	* MINUS
	* LPAR
	* TRUE
	* LSQB
	* __ANON_11
	* __ANON_0
	* LBRACE

Closing since the fix PR got merged. Let me know if you still have any issues with this