Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | 1x 34x 34x 34x 34x 34x 34x 34x 34x 34x 3x 3x 34x 34x 34x 34x 2x 34x 32x 32x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 6x 34x 28x 28x 28x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 16x 16x 4x 6x 4x 4x 6x 5x 5x 6x 5x 5x 6x 6x 6x 6x 6x 6x 6x 34x 4x 4x 4x 4x 4x 4x 273x 48x 48x 48x 48x 48x 273x 173x 173x 273x 4x 4x 4x 4x 4x 4x 4x 4x 4x 34x | import type { ContextSchema } from '@agentix-e/nl2spel';
export interface GBNFGeneratorOptions {
/** Whether to inject identifiers from ContextSchema (default true) */
injectContext?: boolean;
/** Whether to restrict expression type (default: no restriction) */
restrictTo?: 'expression' | 'boolean' | 'number' | 'string';
}
/**
* GBNFGenerator — dynamically generates llama.cpp compatible GBNF grammar constraints.
*
* GBNF format specification:
* - root ::= "<expression-type>" (top-level rule)
* - Rule names: [a-zA-Z_][a-zA-Z0-9_]*
* - Literals: "text"
* - Character classes: [a-z], [0-9], [a-zA-Z]
* - Groups: (pattern)
* - Alternation: pattern | pattern
* - Repetition: pattern* (0+), pattern+ (1+), pattern? (0-1)
*/
export class GBNFGenerator {
private contextSchema?: ContextSchema;
private options: Required<GBNFGeneratorOptions>;
constructor(options: GBNFGeneratorOptions = {}) {
this.options = {
injectContext: options.injectContext ?? true,
restrictTo: options.restrictTo ?? 'expression',
};
}
/**
* Set the ContextSchema
*/
public setContext(schema: ContextSchema): void {
this.contextSchema = schema;
}
/**
* Generate the full GBNF grammar
*/
public generate(schema?: ContextSchema): string {
if (schema) this.contextSchema = schema;
const rules: string[] = [];
// Top-level rule (selected based on restrictTo)
if (this.options.restrictTo === 'boolean') {
rules.push('root ::= boolean-expr');
} else {
rules.push('root ::= expression');
}
// ===== Basic Lexical =====
rules.push('');
rules.push('# Lexical rules');
rules.push('digit ::= [0-9]');
rules.push('nonzero-digit ::= [1-9]');
rules.push('letter ::= [a-zA-Z]');
rules.push('letter-or-digit ::= [a-zA-Z0-9]');
rules.push('underscore ::= "_"');
// ===== Numbers =====
rules.push('');
rules.push('# Numbers');
rules.push('integer ::= "0" | nonzero-digit digit*');
rules.push('decimal ::= integer "." digit+');
rules.push('number ::= decimal | integer');
// ===== Strings =====
rules.push('');
rules.push('# Strings (single-quoted, supports escaped single quotes)');
rules.push('string-char ::= [^' + "'" + '\\\\] | "\'\'"');
rules.push('string ::= "\'" string-char* "\'"');
// ===== Booleans and null =====
rules.push('');
rules.push('# Booleans and null');
rules.push('boolean-literal ::= "true" | "false"');
rules.push('null-literal ::= "null"');
rules.push('literal ::= number | string | boolean-literal | null-literal');
// ===== Identifiers =====
rules.push('');
rules.push('# Identifiers');
if (this.options.injectContext && this.contextSchema) {
rules.push(...this.generateIdentifierRules());
} else {
rules.push('identifier ::= letter (letter-or-digit | underscore)*');
rules.push('qualified-identifier ::= identifier ("." identifier)*');
}
rules.push('variable-ref ::= "#" identifier');
rules.push('field-ref ::= "#" identifier ("." identifier)+');
// ===== Type references =====
rules.push('');
rules.push('# Type references (T(ClassName))');
rules.push('type-ref ::= "T(" qualified-identifier ")"');
// ===== Bean references =====
rules.push('');
rules.push('# Bean references (@beanName)');
rules.push('bean-ref ::= "@" identifier');
// ===== Method calls =====
rules.push('');
rules.push('# Method calls');
rules.push(
'method-name ::= "contains" | "isEmpty" | "size" | "startsWith" | "endsWith" | "matches" | "after" | "before" | "valueOf"',
);
rules.push('method-call ::= "." method-name "(" (expression ("," expression)*)? ")"');
rules.push('method-call-chain ::= reference method-call+');
rules.push('reference ::= variable-ref | field-ref | bean-ref | literal | "(" expression ")"');
// ===== Collection projection and selection =====
rules.push('');
rules.push('# Collection projection and selection');
rules.push('selection ::= collection-ref ".?[" expression "]"');
rules.push('first-selection ::= collection-ref ".^[" expression "]"');
rules.push('projection ::= collection-ref ".![" expression "]"');
rules.push('collection-ref ::= field-ref | variable-ref');
// ===== Function calls =====
rules.push('');
rules.push('# Function calls');
rules.push('func-name ::= "hasRole" | "hasPermission"');
rules.push('function-call ::= func-name "(" string ("," expression)* ")"');
// ===== Operator precedence (lowest to highest, EBNF → GBNF) =====
rules.push('');
rules.push('# Operators');
// primary: the most basic expression
rules.push(
'primary ::= literal | reference | type-ref | function-call | selection | first-selection | projection | "(" expression ")"',
);
// unary: unary operators
rules.push('unary ::= ("!" | "not" ws) primary | primary');
rules.push('ws ::= [ ]?');
// multiplicative: multiplication, division, modulus
rules.push('multiplicative ::= unary (ws ("*" | "/" | "%") ws unary)?');
// additive: addition, subtraction
rules.push('additive ::= multiplicative (ws ("+" | "-") ws multiplicative)?');
// relational: comparison
rules.push('relational ::= additive (ws (">=" | "<=" | ">" | "<") ws additive)?');
// equality: equality/inequality
rules.push('equality-dot ::= "==" | "!="');
rules.push('equality ::= relational (ws equality-dot ws relational)?');
// between: range
rules.push('between ::= relational ws "between" ws "{" ws number ws "," ws number ws "}"');
// logical-and: logical AND
rules.push('logical-and ::= (between | equality) (ws "and" ws (between | equality))*');
// logical-or: logical OR
rules.push('logical-or ::= logical-and (ws "or" ws logical-and)*');
// elvis: default value
rules.push('elvis ::= logical-or (ws "?:" ws logical-or)?');
// Top-level expression
rules.push('expression ::= elvis');
// Boolean expression type (used for conditions)
rules.push('');
rules.push('# Boolean-specific expressions');
rules.push('boolean-expr ::= logical-or (ws "?:" ws logical-or)?');
return rules.join('\n');
}
/**
* Generate identifier rules injected from ContextSchema
*/
private generateIdentifierRules(): string[] {
const rules: string[] = [];
const schema = this.contextSchema!;
// Collect all valid identifiers
const identifiers: string[] = [
'order',
'user',
'amount',
'status',
'paid',
'remark',
'items',
'tags',
];
if (schema.root) {
identifiers.push(schema.root.name);
for (const field of Object.keys(schema.root.fields)) {
identifiers.push(field);
}
}
for (const key of Object.keys(schema.variables)) {
identifiers.push(key);
}
for (const key of Object.keys(schema.beans)) {
identifiers.push(key);
}
for (const key of Object.keys(schema.types)) {
identifiers.push(key);
}
// Deduplicate
const uniqueIds = [...new Set(identifiers)];
// Generate identifier rule (GBNF string alternation)
const idRules = uniqueIds.map((id) => `"${id}"`).join(' | ');
rules.push(`identifier ::= (${idRules}) | general-identifier`);
rules.push('general-identifier ::= letter (letter-or-digit | underscore)*');
// root-field: inject root object fields (makes grammar constraints more precise)
rules.push('qualified-identifier ::= identifier ("." identifier)*');
return rules;
}
/**
* Generate structured GBNF (with comments and sections)
*/
public generateStructured(schema?: ContextSchema): GBNFStructure {
if (schema) this.contextSchema = schema;
const grammar = this.generate();
const lines = grammar.split('\n');
const sections: GBNFSection[] = [];
let currentSection: GBNFSection | null = null;
for (const line of lines) {
if (line.startsWith('# ')) {
if (currentSection) sections.push(currentSection);
currentSection = {
name: line.slice(2).trim(),
rules: [],
};
} else if (line.trim() && currentSection) {
currentSection.rules.push(line.trim());
}
}
if (currentSection) sections.push(currentSection);
return {
rootRule:
sections.find((s) => s.rules.some((r) => r.startsWith('root')))?.rules[0] ??
'root ::= expression',
sections,
ruleCount: lines.filter((l) => l.includes('::=') && !l.startsWith('#')).length,
};
}
}
export interface GBNFSection {
name: string;
rules: string[];
}
export interface GBNFStructure {
rootRule: string;
sections: GBNFSection[];
ruleCount: number;
}
|