@lokascript/framework 4.5.0 → 4.7.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 (123) hide show
  1. package/CHANGELOG.md +220 -8
  2. package/dist/api/create-dsl.d.ts +59 -1
  3. package/dist/api/create-dsl.d.ts.map +1 -1
  4. package/dist/api/domain-registry.d.ts.map +1 -1
  5. package/dist/api/index.d.ts +1 -0
  6. package/dist/api/index.d.ts.map +1 -1
  7. package/dist/api/index.js +1938 -724
  8. package/dist/api/index.js.map +1 -1
  9. package/dist/api/reading.d.ts +8 -0
  10. package/dist/api/reading.d.ts.map +1 -0
  11. package/dist/api/translation-loss.d.ts +84 -0
  12. package/dist/api/translation-loss.d.ts.map +1 -0
  13. package/dist/core/index.d.ts +1 -0
  14. package/dist/core/index.d.ts.map +1 -1
  15. package/dist/core/index.js +1047 -684
  16. package/dist/core/index.js.map +1 -1
  17. package/dist/core/matching/confidence-model.d.ts +54 -0
  18. package/dist/core/matching/confidence-model.d.ts.map +1 -0
  19. package/dist/core/matching/index.d.ts +12 -0
  20. package/dist/core/matching/index.d.ts.map +1 -0
  21. package/dist/core/matching/match-skeleton.d.ts +114 -0
  22. package/dist/core/matching/match-skeleton.d.ts.map +1 -0
  23. package/dist/core/matching/stock-confidence.d.ts +15 -0
  24. package/dist/core/matching/stock-confidence.d.ts.map +1 -0
  25. package/dist/core/matching/stock-reader.d.ts +56 -0
  26. package/dist/core/matching/stock-reader.d.ts.map +1 -0
  27. package/dist/core/matching/stock-reading.d.ts +44 -0
  28. package/dist/core/matching/stock-reading.d.ts.map +1 -0
  29. package/dist/core/matching/value-reader.d.ts +62 -0
  30. package/dist/core/matching/value-reader.d.ts.map +1 -0
  31. package/dist/core/pattern-matching/index.js +561 -660
  32. package/dist/core/pattern-matching/index.js.map +1 -1
  33. package/dist/core/pattern-matching/pattern-matcher.d.ts +0 -117
  34. package/dist/core/pattern-matching/pattern-matcher.d.ts.map +1 -1
  35. package/dist/core/types.d.ts +48 -0
  36. package/dist/core/types.d.ts.map +1 -1
  37. package/dist/core/types.js.map +1 -1
  38. package/dist/generation/index.d.ts +2 -0
  39. package/dist/generation/index.d.ts.map +1 -1
  40. package/dist/generation/index.js +583 -1
  41. package/dist/generation/index.js.map +1 -1
  42. package/dist/generation/pattern-render.d.ts +72 -0
  43. package/dist/generation/pattern-render.d.ts.map +1 -0
  44. package/dist/generation/schema-patterns.d.ts +152 -0
  45. package/dist/generation/schema-patterns.d.ts.map +1 -0
  46. package/dist/index.cjs +2132 -890
  47. package/dist/index.cjs.map +1 -1
  48. package/dist/index.js +2110 -887
  49. package/dist/index.js.map +1 -1
  50. package/dist/ir/index.js.map +1 -1
  51. package/dist/parsing/index.js +561 -660
  52. package/dist/parsing/index.js.map +1 -1
  53. package/dist/testing/gate/index.d.ts +8 -0
  54. package/dist/testing/gate/index.d.ts.map +1 -0
  55. package/dist/testing/gate/index.js +632 -0
  56. package/dist/testing/gate/index.js.map +1 -0
  57. package/dist/testing/gate/lint/format.d.ts +7 -0
  58. package/dist/testing/gate/lint/format.d.ts.map +1 -0
  59. package/dist/testing/gate/lint/index.d.ts +30 -0
  60. package/dist/testing/gate/lint/index.d.ts.map +1 -0
  61. package/dist/testing/gate/lint/rules/extractor-coverage.d.ts +18 -0
  62. package/dist/testing/gate/lint/rules/extractor-coverage.d.ts.map +1 -0
  63. package/dist/testing/gate/lint/rules/index.d.ts +14 -0
  64. package/dist/testing/gate/lint/rules/index.d.ts.map +1 -0
  65. package/dist/testing/gate/lint/rules/keyword-classification.d.ts +19 -0
  66. package/dist/testing/gate/lint/rules/keyword-classification.d.ts.map +1 -0
  67. package/dist/testing/gate/lint/rules/keyword-coverage.d.ts +13 -0
  68. package/dist/testing/gate/lint/rules/keyword-coverage.d.ts.map +1 -0
  69. package/dist/testing/gate/lint/rules/marker-tokenization.d.ts +20 -0
  70. package/dist/testing/gate/lint/rules/marker-tokenization.d.ts.map +1 -0
  71. package/dist/testing/gate/lint/rules/position-ordering.d.ts +25 -0
  72. package/dist/testing/gate/lint/rules/position-ordering.d.ts.map +1 -0
  73. package/dist/testing/gate/lint/rules/renderer-coherence.d.ts +24 -0
  74. package/dist/testing/gate/lint/rules/renderer-coherence.d.ts.map +1 -0
  75. package/dist/testing/gate/lint/rules/schema-rules.d.ts +10 -0
  76. package/dist/testing/gate/lint/rules/schema-rules.d.ts.map +1 -0
  77. package/dist/testing/gate/lint/types.d.ts +74 -0
  78. package/dist/testing/gate/lint/types.d.ts.map +1 -0
  79. package/dist/testing/gate/round-trip.d.ts +163 -0
  80. package/dist/testing/gate/round-trip.d.ts.map +1 -0
  81. package/package.json +7 -3
  82. package/src/__test__/fixtures/sql-dsl.ts +11 -2
  83. package/src/__test__/sql-integration.test.ts +2 -2
  84. package/src/api/create-dsl-engine.test.ts +86 -0
  85. package/src/api/create-dsl.test.ts +120 -0
  86. package/src/api/create-dsl.ts +314 -39
  87. package/src/api/domain-registry.test.ts +70 -5
  88. package/src/api/domain-registry.ts +82 -1
  89. package/src/api/index.ts +1 -0
  90. package/src/api/reading.ts +29 -0
  91. package/src/api/translation-loss.test.ts +89 -0
  92. package/src/api/translation-loss.ts +128 -0
  93. package/src/core/index.ts +1 -0
  94. package/src/core/matching/confidence-model.ts +58 -0
  95. package/src/core/matching/index.ts +12 -0
  96. package/src/core/matching/match-skeleton.test.ts +179 -0
  97. package/src/core/matching/match-skeleton.ts +621 -0
  98. package/src/core/matching/stock-confidence.ts +238 -0
  99. package/src/core/matching/stock-reader.test.ts +181 -0
  100. package/src/core/matching/stock-reader.ts +186 -0
  101. package/src/core/matching/stock-reading.ts +681 -0
  102. package/src/core/matching/value-reader.ts +85 -0
  103. package/src/core/pattern-matching/pattern-matcher.ts +11 -876
  104. package/src/core/types.ts +48 -0
  105. package/src/generation/index.ts +2 -0
  106. package/src/generation/pattern-render.ts +378 -0
  107. package/src/generation/schema-patterns.test.ts +99 -0
  108. package/src/generation/schema-patterns.ts +962 -0
  109. package/src/testing/gate/index.ts +8 -0
  110. package/src/testing/gate/lint/format.ts +23 -0
  111. package/src/testing/gate/lint/index.ts +73 -0
  112. package/src/testing/gate/lint/lint.test.ts +131 -0
  113. package/src/testing/gate/lint/rules/extractor-coverage.ts +63 -0
  114. package/src/testing/gate/lint/rules/index.ts +37 -0
  115. package/src/testing/gate/lint/rules/keyword-classification.ts +52 -0
  116. package/src/testing/gate/lint/rules/keyword-coverage.ts +38 -0
  117. package/src/testing/gate/lint/rules/marker-tokenization.ts +129 -0
  118. package/src/testing/gate/lint/rules/position-ordering.ts +87 -0
  119. package/src/testing/gate/lint/rules/renderer-coherence.ts +100 -0
  120. package/src/testing/gate/lint/rules/schema-rules.ts +35 -0
  121. package/src/testing/gate/lint/types.ts +83 -0
  122. package/src/testing/gate/round-trip.test.ts +118 -0
  123. package/src/testing/gate/round-trip.ts +438 -0
package/dist/api/index.js CHANGED
@@ -108,10 +108,535 @@ function getPossessiveReference(profile, keyword) {
108
108
  return profile?.possessive?.keywords?.[keyword];
109
109
  }
110
110
 
111
- // src/core/pattern-matching/pattern-matcher.ts
111
+ // src/core/matching/stock-reading.ts
112
112
  function isValidReference(_value) {
113
113
  return false;
114
114
  }
