@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
@@ -1,24 +1,3 @@
1
- // src/core/types.ts
2
- import {
3
- createLiteral,
4
- createSelector,
5
- createReference,
6
- createPropertyPath,
7
- createExpression,
8
- createFlag,
9
- createCommandNode,
10
- createEventHandlerNode,
11
- createConditionalNode,
12
- createCompoundNode,
13
- createLoopNode,
14
- createTryNode,
15
- createAsyncNode,
16
- createMatchNode,
17
- extractValue,
18
- extractRoleValue,
19
- getRoleValue
20
- } from "@lokascript/intent";
21
-
22
1
  // src/core/logger.ts
23
2
  var DebugLogger = class {
24
3
  constructor(namespace) {
@@ -86,6 +65,27 @@ function createLogger(namespace) {
86
65
  return new DebugLogger(namespace);
87
66
  }
88
67
 
68
+ // src/core/types.ts
69
+ import {
70
+ createLiteral,
71
+ createSelector,
72
+ createReference,
73
+ createPropertyPath,
74
+ createExpression,
75
+ createFlag,
76
+ createCommandNode,
77
+ createEventHandlerNode,
78
+ createConditionalNode,
79
+ createCompoundNode,
80
+ createLoopNode,
81
+ createTryNode,
82
+ createAsyncNode,
83
+ createMatchNode,
84
+ extractValue,
85
+ extractRoleValue,
86
+ getRoleValue
87
+ } from "@lokascript/intent";
88
+
89
89
  // src/core/pattern-matching/utils/type-validation.ts
90
90
  function isTypeCompatible(actualType, expectedTypes) {
91
91
  if (!expectedTypes || expectedTypes.length === 0) {
@@ -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:
@@ -426,372 +943,6 @@ var _PatternMatcher = class _PatternMatcher {
426
943
  }
427
944
  return false;
428
945
  }
429
- /**
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)
435
- */
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"
445
- );
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;
488
- }
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
946
  /**
796
947
  * Match a group pattern token (optional sequence).
797
948
  * When the group's leading marker isn't at the current position, scans ahead
@@ -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)
@@ -1222,22 +1139,6 @@ var _PatternMatcher = class _PatternMatcher {
1222
1139
  };
1223
1140
  /** Maximum tokens to scan ahead when looking for a group's leading marker */
1224
1141
  _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
1142
  var PatternMatcher = _PatternMatcher;
1242
1143
  var patternMatcher = new PatternMatcher();
1243
1144