@lokascript/framework 2.0.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 (181) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +142 -0
  3. package/dist/aot/aot-orchestrator.d.ts +75 -0
  4. package/dist/aot/aot-orchestrator.d.ts.map +1 -0
  5. package/dist/aot/domain-scanner.d.ts +27 -0
  6. package/dist/aot/domain-scanner.d.ts.map +1 -0
  7. package/dist/aot/index.d.ts +8 -0
  8. package/dist/aot/index.d.ts.map +1 -0
  9. package/dist/aot/types.d.ts +103 -0
  10. package/dist/aot/types.d.ts.map +1 -0
  11. package/dist/api/create-dsl.d.ts +91 -0
  12. package/dist/api/create-dsl.d.ts.map +1 -0
  13. package/dist/api/dispatcher.d.ts +108 -0
  14. package/dist/api/dispatcher.d.ts.map +1 -0
  15. package/dist/api/domain-registry.d.ts +152 -0
  16. package/dist/api/domain-registry.d.ts.map +1 -0
  17. package/dist/api/index.d.ts +7 -0
  18. package/dist/api/index.d.ts.map +1 -0
  19. package/dist/api/index.js +2082 -0
  20. package/dist/api/index.js.map +1 -0
  21. package/dist/core/index.d.ts +7 -0
  22. package/dist/core/index.d.ts.map +1 -0
  23. package/dist/core/index.js +2674 -0
  24. package/dist/core/index.js.map +1 -0
  25. package/dist/core/logger.d.ts +32 -0
  26. package/dist/core/logger.d.ts.map +1 -0
  27. package/dist/core/pattern-matching/index.d.ts +6 -0
  28. package/dist/core/pattern-matching/index.d.ts.map +1 -0
  29. package/dist/core/pattern-matching/index.js +1239 -0
  30. package/dist/core/pattern-matching/index.js.map +1 -0
  31. package/dist/core/pattern-matching/pattern-matcher.d.ts +239 -0
  32. package/dist/core/pattern-matching/pattern-matcher.d.ts.map +1 -0
  33. package/dist/core/pattern-matching/utils/index.d.ts +6 -0
  34. package/dist/core/pattern-matching/utils/index.d.ts.map +1 -0
  35. package/dist/core/pattern-matching/utils/possessive-keywords.d.ts +38 -0
  36. package/dist/core/pattern-matching/utils/possessive-keywords.d.ts.map +1 -0
  37. package/dist/core/pattern-matching/utils/type-validation.d.ts +63 -0
  38. package/dist/core/pattern-matching/utils/type-validation.d.ts.map +1 -0
  39. package/dist/core/tokenization/base-tokenizer.d.ts +344 -0
  40. package/dist/core/tokenization/base-tokenizer.d.ts.map +1 -0
  41. package/dist/core/tokenization/char-classifiers.d.ts +56 -0
  42. package/dist/core/tokenization/char-classifiers.d.ts.map +1 -0
  43. package/dist/core/tokenization/default-extractors.d.ts +48 -0
  44. package/dist/core/tokenization/default-extractors.d.ts.map +1 -0
  45. package/dist/core/tokenization/extractors/index.d.ts +9 -0
  46. package/dist/core/tokenization/extractors/index.d.ts.map +1 -0
  47. package/dist/core/tokenization/extractors/operator.d.ts +23 -0
  48. package/dist/core/tokenization/extractors/operator.d.ts.map +1 -0
  49. package/dist/core/tokenization/extractors/punctuation.d.ts +22 -0
  50. package/dist/core/tokenization/extractors/punctuation.d.ts.map +1 -0
  51. package/dist/core/tokenization/extractors.d.ts +61 -0
  52. package/dist/core/tokenization/extractors.d.ts.map +1 -0
  53. package/dist/core/tokenization/index.d.ts +11 -0
  54. package/dist/core/tokenization/index.d.ts.map +1 -0
  55. package/dist/core/tokenization/index.js +1345 -0
  56. package/dist/core/tokenization/index.js.map +1 -0
  57. package/dist/core/tokenization/morphology/index.d.ts +5 -0
  58. package/dist/core/tokenization/morphology/index.d.ts.map +1 -0
  59. package/dist/core/tokenization/morphology/types.d.ts +110 -0
  60. package/dist/core/tokenization/morphology/types.d.ts.map +1 -0
  61. package/dist/core/tokenization/token-utils.d.ts +111 -0
  62. package/dist/core/tokenization/token-utils.d.ts.map +1 -0
  63. package/dist/core/types.d.ts +382 -0
  64. package/dist/core/types.d.ts.map +1 -0
  65. package/dist/core/types.js +108 -0
  66. package/dist/core/types.js.map +1 -0
  67. package/dist/generation/diagnostics.d.ts +120 -0
  68. package/dist/generation/diagnostics.d.ts.map +1 -0
  69. package/dist/generation/index.d.ts +7 -0
  70. package/dist/generation/index.d.ts.map +1 -0
  71. package/dist/generation/index.js +339 -0
  72. package/dist/generation/index.js.map +1 -0
  73. package/dist/generation/pattern-generator.d.ts +48 -0
  74. package/dist/generation/pattern-generator.d.ts.map +1 -0
  75. package/dist/generation/renderer.d.ts +115 -0
  76. package/dist/generation/renderer.d.ts.map +1 -0
  77. package/dist/grammar/index.d.ts +10 -0
  78. package/dist/grammar/index.d.ts.map +1 -0
  79. package/dist/grammar/index.js +391 -0
  80. package/dist/grammar/index.js.map +1 -0
  81. package/dist/grammar/transformer.d.ts +56 -0
  82. package/dist/grammar/transformer.d.ts.map +1 -0
  83. package/dist/grammar/types.d.ts +236 -0
  84. package/dist/grammar/types.d.ts.map +1 -0
  85. package/dist/index.cjs +4454 -0
  86. package/dist/index.cjs.map +1 -0
  87. package/dist/index.d.ts +46 -0
  88. package/dist/index.d.ts.map +1 -0
  89. package/dist/index.js +4336 -0
  90. package/dist/index.js.map +1 -0
  91. package/dist/interfaces/dictionary.d.ts +82 -0
  92. package/dist/interfaces/dictionary.d.ts.map +1 -0
  93. package/dist/interfaces/index.d.ts +10 -0
  94. package/dist/interfaces/index.d.ts.map +1 -0
  95. package/dist/interfaces/profile-provider.d.ts +67 -0
  96. package/dist/interfaces/profile-provider.d.ts.map +1 -0
  97. package/dist/interfaces/value-extractor.d.ts +168 -0
  98. package/dist/interfaces/value-extractor.d.ts.map +1 -0
  99. package/dist/multilingual/index.d.ts +8 -0
  100. package/dist/multilingual/index.d.ts.map +1 -0
  101. package/dist/multilingual/index.js +1 -0
  102. package/dist/multilingual/index.js.map +1 -0
  103. package/dist/parsing/index.d.ts +8 -0
  104. package/dist/parsing/index.d.ts.map +1 -0
  105. package/dist/parsing/index.js +1415 -0
  106. package/dist/parsing/index.js.map +1 -0
  107. package/dist/parsing/multi-statement.d.ts +265 -0
  108. package/dist/parsing/multi-statement.d.ts.map +1 -0
  109. package/dist/schema/command-schema.d.ts +78 -0
  110. package/dist/schema/command-schema.d.ts.map +1 -0
  111. package/dist/schema/index.d.ts +5 -0
  112. package/dist/schema/index.d.ts.map +1 -0
  113. package/dist/schema/index.js +25 -0
  114. package/dist/schema/index.js.map +1 -0
  115. package/dist/test-setup.d.ts +9 -0
  116. package/dist/test-setup.d.ts.map +1 -0
  117. package/dist/testing/index.d.ts +50 -0
  118. package/dist/testing/index.d.ts.map +1 -0
  119. package/dist/testing/index.js +16969 -0
  120. package/dist/testing/index.js.map +1 -0
  121. package/package.json +122 -0
  122. package/src/__test__/fixtures/sql-dsl.ts +232 -0
  123. package/src/__test__/sql-integration.test.ts +189 -0
  124. package/src/__test__/test-utils.ts +260 -0
  125. package/src/aot/aot-orchestrator.test.ts +413 -0
  126. package/src/aot/aot-orchestrator.ts +238 -0
  127. package/src/aot/domain-scanner.ts +178 -0
  128. package/src/aot/index.ts +8 -0
  129. package/src/aot/types.ts +124 -0
  130. package/src/api/create-dsl.ts +367 -0
  131. package/src/api/dispatcher.test.ts +336 -0
  132. package/src/api/dispatcher.ts +222 -0
  133. package/src/api/domain-registry.test.ts +336 -0
  134. package/src/api/domain-registry.ts +500 -0
  135. package/src/api/index.ts +7 -0
  136. package/src/core/index.ts +7 -0
  137. package/src/core/logger.ts +130 -0
  138. package/src/core/pattern-matching/index.ts +6 -0
  139. package/src/core/pattern-matching/pattern-matcher.test.ts +900 -0
  140. package/src/core/pattern-matching/pattern-matcher.ts +1548 -0
  141. package/src/core/pattern-matching/pattern-matcher.ts.backup +1267 -0
  142. package/src/core/pattern-matching/utils/index.ts +6 -0
  143. package/src/core/pattern-matching/utils/possessive-keywords.ts +55 -0
  144. package/src/core/pattern-matching/utils/type-validation.test.ts +316 -0
  145. package/src/core/pattern-matching/utils/type-validation.ts +134 -0
  146. package/src/core/tokenization/base-tokenizer.ts +916 -0
  147. package/src/core/tokenization/char-classifiers.ts +79 -0
  148. package/src/core/tokenization/create-simple-tokenizer.test.ts +260 -0
  149. package/src/core/tokenization/default-extractors.ts +69 -0
  150. package/src/core/tokenization/extractors/index.ts +9 -0
  151. package/src/core/tokenization/extractors/operator.ts +75 -0
  152. package/src/core/tokenization/extractors/punctuation.ts +39 -0
  153. package/src/core/tokenization/extractors.ts +452 -0
  154. package/src/core/tokenization/index.ts +11 -0
  155. package/src/core/tokenization/morphology/index.ts +5 -0
  156. package/src/core/tokenization/morphology/types.ts +211 -0
  157. package/src/core/tokenization/token-utils.ts +252 -0
  158. package/src/core/types.ts +589 -0
  159. package/src/generation/diagnostics.test.ts +171 -0
  160. package/src/generation/diagnostics.ts +239 -0
  161. package/src/generation/index.ts +7 -0
  162. package/src/generation/pattern-generator.test.ts +430 -0
  163. package/src/generation/pattern-generator.ts +315 -0
  164. package/src/generation/renderer.test.ts +266 -0
  165. package/src/generation/renderer.ts +244 -0
  166. package/src/grammar/index.ts +12 -0
  167. package/src/grammar/transformer.ts +159 -0
  168. package/src/grammar/types.ts +630 -0
  169. package/src/index.ts +157 -0
  170. package/src/interfaces/dictionary.ts +123 -0
  171. package/src/interfaces/index.ts +10 -0
  172. package/src/interfaces/profile-provider.ts +88 -0
  173. package/src/interfaces/value-extractor.ts +435 -0
  174. package/src/multilingual/index.ts +9 -0
  175. package/src/parsing/index.ts +27 -0
  176. package/src/parsing/multi-statement.test.ts +480 -0
  177. package/src/parsing/multi-statement.ts +648 -0
  178. package/src/schema/command-schema.ts +118 -0
  179. package/src/schema/index.ts +5 -0
  180. package/src/test-setup.ts +45 -0
  181. package/src/testing/index.ts +137 -0
