Skip to content

parsers: stop Choice from looping on a nested choice parser - #26

Merged
rameel merged 1 commit into
mainfrom
choice-nested-flattening
Sep 22, 2026
Merged

rameel merged 1 commit into
mainfrom
choice-nested-flattening

Conversation

@rameel

@rameel rameel commented Sep 22, 2026 •

Copy link
Copy Markdown
Owner

Choice flattens nested choices to avoid extra parser calls. The flattening loop appended parsers to a list without rebinding the input array, so a nested Choice never let the loop terminate:

using static Ramstack.Parsing.Parser;

var inner = Choice(L('a'), L('b').Between(L('('), L(')')));
var outer = Choice(inner, L('c')); // out of memory

The fix expands nested unnamed choices in one pass. A choice is flattened when it is created, so one pass expands the whole tree.

A named nested choice stays a single alternative and keeps its name.

Flatten nested choice parsers in one pass. A choice is flattened when it
is created, so expanding each nested unnamed choice once flattens the
whole tree. The previous loop re-added parsers without rebinding the
input array and never terminated, so any nested Choice ran out of
memory.

Keep a named nested choice as a single alternative so its name is not
lost, and use the flattened list for values other than char and Unit.
@rameel
rameel merged commit 7e5540f into main Sep 22, 2026
2 checks passed
@rameel
rameel deleted the choice-nested-flattening branch September 22, 2026 18:23
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.

1 participant