115
+ function safeToLowerCase(value) {
116
+ if (typeof value === "string") {
117
+ return value.toLowerCase();
118
+ }
119
+ if (value === null || value === void 0) {
120
+ return "";
121
+ }
122
+ return String(value).toLowerCase();
123
+ }
124
+ function readRoleValue(tokens, patternToken, captured, profile) {
125
+ skipNoiseWords(tokens);
126
+ const token = tokens.peek();
127
+ if (!token) {
128
+ return patternToken.optional || false;
129
+ }
130
+ const possessiveValue = tryMatchPossessiveExpression(tokens, profile);
131
+ if (possessiveValue) {
132
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
133
+ if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
134
+ return patternToken.optional || false;
135
+ }
136
+ }
137
+ captured.set(patternToken.role, possessiveValue);
138
+ return true;
139
+ }
140
+ const methodCallValue = tryMatchMethodCallExpression(tokens);
141
+ if (methodCallValue) {
142
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
143
+ if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
144
+ return patternToken.optional || false;
145
+ }
146
+ }
147
+ captured.set(patternToken.role, methodCallValue);
148
+ return true;
149
+ }
150
+ const possessiveSelectorValue = tryMatchPossessiveSelectorExpression(tokens);
151
+ if (possessiveSelectorValue) {
152
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
153
+ if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
154
+ return patternToken.optional || false;
155
+ }
156
+ }
157
+ captured.set(patternToken.role, possessiveSelectorValue);
158
+ return true;
159
+ }
160
+ const propertyAccessValue = tryMatchPropertyAccessExpression(tokens);
161
+ if (propertyAccessValue) {
162
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
163
+ if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
164
+ return patternToken.optional || false;
165
+ }
166
+ }
167
+ captured.set(patternToken.role, propertyAccessValue);
168
+ return true;
169
+ }
170
+ const selectorPropertyValue = tryMatchSelectorPropertyExpression(tokens);
171
+ if (selectorPropertyValue) {
172
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
173
+ if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
174
+ return patternToken.optional || false;
175
+ }
176
+ }
177
+ captured.set(patternToken.role, selectorPropertyValue);
178
+ return true;
179
+ }
180
+ const value = tokenToSemanticValue(token);
181
+ if (!value) {
182
+ return patternToken.optional || false;
183
+ }
184
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
185
+ if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
186
+ return patternToken.optional || false;
187
+ }
188
+ }
189
+ captured.set(patternToken.role, value);
190
+ tokens.advance();
191
+ return true;
192
+ }
193
+ function tryMatchPossessiveExpression(tokens, profile) {
194
+ const token = tokens.peek();
195
+ if (!token) return null;
196
+ if (!profile) return null;
197
+ const tokenValue = token.normalized || token.value;
198
+ const tokenLower = safeToLowerCase(tokenValue);
199
+ const baseRef = getPossessiveReference(profile, tokenLower);
200
+ if (!baseRef) return null;
201
+ const mark = tokens.mark();
202
+ tokens.advance();
203
+ const propertyToken = tokens.peek();
204
+ if (!propertyToken) {
205
+ tokens.reset(mark);
206
+ return null;
207
+ }
208
+ 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)) {
209
+ tokens.advance();
210
+ let propertyName = propertyToken.value;
211
+ if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
212
+ propertyName = propertyName.substring(1);
213
+ }
214
+ let chainedProps = propertyName;
215
+ while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
216
+ chainedProps += tokens.peek().value;
217
+ tokens.advance();
218
+ }
219
+ const nextPeek = tokens.peek();
220
+ if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
221
+ chainedProps += nextPeek.value;
222
+ tokens.advance();
223
+ }
224
+ return createPropertyPath(createReference(baseRef), chainedProps);
225
+ }
226
+ tokens.reset(mark);
227
+ return null;
228
+ }
229
+ function isStructuralKeyword(value) {
230
+ const structural = /* @__PURE__ */ new Set([
231
+ // Prepositions
232
+ "into",
233
+ "in",
234
+ "to",
235
+ "from",
236
+ "at",
237
+ "by",
238
+ "with",
239
+ "without",
240
+ "before",
241
+ "after",
242
+ "of",
243
+ "as",
244
+ "on",
245
+ // Control flow
246
+ "then",
247
+ "end",
248
+ "else",
249
+ "if",
250
+ "repeat",
251
+ "while",
252
+ "for",
253
+ // Commands (shouldn't be property names)
254
+ "toggle",
255
+ "add",
256
+ "remove",
257
+ "put",
258
+ "set",
259
+ "show",
260
+ "hide",
261
+ "increment",
262
+ "decrement",
263
+ "send",
264
+ "trigger",
265
+ "call"
266
+ ]);
267
+ return structural.has(value.toLowerCase());
268
+ }
269
+ function tryMatchMethodCallExpression(tokens) {
270
+ const token = tokens.peek();
271
+ if (!token || token.kind !== "selector") return null;
272
+ const mark = tokens.mark();
273
+ tokens.advance();
274
+ const dotToken = tokens.peek();
275
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
276
+ tokens.reset(mark);
277
+ return null;
278
+ }
279
+ tokens.advance();
280
+ const methodToken = tokens.peek();
281
+ if (!methodToken || methodToken.kind !== "identifier") {
282
+ tokens.reset(mark);
283
+ return null;
284
+ }
285
+ tokens.advance();
286
+ const openParen = tokens.peek();
287
+ if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
288
+ tokens.reset(mark);
289
+ return null;
290
+ }
291
+ tokens.advance();
292
+ const args = [];
293
+ while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
294
+ const argToken = tokens.peek();
295
+ if (!argToken) break;
296
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
297
+ tokens.advance();
298
+ break;
299
+ }
300
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
301
+ tokens.advance();
302
+ continue;
303
+ }
304
+ args.push(argToken.value);
305
+ tokens.advance();
306
+ }
307
+ const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
308
+ return {
309
+ type: "expression",
310
+ raw: methodCall
311
+ };
312
+ }
313
+ function tryMatchPropertyAccessExpression(tokens) {
314
+ const token = tokens.peek();
315
+ if (!token) return null;
316
+ if (token.kind !== "identifier" && token.kind !== "keyword") return null;
317
+ const mark = tokens.mark();
318
+ tokens.advance();
319
+ const dotToken = tokens.peek();
320
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
321
+ tokens.reset(mark);
322
+ return null;
323
+ }
324
+ tokens.advance();
325
+ const propertyToken = tokens.peek();
326
+ if (!propertyToken || propertyToken.kind !== "identifier") {
327
+ tokens.reset(mark);
328
+ return null;
329
+ }
330
+ tokens.advance();
331
+ let chain = `${token.value}.${propertyToken.value}`;
332
+ let depth = 1;
333
+ while (!tokens.isAtEnd() && depth < MAX_PROPERTY_DEPTH) {
334
+ const nextDot = tokens.peek();
335
+ if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
336
+ break;
337
+ }
338
+ tokens.advance();
339
+ const nextProp = tokens.peek();
340
+ if (!nextProp || nextProp.kind !== "identifier") {
341
+ break;
342
+ }
343
+ tokens.advance();
344
+ chain += `.${nextProp.value}`;
345
+ depth++;
346
+ }
347
+ const openParen = tokens.peek();
348
+ if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
349
+ tokens.advance();
350
+ const args = [];
351
+ let argDepth = 0;
352
+ while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
353
+ const argToken = tokens.peek();
354
+ if (!argToken) break;
355
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
356
+ if (argDepth === 0) {
357
+ tokens.advance();
358
+ break;
359
+ }
360
+ argDepth--;
361
+ }
362
+ if (argToken.kind === "punctuation" && argToken.value === "(") {
363
+ argDepth++;
364
+ }
365
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
366
+ tokens.advance();
367
+ continue;
368
+ }
369
+ args.push(argToken.value);
370
+ tokens.advance();
371
+ }
372
+ const methodCall = `${chain}(${args.join(", ")})`;
373
+ return {
374
+ type: "expression",
375
+ raw: methodCall
376
+ };
377
+ }
378
+ return {
379
+ type: "expression",
380
+ raw: chain
381
+ };
382
+ }
383
+ function tryMatchPossessiveSelectorExpression(tokens) {
384
+ const token = tokens.peek();
385
+ if (!token || token.kind !== "selector") return null;
386
+ const mark = tokens.mark();
387
+ tokens.advance();
388
+ const possessiveToken = tokens.peek();
389
+ if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
390
+ tokens.reset(mark);
391
+ return null;
392
+ }
393
+ tokens.advance();
394
+ const propertyToken = tokens.peek();
395
+ if (!propertyToken) {
396
+ tokens.reset(mark);
397
+ return null;
398
+ }
399
+ if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
400
+ tokens.reset(mark);
401
+ return null;
402
+ }
403
+ tokens.advance();
404
+ return createPropertyPath(createSelector(token.value), propertyToken.value);
405
+ }
406
+ function tryMatchSelectorPropertyExpression(tokens) {
407
+ const token = tokens.peek();
408
+ if (!token || token.kind !== "selector") return null;
409
+ if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
410
+ const mark = tokens.mark();
411
+ tokens.advance();
412
+ const propertyToken = tokens.peek();
413
+ if (!propertyToken || propertyToken.kind !== "selector") {
414
+ tokens.reset(mark);
415
+ return null;
416
+ }
417
+ if (!propertyToken.value.startsWith(".")) {
418
+ tokens.reset(mark);
419
+ return null;
420
+ }
421
+ const peek2 = tokens.peek(1);
422
+ if (peek2 && peek2.kind === "selector") {
423
+ }
424
+ tokens.advance();
425
+ const propertyName = propertyToken.value.slice(1);
426
+ return createPropertyPath(createSelector(token.value), propertyName);
427
+ }
428
+ var MAX_PROPERTY_DEPTH = 10;
429
+ var MAX_METHOD_ARGS = 20;
430
+ function tokenToSemanticValue(token) {
431
+ switch (token.kind) {
432
+ case "selector":
433
+ return createSelector(token.value);
434
+ case "literal":
435
+ return parseLiteralValue(token.value);
436
+ case "keyword": {
437
+ const tokenValue = token.normalized || token.value;
438
+ const lower = safeToLowerCase(tokenValue);
439
+ if (isValidReference(lower)) {
440
+ return createReference(lower);
441
+ }
442
+ return createLiteral(token.normalized || token.value);
443
+ }
444
+ case "identifier": {
445
+ if (typeof token.value === "string" && token.value.startsWith(":")) {
446
+ return createReference(token.value);
447
+ }
448
+ const identLower = safeToLowerCase(token.value);
449
+ if (isValidReference(identLower)) {
450
+ return createReference(identLower);
451
+ }
452
+ return { type: "expression", raw: token.value };
453
+ }
454
+ case "url":
455
+ return createLiteral(token.value, "string");
456
+ default:
457
+ return null;
458
+ }
459
+ }
460
+ function parseLiteralValue(value) {
461
+ if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
462
+ const inner = value.slice(1, -1);
463
+ return createLiteral(inner, "string");
464
+ }
465
+ if (value === "true") return createLiteral(true, "boolean");
466
+ if (value === "false") return createLiteral(false, "boolean");
467
+ const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
468
+ if (durationMatch) {
469
+ const num2 = parseFloat(durationMatch[1]);
470
+ const unit = durationMatch[2];
471
+ if (unit) {
472
+ return createLiteral(value, "duration");
473
+ }
474
+ return createLiteral(num2, "number");
475
+ }
476
+ const num = parseFloat(value);
477
+ if (!isNaN(num)) {
478
+ return createLiteral(num, "number");
479
+ }
480
+ return createLiteral(value, "string");
481
+ }
482
+ var ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
483
+ function skipNoiseWords(tokens) {
484
+ const token = tokens.peek();
485
+ if (!token) return;
486
+ const tokenLower = safeToLowerCase(token.value);
487
+ if (ENGLISH_NOISE_WORDS.has(tokenLower)) {
488
+ const mark = tokens.mark();
489
+ tokens.advance();
490
+ const nextToken = tokens.peek();
491
+ if (nextToken && nextToken.kind === "selector") {
492
+ return;
493
+ }
494
+ tokens.reset(mark);
495
+ }
496
+ if (tokenLower === "class") {
497
+ tokens.advance();
498
+ }
499
+ }
500
+
501
+ // src/core/matching/stock-confidence.ts
502
+ function scoreParts(ctx) {
503
+ const { pattern, captured } = ctx;
504
+ let score = 0;
505
+ let maxScore = 0;
506
+ const hasDefault = (role) => {
507
+ return pattern.extraction?.[role]?.default !== void 0;
508
+ };
509
+ for (const token of pattern.template.tokens) {
510
+ if (token.type === "role") {
511
+ maxScore += 1;
512
+ if (captured.has(token.role)) {
513
+ score += 1;
514
+ }
515
+ } else if (token.type === "group") {
516
+ for (const subToken of token.tokens) {
517
+ if (subToken.type === "role") {
518
+ const roleHasDefault = hasDefault(subToken.role);
519
+ const weight = 0.8;
520
+ maxScore += weight;
521
+ if (captured.has(subToken.role)) {
522
+ score += weight;
523
+ } else if (roleHasDefault) {
524
+ score += weight * 0.6;
525
+ }
526
+ }
527
+ }
528
+ }
529
+ }
530
+ const roleCoverage = maxScore > 0 ? score / maxScore : 1;
531
+ let baseConfidence = roleCoverage;
532
+ let stemPenalty = 0;
533
+ if (ctx.stemMatchCount > 0 && ctx.totalKeywordMatches > 0) {
534
+ stemPenalty = ctx.stemMatchCount / ctx.totalKeywordMatches * 0.15;
535
+ baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
536
+ }
537
+ const vsoBoost = calculateVSOConfidenceBoost(pattern);
538
+ baseConfidence = Math.min(1, baseConfidence + vsoBoost);
539
+ const prepositionAdjustment = arabicPrepositionDisambiguation(pattern, captured);
540
+ baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
541
+ return {
542
+ roleCoverage,
543
+ stemPenalty,
544
+ languageBoost: vsoBoost,
545
+ languageAdjustment: prepositionAdjustment,
546
+ final: baseConfidence
547
+ };
548
+ }
549
+ function calculateVSOConfidenceBoost(pattern) {
550
+ if (pattern.language !== "ar") {
551
+ return 0;
552
+ }
553
+ const firstToken = pattern.template.tokens[0];
554
+ if (!firstToken || firstToken.type !== "literal") {
555
+ return 0;
556
+ }
557
+ const ARABIC_VERBS = /* @__PURE__ */ new Set([
558
+ "\u0628\u062F\u0644",
559
+ "\u063A\u064A\u0631",
560
+ "\u0623\u0636\u0641",
561
+ "\u0623\u0632\u0644",
562
+ "\u0636\u0639",
563
+ "\u0627\u062C\u0639\u0644",
564
+ "\u0639\u064A\u0646",
565
+ "\u0632\u062F",
566
+ "\u0627\u0646\u0642\u0635",
567
+ "\u0633\u062C\u0644",
568
+ "\u0623\u0638\u0647\u0631",
569
+ "\u0623\u062E\u0641",
570
+ "\u0634\u063A\u0644",
571
+ "\u0623\u0631\u0633\u0644",
572
+ "\u0631\u0643\u0632",
573
+ "\u0634\u0648\u0634",
574
+ "\u062A\u0648\u0642\u0641",
575
+ "\u0627\u0646\u0633\u062E",
576
+ "\u0627\u062D\u0630\u0641",
577
+ "\u0627\u0635\u0646\u0639",
578
+ "\u0627\u0646\u062A\u0638\u0631",
579
+ "\u0627\u0646\u062A\u0642\u0627\u0644",
580
+ "\u0623\u0648"
581
+ ]);
582
+ if (ARABIC_VERBS.has(firstToken.value)) {
583
+ return 0.15;
584
+ }
585
+ if (firstToken.alternatives) {
586
+ for (const alt of firstToken.alternatives) {
587
+ if (ARABIC_VERBS.has(alt)) {
588
+ return 0.15;
589
+ }
590
+ }
591
+ }
592
+ return 0;
593
+ }
594
+ function arabicPrepositionDisambiguation(pattern, captured) {
595
+ if (pattern.language !== "ar") {
596
+ return 0;
597
+ }
598
+ let adjustment = 0;
599
+ const PREFERRED_PREPOSITIONS = {
600
+ patient: ["\u0639\u0644\u0649"],
601
+ // element selectors prefer على (on/upon)
602
+ destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
603
+ // destination prefers إلى (to)
604
+ source: ["\u0645\u0646"],
605
+ // source prefers من (from)
606
+ agent: ["\u0645\u0646"],
607
+ // agent/by prefers من (from/by)
608
+ manner: ["\u0628"],
609
+ // manner prefers ب (with/by)
610
+ style: ["\u0628"],
611
+ // style prefers ب (with)
612
+ goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
613
+ // target state prefers إلى (to)
614
+ method: ["\u0628"]
615
+ // method prefers ب (with/by)
616
+ };
617
+ for (const [role, value] of captured.entries()) {
618
+ const preferred = PREFERRED_PREPOSITIONS[role];
619
+ if (!preferred || preferred.length === 0) {
620
+ continue;
621
+ }
622
+ const metadata = "metadata" in value ? value.metadata : void 0;
623
+ if (metadata && typeof metadata.prepositionValue === "string") {
624
+ const usedPreposition = metadata.prepositionValue;
625
+ if (preferred.includes(usedPreposition)) {
626
+ adjustment += 0.1;
627
+ } else {
628
+ adjustment -= 0.1;
629
+ }
630
+ }
631
+ }
632
+ return Math.max(-0.1, Math.min(0.1, adjustment));
633
+ }
634
+ var stockConfidenceModel = {
635
+ calculate: (ctx) => scoreParts(ctx).final,
636
+ calculateWithBreakdown: (ctx) => scoreParts(ctx)
637
+ };
638
+
639
+ // src/core/pattern-matching/pattern-matcher.ts
115
640
  var _PatternMatcher = class _PatternMatcher {
116
641
  constructor() {
117
642
  /** Debug logger */
@@ -123,19 +648,6 @@ var _PatternMatcher = class _PatternMatcher {
123
648
  this.stemMatchCount = 0;
124
649
  this.totalKeywordMatches = 0;
125
650
  }
126
- /**
127
- * Safely convert a value to lowercase string.
128
- * Provides protection against non-string values at runtime.
129
- */
130
- safeToLowerCase(value) {
131
- if (typeof value === "string") {
132
- return value.toLowerCase();
133
- }
134
- if (value === null || value === void 0) {
135
- return "";
136
- }
137
- return String(value).toLowerCase();
138
- }
139
651
  /**
140
652
  * Try to match a single pattern against the token stream.
141
653
  * Returns the match result or null if no match.
@@ -184,7 +696,12 @@ var _PatternMatcher = class _PatternMatcher {
184
696
  tokens.reset(mark);
185
697
  return null;
186
698
  }
187
- const confidence = this.calculateConfidence(pattern, captured);
699
+ const confidence = stockConfidenceModel.calculate({
700
+ pattern,
701
+ captured,
702
+ stemMatchCount: this.stemMatchCount,
703
+ totalKeywordMatches: this.totalKeywordMatches
704
+ });
188
705
  this.applyExtractionRules(pattern, captured);
189
706
  return {
190
707
  pattern,
@@ -369,7 +886,7 @@ var _PatternMatcher = class _PatternMatcher {
369
886
  case "literal":
370
887
  return this.matchLiteralToken(tokens, patternToken);
371
888
  case "role":
372
- return this.matchRoleToken(tokens, patternToken, captured);
889
+ return readRoleValue(tokens, patternToken, captured, this.currentProfile);
373
890
  case "group":
374
891
  return this.matchGroupToken(tokens, patternToken, captured);
375
892
  default:
@@ -427,420 +944,54 @@ var _PatternMatcher = class _PatternMatcher {
427
944
  return false;
428
945
  }
429
946
  /**
430
- * Match a role pattern token (captures a semantic value).
431
- * Handles multi-token expressions like:
432
- * - 'my value' (possessive keyword + property)
433
- * - '#dialog.showModal()' (method call)
434
- * - "#element's *opacity" (possessive selector + property)
947
+ * Match a group pattern token (optional sequence).
948
+ * When the group's leading marker isn't at the current position, scans ahead
949
+ * up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
950
+ * (e.g., a value like 'hello') to sit between groups without blocking later
951
+ * marker matches.
435
952
  */
436
- matchRoleToken(tokens, patternToken, captured) {
437
- this.logger.debug(" >>> matchRoleToken ENTRY: capturing role", patternToken.role);
438
- this.logger.debug(" >>> matchRoleToken: expected types", patternToken.expectedTypes);
439
- this.logger.debug(" >>> matchRoleToken: optional?", patternToken.optional);
440
- this.skipNoiseWords(tokens);
441
- const token = tokens.peek();
442
- this.logger.debug(
443
- " >>> After skipNoiseWords, current token:",
444
- token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
953
+ matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
954
+ const mark = tokens.mark();
955
+ const capturedBefore = new Set(captured.keys());
956
+ const success = this.matchTokenSequence(
957
+ tokens,
958
+ patternToken.tokens,
959
+ captured,
960
+ parentStopMarkers
445
961
  );
446
- if (!token) {
447
- return patternToken.optional || false;
448
- }
449
- const possessiveValue = this.tryMatchPossessiveExpression(tokens);
450
- if (possessiveValue) {
451
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
452
- if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
453
- return patternToken.optional || false;
454
- }
455
- }
456
- captured.set(patternToken.role, possessiveValue);
457
- return true;
458
- }
459
- const methodCallValue = this.tryMatchMethodCallExpression(tokens);
460
- if (methodCallValue) {
461
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
462
- if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
463
- return patternToken.optional || false;
464
- }
465
- }
466
- captured.set(patternToken.role, methodCallValue);
467
- return true;
468
- }
469
- const possessiveSelectorValue = this.tryMatchPossessiveSelectorExpression(tokens);
470
- if (possessiveSelectorValue) {
471
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
472
- if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
473
- return patternToken.optional || false;
474
- }
475
- }
476
- captured.set(patternToken.role, possessiveSelectorValue);
477
- return true;
478
- }
479
- const propertyAccessValue = this.tryMatchPropertyAccessExpression(tokens);
480
- if (propertyAccessValue) {
481
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
482
- if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
483
- return patternToken.optional || false;
484
- }
485
- }
486
- captured.set(patternToken.role, propertyAccessValue);
487
- return true;
962
+ if (success) return true;
963
+ tokens.reset(mark);
964
+ for (const role of captured.keys()) {
965
+ if (!capturedBefore.has(role)) captured.delete(role);
488
966
  }
