Skip to content

Single-character heredoc delimiters fail to parse (grammar requires 2+ characters) #314

Closed

Description

@kkozik-amplify

Describe the bug

A heredoc whose delimiter is a single character fails to parse. The HEREDOC_TEMPLATE terminal requires two or more characters, so <<E is rejected while <<EO is accepted.

The HCL specification defines the heredoc delimiter as an Identifier:

heredocTemplate = (
    ("<<" | "<<-") Identifier Newline
    (content as defined in prose above)
    Identifier Newline
);

Identifier = ID_Start (ID_Continue | '-')*;

The * permits a single character, so a one-character delimiter is valid HCL.

Software:

  • python-hcl2: main (1432aea), and identical behaviour in 7.x — see below
  • Python 3.9, lark 1.3.1

Snippet of HCL2 code causing the unexpected behaviour:

a = <<E
x
E

Also affects the trimmed form:

a = <<-E
  x
  E

Expected behavior

Parses like any other heredoc, i.e. equivalent to a = <<EO\nx\nEO\n, which works today.

Actual behavior

lark.exceptions.UnexpectedToken: Unexpected token Token('STRING_CHARS', '<<E\nx\nE\n\n') at line 1, column 5.

Delimiter length is the only variable:

Source Result
a = <<E\nx\nE\n raises
a = <<-E\n x\n E\n raises
a = <<EO\nx\nEO\n {'a': '"<<EO\nx\nEO"'}
a = <<EOF\nx\nEOF\n {'a': '"<<EOF\nx\nEOF"'}

Root cause

hcl2/hcl2.lark — the delimiter group is [a-zA-Z][a-zA-Z0-9._-]+, where the + requires at least one character after the first:

HEREDOC_TEMPLATE : /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?\n\s*(?P=heredoc)\n/
HEREDOC_TEMPLATE_TRIM : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?\n\s*(?P=heredoc_trim)\n/

Changing + to * would admit single-character delimiters. I have not tested whether that introduces any lexing ambiguity, so it is a suggestion rather than a verified fix.

Not a regression. The same [a-zA-Z][a-zA-Z0-9._-]+ pattern is present pre-8.x (git show 21b7cf8^:hcl2/hcl2.lark), so this is a long-standing limitation rather than something the v8 rewrite introduced. No single-character delimiter appears anywhere in the current test suite.

Impact

Low in practice — real configurations overwhelmingly use EOF, EOT, JSON, POLICY and similar. It matters for generated or minified HCL, and for anything round-tripping documents produced by other HCL implementations. The failure is loud, so there is no silent data loss.

Workaround

Use a delimiter of two or more characters.


Found while reviewing #309 (empty heredoc), which touches the same two terminals. Filing separately because it is a distinct, pre-existing limitation and out of scope for the fix in #312.

Activity

  1. added
    bug
    grammar
    on Aug 24, 2026
  2. added
    unreleased
    on Aug 26, 2026
  3. kkozik-amplify commented on Aug 26, 2026

    @kkozik-amplify
    CollaboratorAuthor

    Released in v8.1.3, now on PyPI — pip install python-hcl2==8.1.3. Fixed by #323.

  4. removed
    unreleased
    on Aug 26, 2026
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

    buggrammar

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions