Predicates - #28
Merged
Merged
Predicates#28
Conversation
|
🎉 This PR is included in version 1.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
There was a problem hiding this comment.
Pull request overview
This PR introduces PEG-style syntactic predicates (zero-width lookahead) into the grammar model and JIT codegen pipeline, allowing grammars to express !e (negative lookahead) and &e (positive lookahead) constraints without consuming input or producing AST nodes.
Changes:
- Adds a new
PredicateNodeto the grammar type system and utilities (isPredNode), plus printer support. - Extends codegen to compile predicates and includes new unit tests covering
!/&behavior and AST non-emission. - Updates user-facing docs (README + JSON grammar spec) to document the new node type.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/util.ts | Adds PredicateNode import and isPredNode type guard. |
| src/types.ts | Adds PredicateNode type and wires it into GrammarNode / ResolvedGrammarNode. |
| src/print.ts | Adds printing support for predicate nodes in grammar dumps. |
| src/generator.ts | Treats predicates as zero-width during sample generation (currently no-op). |
| src/codegen/CodegenPredicate.ts | New codegen implementation for predicate matching. |
| src/codegen/CodegenGrammar.ts | Integrates predicate compilation into grammar codegen. |
| src/codegen/tests/CodegenPredicate.spec.ts | Adds unit tests for predicate semantics and AST behavior. |
| README.md | Documents PredicateNode as a 6th grammar node type with examples. |
| JSON-Grammar-spec.md | Updates spec to include PredicateNode semantics and examples. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+127
to
+134
| protected compilePredicate(node: PredicateNode, pattern?: Pattern): Pattern { | ||
| const inner: GrammarNode = node.and !== undefined ? {not: node.and} : (node.not as GrammarNode); | ||
| const childParser = this.getNodeParser(inner); | ||
| pattern ??= new Pattern(node.type ?? 'Predicate'); | ||
| pattern.parser = CodegenPredicate.compile(node, pattern, childParser, this.ctx); | ||
| pattern.toAst = () => null; | ||
| return pattern; | ||
| } |
Comment on lines
+33
to
+43
| public generate() { | ||
| const {pattern, codegen, parser} = this; | ||
| const dLeafCstMatch = codegen.linkDependency(LeafCstMatch); | ||
| const dPattern = codegen.linkDependency(pattern); | ||
| const dParser = codegen.linkDependency(parser); | ||
| const rMatch = codegen.var(`${dParser}(ctx, pos)`); | ||
| codegen.if(rMatch, () => { | ||
| codegen.return(''); | ||
| }); | ||
| codegen.return(`new ${dLeafCstMatch}(pos, pos, ${dPattern})`); | ||
| } |
| else if (isProductionShorthandNode(node)) return this.genProduction({p: node}); | ||
| else if (isUnionNode(node)) return this.genUnion(node); | ||
| else if (isListNode(node)) return this.genList(node); | ||
| else if (isPredNode(node)) return ''; |
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.