489
- const selectorPropertyValue = this.tryMatchSelectorPropertyExpression(tokens);
490
- if (selectorPropertyValue) {
491
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
492
- if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
493
- return patternToken.optional || false;
494
- }
495
- }
496
- captured.set(patternToken.role, selectorPropertyValue);
497
- return true;
498
- }
499
- this.logger.debug(
500
- " >>> Trying tokenToSemanticValue for token:",
501
- token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
502
- );
503
- const value = this.tokenToSemanticValue(token);
504
- this.logger.debug(
505
- " >>> tokenToSemanticValue returned:",
506
- value ? JSON.stringify(value) : "null"
507
- );
508
- if (!value) {
509
- return patternToken.optional || false;
510
- }
511
- this.logger.debug(
512
- " >>> Validating type:",
513
- value.type,
514
- "against expected:",
515
- patternToken.expectedTypes
516
- );
517
- if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
518
- if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
519
- this.logger.debug(" >>> TYPE MISMATCH - returning", patternToken.optional || false);
520
- return patternToken.optional || false;
521
- }
522
- }
523
- this.logger.debug(" >>> Type validation PASSED");
524
- captured.set(patternToken.role, value);
525
- tokens.advance();
526
- return true;
527
- }
528
- /**
529
- * Try to match a possessive expression like 'my value' or 'its innerHTML'.
530
- * Returns the PropertyPathValue if matched, or null if not.
531
- */
532
- tryMatchPossessiveExpression(tokens) {
533
- const token = tokens.peek();
534
- if (!token) return null;
535
- if (!this.currentProfile) return null;
536
- const tokenValue = token.normalized || token.value;
537
- const tokenLower = this.safeToLowerCase(tokenValue);
538
- const baseRef = getPossessiveReference(this.currentProfile, tokenLower);
539
- if (!baseRef) return null;
540
- const mark = tokens.mark();
541
- tokens.advance();
542
- const propertyToken = tokens.peek();
543
- if (!propertyToken) {
544
- tokens.reset(mark);
545
- return null;
546
- }
547
- 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)) {
548
- tokens.advance();
549
- let propertyName = propertyToken.value;
550
- if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
551
- propertyName = propertyName.substring(1);
552
- }
553
- let chainedProps = propertyName;
554
- while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
555
- chainedProps += tokens.peek().value;
556
- tokens.advance();
557
- }
558
- const nextPeek = tokens.peek();
559
- if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
560
- chainedProps += nextPeek.value;
561
- tokens.advance();
562
- }
563
- return createPropertyPath(createReference(baseRef), chainedProps);
564
- }
565
- tokens.reset(mark);
566
- return null;
567
- }
568
- /**
569
- * Check if a keyword is a structural keyword (preposition, control flow, etc.)
570
- * that shouldn't be consumed as a property name.
571
- */
572
- isStructuralKeyword(value) {
573
- const structural = /* @__PURE__ */ new Set([
574
- // Prepositions
575
- "into",
576
- "in",
577
- "to",
578
- "from",
579
- "at",
580
- "by",
581
- "with",
582
- "without",
583
- "before",
584
- "after",
585
- "of",
586
- "as",
587
- "on",
588
- // Control flow
589
- "then",
590
- "end",
591
- "else",
592
- "if",
593
- "repeat",
594
- "while",
595
- "for",
596
- // Commands (shouldn't be property names)
597
- "toggle",
598
- "add",
599
- "remove",
600
- "put",
601
- "set",
602
- "show",
603
- "hide",
604
- "increment",
605
- "decrement",
606
- "send",
607
- "trigger",
608
- "call"
609
- ]);
610
- return structural.has(value.toLowerCase());
611
- }
612
- /**
613
- * Try to match a method call expression like '#dialog.showModal()'.
614
- * Pattern: selector + '.' + identifier + '(' + [args] + ')'
615
- * Returns an expression value if matched, or null if not.
616
- */
617
- tryMatchMethodCallExpression(tokens) {
618
- const token = tokens.peek();
619
- if (!token || token.kind !== "selector") return null;
620
- const mark = tokens.mark();
621
- tokens.advance();
622
- const dotToken = tokens.peek();
623
- if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
624
- tokens.reset(mark);
625
- return null;
626
- }
627
- tokens.advance();
628
- const methodToken = tokens.peek();
629
- if (!methodToken || methodToken.kind !== "identifier") {
630
- tokens.reset(mark);
631
- return null;
632
- }
633
- tokens.advance();
634
- const openParen = tokens.peek();
635
- if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
636
- tokens.reset(mark);
637
- return null;
638
- }
639
- tokens.advance();
640
- const args = [];
641
- while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
642
- const argToken = tokens.peek();
643
- if (!argToken) break;
644
- if (argToken.kind === "punctuation" && argToken.value === ")") {
645
- tokens.advance();
646
- break;
647
- }
648
- if (argToken.kind === "punctuation" && argToken.value === ",") {
649
- tokens.advance();
650
- continue;
651
- }
652
- args.push(argToken.value);
653
- tokens.advance();
654
- }
655
- const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
656
- return {
657
- type: "expression",
658
- raw: methodCall
659
- };
660
- }
661
- /**
662
- * Try to match a property access expression like 'userData.name' or 'it.data'.
663
- * Pattern: (identifier | keyword) + '.' + identifier [+ '.' + identifier ...]
664
- * Returns an expression value if matched, or null if not.
665
- */
666
- tryMatchPropertyAccessExpression(tokens) {
667
- const token = tokens.peek();
668
- if (!token) return null;
669
- if (token.kind !== "identifier" && token.kind !== "keyword") return null;
670
- const mark = tokens.mark();
671
- tokens.advance();
672
- const dotToken = tokens.peek();
673
- if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
674
- tokens.reset(mark);
675
- return null;
676
- }
677
- tokens.advance();
678
- const propertyToken = tokens.peek();
679
- if (!propertyToken || propertyToken.kind !== "identifier") {
680
- tokens.reset(mark);
681
- return null;
682
- }
683
- tokens.advance();
684
- let chain = `${token.value}.${propertyToken.value}`;
685
- let depth = 1;
686
- while (!tokens.isAtEnd() && depth < _PatternMatcher.MAX_PROPERTY_DEPTH) {
687
- const nextDot = tokens.peek();
688
- if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
689
- break;
690
- }
691
- tokens.advance();
692
- const nextProp = tokens.peek();
693
- if (!nextProp || nextProp.kind !== "identifier") {
694
- break;
695
- }
696
- tokens.advance();
697
- chain += `.${nextProp.value}`;
698
- depth++;
699
- }
700
- const openParen = tokens.peek();
701
- if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
702
- tokens.advance();
703
- const args = [];
704
- let argDepth = 0;
705
- while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
706
- const argToken = tokens.peek();
707
- if (!argToken) break;
708
- if (argToken.kind === "punctuation" && argToken.value === ")") {
709
- if (argDepth === 0) {
710
- tokens.advance();
711
- break;
712
- }
713
- argDepth--;
714
- }
715
- if (argToken.kind === "punctuation" && argToken.value === "(") {
716
- argDepth++;
717
- }
718
- if (argToken.kind === "punctuation" && argToken.value === ",") {
719
- tokens.advance();
720
- continue;
721
- }
722
- args.push(argToken.value);
723
- tokens.advance();
724
- }
725
- const methodCall = `${chain}(${args.join(", ")})`;
726
- return {
727
- type: "expression",
728
- raw: methodCall
729
- };
730
- }
731
- return {
732
- type: "expression",
733
- raw: chain
734
- };
735
- }
736
- /**
737
- * Try to match a possessive selector expression like "#element's *opacity".
738
- * Pattern: selector + "'s" + (selector | identifier)
739
- * Returns a property-path value if matched, or null if not.
740
- */
741
- tryMatchPossessiveSelectorExpression(tokens) {
742
- const token = tokens.peek();
743
- if (!token || token.kind !== "selector") return null;
744
- const mark = tokens.mark();
745
- tokens.advance();
746
- const possessiveToken = tokens.peek();
747
- if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
748
- tokens.reset(mark);
749
- return null;
750
- }
751
- tokens.advance();
752
- const propertyToken = tokens.peek();
753
- if (!propertyToken) {
754
- tokens.reset(mark);
755
- return null;
756
- }
757
- if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
758
- tokens.reset(mark);
759
- return null;
760
- }
761
- tokens.advance();
762
- return createPropertyPath(createSelector(token.value), propertyToken.value);
763
- }
764
- /**
765
- * Try to match a selector + property expression like "#output.innerText".
766
- * This handles cases where the tokenizer produces two selector tokens:
767
- * - #output (id selector)
768
- * - .innerText (looks like class selector, but is actually property)
769
- *
770
- * Pattern: id-selector + class-selector-that-is-actually-property
771
- * Returns a property-path value if matched, or null if not.
772
- */
773
- tryMatchSelectorPropertyExpression(tokens) {
774
- const token = tokens.peek();
775
- if (!token || token.kind !== "selector") return null;
776
- if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
777
- const mark = tokens.mark();
778
- tokens.advance();
779
- const propertyToken = tokens.peek();
780
- if (!propertyToken || propertyToken.kind !== "selector") {
781
- tokens.reset(mark);
782
- return null;
783
- }
784
- if (!propertyToken.value.startsWith(".")) {
785
- tokens.reset(mark);
786
- return null;
787
- }
788
- const peek2 = tokens.peek(1);
789
- if (peek2 && peek2.kind === "selector") {
790
- }
791
- tokens.advance();
792
- const propertyName = propertyToken.value.slice(1);
793
- return createPropertyPath(createSelector(token.value), propertyName);
794
- }
795
- /**
796
- * Match a group pattern token (optional sequence).
797
- * When the group's leading marker isn't at the current position, scans ahead
798
- * up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
799
- * (e.g., a value like 'hello') to sit between groups without blocking later
800
- * marker matches.
801
- */
802
- matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
803
- const mark = tokens.mark();
804
- const capturedBefore = new Set(captured.keys());
805
- const success = this.matchTokenSequence(
806
- tokens,
807
- patternToken.tokens,
808
- captured,
809
- parentStopMarkers
810
- );
811
- if (success) return true;
812
- tokens.reset(mark);
813
- for (const role of captured.keys()) {
814
- if (!capturedBefore.has(role)) captured.delete(role);
815
- }
816
- if (!patternToken.optional) return false;
817
- const leadingMarker = this.getGroupLeadingMarker(patternToken);
818
- if (leadingMarker) {
819
- for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
820
- const ahead = tokens.peek(offset);
821
- if (!ahead) break;
822
- const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
823
- if (aheadValue === leadingMarker) {
824
- this.logger.debug(
825
- " >> MARKER SCAN: found",
826
- leadingMarker,
827
- "at offset",
828
- offset,
829
- "- skipping intervening tokens"
830
- );
831
- for (let s = 0; s < offset; s++) tokens.advance();
832
- const retrySuccess = this.matchTokenSequence(
833
- tokens,
834
- patternToken.tokens,
835
- captured,
836
- parentStopMarkers
837
- );
838
- if (retrySuccess) return true;
839
- tokens.reset(mark);
840
- for (const role of captured.keys()) {
841
- if (!capturedBefore.has(role)) captured.delete(role);
842
- }
843
- break;
967
+ if (!patternToken.optional) return false;
968
+ const leadingMarker = this.getGroupLeadingMarker(patternToken);
969
+ if (leadingMarker) {
970
+ for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
971
+ const ahead = tokens.peek(offset);
972
+ if (!ahead) break;
973
+ const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
974
+ if (aheadValue === leadingMarker) {
975
+ this.logger.debug(
976
+ " >> MARKER SCAN: found",
977
+ leadingMarker,
978
+ "at offset",
979
+ offset,
980
+ "- skipping intervening tokens"
981
+ );
982
+ for (let s = 0; s < offset; s++) tokens.advance();
983
+ const retrySuccess = this.matchTokenSequence(
984
+ tokens,
985
+ patternToken.tokens,
986
+ captured,
987
+ parentStopMarkers
988
+ );
989
+ if (retrySuccess) return true;
990
+ tokens.reset(mark);
991
+ for (const role of captured.keys()) {
992
+ if (!capturedBefore.has(role)) captured.delete(role);
993
+ }
994
+ break;
844
995
  }
845
996
  }
846
997
  }
@@ -867,7 +1018,7 @@ var _PatternMatcher = class _PatternMatcher {
867
1018
  if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
868
1019
  return "stem";
869
1020
  }
870
- if (token.kind === "keyword" && this.safeToLowerCase(token.value) === value.toLowerCase()) {
1021
+ if (token.kind === "keyword" && safeToLowerCase(token.value) === value.toLowerCase()) {
871
1022
  return "case-insensitive";
872
1023
  }
873
1024
  return "none";
@@ -915,62 +1066,9 @@ var _PatternMatcher = class _PatternMatcher {
915
1066
  if (token.normalized && stopMarkers.has(token.normalized.toLowerCase())) return true;
916
1067
  return false;
917
1068
  }
918
- /**
919
- * Convert a language token to a semantic value.
920
- */
921
- tokenToSemanticValue(token) {
922
- switch (token.kind) {
923
- case "selector":
924
- return createSelector(token.value);
925
- case "literal":
926
- return this.parseLiteralValue(token.value);
927
- case "keyword":
928
- const tokenValue = token.normalized || token.value;
929
- const lower = this.safeToLowerCase(tokenValue);
930
- if (isValidReference(lower)) {
931
- return createReference(lower);
932
- }
933
- return createLiteral(token.normalized || token.value);
934
- case "identifier":
935
- if (typeof token.value === "string" && token.value.startsWith(":")) {
936
- return createReference(token.value);
937
- }
938
- const identLower = this.safeToLowerCase(token.value);
939
- if (isValidReference(identLower)) {
940
- return createReference(identLower);
941
- }
942
- return { type: "expression", raw: token.value };
943
- case "url":
944
- return createLiteral(token.value, "string");
945
- default:
946
- return null;
947
- }
948
- }
949
- /**
950
- * Parse a literal value (string, number, boolean).
951
- */
952
- parseLiteralValue(value) {
953
- if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
954
- const inner = value.slice(1, -1);
955
- return createLiteral(inner, "string");
956
- }
957
- if (value === "true") return createLiteral(true, "boolean");
958
- if (value === "false") return createLiteral(false, "boolean");
959
- const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
960
- if (durationMatch) {
961
- const num2 = parseFloat(durationMatch[1]);
962
- const unit = durationMatch[2];
963
- if (unit) {
964
- return createLiteral(value, "duration");
965
- }
966
- return createLiteral(num2, "number");
967
- }
968
- const num = parseFloat(value);
969
- if (!isNaN(num)) {
970
- return createLiteral(num, "number");
971
- }
972
- return createLiteral(value, "string");
973
- }
1069
+ // ==========================================================================
1070
+ // Depth Limits for Expression Parsing (security hardening)
1071
+ // ==========================================================================
974
1072
  /**
975
1073
  * Apply extraction rules to fill in static values and defaults for missing roles.
976
1074
  */
@@ -991,190 +1089,9 @@ var _PatternMatcher = class _PatternMatcher {
991
1089
  isOptional(patternToken) {
992
1090
  return patternToken.type !== "literal" && patternToken.optional === true;
993
1091
  }
994
- /**
995
- * Calculate confidence score for a match (0-1).
996
- *
997
- * Confidence is reduced for:
998
- * - Stem matches (morphological normalization has inherent uncertainty)
999
- * - Missing optional roles (but less penalty if role has a default value)
1000
- *
1001
- * Confidence is increased for:
1002
- * - VSO languages (Arabic) when pattern starts with a verb
1003
- */
1004
- calculateConfidence(pattern, captured) {
1005
- let score = 0;
1006
- let maxScore = 0;
1007
- const hasDefault = (role) => {
1008
- return pattern.extraction?.[role]?.default !== void 0;
1009
- };
1010
- for (const token of pattern.template.tokens) {
1011
- if (token.type === "role") {
1012
- maxScore += 1;
1013
- if (captured.has(token.role)) {
1014
- score += 1;
1015
- }
1016
- } else if (token.type === "group") {
1017
- for (const subToken of token.tokens) {
1018
- if (subToken.type === "role") {
1019
- const roleHasDefault = hasDefault(subToken.role);
1020
- const weight = 0.8;
1021
- maxScore += weight;
1022
- if (captured.has(subToken.role)) {
1023
- score += weight;
1024
- } else if (roleHasDefault) {
1025
- score += weight * 0.6;
1026
- }
1027
- }
1028
- }
1029
- }
1030
- }
1031
- let baseConfidence = maxScore > 0 ? score / maxScore : 1;
1032
- if (this.stemMatchCount > 0 && this.totalKeywordMatches > 0) {
1033
- const stemPenalty = this.stemMatchCount / this.totalKeywordMatches * 0.15;
1034
- baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
1035
- }
1036
- const vsoBoost = this.calculateVSOConfidenceBoost(pattern);
1037
- baseConfidence = Math.min(1, baseConfidence + vsoBoost);
1038
- const prepositionAdjustment = this.arabicPrepositionDisambiguation(pattern, captured);
1039
- baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
1040
- return baseConfidence;
1041
- }
1042
- /**
1043
- * Calculate confidence boost for VSO (Verb-Subject-Object) language patterns.
1044
- * Arabic naturally uses VSO word order, so patterns that start with a verb
1045
- * should receive a confidence boost.
1046
- *
1047
- * Returns +0.15 confidence boost if:
1048
- * - Language is Arabic ('ar')
1049
- * - Pattern's first token is a verb keyword
1050
- *
1051
- * @param pattern The language pattern being matched
1052
- * @returns Confidence boost (0 or 0.15)
1053
- */
1054
- calculateVSOConfidenceBoost(pattern) {
1055
- if (pattern.language !== "ar") {
1056
- return 0;
1057
- }
1058
- const firstToken = pattern.template.tokens[0];
1059
- if (!firstToken || firstToken.type !== "literal") {
1060
- return 0;
1061
- }
1062
- const ARABIC_VERBS = /* @__PURE__ */ new Set([
1063
- "\u0628\u062F\u0644",
1064
- "\u063A\u064A\u0631",
1065
- "\u0623\u0636\u0641",
1066
- "\u0623\u0632\u0644",
1067
- "\u0636\u0639",
1068
- "\u0627\u062C\u0639\u0644",
1069
- "\u0639\u064A\u0646",
1070
- "\u0632\u062F",
1071
- "\u0627\u0646\u0642\u0635",
1072
- "\u0633\u062C\u0644",
1073
- "\u0623\u0638\u0647\u0631",
1074
- "\u0623\u062E\u0641",
1075
- "\u0634\u063A\u0644",
1076
- "\u0623\u0631\u0633\u0644",
1077
- "\u0631\u0643\u0632",
1078
- "\u0634\u0648\u0634",
1079
- "\u062A\u0648\u0642\u0641",
1080
- "\u0627\u0646\u0633\u062E",
1081
- "\u0627\u062D\u0630\u0641",
1082
- "\u0627\u0635\u0646\u0639",
1083
- "\u0627\u0646\u062A\u0638\u0631",
1084
- "\u0627\u0646\u062A\u0642\u0627\u0644",
1085
- "\u0623\u0648"
1086
- ]);
1087
- if (ARABIC_VERBS.has(firstToken.value)) {
1088
- return 0.15;
1089
- }
1090
- if (firstToken.alternatives) {
1091
- for (const alt of firstToken.alternatives) {
1092
- if (ARABIC_VERBS.has(alt)) {
1093
- return 0.15;
1094
- }
1095
- }
1096
- }
1097
- return 0;
1098
- }
1099
- /**
1100
- * Arabic preposition disambiguation for confidence adjustment.
1101
- *
1102
- * Different Arabic prepositions are more or less natural for different semantic roles:
1103
- * - على (on/upon) is preferred for patient/target roles (element selectors)
1104
- * - إلى (to) is preferred for destination roles
1105
- * - من (from) is preferred for source roles
1106
- * - في (in) is preferred for location roles
1107
- *
1108
- * This method analyzes the prepositions used with captured semantic roles and
1109
- * adjusts confidence based on idiomaticity:
1110
- * - +0.10 for highly idiomatic preposition choices
1111
- * - -0.10 for less natural preposition choices
1112
- *
1113
- * @param pattern The language pattern being matched
1114
- * @param captured The captured semantic values
1115
- * @returns Confidence adjustment (-0.10 to +0.10)
1116
- */
1117
- arabicPrepositionDisambiguation(pattern, captured) {
1118
- if (pattern.language !== "ar") {
1119
- return 0;
1120
- }
1121
- let adjustment = 0;
1122
- const PREFERRED_PREPOSITIONS = {
1123
- patient: ["\u0639\u0644\u0649"],
1124
- // element selectors prefer على (on/upon)
1125
- destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1126
- // destination prefers إلى (to)
1127
- source: ["\u0645\u0646"],
1128
- // source prefers من (from)
1129
- agent: ["\u0645\u0646"],
1130
- // agent/by prefers من (from/by)
1131
- manner: ["\u0628"],
1132
- // manner prefers ب (with/by)
1133
- style: ["\u0628"],
1134
- // style prefers ب (with)
1135
- goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1136
- // target state prefers إلى (to)
1137
- method: ["\u0628"]
1138
- // method prefers ب (with/by)
1139
- };
1140
- for (const [role, value] of captured.entries()) {
1141
- const preferred = PREFERRED_PREPOSITIONS[role];
1142
- if (!preferred || preferred.length === 0) {
1143
- continue;
1144
- }
1145
- const metadata = "metadata" in value ? value.metadata : void 0;
1146
- if (metadata && typeof metadata.prepositionValue === "string") {
1147
- const usedPreposition = metadata.prepositionValue;
1148
- if (preferred.includes(usedPreposition)) {
1149
- adjustment += 0.1;
1150
- } else {
1151
- adjustment -= 0.1;
1152
- }
1153
- }
1154
- }
1155
- return Math.max(-0.1, Math.min(0.1, adjustment));
1156
- }
1157
- /**
1158
- * Skip noise words like "the" before selectors.
1159
- * This enables more natural English syntax like "toggle the .active".
1160
- */
1161
- skipNoiseWords(tokens) {
1162
- const token = tokens.peek();
1163
- if (!token) return;
1164
- const tokenLower = this.safeToLowerCase(token.value);
1165
- if (_PatternMatcher.ENGLISH_NOISE_WORDS.has(tokenLower)) {
1166
- const mark = tokens.mark();
1167
- tokens.advance();
1168
- const nextToken = tokens.peek();
1169
- if (nextToken && nextToken.kind === "selector") {
1170
- return;
1171
- }
1172
- tokens.reset(mark);
1173
- }
1174
- if (tokenLower === "class") {
1175
- tokens.advance();
1176
- }
1177
- }
1092
+ // ===========================================================================
1093
+ // English Idiom Support - Noise Word Handling
1094
+ // ===========================================================================
1178
1095
  /**
1179
1096
  * Extract event modifiers from the token stream.
1180
1097
  * Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
@@ -1215,31 +1132,475 @@ var _PatternMatcher = class _PatternMatcher {
1215
1132
  }
1216
1133
  break;
1217
1134
  }
1218
- tokens.advance();
1135
+ tokens.advance();
1136
+ }
1137
+ return foundModifier ? modifiers : void 0;
1138
+ }
1139
+ };
1140
+ /** Maximum tokens to scan ahead when looking for a group's leading marker */
1141
+ _PatternMatcher.MAX_MARKER_SCAN = 3;
1142
+ var PatternMatcher = _PatternMatcher;
1143
+ var patternMatcher = new PatternMatcher();
1144
+
1145
+ // src/core/tokenization/token-utils.ts
1146
+ function stripOptionalDiacritics(word) {
1147
+ return word.replace(/[\u064b-\u0652\u0670]/g, "");
1148
+ }
1149
+
1150
+ // src/core/matching/match-skeleton.ts
1151
+ function groupHasRole(group) {
1152
+ return group.tokens.some((t) => t.type === "role" || t.type === "group" && groupHasRole(t));
1153
+ }
1154
+ var MatchSkeleton = class {
1155
+ /**
1156
+ * @param makeReader - builds the grammar's reader, handed this matcher's view
1157
+ * of the match in progress (one reader per matcher).
1158
+ */
1159
+ constructor(makeReader, confidenceModel) {
1160
+ /**
1161
+ * The pattern tokens still to match after the one being matched, through
1162
+ * every enclosing group: a value stops at any marker the pattern still owes.
1163
+ */
1164
+ this.patternRest = [];
1165
+ /**
1166
+ * Whether the role slot being matched directly follows a literal the pattern
1167
+ * matched: its marker (`[por {quantity}]`), or the command's verb.
1168
+ */
1169
+ this.slotAfterLiteral = false;
1170
+ /**
1171
+ * Selective memoization cache (Phase 6.1).
1172
+ * Caches pattern match results keyed by `tokenPosition:patternId`.
1173
+ * Cleared before each top-level matchBest() call.
1174
+ */
1175
+ this.matchCache = /* @__PURE__ */ new Map();
1176
+ /**
1177
+ * Stream index where each role's capture began, for the current
1178
+ * `matchPattern` call. Rebuilt per call, written on the same success path as
1179
+ * `captured` — see `PatternMatchResult.roleStarts`.
1180
+ */
1181
+ this.roleStarts = /* @__PURE__ */ new Map();
1182
+ /**
1183
+ * Track stem matches for confidence calculation.
1184
+ * This is set during matching and read during confidence calculation.
1185
+ */
1186
+ this.stemMatchCount = 0;
1187
+ this.totalKeywordMatches = 0;
1188
+ this.confidenceModel = confidenceModel;
1189
+ this.reader = makeReader({
1190
+ patternRest: () => this.patternRest,
1191
+ slotAfterLiteral: () => this.slotAfterLiteral,
1192
+ wouldMatch: (pt, token) => this.patternTokenWouldMatch(pt, token)
1193
+ });
1194
+ }
1195
+ /**
1196
+ * Try to match a single pattern against the token stream.
1197
+ * Returns the match result or null if no match.
1198
+ */
1199
+ matchPattern(tokens, pattern) {
1200
+ const mark = tokens.mark();
1201
+ const captured = /* @__PURE__ */ new Map();
1202
+ this.reader.beginPattern(pattern);
1203
+ this.stemMatchCount = 0;
1204
+ this.totalKeywordMatches = 0;
1205
+ this.roleStarts = /* @__PURE__ */ new Map();
1206
+ this.patternRest = [];
1207
+ const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
1208
+ if (!success) {
1209
+ tokens.reset(mark);
1210
+ return null;
1211
+ }
1212
+ const confidence = this.confidenceModel.calculate({
1213
+ pattern,
1214
+ captured,
1215
+ stemMatchCount: this.stemMatchCount,
1216
+ totalKeywordMatches: this.totalKeywordMatches
1217
+ });
1218
+ this.applyExtractionRules(pattern, captured);
1219
+ return {
1220
+ pattern,
1221
+ captured,
1222
+ consumedTokens: tokens.position() - mark.position,
1223
+ confidence,
1224
+ roleStarts: this.roleStarts,
1225
+ ...this.reader.resultFields?.()
1226
+ };
1227
+ }
1228
+ /**
1229
+ * Try to match multiple patterns, return the best match.
1230
+ */
1231
+ matchBest(tokens, patterns) {
1232
+ this.matchCache.clear();
1233
+ const matches = [];
1234
+ const startPos = tokens.position();
1235
+ for (const pattern of patterns) {
1236
+ const mark = tokens.mark();
1237
+ const cacheKey = `${startPos}:${pattern.id}`;
1238
+ let result;
1239
+ if (this.matchCache.has(cacheKey)) {
1240
+ result = this.matchCache.get(cacheKey);
1241
+ } else {
1242
+ result = this.matchPattern(tokens, pattern);
1243
+ this.matchCache.set(cacheKey, result);
1244
+ }
1245
+ if (result) {
1246
+ matches.push(result);
1247
+ }
1248
+ tokens.reset(mark);
1249
+ }
1250
+ if (matches.length === 0) {
1251
+ return null;
1252
+ }
1253
+ matches.sort((a, b) => {
1254
+ const priorityDiff = b.pattern.priority - a.pattern.priority;
1255
+ if (priorityDiff !== 0) return priorityDiff;
1256
+ return b.confidence - a.confidence;
1257
+ });
1258
+ const best = matches[0];
1259
+ this.matchPattern(tokens, best.pattern);
1260
+ return best;
1261
+ }
1262
+ /**
1263
+ * Match a sequence of pattern tokens against the token stream.
1264
+ */
1265
+ matchTokenSequence(tokens, patternTokens, captured, nextAfterSequence) {
1266
+ this.reader.skipLeading?.(tokens, patternTokens);
1267
+ for (let i = 0; i < patternTokens.length; i++) {
1268
+ const patternToken = patternTokens[i];
1269
+ this.reader.beforePatternToken?.(tokens, patternTokens, i, captured);
1270
+ if (this.reader.maySkipSlot(
1271
+ patternToken,
1272
+ tokens.peek(),
1273
+ patternTokens[i + 1] ?? nextAfterSequence
1274
+ )) {
1275
+ const skipMark = tokens.mark();
1276
+ const speculative = new Map(captured);
1277
+ if (this.matchTokenSequence(
1278
+ tokens,
1279
+ patternTokens.slice(i + 1),
1280
+ speculative,
1281
+ nextAfterSequence
1282
+ ) && this.reader.atClauseEnd(tokens)) {
1283
+ captured.clear();
1284
+ for (const [role, value] of speculative) captured.set(role, value);
1285
+ return true;
1286
+ }
1287
+ tokens.reset(skipMark);
1288
+ }
1289
+ const outerRest = this.patternRest;
1290
+ this.patternRest = [...patternTokens.slice(i + 1), ...outerRest];
1291
+ this.slotAfterLiteral = patternToken.type === "role" && patternTokens[i - 1]?.type === "literal";
1292
+ const matched = this.matchPatternToken(
1293
+ tokens,
1294
+ patternToken,
1295
+ captured,
1296
+ patternTokens[i + 1] ?? nextAfterSequence
1297
+ );
1298
+ this.patternRest = outerRest;
1299
+ if (!matched) {
1300
+ if (this.isOptional(patternToken)) {
1301
+ continue;
1302
+ }
1303
+ return false;
1304
+ }
1305
+ }
1306
+ return true;
1307
+ }
1308
+ /**
1309
+ * Match a single pattern token against the current position in the stream.
1310
+ */
1311
+ matchPatternToken(tokens, patternToken, captured, nextPatternToken) {
1312
+ switch (patternToken.type) {
1313
+ case "literal":
1314
+ return this.matchLiteralToken(tokens, patternToken);
1315
+ case "role": {
1316
+ const roleStart = tokens.position();
1317
+ const matchedRole = this.reader.readRole(tokens, patternToken, captured, nextPatternToken);
1318
+ if (matchedRole && captured.has(patternToken.role)) {
1319
+ this.roleStarts.set(patternToken.role, roleStart);
1320
+ }
1321
+ return matchedRole;
1322
+ }
1323
+ case "group":
1324
+ return this.matchGroupToken(tokens, patternToken, captured, nextPatternToken);
1325
+ default:
1326
+ return false;
1327
+ }
1328
+ }
1329
+ /**
1330
+ * Match a literal pattern token (keyword or particle).
1331
+ */
1332
+ matchLiteralToken(tokens, patternToken) {
1333
+ const token = tokens.peek();
1334
+ if (!token) return false;
1335
+ const matchType = this.getMatchType(token, patternToken.value);
1336
+ if (matchType !== "none") {
1337
+ this.totalKeywordMatches++;
1338
+ if (matchType === "stem") {
1339
+ this.stemMatchCount++;
1340
+ }
1341
+ tokens.advance();
1342
+ return true;
1343
+ }
1344
+ if (patternToken.alternatives) {
1345
+ for (const alt of patternToken.alternatives) {
1346
+ const altMatchType = this.getMatchType(token, alt);
1347
+ if (altMatchType !== "none") {
1348
+ this.totalKeywordMatches++;
1349
+ if (altMatchType === "stem") {
1350
+ this.stemMatchCount++;
1351
+ }
1352
+ tokens.advance();
1353
+ return true;
1354
+ }
1355
+ }
1356
+ }
1357
+ for (const value of [patternToken.value, ...patternToken.alternatives ?? []]) {
1358
+ for (const sep of ["-", "_"]) {
1359
+ if (value.includes(sep) && this.tryMatchShatteredCompound(tokens, value, sep)) {
1360
+ this.totalKeywordMatches++;
1361
+ return true;
1362
+ }
1363
+ }
1364
+ }
1365
+ return false;
1366
+ }
1367
+ /**
1368
+ * Match a compound keyword (`titik-henti`, `по_умолчанию`) against a
1369
+ * shattered token run (`titik` `-` `henti` / `по` `_` `умолчанию`): word
1370
+ * segments must be identifier/keyword/particle tokens equal to the expected
1371
+ * segments (case-insensitive; particle because prepositions like ru `по` /
1372
+ * uk `за` lead some underscore compounds), separators lone `sep` tokens.
1373
+ * Consumes the run on success, resets the stream on failure.
1374
+ */
1375
+ tryMatchShatteredCompound(tokens, expected, sep) {
1376
+ const segments = expected.split(sep);
1377
+ if (segments.length < 2 || segments.some((s) => s.length === 0)) return false;
1378
+ const mark = tokens.mark();
1379
+ for (let i = 0; i < segments.length; i++) {
1380
+ if (i > 0) {
1381
+ const sepToken = tokens.peek();
1382
+ if (!sepToken || sepToken.value !== sep) {
1383
+ tokens.reset(mark);
1384
+ return false;
1385
+ }
1386
+ tokens.advance();
1387
+ }
1388
+ const word = tokens.peek();
1389
+ if (!word || word.kind !== "identifier" && word.kind !== "keyword" && word.kind !== "particle" || word.value.toLowerCase() !== segments[i].toLowerCase()) {
1390
+ tokens.reset(mark);
1391
+ return false;
1392
+ }
1393
+ tokens.advance();
1394
+ }
1395
+ return true;
1396
+ }
1397
+ /**
1398
+ * Match a group pattern token (optional sequence).
1399
+ */
1400
+ matchGroupToken(tokens, patternToken, captured, nextPatternToken) {
1401
+ const mark = tokens.mark();
1402
+ const capturedBefore = new Map(captured);
1403
+ let success = this.matchTokenSequence(tokens, patternToken.tokens, captured, nextPatternToken);
1404
+ if (success && tokens.position() > mark.position && groupHasRole(patternToken) && [...captured].every(([role, value]) => capturedBefore.get(role) === value) && captured.size === capturedBefore.size) {
1405
+ success = false;
1406
+ }
1407
+ if (success) {
1408
+ const markerLiterals = [];
1409
+ for (const inner of patternToken.tokens) {
1410
+ if (inner.type === "group" && inner.renderRequired) {
1411
+ for (const t of inner.tokens) {
1412
+ if (t.type === "literal") markerLiterals.push(t.value, ...t.alternatives ?? []);
1413
+ }
1414
+ }
1415
+ }
1416
+ if (markerLiterals.length > 0) {
1417
+ const addedRole = [...captured.keys()].some((role) => !capturedBefore.has(role));
1418
+ if (addedRole) {
1419
+ const last = tokens.tokens[tokens.position() - 1];
1420
+ const markerSeen = last !== void 0 && tokens.position() > mark.position && markerLiterals.some((lit) => this.getMatchType(last, lit) !== "none");
1421
+ if (!markerSeen) success = false;
1422
+ }
1423
+ }
1424
+ }
1425
+ if (!success) {
1426
+ tokens.reset(mark);
1427
+ for (const role of captured.keys()) {
1428
+ if (!capturedBefore.has(role)) {
1429
+ captured.delete(role);
1430
+ }
1431
+ }
1432
+ for (const [role, value] of capturedBefore) {
1433
+ if (captured.get(role) !== value) {
1434
+ captured.set(role, value);
1435
+ }
1436
+ }
1437
+ return patternToken.optional || false;
1438
+ }
1439
+ return true;
1440
+ }
1441
+ /**
1442
+ * Whether a pattern token (literal, or a group starting with a literal) would
1443
+ * match the given stream token. Used to keep the event-head source-clause
1444
+ * consumption from stealing a marker the pattern explicitly expects.
1445
+ */
1446
+ patternTokenWouldMatch(pt, token) {
1447
+ if (!pt) return false;
1448
+ if (pt.type === "literal") {
1449
+ if (this.getMatchType(token, pt.value) !== "none") return true;
1450
+ return !!pt.alternatives?.some((a) => this.getMatchType(token, a) !== "none");
1451
+ }
1452
+ if (pt.type === "group") {
1453
+ return this.patternTokenWouldMatch(pt.tokens?.[0], token);
1454
+ }
1455
+ return false;
1456
+ }
1457
+ /**
1458
+ * Get the type of match for a token against a value.
1459
+ * Used for confidence calculation.
1460
+ */
1461
+ getMatchType(token, value) {
1462
+ if (token.value === value) return "exact";
1463
+ if (token.normalized === value) return "normalized";
1464
+ if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
1465
+ return "stem";
1466
+ }
1467
+ if (token.kind === "keyword" && token.value.toLowerCase() === value.toLowerCase()) {
1468
+ return "case-insensitive";
1469
+ }
1470
+ if (token.kind === "keyword") {
1471
+ const strippedToken = stripOptionalDiacritics(token.value).toLowerCase();
1472
+ if (strippedToken && strippedToken === stripOptionalDiacritics(value).toLowerCase()) {
1473
+ return "case-insensitive";
1474
+ }
1475
+ }
1476
+ return "none";
1477
+ }
1478
+ /**
1479
+ * Apply extraction rules to fill in static values and defaults for missing
1480
+ * roles, and re-shape captured roles that declare a `transform` (the
1481
+ * ExtractionRule field was documented but unwired — go-qu-url-dest is the
1482
+ * first consumer: it re-types the marker-bound capture to the expression
1483
+ * shape the en reference produces).
1484
+ */
1485
+ applyExtractionRules(pattern, captured) {
1486
+ for (const [role, rule] of Object.entries(pattern.extraction)) {
1487
+ const existing = captured.get(role);
1488
+ if (existing) {
1489
+ if (rule.transform) {
1490
+ const raw = existing.type === "literal" ? String(existing.value) : String(existing.raw ?? "");
1491
+ captured.set(role, rule.transform(raw));
1492
+ }
1493
+ } else if (rule.value !== void 0) {
1494
+ captured.set(
1495
+ role,
1496
+ rule.valueIsExpression ? { type: "expression", raw: rule.value } : { type: "literal", value: rule.value }
1497
+ );
1498
+ } else if (rule.default) {
1499
+ const implicitDefault = {
1500
+ ...rule.default,
1501
+ implicit: true
1502
+ };
1503
+ captured.set(role, implicitDefault);
1504
+ }
1505
+ }
1506
+ }
1507
+ /**
1508
+ * Check if a pattern token is optional.
1509
+ */
1510
+ isOptional(patternToken) {
1511
+ return patternToken.type !== "literal" && patternToken.optional === true;
1512
+ }
1513
+ };
1514
+
1515
+ // src/core/matching/stock-reader.ts
1516
+ function greedyStopMarkers(rest) {
1517
+ const markers = /* @__PURE__ */ new Set();
1518
+ const add = (pt) => {
1519
+ markers.add(pt.value.toLowerCase());
1520
+ for (const alt of pt.alternatives ?? []) markers.add(alt.toLowerCase());
1521
+ };
1522
+ for (const pt of rest) {
1523
+ if (pt.type === "literal") {
1524
+ add(pt);
1525
+ break;
1526
+ }
1527
+ if (pt.type === "group") {
1528
+ const lead = pt.tokens.find((t) => t.type === "literal");
1529
+ if (lead?.type === "literal") add(lead);
1530
+ if (!pt.optional) break;
1531
+ }
1532
+ }
1533
+ return markers;
1534
+ }
1535
+ function isStopMarker(token, stopMarkers) {
1536
+ if (stopMarkers.size === 0) return false;
1537
+ if (stopMarkers.has(token.value.toLowerCase())) return true;
1538
+ return !!token.normalized && stopMarkers.has(token.normalized.toLowerCase());
1539
+ }
1540
+ function readGreedy(tokens, stopMarkers) {
1541
+ const values = [];
1542
+ while (!tokens.isAtEnd()) {
1543
+ const next = tokens.peek();
1544
+ if (!next || isStopMarker(next, stopMarkers)) break;
1545
+ values.push(next.value);
1546
+ tokens.advance();
1547
+ }
1548
+ return values.length > 0 ? { type: "expression", raw: values.join(" ") } : null;
1549
+ }
1550
+ var StockValueReader = class {
1551
+ constructor(view, profiles = {}) {
1552
+ this.view = view;
1553
+ this.profiles = profiles;
1554
+ }
1555
+ beginPattern(pattern) {
1556
+ this.profile = this.profiles[pattern.language] ?? { code: pattern.language };
1557
+ this.lastOptional = void 0;
1558
+ }
1559
+ readRole(tokens, slot, captured, next) {
1560
+ const start = tokens.mark();
1561
+ if (this.readOnce(tokens, slot, captured, next, true)) {
1562
+ if (!slot.optional) {
1563
+ this.lastOptional = void 0;
1564
+ } else if (captured.has(slot.role) && tokens.position() > start.position && !this.view.slotAfterLiteral() && next?.type !== "literal") {
1565
+ this.lastOptional = { role: slot.role, start, end: tokens.position() };
1566
+ }
1567
+ return true;
1568
+ }
1569
+ const before = this.lastOptional;
1570
+ if (slot.optional || !before || before.end !== tokens.position()) return false;
1571
+ this.lastOptional = void 0;
1572
+ tokens.reset(before.start);
1573
+ captured.delete(before.role);
1574
+ return this.readOnce(tokens, slot, captured, next, false);
1575
+ }
1576
+ /** One read of `slot`; `guard` refuses a required slot the marker the pattern wants next. */
1577
+ readOnce(tokens, slot, captured, next, guard) {
1578
+ if (guard && !slot.optional && next?.type === "literal") {
1579
+ const token = tokens.peek();
1580
+ if (token && this.view.wouldMatch(next, token)) return false;
1581
+ }
1582
+ if (slot.greedy) {
1583
+ const value = readGreedy(tokens, greedyStopMarkers(this.view.patternRest()));
1584
+ if (value) {
1585
+ captured.set(slot.role, value);
1586
+ return true;
1587
+ }
1588
+ return slot.optional ?? false;
1219
1589
  }
1220
- return foundModifier ? modifiers : void 0;
1590
+ return readRoleValue(tokens, slot, captured, this.profile);
1591
+ }
1592
+ maySkipSlot(patternToken, token, next) {
1593
+ const slot = patternToken.type === "group" && patternToken.optional ? patternToken.tokens.every((t) => t.type === "role") ? patternToken.tokens[0] : void 0 : patternToken;
1594
+ if (!slot || slot.type !== "role" || !slot.optional) return false;
1595
+ return !!token && this.view.wouldMatch(next, token);
1596
+ }
1597
+ atClauseEnd(tokens) {
1598
+ return tokens.isAtEnd();
1221
1599
  }
1222
1600
  };
1223
- /** Maximum tokens to scan ahead when looking for a group's leading marker */
1224
- _PatternMatcher.MAX_MARKER_SCAN = 3;
1225
- // ==========================================================================
1226
- // Depth Limits for Expression Parsing (security hardening)
1227
- // ==========================================================================
1228
- /** Maximum depth for nested property access (e.g., a.b.c.d...) */
1229
- _PatternMatcher.MAX_PROPERTY_DEPTH = 10;
1230
- /** Maximum number of arguments in method calls */
1231
- _PatternMatcher.MAX_METHOD_ARGS = 20;
1232
- // ===========================================================================
1233
- // English Idiom Support - Noise Word Handling
1234
- // ===========================================================================
1235
- /**
1236
- * Noise words that can be skipped in English for more natural syntax.
1237
- * - "the" before selectors: "toggle the .active" → "toggle .active"
1238
- * - "class" after class selectors: "add the .visible class" → "add .visible"
1239
- */
1240
- _PatternMatcher.ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
1241
- var PatternMatcher = _PatternMatcher;
1242
- var patternMatcher = new PatternMatcher();
1601
+ function createStockReader(profiles = {}) {
1602
+ return (view) => new StockValueReader(view, profiles);
1603
+ }
1243
1604
 
1244
1605
  // src/generation/pattern-generator.ts
1245
1606
  var defaultConfig = {
@@ -1496,6 +1857,581 @@ function renderFromSchema(node, language, tables) {
1496
1857
  return isSOV ? buildPhrase(...preVerb, keyword, ...postVerb) : buildPhrase(keyword, ...preVerb, ...postVerb);
1497
1858
  }
1498
1859
 
1860
+ // src/generation/schema-patterns.ts
1861
+ function createSchemaPatternGenerator(grammar) {
1862
+ return {
1863
+ generatePattern: (schema, profile, config) => generatePattern2(grammar, schema, profile, config),
1864
+ generateSimplePattern: (schema, profile, config) => generateSimplePattern(grammar, schema, profile, config),
1865
+ generateVerbFirstPattern: (schema, profile, config) => generateVerbFirstPattern(schema, profile, config),
1866
+ generatePatternVariants: (schema, profile, config) => generatePatternVariants(grammar, schema, profile, config),
1867
+ buildRoleToken
1868
+ };
1869
+ }
1870
+ function withoutVariantOnlyRoles(schema) {
1871
+ return schema.roles.some((r) => r.variantOnly) ? { ...schema, roles: schema.roles.filter((r) => !r.variantOnly) } : schema;
1872
+ }
1873
+ function sortRolesByPosition(roles, wordOrder, order) {
1874
+ const sortKey = wordOrder === "SOV" ? "sovPosition" : "svoPosition";
1875
+ return [...roles].sort(
1876
+ (a, b) => order === "ascending" ? (a[sortKey] ?? 99) - (b[sortKey] ?? 99) : (b[sortKey] ?? 0) - (a[sortKey] ?? 0)
1877
+ );
1878
+ }
1879
+ function resolveMarkerForRole(roleSpec, profile) {
1880
+ const overrideMarker = roleSpec.markerOverride?.[profile.code];
1881
+ const defaultMarker = profile.roleMarkers[roleSpec.role];
1882
+ if (overrideMarker !== void 0) {
1883
+ const alternatives = legacyMarkerAlternatives(roleSpec, profile.code, overrideMarker);
1884
+ return {
1885
+ primary: overrideMarker,
1886
+ ...alternatives && { alternatives },
1887
+ position: defaultMarker?.position ?? "before",
1888
+ isOverride: true
1889
+ };
1890
+ }
1891
+ if (defaultMarker && defaultMarker.primary) {
1892
+ const result = {
1893
+ primary: defaultMarker.primary,
1894
+ position: defaultMarker.position,
1895
+ isOverride: false
1896
+ };
1897
+ if (defaultMarker.alternatives) {
1898
+ result.alternatives = defaultMarker.alternatives;
1899
+ }
1900
+ return result;
1901
+ }
1902
+ return null;
1903
+ }
1904
+ function legacyMarkerAlternatives(roleSpec, languageCode, overrideMarker) {
1905
+ const legacy = roleSpec.markerLegacy?.[languageCode];
1906
+ if (!legacy?.length) return void 0;
1907
+ const alternatives = [...new Set(legacy)].filter((a) => a && a !== overrideMarker);
1908
+ return alternatives.length ? alternatives : void 0;
1909
+ }
1910
+ function schemaMarkerAlternatives(roleSpec, languageCode, marker) {
1911
+ const legacy = roleSpec.markerLegacy?.[languageCode] ?? [];
1912
+ const variants = roleSpec.methodCarrier ? [] : roleSpec.markerVariants?.[languageCode] ?? [];
1913
+ const alternatives = [.../* @__PURE__ */ new Set([...legacy, ...variants])].filter((a) => a && a !== marker);
1914
+ return alternatives.length ? alternatives : void 0;
1915
+ }
1916
+ var defaultConfig2 = {
1917
+ basePriority: 100,
1918
+ generateSimpleVariants: true,
1919
+ generateAlternatives: true
1920
+ };
1921
+ function generatePattern2(grammar, schema, profile, config = defaultConfig2) {
1922
+ const id = `${schema.action}-${profile.code}-generated`;
1923
+ const priority = config.basePriority ?? 100;
1924
+ const keyword = profile.keywords[schema.action];
1925
+ if (!keyword) {
1926
+ throw new Error(`No keyword translation for '${schema.action}' in ${profile.code}`);
1927
+ }
1928
+ const tokens = buildTokens2(grammar, schema, profile, keyword);
1929
+ const extraction = buildExtractionRulesWithDefaults(schema, profile);
1930
+ const format = buildFormatString2(grammar, schema, profile, keyword);
1931
+ return {
1932
+ id,
1933
+ language: profile.code,
1934
+ command: schema.action,
1935
+ priority,
1936
+ template: {
1937
+ format,
1938
+ tokens
1939
+ },
1940
+ extraction
1941
+ };
1942
+ }
1943
+ function generateSimplePattern(grammar, schema, profile, config = defaultConfig2) {
1944
+ const optionalRoles = schema.roles.filter((r) => !r.required);
1945
+ if (optionalRoles.length === 0) {
1946
+ return null;
1947
+ }
1948
+ const requiredRoles = schema.roles.filter((r) => r.required);
1949
+ const simpleSchema = {
1950
+ ...schema,
1951
+ roles: requiredRoles
1952
+ };
1953
+ const pattern = generatePattern2(grammar, simpleSchema, profile, config);
1954
+ return {
1955
+ ...pattern,
1956
+ id: `${schema.action}-${profile.code}-generated-simple`,
1957
+ priority: (config.basePriority ?? 100) - 5,
1958
+ // Lower priority than full pattern (was -10)
1959
+ extraction: buildExtractionRulesWithDefaults(schema, profile)
1960
+ };
1961
+ }
1962
+ function generateVerbFirstPattern(schema, profile, config = defaultConfig2) {
1963
+ if (profile.wordOrder !== "SOV") return null;
1964
+ const requiredRoles = schema.roles.filter((r) => r.required);
1965
+ if (requiredRoles.length !== 1) return null;
1966
+ const keyword = profile.keywords[schema.action];
1967
+ if (!keyword) return null;
1968
+ const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
1969
+ const roleTokens = requiredRoles.flatMap((r) => {
1970
+ const prefix = r.valuePrefixLiteral?.[profile.code];
1971
+ const roleToken = {
1972
+ type: "role",
1973
+ role: r.role,
1974
+ optional: false,
1975
+ expectedTypes: r.expectedTypes
1976
+ };
1977
+ return prefix ? [{ type: "literal", value: prefix }, roleToken] : [roleToken];
1978
+ });
1979
+ return {
1980
+ id: `${schema.action}-${profile.code}-generated-verb-first`,
1981
+ language: profile.code,
1982
+ command: schema.action,
1983
+ // Strictly below the canonical (basePriority) and simple (basePriority - 5)
1984
+ // patterns so native order is always preferred.
1985
+ priority: (config.basePriority ?? 100) - 20,
1986
+ template: {
1987
+ format: `${keyword.primary} <${requiredRoles[0].role}>`,
1988
+ tokens: [verbToken, ...roleTokens]
1989
+ },
1990
+ extraction: buildExtractionRulesWithDefaults(schema, profile)
1991
+ };
1992
+ }
1993
+ function generatePatternVariants(grammar, schema, profile, config = defaultConfig2) {
1994
+ const patterns = [];
1995
+ const full = schema;
1996
+ schema = withoutVariantOnlyRoles(schema);
1997
+ patterns.push(generatePattern2(grammar, schema, profile, config));
1998
+ if (config.generateSimpleVariants) {
1999
+ const simple = generateSimplePattern(grammar, schema, profile, config);
2000
+ if (simple) {
2001
+ patterns.push(simple);
2002
+ }
2003
+ }
2004
+ for (const omitted of schema.omitRoleVariants ?? []) {
2005
+ const variantSchema = {
2006
+ ...schema,
2007
+ roles: schema.roles.filter((r) => r.role !== omitted)
2008
+ };
2009
+ const variant = generatePattern2(grammar, variantSchema, profile, config);
2010
+ patterns.push({
2011
+ ...variant,
2012
+ id: `${schema.action}-${profile.code}-generated-no-${omitted}`,
2013
+ priority: (config.basePriority ?? 100) - 8
2014
+ });
2015
+ }
2016
+ for (const widen of schema.widenTypeVariants ?? []) {
2017
+ if (widen.excludeLanguages?.includes(profile.code)) continue;
2018
+ const cloneSchema = {
2019
+ ...schema,
2020
+ roles: schema.roles.map(
2021
+ (r) => r.role === widen.role ? { ...r, expectedTypes: [...widen.types] } : r
2022
+ )
2023
+ };
2024
+ const main = generatePattern2(grammar, cloneSchema, profile, config);
2025
+ patterns.push({
2026
+ ...main,
2027
+ id: `${schema.action}-${profile.code}-generated-${widen.idSuffix}`,
2028
+ priority: (config.basePriority ?? 100) - (widen.priorityDelta ?? 15)
2029
+ });
2030
+ }
2031
+ for (const v of full.requiredRoleVariants ?? []) {
2032
+ const variantSchema = {
2033
+ ...full,
2034
+ roles: full.roles.filter((r) => !v.omit?.includes(r.role)).map((r) => {
2035
+ const required = r.role === v.role ? { ...r, required: true } : r;
2036
+ return r.role === v.widen?.role ? { ...required, expectedTypes: [...v.widen.types] } : required;
2037
+ })
2038
+ };
2039
+ const variant = generatePattern2(grammar, variantSchema, profile, config);
2040
+ patterns.push({
2041
+ ...variant,
2042
+ id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
2043
+ priority: (config.basePriority ?? 100) + (v.priorityDelta ?? 5)
2044
+ });
2045
+ }
2046
+ if (config.generateVerbFirstVariants !== false) {
2047
+ const verbFirst = generateVerbFirstPattern(schema, profile, config);
2048
+ if (verbFirst) {
2049
+ patterns.push(verbFirst);
2050
+ }
2051
+ }
2052
+ for (const v of schema.rolePrefixLiteralVariants ?? []) {
2053
+ const literal = v.literal[profile.code];
2054
+ if (!literal) continue;
2055
+ const { rolePrefixLiteralVariants: _omitted, ...baseSchema } = schema;
2056
+ const cloneSchema = {
2057
+ ...baseSchema,
2058
+ roles: schema.roles.map(
2059
+ (r) => r.role === v.role ? { ...r, valuePrefixLiteral: { [profile.code]: literal } } : r
2060
+ )
2061
+ };
2062
+ const delta = v.priorityDelta ?? 5;
2063
+ const carrier = v.methodCarrier ? { [v.methodCarrier]: { value: literal } } : {};
2064
+ const main = generatePattern2(grammar, cloneSchema, profile, config);
2065
+ patterns.push({
2066
+ ...main,
2067
+ id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
2068
+ priority: (config.basePriority ?? 100) + delta,
2069
+ extraction: { ...main.extraction, ...carrier }
2070
+ });
2071
+ if (config.generateVerbFirstVariants !== false) {
2072
+ const verbFirstUrl = generateVerbFirstPattern(cloneSchema, profile, config);
2073
+ if (verbFirstUrl) {
2074
+ patterns.push({
2075
+ ...verbFirstUrl,
2076
+ id: `${schema.action}-${profile.code}-generated-verb-first-${v.idSuffix}`,
2077
+ priority: (config.basePriority ?? 100) - 20 + delta,
2078
+ extraction: { ...verbFirstUrl.extraction, ...carrier }
2079
+ });
2080
+ }
2081
+ }
2082
+ }
2083
+ return patterns;
2084
+ }
2085
+ function buildTokens2(grammar, schema, profile, keyword) {
2086
+ const tokens = [];
2087
+ const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
2088
+ const postVerbRoles = profile.wordOrder === "SOV" ? schema.roles.filter((r) => r.sovSlot === "postVerb") : [];
2089
+ const roleTokens = buildRoleTokens(
2090
+ grammar,
2091
+ postVerbRoles.length ? { ...schema, roles: schema.roles.filter((r) => r.sovSlot !== "postVerb") } : schema,
2092
+ profile
2093
+ );
2094
+ const postVerbTokens = postVerbRoles.length ? buildRoleTokens(grammar, { ...schema, roles: postVerbRoles }, profile) : [];
2095
+ switch (profile.wordOrder) {
2096
+ case "SVO":
2097
+ tokens.push(verbToken);
2098
+ tokens.push(...roleTokens);
2099
+ break;
2100
+ case "SOV":
2101
+ tokens.push(...roleTokens);
2102
+ tokens.push(verbToken);
2103
+ tokens.push(...postVerbTokens);
2104
+ break;
2105
+ case "VSO":
2106
+ tokens.push(verbToken);
2107
+ tokens.push(...roleTokens);
2108
+ break;
2109
+ default:
2110
+ tokens.push(verbToken);
2111
+ tokens.push(...roleTokens);
2112
+ }
2113
+ return collapseAdjacentLiterals(tokens);
2114
+ }
2115
+ function collapseAdjacentLiterals(tokens) {
2116
+ const out = [];
2117
+ for (const tok of tokens) {
2118
+ const prev = out[out.length - 1];
2119
+ if (prev && prev.type === "literal" && tok.type === "literal" && prev.value === tok.value) {
2120
+ const alts = [...prev.alternatives ?? [], ...tok.alternatives ?? []];
2121
+ if (alts.length) out[out.length - 1] = { ...prev, alternatives: [...new Set(alts)] };
2122
+ continue;
2123
+ }
2124
+ out.push(tok);
2125
+ }
2126
+ return out;
2127
+ }
2128
+ function buildRoleTokens(grammar, schema, profile) {
2129
+ const tokens = [];
2130
+ const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
2131
+ for (const roleSpec of sortedRoles) {
2132
+ const roleToken = buildRoleToken(roleSpec, profile);
2133
+ if (!roleSpec.required) {
2134
+ tokens.push({
2135
+ type: "group",
2136
+ optional: true,
2137
+ tokens: roleToken
2138
+ });
2139
+ } else {
2140
+ tokens.push(...roleToken);
2141
+ }
2142
+ }
2143
+ return tokens;
2144
+ }
2145
+ function buildRoleToken(roleSpec, profile) {
2146
+ const tokens = [];
2147
+ const overrideMarker = roleSpec.markerOverride?.[profile.code];
2148
+ const defaultMarker = profile.roleMarkers[roleSpec.role];
2149
+ const suppressMarker = roleSpec.renderOverride?.[profile.code] === "";
2150
+ const roleValueToken = {
2151
+ type: "role",
2152
+ role: roleSpec.role,
2153
+ optional: !roleSpec.required,
2154
+ expectedTypes: roleSpec.expectedTypes,
2155
+ // The matcher reads this too — a `'keyword'`-shaped slot holds a fixed
2156
+ // keyword phrase (`using view transition`) and must not be skipped by the
2157
+ // trailing-slot verb guard.
2158
+ ...roleSpec.valueShape !== void 0 ? { valueShape: roleSpec.valueShape } : {},
2159
+ // A greedy role reads up to the next marker the pattern owes (a domain's
2160
+ // SQL condition); its value reader does the reading.
2161
+ ...roleSpec.greedy ? { greedy: true } : {}
2162
+ };
2163
+ const prefixLiteral = roleSpec.valuePrefixLiteral?.[profile.code];
2164
+ const pushPrefixed = () => {
2165
+ if (prefixLiteral) tokens.push({ type: "literal", value: prefixLiteral });
2166
+ tokens.push(roleValueToken);
2167
+ };
2168
+ if (overrideMarker !== void 0) {
2169
+ const markerWords = overrideMarker ? overrideMarker.split(/\s+/).filter(Boolean) : [];
2170
+ const isPassthroughOverride = overrideMarker !== void 0 && overrideMarker === roleSpec.markerOverride?.en;
2171
+ const position = roleSpec.markerPositionOverride?.[profile.code] ?? roleSpec.markerPosition ?? defaultMarker?.position ?? (profile.wordOrder === "SOV" && !isPassthroughOverride ? "after" : "before");
2172
+ const optionalMarker = roleSpec.markerOptional?.[profile.code] === true;
2173
+ const pushWord = (word) => {
2174
+ const alternatives = markerWords.length === 1 ? schemaMarkerAlternatives(roleSpec, profile.code, word) ?? [] : [];
2175
+ const literal = {
2176
+ type: "literal",
2177
+ value: word,
2178
+ ...alternatives.length ? { alternatives } : {},
2179
+ ...suppressMarker ? { renderSuppress: true } : {}
2180
+ };
2181
+ tokens.push(optionalMarker ? { type: "group", optional: true, tokens: [literal] } : literal);
2182
+ };
2183
+ if (position === "before") {
2184
+ for (const word of markerWords) pushWord(word);
2185
+ pushPrefixed();
2186
+ } else {
2187
+ pushPrefixed();
2188
+ for (const word of markerWords) pushWord(word);
2189
+ }
2190
+ } else if (defaultMarker) {
2191
+ const asMarker = () => {
2192
+ const alternatives = [
2193
+ .../* @__PURE__ */ new Set([
2194
+ ...defaultMarker.alternatives ?? [],
2195
+ ...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
2196
+ ])
2197
+ ].filter((a) => a !== defaultMarker.primary);
2198
+ return {
2199
+ type: "literal",
2200
+ value: defaultMarker.primary,
2201
+ ...alternatives.length ? { alternatives } : {},
2202
+ ...suppressMarker ? { renderSuppress: true } : {}
2203
+ };
2204
+ };
2205
+ const pushMarker = (marker) => {
2206
+ if (!profile.markersOptional && !roleSpec.markerOptional?.[profile.code]) {
2207
+ tokens.push(marker);
2208
+ return;
2209
+ }
2210
+ tokens.push({
2211
+ type: "group",
2212
+ optional: true,
2213
+ ...profile.markersOptional && !roleSpec.markerOptional?.[profile.code] ? { renderRequired: true } : {},
2214
+ tokens: [marker]
2215
+ });
2216
+ };
2217
+ if ((roleSpec.markerPosition ?? defaultMarker.position) === "before") {
2218
+ if (defaultMarker.primary) {
2219
+ pushMarker(asMarker());
2220
+ }
2221
+ pushPrefixed();
2222
+ } else {
2223
+ pushPrefixed();
2224
+ pushMarker(asMarker());
2225
+ }
2226
+ } else {
2227
+ pushPrefixed();
2228
+ }
2229
+ return tokens;
2230
+ }
2231
+ function buildExtractionRules2(schema, profile) {
2232
+ const rules = {};
2233
+ for (const roleSpec of schema.roles) {
2234
+ const overrideMarker = roleSpec.markerOverride?.[profile.code];
2235
+ const defaultMarker = profile.roleMarkers[roleSpec.role];
2236
+ if (roleSpec.valuePrefixLiteral?.[profile.code]) {
2237
+ rules[roleSpec.role] = { marker: roleSpec.valuePrefixLiteral[profile.code] };
2238
+ } else if (overrideMarker !== void 0) {
2239
+ if (!overrideMarker) {
2240
+ rules[roleSpec.role] = {};
2241
+ } else {
2242
+ const isSingleWord = !/\s/.test(overrideMarker.trim());
2243
+ const markerAlternatives = isSingleWord ? schemaMarkerAlternatives(roleSpec, profile.code, overrideMarker) ?? [] : [];
2244
+ rules[roleSpec.role] = markerAlternatives.length ? { marker: overrideMarker, markerAlternatives } : { marker: overrideMarker };
2245
+ }
2246
+ } else if (defaultMarker && defaultMarker.primary) {
2247
+ const markerAlternatives = [
2248
+ .../* @__PURE__ */ new Set([
2249
+ ...defaultMarker.alternatives ?? [],
2250
+ ...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
2251
+ ])
2252
+ ].filter((a) => a !== defaultMarker.primary);
2253
+ rules[roleSpec.role] = markerAlternatives.length ? { marker: defaultMarker.primary, markerAlternatives } : { marker: defaultMarker.primary };
2254
+ } else {
2255
+ rules[roleSpec.role] = {};
2256
+ }
2257
+ }
2258
+ return rules;
2259
+ }
2260
+ function buildExtractionRulesWithDefaults(schema, profile) {
2261
+ const baseRules = buildExtractionRules2(schema, profile);
2262
+ const rules = {};
2263
+ for (const roleSpec of schema.roles) {
2264
+ const baseRule = baseRules[roleSpec.role] || {};
2265
+ if (!roleSpec.required && roleSpec.default) {
2266
+ rules[roleSpec.role] = { ...baseRule, default: roleSpec.default };
2267
+ } else {
2268
+ rules[roleSpec.role] = baseRule;
2269
+ }
2270
+ }
2271
+ return rules;
2272
+ }
2273
+ function buildFormatString2(grammar, schema, profile, keyword) {
2274
+ const parts = [];
2275
+ const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
2276
+ const roleParts = sortedRoles.map((roleSpec) => {
2277
+ const resolved = resolveMarkerForRole(roleSpec, profile);
2278
+ let part = "";
2279
+ if (resolved && resolved.primary) {
2280
+ if (resolved.position === "before") {
2281
+ part = `${resolved.primary} {${roleSpec.role}}`;
2282
+ } else {
2283
+ part = `{${roleSpec.role}} ${resolved.primary}`;
2284
+ }
2285
+ } else {
2286
+ part = `{${roleSpec.role}}`;
2287
+ }
2288
+ return roleSpec.required ? part : `[${part}]`;
2289
+ });
2290
+ switch (profile.wordOrder) {
2291
+ case "SVO":
2292
+ case "VSO":
2293
+ parts.push(keyword.primary, ...roleParts);
2294
+ break;
2295
+ case "SOV":
2296
+ parts.push(...roleParts, keyword.primary);
2297
+ break;
2298
+ default:
2299
+ parts.push(keyword.primary, ...roleParts);
2300
+ }
2301
+ return parts.join(" ");
2302
+ }
2303
+
2304
+ // src/generation/pattern-render.ts
2305
+ var NESTED_ROLE_BONUS = 5;
2306
+ var UNCOVERED_ROLE_PENALTY = 30;
2307
+ function pinnedLiteral(rule) {
2308
+ if (rule?.default?.type === "literal") return String(rule.default.value).trim().toLowerCase();
2309
+ if (rule?.value !== void 0) return String(rule.value).trim().toLowerCase();
2310
+ return void 0;
2311
+ }
2312
+ function selectRenderPattern(node, patterns, hooks) {
2313
+ let candidates = patterns.filter((pattern) => !pattern.parseOnly);
2314
+ if (candidates.length === 0) candidates = patterns;
2315
+ if (hooks.candidates) candidates = hooks.candidates(node, candidates);
2316
+ const pinnedValues = /* @__PURE__ */ new Map();
2317
+ for (const pattern of candidates) {
2318
+ for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
2319
+ const key = pinnedLiteral(rule);
2320
+ if (key === void 0) continue;
2321
+ const set = pinnedValues.get(role) ?? /* @__PURE__ */ new Set();
2322
+ set.add(key);
2323
+ pinnedValues.set(role, set);
2324
+ }
2325
+ }
2326
+ const scored = candidates.map((pattern) => {
2327
+ let score = pattern.priority;
2328
+ const hasRealRole = (role) => hooks.hasRealRole(node, role);
2329
+ const scoreTokens = (tokens, inGroup) => {
2330
+ for (const token of tokens) {
2331
+ if (token.type === "group") {
2332
+ scoreTokens(token.tokens, true);
2333
+ } else if (token.type === "role") {
2334
+ if (hasRealRole(token.role)) {
2335
+ score += inGroup ? NESTED_ROLE_BONUS : 10;
2336
+ } else if (!inGroup && !token.optional) {
2337
+ score -= 50;
2338
+ }
2339
+ }
2340
+ }
2341
+ };
2342
+ scoreTokens(pattern.template.tokens, false);
2343
+ const slots = /* @__PURE__ */ new Set();
2344
+ const collectSlots = (tokens) => {
2345
+ for (const token of tokens) {
2346
+ if (token.type === "group") collectSlots(token.tokens);
2347
+ else if (token.type === "role") slots.add(token.role);
2348
+ }
2349
+ };
2350
+ collectSlots(pattern.template.tokens);
2351
+ for (const role of node.roles.keys()) {
2352
+ if (hasRealRole(role) && !slots.has(role)) score -= UNCOVERED_ROLE_PENALTY;
2353
+ }
2354
+ for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
2355
+ const pinnedKey = pinnedLiteral(rule);
2356
+ if (pinnedKey === void 0) continue;
2357
+ const actual = node.roles.get(role);
2358
+ const actualKey = actual?.type === (rule.valueIsExpression ? "expression" : "literal") ? String(
2359
+ actual.type === "expression" ? actual.raw : actual.value
2360
+ ).trim().toLowerCase() : void 0;
2361
+ if (actualKey === pinnedKey) {
2362
+ score += 60;
2363
+ } else if (actualKey === void 0 || pinnedValues.get(role)?.has(actualKey)) {
2364
+ score -= 200;
2365
+ }
2366
+ }
2367
+ score += hooks.adjustScore?.(pattern, node) ?? 0;
2368
+ return { pattern, score };
2369
+ });
2370
+ scored.sort((a, b) => b.score - a.score);
2371
+ return scored.length > 0 ? scored[0].pattern : null;
2372
+ }
2373
+ function renderPattern(node, pattern, hooks) {
2374
+ const parts = [];
2375
+ const language = pattern.language;
2376
+ const tokens = pattern.template.tokens;
2377
+ for (let i = 0; i < tokens.length; i++) {
2378
+ hooks.beforeToken?.(tokens, i, parts, node, language);
2379
+ const span = hooks.span?.(tokens, i, node, language, true);
2380
+ if (span !== void 0) {
2381
+ parts.push(span.text);
2382
+ i += span.tokens - 1;
2383
+ continue;
2384
+ }
2385
+ const token = tokens[i];
2386
+ const rendered = renderPatternToken(token, node, language, hooks);
2387
+ if (rendered !== null) {
2388
+ pushPart(hooks, parts, rendered, token, tokens[i - 1], node, language);
2389
+ hooks.afterToken?.(token, parts.length - 1, node);
2390
+ }
2391
+ }
2392
+ return parts;
2393
+ }
2394
+ function pushPart(hooks, parts, text, token, previous, node, language) {
2395
+ if (hooks.pushPart) hooks.pushPart(parts, text, token, previous, node, language);
2396
+ else parts.push(text);
2397
+ }
2398
+ function renderPatternToken(token, node, language, hooks) {
2399
+ switch (token.type) {
2400
+ case "literal":
2401
+ return token.renderSuppress ? null : token.value;
2402
+ case "role":
2403
+ return hooks.writeRole(token, node, language);
2404
+ case "group": {
2405
+ const hasRequired = token.tokens.filter((t) => t.type === "role" && !t.optional).every((t) => node.roles.has(t.role));
2406
+ if (!hasRequired && token.optional) {
2407
+ return null;
2408
+ }
2409
+ if (hooks.writeGroup && !hooks.writeGroup(token, node, language)) return null;
2410
+ const groupParts = [];
2411
+ let hasRoleValue = false;
2412
+ for (let i = 0; i < token.tokens.length; i++) {
2413
+ const span = hooks.span?.(token.tokens, i, node, language, false);
2414
+ if (span !== void 0) {
2415
+ groupParts.push(span.text);
2416
+ hasRoleValue = true;
2417
+ i += span.tokens - 1;
2418
+ continue;
2419
+ }
2420
+ const subToken = token.tokens[i];
2421
+ const rendered = renderPatternToken(subToken, node, language, hooks);
2422
+ if (rendered !== null) {
2423
+ pushPart(hooks, groupParts, rendered, subToken, token.tokens[i - 1], node, language);
2424
+ if (subToken.type === "role") hasRoleValue = true;
2425
+ }
2426
+ }
2427
+ if (token.optional && !hasRoleValue && !token.renderRequired) return null;
2428
+ return groupParts.length > 0 ? groupParts.join(" ") : null;
2429
+ }
2430
+ default:
2431
+ return null;
2432
+ }
2433
+ }
2434
+
1499
2435
  // src/grammar/types.ts
