List options - #29
Merged
Merged
List options#29
Conversation
There was a problem hiding this comment.
Pull request overview
Adds configurable list parsing options to the grammar/codegen so list nodes can express bounded repetition and optional separators, enabling more concise grammars for common “delimited list” patterns.
Changes:
- Extend
ListNodewithmin,max, andsepoptions. - Update list parser codegen to enforce
min/maxand optionally parse separators with backtracking for trailing delimiters. - Add unit tests covering min/max constraints, separator behavior, and grammar-level integration.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/types.ts | Adds min, max, and sep fields to ListNode. |
| src/codegen/CodegenList.ts | Implements min/max enforcement and optional separator parsing in generated list parsers. |
| src/codegen/CodegenGrammar.ts | Wires ListNode.sep through to CodegenList so separators work in full grammars. |
| src/codegen/tests/CodegenList.spec.ts | Adds tests for bounded lists, separators, backtracking, debug trace balancing, and AST behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const rChild = codegen.var(); | ||
| const rChildren = codegen.var('[]'); | ||
| const min = node.min ?? 0; | ||
| const max = node.max ?? 0; |
Comment on lines
+59
to
+82
| if (dSepParser) { | ||
| const rSep = codegen.var(); | ||
| const rSepPos = codegen.var(); | ||
| codegen.js(`${rChild} = ${dParser}(ctx, pos);`); | ||
| codegen.if(`${rChild} && ${rChild}.end !== pos`, () => { | ||
| codegen.js(`${rChildren}.push(${rChild});`); | ||
| codegen.js(`pos = ${rChild}.end;`); | ||
| codegen.js(`${rCount}++;`); | ||
| const maxCondition = max > 0 ? ` && ${rCount} < ${max}` : ''; | ||
| codegen.while(`1${maxCondition}`, () => { | ||
| codegen.js(`${rSepPos} = pos;`); | ||
| codegen.js(`${rSep} = ${dSepParser}(ctx, pos);`); | ||
| codegen.js(`if (!${rSep} || ${rSep}.end === pos) break;`); | ||
| codegen.js(`pos = ${rSep}.end;`); | ||
| codegen.js(`${rChild} = ${dParser}(ctx, pos);`); | ||
| codegen.js(`if (!${rChild} || ${rChild}.end === pos) {`); | ||
| codegen.js(`pos = ${rSepPos}; break;`); | ||
| codegen.js(`}`); | ||
| codegen.js(`${rChildren}.push(${rSep}, ${rChild});`); | ||
| codegen.js(`pos = ${rChild}.end;`); | ||
| codegen.js(`${rCount}++;`); | ||
| }); | ||
| }); | ||
| } else { |
Comment on lines
+83
to
+92
| codegen.while(`${rChild} = ${dParser}(ctx, pos)`, () => { | ||
| codegen.js(`if (${rChild}.end === pos) break;`); | ||
| codegen.js(`${rChildren}.push(${rChild});`); | ||
| codegen.js(`pos = ${rChild}.end;`); | ||
| codegen.js(`${rCount}++;`); | ||
| if (max > 0) { | ||
| codegen.js(`if (${rCount} >= ${max}) break;`); | ||
| } | ||
| }); | ||
| } |
Comment on lines
+127
to
+128
| }); | ||
| }); |
|
🎉 This PR is included in version 1.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
No description provided.