virtuald/pyhcl

Issue parsing nested parenthesis in hcl files

cesar-rodriguez opened this issue · 1 comments

Description

pyhcl is not able to handle parsing of hcl files containing nested parenthesis.

How to reproduce the issue

pyhcl: v0.4.4
Python: 3.7.5

$ cat test.tf 
locals {
  map = {
    r1 = "21.0.0.0/16"
    r2 = "22.4.0.0/16"
  }

  sg = {
    "test" = [
      "r1",
      "r2"
    ]
  }

}

resource "aws_security_group" "test" {
  name   = "testshubham"
  vpc_id = "vpc-xxxxxxxxx"

  dynamic "ingress" {
    for_each = local.sg.test
    content {
      from_port   = 80
      to_port     = 80
      protocol    = "tcp"
      cidr_blocks = split(",", lookup(local.map, ingress.value, ingress.value))
    }
  }
}

$ python
Python 3.7.5 (default, Jan  1 2020, 15:46:16) 
[Clang 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hcl
>>> with open('test.tf', 'r') as fp:
...   obj = hcl.load(fp)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/therasec/.pyenv/versions/terrascan/lib/python3.7/site-packages/hcl/api.py", line 62, in load
    return loads(fp.read(), export_comments=export_comments)
  File "/Users/therasec/.pyenv/versions/terrascan/lib/python3.7/site-packages/hcl/api.py", line 81, in loads
    return HclParser().parse(s, export_comments=export_comments)
  File "/Users/therasec/.pyenv/versions/terrascan/lib/python3.7/site-packages/hcl/parser.py", line 643, in parse
    s, lexer=Lexer(export_comments=export_comments), debug=DEBUG
  File "/Users/therasec/.pyenv/versions/terrascan/lib/python3.7/site-packages/hcl/ply/yacc.py", line 503, in parse
    tok = self.errorfunc(errtoken)
  File "/Users/therasec/.pyenv/versions/terrascan/lib/python3.7/site-packages/hcl/parser.py", line 634, in p_error
    raise ValueError(msg)
ValueError: Line 26, column 397: unexpected LEFTPAREN; expected ASTERISK_PERIOD, RIGHTBRACKET, COMMA, RIGHTPAREN, ADD, MINUS, MULTIPLY, DIVIDE

That appears to be HCL2 which is not supported by pyhcl. See the readme for alternative parsers.