amplify-education / python-hcl2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected token exception on empty heredoc

KetchupBomb opened this issue · comments

File content for foo.tf:

foo = <<EOF
Hello world
EOF

File content for bar.tf:

bar = <<EOF
EOF

Parsing with both hcl and hcl2:

>>> import hcl as hcl1
>>> import hcl2

>>> hcl1.load(open("foo.tf"))
{'foo': 'Hello world'}

>>> hcl1.load(open("bar.tf"))
{'bar': ''}

>>> hcl2.load(open("foo.tf"))
{'foo': 'Hello world'}

>>> hcl2.load(open("bar.tf"))
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 59, in get_action
    return states[state][token.type]
KeyError: 'LESSTHAN'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/hcl2/api.py", line 9, in load
    return loads(file.read())
  File "/usr/local/lib/python3.9/site-packages/hcl2/api.py", line 18, in loads
    return hcl2.parse(text + "\n")
  File "/usr/local/lib/python3.9/site-packages/lark/lark.py", line 464, in parse
    return self.parser.parse(text, start=start)
  File "/usr/local/lib/python3.9/site-packages/lark/parser_frontends.py", line 115, in parse
    return self._parse(token_stream, start)
  File "/usr/local/lib/python3.9/site-packages/lark/parser_frontends.py", line 63, in _parse
    return self.parser.parse(input, start, *args)
  File "/usr/local/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 35, in parse
    return self.parser.parse(*args)
  File "/usr/local/lib/python3.9/site-packages/lark/parsers/lalr_parser.py", line 88, in parse
    action, arg = get_action(token)
  File "/usr/local/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('LESSTHAN', '<') at line 1, column 7.
Expected one of: 
        * LSQB
        * BANG
        * MINUS
        * __ANON_3
        * LBRACE
        * LPAR
        * __ANON_11
        * STRING_LIT
        * DECIMAL
        * __ANON_10

This seems like valid HCL, both in theory and in practice with HCL1, so it appears to be an HCL2 (only) bug.