@@ -0,0 +1,1239 @@
1
+ // src/core/types.ts
2
+ function createLiteral(value, dataType) {
3
+ return dataType ? { type: "literal", value, dataType } : { type: "literal", value };
4
+ }
5
+ function createSelector(value, selectorKind) {
6
+ return selectorKind ? { type: "selector", value, selectorKind } : { type: "selector", value };
7
+ }
8
+ function createReference(value) {
9
+ return { type: "reference", value };
10
+ }
11
+ function createPropertyPath(object, property) {
12
+ return { type: "property-path", object, property };
13
+ }
14
+
15
+ // src/core/logger.ts
16
+ var DebugLogger = class {
17
+ constructor(namespace) {
18
+ this.namespace = namespace;
19
+ this.enabled = this.checkEnabled();
20
+ }
21
+ checkEnabled() {
22
+ if (typeof process !== "undefined" && process.env) {
23
+ const DEBUG = process.env.DEBUG || "";
24
+ return DEBUG === "*" || DEBUG.includes("framework:*") || DEBUG.includes(`framework:${this.namespace}`);
25
+ }
26
+ return false;
27
+ }
28
+ log(level, message, ...args) {
29
+ if (!this.enabled) return;
30
+ const prefix = `[framework:${this.namespace}]`;
31
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
32
+ switch (level) {
33
+ case "debug":
34
+ console.debug(`${timestamp} ${prefix} DEBUG:`, message, ...args);
35
+ break;
36
+ case "info":
37
+ console.info(`${timestamp} ${prefix} INFO:`, message, ...args);
38
+ break;
39
+ case "warn":
40
+ console.warn(`${timestamp} ${prefix} WARN:`, message, ...args);
41
+ break;
42
+ case "error":
43
+ console.error(`${timestamp} ${prefix} ERROR:`, message, ...args);
44
+ break;
45
+ }
46
+ }
47
+ debug(message, ...args) {
48
+ this.log("debug", message, ...args);
49
+ }
50
+ info(message, ...args) {
51
+ this.log("info", message, ...args);
52
+ }
53
+ warn(message, ...args) {
54
+ this.log("warn", message, ...args);
55
+ }
56
+ error(message, ...args) {
57
+ this.log("error", message, ...args);
58
+ }
59
+ /**
60
+ * Check if logging is enabled for this namespace.
61
+ */
62
+ isEnabled() {
63
+ return this.enabled;
64
+ }
65
+ /**
66
+ * Programmatically enable logging for this namespace.
67
+ */
68
+ enable() {
69
+ this.enabled = true;
70
+ }
71
+ /**
72
+ * Programmatically disable logging for this namespace.
73
+ */
74
+ disable() {
75
+ this.enabled = false;
76
+ }
77
+ };
78
+ function createLogger(namespace) {
79
+ return new DebugLogger(namespace);
80
+ }
81
+
82
+ // src/core/pattern-matching/utils/type-validation.ts
83
+ function isTypeCompatible(actualType, expectedTypes) {
84
+ if (!expectedTypes || expectedTypes.length === 0) {
85
+ return true;
86
+ }
87
+ if (expectedTypes.includes(actualType)) {
88
+ return true;
89
+ }
90
+ if (expectedTypes.includes("expression")) {
91
+ return true;
92
+ }
93
+ if (actualType === "property-path") {
94
+ return expectedTypes.some((t) => ["selector", "reference", "expression"].includes(t));
95
+ }
96
+ return false;
97
+ }
98
+ function validateValueType(value, expectedTypes) {
99
+ if (!expectedTypes || expectedTypes.length === 0) {
100
+ return true;
101
+ }
102
+ return isTypeCompatible(value.type, expectedTypes);
103
+ }
104
+ function isCSSSelector(value) {
105
+ return value.startsWith(".") || value.startsWith("#") || value.startsWith("<");
106
+ }
107
+ function isClassName(value) {
108
+ return value.startsWith(".");
109
+ }
110
+ function isIdSelector(value) {
111
+ return value.startsWith("#");
112
+ }
113
+ function isCSSPropertyRef(value) {
114
+ return value.startsWith("*");
115
+ }
116
+ function isNumericValue(value) {
117
+ const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
118
+ if (durationMatch) {
119
+ return true;
120
+ }
121
+ const num = parseFloat(value);
122
+ return !isNaN(num) && isFinite(num);
123
+ }
124
+ function isPropertyName(value) {
125
+ return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);
126
+ }
127
+ function isVariableRef(value) {
128
+ return value.startsWith(":");
129
+ }
130
+ function isBuiltInReference(value) {
131
+ const builtIns = /* @__PURE__ */ new Set(["me", "you", "it", "result", "event", "target", "body"]);
132
+ return builtIns.has(value.toLowerCase());
133
+ }
134
+
135
+ // src/core/pattern-matching/utils/possessive-keywords.ts
136
+ function getPossessiveReference(profile, keyword) {
137
+ return profile?.possessive?.keywords?.[keyword];
138
+ }
139
+ function isPossessiveKeyword(profile, keyword) {
140
+ return profile?.possessive?.keywords?.[keyword] !== void 0;
141
+ }
142
+ function getAllPossessiveKeywords(profile) {
143
+ return profile?.possessive?.keywords ?? {};
144
+ }
145
+
146
+ // src/core/pattern-matching/pattern-matcher.ts
147
+ function isValidReference(_value) {
148
+ return false;
149
+ }
150
+ var _PatternMatcher = class _PatternMatcher {
151
+ constructor() {
152
+ /** Debug logger */
153
+ this.logger = createLogger("pattern-matcher");
154
+ /**
155
+ * Track stem matches for confidence calculation.
156
+ * This is set during matching and read during confidence calculation.
157
+ */
158
+ this.stemMatchCount = 0;
159
+ this.totalKeywordMatches = 0;
160
+ }
161
+ /**
162
+ * Safely convert a value to lowercase string.
163
+ * Provides protection against non-string values at runtime.
164
+ */
165
+ safeToLowerCase(value) {
166
+ if (typeof value === "string") {
167
+ return value.toLowerCase();
168
+ }
169
+ if (value === null || value === void 0) {
170
+ return "";
171
+ }
172
+ return String(value).toLowerCase();
173
+ }
174
+ /**
175
+ * Try to match a single pattern against the token stream.
176
+ * Returns the match result or null if no match.
177
+ *
178
+ * @param tokens - Token stream to match against
179
+ * @param pattern - Pattern to match
180
+ * @param profile - Optional language profile for possessive handling
181
+ */
182
+ matchPattern(tokens, pattern, profile) {
183
+ const mark = tokens.mark();
184
+ const captured = /* @__PURE__ */ new Map();
185
+ this.logger.debug("========================================");
186
+ this.logger.debug("matchPattern ENTRY");
187
+ this.logger.debug("Pattern ID:", pattern.id);
188
+ this.logger.debug("Pattern command:", pattern.command);
189
+ this.logger.debug("Pattern language:", pattern.language);
190
+ this.logger.debug("Pattern template:", JSON.stringify(pattern.template, null, 2));
191
+ if (this.logger.isEnabled()) {
192
+ const firstTokens = [];
193
+ for (let i = 0; i < 10; i++) {
194
+ const t = tokens.peek(i);
195
+ if (t)
196
+ firstTokens.push({
197
+ type: t.type,
198
+ value: t.value,
199
+ kind: t.kind
200
+ });
201
+ else break;
202
+ }
203
+ this.logger.debug("Input tokens (first 10):", firstTokens);
204
+ this.logger.debug("Profile code:", profile?.code);
205
+ }
206
+ this.currentProfile = profile;
207
+ this.stemMatchCount = 0;
208
+ this.totalKeywordMatches = 0;
209
+ this.logger.debug("--- Calling matchTokenSequence ---");
210
+ this.logger.debug("Pattern tokens to match:", JSON.stringify(pattern.template.tokens, null, 2));
211
+ const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
212
+ this.logger.debug("matchTokenSequence returned:", success);
213
+ this.logger.debug(
214
+ "Captured roles:",
215
+ Array.from(captured.entries()).map(([k, v]) => [k, JSON.stringify(v)])
216
+ );
217
+ if (!success) {
218
+ this.logger.debug(">>> MATCH FAILED - resetting token position");
219
+ tokens.reset(mark);
220
+ return null;
221
+ }
222
+ const confidence = this.calculateConfidence(pattern, captured);
223
+ this.applyExtractionRules(pattern, captured);
224
+ return {
225
+ pattern,
226
+ captured,
227
+ consumedTokens: tokens.position() - mark.position,
228
+ confidence
229
+ };
230
+ }
231
+ /**
232
+ * Try to match multiple patterns, return the best match.
233
+ *
234
+ * @param tokens - Token stream to match against
235
+ * @param patterns - Candidate patterns to try
236
+ * @param profile - Optional language profile for possessive handling
237
+ */
238
+ matchBest(tokens, patterns, profile) {
239
+ const matches = [];
240
+ for (const pattern of patterns) {
241
+ const mark = tokens.mark();
242
+ const result = this.matchPattern(tokens, pattern, profile);
243
+ if (result) {
244
+ matches.push(result);
245
+ }
246
+ tokens.reset(mark);
247
+ }
248
+ if (matches.length === 0) {
249
+ return null;
250
+ }
251
+ matches.sort((a, b) => {
252
+ const priorityDiff = b.pattern.priority - a.pattern.priority;
253
+ if (priorityDiff !== 0) return priorityDiff;
254
+ const confidenceDiff = b.confidence - a.confidence;
255
+ if (Math.abs(confidenceDiff) > 1e-3) return confidenceDiff;
256
+ return b.consumedTokens - a.consumedTokens;
257
+ });
258
+ const best = matches[0];
259
+ this.matchPattern(tokens, best.pattern);
260
+ return best;
261
+ }
262
+ /**
263
+ * Match a sequence of pattern tokens against the token stream.
264
+ *
265
+ * Supports bounded single-step backtracking: if an optional role consumes
266
+ * a token and the immediately following pattern token fails, the matcher
267
+ * resets to before the optional role and retries the failed token.
268
+ */
269
+ matchTokenSequence(tokens, patternTokens, captured) {
270
+ const firstPatternToken = patternTokens[0];
271
+ const patternExpectsConjunction = firstPatternToken?.type === "literal" && (firstPatternToken.value === "and" || firstPatternToken.value === "then" || firstPatternToken.alternatives?.includes("and") || firstPatternToken.alternatives?.includes("then"));
272
+ if (this.currentProfile?.code === "ar" && !patternExpectsConjunction) {
273
+ while (tokens.peek()?.kind === "conjunction") {
274
+ tokens.advance();
275
+ }
276
+ }
277
+ let prevOptionalMark = null;
278
+ let prevOptionalRole = null;
279
+ for (let i = 0; i < patternTokens.length; i++) {
280
+ const patternToken = patternTokens[i];
281
+ this.logger.debug(" >> Matching pattern token:", JSON.stringify(patternToken, null, 2));
282
+ const currTok = tokens.peek();
283
+ this.logger.debug(
284
+ " >> Current input token:",
285
+ currTok ? JSON.stringify({
286
+ type: currTok.type,
287
+ value: currTok.value,
288
+ kind: currTok.kind
289
+ }) : "EOF"
290
+ );
291
+ if (patternToken.type === "role" && patternToken.greedy) {
292
+ const stopMarkers = this.collectStopMarkers(patternTokens, i + 1);
293
+ const values = [];
294
+ while (!tokens.isAtEnd()) {
295
+ const nextToken = tokens.peek();
296
+ if (!nextToken) break;
297
+ if (this.isStopMarker(nextToken, stopMarkers)) break;
298
+ values.push(nextToken.value);
299
+ tokens.advance();
300
+ }
301
+ if (values.length > 0) {
302
+ captured.set(patternToken.role, { type: "expression", raw: values.join(" ") });
303
+ prevOptionalMark = null;
304
+ prevOptionalRole = null;
305
+ continue;
306
+ } else if (patternToken.optional) {
307
+ continue;
308
+ } else {
309
+ return false;
310
+ }
311
+ }
312
+ const isOptionalRole = patternToken.type === "role" && patternToken.optional === true;
313
+ const markBefore = isOptionalRole ? tokens.mark() : null;
314
+ const matched = this.matchPatternToken(tokens, patternToken, captured);
315
+ this.logger.debug(" >> Match result:", matched);
316
+ if (matched) {
317
+ if (isOptionalRole) {
318
+ prevOptionalMark = markBefore;
319
+ prevOptionalRole = patternToken.role;
320
+ } else {
321
+ prevOptionalMark = null;
322
+ prevOptionalRole = null;
323
+ }
324
+ continue;
325
+ }
326
+ this.logger.debug(" >> Token match FAILED");
327
+ if (this.isOptional(patternToken)) {
328
+ continue;
329
+ }
330
+ if (prevOptionalMark && prevOptionalRole) {
331
+ this.logger.debug(" >> BACKTRACKING: undoing optional role", prevOptionalRole);
332
+ tokens.reset(prevOptionalMark);
333
+ captured.delete(prevOptionalRole);
334
+ prevOptionalMark = null;
335
+ prevOptionalRole = null;
336
+ const retryMatched = this.matchPatternToken(tokens, patternToken, captured);
337
+ this.logger.debug(" >> Backtrack retry result:", retryMatched);
338
+ if (retryMatched) {
339
+ continue;
340
+ }
341
+ }
342
+ return false;
343
+ }
344
+ return true;
345
+ }
346
+ /**
347
+ * Match a single pattern token against the current position in the stream.
348
+ */
349
+ matchPatternToken(tokens, patternToken, captured) {
350
+ switch (patternToken.type) {
351
+ case "literal":
352
+ return this.matchLiteralToken(tokens, patternToken);
353
+ case "role":
354
+ return this.matchRoleToken(tokens, patternToken, captured);
355
+ case "group":
356
+ return this.matchGroupToken(tokens, patternToken, captured);
357
+ default:
358
+ return false;
359
+ }
360
+ }
361
+ /**
362
+ * Match a literal pattern token (keyword or particle).
363
+ */
364
+ matchLiteralToken(tokens, patternToken) {
365
+ const token = tokens.peek();
366
+ this.logger.debug(" >>> matchLiteralToken: expecting", patternToken.value);
367
+ this.logger.debug(
368
+ " >>> matchLiteralToken: got token",
369
+ token ? JSON.stringify({
370
+ type: token.type,
371
+ value: token.value,
372
+ kind: token.kind
373
+ }) : "null"
374
+ );
375
+ if (!token) {
376
+ this.logger.debug(" >>> matchLiteralToken: FAIL - no token");
377
+ return false;
378
+ }
379
+ const matchType = this.getMatchType(token, patternToken.value);
380
+ this.logger.debug(
381
+ " >>> matchType for",
382
+ token.value,
383
+ "vs",
384
+ patternToken.value,
385
+ ":",
386
+ matchType
387
+ );
388
+ if (matchType !== "none") {
389
+ this.totalKeywordMatches++;
390
+ if (matchType === "stem") {
391
+ this.stemMatchCount++;
392
+ }
393
+ tokens.advance();
394
+ return true;
395
+ }
396
+ if (patternToken.alternatives) {
397
+ for (const alt of patternToken.alternatives) {
398
+ const altMatchType = this.getMatchType(token, alt);
399
+ if (altMatchType !== "none") {
400
+ this.totalKeywordMatches++;
401
+ if (altMatchType === "stem") {
402
+ this.stemMatchCount++;
403
+ }
404
+ tokens.advance();
405
+ return true;
406
+ }
407
+ }
408
+ }
409
+ return false;
410
+ }
411
+ /**
412
+ * Match a role pattern token (captures a semantic value).
413
+ * Handles multi-token expressions like:
414
+ * - 'my value' (possessive keyword + property)
415
+ * - '#dialog.showModal()' (method call)
416
+ * - "#element's *opacity" (possessive selector + property)
417
+ */
418
+ matchRoleToken(tokens, patternToken, captured) {
419
+ this.logger.debug(" >>> matchRoleToken ENTRY: capturing role", patternToken.role);
420
+ this.logger.debug(" >>> matchRoleToken: expected types", patternToken.expectedTypes);
421
+ this.logger.debug(" >>> matchRoleToken: optional?", patternToken.optional);
422
+ this.skipNoiseWords(tokens);
423
+ const token = tokens.peek();
424
+ this.logger.debug(
425
+ " >>> After skipNoiseWords, current token:",
426
+ token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
427
+ );
428
+ if (!token) {
429
+ return patternToken.optional || false;
430
+ }
431
+ const possessiveValue = this.tryMatchPossessiveExpression(tokens);
432
+ if (possessiveValue) {
433
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
434
+ if (!patternToken.expectedTypes.includes(possessiveValue.type) && !patternToken.expectedTypes.includes("expression")) {
435
+ return patternToken.optional || false;
436
+ }
437
+ }
438
+ captured.set(patternToken.role, possessiveValue);
439
+ return true;
440
+ }
441
+ const methodCallValue = this.tryMatchMethodCallExpression(tokens);
442
+ if (methodCallValue) {
443
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
444
+ if (!patternToken.expectedTypes.includes(methodCallValue.type) && !patternToken.expectedTypes.includes("expression")) {
445
+ return patternToken.optional || false;
446
+ }
447
+ }
448
+ captured.set(patternToken.role, methodCallValue);
449
+ return true;
450
+ }
451
+ const possessiveSelectorValue = this.tryMatchPossessiveSelectorExpression(tokens);
452
+ if (possessiveSelectorValue) {
453
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
454
+ if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
455
+ return patternToken.optional || false;
456
+ }
457
+ }
458
+ captured.set(patternToken.role, possessiveSelectorValue);
459
+ return true;
460
+ }
461
+ const propertyAccessValue = this.tryMatchPropertyAccessExpression(tokens);
462
+ if (propertyAccessValue) {
463
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
464
+ if (!patternToken.expectedTypes.includes(propertyAccessValue.type) && !patternToken.expectedTypes.includes("expression")) {
465
+ return patternToken.optional || false;
466
+ }
467
+ }
468
+ captured.set(patternToken.role, propertyAccessValue);
469
+ return true;
470
+ }
471
+ const selectorPropertyValue = this.tryMatchSelectorPropertyExpression(tokens);
472
+ if (selectorPropertyValue) {
473
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
474
+ if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
475
+ return patternToken.optional || false;
476
+ }
477
+ }
478
+ captured.set(patternToken.role, selectorPropertyValue);
479
+ return true;
480
+ }
481
+ this.logger.debug(
482
+ " >>> Trying tokenToSemanticValue for token:",
483
+ token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
484
+ );
485
+ const value = this.tokenToSemanticValue(token);
486
+ this.logger.debug(
487
+ " >>> tokenToSemanticValue returned:",
488
+ value ? JSON.stringify(value) : "null"
489
+ );
490
+ if (!value) {
491
+ return patternToken.optional || false;
492
+ }
493
+ this.logger.debug(
494
+ " >>> Validating type:",
495
+ value.type,
496
+ "against expected:",
497
+ patternToken.expectedTypes
498
+ );
499
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
500
+ if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
501
+ this.logger.debug(" >>> TYPE MISMATCH - returning", patternToken.optional || false);
502
+ return patternToken.optional || false;
503
+ }
504
+ }
505
+ this.logger.debug(" >>> Type validation PASSED");
506
+ captured.set(patternToken.role, value);
507
+ tokens.advance();
508
+ return true;
509
+ }
510
+ /**
511
+ * Try to match a possessive expression like 'my value' or 'its innerHTML'.
512
+ * Returns the PropertyPathValue if matched, or null if not.
513
+ */
514
+ tryMatchPossessiveExpression(tokens) {
515
+ const token = tokens.peek();
516
+ if (!token) return null;
517
+ if (!this.currentProfile) return null;
518
+ const tokenValue = token.normalized || token.value;
519
+ const tokenLower = this.safeToLowerCase(tokenValue);
520
+ const baseRef = getPossessiveReference(this.currentProfile, tokenLower);
521
+ if (!baseRef) return null;
522
+ const mark = tokens.mark();
523
+ tokens.advance();
524
+ const propertyToken = tokens.peek();
525
+ if (!propertyToken) {
526
+ tokens.reset(mark);
527
+ return null;
528
+ }
529
+ 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)) {
530
+ tokens.advance();
531
+ let propertyName = propertyToken.value;
532
+ if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
533
+ propertyName = propertyName.substring(1);
534
+ }
535
+ let chainedProps = propertyName;
536
+ while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
537
+ chainedProps += tokens.peek().value;
538
+ tokens.advance();
539
+ }
540
+ const nextPeek = tokens.peek();
541
+ if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
542
+ chainedProps += nextPeek.value;
543
+ tokens.advance();
544
+ }
545
+ return createPropertyPath(createReference(baseRef), chainedProps);
546
+ }
547
+ tokens.reset(mark);
548
+ return null;
549
+ }
550
+ /**
551
+ * Check if a keyword is a structural keyword (preposition, control flow, etc.)
552
+ * that shouldn't be consumed as a property name.
553
+ */
554
+ isStructuralKeyword(value) {
555
+ const structural = /* @__PURE__ */ new Set([
556
+ // Prepositions
557
+ "into",
558
+ "in",
559
+ "to",
560
+ "from",
561
+ "at",
562
+ "by",
563
+ "with",
564
+ "without",
565
+ "before",
566
+ "after",
567
+ "of",
568
+ "as",
569
+ "on",
570
+ // Control flow
571
+ "then",
572
+ "end",
573
+ "else",
574
+ "if",
575
+ "repeat",
576
+ "while",
577
+ "for",
578
+ // Commands (shouldn't be property names)
579
+ "toggle",
580
+ "add",
581
+ "remove",
582
+ "put",
583
+ "set",
584
+ "show",
585
+ "hide",
586
+ "increment",
587
+ "decrement",
588
+ "send",
589
+ "trigger",
590
+ "call"
591
+ ]);
592
+ return structural.has(value.toLowerCase());
593
+ }
594
+ /**
595
+ * Try to match a method call expression like '#dialog.showModal()'.
596
+ * Pattern: selector + '.' + identifier + '(' + [args] + ')'
597
+ * Returns an expression value if matched, or null if not.
598
+ */
599
+ tryMatchMethodCallExpression(tokens) {
600
+ const token = tokens.peek();
601
+ if (!token || token.kind !== "selector") return null;
602
+ const mark = tokens.mark();
603
+ tokens.advance();
604
+ const dotToken = tokens.peek();
605
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
606
+ tokens.reset(mark);
607
+ return null;
608
+ }
609
+ tokens.advance();
610
+ const methodToken = tokens.peek();
611
+ if (!methodToken || methodToken.kind !== "identifier") {
612
+ tokens.reset(mark);
613
+ return null;
614
+ }
615
+ tokens.advance();
616
+ const openParen = tokens.peek();
617
+ if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
618
+ tokens.reset(mark);
619
+ return null;
620
+ }
621
+ tokens.advance();
622
+ const args = [];
623
+ while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
624
+ const argToken = tokens.peek();
625
+ if (!argToken) break;
626
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
627
+ tokens.advance();
628
+ break;
629
+ }
630
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
631
+ tokens.advance();
632
+ continue;
633
+ }
634
+ args.push(argToken.value);
635
+ tokens.advance();
636
+ }
637
+ const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
638
+ return {
639
+ type: "expression",
640
+ raw: methodCall
641
+ };
642
+ }
643
+ /**
644
+ * Try to match a property access expression like 'userData.name' or 'it.data'.
645
+ * Pattern: (identifier | keyword) + '.' + identifier [+ '.' + identifier ...]
646
+ * Returns an expression value if matched, or null if not.
647
+ */
648
+ tryMatchPropertyAccessExpression(tokens) {
649
+ const token = tokens.peek();
650
+ if (!token) return null;
651
+ if (token.kind !== "identifier" && token.kind !== "keyword") return null;
652
+ const mark = tokens.mark();
653
+ tokens.advance();
654
+ const dotToken = tokens.peek();
655
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
656
+ tokens.reset(mark);
657
+ return null;
658
+ }
659
+ tokens.advance();
660
+ const propertyToken = tokens.peek();
661
+ if (!propertyToken || propertyToken.kind !== "identifier") {
662
+ tokens.reset(mark);
663
+ return null;
664
+ }
665
+ tokens.advance();
666
+ let chain = `${token.value}.${propertyToken.value}`;
667
+ let depth = 1;
668
+ while (!tokens.isAtEnd() && depth < _PatternMatcher.MAX_PROPERTY_DEPTH) {
669
+ const nextDot = tokens.peek();
670
+ if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
671
+ break;
672
+ }
673
+ tokens.advance();
674
+ const nextProp = tokens.peek();
675
+ if (!nextProp || nextProp.kind !== "identifier") {
676
+ break;
677
+ }
678
+ tokens.advance();
679
+ chain += `.${nextProp.value}`;
680
+ depth++;
681
+ }
682
+ const openParen = tokens.peek();
683
+ if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
684
+ tokens.advance();
685
+ const args = [];
686
+ let argDepth = 0;
687
+ while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
688
+ const argToken = tokens.peek();
689
+ if (!argToken) break;
690
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
691
+ if (argDepth === 0) {
692
+ tokens.advance();
693
+ break;
694
+ }
695
+ argDepth--;
696
+ }
697
+ if (argToken.kind === "punctuation" && argToken.value === "(") {
698
+ argDepth++;
699
+ }
700
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
701
+ tokens.advance();
702
+ continue;
703
+ }
704
+ args.push(argToken.value);
705
+ tokens.advance();
706
+ }
707
+ const methodCall = `${chain}(${args.join(", ")})`;
708
+ return {
709
+ type: "expression",
710
+ raw: methodCall
711
+ };
712
+ }
713
+ return {
714
+ type: "expression",
715
+ raw: chain
716
+ };
717
+ }
718
+ /**
719
+ * Try to match a possessive selector expression like "#element's *opacity".
720
+ * Pattern: selector + "'s" + (selector | identifier)
721
+ * Returns a property-path value if matched, or null if not.
722
+ */
723
+ tryMatchPossessiveSelectorExpression(tokens) {
724
+ const token = tokens.peek();
725
+ if (!token || token.kind !== "selector") return null;
726
+ const mark = tokens.mark();
727
+ tokens.advance();
728
+ const possessiveToken = tokens.peek();
729
+ if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
730
+ tokens.reset(mark);
731
+ return null;
732
+ }
733
+ tokens.advance();
734
+ const propertyToken = tokens.peek();
735
+ if (!propertyToken) {
736
+ tokens.reset(mark);
737
+ return null;
738
+ }
739
+ if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
740
+ tokens.reset(mark);
741
+ return null;
742
+ }
743
+ tokens.advance();
744
+ return createPropertyPath(createSelector(token.value), propertyToken.value);
745
+ }
746
+ /**
747
+ * Try to match a selector + property expression like "#output.innerText".
748
+ * This handles cases where the tokenizer produces two selector tokens:
749
+ * - #output (id selector)
750
+ * - .innerText (looks like class selector, but is actually property)
751
+ *
752
+ * Pattern: id-selector + class-selector-that-is-actually-property
753
+ * Returns a property-path value if matched, or null if not.
754
+ */
755
+ tryMatchSelectorPropertyExpression(tokens) {
756
+ const token = tokens.peek();
757
+ if (!token || token.kind !== "selector") return null;
758
+ if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
759
+ const mark = tokens.mark();
760
+ tokens.advance();
761
+ const propertyToken = tokens.peek();
762
+ if (!propertyToken || propertyToken.kind !== "selector") {
763
+ tokens.reset(mark);
764
+ return null;
765
+ }
766
+ if (!propertyToken.value.startsWith(".")) {
767
+ tokens.reset(mark);
768
+ return null;
769
+ }
770
+ const peek2 = tokens.peek(1);
771
+ if (peek2 && peek2.kind === "selector") {
772
+ }
773
+ tokens.advance();
774
+ const propertyName = propertyToken.value.slice(1);
775
+ return createPropertyPath(createSelector(token.value), propertyName);
776
+ }
777
+ /**
778
+ * Match a group pattern token (optional sequence).
779
+ * When the group's leading marker isn't at the current position, scans ahead
780
+ * up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
781
+ * (e.g., a value like 'hello') to sit between groups without blocking later
782
+ * marker matches.
783
+ */
784
+ matchGroupToken(tokens, patternToken, captured) {
785
+ const mark = tokens.mark();
786
+ const capturedBefore = new Set(captured.keys());
787
+ const success = this.matchTokenSequence(tokens, patternToken.tokens, captured);
788
+ if (success) return true;
789
+ tokens.reset(mark);
790
+ for (const role of captured.keys()) {
791
+ if (!capturedBefore.has(role)) captured.delete(role);
792
+ }
793
+ if (!patternToken.optional) return false;
794
+ const leadingMarker = this.getGroupLeadingMarker(patternToken);
795
+ if (leadingMarker) {
796
+ for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
797
+ const ahead = tokens.peek(offset);
798
+ if (!ahead) break;
799
+ const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
800
+ if (aheadValue === leadingMarker) {
801
+ this.logger.debug(
802
+ " >> MARKER SCAN: found",
803
+ leadingMarker,
804
+ "at offset",
805
+ offset,
806
+ "- skipping intervening tokens"
807
+ );
808
+ for (let s = 0; s < offset; s++) tokens.advance();
809
+ const retrySuccess = this.matchTokenSequence(tokens, patternToken.tokens, captured);
810
+ if (retrySuccess) return true;
811
+ tokens.reset(mark);
812
+ for (const role of captured.keys()) {
813
+ if (!capturedBefore.has(role)) captured.delete(role);
814
+ }
815
+ break;
816
+ }
817
+ }
818
+ }
819
+ return true;
820
+ }
821
+ /**
822
+ * Get the leading marker literal from an optional group.
823
+ * Returns the lowercase marker value, or null if the group
824
+ * doesn't start with a literal token (e.g., SOV groups where
825
+ * the role comes before the marker).
826
+ */
827
+ getGroupLeadingMarker(group) {
828
+ const first = group.tokens[0];
829
+ if (first?.type === "literal") return first.value.toLowerCase();
830
+ return null;
831
+ }
832
+ /**
833
+ * Get the type of match for a token against a value.
834
+ * Used for confidence calculation.
835
+ */
836
+ getMatchType(token, value) {
837
+ if (token.value === value) return "exact";
838
+ if (token.normalized === value) return "normalized";
839
+ if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
840
+ return "stem";
841
+ }
842
+ if (token.kind === "keyword" && this.safeToLowerCase(token.value) === value.toLowerCase()) {
843
+ return "case-insensitive";
844
+ }
845
+ return "none";
846
+ }
847
+ /**
848
+ * Collect literal values from upcoming pattern tokens that act as stop markers
849
+ * for greedy role capture. Returns the set of lowercase values.
850
+ */
851
+ collectStopMarkers(patternTokens, startIndex) {
852
+ const markers = /* @__PURE__ */ new Set();
853
+ for (let j = startIndex; j < patternTokens.length; j++) {
854
+ const pt = patternTokens[j];
855
+ if (pt.type === "literal") {
856
+ markers.add(pt.value.toLowerCase());
857
+ if (pt.alternatives) {
858
+ for (const alt of pt.alternatives) {
859
+ markers.add(alt.toLowerCase());
860
+ }
861
+ }
862
+ break;
863
+ }
864
+ if (pt.type === "group") {
865
+ for (const gt of pt.tokens) {
866
+ if (gt.type === "literal") {
867
+ markers.add(gt.value.toLowerCase());
868
+ if (gt.alternatives) {
869
+ for (const alt of gt.alternatives) {
870
+ markers.add(alt.toLowerCase());
871
+ }
872
+ }
873
+ break;
874
+ }
875
+ }
876
+ break;
877
+ }
878
+ }
879
+ return markers;
880
+ }
881
+ /**
882
+ * Check if a token matches any stop marker for greedy capture.
883
+ */
884
+ isStopMarker(token, stopMarkers) {
885
+ if (stopMarkers.size === 0) return false;
886
+ const value = (token.normalized || token.value).toLowerCase();
887
+ return stopMarkers.has(value);
888
+ }
889
+ /**
890
+ * Convert a language token to a semantic value.
891
+ */
892
+ tokenToSemanticValue(token) {
893
+ switch (token.kind) {
894
+ case "selector":
895
+ return createSelector(token.value);
896
+ case "literal":
897
+ return this.parseLiteralValue(token.value);
898
+ case "keyword":
899
+ const tokenValue = token.normalized || token.value;
900
+ const lower = this.safeToLowerCase(tokenValue);
901
+ if (isValidReference(lower)) {
902
+ return createReference(lower);
903
+ }
904
+ return createLiteral(token.normalized || token.value);
905
+ case "identifier":
906
+ if (typeof token.value === "string" && token.value.startsWith(":")) {
907
+ return createReference(token.value);
908
+ }
909
+ const identLower = this.safeToLowerCase(token.value);
910
+ if (isValidReference(identLower)) {
911
+ return createReference(identLower);
912
+ }
913
+ return { type: "expression", raw: token.value };
914
+ case "url":
915
+ return createLiteral(token.value, "string");
916
+ default:
917
+ return null;
918
+ }
919
+ }
920
+ /**
921
+ * Parse a literal value (string, number, boolean).
922
+ */
923
+ parseLiteralValue(value) {
924
+ if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
925
+ const inner = value.slice(1, -1);
926
+ return createLiteral(inner, "string");
927
+ }
928
+ if (value === "true") return createLiteral(true, "boolean");
929
+ if (value === "false") return createLiteral(false, "boolean");
930
+ const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
931
+ if (durationMatch) {
932
+ const num2 = parseFloat(durationMatch[1]);
933
+ const unit = durationMatch[2];
934
+ if (unit) {
935
+ return createLiteral(value, "duration");
936
+ }
937
+ return createLiteral(num2, "number");
938
+ }
939
+ const num = parseFloat(value);
940
+ if (!isNaN(num)) {
941
+ return createLiteral(num, "number");
942
+ }
943
+ return createLiteral(value, "string");
944
+ }
945
+ /**
946
+ * Apply extraction rules to fill in static values and defaults for missing roles.
947
+ */
948
+ applyExtractionRules(pattern, captured) {
949
+ for (const [role, rule] of Object.entries(pattern.extraction)) {
950
+ if (!captured.has(role)) {
951
+ if (rule.value !== void 0) {
952
+ captured.set(role, { type: "literal", value: rule.value });
953
+ } else if (rule.default) {
954
+ captured.set(role, rule.default);
955
+ }
956
+ }
957
+ }
958
+ }
959
+ /**
960
+ * Check if a pattern token is optional.
961
+ */
962
+ isOptional(patternToken) {
963
+ return patternToken.type !== "literal" && patternToken.optional === true;
964
+ }
965
+ /**
966
+ * Calculate confidence score for a match (0-1).
967
+ *
968
+ * Confidence is reduced for:
969
+ * - Stem matches (morphological normalization has inherent uncertainty)
970
+ * - Missing optional roles (but less penalty if role has a default value)
971
+ *
972
+ * Confidence is increased for:
973
+ * - VSO languages (Arabic) when pattern starts with a verb
974
+ */
975
+ calculateConfidence(pattern, captured) {
976
+ let score = 0;
977
+ let maxScore = 0;
978
+ const hasDefault = (role) => {
979
+ return pattern.extraction?.[role]?.default !== void 0;
980
+ };
981
+ for (const token of pattern.template.tokens) {
982
+ if (token.type === "role") {
983
+ maxScore += 1;
984
+ if (captured.has(token.role)) {
985
+ score += 1;
986
+ }
987
+ } else if (token.type === "group") {
988
+ for (const subToken of token.tokens) {
989
+ if (subToken.type === "role") {
990
+ const roleHasDefault = hasDefault(subToken.role);
991
+ const weight = 0.8;
992
+ maxScore += weight;
993
+ if (captured.has(subToken.role)) {
994
+ score += weight;
995
+ } else if (roleHasDefault) {
996
+ score += weight * 0.6;
997
+ }
998
+ }
999
+ }
1000
+ }
1001
+ }
1002
+ let baseConfidence = maxScore > 0 ? score / maxScore : 1;
1003
+ if (this.stemMatchCount > 0 && this.totalKeywordMatches > 0) {
1004
+ const stemPenalty = this.stemMatchCount / this.totalKeywordMatches * 0.15;
1005
+ baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
1006
+ }
1007
+ const vsoBoost = this.calculateVSOConfidenceBoost(pattern);
1008
+ baseConfidence = Math.min(1, baseConfidence + vsoBoost);
1009
+ const prepositionAdjustment = this.arabicPrepositionDisambiguation(pattern, captured);
1010
+ baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
1011
+ return baseConfidence;
1012
+ }
1013
+ /**
1014
+ * Calculate confidence boost for VSO (Verb-Subject-Object) language patterns.
1015
+ * Arabic naturally uses VSO word order, so patterns that start with a verb
1016
+ * should receive a confidence boost.
1017
+ *
1018
+ * Returns +0.15 confidence boost if:
1019
+ * - Language is Arabic ('ar')
1020
+ * - Pattern's first token is a verb keyword
1021
+ *
1022
+ * @param pattern The language pattern being matched
1023
+ * @returns Confidence boost (0 or 0.15)
1024
+ */
1025
+ calculateVSOConfidenceBoost(pattern) {
1026
+ if (pattern.language !== "ar") {
1027
+ return 0;
1028
+ }
1029
+ const firstToken = pattern.template.tokens[0];
1030
+ if (!firstToken || firstToken.type !== "literal") {
1031
+ return 0;
1032
+ }
1033
+ const ARABIC_VERBS = /* @__PURE__ */ new Set([
1034
+ "\u0628\u062F\u0644",
1035
+ "\u063A\u064A\u0631",
1036
+ "\u0623\u0636\u0641",
1037
+ "\u0623\u0632\u0644",
1038
+ "\u0636\u0639",
1039
+ "\u0627\u062C\u0639\u0644",
1040
+ "\u0639\u064A\u0646",
1041
+ "\u0632\u062F",
1042
+ "\u0627\u0646\u0642\u0635",
1043
+ "\u0633\u062C\u0644",
1044
+ "\u0623\u0638\u0647\u0631",
1045
+ "\u0623\u062E\u0641",
1046
+ "\u0634\u063A\u0644",
1047
+ "\u0623\u0631\u0633\u0644",
1048
+ "\u0631\u0643\u0632",
1049
+ "\u0634\u0648\u0634",
1050
+ "\u062A\u0648\u0642\u0641",
1051
+ "\u0627\u0646\u0633\u062E",
1052
+ "\u0627\u062D\u0630\u0641",
1053
+ "\u0627\u0635\u0646\u0639",
1054
+ "\u0627\u0646\u062A\u0638\u0631",
1055
+ "\u0627\u0646\u062A\u0642\u0627\u0644",
1056
+ "\u0623\u0648"
1057
+ ]);
1058
+ if (ARABIC_VERBS.has(firstToken.value)) {
1059
+ return 0.15;
1060
+ }
1061
+ if (firstToken.alternatives) {
1062
+ for (const alt of firstToken.alternatives) {
1063
+ if (ARABIC_VERBS.has(alt)) {
1064
+ return 0.15;
1065
+ }
1066
+ }
1067
+ }
1068
+ return 0;
1069
+ }
1070
+ /**
1071
+ * Arabic preposition disambiguation for confidence adjustment.
1072
+ *
1073
+ * Different Arabic prepositions are more or less natural for different semantic roles:
1074
+ * - على (on/upon) is preferred for patient/target roles (element selectors)
1075
+ * - إلى (to) is preferred for destination roles
1076
+ * - من (from) is preferred for source roles
1077
+ * - في (in) is preferred for location roles
1078
+ *
1079
+ * This method analyzes the prepositions used with captured semantic roles and
1080
+ * adjusts confidence based on idiomaticity:
1081
+ * - +0.10 for highly idiomatic preposition choices
1082
+ * - -0.10 for less natural preposition choices
1083
+ *
1084
+ * @param pattern The language pattern being matched
1085
+ * @param captured The captured semantic values
1086
+ * @returns Confidence adjustment (-0.10 to +0.10)
1087
+ */
1088
+ arabicPrepositionDisambiguation(pattern, captured) {
1089
+ if (pattern.language !== "ar") {
1090
+ return 0;
1091
+ }
1092
+ let adjustment = 0;
1093
+ const PREFERRED_PREPOSITIONS = {
1094
+ patient: ["\u0639\u0644\u0649"],
1095
+ // element selectors prefer على (on/upon)
1096
+ destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1097
+ // destination prefers إلى (to)
1098
+ source: ["\u0645\u0646"],
1099
+ // source prefers من (from)
1100
+ agent: ["\u0645\u0646"],
1101
+ // agent/by prefers من (from/by)
1102
+ manner: ["\u0628"],
1103
+ // manner prefers ب (with/by)
1104
+ style: ["\u0628"],
1105
+ // style prefers ب (with)
1106
+ goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1107
+ // target state prefers إلى (to)
1108
+ method: ["\u0628"]
1109
+ // method prefers ب (with/by)
1110
+ };
1111
+ for (const [role, value] of captured.entries()) {
1112
+ const preferred = PREFERRED_PREPOSITIONS[role];
1113
+ if (!preferred || preferred.length === 0) {
1114
+ continue;
1115
+ }
1116
+ const metadata = "metadata" in value ? value.metadata : void 0;
1117
+ if (metadata && typeof metadata.prepositionValue === "string") {
1118
+ const usedPreposition = metadata.prepositionValue;
1119
+ if (preferred.includes(usedPreposition)) {
1120
+ adjustment += 0.1;
1121
+ } else {
1122
+ adjustment -= 0.1;
1123
+ }
1124
+ }
1125
+ }
1126
+ return Math.max(-0.1, Math.min(0.1, adjustment));
1127
+ }
1128
+ /**
1129
+ * Skip noise words like "the" before selectors.
1130
+ * This enables more natural English syntax like "toggle the .active".
1131
+ */
1132
+ skipNoiseWords(tokens) {
1133
+ const token = tokens.peek();
1134
+ if (!token) return;
1135
+ const tokenLower = this.safeToLowerCase(token.value);
1136
+ if (_PatternMatcher.ENGLISH_NOISE_WORDS.has(tokenLower)) {
1137
+ const mark = tokens.mark();
1138
+ tokens.advance();
1139
+ const nextToken = tokens.peek();
1140
+ if (nextToken && nextToken.kind === "selector") {
1141
+ return;
1142
+ }
1143
+ tokens.reset(mark);
1144
+ }
1145
+ if (tokenLower === "class") {
1146
+ tokens.advance();
1147
+ }
1148
+ }
1149
+ /**
1150
+ * Extract event modifiers from the token stream.
1151
+ * Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
1152
+ * that can appear after event names.
1153
+ *
1154
+ * Returns EventModifiers object or undefined if no modifiers found.
1155
+ */
1156
+ extractEventModifiers(tokens) {
1157
+ const modifiers = {};
1158
+ let foundModifier = false;
1159
+ while (!tokens.isAtEnd()) {
1160
+ const token = tokens.peek();
1161
+ if (!token || token.kind !== "event-modifier") {
1162
+ break;
1163
+ }
1164
+ const metadata = token.metadata;
1165
+ if (!metadata) {
1166
+ break;
1167
+ }
1168
+ foundModifier = true;
1169
+ switch (metadata.modifierName) {
1170
+ case "once":
1171
+ modifiers.once = true;
1172
+ break;
1173
+ case "debounce":
1174
+ if (typeof metadata.value === "number") {
1175
+ modifiers.debounce = metadata.value;
1176
+ }
1177
+ break;
1178
+ case "throttle":
1179
+ if (typeof metadata.value === "number") {
1180
+ modifiers.throttle = metadata.value;
1181
+ }
1182
+ break;
1183
+ case "queue":
1184
+ if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
1185
+ modifiers.queue = metadata.value;
1186
+ }
1187
+ break;
1188
+ }
1189
+ tokens.advance();
1190
+ }
1191
+ return foundModifier ? modifiers : void 0;
1192
+ }
1193
+ };
1194
+ /** Maximum tokens to scan ahead when looking for a group's leading marker */
1195
+ _PatternMatcher.MAX_MARKER_SCAN = 3;
1196
+ // ==========================================================================
1197
+ // Depth Limits for Expression Parsing (security hardening)
1198
+ // ==========================================================================
1199
+ /** Maximum depth for nested property access (e.g., a.b.c.d...) */
1200
+ _PatternMatcher.MAX_PROPERTY_DEPTH = 10;
1201
+ /** Maximum number of arguments in method calls */
1202
+ _PatternMatcher.MAX_METHOD_ARGS = 20;
1203
+ // ===========================================================================
1204
+ // English Idiom Support - Noise Word Handling
1205
+ // ===========================================================================
1206
+ /**
1207
+ * Noise words that can be skipped in English for more natural syntax.
1208
+ * - "the" before selectors: "toggle the .active" → "toggle .active"
1209
+ * - "class" after class selectors: "add the .visible class" → "add .visible"
1210
+ */
1211
+ _PatternMatcher.ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
1212
+ var PatternMatcher = _PatternMatcher;
1213
+ var patternMatcher = new PatternMatcher();
1214
+ function matchPattern(tokens, pattern) {
1215
+ return patternMatcher.matchPattern(tokens, pattern);
1216
+ }
1217
+ function matchBest(tokens, patterns) {
1218
+ return patternMatcher.matchBest(tokens, patterns);
1219
+ }
1220
+ export {
1221
+ PatternMatcher,
1222
+ getAllPossessiveKeywords,
1223
+ getPossessiveReference,
1224
+ isBuiltInReference,
1225
+ isCSSPropertyRef,
1226
+ isCSSSelector,
1227
+ isClassName,
1228
+ isIdSelector,
1229
+ isNumericValue,
1230
+ isPossessiveKeyword,
1231
+ isPropertyName,
1232
+ isTypeCompatible,
1233
+ isVariableRef,
1234
+ matchBest,
1235
+ matchPattern,
1236
+ patternMatcher,
1237
+ validateValueType
1238
+ };
1239
+ //# sourceMappingURL=index.js.map