Skip to content

Two ternary operators - fail to parse #140

Closed

Description

@jeremycarroll

I had a parse error that I reduced to

resource "aws_instance" "jenkins_controller" {
  user_data = var.color == "blue" ? 0 :  1
  tags = {
    email          = var.assignee == "" ? null : var.assignee
    color          = var.color
  }
}

results in

Traceback (most recent call last):
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 126, in feed_token
    action, arg = states[state][token.type]
                  ~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: '__ANON_3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/hcl2/api.py", line 14, in load
    return loads(file.read(), with_meta=with_meta)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/hcl2/api.py", line 27, in loads
    tree = hcl2.parse(text + "\n")
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/lark.py", line 645, in parse
    return self.parser.parse(text, start=start, on_error=on_error)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/parser_frontends.py", line 96, in parse
    return self.parser.parse(stream, chosen_start, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 41, in parse
    return self.parser.parse(lexer, start)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 171, in parse
    return self.parse_from_state(parser_state)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 193, in parse_from_state
    raise e
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 184, in parse_from_state
    state.feed_token(token)
  File "/Users/jcarroll/.local/share/virtualenvs/codeowners-dsyKmJWy/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 129, in feed_token
    raise UnexpectedToken(token, expected, state=self, interactive_parser=None)
lark.exceptions.UnexpectedToken: Unexpected token Token('__ANON_3', 'color') at line 5, column 5.
Expected one of: 
	* SLASH
	* QMARK
	* __ANON_4
	* __ANON_0
	* STAR
	* MINUS
	* PLUS
	* COMMA
	* __ANON_8
	* __ANON_9
	* __ANON_5
	* __ANON_2
	* __ANON_7
	* PERCENT
	* MORETHAN
	* RBRACE
	* __ANON_6
	* LESSTHAN
	* __ANON_1

Python 3.11.3; HCL2: 4.3.2

Activity

  1. IButskhrikidze commented on Jun 20, 2023

    @IButskhrikidze
    Contributor

    If you make parentheses for the entire ternary or for only a condition, it works.
    like those:

    resource "aws_instance" "jenkins_controller" {
      user_data = var.color == "blue" ? 0 :  1
      tags = {
        email          = (var.assignee == "" ? null : var.assignee)
        color          = var.color
      }
    }
    

    or

    resource "aws_instance" "jenkins_controller" {
      user_data = var.color == "blue" ? 0 :  1
      tags = {
        email          = (var.assignee == "") ? null : var.assignee
        color          = var.color
      }
    }
    
  2. jeremycarroll commented on Jun 28, 2023

    @jeremycarroll
    Author

    @IButskhrikidze - I am trying to parse all the code in a repo, and it is what it is ... The parser is very close, but even one or two errors cause pain

  3. gimbo commented on Jul 14, 2023

    @gimbo

    For me this bug appeared in 4.3.1; downgrading to 4.3.0 fixed it.

  4. Garamotte commented on Sep 15, 2023

    @Garamotte

    I have the same issue with 4.3.1 for a binary operation : priority = 20 + value.

    When downgrading to 4.3.0, everything is OK.

  5. dogmatic69 commented on Nov 5, 2023

    @dogmatic69

    also broken for multi-line ternary even if wrapped in ().

    (var.foo ? 1 : 2) works

    this does not

    (
      var.foo
      ? 1
      : 2
    )
    
  6. kkozik-amplify commented on Mar 27, 2025

    @kkozik-amplify
    Collaborator

    Fixed in version 7.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions