Skip to content

"No terminal matches '|' in the current parser context" when parsing multi-line conditional #142

Closed

Description

@xRokco

I have the following Terraform local:

my_local = {
    for k, v in local.map_a : k => v
    if lookup(local.map_b[v.id], "enabled", false)
    || (
      contains(local.map_c, v.id)
      && contains(local.map_d, v.id)
    )
  }

It returns the following error when I attempt to parse the file:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/lark/lexer.py", line 590, in lex
    yield lexer.next_token(lexer_state, parser_state)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/lark/lexer.py", line 528, in next_token
    raise UnexpectedCharacters(lex_state.text, line_ctr.char_pos, line_ctr.line, line_ctr.column,
lark.exceptions.UnexpectedCharacters: No terminal matches '|' in the current parser context, at line 20 col 5
    || (
    ^
Expected one of:
    * /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc)/
    * BANG
    * /#.*\n/
    * COLON
    * RSQB
    * /[a-zA-Z_][a-zA-Z0-9_-]*/
    * LBRACE
    * STRING_LIT
    * RBRACE
    * DECIMAL
    * LSQB
    * /\n/
    * /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?(?P=heredoc_trim)/
    * MINUS
    * /\/\/.*\n/
    * LPAR
    * COMMA
    * RPAR
Previous tokens: Token('__ANON_0', '\n')

I can fix this by moving the entire local on to a single line, or adjusting it like this:

my_local = {
    for k, v in local.map_a : k => v
    if lookup(local.map_b[v.id], "enabled", false) || (
      contains(local.map_c, v.id) && (
      contains(local.map_d, v.id))
    )
  }

Seems like as long as I put || or && at the start or end of a line it produces this error.

Activity

  1. jskirde commented on Oct 30, 2024

    @jskirde

    Also having this problem where we split lines on "." accessor inside braces when writing hcl test conditions. The conditions are accessing deeply nested json documents. We can workaround by removing the line breaks but we end up with ridiculously long lines.

    assert {
        condition     = (
          jsondecode(bigip_as3.application.as3_json)
          .declaration
          .partition
          .application
          .object
          .object_attribute == "attribute_value"
        )
        error_message = ""
      }
    
  2. kkozik-amplify commented on Apr 7, 2026

    @kkozik-amplify
    Collaborator
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