Skip to content

fix: pad narrower alternatives in maybe_placeholders mode - #1648

Open
mmustafasenoglu wants to merge 1 commit into
lark-parser:masterfrom
mmustafasenoglu:fix/maybe-placeholders-padding
Open

fix: pad narrower alternatives in maybe_placeholders mode#1648
mmustafasenoglu wants to merge 1 commit into
lark-parser:masterfrom
mmustafasenoglu:fix/maybe-placeholders-padding

Conversation

@mmustafasenoglu

Copy link
Copy Markdown

Fixes #1629

When maybe_placeholders is enabled and an alternative within an optional group matches fewer symbols than the widest alternative in the same group, the children list is left short.

For example, with !start: "a" ["b" | "c" "d"]:

Lark('!start: "a" ["b" | "c" "d"]', maybe_placeholders=True).parse("ab")
# Before: Tree('start', ['a', 'b'])
# After:  Tree('start', ['a', 'b', None])

The absent case (parse("a")) already returned 3 elements (['a', None, None]) because empty_indices padded it correctly. However, matched narrower alternatives like parse("ab") were left short at 2 elements.

Root cause: Rules without empty_indices (representing matched alternatives) were not being padded to match the widest alternative's width.

Fix: In ParseTreeBuilder._init_builders, detect the target width from rules whose empty_indices contain trailing Nones (indicating an optional group was absent at the end). Then pad narrower alternatives that have no empty_indices with trailing Nones to match that target width.

All existing tests pass (Earley, LALR, standalone, etc.).

Co-authored-by: opencode opencode@opencode.ai

When maybe_placeholders is enabled and an alternative within an optional
group matches fewer symbols than the widest alternative in the same
group, the children list is left short. This fix detects the target width
from rules with empty_indices that have trailing Nones (indicating an
optional group was absent), and pads narrower alternatives without
empty_indices to match that width.

Fixes lark-parser#1629
Copilot AI lite review requested due to automatic review settings August 31, 2026 12:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants