@lokascript/framework 4.6.0 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +121 -1
  2. package/dist/api/create-dsl.d.ts +18 -2
  3. package/dist/api/create-dsl.d.ts.map +1 -1
  4. package/dist/api/index.d.ts +1 -0
  5. package/dist/api/index.d.ts.map +1 -1
  6. package/dist/api/index.js +1799 -713
  7. package/dist/api/index.js.map +1 -1
  8. package/dist/api/translation-loss.d.ts +84 -0
  9. package/dist/api/translation-loss.d.ts.map +1 -0
  10. package/dist/core/index.d.ts +1 -0
  11. package/dist/core/index.d.ts.map +1 -1
  12. package/dist/core/index.js +1057 -684
  13. package/dist/core/index.js.map +1 -1
  14. package/dist/core/matching/confidence-model.d.ts +54 -0
  15. package/dist/core/matching/confidence-model.d.ts.map +1 -0
  16. package/dist/core/matching/index.d.ts +12 -0
  17. package/dist/core/matching/index.d.ts.map +1 -0
  18. package/dist/core/matching/match-skeleton.d.ts +114 -0
  19. package/dist/core/matching/match-skeleton.d.ts.map +1 -0
  20. package/dist/core/matching/stock-confidence.d.ts +15 -0
  21. package/dist/core/matching/stock-confidence.d.ts.map +1 -0
  22. package/dist/core/matching/stock-reader.d.ts +63 -0
  23. package/dist/core/matching/stock-reader.d.ts.map +1 -0
  24. package/dist/core/matching/stock-reading.d.ts +44 -0
  25. package/dist/core/matching/stock-reading.d.ts.map +1 -0
  26. package/dist/core/matching/value-reader.d.ts +62 -0
  27. package/dist/core/matching/value-reader.d.ts.map +1 -0
  28. package/dist/core/pattern-matching/index.js +561 -660
  29. package/dist/core/pattern-matching/index.js.map +1 -1
  30. package/dist/core/pattern-matching/pattern-matcher.d.ts +0 -117
  31. package/dist/core/pattern-matching/pattern-matcher.d.ts.map +1 -1
  32. package/dist/core/types.d.ts +53 -0
  33. package/dist/core/types.d.ts.map +1 -1
  34. package/dist/core/types.js.map +1 -1
  35. package/dist/generation/index.d.ts +2 -0
  36. package/dist/generation/index.d.ts.map +1 -1
  37. package/dist/generation/index.js +593 -1
  38. package/dist/generation/index.js.map +1 -1
  39. package/dist/generation/pattern-render.d.ts +72 -0
  40. package/dist/generation/pattern-render.d.ts.map +1 -0
  41. package/dist/generation/schema-patterns.d.ts +152 -0
  42. package/dist/generation/schema-patterns.d.ts.map +1 -0
  43. package/dist/index.cjs +2016 -904
  44. package/dist/index.cjs.map +1 -1
  45. package/dist/index.js +1998 -903
  46. package/dist/index.js.map +1 -1
  47. package/dist/ir/index.js.map +1 -1
  48. package/dist/parsing/index.js +561 -660
  49. package/dist/parsing/index.js.map +1 -1
  50. package/dist/testing/gate/index.d.ts +8 -0
  51. package/dist/testing/gate/index.d.ts.map +1 -0
  52. package/dist/testing/gate/index.js +664 -0
  53. package/dist/testing/gate/index.js.map +1 -0
  54. package/dist/testing/gate/lint/format.d.ts +7 -0
  55. package/dist/testing/gate/lint/format.d.ts.map +1 -0
  56. package/dist/testing/gate/lint/index.d.ts +30 -0
  57. package/dist/testing/gate/lint/index.d.ts.map +1 -0
  58. package/dist/testing/gate/lint/rules/extractor-coverage.d.ts +18 -0
  59. package/dist/testing/gate/lint/rules/extractor-coverage.d.ts.map +1 -0
  60. package/dist/testing/gate/lint/rules/index.d.ts +14 -0
  61. package/dist/testing/gate/lint/rules/index.d.ts.map +1 -0
  62. package/dist/testing/gate/lint/rules/keyword-classification.d.ts +19 -0
  63. package/dist/testing/gate/lint/rules/keyword-classification.d.ts.map +1 -0
  64. package/dist/testing/gate/lint/rules/keyword-coverage.d.ts +13 -0
  65. package/dist/testing/gate/lint/rules/keyword-coverage.d.ts.map +1 -0
  66. package/dist/testing/gate/lint/rules/marker-tokenization.d.ts +20 -0
  67. package/dist/testing/gate/lint/rules/marker-tokenization.d.ts.map +1 -0
  68. package/dist/testing/gate/lint/rules/position-ordering.d.ts +25 -0
  69. package/dist/testing/gate/lint/rules/position-ordering.d.ts.map +1 -0
  70. package/dist/testing/gate/lint/rules/renderer-coherence.d.ts +24 -0
  71. package/dist/testing/gate/lint/rules/renderer-coherence.d.ts.map +1 -0
  72. package/dist/testing/gate/lint/rules/schema-rules.d.ts +10 -0
  73. package/dist/testing/gate/lint/rules/schema-rules.d.ts.map +1 -0
  74. package/dist/testing/gate/lint/types.d.ts +74 -0
  75. package/dist/testing/gate/lint/types.d.ts.map +1 -0
  76. package/dist/testing/gate/round-trip.d.ts +184 -0
  77. package/dist/testing/gate/round-trip.d.ts.map +1 -0
  78. package/package.json +6 -2
  79. package/src/__test__/fixtures/sql-dsl.ts +8 -2
  80. package/src/api/create-dsl-engine.test.ts +150 -0
  81. package/src/api/create-dsl.test.ts +7 -0
  82. package/src/api/create-dsl.ts +173 -22
  83. package/src/api/index.ts +1 -0
  84. package/src/api/translation-loss.test.ts +89 -0
  85. package/src/api/translation-loss.ts +128 -0
  86. package/src/core/index.ts +1 -0
  87. package/src/core/matching/confidence-model.ts +58 -0
  88. package/src/core/matching/index.ts +12 -0
  89. package/src/core/matching/match-skeleton.test.ts +179 -0
  90. package/src/core/matching/match-skeleton.ts +621 -0
  91. package/src/core/matching/stock-confidence.ts +238 -0
  92. package/src/core/matching/stock-reader.test.ts +265 -0
  93. package/src/core/matching/stock-reader.ts +205 -0
  94. package/src/core/matching/stock-reading.ts +681 -0
  95. package/src/core/matching/value-reader.ts +85 -0
  96. package/src/core/pattern-matching/pattern-matcher.ts +11 -876
  97. package/src/core/types.ts +53 -0
  98. package/src/generation/index.ts +2 -0
  99. package/src/generation/pattern-render.ts +378 -0
  100. package/src/generation/schema-patterns.test.ts +139 -0
  101. package/src/generation/schema-patterns.ts +974 -0
  102. package/src/testing/gate/index.ts +8 -0
  103. package/src/testing/gate/lint/format.ts +23 -0
  104. package/src/testing/gate/lint/index.ts +73 -0
  105. package/src/testing/gate/lint/lint.test.ts +131 -0
  106. package/src/testing/gate/lint/rules/extractor-coverage.ts +63 -0
  107. package/src/testing/gate/lint/rules/index.ts +37 -0
  108. package/src/testing/gate/lint/rules/keyword-classification.ts +52 -0
  109. package/src/testing/gate/lint/rules/keyword-coverage.ts +38 -0
  110. package/src/testing/gate/lint/rules/marker-tokenization.ts +129 -0
  111. package/src/testing/gate/lint/rules/position-ordering.ts +87 -0
  112. package/src/testing/gate/lint/rules/renderer-coherence.ts +100 -0
  113. package/src/testing/gate/lint/rules/schema-rules.ts +35 -0
  114. package/src/testing/gate/lint/types.ts +83 -0
  115. package/src/testing/gate/round-trip.test.ts +208 -0
  116. package/src/testing/gate/round-trip.ts +503 -0
package/dist/index.js CHANGED
@@ -150,10 +150,535 @@ function getAllPossessiveKeywords(profile) {
150
150
  return profile?.possessive?.keywords ?? {};
151
151
  }
152
152
 
153
- // src/core/pattern-matching/pattern-matcher.ts
153
+ // src/core/matching/stock-reading.ts
154
154
  function isValidReference(_value) {
155
155
  return false;
156
156
  }
