Skip to content

Predicates - #28

Merged
streamich merged 2 commits into
masterfrom
predicates
Jun 5, 2026
Merged

streamich merged 2 commits into
masterfrom
predicates

Conversation

@streamich

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings June 5, 2026 14:39
@streamich
streamich merged commit 89ccfe2 into master Jun 5, 2026
3 checks passed
@streamich
streamich deleted the predicates branch June 5, 2026 14:39
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PredicateNode to 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})`);
}
Comment thread src/generator.ts
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 '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants