fix: pad narrower alternatives in maybe_placeholders mode - #1648
Open
mmustafasenoglu wants to merge 1 commit into
Open
fix: pad narrower alternatives in maybe_placeholders mode#1648mmustafasenoglu wants to merge 1 commit into
mmustafasenoglu wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1629
When
maybe_placeholdersis 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"]:The absent case (
parse("a")) already returned 3 elements (['a', None, None]) becauseempty_indicespadded it correctly. However, matched narrower alternatives likeparse("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 whoseempty_indicescontain trailing Nones (indicating an optional group was absent at the end). Then pad narrower alternatives that have noempty_indiceswith trailing Nones to match that target width.All existing tests pass (Earley, LALR, standalone, etc.).
Co-authored-by: opencode opencode@opencode.ai