157
+ function safeToLowerCase(value) {
158
+ if (typeof value === "string") {
159
+ return value.toLowerCase();
160
+ }
161
+ if (value === null || value === void 0) {
162
+ return "";
163
+ }
164
+ return String(value).toLowerCase();
165
+ }
166
+ function readRoleValue(tokens, patternToken, captured, profile) {
167
+ skipNoiseWords(tokens);
168
+ const token = tokens.peek();
169
+ if (!token) {
170
+ return patternToken.optional || false;
171
+ }
172
+ const possessiveValue = tryMatchPossessiveExpression(tokens, profile);
173
+ if (possessiveValue) {
174
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
175
+ if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
176
+ return patternToken.optional || false;
177
+ }
178
+ }
179
+ captured.set(patternToken.role, possessiveValue);
180
+ return true;
181
+ }
182
+ const methodCallValue = tryMatchMethodCallExpression(tokens);
183
+ if (methodCallValue) {
184
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
185
+ if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
186
+ return patternToken.optional || false;
187
+ }
188
+ }
189
+ captured.set(patternToken.role, methodCallValue);
190
+ return true;
191
+ }
192
+ const possessiveSelectorValue = tryMatchPossessiveSelectorExpression(tokens);
193
+ if (possessiveSelectorValue) {
194
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
195
+ if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
196
+ return patternToken.optional || false;
197
+ }
198
+ }
199
+ captured.set(patternToken.role, possessiveSelectorValue);
200
+ return true;
201
+ }
202
+ const propertyAccessValue = tryMatchPropertyAccessExpression(tokens);
203
+ if (propertyAccessValue) {
204
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
205
+ if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
206
+ return patternToken.optional || false;
207
+ }
208
+ }
209
+ captured.set(patternToken.role, propertyAccessValue);
210
+ return true;
211
+ }
212
+ const selectorPropertyValue = tryMatchSelectorPropertyExpression(tokens);
213
+ if (selectorPropertyValue) {
214
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
215
+ if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
216
+ return patternToken.optional || false;
217
+ }
218
+ }
219
+ captured.set(patternToken.role, selectorPropertyValue);
220
+ return true;
221
+ }
222
+ const value = tokenToSemanticValue(token);
223
+ if (!value) {
224
+ return patternToken.optional || false;
225
+ }
226
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
227
+ if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
228
+ return patternToken.optional || false;
229
+ }
230
+ }
231
+ captured.set(patternToken.role, value);
232
+ tokens.advance();
233
+ return true;
234
+ }
235
+ function tryMatchPossessiveExpression(tokens, profile) {
236
+ const token = tokens.peek();
237
+ if (!token) return null;
238
+ if (!profile) return null;
239
+ const tokenValue = token.normalized || token.value;
240
+ const tokenLower = safeToLowerCase(tokenValue);
241
+ const baseRef = getPossessiveReference(profile, tokenLower);
242
+ if (!baseRef) return null;
243
+ const mark = tokens.mark();
244
+ tokens.advance();
245
+ const propertyToken = tokens.peek();
246
+ if (!propertyToken) {
247
+ tokens.reset(mark);
248
+ return null;
249
+ }
250
+ if (propertyToken.kind === "identifier" || propertyToken.kind === "keyword" && !isStructuralKeyword(propertyToken.value) || propertyToken.kind === "selector" && propertyToken.value.startsWith("*") || propertyToken.kind === "selector" && propertyToken.value.startsWith("@") || propertyToken.kind === "selector" && propertyToken.value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyToken.value)) {
251
+ tokens.advance();
252
+ let propertyName = propertyToken.value;
253
+ if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
254
+ propertyName = propertyName.substring(1);
255
+ }
256
+ let chainedProps = propertyName;
257
+ while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
258
+ chainedProps += tokens.peek().value;
259
+ tokens.advance();
260
+ }
261
+ const nextPeek = tokens.peek();
262
+ if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
263
+ chainedProps += nextPeek.value;
264
+ tokens.advance();
265
+ }
266
+ return createPropertyPath(createReference(baseRef), chainedProps);
267
+ }
268
+ tokens.reset(mark);
269
+ return null;
270
+ }
271
+ function isStructuralKeyword(value) {
272
+ const structural = /* @__PURE__ */ new Set([
273
+ // Prepositions
274
+ "into",
275
+ "in",
276
+ "to",
277
+ "from",
278
+ "at",
279
+ "by",
280
+ "with",
281
+ "without",
282
+ "before",
283
+ "after",
284
+ "of",
285
+ "as",
286
+ "on",
287
+ // Control flow
288
+ "then",
289
+ "end",
290
+ "else",
291
+ "if",
292
+ "repeat",
293
+ "while",
294
+ "for",
295
+ // Commands (shouldn't be property names)
296
+ "toggle",
297
+ "add",
298
+ "remove",
299
+ "put",
300
+ "set",
301
+ "show",
302
+ "hide",
303
+ "increment",
304
+ "decrement",
305
+ "send",
306
+ "trigger",
307
+ "call"
308
+ ]);
309
+ return structural.has(value.toLowerCase());
310
+ }
311
+ function tryMatchMethodCallExpression(tokens) {
312
+ const token = tokens.peek();
313
+ if (!token || token.kind !== "selector") return null;
314
+ const mark = tokens.mark();
315
+ tokens.advance();
316
+ const dotToken = tokens.peek();
317
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
318
+ tokens.reset(mark);
319
+ return null;
320
+ }
321
+ tokens.advance();
322
+ const methodToken = tokens.peek();
323
+ if (!methodToken || methodToken.kind !== "identifier") {
324
+ tokens.reset(mark);
325
+ return null;
326
+ }
327
+ tokens.advance();
328
+ const openParen = tokens.peek();
329
+ if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
330
+ tokens.reset(mark);
331
+ return null;
332
+ }
333
+ tokens.advance();
334
+ const args = [];
335
+ while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
336
+ const argToken = tokens.peek();
337
+ if (!argToken) break;
338
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
339
+ tokens.advance();
340
+ break;
341
+ }
342
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
343
+ tokens.advance();
344
+ continue;
345
+ }
346
+ args.push(argToken.value);
347
+ tokens.advance();
348
+ }
349
+ const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
350
+ return {
351
+ type: "expression",
352
+ raw: methodCall
353
+ };
354
+ }
355
+ function tryMatchPropertyAccessExpression(tokens) {
356
+ const token = tokens.peek();
357
+ if (!token) return null;
358
+ if (token.kind !== "identifier" && token.kind !== "keyword") return null;
359
+ const mark = tokens.mark();
360
+ tokens.advance();
361
+ const dotToken = tokens.peek();
362
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
363
+ tokens.reset(mark);
364
+ return null;
365
+ }
366
+ tokens.advance();
367
+ const propertyToken = tokens.peek();
368
+ if (!propertyToken || propertyToken.kind !== "identifier") {
369
+ tokens.reset(mark);
370
+ return null;
371
+ }
372
+ tokens.advance();
373
+ let chain = `${token.value}.${propertyToken.value}`;
374
+ let depth = 1;
375
+ while (!tokens.isAtEnd() && depth < MAX_PROPERTY_DEPTH) {
376
+ const nextDot = tokens.peek();
377
+ if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
378
+ break;
379
+ }
380
+ tokens.advance();
381
+ const nextProp = tokens.peek();
382
+ if (!nextProp || nextProp.kind !== "identifier") {
383
+ break;
384
+ }
385
+ tokens.advance();
386
+ chain += `.${nextProp.value}`;
387
+ depth++;
388
+ }
389
+ const openParen = tokens.peek();
390
+ if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
391
+ tokens.advance();
392
+ const args = [];
393
+ let argDepth = 0;
394
+ while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
395
+ const argToken = tokens.peek();
396
+ if (!argToken) break;
397
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
398
+ if (argDepth === 0) {
399
+ tokens.advance();
400
+ break;
401
+ }
402
+ argDepth--;
403
+ }
404
+ if (argToken.kind === "punctuation" && argToken.value === "(") {
405
+ argDepth++;
406
+ }
407
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
408
+ tokens.advance();
409
+ continue;
410
+ }
411
+ args.push(argToken.value);
412
+ tokens.advance();
413
+ }
414
+ const methodCall = `${chain}(${args.join(", ")})`;
415
+ return {
416
+ type: "expression",
417
+ raw: methodCall
418
+ };
419
+ }
420
+ return {
421
+ type: "expression",
422
+ raw: chain
423
+ };
424
+ }
425
+ function tryMatchPossessiveSelectorExpression(tokens) {
426
+ const token = tokens.peek();
427
+ if (!token || token.kind !== "selector") return null;
428
+ const mark = tokens.mark();
429
+ tokens.advance();
430
+ const possessiveToken = tokens.peek();
431
+ if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
432
+ tokens.reset(mark);
433
+ return null;
434
+ }
435
+ tokens.advance();
436
+ const propertyToken = tokens.peek();
437
+ if (!propertyToken) {
438
+ tokens.reset(mark);
439
+ return null;
440
+ }
441
+ if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
442
+ tokens.reset(mark);
443
+ return null;
444
+ }
445
+ tokens.advance();
446
+ return createPropertyPath(createSelector(token.value), propertyToken.value);
447
+ }
448
+ function tryMatchSelectorPropertyExpression(tokens) {
449
+ const token = tokens.peek();
450
+ if (!token || token.kind !== "selector") return null;
451
+ if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
452
+ const mark = tokens.mark();
453
+ tokens.advance();
454
+ const propertyToken = tokens.peek();
455
+ if (!propertyToken || propertyToken.kind !== "selector") {
456
+ tokens.reset(mark);
457
+ return null;
458
+ }
459
+ if (!propertyToken.value.startsWith(".")) {
460
+ tokens.reset(mark);
461
+ return null;
462
+ }
463
+ const peek2 = tokens.peek(1);
464
+ if (peek2 && peek2.kind === "selector") {
465
+ }
466
+ tokens.advance();
467
+ const propertyName = propertyToken.value.slice(1);
468
+ return createPropertyPath(createSelector(token.value), propertyName);
469
+ }
470
+ var MAX_PROPERTY_DEPTH = 10;
471
+ var MAX_METHOD_ARGS = 20;
472
+ function tokenToSemanticValue(token) {
473
+ switch (token.kind) {
474
+ case "selector":
475
+ return createSelector(token.value);
476
+ case "literal":
477
+ return parseLiteralValue(token.value);
478
+ case "keyword": {
479
+ const tokenValue = token.normalized || token.value;
480
+ const lower = safeToLowerCase(tokenValue);
481
+ if (isValidReference(lower)) {
482
+ return createReference(lower);
483
+ }
484
+ return createLiteral(token.normalized || token.value);
485
+ }
486
+ case "identifier": {
487
+ if (typeof token.value === "string" && token.value.startsWith(":")) {
488
+ return createReference(token.value);
489
+ }
490
+ const identLower = safeToLowerCase(token.value);
491
+ if (isValidReference(identLower)) {
492
+ return createReference(identLower);
493
+ }
494
+ return { type: "expression", raw: token.value };
495
+ }
496
+ case "url":
497
+ return createLiteral(token.value, "string");
498
+ default:
499
+ return null;
500
+ }
501
+ }
502
+ function parseLiteralValue(value) {
503
+ if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
504
+ const inner = value.slice(1, -1);
505
+ return createLiteral(inner, "string");
506
+ }
507
+ if (value === "true") return createLiteral(true, "boolean");
508
+ if (value === "false") return createLiteral(false, "boolean");
509
+ const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
510
+ if (durationMatch) {
511
+ const num2 = parseFloat(durationMatch[1]);
512
+ const unit = durationMatch[2];
513
+ if (unit) {
514
+ return createLiteral(value, "duration");
515
+ }
516
+ return createLiteral(num2, "number");
517
+ }
518
+ const num = parseFloat(value);
519
+ if (!isNaN(num)) {
520
+ return createLiteral(num, "number");
521
+ }
522
+ return createLiteral(value, "string");
523
+ }
524
+ var ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
525
+ function skipNoiseWords(tokens) {
526
+ const token = tokens.peek();
527
+ if (!token) return;
528
+ const tokenLower = safeToLowerCase(token.value);
529
+ if (ENGLISH_NOISE_WORDS.has(tokenLower)) {
530
+ const mark = tokens.mark();
531
+ tokens.advance();
532
+ const nextToken = tokens.peek();
533
+ if (nextToken && nextToken.kind === "selector") {
534
+ return;
535
+ }
536
+ tokens.reset(mark);
537
+ }
538
+ if (tokenLower === "class") {
539
+ tokens.advance();
540
+ }
541
+ }
542
+
543
+ // src/core/matching/stock-confidence.ts
544
+ function scoreParts(ctx) {
545
+ const { pattern, captured } = ctx;
546
+ let score = 0;
547
+ let maxScore = 0;
548
+ const hasDefault = (role) => {
549
+ return pattern.extraction?.[role]?.default !== void 0;
550
+ };
551
+ for (const token of pattern.template.tokens) {
552
+ if (token.type === "role") {
553
+ maxScore += 1;
554
+ if (captured.has(token.role)) {
555
+ score += 1;
556
+ }
557
+ } else if (token.type === "group") {
558
+ for (const subToken of token.tokens) {
559
+ if (subToken.type === "role") {
560
+ const roleHasDefault = hasDefault(subToken.role);
561
+ const weight = 0.8;
562
+ maxScore += weight;
563
+ if (captured.has(subToken.role)) {
564
+ score += weight;
565
+ } else if (roleHasDefault) {
566
+ score += weight * 0.6;
567
+ }
568
+ }
569
+ }
570
+ }
571
+ }
572
+ const roleCoverage = maxScore > 0 ? score / maxScore : 1;
573
+ let baseConfidence = roleCoverage;
574
+ let stemPenalty = 0;
575
+ if (ctx.stemMatchCount > 0 && ctx.totalKeywordMatches > 0) {
576
+ stemPenalty = ctx.stemMatchCount / ctx.totalKeywordMatches * 0.15;
577
+ baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
578
+ }
579
+ const vsoBoost = calculateVSOConfidenceBoost(pattern);
580
+ baseConfidence = Math.min(1, baseConfidence + vsoBoost);
581
+ const prepositionAdjustment = arabicPrepositionDisambiguation(pattern, captured);
582
+ baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
583
+ return {
584
+ roleCoverage,
585
+ stemPenalty,
586
+ languageBoost: vsoBoost,
587
+ languageAdjustment: prepositionAdjustment,
588
+ final: baseConfidence
589
+ };
590
+ }
591
+ function calculateVSOConfidenceBoost(pattern) {
592
+ if (pattern.language !== "ar") {
593
+ return 0;
594
+ }
595
+ const firstToken = pattern.template.tokens[0];
596
+ if (!firstToken || firstToken.type !== "literal") {
597
+ return 0;
598
+ }
599
+ const ARABIC_VERBS = /* @__PURE__ */ new Set([
600
+ "\u0628\u062F\u0644",
601
+ "\u063A\u064A\u0631",
602
+ "\u0623\u0636\u0641",
603
+ "\u0623\u0632\u0644",
604
+ "\u0636\u0639",
605
+ "\u0627\u062C\u0639\u0644",
606
+ "\u0639\u064A\u0646",
607
+ "\u0632\u062F",
608
+ "\u0627\u0646\u0642\u0635",
609
+ "\u0633\u062C\u0644",
610
+ "\u0623\u0638\u0647\u0631",
611
+ "\u0623\u062E\u0641",
612
+ "\u0634\u063A\u0644",
613
+ "\u0623\u0631\u0633\u0644",
614
+ "\u0631\u0643\u0632",
615
+ "\u0634\u0648\u0634",
616
+ "\u062A\u0648\u0642\u0641",
617
+ "\u0627\u0646\u0633\u062E",
618
+ "\u0627\u062D\u0630\u0641",
619
+ "\u0627\u0635\u0646\u0639",
620
+ "\u0627\u0646\u062A\u0638\u0631",
621
+ "\u0627\u0646\u062A\u0642\u0627\u0644",
622
+ "\u0623\u0648"
623
+ ]);
624
+ if (ARABIC_VERBS.has(firstToken.value)) {
625
+ return 0.15;
626
+ }
627
+ if (firstToken.alternatives) {
628
+ for (const alt of firstToken.alternatives) {
629
+ if (ARABIC_VERBS.has(alt)) {
630
+ return 0.15;
631
+ }
632
+ }
633
+ }
634
+ return 0;
635
+ }
636
+ function arabicPrepositionDisambiguation(pattern, captured) {
637
+ if (pattern.language !== "ar") {
638
+ return 0;
639
+ }
640
+ let adjustment = 0;
641
+ const PREFERRED_PREPOSITIONS = {
642
+ patient: ["\u0639\u0644\u0649"],
643
+ // element selectors prefer على (on/upon)
644
+ destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
645
+ // destination prefers إلى (to)
646
+ source: ["\u0645\u0646"],
647
+ // source prefers من (from)
648
+ agent: ["\u0645\u0646"],
649
+ // agent/by prefers من (from/by)
650
+ manner: ["\u0628"],
651
+ // manner prefers ب (with/by)
652
+ style: ["\u0628"],
653
+ // style prefers ب (with)
654
+ goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
655
+ // target state prefers إلى (to)
656
+ method: ["\u0628"]
657
+ // method prefers ب (with/by)
658
+ };
659
+ for (const [role, value] of captured.entries()) {
660
+ const preferred = PREFERRED_PREPOSITIONS[role];
661
+ if (!preferred || preferred.length === 0) {
662
+ continue;
663
+ }
664
+ const metadata = "metadata" in value ? value.metadata : void 0;
665
+ if (metadata && typeof metadata.prepositionValue === "string") {
666
+ const usedPreposition = metadata.prepositionValue;
667
+ if (preferred.includes(usedPreposition)) {
668
+ adjustment += 0.1;
669
+ } else {
670
+ adjustment -= 0.1;
671
+ }
672
+ }
673
+ }
674
+ return Math.max(-0.1, Math.min(0.1, adjustment));
675
+ }
676
+ var stockConfidenceModel = {
677
+ calculate: (ctx) => scoreParts(ctx).final,
678
+ calculateWithBreakdown: (ctx) => scoreParts(ctx)
679
+ };
680
+
681
+ // src/core/pattern-matching/pattern-matcher.ts
157
682
  var _PatternMatcher = class _PatternMatcher {
158
683
  constructor() {
159
684
  /** Debug logger */
@@ -165,19 +690,6 @@ var _PatternMatcher = class _PatternMatcher {
165
690
  this.stemMatchCount = 0;
166
691
  this.totalKeywordMatches = 0;
167
692
  }
168
- /**
169
- * Safely convert a value to lowercase string.
170
- * Provides protection against non-string values at runtime.
171
- */
172
- safeToLowerCase(value) {
173
- if (typeof value === "string") {
174
- return value.toLowerCase();
175
- }
176
- if (value === null || value === void 0) {
177
- return "";
178
- }
179
- return String(value).toLowerCase();
180
- }
181
693
  /**
182
694
  * Try to match a single pattern against the token stream.
183
695
  * Returns the match result or null if no match.
@@ -226,7 +738,12 @@ var _PatternMatcher = class _PatternMatcher {
226
738
  tokens.reset(mark);
227
739
  return null;
228
740
  }
229
- const confidence = this.calculateConfidence(pattern, captured);
741
+ const confidence = stockConfidenceModel.calculate({
742
+ pattern,
743
+ captured,
744
+ stemMatchCount: this.stemMatchCount,
745
+ totalKeywordMatches: this.totalKeywordMatches
746
+ });
230
747
  this.applyExtractionRules(pattern, captured);
231
748
  return {
232
749
  pattern,
@@ -411,7 +928,7 @@ var _PatternMatcher = class _PatternMatcher {
411
928
  case "literal":
412
929
  return this.matchLiteralToken(tokens, patternToken);
413
930
  case "role":
414
- return this.matchRoleToken(tokens, patternToken, captured);
931
+ return readRoleValue(tokens, patternToken, captured, this.currentProfile);
415
932
  case "group":
416
933
  return this.matchGroupToken(tokens, patternToken, captured);
417
934
  default:
@@ -469,420 +986,54 @@ var _PatternMatcher = class _PatternMatcher {
469
986
  return false;
470
987
  }
471
988
  /**
472
- * Match a role pattern token (captures a semantic value).
473
- * Handles multi-token expressions like:
474
- * - 'my value' (possessive keyword + property)
475
- * - '#dialog.showModal()' (method call)
476
- * - "#element's *opacity" (possessive selector + property)
989
+ * Match a group pattern token (optional sequence).
990
+ * When the group's leading marker isn't at the current position, scans ahead
991
+ * up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
992
+ * (e.g., a value like 'hello') to sit between groups without blocking later
993
+ * marker matches.
477
994
  */
478
- matchRoleToken(tokens, patternToken, captured) {
479
- this.logger.debug(" >>> matchRoleToken ENTRY: capturing role", patternToken.role);
480
- this.logger.debug(" >>> matchRoleToken: expected types", patternToken.expectedTypes);
481
- this.logger.debug(" >>> matchRoleToken: optional?", patternToken.optional);
482
- this.skipNoiseWords(tokens);
483
- const token = tokens.peek();
484
- this.logger.debug(
485
- " >>> After skipNoiseWords, current token:",
486
- token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
995
+ matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
996
+ const mark = tokens.mark();
997
+ const capturedBefore = new Set(captured.keys());
998
+ const success = this.matchTokenSequence(
999
+ tokens,
1000
+ patternToken.tokens,
1001
+ captured,
1002
+ parentStopMarkers
487
1003
  );
488
- if (!token) {
489
- return patternToken.optional || false;
490
- }
491
- const possessiveValue = this.tryMatchPossessiveExpression(tokens);
492
- if (possessiveValue) {
493
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
494
- if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
495
- return patternToken.optional || false;
496
- }
497
- }
498
- captured.set(patternToken.role, possessiveValue);
499
- return true;
500
- }
501
- const methodCallValue = this.tryMatchMethodCallExpression(tokens);
502
- if (methodCallValue) {
503
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
504
- if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
505
- return patternToken.optional || false;
506
- }
507
- }
508
- captured.set(patternToken.role, methodCallValue);
509
- return true;
510
- }
511
- const possessiveSelectorValue = this.tryMatchPossessiveSelectorExpression(tokens);
512
- if (possessiveSelectorValue) {
513
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
514
- if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
515
- return patternToken.optional || false;
516
- }
517
- }
518
- captured.set(patternToken.role, possessiveSelectorValue);
519
- return true;
520
- }
521
- const propertyAccessValue = this.tryMatchPropertyAccessExpression(tokens);
522
- if (propertyAccessValue) {
523
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
524
- if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
525
- return patternToken.optional || false;
526
- }
527
- }
528
- captured.set(patternToken.role, propertyAccessValue);
529
- return true;
1004
+ if (success) return true;
1005
+ tokens.reset(mark);
1006
+ for (const role of captured.keys()) {
1007
+ if (!capturedBefore.has(role)) captured.delete(role);
530
1008
  }