1500
2436
  function reorderRoles(roles, targetOrder) {
1501
2437
  const result = [];
@@ -1723,7 +2659,53 @@ import {
1723
2659
  // src/ir/explicit-renderer.ts
1724
2660
  import { renderExplicit, renderDocument } from "@lokascript/intent";
1725
2661
 
2662
+ // src/api/reading.ts
2663
+ function roleValues(node) {
2664
+ const out = /* @__PURE__ */ new Map();
2665
+ for (const [role, value] of node.roles) {
2666
+ const v = value;
2667
+ out.set(role, String(v?.value ?? v?.raw ?? value));
2668
+ }
2669
+ return out;
2670
+ }
2671
+ function readingDifference(source, back) {
2672
+ if (back.action !== source.action) return `reads as ${back.action}`;
2673
+ const want = roleValues(source);
2674
+ const got = roleValues(back);
2675
+ const parts = [];
2676
+ for (const [role, value] of want) {
2677
+ if (!got.has(role)) parts.push(`${role} lost`);
2678
+ else if (got.get(role) !== value) parts.push(`${role}: ${value} \u2192 ${got.get(role)}`);
2679
+ }
2680
+ for (const role of got.keys()) if (!want.has(role)) parts.push(`${role} added`);
2681
+ return parts.join(", ");
2682
+ }
2683
+
1726
2684
  // src/api/create-dsl.ts
2685
+ var UnreadInputError = class extends Error {
2686
+ constructor(input, language, reading, unread) {
2687
+ super(`"${input}" (${language}) reads as ${reading.action} only by leaving "${unread}" unread`);
2688
+ this.input = input;
2689
+ this.language = language;
2690
+ this.reading = reading;
2691
+ this.unread = unread;
2692
+ this.code = "unread-input";
2693
+ this.name = "UnreadInputError";
2694
+ }
2695
+ };
2696
+ var TranslationReadBackError = class extends Error {
2697
+ constructor(partial, from, to, source, reading, why) {
2698
+ super(`translation ${from} \u2192 ${to} does not read back as the source (${why}): "${partial}"`);
2699
+ this.partial = partial;
2700
+ this.from = from;
2701
+ this.to = to;
2702
+ this.source = source;
2703
+ this.reading = reading;
2704
+ this.why = why;
2705
+ this.code = "translation-does-not-read-back";
2706
+ this.name = "TranslationReadBackError";
2707
+ }
2708
+ };
1727
2709
  var DSLRegistry = class {
1728
2710
  /**
1729
2711
  * @param partiallyTranslatedActions Actions permitted to lack a keyword in
@@ -1731,7 +2713,8 @@ var DSLRegistry = class {
1731
2713
  * subset of the DSL's languages. A built-in command missing a keyword stays
1732
2714
  * an error, since that is a domain-authoring mistake.
1733
2715
  */
1734
- constructor(config, partiallyTranslatedActions = /* @__PURE__ */ new Set()) {
2716
+ constructor(config, partiallyTranslatedActions, generate) {
2717
+ this.generate = generate;
1735
2718
  this.patterns = /* @__PURE__ */ new Map();
1736
2719
  this.tokenizers = /* @__PURE__ */ new Map();
1737
2720
  this.schemas = config.schemas;
@@ -1747,7 +2730,7 @@ var DSLRegistry = class {
1747
2730
  if (this.partiallyTranslatedActions.has(schema.action) && !lang.patternProfile.keywords[schema.action]) {
1748
2731
  continue;
1749
2732
  }
1750
- const pattern = generatePattern(schema, lang.patternProfile);
2733
+ const pattern = this.generate(schema, lang.patternProfile);
1751
2734
  patterns.push(pattern);
1752
2735
  }
1753
2736
  this.patterns.set(lang.code, patterns);
@@ -1763,9 +2746,9 @@ var DSLRegistry = class {
1763
2746
  }
1764
2747
  };
1765
2748
  var MultilingualDSLImpl = class {
1766
- constructor(config, registry, transformer, profileProvider) {
2749
+ constructor(config, registry, transformer, profileProvider, engine) {
1767
2750
  this.registry = registry;
1768
- this.matcher = new PatternMatcher();
2751
+ this.match = engine.match;
1769
2752
  this.transformer = transformer;
1770
2753
  this.profileProvider = profileProvider;
1771
2754
  if (config.codeGenerator) {
@@ -1778,10 +2761,7 @@ var MultilingualDSLImpl = class {
1778
2761
  for (const extension of config.extensions ?? []) {
1779
2762
  if (extension.render) this.extensionRenderers.set(extension.schema.action, extension.render);
1780
2763
  }
1781
- this.schemaFallbackRenderer = createDomainRenderer({
1782
- schemas: config.schemas,
1783
- profiles: config.languages.map((l) => l.patternProfile)
1784
- });
2764
+ this.schemaFallbackRenderer = engine.fallbackRenderer(config, registry);
1785
2765
  const schemaMap = new Map(config.schemas.map((s) => [s.action, s]));
1786
2766
  this.schemaLookup = {
1787
2767
  getSchema: (action) => schemaMap.get(action)
@@ -1805,28 +2785,50 @@ var MultilingualDSLImpl = class {
1805
2785
  }
1806
2786
  const tokens = tokenizer.tokenize(input);
1807
2787
  const patterns = this.registry.getPatterns(language);
1808
- const profile = {
1809
- code: language
1810
- };
2788
+ const total = tokens.tokens.length;
2789
+ let best = null;
2790
+ let longestPartial = null;
1811
2791
  for (const pattern of patterns) {
1812
- const match = this.matcher.matchPattern(tokens, pattern, profile);
1813
- if (match) {
1814
- const node = {
1815
- kind: "command",
1816
- action: pattern.command,
1817
- roles: match.captured,
1818
- metadata: {
1819
- sourceLanguage: language,
1820
- sourceText: input,
1821
- patternId: pattern.id,
1822
- confidence: match.confidence
1823
- }
1824
- };
1825
- return { node, confidence: match.confidence };
2792
+ const mark = tokens.mark();
2793
+ const match = this.match(tokens, pattern, language);
2794
+ tokens.reset(mark);
2795
+ if (!match) continue;
2796
+ if (match.consumedTokens < total) {
2797
+ if (!longestPartial || match.consumedTokens > longestPartial.consumedTokens)
2798
+ longestPartial = match;
2799
+ continue;
1826
2800
  }
2801
+ if (!best || match.pattern.priority > best.pattern.priority || match.pattern.priority === best.pattern.priority && match.confidence > best.confidence + 1e-3)
2802
+ best = match;
2803
+ }
2804
+ if (best) {
2805
+ const node = this.nodeOf(best, input, language);
2806
+ return { node, confidence: best.confidence };
2807
+ }
2808
+ if (longestPartial) {
2809
+ const unread = tokens.tokens.slice(longestPartial.consumedTokens).map((t) => t.value).join(" ");
2810
+ throw new UnreadInputError(
2811
+ input,
2812
+ language,
2813
+ this.nodeOf(longestPartial, input, language),
2814
+ unread
2815
+ );
1827
2816
  }
1828
2817
  throw new Error(`No pattern matched for input: ${input}`);
1829
2818
  }
2819
+ nodeOf(match, input, language) {
2820
+ return {
2821
+ kind: "command",
2822
+ action: match.pattern.command,
2823
+ roles: match.captured,
2824
+ metadata: {
2825
+ sourceLanguage: language,
2826
+ sourceText: input,
2827
+ patternId: match.pattern.id,
2828
+ confidence: match.confidence
2829
+ }
2830
+ };
2831
+ }
1830
2832
  validate(input, language) {
1831
2833
  try {
1832
2834
  const result = this.parseWithConfidence(input, language);
@@ -1837,11 +2839,12 @@ var MultilingualDSLImpl = class {
1837
2839
  } catch (error) {
1838
2840
  return {
1839
2841
  valid: false,
1840
- errors: [error instanceof Error ? error.message : String(error)]
2842
+ errors: [error instanceof Error ? error.message : String(error)],
2843
+ ...error instanceof UnreadInputError ? { errorCode: error.code } : {}
1841
2844
  };
1842
2845
  }
1843
2846
  }
1844
- translate(input, fromLanguage, toLanguage) {
2847
+ translate(input, fromLanguage, toLanguage, options) {
1845
2848
  if (isExplicitSyntax(input)) return input;
1846
2849
  let node;
1847
2850
  try {
@@ -1853,14 +2856,46 @@ var MultilingualDSLImpl = class {
1853
2856
  throw parseError;
1854
2857
  }
1855
2858
  const rendered = this.registry.getSupportedLanguages().includes(toLanguage) ? this.render(node, toLanguage) : null;
1856
- if (rendered != null) return rendered;
2859
+ if (rendered != null) return this.readsBack(rendered, node, fromLanguage, toLanguage, options);
1857
2860
  if (this.hasGrammarProfile(fromLanguage) && this.hasGrammarProfile(toLanguage)) {
1858
- return this.transformer.transform(input, fromLanguage, toLanguage);
2861
+ return this.readsBack(
2862
+ this.transformer.transform(input, fromLanguage, toLanguage),
2863
+ node,
2864
+ fromLanguage,
2865
+ toLanguage,
2866
+ options
2867
+ );
1859
2868
  }
1860
2869
  throw new Error(
1861
2870
  `translate() could not render action "${node.action}" in language "${toLanguage}": no custom renderer, domain renderer, or schema pattern covers it, and no grammar profile is configured for the transformer fallback. Either add "${toLanguage}" to the DSL's languages/renderer, or set 'grammarProfile' on its LanguageConfig in createMultilingualDSL() (parse/validate/compile do not need it).`
1862
2871
  );
1863
2872
  }
2873
+ /**
2874
+ * `rendered`, read in `to`, must be `source`: the same action and role values, or, when the DSL
2875
+ * compiles, the same code (a parser may keep a translated word its generator normalizes: bdd ja
2876
+ * `表示` is `visible`). The rule is the domain gate's (lokascript-domains). A translation used to be
2877
+ * returned whatever it read as: ja/ko bdd `select` dropped its value, and nothing said so.
2878
+ */
2879
+ readsBack(rendered, source, from, to, options) {
2880
+ if (options?.lossy === "allow") return rendered;
2881
+ let back;
2882
+ try {
2883
+ back = this.parseWithConfidence(rendered, to).node;
2884
+ } catch (error) {
2885
+ const why = error instanceof Error ? error.message : String(error);
2886
+ throw new TranslationReadBackError(rendered, from, to, source, null, why);
2887
+ }
2888
+ const difference = readingDifference(source, back);
2889
+ if (!difference) return rendered;
2890
+ if (this.codeGenerator) {
2891
+ try {
2892
+ if (this.codeGenerator.generate(back) === this.codeGenerator.generate(source))
2893
+ return rendered;
2894
+ } catch {
2895
+ }
2896
+ }
2897
+ throw new TranslationReadBackError(rendered, from, to, source, back, difference);
2898
+ }
1864
2899
  hasGrammarProfile(language) {
1865
2900
  return this.profileProvider.getProfile(language) != null;
1866
2901
  }
@@ -1886,7 +2921,8 @@ var MultilingualDSLImpl = class {
1886
2921
  } catch (error) {
1887
2922
  return {
1888
2923
  ok: false,
1889
- errors: [error instanceof Error ? error.message : String(error)]
2924
+ errors: [error instanceof Error ? error.message : String(error)],
2925
+ ...error instanceof UnreadInputError ? { errorCode: error.code } : {}
1890
2926
  };
1891
2927
  }
1892
2928
  }
@@ -1998,8 +3034,76 @@ function createMultilingualDSL(config) {
1998
3034
  dictionary,
1999
3035
  profileProvider
2000
3036
  });
2001
- const registry = new DSLRegistry(effectiveConfig, extensionActions);
2002
- return new MultilingualDSLImpl(effectiveConfig, registry, transformer, profileProvider);
3037
+ const engine = engineFor(config);
3038
+ const registry = new DSLRegistry(effectiveConfig, extensionActions, engine.generate);
3039
+ return new MultilingualDSLImpl(effectiveConfig, registry, transformer, profileProvider, engine);
3040
+ }
3041
+ function engineFor(config) {
3042
+ const name = config.engine ?? "classic";
3043
+ if (name === "classic") return classicEngine();
3044
+ if (name === "core") return coreEngine();
3045
+ throw new Error(`Unknown DSL engine "${String(name)}" (expected "classic" or "core")`);
3046
+ }
3047
+ function classicEngine() {
3048
+ const matcher = new PatternMatcher();
3049
+ return {
3050
+ generate: (schema, profile) => generatePattern(schema, profile),
3051
+ match: (tokens, pattern, language) => matcher.matchPattern(tokens, pattern, { code: language }),
3052
+ fallbackRenderer: (config) => createDomainRenderer({
3053
+ schemas: config.schemas,
3054
+ profiles: config.languages.map((l) => l.patternProfile)
3055
+ })
3056
+ };
3057
+ }
3058
+ function schemaProfileOf(profile) {
3059
+ const side = profile.wordOrder === "SOV" ? "after" : "before";
3060
+ const roleMarkers = {};
3061
+ for (const [role, marker] of Object.entries(profile.roleMarkers ?? {})) {
3062
+ roleMarkers[role] = {
3063
+ primary: marker.primary,
3064
+ ...marker.alternatives ? { alternatives: marker.alternatives } : {},
3065
+ position: marker.position ?? side
3066
+ };
3067
+ }
3068
+ return {
3069
+ code: profile.code,
3070
+ wordOrder: profile.wordOrder,
3071
+ keywords: profile.keywords,
3072
+ roleMarkers
3073
+ };
3074
+ }
3075
+ function coreEngine() {
3076
+ const generator = createSchemaPatternGenerator({ positionOrder: "descending" });
3077
+ const matcher = new MatchSkeleton(createStockReader(), stockConfidenceModel);
3078
+ return {
3079
+ generate: (schema, profile) => generator.generatePattern(schema, schemaProfileOf(profile)),
3080
+ match: (tokens, pattern) => matcher.matchPattern(tokens, pattern),
3081
+ fallbackRenderer: (config, registry) => {
3082
+ const schemas = new Map(config.schemas.map((s) => [s.action, s]));
3083
+ const hooks = {
3084
+ hasRealRole: (node, role) => {
3085
+ const value = node.roles.get(role);
3086
+ return value !== void 0 && value.implicit !== true;
3087
+ },
3088
+ writeRole: (token, node) => {
3089
+ if (!node.roles.has(token.role)) return null;
3090
+ let value = extractRoleValue(node, token.role);
3091
+ const spec = schemas.get(node.action)?.roles.find((r) => r.role === token.role);
3092
+ if (spec?.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
3093
+ value = `"${value}"`;
3094
+ }
3095
+ return value || null;
3096
+ }
3097
+ };
3098
+ return (node, language) => {
3099
+ const patterns = registry.getPatterns(language).filter((p) => p.command === node.action);
3100
+ const pattern = selectRenderPattern(node, patterns, hooks);
3101
+ if (!pattern) return null;
3102
+ const text = renderPattern(node, pattern, hooks).join(" ");
3103
+ return text || null;
3104
+ };
3105
+ }
3106
+ };
2003
3107
  }
2004
3108
 
2005
3109
  // src/prompts/prompt-generator.ts
@@ -2804,6 +3908,8 @@ var DomainRegistry = class {
2804
3908
  return jsonResponse({ error: `Unknown operation: ${parsed.operation}` }, true);
2805
3909
  }
2806
3910
  } catch (error) {
3911
+ if (error instanceof UnreadInputError) return unreadInputResponse(error);
3912
+ if (error instanceof TranslationReadBackError) return readBackResponse(error);
2807
3913
  const message = error instanceof Error ? error.message : String(error);
2808
3914
  return jsonResponse({ error: `${descriptor.name} tool error: ${message}` }, true);
2809
3915
  }
@@ -2929,6 +4035,7 @@ var DomainRegistry = class {
2929
4035
  ok: result.ok,
2930
4036
  code: result.code,
2931
4037
  errors: result.errors,
4038
+ ...result.errorCode === "unread-input" ? { diagnostics: [UNREAD_INPUT(result.errors)] } : {},
2932
4039
  language,
2933
4040
  input
2934
4041
  });
@@ -2941,6 +4048,7 @@ var DomainRegistry = class {
2941
4048
  return jsonResponse({
2942
4049
  valid: result.valid,
2943
4050
  errors: result.errors,
4051
+ ...result.errorCode === "unread-input" ? { diagnostics: [UNREAD_INPUT(result.errors)] } : {},
2944
4052
  language,
2945
4053
  [descriptor.inputLabel]: input
2946
4054
  });
@@ -2971,7 +4079,24 @@ var DomainRegistry = class {
2971
4079
  for (const [key, value] of node.roles) {
2972
4080
  roles[key] = value;
2973
4081
  }
4082
+ if (rendered != null) {
4083
+ let back = null;
4084
+ let why = "";
4085
+ try {
4086
+ back = dsl.parse(rendered, to);
4087
+ why = readingDifference(node, back);
4088
+ if (why && compiled.ok) {
4089
+ const again = dsl.compile(rendered, to);
4090
+ if (again.ok && again.code === compiled.code) why = "";
4091
+ }
4092
+ } catch (error) {
4093
+ why = error instanceof Error ? error.message : String(error);
4094
+ }
4095
+ if (why)
4096
+ return readBackResponse(new TranslationReadBackError(rendered, from, to, node, back, why));
4097
+ }
2974
4098
  return jsonResponse({
4099
+ ok: true,
2975
4100
  input: { [descriptor.inputLabel]: input, language: from },
2976
4101
  ...rendered != null && { rendered: { text: rendered, language: to } },
2977
4102
  semantic: { action: node.action, roles },
@@ -3051,7 +4176,7 @@ function generateToolDefinitions(desc) {
3051
4176
  case "translate":
3052
4177
  tools.push({
3053
4178
  name: `translate_${desc.name}`,
3054
- description: `Translate ${desc.description} input between natural languages (requires 'from' and 'to' codes). Supports: ${langList}.`,
4179
+ description: `Translate ${desc.description} input between natural languages (requires 'from' and 'to' codes). Supports: ${langList}. A rendering that does not read back as the source is REFUSED: ok is false, the diagnostic is TRANSLATION_DOES_NOT_READ_BACK, and its text is under \`partial\`, never \`rendered\`: do not present it as the translation. Input left unread is refused as UNREAD_INPUT.`,
3055
4180
  inputSchema: {
3056
4181
  type: "object",
3057
4182
  properties: {
@@ -3085,6 +4210,46 @@ function getString(args, name, defaultValue = "") {
3085
4210
  const value = args[name];
3086
4211
  return typeof value === "string" ? value : defaultValue;
3087
4212
  }
4213
+ function readingOf(node) {
4214
+ const roles = {};
4215
+ for (const [key, value] of node.roles) roles[key] = value;
4216
+ return { action: node.action, roles };
4217
+ }
4218
+ var UNREAD_INPUT = (errors) => ({
4219
+ severity: "error",
4220
+ code: "UNREAD_INPUT",
4221
+ message: errors?.[0] ?? "part of the input was not read",
4222
+ suggestion: "Part of the input belongs to no role of the command it reads as: remove it, or quote a value of several words."
4223
+ });
4224
+ function unreadInputResponse(error) {
4225
+ return jsonResponse(
4226
+ {
4227
+ ok: false,
4228
+ unread: error.unread,
4229
+ reading: readingOf(error.reading),
4230
+ diagnostics: [UNREAD_INPUT([error.message])]
4231
+ },
4232
+ true
4233
+ );
4234
+ }
4235
+ function readBackResponse(error) {
4236
+ return jsonResponse(
4237
+ {
4238
+ ok: false,
4239
+ partial: error.partial,
4240
+ ...error.reading ? { reading: readingOf(error.reading) } : {},
4241
+ diagnostics: [
4242
+ {
4243
+ severity: "error",
4244
+ code: "TRANSLATION_DOES_NOT_READ_BACK",
4245
+ message: error.message,
4246
+ suggestion: "The rendering would not mean the source in the target language: do not present `partial` as the translation."
4247
+ }
4248
+ ]
4249
+ },
4250
+ true
4251
+ );
4252
+ }
3088
4253
  function jsonResponse(data, isError) {
3089
4254
  return {
3090
4255
  content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
@@ -3234,9 +4399,58 @@ var CrossDomainDispatcher = class {
3234
4399
  return idx === -1 ? this.priority.length : idx;
3235
4400
  }
3236
4401
  };
4402
+
4403
+ // src/api/translation-loss.ts
4404
+ var LossyTranslationError = class extends Error {
4405
+ constructor(partial, loss, from, to) {
4406
+ super(
4407
+ `translation ${from} \u2192 ${to} would lose part of the script (${loss.kind}): ` + loss.lost.join(", ")
4408
+ );
4409
+ this.partial = partial;
4410
+ this.loss = loss;
4411
+ this.from = from;
4412
+ this.to = to;
4413
+ this.name = "LossyTranslationError";
4414
+ }
4415
+ };
4416
+ function unconsumedSpans(node) {
4417
+ const out = [];
4418
+ for (const d of node.diagnostics ?? []) {
4419
+ if (d.code !== "unconsumed-input") continue;
4420
+ out.push(/"(.*)"\s*$/s.exec(d.message)?.[1] ?? d.message);
4421
+ }
4422
+ return out;
4423
+ }
4424
+ function findTranslationLoss(input, node, output, readBack, rules) {
4425
+ const unread = unconsumedSpans(node);
4426
+ if (unread.length) return { kind: "truncation", lost: unread };
4427
+ const unwritable = rules.unwritable?.(node) ?? [];
4428
+ if (unwritable.length) return { kind: "core-only", lost: unwritable };
4429
+ let back;
4430
+ try {
4431
+ back = readBack(output);
4432
+ } catch (e) {
4433
+ return { kind: "read-back", lost: [`the output does not read: ${e.message}`] };
4434
+ }
4435
+ const misread = unconsumedSpans(back);
4436
+ if (misread.length) return { kind: "read-back", lost: misread };
4437
+ const written = rules.written ? rules.written(node) : node;
4438
+ for (const difference of rules.differences) {
4439
+ const lost = difference(written, back);
4440
+ if (lost.length) return { kind: "read-back", lost };
4441
+ }
4442
+ const values = rules.missingValues?.(input, output) ?? [];
4443
+ if (values.length) return { kind: "invariant", lost: values };
4444
+ return void 0;
4445
+ }
3237
4446
  export {
3238
4447
  CrossDomainDispatcher,
3239
4448
  DomainRegistry,
3240
- createMultilingualDSL
4449
+ LossyTranslationError,
4450
+ TranslationReadBackError,
4451
+ UnreadInputError,
4452
+ createMultilingualDSL,
4453
+ findTranslationLoss,
4454
+ unconsumedSpans
3241
4455
  };
3242
4456
  //# sourceMappingURL=index.js.map