Skip to content

parser: avoid exponential backtracking for parenthesized expressions - #1401

Open
Riley-Kilgore wants to merge 1 commit into
mainfrom
fix-1377
Open

parser: avoid exponential backtracking for parenthesized expressions#1401
Riley-Kilgore wants to merge 1 commit into
mainfrom
fix-1377

Conversation

@Riley-Kilgore

Copy link
Copy Markdown
Member

Left-factor the shared '(' prefix of tuples and grouped expressions so the first interior expression is parsed exactly once, instead of being fully parsed by the tuple alternative, discarded, and re-parsed as a group. Nested grouping previously cost O(2^depth); it is now linear.

Preserves tuple, trailing-comma, grouping, parenthesized sequence/assignment, and NewLineLeftParen semantics, along with source spans. Adds depth-based regression tests (16–128 levels).

Closes #1377

@Riley-Kilgore
Riley-Kilgore marked this pull request as ready for review August 10, 2026 23:22
@Riley-Kilgore
Riley-Kilgore requested a review from a team as a code owner August 10, 2026 23:22
Left-factor the shared '(' prefix of tuples and grouped expressions so
the first interior expression is parsed exactly once, instead of being
fully parsed by the tuple alternative, discarded, and re-parsed as a
group. Nested grouping previously cost O(2^depth); it is now linear.

Preserves tuple, trailing-comma, grouping, parenthesized
sequence/assignment, and NewLineLeftParen semantics, along with source
spans. Adds depth-based regression tests (16–128 levels).

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Compile time is exponential in arithmetic expression size (hangs at ~24 binary operators)

1 participant