531
- const selectorPropertyValue = this.tryMatchSelectorPropertyExpression(tokens);
532
- if (selectorPropertyValue) {
533
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
534
- if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
535
- return patternToken.optional || false;
536
- }
537
- }
538
- captured.set(patternToken.role, selectorPropertyValue);
539
- return true;
540
- }
541
- this.logger.debug(
542
- " >>> Trying tokenToSemanticValue for token:",
543
- token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
544
- );
545
- const value = this.tokenToSemanticValue(token);
546
- this.logger.debug(
547
- " >>> tokenToSemanticValue returned:",
548
- value ? JSON.stringify(value) : "null"
549
- );
550
- if (!value) {
551
- return patternToken.optional || false;
552
- }
553
- this.logger.debug(
554
- " >>> Validating type:",
555
- value.type,
556
- "against expected:",
557
- patternToken.expectedTypes
558
- );
559
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
560
- if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
561
- this.logger.debug(" >>> TYPE MISMATCH - returning", patternToken.optional || false);
562
- return patternToken.optional || false;
563
- }
564
- }
565
- this.logger.debug(" >>> Type validation PASSED");
566
- captured.set(patternToken.role, value);
567
- tokens.advance();
568
- return true;
569
- }
570
- /**
571
- * Try to match a possessive expression like 'my value' or 'its innerHTML'.
572
- * Returns the PropertyPathValue if matched, or null if not.
573
- */
574
- tryMatchPossessiveExpression(tokens) {
575
- const token = tokens.peek();
576
- if (!token) return null;
577
- if (!this.currentProfile) return null;
578
- const tokenValue = token.normalized || token.value;
579
- const tokenLower = this.safeToLowerCase(tokenValue);
580
- const baseRef = getPossessiveReference(this.currentProfile, tokenLower);
581
- if (!baseRef) return null;
582
- const mark = tokens.mark();
583
- tokens.advance();
584
- const propertyToken = tokens.peek();
585
- if (!propertyToken) {
586
- tokens.reset(mark);
587
- return null;
588
- }
589
- if (propertyToken.kind === "identifier" || propertyToken.kind === "keyword" && !this.isStructuralKeyword(propertyToken.value) || propertyToken.kind === "selector" && propertyToken.value.startsWith("*") || propertyToken.kind === "selector" && propertyToken.value.startsWith("@") || propertyToken.kind === "selector" && propertyToken.value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyToken.value)) {
590
- tokens.advance();
591
- let propertyName = propertyToken.value;
592
- if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
593
- propertyName = propertyName.substring(1);
594
- }
595
- let chainedProps = propertyName;
596
- while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
597
- chainedProps += tokens.peek().value;
598
- tokens.advance();
599
- }
600
- const nextPeek = tokens.peek();
601
- if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
602
- chainedProps += nextPeek.value;
603
- tokens.advance();
604
- }
605
- return createPropertyPath(createReference(baseRef), chainedProps);
606
- }
607
- tokens.reset(mark);
608
- return null;
609
- }
610
- /**
611
- * Check if a keyword is a structural keyword (preposition, control flow, etc.)
612
- * that shouldn't be consumed as a property name.
613
- */
614
- isStructuralKeyword(value) {
615
- const structural = /* @__PURE__ */ new Set([
616
- // Prepositions
617
- "into",
618
- "in",
619
- "to",
620
- "from",
621
- "at",
622
- "by",
623
- "with",
624
- "without",
625
- "before",
626
- "after",
627
- "of",
628
- "as",
629
- "on",
630
- // Control flow
631
- "then",
632
- "end",
633
- "else",
634
- "if",
635
- "repeat",
636
- "while",
637
- "for",
638
- // Commands (shouldn't be property names)
639
- "toggle",
640
- "add",
641
- "remove",
642
- "put",
643
- "set",
644
- "show",
645
- "hide",
646
- "increment",
647
- "decrement",
648
- "send",
649
- "trigger",
650
- "call"
651
- ]);
652
- return structural.has(value.toLowerCase());
653
- }
654
- /**
655
- * Try to match a method call expression like '#dialog.showModal()'.
656
- * Pattern: selector + '.' + identifier + '(' + [args] + ')'
657
- * Returns an expression value if matched, or null if not.
658
- */
659
- tryMatchMethodCallExpression(tokens) {
660
- const token = tokens.peek();
661
- if (!token || token.kind !== "selector") return null;
662
- const mark = tokens.mark();
663
- tokens.advance();
664
- const dotToken = tokens.peek();
665
- if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
666
- tokens.reset(mark);
667
- return null;
668
- }
669
- tokens.advance();
670
- const methodToken = tokens.peek();
671
- if (!methodToken || methodToken.kind !== "identifier") {
672
- tokens.reset(mark);
673
- return null;
674
- }
675
- tokens.advance();
676
- const openParen = tokens.peek();
677
- if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
678
- tokens.reset(mark);
679
- return null;
680
- }
681
- tokens.advance();
682
- const args = [];
683
- while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
684
- const argToken = tokens.peek();
685
- if (!argToken) break;
686
- if (argToken.kind === "punctuation" && argToken.value === ")") {
687
- tokens.advance();
688
- break;
689
- }
690
- if (argToken.kind === "punctuation" && argToken.value === ",") {
691
- tokens.advance();
692
- continue;
693
- }
694
- args.push(argToken.value);
695
- tokens.advance();
696
- }
697
- const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
698
- return {
699
- type: "expression",
700
- raw: methodCall
701
- };
702
- }
703
- /**
704
- * Try to match a property access expression like 'userData.name' or 'it.data'.
705
- * Pattern: (identifier | keyword) + '.' + identifier [+ '.' + identifier ...]
706
- * Returns an expression value if matched, or null if not.
707
- */
708
- tryMatchPropertyAccessExpression(tokens) {
709
- const token = tokens.peek();
710
- if (!token) return null;
711
- if (token.kind !== "identifier" && token.kind !== "keyword") return null;
712
- const mark = tokens.mark();
713
- tokens.advance();
714
- const dotToken = tokens.peek();
715
- if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
716
- tokens.reset(mark);
717
- return null;
718
- }
719
- tokens.advance();
720
- const propertyToken = tokens.peek();
721
- if (!propertyToken || propertyToken.kind !== "identifier") {
722
- tokens.reset(mark);
723
- return null;
724
- }
725
- tokens.advance();
726
- let chain = `${token.value}.${propertyToken.value}`;
727
- let depth = 1;
728
- while (!tokens.isAtEnd() && depth < _PatternMatcher.MAX_PROPERTY_DEPTH) {
729
- const nextDot = tokens.peek();
730
- if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
731
- break;
732
- }
733
- tokens.advance();
734
- const nextProp = tokens.peek();
735
- if (!nextProp || nextProp.kind !== "identifier") {
736
- break;
737
- }
738
- tokens.advance();
739
- chain += `.${nextProp.value}`;
740
- depth++;
741
- }
742
- const openParen = tokens.peek();
743
- if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
744
- tokens.advance();
745
- const args = [];
746
- let argDepth = 0;
747
- while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
748
- const argToken = tokens.peek();
749
- if (!argToken) break;
750
- if (argToken.kind === "punctuation" && argToken.value === ")") {
751
- if (argDepth === 0) {
752
- tokens.advance();
753
- break;
754
- }
755
- argDepth--;
756
- }
757
- if (argToken.kind === "punctuation" && argToken.value === "(") {
758
- argDepth++;
759
- }
760
- if (argToken.kind === "punctuation" && argToken.value === ",") {
761
- tokens.advance();
762
- continue;
763
- }
764
- args.push(argToken.value);
765
- tokens.advance();
766
- }
767
- const methodCall = `${chain}(${args.join(", ")})`;
768
- return {
769
- type: "expression",
770
- raw: methodCall
771
- };
772
- }
773
- return {
774
- type: "expression",
775
- raw: chain
776
- };
777
- }
778
- /**
779
- * Try to match a possessive selector expression like "#element's *opacity".
780
- * Pattern: selector + "'s" + (selector | identifier)
781
- * Returns a property-path value if matched, or null if not.
782
- */
783
- tryMatchPossessiveSelectorExpression(tokens) {
784
- const token = tokens.peek();
785
- if (!token || token.kind !== "selector") return null;
786
- const mark = tokens.mark();
787
- tokens.advance();
788
- const possessiveToken = tokens.peek();
789
- if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
790
- tokens.reset(mark);
791
- return null;
792
- }
793
- tokens.advance();
794
- const propertyToken = tokens.peek();
795
- if (!propertyToken) {
796
- tokens.reset(mark);
797
- return null;
798
- }
799
- if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
800
- tokens.reset(mark);
801
- return null;
802
- }
803
- tokens.advance();
804
- return createPropertyPath(createSelector(token.value), propertyToken.value);
805
- }
806
- /**
807
- * Try to match a selector + property expression like "#output.innerText".
808
- * This handles cases where the tokenizer produces two selector tokens:
809
- * - #output (id selector)
810
- * - .innerText (looks like class selector, but is actually property)
811
- *
812
- * Pattern: id-selector + class-selector-that-is-actually-property
813
- * Returns a property-path value if matched, or null if not.
814
- */
815
- tryMatchSelectorPropertyExpression(tokens) {
816
- const token = tokens.peek();
817
- if (!token || token.kind !== "selector") return null;
818
- if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
819
- const mark = tokens.mark();
820
- tokens.advance();
821
- const propertyToken = tokens.peek();
822
- if (!propertyToken || propertyToken.kind !== "selector") {
823
- tokens.reset(mark);
824
- return null;
825
- }
826
- if (!propertyToken.value.startsWith(".")) {
827
- tokens.reset(mark);
828
- return null;
829
- }
830
- const peek2 = tokens.peek(1);
831
- if (peek2 && peek2.kind === "selector") {
832
- }
833
- tokens.advance();
834
- const propertyName = propertyToken.value.slice(1);
835
- return createPropertyPath(createSelector(token.value), propertyName);
836
- }
837
- /**
838
- * Match a group pattern token (optional sequence).
839
- * When the group's leading marker isn't at the current position, scans ahead
840
- * up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
841
- * (e.g., a value like 'hello') to sit between groups without blocking later
842
- * marker matches.
843
- */
844
- matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
845
- const mark = tokens.mark();
846
- const capturedBefore = new Set(captured.keys());
847
- const success = this.matchTokenSequence(
848
- tokens,
849
- patternToken.tokens,
850
- captured,
851
- parentStopMarkers
852
- );
853
- if (success) return true;
854
- tokens.reset(mark);
855
- for (const role of captured.keys()) {
856
- if (!capturedBefore.has(role)) captured.delete(role);
857
- }
858
- if (!patternToken.optional) return false;
859
- const leadingMarker = this.getGroupLeadingMarker(patternToken);
860
- if (leadingMarker) {
861
- for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
862
- const ahead = tokens.peek(offset);
863
- if (!ahead) break;
864
- const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
865
- if (aheadValue === leadingMarker) {
866
- this.logger.debug(
867
- " >> MARKER SCAN: found",
868
- leadingMarker,
869
- "at offset",
870
- offset,
871
- "- skipping intervening tokens"
872
- );
873
- for (let s = 0; s < offset; s++) tokens.advance();
874
- const retrySuccess = this.matchTokenSequence(
875
- tokens,
876
- patternToken.tokens,
877
- captured,
878
- parentStopMarkers
879
- );
880
- if (retrySuccess) return true;
881
- tokens.reset(mark);
882
- for (const role of captured.keys()) {
883
- if (!capturedBefore.has(role)) captured.delete(role);
884
- }
885
- break;
1009
+ if (!patternToken.optional) return false;
1010
+ const leadingMarker = this.getGroupLeadingMarker(patternToken);
1011
+ if (leadingMarker) {
1012
+ for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
1013
+ const ahead = tokens.peek(offset);
1014
+ if (!ahead) break;
1015
+ const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
1016
+ if (aheadValue === leadingMarker) {
1017
+ this.logger.debug(
1018
+ " >> MARKER SCAN: found",
1019
+ leadingMarker,
1020
+ "at offset",
1021
+ offset,
1022
+ "- skipping intervening tokens"
1023
+ );
1024
+ for (let s = 0; s < offset; s++) tokens.advance();
1025
+ const retrySuccess = this.matchTokenSequence(
1026
+ tokens,
1027
+ patternToken.tokens,
1028
+ captured,
1029
+ parentStopMarkers
1030
+ );
1031
+ if (retrySuccess) return true;
1032
+ tokens.reset(mark);
1033
+ for (const role of captured.keys()) {
1034
+ if (!capturedBefore.has(role)) captured.delete(role);
1035
+ }
1036
+ break;
886
1037
  }
887
1038
  }
888
1039
  }
@@ -909,7 +1060,7 @@ var _PatternMatcher = class _PatternMatcher {
909
1060
  if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
910
1061
  return "stem";
911
1062
  }
912
- if (token.kind === "keyword" && this.safeToLowerCase(token.value) === value.toLowerCase()) {
1063
+ if (token.kind === "keyword" && safeToLowerCase(token.value) === value.toLowerCase()) {
913
1064
  return "case-insensitive";
914
1065
  }
915
1066
  return "none";
@@ -957,62 +1108,9 @@ var _PatternMatcher = class _PatternMatcher {
957
1108
  if (token.normalized && stopMarkers.has(token.normalized.toLowerCase())) return true;
958
1109
  return false;
959
1110
  }
960
- /**
961
- * Convert a language token to a semantic value.
962
- */
963
- tokenToSemanticValue(token) {
964
- switch (token.kind) {
965
- case "selector":
966
- return createSelector(token.value);
967
- case "literal":
968
- return this.parseLiteralValue(token.value);
969
- case "keyword":
970
- const tokenValue = token.normalized || token.value;
971
- const lower = this.safeToLowerCase(tokenValue);
972
- if (isValidReference(lower)) {
973
- return createReference(lower);
974
- }
975
- return createLiteral(token.normalized || token.value);
976
- case "identifier":
977
- if (typeof token.value === "string" && token.value.startsWith(":")) {
978
- return createReference(token.value);
979
- }
980
- const identLower = this.safeToLowerCase(token.value);
981
- if (isValidReference(identLower)) {
982
- return createReference(identLower);
983
- }
984
- return { type: "expression", raw: token.value };
985
- case "url":
986
- return createLiteral(token.value, "string");
987
- default:
988
- return null;
989
- }
990
- }
991
- /**
992
- * Parse a literal value (string, number, boolean).
993
- */
994
- parseLiteralValue(value) {
995
- if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
996
- const inner = value.slice(1, -1);
997
- return createLiteral(inner, "string");
998
- }
999
- if (value === "true") return createLiteral(true, "boolean");
1000
- if (value === "false") return createLiteral(false, "boolean");
1001
- const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
1002
- if (durationMatch) {
1003
- const num2 = parseFloat(durationMatch[1]);
1004
- const unit = durationMatch[2];
1005
- if (unit) {
1006
- return createLiteral(value, "duration");
1007
- }
1008
- return createLiteral(num2, "number");
1009
- }
1010
- const num = parseFloat(value);
1011
- if (!isNaN(num)) {
1012
- return createLiteral(num, "number");
1013
- }
1014
- return createLiteral(value, "string");
1015
- }
1111
+ // ==========================================================================
1112
+ // Depth Limits for Expression Parsing (security hardening)
1113
+ // ==========================================================================
1016
1114
  /**
1017
1115
  * Apply extraction rules to fill in static values and defaults for missing roles.
1018
1116
  */
@@ -1033,260 +1131,644 @@ var _PatternMatcher = class _PatternMatcher {
1033
1131
  isOptional(patternToken) {
1034
1132
  return patternToken.type !== "literal" && patternToken.optional === true;
1035
1133
  }
1134
+ // ===========================================================================
1135
+ // English Idiom Support - Noise Word Handling
1136
+ // ===========================================================================
1036
1137
  /**
1037
- * Calculate confidence score for a match (0-1).
1038
- *
1039
- * Confidence is reduced for:
1040
- * - Stem matches (morphological normalization has inherent uncertainty)
1041
- * - Missing optional roles (but less penalty if role has a default value)
1138
+ * Extract event modifiers from the token stream.
1139
+ * Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
1140
+ * that can appear after event names.
1042
1141
  *
1043
- * Confidence is increased for:
1044
- * - VSO languages (Arabic) when pattern starts with a verb
1142
+ * Returns EventModifiers object or undefined if no modifiers found.
1045
1143
  */
1046
- calculateConfidence(pattern, captured) {
1047
- let score = 0;
1048
- let maxScore = 0;
1049
- const hasDefault = (role) => {
1050
- return pattern.extraction?.[role]?.default !== void 0;
1051
- };
1052
- for (const token of pattern.template.tokens) {
1053
- if (token.type === "role") {
1054
- maxScore += 1;
1055
- if (captured.has(token.role)) {
1056
- score += 1;
1057
- }
1058
- } else if (token.type === "group") {
1059
- for (const subToken of token.tokens) {
1060
- if (subToken.type === "role") {
1061
- const roleHasDefault = hasDefault(subToken.role);
1062
- const weight = 0.8;
1063
- maxScore += weight;
1064
- if (captured.has(subToken.role)) {
1065
- score += weight;
1066
- } else if (roleHasDefault) {
1067
- score += weight * 0.6;
1068
- }
1144
+ extractEventModifiers(tokens) {
1145
+ const modifiers = {};
1146
+ let foundModifier = false;
1147
+ while (!tokens.isAtEnd()) {
1148
+ const token = tokens.peek();
1149
+ if (!token || token.kind !== "event-modifier") {
1150
+ break;
1151
+ }
1152
+ const metadata = token.metadata;
1153
+ if (!metadata) {
1154
+ break;
1155
+ }
1156
+ foundModifier = true;
1157
+ switch (metadata.modifierName) {
1158
+ case "once":
1159
+ modifiers.once = true;
1160
+ break;
1161
+ case "debounce":
1162
+ if (typeof metadata.value === "number") {
1163
+ modifiers.debounce = metadata.value;
1069
1164
  }
1070
- }
1165
+ break;
1166
+ case "throttle":
1167
+ if (typeof metadata.value === "number") {
1168
+ modifiers.throttle = metadata.value;
1169
+ }
1170
+ break;
1171
+ case "queue":
1172
+ if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
1173
+ modifiers.queue = metadata.value;
1174
+ }
1175
+ break;
1071
1176
  }
1177
+ tokens.advance();
1072
1178
  }
1073
- let baseConfidence = maxScore > 0 ? score / maxScore : 1;
1074
- if (this.stemMatchCount > 0 && this.totalKeywordMatches > 0) {
1075
- const stemPenalty = this.stemMatchCount / this.totalKeywordMatches * 0.15;
1076
- baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
1077
- }
1078
- const vsoBoost = this.calculateVSOConfidenceBoost(pattern);
1079
- baseConfidence = Math.min(1, baseConfidence + vsoBoost);
1080
- const prepositionAdjustment = this.arabicPrepositionDisambiguation(pattern, captured);
1081
- baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
1082
- return baseConfidence;
1179
+ return foundModifier ? modifiers : void 0;
1083
1180
  }
1084
- /**
1085
- * Calculate confidence boost for VSO (Verb-Subject-Object) language patterns.
1086
- * Arabic naturally uses VSO word order, so patterns that start with a verb
1087
- * should receive a confidence boost.
1088
- *
1089
- * Returns +0.15 confidence boost if:
1090
- * - Language is Arabic ('ar')
1091
- * - Pattern's first token is a verb keyword
1092
- *
1093
- * @param pattern The language pattern being matched
1094
- * @returns Confidence boost (0 or 0.15)
1095
- */
1096
- calculateVSOConfidenceBoost(pattern) {
1097
- if (pattern.language !== "ar") {
1098
- return 0;
1099
- }
1100
- const firstToken = pattern.template.tokens[0];
1101
- if (!firstToken || firstToken.type !== "literal") {
1102
- return 0;
1103
- }
1104
- const ARABIC_VERBS = /* @__PURE__ */ new Set([
1105
- "\u0628\u062F\u0644",
1106
- "\u063A\u064A\u0631",
1107
- "\u0623\u0636\u0641",
1108
- "\u0623\u0632\u0644",
1109
- "\u0636\u0639",
1110
- "\u0627\u062C\u0639\u0644",
1111
- "\u0639\u064A\u0646",
1112
- "\u0632\u062F",
1113
- "\u0627\u0646\u0642\u0635",
1114
- "\u0633\u062C\u0644",
1115
- "\u0623\u0638\u0647\u0631",
1116
- "\u0623\u062E\u0641",
1117
- "\u0634\u063A\u0644",
1118
- "\u0623\u0631\u0633\u0644",
1119
- "\u0631\u0643\u0632",
1120
- "\u0634\u0648\u0634",
1121
- "\u062A\u0648\u0642\u0641",
1122
- "\u0627\u0646\u0633\u062E",
1123
- "\u0627\u062D\u0630\u0641",
1124
- "\u0627\u0635\u0646\u0639",
1125
- "\u0627\u0646\u062A\u0638\u0631",
1126
- "\u0627\u0646\u062A\u0642\u0627\u0644",
1127
- "\u0623\u0648"
1128
- ]);
1129
- if (ARABIC_VERBS.has(firstToken.value)) {
1130
- return 0.15;
1131
- }
1132
- if (firstToken.alternatives) {
1133
- for (const alt of firstToken.alternatives) {
1134
- if (ARABIC_VERBS.has(alt)) {
1135
- return 0.15;
1136
- }
1137
- }
1181
+ };
1182
+ /** Maximum tokens to scan ahead when looking for a group's leading marker */
1183
+ _PatternMatcher.MAX_MARKER_SCAN = 3;
1184
+ var PatternMatcher = _PatternMatcher;
1185
+ var patternMatcher = new PatternMatcher();
1186
+ function matchPattern(tokens, pattern) {
1187
+ return patternMatcher.matchPattern(tokens, pattern);
1188
+ }
1189
+ function matchBest(tokens, patterns) {
1190
+ return patternMatcher.matchBest(tokens, patterns);
1191
+ }
1192
+
1193
+ // src/core/tokenization/token-utils.ts
1194
+ var TokenStreamImpl = class {
1195
+ constructor(tokens, language) {
1196
+ this.pos = 0;
1197
+ this.tokens = tokens;
1198
+ this.language = language;
1199
+ }
1200
+ peek(offset = 0) {
1201
+ const index = this.pos + offset;
1202
+ if (index < 0 || index >= this.tokens.length) {
1203
+ return null;
1138
1204
  }
1139
- return 0;
1205
+ return this.tokens[index];
1206
+ }
1207
+ advance() {
1208
+ if (this.isAtEnd()) {
1209
+ throw new Error("Unexpected end of token stream");
1210
+ }
1211
+ return this.tokens[this.pos++];
1212
+ }
1213
+ isAtEnd() {
1214
+ return this.pos >= this.tokens.length;
1215
+ }
1216
+ mark() {
1217
+ return { position: this.pos };
1218
+ }
1219
+ reset(mark) {
1220
+ this.pos = mark.position;
1221
+ }
1222
+ position() {
1223
+ return this.pos;
1140
1224
  }
1141
1225
  /**
1142
- * Arabic preposition disambiguation for confidence adjustment.
1143
- *
1144
- * Different Arabic prepositions are more or less natural for different semantic roles:
1145
- * - على (on/upon) is preferred for patient/target roles (element selectors)
1146
- * - إلى (to) is preferred for destination roles
1147
- * - من (from) is preferred for source roles
1148
- * - في (in) is preferred for location roles
1149
- *
1150
- * This method analyzes the prepositions used with captured semantic roles and
1151
- * adjusts confidence based on idiomaticity:
1152
- * - +0.10 for highly idiomatic preposition choices
1153
- * - -0.10 for less natural preposition choices
1154
- *
1155
- * @param pattern The language pattern being matched
1156
- * @param captured The captured semantic values
1157
- * @returns Confidence adjustment (-0.10 to +0.10)
1226
+ * Get remaining tokens as an array.
1227
+ */
1228
+ remaining() {
1229
+ return this.tokens.slice(this.pos);
1230
+ }
1231
+ /**
1232
+ * Consume tokens while predicate is true.
1233
+ */
1234
+ takeWhile(predicate) {
1235
+ const result = [];
1236
+ while (!this.isAtEnd() && predicate(this.peek())) {
1237
+ result.push(this.advance());
1238
+ }
1239
+ return result;
1240
+ }
1241
+ /**
1242
+ * Skip tokens while predicate is true.
1158
1243
  */
1159
- arabicPrepositionDisambiguation(pattern, captured) {
1160
- if (pattern.language !== "ar") {
1161
- return 0;
1162
- }
1163
- let adjustment = 0;
1164
- const PREFERRED_PREPOSITIONS = {
1165
- patient: ["\u0639\u0644\u0649"],
1166
- // element selectors prefer على (on/upon)
1167
- destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1168
- // destination prefers إلى (to)
1169
- source: ["\u0645\u0646"],
1170
- // source prefers من (from)
1171
- agent: ["\u0645\u0646"],
1172
- // agent/by prefers من (from/by)
1173
- manner: ["\u0628"],
1174
- // manner prefers ب (with/by)
1175
- style: ["\u0628"],
1176
- // style prefers ب (with)
1177
- goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1178
- // target state prefers إلى (to)
1179
- method: ["\u0628"]
1180
- // method prefers ب (with/by)
1244
+ skipWhile(predicate) {
1245
+ while (!this.isAtEnd() && predicate(this.peek())) {
1246
+ this.advance();
1247
+ }
1248
+ }
1249
+ };
1250
+ function createPosition(start, end) {
1251
+ return { start, end };
1252
+ }
1253
+ function createToken(valueOrParams, kind, position, normalizedOrOptions) {
1254
+ if (typeof valueOrParams === "object") {
1255
+ const { value: value2, kind: kind2, position: position2, normalized: normalized2, stem, stemConfidence, metadata } = valueOrParams;
1256
+ return {
1257
+ value: value2,
1258
+ kind: kind2,
1259
+ position: position2,
1260
+ ...normalized2 !== void 0 && { normalized: normalized2 },
1261
+ ...stem !== void 0 && { stem },
1262
+ ...stemConfidence !== void 0 && { stemConfidence },
1263
+ ...metadata !== void 0 && { metadata }
1181
1264
  };
1182
- for (const [role, value] of captured.entries()) {
1183
- const preferred = PREFERRED_PREPOSITIONS[role];
1184
- if (!preferred || preferred.length === 0) {
1185
- continue;
1265
+ }
1266
+ const value = valueOrParams;
1267
+ if (!kind || !position) {
1268
+ throw new Error("createToken requires kind and position parameters");
1269
+ }
1270
+ if (typeof normalizedOrOptions === "string") {
1271
+ return { value, kind, position, normalized: normalizedOrOptions };
1272
+ }
1273
+ if (normalizedOrOptions) {
1274
+ const { normalized: normalized2, stem, stemConfidence, metadata } = normalizedOrOptions;
1275
+ return {
1276
+ value,
1277
+ kind,
1278
+ position,
1279
+ ...normalized2 !== void 0 && { normalized: normalized2 },
1280
+ ...stem !== void 0 && { stem },
1281
+ ...stemConfidence !== void 0 && { stemConfidence },
1282
+ ...metadata !== void 0 && { metadata }
1283
+ };
1284
+ }
1285
+ return { value, kind, position };
1286
+ }
1287
+ function isWhitespace(char) {
1288
+ return /\s/.test(char);
1289
+ }
1290
+ function isSelectorStart(char) {
1291
+ return char === "#" || char === "." || char === "[" || char === "@" || char === "*" || char === "<";
1292
+ }
1293
+ function isQuote(char) {
1294
+ return char === '"' || char === "'" || char === "`" || char === "\u300C" || char === "\u300D";
1295
+ }
1296
+ function isDigit(char) {
1297
+ return /\d/.test(char);
1298
+ }
1299
+ function stripOptionalDiacritics(word) {
1300
+ return word.replace(/[\u064b-\u0652\u0670]/g, "");
1301
+ }
1302
+ function isAsciiLetter(char) {
1303
+ return /[a-zA-Z]/.test(char);
1304
+ }
1305
+ function isAsciiIdentifierChar(char) {
1306
+ return /[a-zA-Z0-9_-]/.test(char);
1307
+ }
1308
+
1309
+ // src/core/matching/match-skeleton.ts
1310
+ function groupHasRole(group) {
1311
+ return group.tokens.some((t) => t.type === "role" || t.type === "group" && groupHasRole(t));
1312
+ }
1313
+ var MatchSkeleton = class {
1314
+ /**
1315
+ * @param makeReader - builds the grammar's reader, handed this matcher's view
1316
+ * of the match in progress (one reader per matcher).
1317
+ */
1318
+ constructor(makeReader, confidenceModel) {
1319
+ /**
1320
+ * The pattern tokens still to match after the one being matched, through
1321
+ * every enclosing group: a value stops at any marker the pattern still owes.
1322
+ */
1323
+ this.patternRest = [];
1324
+ /**
1325
+ * Whether the role slot being matched directly follows a literal the pattern
1326
+ * matched: its marker (`[por {quantity}]`), or the command's verb.
1327
+ */
1328
+ this.slotAfterLiteral = false;
1329
+ /**
1330
+ * Selective memoization cache (Phase 6.1).
1331
+ * Caches pattern match results keyed by `tokenPosition:patternId`.
1332
+ * Cleared before each top-level matchBest() call.
1333
+ */
1334
+ this.matchCache = /* @__PURE__ */ new Map();
1335
+ /**
1336
+ * Stream index where each role's capture began, for the current
1337
+ * `matchPattern` call. Rebuilt per call, written on the same success path as
1338
+ * `captured` — see `PatternMatchResult.roleStarts`.
1339
+ */
1340
+ this.roleStarts = /* @__PURE__ */ new Map();
1341
+ /**
1342
+ * Track stem matches for confidence calculation.
1343
+ * This is set during matching and read during confidence calculation.
1344
+ */
1345
+ this.stemMatchCount = 0;
1346
+ this.totalKeywordMatches = 0;
1347
+ this.confidenceModel = confidenceModel;
1348
+ this.reader = makeReader({
1349
+ patternRest: () => this.patternRest,
1350
+ slotAfterLiteral: () => this.slotAfterLiteral,
1351
+ wouldMatch: (pt, token) => this.patternTokenWouldMatch(pt, token)
1352
+ });
1353
+ }
1354
+ /**
1355
+ * Try to match a single pattern against the token stream.
1356
+ * Returns the match result or null if no match.
1357
+ */
1358
+ matchPattern(tokens, pattern) {
1359
+ const mark = tokens.mark();
1360
+ const captured = /* @__PURE__ */ new Map();
1361
+ this.reader.beginPattern(pattern);
1362
+ this.stemMatchCount = 0;
1363
+ this.totalKeywordMatches = 0;
1364
+ this.roleStarts = /* @__PURE__ */ new Map();
1365
+ this.patternRest = [];
1366
+ const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
1367
+ if (!success) {
1368
+ tokens.reset(mark);
1369
+ return null;
1370
+ }
1371
+ const confidence = this.confidenceModel.calculate({
1372
+ pattern,
1373
+ captured,
1374
+ stemMatchCount: this.stemMatchCount,
1375
+ totalKeywordMatches: this.totalKeywordMatches
1376
+ });
1377
+ this.applyExtractionRules(pattern, captured);
1378
+ return {
1379
+ pattern,
1380
+ captured,
1381
+ consumedTokens: tokens.position() - mark.position,
1382
+ confidence,
1383
+ roleStarts: this.roleStarts,
1384
+ ...this.reader.resultFields?.()
1385
+ };
1386
+ }
1387
+ /**
1388
+ * Try to match multiple patterns, return the best match.
1389
+ */
1390
+ matchBest(tokens, patterns) {
1391
+ this.matchCache.clear();
1392
+ const matches = [];
1393
+ const startPos = tokens.position();
1394
+ for (const pattern of patterns) {
1395
+ const mark = tokens.mark();
1396
+ const cacheKey = `${startPos}:${pattern.id}`;
1397
+ let result;
1398
+ if (this.matchCache.has(cacheKey)) {
1399
+ result = this.matchCache.get(cacheKey);
1400
+ } else {
1401
+ result = this.matchPattern(tokens, pattern);
1402
+ this.matchCache.set(cacheKey, result);
1186
1403
  }
1187
- const metadata = "metadata" in value ? value.metadata : void 0;
1188
- if (metadata && typeof metadata.prepositionValue === "string") {
1189
- const usedPreposition = metadata.prepositionValue;
1190
- if (preferred.includes(usedPreposition)) {
1191
- adjustment += 0.1;
1192
- } else {
1193
- adjustment -= 0.1;
1404
+ if (result) {
1405
+ matches.push(result);
1406
+ }
1407
+ tokens.reset(mark);
1408
+ }
1409
+ if (matches.length === 0) {
1410
+ return null;
1411
+ }
1412
+ matches.sort((a, b) => {
1413
+ const priorityDiff = b.pattern.priority - a.pattern.priority;
1414
+ if (priorityDiff !== 0) return priorityDiff;
1415
+ return b.confidence - a.confidence;
1416
+ });
1417
+ const best = matches[0];
1418
+ this.matchPattern(tokens, best.pattern);
1419
+ return best;
1420
+ }
1421
+ /**
1422
+ * Match a sequence of pattern tokens against the token stream.
1423
+ */
1424
+ matchTokenSequence(tokens, patternTokens, captured, nextAfterSequence) {
1425
+ this.reader.skipLeading?.(tokens, patternTokens);
1426
+ for (let i = 0; i < patternTokens.length; i++) {
1427
+ const patternToken = patternTokens[i];
1428
+ this.reader.beforePatternToken?.(tokens, patternTokens, i, captured);
1429
+ if (this.reader.maySkipSlot(
1430
+ patternToken,
1431
+ tokens.peek(),
1432
+ patternTokens[i + 1] ?? nextAfterSequence
1433
+ )) {
1434
+ const skipMark = tokens.mark();
1435
+ const speculative = new Map(captured);
1436
+ if (this.matchTokenSequence(
1437
+ tokens,
1438
+ patternTokens.slice(i + 1),
1439
+ speculative,
1440
+ nextAfterSequence
1441
+ ) && this.reader.atClauseEnd(tokens)) {
1442
+ captured.clear();
1443
+ for (const [role, value] of speculative) captured.set(role, value);
1444
+ return true;
1194
1445
  }
1446
+ tokens.reset(skipMark);
1447
+ }
1448
+ const outerRest = this.patternRest;
1449
+ this.patternRest = [...patternTokens.slice(i + 1), ...outerRest];
1450
+ this.slotAfterLiteral = patternToken.type === "role" && patternTokens[i - 1]?.type === "literal";
1451
+ const matched = this.matchPatternToken(
1452
+ tokens,
1453
+ patternToken,
1454
+ captured,
1455
+ patternTokens[i + 1] ?? nextAfterSequence
1456
+ );
1457
+ this.patternRest = outerRest;
1458
+ if (!matched) {
1459
+ if (this.isOptional(patternToken)) {
1460
+ continue;
1461
+ }
1462
+ return false;
1195
1463
  }
1196
1464
  }
1197
- return Math.max(-0.1, Math.min(0.1, adjustment));
1465
+ return true;
1198
1466
  }
1199
1467
  /**
1200
- * Skip noise words like "the" before selectors.
1201
- * This enables more natural English syntax like "toggle the .active".
1468
+ * Match a single pattern token against the current position in the stream.
1202
1469
  */
1203
- skipNoiseWords(tokens) {
1470
+ matchPatternToken(tokens, patternToken, captured, nextPatternToken) {
1471
+ switch (patternToken.type) {
1472
+ case "literal":
1473
+ return this.matchLiteralToken(tokens, patternToken);
1474
+ case "role": {
1475
+ const roleStart = tokens.position();
1476
+ const matchedRole = this.reader.readRole(tokens, patternToken, captured, nextPatternToken);
1477
+ if (matchedRole && captured.has(patternToken.role)) {
1478
+ this.roleStarts.set(patternToken.role, roleStart);
1479
+ }
1480
+ return matchedRole;
1481
+ }
1482
+ case "group":
1483
+ return this.matchGroupToken(tokens, patternToken, captured, nextPatternToken);
1484
+ default:
1485
+ return false;
1486
+ }
1487
+ }
1488
+ /**
1489
+ * Match a literal pattern token (keyword or particle).
1490
+ */
1491
+ matchLiteralToken(tokens, patternToken) {
1204
1492
  const token = tokens.peek();
1205
- if (!token) return;
1206
- const tokenLower = this.safeToLowerCase(token.value);
1207
- if (_PatternMatcher.ENGLISH_NOISE_WORDS.has(tokenLower)) {
1208
- const mark = tokens.mark();
1493
+ if (!token) return false;
1494
+ const matchType = this.getMatchType(token, patternToken.value);
1495
+ if (matchType !== "none") {
1496
+ this.totalKeywordMatches++;
1497
+ if (matchType === "stem") {
1498
+ this.stemMatchCount++;
1499
+ }
1209
1500
  tokens.advance();
1210
- const nextToken = tokens.peek();
1211
- if (nextToken && nextToken.kind === "selector") {
1212
- return;
1501
+ return true;
1502
+ }
1503
+ if (patternToken.alternatives) {
1504
+ for (const alt of patternToken.alternatives) {
1505
+ const altMatchType = this.getMatchType(token, alt);
1506
+ if (altMatchType !== "none") {
1507
+ this.totalKeywordMatches++;
1508
+ if (altMatchType === "stem") {
1509
+ this.stemMatchCount++;
1510
+ }
1511
+ tokens.advance();
1512
+ return true;
1513
+ }
1514
+ }
1515
+ }
1516
+ for (const value of [patternToken.value, ...patternToken.alternatives ?? []]) {
1517
+ for (const sep of ["-", "_"]) {
1518
+ if (value.includes(sep) && this.tryMatchShatteredCompound(tokens, value, sep)) {
1519
+ this.totalKeywordMatches++;
1520
+ return true;
1521
+ }
1213
1522
  }
1214
- tokens.reset(mark);
1215
1523
  }
1216
- if (tokenLower === "class") {
1524
+ return false;
1525
+ }
1526
+ /**
1527
+ * Match a compound keyword (`titik-henti`, `по_умолчанию`) against a
1528
+ * shattered token run (`titik` `-` `henti` / `по` `_` `умолчанию`): word
1529
+ * segments must be identifier/keyword/particle tokens equal to the expected
1530
+ * segments (case-insensitive; particle because prepositions like ru `по` /
1531
+ * uk `за` lead some underscore compounds), separators lone `sep` tokens.
1532
+ * Consumes the run on success, resets the stream on failure.
1533
+ */
1534
+ tryMatchShatteredCompound(tokens, expected, sep) {
1535
+ const segments = expected.split(sep);
1536
+ if (segments.length < 2 || segments.some((s) => s.length === 0)) return false;
1537
+ const mark = tokens.mark();
1538
+ for (let i = 0; i < segments.length; i++) {
1539
+ if (i > 0) {
1540
+ const sepToken = tokens.peek();
1541
+ if (!sepToken || sepToken.value !== sep) {
1542
+ tokens.reset(mark);
1543
+ return false;
1544
+ }
1545
+ tokens.advance();
1546
+ }
1547
+ const word = tokens.peek();
1548
+ if (!word || word.kind !== "identifier" && word.kind !== "keyword" && word.kind !== "particle" || word.value.toLowerCase() !== segments[i].toLowerCase()) {
1549
+ tokens.reset(mark);
1550
+ return false;
1551
+ }
1217
1552
  tokens.advance();
1218
1553
  }
1554
+ return true;
1555
+ }
1556
+ /**
1557
+ * Match a group pattern token (optional sequence).
1558
+ */
1559
+ matchGroupToken(tokens, patternToken, captured, nextPatternToken) {
1560
+ const mark = tokens.mark();
1561
+ const capturedBefore = new Map(captured);
1562
+ let success = this.matchTokenSequence(tokens, patternToken.tokens, captured, nextPatternToken);
1563
+ if (success && tokens.position() > mark.position && groupHasRole(patternToken) && [...captured].every(([role, value]) => capturedBefore.get(role) === value) && captured.size === capturedBefore.size) {
1564
+ success = false;
1565
+ }
1566
+ if (success) {
1567
+ const markerLiterals = [];
1568
+ for (const inner of patternToken.tokens) {
1569
+ if (inner.type === "group" && inner.renderRequired) {
1570
+ for (const t of inner.tokens) {
1571
+ if (t.type === "literal") markerLiterals.push(t.value, ...t.alternatives ?? []);
1572
+ }
1573
+ }
1574
+ }
1575
+ if (markerLiterals.length > 0) {
1576
+ const addedRole = [...captured.keys()].some((role) => !capturedBefore.has(role));
1577
+ if (addedRole) {
1578
+ const last = tokens.tokens[tokens.position() - 1];
1579
+ const markerSeen = last !== void 0 && tokens.position() > mark.position && markerLiterals.some((lit) => this.getMatchType(last, lit) !== "none");
1580
+ if (!markerSeen) success = false;
1581
+ }
1582
+ }
1583
+ }
1584
+ if (!success) {
1585
+ tokens.reset(mark);
1586
+ for (const role of captured.keys()) {
1587
+ if (!capturedBefore.has(role)) {
1588
+ captured.delete(role);
1589
+ }
1590
+ }
1591
+ for (const [role, value] of capturedBefore) {
1592
+ if (captured.get(role) !== value) {
1593
+ captured.set(role, value);
1594
+ }
1595
+ }
1596
+ return patternToken.optional || false;
1597
+ }
1598
+ return true;
1599
+ }
1600
+ /**
1601
+ * Whether a pattern token (literal, or a group starting with a literal) would
1602
+ * match the given stream token. Used to keep the event-head source-clause
1603
+ * consumption from stealing a marker the pattern explicitly expects.
1604
+ */
1605
+ patternTokenWouldMatch(pt, token) {
1606
+ if (!pt) return false;
1607
+ if (pt.type === "literal") {
1608
+ if (this.getMatchType(token, pt.value) !== "none") return true;
1609
+ return !!pt.alternatives?.some((a) => this.getMatchType(token, a) !== "none");
1610
+ }
1611
+ if (pt.type === "group") {
1612
+ return this.patternTokenWouldMatch(pt.tokens?.[0], token);
1613
+ }
1614
+ return false;
1615
+ }
1616
+ /**
1617
+ * Get the type of match for a token against a value.
1618
+ * Used for confidence calculation.
1619
+ */
1620
+ getMatchType(token, value) {
1621
+ if (token.value === value) return "exact";
1622
+ if (token.normalized === value) return "normalized";
1623
+ if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
1624
+ return "stem";
1625
+ }
1626
+ if (token.kind === "keyword" && token.value.toLowerCase() === value.toLowerCase()) {
1627
+ return "case-insensitive";
1628
+ }
1629
+ if (token.kind === "keyword") {
1630
+ const strippedToken = stripOptionalDiacritics(token.value).toLowerCase();
1631
+ if (strippedToken && strippedToken === stripOptionalDiacritics(value).toLowerCase()) {
1632
+ return "case-insensitive";
1633
+ }
1634
+ }
1635
+ return "none";
1636
+ }
1637
+ /**
1638
+ * Apply extraction rules to fill in static values and defaults for missing
1639
+ * roles, and re-shape captured roles that declare a `transform` (the
1640
+ * ExtractionRule field was documented but unwired — go-qu-url-dest is the
1641
+ * first consumer: it re-types the marker-bound capture to the expression
1642
+ * shape the en reference produces).
1643
+ */
1644
+ applyExtractionRules(pattern, captured) {
1645
+ for (const [role, rule] of Object.entries(pattern.extraction)) {
1646
+ const existing = captured.get(role);
1647
+ if (existing) {
1648
+ if (rule.transform) {
1649
+ const raw = existing.type === "literal" ? String(existing.value) : String(existing.raw ?? "");
1650
+ captured.set(role, rule.transform(raw));
1651
+ }
1652
+ } else if (rule.value !== void 0) {
1653
+ captured.set(
1654
+ role,
1655
+ rule.valueIsExpression ? { type: "expression", raw: rule.value } : { type: "literal", value: rule.value }
1656
+ );
1657
+ } else if (rule.default) {
1658
+ const implicitDefault = {
1659
+ ...rule.default,
1660
+ implicit: true
1661
+ };
1662
+ captured.set(role, implicitDefault);
1663
+ }
1664
+ }
1665
+ }
1666
+ /**
1667
+ * Check if a pattern token is optional.
1668
+ */
1669
+ isOptional(patternToken) {
1670
+ return patternToken.type !== "literal" && patternToken.optional === true;
1671
+ }
1672
+ };
1673
+
1674
+ // src/core/matching/stock-reader.ts
1675
+ function greedyStopMarkers(rest) {
1676
+ const markers = /* @__PURE__ */ new Set();
1677
+ const add = (pt) => {
1678
+ markers.add(pt.value.toLowerCase());
1679
+ for (const alt of pt.alternatives ?? []) markers.add(alt.toLowerCase());
1680
+ };
1681
+ for (const pt of rest) {
1682
+ if (pt.type === "literal") {
1683
+ add(pt);
1684
+ break;
1685
+ }
1686
+ if (pt.type === "group") {
1687
+ const lead = pt.tokens.find((t) => t.type === "literal");
1688
+ if (lead?.type === "literal") add(lead);
1689
+ if (!pt.optional) break;
1690
+ }
1691
+ }
1692
+ return markers;
1693
+ }
1694
+ function isStopMarker(token, stopMarkers) {
1695
+ if (stopMarkers.size === 0) return false;
1696
+ if (stopMarkers.has(token.value.toLowerCase())) return true;
1697
+ return !!token.normalized && stopMarkers.has(token.normalized.toLowerCase());
1698
+ }
1699
+ function readGreedy(tokens, stopMarkers) {
1700
+ const values = [];
1701
+ while (!tokens.isAtEnd()) {
1702
+ const next = tokens.peek();
1703
+ if (!next || isStopMarker(next, stopMarkers)) break;
1704
+ values.push(next.value);
1705
+ tokens.advance();
1706
+ }
1707
+ return values.length > 0 ? { type: "expression", raw: values.join(" ") } : null;
1708
+ }
1709
+ function isListedWord(token, words) {
1710
+ if (!token) return false;
1711
+ const forms = [token.value, token.normalized].filter((f) => typeof f === "string");
1712
+ return forms.some((form) => words.some((word) => word.toLowerCase() === form.toLowerCase()));
1713
+ }
1714
+ var StockValueReader = class {
1715
+ constructor(view, profiles = {}) {
1716
+ this.view = view;
1717
+ this.profiles = profiles;
1718
+ }
1719
+ beginPattern(pattern) {
1720
+ this.profile = this.profiles[pattern.language] ?? { code: pattern.language };
1721
+ this.lastOptional = void 0;
1722
+ }
1723
+ readRole(tokens, slot, captured, next) {
1724
+ const start = tokens.mark();
1725
+ if (this.readOnce(tokens, slot, captured, next, true)) {
1726
+ if (!slot.optional) {
1727
+ this.lastOptional = void 0;
1728
+ } else if (captured.has(slot.role) && tokens.position() > start.position && !this.view.slotAfterLiteral() && next?.type !== "literal") {
1729
+ this.lastOptional = { role: slot.role, start, end: tokens.position() };
1730
+ }
1731
+ return true;
1732
+ }
1733
+ const before = this.lastOptional;
1734
+ if (slot.optional || !before || before.end !== tokens.position()) return false;
1735
+ this.lastOptional = void 0;
1736
+ tokens.reset(before.start);
1737
+ captured.delete(before.role);
1738
+ return this.readOnce(tokens, slot, captured, next, false);
1219
1739
  }
1220
- /**
1221
- * Extract event modifiers from the token stream.
1222
- * Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
1223
- * that can appear after event names.
1224
- *
1225
- * Returns EventModifiers object or undefined if no modifiers found.
1226
- */
1227
- extractEventModifiers(tokens) {
1228
- const modifiers = {};
1229
- let foundModifier = false;
1230
- while (!tokens.isAtEnd()) {
1740
+ /** One read of `slot`; `guard` refuses a required slot the marker the pattern wants next. */
1741
+ readOnce(tokens, slot, captured, next, guard) {
1742
+ if (guard && !slot.optional && next?.type === "literal") {
1231
1743
  const token = tokens.peek();
1232
- if (!token || token.kind !== "event-modifier") {
1233
- break;
1234
- }
1235
- const metadata = token.metadata;
1236
- if (!metadata) {
1237
- break;
1238
- }
1239
- foundModifier = true;
1240
- switch (metadata.modifierName) {
1241
- case "once":
1242
- modifiers.once = true;
1243
- break;
1244
- case "debounce":
1245
- if (typeof metadata.value === "number") {
1246
- modifiers.debounce = metadata.value;
1247
- }
1248
- break;
1249
- case "throttle":
1250
- if (typeof metadata.value === "number") {
1251
- modifiers.throttle = metadata.value;
1252
- }
1253
- break;
1254
- case "queue":
1255
- if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
1256
- modifiers.queue = metadata.value;
1257
- }
1258
- break;
1744
+ if (token && this.view.wouldMatch(next, token)) return false;
1745
+ }
1746
+ if (slot.words && !isListedWord(tokens.peek(), slot.words)) return slot.optional ?? false;
1747
+ if (slot.optional && !slot.greedy) {
1748
+ const token = tokens.peek();
1749
+ if (token && isStopMarker(token, greedyStopMarkers(this.view.patternRest()))) return true;
1750
+ }
1751
+ if (slot.greedy) {
1752
+ const value = readGreedy(tokens, greedyStopMarkers(this.view.patternRest()));
1753
+ if (value) {
1754
+ captured.set(slot.role, value);
1755
+ return true;
1259
1756
  }
1260
- tokens.advance();
1757
+ return slot.optional ?? false;
1261
1758
  }
1262
- return foundModifier ? modifiers : void 0;
1759
+ return readRoleValue(tokens, slot, captured, this.profile);
1760
+ }
1761
+ maySkipSlot(patternToken, token, next) {
1762
+ const slot = patternToken.type === "group" && patternToken.optional ? patternToken.tokens.every((t) => t.type === "role") ? patternToken.tokens[0] : void 0 : patternToken;
1763
+ if (!slot || slot.type !== "role" || !slot.optional) return false;
1764
+ return !!token && this.view.wouldMatch(next, token);
1765
+ }
1766
+ atClauseEnd(tokens) {
1767
+ return tokens.isAtEnd();
1263
1768
  }
1264
1769
  };
1265
- /** Maximum tokens to scan ahead when looking for a group's leading marker */
1266
- _PatternMatcher.MAX_MARKER_SCAN = 3;
1267
- // ==========================================================================
1268
- // Depth Limits for Expression Parsing (security hardening)
1269
- // ==========================================================================
1270
- /** Maximum depth for nested property access (e.g., a.b.c.d...) */
1271
- _PatternMatcher.MAX_PROPERTY_DEPTH = 10;
1272
- /** Maximum number of arguments in method calls */
1273
- _PatternMatcher.MAX_METHOD_ARGS = 20;
1274
- // ===========================================================================
1275
- // English Idiom Support - Noise Word Handling
1276
- // ===========================================================================
1277
- /**
1278
- * Noise words that can be skipped in English for more natural syntax.
1279
- * - "the" before selectors: "toggle the .active" → "toggle .active"
1280
- * - "class" after class selectors: "add the .visible class" → "add .visible"
1281
- */
1282
- _PatternMatcher.ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
1283
- var PatternMatcher = _PatternMatcher;
1284
- var patternMatcher = new PatternMatcher();
1285
- function matchPattern(tokens, pattern) {
1286
- return patternMatcher.matchPattern(tokens, pattern);
1287
- }
1288
- function matchBest(tokens, patterns) {
1289
- return patternMatcher.matchBest(tokens, patterns);
1770
+ function createStockReader(profiles = {}) {
1771
+ return (view) => new StockValueReader(view, profiles);
1290
1772
  }
1291
1773
 
1292
1774
  // src/generation/pattern-generator.ts
@@ -1485,79 +1967,664 @@ function buildTablesFromProfiles(schemas, profiles) {
1485
1967
  }
1486
1968
  }
1487
1969
  }
1488
- return { keywords, markers };
1970
+ return { keywords, markers };
1971
+ }
1972
+ function detectWordOrders(profiles) {
1973
+ const sovLanguages = /* @__PURE__ */ new Set();
1974
+ const vsoLanguages = /* @__PURE__ */ new Set();
1975
+ for (const profile of profiles) {
1976
+ if (profile.wordOrder === "SOV") sovLanguages.add(profile.code);
1977
+ if (profile.wordOrder === "VSO") vsoLanguages.add(profile.code);
1978
+ }
1979
+ return { sovLanguages, vsoLanguages };
1980
+ }
1981
+ function createSchemaRenderer(schemas, profiles) {
1982
+ const tables = buildRenderTables(schemas, profiles);
1983
+ return {
1984
+ render(node, language) {
1985
+ return renderFromSchema(node, language, tables) ?? node.action;
1986
+ }
1987
+ };
1988
+ }
1989
+ function createDomainRenderer(config) {
1990
+ const { schemas, profiles, overrides } = config;
1991
+ let tables;
1992
+ return (node, language) => {
1993
+ const override = overrides?.[node.action];
1994
+ if (override) return override(node, language);
1995
+ tables ?? (tables = buildRenderTables(schemas, profiles));
1996
+ return renderFromSchema(node, language, tables);
1997
+ };
1998
+ }
1999
+ function buildRenderTables(schemas, profiles) {
2000
+ const { keywords, markers } = buildTablesFromProfiles(schemas, profiles);
2001
+ const { sovLanguages } = detectWordOrders(profiles);
2002
+ const markerPositions = {};
2003
+ for (const profile of profiles) {
2004
+ for (const [role, markerDef] of Object.entries(profile.roleMarkers ?? {})) {
2005
+ if (!markerDef.position) continue;
2006
+ markerPositions[role] ?? (markerPositions[role] = {});
2007
+ markerPositions[role][profile.code] = markerDef.position;
2008
+ }
2009
+ }
2010
+ const schemaMap = /* @__PURE__ */ new Map();
2011
+ for (const s of schemas) schemaMap.set(s.action, s);
2012
+ return { keywords, markers, markerPositions, sovLanguages, schemaMap };
2013
+ }
2014
+ function renderFromSchema(node, language, tables) {
2015
+ const schema = tables.schemaMap.get(node.action);
2016
+ if (!schema) return null;
2017
+ const keyword = lookupKeyword(tables.keywords, node.action, language);
2018
+ const isSOV = tables.sovLanguages.has(language);
2019
+ const ordered = sortRolesByWordOrder([...schema.roles], isSOV ? "SOV" : "SVO");
2020
+ const preVerb = [];
2021
+ const postVerb = [];
2022
+ for (const role of ordered) {
2023
+ let value = extractRoleValue(node, role.role);
2024
+ if (!value && role.default !== void 0) {
2025
+ value = String(extractValue(role.default));
2026
+ }
2027
+ if (!value) continue;
2028
+ if (role.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
2029
+ value = `"${value}"`;
2030
+ }
2031
+ const markerText = role.renderOverride?.[language] ?? role.markerOverride?.[language] ?? role.renderOverride?.["*"] ?? tables.markers[role.role]?.[language];
2032
+ const markerPosition = role.markerPositionOverride?.[language] ?? role.markerPosition ?? tables.markerPositions[role.role]?.[language] ?? (isSOV ? "after" : "before");
2033
+ const bucket = isSOV && role.sovSlot === "postVerb" ? postVerb : preVerb;
2034
+ if (markerText && markerPosition === "before") {
2035
+ bucket.push(markerText, value);
2036
+ } else if (markerText) {
2037
+ bucket.push(value, markerText);
2038
+ } else {
2039
+ bucket.push(value);
2040
+ }
2041
+ }
2042
+ return isSOV ? buildPhrase(...preVerb, keyword, ...postVerb) : buildPhrase(keyword, ...preVerb, ...postVerb);
2043
+ }
2044
+
2045
+ // src/generation/schema-patterns.ts
2046
+ function createSchemaPatternGenerator(grammar) {
2047
+ return {
2048
+ generatePattern: (schema, profile, config) => generatePattern2(grammar, schema, profile, config),
2049
+ generateSimplePattern: (schema, profile, config) => generateSimplePattern(grammar, schema, profile, config),
2050
+ generateVerbFirstPattern: (schema, profile, config) => generateVerbFirstPattern(schema, profile, config),
2051
+ generatePatternVariants: (schema, profile, config) => generatePatternVariants2(grammar, schema, profile, config),
2052
+ buildRoleToken
2053
+ };
2054
+ }
2055
+ function withoutVariantOnlyRoles(schema) {
2056
+ return schema.roles.some((r) => r.variantOnly) ? { ...schema, roles: schema.roles.filter((r) => !r.variantOnly) } : schema;
2057
+ }
2058
+ function sortRolesByPosition(roles, wordOrder, order) {
2059
+ const sortKey = wordOrder === "SOV" ? "sovPosition" : "svoPosition";
2060
+ return [...roles].sort(
2061
+ (a, b) => order === "ascending" ? (a[sortKey] ?? 99) - (b[sortKey] ?? 99) : (b[sortKey] ?? 0) - (a[sortKey] ?? 0)
2062
+ );
2063
+ }
2064
+ function resolveMarkerForRole(roleSpec, profile) {
2065
+ const overrideMarker = roleSpec.markerOverride?.[profile.code];
2066
+ const defaultMarker = profile.roleMarkers[roleSpec.role];
2067
+ if (overrideMarker !== void 0) {
2068
+ const alternatives = legacyMarkerAlternatives(roleSpec, profile.code, overrideMarker);
2069
+ return {
2070
+ primary: overrideMarker,
2071
+ ...alternatives && { alternatives },
2072
+ position: defaultMarker?.position ?? "before",
2073
+ isOverride: true
2074
+ };
2075
+ }
2076
+ if (defaultMarker && defaultMarker.primary) {
2077
+ const result = {
2078
+ primary: defaultMarker.primary,
2079
+ position: defaultMarker.position,
2080
+ isOverride: false
2081
+ };
2082
+ if (defaultMarker.alternatives) {
2083
+ result.alternatives = defaultMarker.alternatives;
2084
+ }
2085
+ return result;
2086
+ }
2087
+ return null;
2088
+ }
2089
+ function legacyMarkerAlternatives(roleSpec, languageCode, overrideMarker) {
2090
+ const legacy = roleSpec.markerLegacy?.[languageCode];
2091
+ if (!legacy?.length) return void 0;
2092
+ const alternatives = [...new Set(legacy)].filter((a) => a && a !== overrideMarker);
2093
+ return alternatives.length ? alternatives : void 0;
2094
+ }
2095
+ function schemaMarkerAlternatives(roleSpec, languageCode, marker) {
2096
+ const legacy = roleSpec.markerLegacy?.[languageCode] ?? [];
2097
+ const variants = roleSpec.methodCarrier ? [] : roleSpec.markerVariants?.[languageCode] ?? [];
2098
+ const alternatives = [.../* @__PURE__ */ new Set([...legacy, ...variants])].filter((a) => a && a !== marker);
2099
+ return alternatives.length ? alternatives : void 0;
2100
+ }
2101
+ var defaultConfig2 = {
2102
+ basePriority: 100,
2103
+ generateSimpleVariants: true,
2104
+ generateAlternatives: true
2105
+ };
2106
+ function generatePattern2(grammar, schema, profile, config = defaultConfig2) {
2107
+ const id = `${schema.action}-${profile.code}-generated`;
2108
+ const priority = config.basePriority ?? 100;
2109
+ const keyword = profile.keywords[schema.action];
2110
+ if (!keyword) {
2111
+ throw new Error(`No keyword translation for '${schema.action}' in ${profile.code}`);
2112
+ }
2113
+ const tokens = buildTokens2(grammar, schema, profile, keyword);
2114
+ const extraction = buildExtractionRulesWithDefaults(schema, profile);
2115
+ const format = buildFormatString2(grammar, schema, profile, keyword);
2116
+ return {
2117
+ id,
2118
+ language: profile.code,
2119
+ command: schema.action,
2120
+ priority,
2121
+ template: {
2122
+ format,
2123
+ tokens
2124
+ },
2125
+ extraction
2126
+ };
2127
+ }
2128
+ function generateSimplePattern(grammar, schema, profile, config = defaultConfig2) {
2129
+ const optionalRoles = schema.roles.filter((r) => !r.required);
2130
+ if (optionalRoles.length === 0) {
2131
+ return null;
2132
+ }
2133
+ const requiredRoles = schema.roles.filter((r) => r.required);
2134
+ const simpleSchema = {
2135
+ ...schema,
2136
+ roles: requiredRoles
2137
+ };
2138
+ const pattern = generatePattern2(grammar, simpleSchema, profile, config);
2139
+ return {
2140
+ ...pattern,
2141
+ id: `${schema.action}-${profile.code}-generated-simple`,
2142
+ priority: (config.basePriority ?? 100) - 5,
2143
+ // Lower priority than full pattern (was -10)
2144
+ extraction: buildExtractionRulesWithDefaults(schema, profile)
2145
+ };
2146
+ }
2147
+ function generateVerbFirstPattern(schema, profile, config = defaultConfig2) {
2148
+ if (profile.wordOrder !== "SOV") return null;
2149
+ const requiredRoles = schema.roles.filter((r) => r.required);
2150
+ if (requiredRoles.length !== 1) return null;
2151
+ const keyword = profile.keywords[schema.action];
2152
+ if (!keyword) return null;
2153
+ const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
2154
+ const roleTokens = requiredRoles.flatMap((r) => {
2155
+ const prefix = r.valuePrefixLiteral?.[profile.code];
2156
+ const words = wordsFor(r, profile.code);
2157
+ const roleToken = {
2158
+ type: "role",
2159
+ role: r.role,
2160
+ optional: false,
2161
+ expectedTypes: r.expectedTypes,
2162
+ ...words ? { words } : {}
2163
+ };
2164
+ return prefix ? [{ type: "literal", value: prefix }, roleToken] : [roleToken];
2165
+ });
2166
+ return {
2167
+ id: `${schema.action}-${profile.code}-generated-verb-first`,
2168
+ language: profile.code,
2169
+ command: schema.action,
2170
+ // Strictly below the canonical (basePriority) and simple (basePriority - 5)
2171
+ // patterns so native order is always preferred.
2172
+ priority: (config.basePriority ?? 100) - 20,
2173
+ template: {
2174
+ format: `${keyword.primary} <${requiredRoles[0].role}>`,
2175
+ tokens: [verbToken, ...roleTokens]
2176
+ },
2177
+ extraction: buildExtractionRulesWithDefaults(schema, profile)
2178
+ };
2179
+ }
2180
+ function generatePatternVariants2(grammar, schema, profile, config = defaultConfig2) {
2181
+ const patterns = [];
2182
+ const full = schema;
2183
+ schema = withoutVariantOnlyRoles(schema);
2184
+ patterns.push(generatePattern2(grammar, schema, profile, config));
2185
+ if (config.generateSimpleVariants) {
2186
+ const simple = generateSimplePattern(grammar, schema, profile, config);
2187
+ if (simple) {
2188
+ patterns.push(simple);
2189
+ }
2190
+ }
2191
+ for (const omitted of schema.omitRoleVariants ?? []) {
2192
+ const variantSchema = {
2193
+ ...schema,
2194
+ roles: schema.roles.filter((r) => r.role !== omitted)
2195
+ };
2196
+ const variant = generatePattern2(grammar, variantSchema, profile, config);
2197
+ patterns.push({
2198
+ ...variant,
2199
+ id: `${schema.action}-${profile.code}-generated-no-${omitted}`,
2200
+ priority: (config.basePriority ?? 100) - 8
2201
+ });
2202
+ }
2203
+ for (const widen of schema.widenTypeVariants ?? []) {
2204
+ if (widen.excludeLanguages?.includes(profile.code)) continue;
2205
+ const cloneSchema = {
2206
+ ...schema,
2207
+ roles: schema.roles.map(
2208
+ (r) => r.role === widen.role ? { ...r, expectedTypes: [...widen.types] } : r
2209
+ )
2210
+ };
2211
+ const main = generatePattern2(grammar, cloneSchema, profile, config);
2212
+ patterns.push({
2213
+ ...main,
2214
+ id: `${schema.action}-${profile.code}-generated-${widen.idSuffix}`,
2215
+ priority: (config.basePriority ?? 100) - (widen.priorityDelta ?? 15)
2216
+ });
2217
+ }
2218
+ for (const v of full.requiredRoleVariants ?? []) {
2219
+ const variantSchema = {
2220
+ ...full,
2221
+ roles: full.roles.filter((r) => !v.omit?.includes(r.role)).map((r) => {
2222
+ const required = r.role === v.role ? { ...r, required: true } : r;
2223
+ return r.role === v.widen?.role ? { ...required, expectedTypes: [...v.widen.types] } : required;
2224
+ })
2225
+ };
2226
+ const variant = generatePattern2(grammar, variantSchema, profile, config);
2227
+ patterns.push({
2228
+ ...variant,
2229
+ id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
2230
+ priority: (config.basePriority ?? 100) + (v.priorityDelta ?? 5)
2231
+ });
2232
+ }
2233
+ if (config.generateVerbFirstVariants !== false) {
2234
+ const verbFirst = generateVerbFirstPattern(schema, profile, config);
2235
+ if (verbFirst) {
2236
+ patterns.push(verbFirst);
2237
+ }
2238
+ }
2239
+ for (const v of schema.rolePrefixLiteralVariants ?? []) {
2240
+ const literal = v.literal[profile.code];
2241
+ if (!literal) continue;
2242
+ const { rolePrefixLiteralVariants: _omitted, ...baseSchema } = schema;
2243
+ const cloneSchema = {
2244
+ ...baseSchema,
2245
+ roles: schema.roles.map(
2246
+ (r) => r.role === v.role ? { ...r, valuePrefixLiteral: { [profile.code]: literal } } : r
2247
+ )
2248
+ };
2249
+ const delta = v.priorityDelta ?? 5;
2250
+ const carrier = v.methodCarrier ? { [v.methodCarrier]: { value: literal } } : {};
2251
+ const main = generatePattern2(grammar, cloneSchema, profile, config);
2252
+ patterns.push({
2253
+ ...main,
2254
+ id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
2255
+ priority: (config.basePriority ?? 100) + delta,
2256
+ extraction: { ...main.extraction, ...carrier }
2257
+ });
2258
+ if (config.generateVerbFirstVariants !== false) {
2259
+ const verbFirstUrl = generateVerbFirstPattern(cloneSchema, profile, config);
2260
+ if (verbFirstUrl) {
2261
+ patterns.push({
2262
+ ...verbFirstUrl,
2263
+ id: `${schema.action}-${profile.code}-generated-verb-first-${v.idSuffix}`,
2264
+ priority: (config.basePriority ?? 100) - 20 + delta,
2265
+ extraction: { ...verbFirstUrl.extraction, ...carrier }
2266
+ });
2267
+ }
2268
+ }
2269
+ }
2270
+ return patterns;
2271
+ }
2272
+ function buildTokens2(grammar, schema, profile, keyword) {
2273
+ const tokens = [];
2274
+ const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
2275
+ const postVerbRoles = profile.wordOrder === "SOV" ? schema.roles.filter((r) => r.sovSlot === "postVerb") : [];
2276
+ const roleTokens = buildRoleTokens(
2277
+ grammar,
2278
+ postVerbRoles.length ? { ...schema, roles: schema.roles.filter((r) => r.sovSlot !== "postVerb") } : schema,
2279
+ profile
2280
+ );
2281
+ const postVerbTokens = postVerbRoles.length ? buildRoleTokens(grammar, { ...schema, roles: postVerbRoles }, profile) : [];
2282
+ switch (profile.wordOrder) {
2283
+ case "SVO":
2284
+ tokens.push(verbToken);
2285
+ tokens.push(...roleTokens);
2286
+ break;
2287
+ case "SOV":
2288
+ tokens.push(...roleTokens);
2289
+ tokens.push(verbToken);
2290
+ tokens.push(...postVerbTokens);
2291
+ break;
2292
+ case "VSO":
2293
+ tokens.push(verbToken);
2294
+ tokens.push(...roleTokens);
2295
+ break;
2296
+ default:
2297
+ tokens.push(verbToken);
2298
+ tokens.push(...roleTokens);
2299
+ }
2300
+ return collapseAdjacentLiterals(tokens);
2301
+ }
2302
+ function collapseAdjacentLiterals(tokens) {
2303
+ const out = [];
2304
+ for (const tok of tokens) {
2305
+ const prev = out[out.length - 1];
2306
+ if (prev && prev.type === "literal" && tok.type === "literal" && prev.value === tok.value) {
2307
+ const alts = [...prev.alternatives ?? [], ...tok.alternatives ?? []];
2308
+ if (alts.length) out[out.length - 1] = { ...prev, alternatives: [...new Set(alts)] };
2309
+ continue;
2310
+ }
2311
+ out.push(tok);
2312
+ }
2313
+ return out;
2314
+ }
2315
+ function buildRoleTokens(grammar, schema, profile) {
2316
+ const tokens = [];
2317
+ const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
2318
+ for (const roleSpec of sortedRoles) {
2319
+ const roleToken = buildRoleToken(roleSpec, profile);
2320
+ if (!roleSpec.required) {
2321
+ tokens.push({
2322
+ type: "group",
2323
+ optional: true,
2324
+ tokens: roleToken
2325
+ });
2326
+ } else {
2327
+ tokens.push(...roleToken);
2328
+ }
2329
+ }
2330
+ return tokens;
2331
+ }
2332
+ function wordsFor(roleSpec, language) {
2333
+ if (!roleSpec.words) return void 0;
2334
+ const words = [...roleSpec.words["*"] ?? [], ...roleSpec.words[language] ?? []];
2335
+ return words.length > 0 ? words : void 0;
2336
+ }
2337
+ function buildRoleToken(roleSpec, profile) {
2338
+ const tokens = [];
2339
+ const overrideMarker = roleSpec.markerOverride?.[profile.code];
2340
+ const defaultMarker = profile.roleMarkers[roleSpec.role];
2341
+ const suppressMarker = roleSpec.renderOverride?.[profile.code] === "";
2342
+ const words = wordsFor(roleSpec, profile.code);
2343
+ const roleValueToken = {
2344
+ type: "role",
2345
+ role: roleSpec.role,
2346
+ optional: !roleSpec.required,
2347
+ expectedTypes: roleSpec.expectedTypes,
2348
+ // The matcher reads this too — a `'keyword'`-shaped slot holds a fixed
2349
+ // keyword phrase (`using view transition`) and must not be skipped by the
2350
+ // trailing-slot verb guard.
2351
+ ...roleSpec.valueShape !== void 0 ? { valueShape: roleSpec.valueShape } : {},
2352
+ // A greedy role reads up to the next marker the pattern owes (a domain's
2353
+ // SQL condition); its value reader does the reading.
2354
+ ...roleSpec.greedy ? { greedy: true } : {},
2355
+ // A role with a word list reads only one of its words.
2356
+ ...words ? { words } : {}
2357
+ };
2358
+ const prefixLiteral = roleSpec.valuePrefixLiteral?.[profile.code];
2359
+ const pushPrefixed = () => {
2360
+ if (prefixLiteral) tokens.push({ type: "literal", value: prefixLiteral });
2361
+ tokens.push(roleValueToken);
2362
+ };
2363
+ if (overrideMarker !== void 0) {
2364
+ const markerWords = overrideMarker ? overrideMarker.split(/\s+/).filter(Boolean) : [];
2365
+ const isPassthroughOverride = overrideMarker !== void 0 && overrideMarker === roleSpec.markerOverride?.en;
2366
+ const position = roleSpec.markerPositionOverride?.[profile.code] ?? roleSpec.markerPosition ?? defaultMarker?.position ?? (profile.wordOrder === "SOV" && !isPassthroughOverride ? "after" : "before");
2367
+ const optionalMarker = roleSpec.markerOptional?.[profile.code] === true;
2368
+ const pushWord = (word) => {
2369
+ const alternatives = markerWords.length === 1 ? schemaMarkerAlternatives(roleSpec, profile.code, word) ?? [] : [];
2370
+ const literal = {
2371
+ type: "literal",
2372
+ value: word,
2373
+ ...alternatives.length ? { alternatives } : {},
2374
+ ...suppressMarker ? { renderSuppress: true } : {}
2375
+ };
2376
+ tokens.push(optionalMarker ? { type: "group", optional: true, tokens: [literal] } : literal);
2377
+ };
2378
+ if (position === "before") {
2379
+ for (const word of markerWords) pushWord(word);
2380
+ pushPrefixed();
2381
+ } else {
2382
+ pushPrefixed();
2383
+ for (const word of markerWords) pushWord(word);
2384
+ }
2385
+ } else if (defaultMarker) {
2386
+ const asMarker = () => {
2387
+ const alternatives = [
2388
+ .../* @__PURE__ */ new Set([
2389
+ ...defaultMarker.alternatives ?? [],
2390
+ ...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
2391
+ ])
2392
+ ].filter((a) => a !== defaultMarker.primary);
2393
+ return {
2394
+ type: "literal",
2395
+ value: defaultMarker.primary,
2396
+ ...alternatives.length ? { alternatives } : {},
2397
+ ...suppressMarker ? { renderSuppress: true } : {}
2398
+ };
2399
+ };
2400
+ const pushMarker = (marker) => {
2401
+ if (!profile.markersOptional && !roleSpec.markerOptional?.[profile.code]) {
2402
+ tokens.push(marker);
2403
+ return;
2404
+ }
2405
+ tokens.push({
2406
+ type: "group",
2407
+ optional: true,
2408
+ ...profile.markersOptional && !roleSpec.markerOptional?.[profile.code] ? { renderRequired: true } : {},
2409
+ tokens: [marker]
2410
+ });
2411
+ };
2412
+ if ((roleSpec.markerPosition ?? defaultMarker.position) === "before") {
2413
+ if (defaultMarker.primary) {
2414
+ pushMarker(asMarker());
2415
+ }
2416
+ pushPrefixed();
2417
+ } else {
2418
+ pushPrefixed();
2419
+ pushMarker(asMarker());
2420
+ }
2421
+ } else {
2422
+ pushPrefixed();
2423
+ }
2424
+ return tokens;
2425
+ }
2426
+ function buildExtractionRules2(schema, profile) {
2427
+ const rules = {};
2428
+ for (const roleSpec of schema.roles) {
2429
+ const overrideMarker = roleSpec.markerOverride?.[profile.code];
2430
+ const defaultMarker = profile.roleMarkers[roleSpec.role];
2431
+ if (roleSpec.valuePrefixLiteral?.[profile.code]) {
2432
+ rules[roleSpec.role] = { marker: roleSpec.valuePrefixLiteral[profile.code] };
2433
+ } else if (overrideMarker !== void 0) {
2434
+ if (!overrideMarker) {
2435
+ rules[roleSpec.role] = {};
2436
+ } else {
2437
+ const isSingleWord = !/\s/.test(overrideMarker.trim());
2438
+ const markerAlternatives = isSingleWord ? schemaMarkerAlternatives(roleSpec, profile.code, overrideMarker) ?? [] : [];
2439
+ rules[roleSpec.role] = markerAlternatives.length ? { marker: overrideMarker, markerAlternatives } : { marker: overrideMarker };
2440
+ }
2441
+ } else if (defaultMarker && defaultMarker.primary) {
2442
+ const markerAlternatives = [
2443
+ .../* @__PURE__ */ new Set([
2444
+ ...defaultMarker.alternatives ?? [],
2445
+ ...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
2446
+ ])
2447
+ ].filter((a) => a !== defaultMarker.primary);
2448
+ rules[roleSpec.role] = markerAlternatives.length ? { marker: defaultMarker.primary, markerAlternatives } : { marker: defaultMarker.primary };
2449
+ } else {
2450
+ rules[roleSpec.role] = {};
2451
+ }
2452
+ }
2453
+ return rules;
1489
2454
  }
1490
- function detectWordOrders(profiles) {
1491
- const sovLanguages = /* @__PURE__ */ new Set();
1492
- const vsoLanguages = /* @__PURE__ */ new Set();
1493
- for (const profile of profiles) {
1494
- if (profile.wordOrder === "SOV") sovLanguages.add(profile.code);
1495
- if (profile.wordOrder === "VSO") vsoLanguages.add(profile.code);
2455
+ function buildExtractionRulesWithDefaults(schema, profile) {
2456
+ const baseRules = buildExtractionRules2(schema, profile);
2457
+ const rules = {};
2458
+ for (const roleSpec of schema.roles) {
2459
+ const baseRule = baseRules[roleSpec.role] || {};
2460
+ if (!roleSpec.required && roleSpec.default) {
2461
+ rules[roleSpec.role] = { ...baseRule, default: roleSpec.default };
2462
+ } else {
2463
+ rules[roleSpec.role] = baseRule;
2464
+ }
1496
2465
  }
1497
- return { sovLanguages, vsoLanguages };
2466
+ return rules;
1498
2467
  }
1499
- function createSchemaRenderer(schemas, profiles) {
1500
- const tables = buildRenderTables(schemas, profiles);
1501
- return {
1502
- render(node, language) {
1503
- return renderFromSchema(node, language, tables) ?? node.action;
2468
+ function buildFormatString2(grammar, schema, profile, keyword) {
2469
+ const parts = [];
2470
+ const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
2471
+ const roleParts = sortedRoles.map((roleSpec) => {
2472
+ const resolved = resolveMarkerForRole(roleSpec, profile);
2473
+ let part = "";
2474
+ if (resolved && resolved.primary) {
2475
+ if (resolved.position === "before") {
2476
+ part = `${resolved.primary} {${roleSpec.role}}`;
2477
+ } else {
2478
+ part = `{${roleSpec.role}} ${resolved.primary}`;
2479
+ }
2480
+ } else {
2481
+ part = `{${roleSpec.role}}`;
1504
2482
  }
1505
- };
2483
+ return roleSpec.required ? part : `[${part}]`;
2484
+ });
2485
+ switch (profile.wordOrder) {
2486
+ case "SVO":
2487
+ case "VSO":
2488
+ parts.push(keyword.primary, ...roleParts);
2489
+ break;
2490
+ case "SOV":
2491
+ parts.push(...roleParts, keyword.primary);
2492
+ break;
2493
+ default:
2494
+ parts.push(keyword.primary, ...roleParts);
2495
+ }
2496
+ return parts.join(" ");
1506
2497
  }
1507
- function createDomainRenderer(config) {
1508
- const { schemas, profiles, overrides } = config;
1509
- let tables;
1510
- return (node, language) => {
1511
- const override = overrides?.[node.action];
1512
- if (override) return override(node, language);
1513
- tables ?? (tables = buildRenderTables(schemas, profiles));
1514
- return renderFromSchema(node, language, tables);
1515
- };
2498
+
2499
+ // src/generation/pattern-render.ts
2500
+ var NESTED_ROLE_BONUS = 5;
2501
+ var UNCOVERED_ROLE_PENALTY = 30;
2502
+ function pinnedLiteral(rule) {
2503
+ if (rule?.default?.type === "literal") return String(rule.default.value).trim().toLowerCase();
2504
+ if (rule?.value !== void 0) return String(rule.value).trim().toLowerCase();
2505
+ return void 0;
1516
2506
  }
1517
- function buildRenderTables(schemas, profiles) {
1518
- const { keywords, markers } = buildTablesFromProfiles(schemas, profiles);
1519
- const { sovLanguages } = detectWordOrders(profiles);
1520
- const markerPositions = {};
1521
- for (const profile of profiles) {
1522
- for (const [role, markerDef] of Object.entries(profile.roleMarkers ?? {})) {
1523
- if (!markerDef.position) continue;
1524
- markerPositions[role] ?? (markerPositions[role] = {});
1525
- markerPositions[role][profile.code] = markerDef.position;
1526
- }
1527
- }
1528
- const schemaMap = /* @__PURE__ */ new Map();
1529
- for (const s of schemas) schemaMap.set(s.action, s);
1530
- return { keywords, markers, markerPositions, sovLanguages, schemaMap };
2507
+ function selectRenderPattern(node, patterns, hooks) {
2508
+ let candidates = patterns.filter((pattern) => !pattern.parseOnly);
2509
+ if (candidates.length === 0) candidates = patterns;
2510
+ if (hooks.candidates) candidates = hooks.candidates(node, candidates);
2511
+ const pinnedValues = /* @__PURE__ */ new Map();
2512
+ for (const pattern of candidates) {
2513
+ for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
2514
+ const key = pinnedLiteral(rule);
2515
+ if (key === void 0) continue;
2516
+ const set = pinnedValues.get(role) ?? /* @__PURE__ */ new Set();
2517
+ set.add(key);
2518
+ pinnedValues.set(role, set);
2519
+ }
2520
+ }
2521
+ const scored = candidates.map((pattern) => {
2522
+ let score = pattern.priority;
2523
+ const hasRealRole = (role) => hooks.hasRealRole(node, role);
2524
+ const scoreTokens = (tokens, inGroup) => {
2525
+ for (const token of tokens) {
2526
+ if (token.type === "group") {
2527
+ scoreTokens(token.tokens, true);
2528
+ } else if (token.type === "role") {
2529
+ if (hasRealRole(token.role)) {
2530
+ score += inGroup ? NESTED_ROLE_BONUS : 10;
2531
+ } else if (!inGroup && !token.optional) {
2532
+ score -= 50;
2533
+ }
2534
+ }
2535
+ }
2536
+ };
2537
+ scoreTokens(pattern.template.tokens, false);
2538
+ const slots = /* @__PURE__ */ new Set();
2539
+ const collectSlots = (tokens) => {
2540
+ for (const token of tokens) {
2541
+ if (token.type === "group") collectSlots(token.tokens);
2542
+ else if (token.type === "role") slots.add(token.role);
2543
+ }
2544
+ };
2545
+ collectSlots(pattern.template.tokens);
2546
+ for (const role of node.roles.keys()) {
2547
+ if (hasRealRole(role) && !slots.has(role)) score -= UNCOVERED_ROLE_PENALTY;
2548
+ }
2549
+ for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
2550
+ const pinnedKey = pinnedLiteral(rule);
2551
+ if (pinnedKey === void 0) continue;
2552
+ const actual = node.roles.get(role);
2553
+ const actualKey = actual?.type === (rule.valueIsExpression ? "expression" : "literal") ? String(
2554
+ actual.type === "expression" ? actual.raw : actual.value
2555
+ ).trim().toLowerCase() : void 0;
2556
+ if (actualKey === pinnedKey) {
2557
+ score += 60;
2558
+ } else if (actualKey === void 0 || pinnedValues.get(role)?.has(actualKey)) {
2559
+ score -= 200;
2560
+ }
2561
+ }
2562
+ score += hooks.adjustScore?.(pattern, node) ?? 0;
2563
+ return { pattern, score };
2564
+ });
2565
+ scored.sort((a, b) => b.score - a.score);
2566
+ return scored.length > 0 ? scored[0].pattern : null;
1531
2567
  }
1532
- function renderFromSchema(node, language, tables) {
1533
- const schema = tables.schemaMap.get(node.action);
1534
- if (!schema) return null;
1535
- const keyword = lookupKeyword(tables.keywords, node.action, language);
1536
- const isSOV = tables.sovLanguages.has(language);
1537
- const ordered = sortRolesByWordOrder([...schema.roles], isSOV ? "SOV" : "SVO");
1538
- const preVerb = [];
1539
- const postVerb = [];
1540
- for (const role of ordered) {
1541
- let value = extractRoleValue(node, role.role);
1542
- if (!value && role.default !== void 0) {
1543
- value = String(extractValue(role.default));
2568
+ function renderPattern(node, pattern, hooks) {
2569
+ const parts = [];
2570
+ const language = pattern.language;
2571
+ const tokens = pattern.template.tokens;
2572
+ for (let i = 0; i < tokens.length; i++) {
2573
+ hooks.beforeToken?.(tokens, i, parts, node, language);
2574
+ const span = hooks.span?.(tokens, i, node, language, true);
2575
+ if (span !== void 0) {
2576
+ parts.push(span.text);
2577
+ i += span.tokens - 1;
2578
+ continue;
1544
2579
  }
1545
- if (!value) continue;
1546
- if (role.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
1547
- value = `"${value}"`;
2580
+ const token = tokens[i];
2581
+ const rendered = renderPatternToken(token, node, language, hooks);
2582
+ if (rendered !== null) {
2583
+ pushPart(hooks, parts, rendered, token, tokens[i - 1], node, language);
2584
+ hooks.afterToken?.(token, parts.length - 1, node);
1548
2585
  }
1549
- const markerText = role.renderOverride?.[language] ?? role.markerOverride?.[language] ?? role.renderOverride?.["*"] ?? tables.markers[role.role]?.[language];
1550
- const markerPosition = role.markerPositionOverride?.[language] ?? role.markerPosition ?? tables.markerPositions[role.role]?.[language] ?? (isSOV ? "after" : "before");
1551
- const bucket = isSOV && role.sovSlot === "postVerb" ? postVerb : preVerb;
1552
- if (markerText && markerPosition === "before") {
1553
- bucket.push(markerText, value);
1554
- } else if (markerText) {
1555
- bucket.push(value, markerText);
1556
- } else {
1557
- bucket.push(value);
2586
+ }
2587
+ return parts;
2588
+ }
2589
+ function pushPart(hooks, parts, text, token, previous, node, language) {
2590
+ if (hooks.pushPart) hooks.pushPart(parts, text, token, previous, node, language);
2591
+ else parts.push(text);
2592
+ }
2593
+ function renderPatternToken(token, node, language, hooks) {
2594
+ switch (token.type) {
2595
+ case "literal":
2596
+ return token.renderSuppress ? null : token.value;
2597
+ case "role":
2598
+ return hooks.writeRole(token, node, language);
2599
+ case "group": {
2600
+ const hasRequired = token.tokens.filter((t) => t.type === "role" && !t.optional).every((t) => node.roles.has(t.role));
2601
+ if (!hasRequired && token.optional) {
2602
+ return null;
2603
+ }
2604
+ if (hooks.writeGroup && !hooks.writeGroup(token, node, language)) return null;
2605
+ const groupParts = [];
2606
+ let hasRoleValue = false;
2607
+ for (let i = 0; i < token.tokens.length; i++) {
2608
+ const span = hooks.span?.(token.tokens, i, node, language, false);
2609
+ if (span !== void 0) {
2610
+ groupParts.push(span.text);
2611
+ hasRoleValue = true;
2612
+ i += span.tokens - 1;
2613
+ continue;
2614
+ }
2615
+ const subToken = token.tokens[i];
2616
+ const rendered = renderPatternToken(subToken, node, language, hooks);
2617
+ if (rendered !== null) {
2618
+ pushPart(hooks, groupParts, rendered, subToken, token.tokens[i - 1], node, language);
2619
+ if (subToken.type === "role") hasRoleValue = true;
2620
+ }
2621
+ }
2622
+ if (token.optional && !hasRoleValue && !token.renderRequired) return null;
2623
+ return groupParts.length > 0 ? groupParts.join(" ") : null;
1558
2624
  }
2625
+ default:
2626
+ return null;
1559
2627
  }
1560
- return isSOV ? buildPhrase(...preVerb, keyword, ...postVerb) : buildPhrase(keyword, ...preVerb, ...postVerb);
1561
2628
  }
1562
2629
 
1563
2630
  // src/grammar/types.ts
@@ -2842,12 +3909,15 @@ function readingDifference(source, back) {
2842
3909
 
2843
3910
  // src/api/create-dsl.ts
2844
3911
  var UnreadInputError = class extends Error {
2845
- constructor(input, language, reading, unread) {
2846
- super(`"${input}" (${language}) reads as ${reading.action} only by leaving "${unread}" unread`);
3912
+ constructor(input, language, reading, unread, hint) {
3913
+ super(
3914
+ `"${input}" (${language}) reads as ${reading.action} only by leaving "${unread}" unread` + (hint ? `; ${hint}` : "")
3915
+ );
2847
3916
  this.input = input;
2848
3917
  this.language = language;
2849
3918
  this.reading = reading;
2850
3919
  this.unread = unread;
3920
+ this.hint = hint;
2851
3921
  this.code = "unread-input";
2852
3922
  this.name = "UnreadInputError";
2853
3923
  }
@@ -2872,7 +3942,8 @@ var DSLRegistry = class {
2872
3942
  * subset of the DSL's languages. A built-in command missing a keyword stays
2873
3943
  * an error, since that is a domain-authoring mistake.
2874
3944
  */
2875
- constructor(config, partiallyTranslatedActions = /* @__PURE__ */ new Set()) {
3945
+ constructor(config, partiallyTranslatedActions, generate) {
3946
+ this.generate = generate;
2876
3947
  this.patterns = /* @__PURE__ */ new Map();
2877
3948
  this.tokenizers = /* @__PURE__ */ new Map();
2878
3949
  this.schemas = config.schemas;
@@ -2888,7 +3959,7 @@ var DSLRegistry = class {
2888
3959
  if (this.partiallyTranslatedActions.has(schema.action) && !lang.patternProfile.keywords[schema.action]) {
2889
3960
  continue;
2890
3961
  }
2891
- const pattern = generatePattern(schema, lang.patternProfile);
3962
+ const pattern = this.generate(schema, lang.patternProfile);
2892
3963
  patterns.push(pattern);
2893
3964
  }
2894
3965
  this.patterns.set(lang.code, patterns);
@@ -2904,9 +3975,9 @@ var DSLRegistry = class {
2904
3975
  }
2905
3976
  };
2906
3977
  var MultilingualDSLImpl = class {
2907
- constructor(config, registry, transformer, profileProvider) {
3978
+ constructor(config, registry, transformer, profileProvider, engine) {
2908
3979
  this.registry = registry;
2909
- this.matcher = new PatternMatcher();
3980
+ this.match = engine.match;
2910
3981
  this.transformer = transformer;
2911
3982
  this.profileProvider = profileProvider;
2912
3983
  if (config.codeGenerator) {
@@ -2919,10 +3990,7 @@ var MultilingualDSLImpl = class {
2919
3990
  for (const extension of config.extensions ?? []) {
2920
3991
  if (extension.render) this.extensionRenderers.set(extension.schema.action, extension.render);
2921
3992
  }
2922
- this.schemaFallbackRenderer = createDomainRenderer({
2923
- schemas: config.schemas,
2924
- profiles: config.languages.map((l) => l.patternProfile)
2925
- });
3993
+ this.schemaFallbackRenderer = engine.fallbackRenderer(config, registry);
2926
3994
  const schemaMap = new Map(config.schemas.map((s) => [s.action, s]));
2927
3995
  this.schemaLookup = {
2928
3996
  getSchema: (action) => schemaMap.get(action)
@@ -2946,15 +4014,12 @@ var MultilingualDSLImpl = class {
2946
4014
  }
2947
4015
  const tokens = tokenizer.tokenize(input);
2948
4016
  const patterns = this.registry.getPatterns(language);
2949
- const profile = {
2950
- code: language
2951
- };
2952
4017
  const total = tokens.tokens.length;
2953
4018
  let best = null;
2954
4019
  let longestPartial = null;
2955
4020
  for (const pattern of patterns) {
2956
4021
  const mark = tokens.mark();
2957
- const match = this.matcher.matchPattern(tokens, pattern, profile);
4022
+ const match = this.match(tokens, pattern, language);
2958
4023
  tokens.reset(mark);
2959
4024
  if (!match) continue;
2960
4025
  if (match.consumedTokens < total) {
@@ -2971,15 +4036,32 @@ var MultilingualDSLImpl = class {
2971
4036
  }
2972
4037
  if (longestPartial) {
2973
4038
  const unread = tokens.tokens.slice(longestPartial.consumedTokens).map((t) => t.value).join(" ");
4039
+ const hint = this.quoteHint(longestPartial, tokens.tokens);
2974
4040
  throw new UnreadInputError(
2975
4041
  input,
2976
4042
  language,
2977
4043
  this.nodeOf(longestPartial, input, language),
2978
- unread
4044
+ unread,
4045
+ ...hint ? [hint] : []
2979
4046
  );
2980
4047
  }
2981
4048
  throw new Error(`No pattern matched for input: ${input}`);
2982
4049
  }
4050
+ /**
4051
+ * `if "buy milk" is one value, quote it`, where the last token a match read is a bare word it took
4052
+ * as a role's value (a role with no word list) and the first it left is a bare word too.
4053
+ */
4054
+ quoteHint(match, tokens) {
4055
+ const last = tokens[match.consumedTokens - 1];
4056
+ const next = tokens[match.consumedTokens];
4057
+ if (last?.kind !== "identifier" || next?.kind !== "identifier") return void 0;
4058
+ const schema = this.schemaLookup.getSchema(match.pattern.command);
4059
+ const role = [...match.captured].find(
4060
+ ([, v]) => v.raw === last.value
4061
+ )?.[0];
4062
+ if (!role || schema?.roles.find((r) => r.role === role)?.words) return void 0;
4063
+ return `if "${last.value} ${next.value}" is one value, quote it`;
4064
+ }
2983
4065
  nodeOf(match, input, language) {
2984
4066
  return {
2985
4067
  kind: "command",
@@ -3198,8 +4280,76 @@ function createMultilingualDSL(config) {
3198
4280
  dictionary,
3199
4281
  profileProvider
3200
4282
  });
3201
- const registry = new DSLRegistry(effectiveConfig, extensionActions);
3202
- return new MultilingualDSLImpl(effectiveConfig, registry, transformer, profileProvider);
4283
+ const engine = engineFor(config);
4284
+ const registry = new DSLRegistry(effectiveConfig, extensionActions, engine.generate);
4285
+ return new MultilingualDSLImpl(effectiveConfig, registry, transformer, profileProvider, engine);
4286
+ }
4287
+ function engineFor(config) {
4288
+ const name = config.engine ?? "classic";
4289
+ if (name === "classic") return classicEngine();
4290
+ if (name === "core") return coreEngine();
4291
+ throw new Error(`Unknown DSL engine "${String(name)}" (expected "classic" or "core")`);
4292
+ }
4293
+ function classicEngine() {
4294
+ const matcher = new PatternMatcher();
4295
+ return {
4296
+ generate: (schema, profile) => generatePattern(schema, profile),
4297
+ match: (tokens, pattern, language) => matcher.matchPattern(tokens, pattern, { code: language }),
4298
+ fallbackRenderer: (config) => createDomainRenderer({
4299
+ schemas: config.schemas,
4300
+ profiles: config.languages.map((l) => l.patternProfile)
4301
+ })
4302
+ };
4303
+ }
4304
+ function schemaProfileOf(profile) {
4305
+ const side = profile.wordOrder === "SOV" ? "after" : "before";
4306
+ const roleMarkers = {};
4307
+ for (const [role, marker] of Object.entries(profile.roleMarkers ?? {})) {
4308
+ roleMarkers[role] = {
4309
+ primary: marker.primary,
4310
+ ...marker.alternatives ? { alternatives: marker.alternatives } : {},
4311
+ position: marker.position ?? side
4312
+ };
4313
+ }
4314
+ return {
4315
+ code: profile.code,
4316
+ wordOrder: profile.wordOrder,
4317
+ keywords: profile.keywords,
4318
+ roleMarkers
4319
+ };
4320
+ }
4321
+ function coreEngine() {
4322
+ const generator = createSchemaPatternGenerator({ positionOrder: "descending" });
4323
+ const matcher = new MatchSkeleton(createStockReader(), stockConfidenceModel);
4324
+ return {
4325
+ generate: (schema, profile) => generator.generatePattern(schema, schemaProfileOf(profile)),
4326
+ match: (tokens, pattern) => matcher.matchPattern(tokens, pattern),
4327
+ fallbackRenderer: (config, registry) => {
4328
+ const schemas = new Map(config.schemas.map((s) => [s.action, s]));
4329
+ const hooks = {
4330
+ hasRealRole: (node, role) => {
4331
+ const value = node.roles.get(role);
4332
+ return value !== void 0 && value.implicit !== true;
4333
+ },
4334
+ writeRole: (token, node) => {
4335
+ if (!node.roles.has(token.role)) return null;
4336
+ let value = extractRoleValue(node, token.role);
4337
+ const spec = schemas.get(node.action)?.roles.find((r) => r.role === token.role);
4338
+ if (spec?.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
4339
+ value = `"${value}"`;
4340
+ }
4341
+ return value || null;
4342
+ }
4343
+ };
4344
+ return (node, language) => {
4345
+ const patterns = registry.getPatterns(language).filter((p) => p.command === node.action);
4346
+ const pattern = selectRenderPattern(node, patterns, hooks);
4347
+ if (!pattern) return null;
4348
+ const text = renderPattern(node, pattern, hooks).join(" ");
4349
+ return text || null;
4350
+ };
4351
+ }
4352
+ };
3203
4353
  }
3204
4354
 
3205
4355
  // src/prompts/prompt-generator.ts
@@ -4505,6 +5655,50 @@ var CrossDomainDispatcher = class {
4505
5655
  }
4506
5656
  };
4507
5657
 
5658
+ // src/api/translation-loss.ts
5659
+ var LossyTranslationError = class extends Error {
5660
+ constructor(partial, loss, from, to) {
5661
+ super(
5662
+ `translation ${from} \u2192 ${to} would lose part of the script (${loss.kind}): ` + loss.lost.join(", ")
5663
+ );
5664
+ this.partial = partial;
5665
+ this.loss = loss;
5666
+ this.from = from;
5667
+ this.to = to;
5668
+ this.name = "LossyTranslationError";
5669
+ }
5670
+ };
5671
+ function unconsumedSpans(node) {
5672
+ const out = [];
5673
+ for (const d of node.diagnostics ?? []) {
5674
+ if (d.code !== "unconsumed-input") continue;
5675
+ out.push(/"(.*)"\s*$/s.exec(d.message)?.[1] ?? d.message);
5676
+ }
5677
+ return out;
5678
+ }
5679
+ function findTranslationLoss(input, node, output, readBack, rules) {
5680
+ const unread = unconsumedSpans(node);
5681
+ if (unread.length) return { kind: "truncation", lost: unread };
5682
+ const unwritable = rules.unwritable?.(node) ?? [];
5683
+ if (unwritable.length) return { kind: "core-only", lost: unwritable };
5684
+ let back;
5685
+ try {
5686
+ back = readBack(output);
5687
+ } catch (e) {
5688
+ return { kind: "read-back", lost: [`the output does not read: ${e.message}`] };
5689
+ }
5690
+ const misread = unconsumedSpans(back);
5691
+ if (misread.length) return { kind: "read-back", lost: misread };
5692
+ const written = rules.written ? rules.written(node) : node;
5693
+ for (const difference of rules.differences) {
5694
+ const lost = difference(written, back);
5695
+ if (lost.length) return { kind: "read-back", lost };
5696
+ }
5697
+ const values = rules.missingValues?.(input, output) ?? [];
5698
+ if (values.length) return { kind: "invariant", lost: values };
5699
+ return void 0;
5700
+ }
5701
+
4508
5702
  // src/aot/domain-scanner.ts
4509
5703
  function buildAttributePatterns(attributes) {
4510
5704
  const patterns = [];
@@ -4796,122 +5990,6 @@ import { defineCommand, defineRole, getRoleSpec } from "@lokascript/intent";
4796
5990
  // src/generation/diagnostics.ts
4797
5991
  import { createDiagnosticCollector, fromError, filterBySeverity } from "@lokascript/intent";
4798
5992
 
4799
- // src/core/tokenization/token-utils.ts
4800
- var TokenStreamImpl = class {
4801
- constructor(tokens, language) {
4802
- this.pos = 0;
4803
- this.tokens = tokens;
4804
- this.language = language;
4805
- }
4806
- peek(offset = 0) {
4807
- const index = this.pos + offset;
4808
- if (index < 0 || index >= this.tokens.length) {
4809
- return null;
4810
- }
4811
- return this.tokens[index];
4812
- }
4813
- advance() {
4814
- if (this.isAtEnd()) {
4815
- throw new Error("Unexpected end of token stream");
4816
- }
4817
- return this.tokens[this.pos++];
4818
- }
4819
- isAtEnd() {
4820
- return this.pos >= this.tokens.length;
4821
- }
4822
- mark() {
4823
- return { position: this.pos };
4824
- }
4825
- reset(mark) {
4826
- this.pos = mark.position;
4827
- }
4828
- position() {
4829
- return this.pos;
4830
- }
4831
- /**
4832
- * Get remaining tokens as an array.
4833
- */
4834
- remaining() {
4835
- return this.tokens.slice(this.pos);
4836
- }
4837
- /**
4838
- * Consume tokens while predicate is true.
4839
- */
4840
- takeWhile(predicate) {
4841
- const result = [];
4842
- while (!this.isAtEnd() && predicate(this.peek())) {
4843
- result.push(this.advance());
4844
- }
4845
- return result;
4846
- }
4847
- /**
4848
- * Skip tokens while predicate is true.
4849
- */
4850
- skipWhile(predicate) {
4851
- while (!this.isAtEnd() && predicate(this.peek())) {
4852
- this.advance();
4853
- }
4854
- }
4855
- };
4856
- function createPosition(start, end) {
4857
- return { start, end };
4858
- }
4859
- function createToken(valueOrParams, kind, position, normalizedOrOptions) {
4860
- if (typeof valueOrParams === "object") {
4861
- const { value: value2, kind: kind2, position: position2, normalized: normalized2, stem, stemConfidence, metadata } = valueOrParams;
4862
- return {
4863
- value: value2,
4864
- kind: kind2,
4865
- position: position2,
4866
- ...normalized2 !== void 0 && { normalized: normalized2 },
4867
- ...stem !== void 0 && { stem },
4868
- ...stemConfidence !== void 0 && { stemConfidence },
4869
- ...metadata !== void 0 && { metadata }
4870
- };
4871
- }
4872
- const value = valueOrParams;
4873
- if (!kind || !position) {
4874
- throw new Error("createToken requires kind and position parameters");
4875
- }
4876
- if (typeof normalizedOrOptions === "string") {
4877
- return { value, kind, position, normalized: normalizedOrOptions };
4878
- }
4879
- if (normalizedOrOptions) {
4880
- const { normalized: normalized2, stem, stemConfidence, metadata } = normalizedOrOptions;
4881
- return {
4882
- value,
4883
- kind,
4884
- position,
4885
- ...normalized2 !== void 0 && { normalized: normalized2 },
4886
- ...stem !== void 0 && { stem },
4887
- ...stemConfidence !== void 0 && { stemConfidence },
4888
- ...metadata !== void 0 && { metadata }
4889
- };
4890
- }
4891
- return { value, kind, position };
4892
- }
4893
- function isWhitespace(char) {
4894
- return /\s/.test(char);
4895
- }
4896
- function isSelectorStart(char) {
4897
- return char === "#" || char === "." || char === "[" || char === "@" || char === "*" || char === "<";
4898
- }
4899
- function isQuote(char) {
4900
- return char === '"' || char === "'" || char === "`" || char === "\u300C" || char === "\u300D";
4901
- }
4902
- function isDigit(char) {
4903
- return /\d/.test(char);
4904
- }
4905
- function stripOptionalDiacritics(word) {
4906
- return word.replace(/[\u064b-\u0652\u0670]/g, "");
4907
- }
4908
- function isAsciiLetter(char) {
4909
- return /[a-zA-Z]/.test(char);
4910
- }
4911
- function isAsciiIdentifierChar(char) {
4912
- return /[a-zA-Z0-9_-]/.test(char);
4913
- }
4914
-
4915
5993
  // src/core/tokenization/extractors.ts
4916
5994
  function extractCssSelector(input, startPos) {
4917
5995
  if (startPos >= input.length) return null;
@@ -6756,6 +7834,8 @@ export {
6756
7834
  InMemoryDictionary,
6757
7835
  InMemoryProfileProvider,
6758
7836
  LatinExtendedIdentifierExtractor,
7837
+ LossyTranslationError,
7838
+ MatchSkeleton,
6759
7839
  NullDictionary,
6760
7840
  NullProfileProvider,
6761
7841
  NumberExtractor,
@@ -6763,6 +7843,7 @@ export {
6763
7843
  PatternMatcher,
6764
7844
  PatternTracker,
6765
7845
  PunctuationExtractor,
7846
+ StockValueReader,
6766
7847
  StringLiteralExtractor,
6767
7848
  TokenStreamImpl,
6768
7849
  TranslationReadBackError,
@@ -6797,9 +7878,11 @@ export {
6797
7878
  createPosition,
6798
7879
  createPropertyPath,
6799
7880
  createReference,
7881
+ createSchemaPatternGenerator,
6800
7882
  createSchemaRenderer,
6801
7883
  createSelector,
6802
7884
  createSimpleTokenizer,
7885
+ createStockReader,
6803
7886
  createToken,
6804
7887
  createTokenizerContext,
6805
7888
  createTryNode,
@@ -6815,6 +7898,7 @@ export {
6815
7898
  extractUrl,
6816
7899
  extractValue,
6817
7900
  filterBySeverity,
7901
+ findTranslationLoss,
6818
7902
  fromEnvelopeJSON,
6819
7903
  fromError,
6820
7904
  fromInterchangeNode,
@@ -6829,6 +7913,7 @@ export {
6829
7913
  getPossessiveReference,
6830
7914
  getRoleSpec,
6831
7915
  getRoleValue,
7916
+ greedyStopMarkers,
6832
7917
  insertMarkers,
6833
7918
  isAsciiIdentifierChar,
6834
7919
  isAsciiLetter,
@@ -6856,6 +7941,7 @@ export {
6856
7941
  isWhitespace,
6857
7942
  joinTokens,
6858
7943
  jsonToSemanticNode,
7944
+ legacyMarkerAlternatives,
6859
7945
  lookupKeyword,
6860
7946
  lookupMarker,
6861
7947
  matchBest,
@@ -6868,24 +7954,33 @@ export {
6868
7954
  parseExplicit,
6869
7955
  parseJSONL,
6870
7956
  patternMatcher,
7957
+ readGreedy,
7958
+ readRoleValue,
6871
7959
  registryToAOTBackends,
6872
7960
  renderDocument,
6873
7961
  renderExplicit,
6874
7962
  renderExpr,
7963
+ renderPattern,
6875
7964
  reorderRoles,
7965
+ resolveMarkerForRole,
7966
+ schemaMarkerAlternatives,
7967
+ selectRenderPattern,
6876
7968
  semanticNodeToJSON,
6877
7969
  semanticNodeToRuntimeAST,
6878
7970
  semanticValueToAST,
6879
7971
  sortRolesByWordOrder,
7972
+ stockConfidenceModel,
6880
7973
  stripOptionalDiacritics,
6881
7974
  synthesizeFromSchemas,
6882
7975
  toEnvelopeJSON,
6883
7976
  toJSONL,
6884
7977
  toJSONLRow,
6885
7978
  toProtocolJSON,
7979
+ unconsumedSpans,
6886
7980
  validateProtocolJSON,
6887
7981
  validateSemanticJSON,
6888
7982
  validateValueType,
6889
- withDefaultExtractors
7983
+ withDefaultExtractors,
7984
+ withoutVariantOnlyRoles
6890
7985
  };
6891
7986
  //# sourceMappingURL=index.js.map