@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,2082 @@
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
+
99
+ // src/core/pattern-matching/utils/possessive-keywords.ts
100
+ function getPossessiveReference(profile, keyword) {
101
+ return profile?.possessive?.keywords?.[keyword];
102
+ }
103
+
104
+ // src/core/pattern-matching/pattern-matcher.ts
105
+ function isValidReference(_value) {
106
+ return false;
107
+ }
108
+ var _PatternMatcher = class _PatternMatcher {
109
+ constructor() {
110
+ /** Debug logger */
111
+ this.logger = createLogger("pattern-matcher");
112
+ /**
113
+ * Track stem matches for confidence calculation.
114
+ * This is set during matching and read during confidence calculation.
115
+ */
116
+ this.stemMatchCount = 0;
117
+ this.totalKeywordMatches = 0;
118
+ }
119
+ /**
120
+ * Safely convert a value to lowercase string.
121
+ * Provides protection against non-string values at runtime.
122
+ */
123
+ safeToLowerCase(value) {
124
+ if (typeof value === "string") {
125
+ return value.toLowerCase();
126
+ }
127
+ if (value === null || value === void 0) {
128
+ return "";
129
+ }
130
+ return String(value).toLowerCase();
131
+ }
132
+ /**
133
+ * Try to match a single pattern against the token stream.
134
+ * Returns the match result or null if no match.
135
+ *
136
+ * @param tokens - Token stream to match against
137
+ * @param pattern - Pattern to match
138
+ * @param profile - Optional language profile for possessive handling
139
+ */
140
+ matchPattern(tokens, pattern, profile) {
141
+ const mark = tokens.mark();
142
+ const captured = /* @__PURE__ */ new Map();
143
+ this.logger.debug("========================================");
144
+ this.logger.debug("matchPattern ENTRY");
145
+ this.logger.debug("Pattern ID:", pattern.id);
146
+ this.logger.debug("Pattern command:", pattern.command);
147
+ this.logger.debug("Pattern language:", pattern.language);
148
+ this.logger.debug("Pattern template:", JSON.stringify(pattern.template, null, 2));
149
+ if (this.logger.isEnabled()) {
150
+ const firstTokens = [];
151
+ for (let i = 0; i < 10; i++) {
152
+ const t = tokens.peek(i);
153
+ if (t)
154
+ firstTokens.push({
155
+ type: t.type,
156
+ value: t.value,
157
+ kind: t.kind
158
+ });
159
+ else break;
160
+ }
161
+ this.logger.debug("Input tokens (first 10):", firstTokens);
162
+ this.logger.debug("Profile code:", profile?.code);
163
+ }
164
+ this.currentProfile = profile;
165
+ this.stemMatchCount = 0;
166
+ this.totalKeywordMatches = 0;
167
+ this.logger.debug("--- Calling matchTokenSequence ---");
168
+ this.logger.debug("Pattern tokens to match:", JSON.stringify(pattern.template.tokens, null, 2));
169
+ const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
170
+ this.logger.debug("matchTokenSequence returned:", success);
171
+ this.logger.debug(
172
+ "Captured roles:",
173
+ Array.from(captured.entries()).map(([k, v]) => [k, JSON.stringify(v)])
174
+ );
175
+ if (!success) {
176
+ this.logger.debug(">>> MATCH FAILED - resetting token position");
177
+ tokens.reset(mark);
178
+ return null;
179
+ }
180
+ const confidence = this.calculateConfidence(pattern, captured);
181
+ this.applyExtractionRules(pattern, captured);
182
+ return {
183
+ pattern,
184
+ captured,
185
+ consumedTokens: tokens.position() - mark.position,
186
+ confidence
187
+ };
188
+ }
189
+ /**
190
+ * Try to match multiple patterns, return the best match.
191
+ *
192
+ * @param tokens - Token stream to match against
193
+ * @param patterns - Candidate patterns to try
194
+ * @param profile - Optional language profile for possessive handling
195
+ */
196
+ matchBest(tokens, patterns, profile) {
197
+ const matches = [];
198
+ for (const pattern of patterns) {
199
+ const mark = tokens.mark();
200
+ const result = this.matchPattern(tokens, pattern, profile);
201
+ if (result) {
202
+ matches.push(result);
203
+ }
204
+ tokens.reset(mark);
205
+ }
206
+ if (matches.length === 0) {
207
+ return null;
208
+ }
209
+ matches.sort((a, b) => {
210
+ const priorityDiff = b.pattern.priority - a.pattern.priority;
211
+ if (priorityDiff !== 0) return priorityDiff;
212
+ const confidenceDiff = b.confidence - a.confidence;
213
+ if (Math.abs(confidenceDiff) > 1e-3) return confidenceDiff;
214
+ return b.consumedTokens - a.consumedTokens;
215
+ });
216
+ const best = matches[0];
217
+ this.matchPattern(tokens, best.pattern);
218
+ return best;
219
+ }
220
+ /**
221
+ * Match a sequence of pattern tokens against the token stream.
222
+ *
223
+ * Supports bounded single-step backtracking: if an optional role consumes
224
+ * a token and the immediately following pattern token fails, the matcher
225
+ * resets to before the optional role and retries the failed token.
226
+ */
227
+ matchTokenSequence(tokens, patternTokens, captured) {
228
+ const firstPatternToken = patternTokens[0];
229
+ const patternExpectsConjunction = firstPatternToken?.type === "literal" && (firstPatternToken.value === "and" || firstPatternToken.value === "then" || firstPatternToken.alternatives?.includes("and") || firstPatternToken.alternatives?.includes("then"));
230
+ if (this.currentProfile?.code === "ar" && !patternExpectsConjunction) {
231
+ while (tokens.peek()?.kind === "conjunction") {
232
+ tokens.advance();
233
+ }
234
+ }
235
+ let prevOptionalMark = null;
236
+ let prevOptionalRole = null;
237
+ for (let i = 0; i < patternTokens.length; i++) {
238
+ const patternToken = patternTokens[i];
239
+ this.logger.debug(" >> Matching pattern token:", JSON.stringify(patternToken, null, 2));
240
+ const currTok = tokens.peek();
241
+ this.logger.debug(
242
+ " >> Current input token:",
243
+ currTok ? JSON.stringify({
244
+ type: currTok.type,
245
+ value: currTok.value,
246
+ kind: currTok.kind
247
+ }) : "EOF"
248
+ );
249
+ if (patternToken.type === "role" && patternToken.greedy) {
250
+ const stopMarkers = this.collectStopMarkers(patternTokens, i + 1);
251
+ const values = [];
252
+ while (!tokens.isAtEnd()) {
253
+ const nextToken = tokens.peek();
254
+ if (!nextToken) break;
255
+ if (this.isStopMarker(nextToken, stopMarkers)) break;
256
+ values.push(nextToken.value);
257
+ tokens.advance();
258
+ }
259
+ if (values.length > 0) {
260
+ captured.set(patternToken.role, { type: "expression", raw: values.join(" ") });
261
+ prevOptionalMark = null;
262
+ prevOptionalRole = null;
263
+ continue;
264
+ } else if (patternToken.optional) {
265
+ continue;
266
+ } else {
267
+ return false;
268
+ }
269
+ }
270
+ const isOptionalRole = patternToken.type === "role" && patternToken.optional === true;
271
+ const markBefore = isOptionalRole ? tokens.mark() : null;
272
+ const matched = this.matchPatternToken(tokens, patternToken, captured);
273
+ this.logger.debug(" >> Match result:", matched);
274
+ if (matched) {
275
+ if (isOptionalRole) {
276
+ prevOptionalMark = markBefore;
277
+ prevOptionalRole = patternToken.role;
278
+ } else {
279
+ prevOptionalMark = null;
280
+ prevOptionalRole = null;
281
+ }
282
+ continue;
283
+ }
284
+ this.logger.debug(" >> Token match FAILED");
285
+ if (this.isOptional(patternToken)) {
286
+ continue;
287
+ }
288
+ if (prevOptionalMark && prevOptionalRole) {
289
+ this.logger.debug(" >> BACKTRACKING: undoing optional role", prevOptionalRole);
290
+ tokens.reset(prevOptionalMark);
291
+ captured.delete(prevOptionalRole);
292
+ prevOptionalMark = null;
293
+ prevOptionalRole = null;
294
+ const retryMatched = this.matchPatternToken(tokens, patternToken, captured);
295
+ this.logger.debug(" >> Backtrack retry result:", retryMatched);
296
+ if (retryMatched) {
297
+ continue;
298
+ }
299
+ }
300
+ return false;
301
+ }
302
+ return true;
303
+ }
304
+ /**
305
+ * Match a single pattern token against the current position in the stream.
306
+ */
307
+ matchPatternToken(tokens, patternToken, captured) {
308
+ switch (patternToken.type) {
309
+ case "literal":
310
+ return this.matchLiteralToken(tokens, patternToken);
311
+ case "role":
312
+ return this.matchRoleToken(tokens, patternToken, captured);
313
+ case "group":
314
+ return this.matchGroupToken(tokens, patternToken, captured);
315
+ default:
316
+ return false;
317
+ }
318
+ }
319
+ /**
320
+ * Match a literal pattern token (keyword or particle).
321
+ */
322
+ matchLiteralToken(tokens, patternToken) {
323
+ const token = tokens.peek();
324
+ this.logger.debug(" >>> matchLiteralToken: expecting", patternToken.value);
325
+ this.logger.debug(
326
+ " >>> matchLiteralToken: got token",
327
+ token ? JSON.stringify({
328
+ type: token.type,
329
+ value: token.value,
330
+ kind: token.kind
331
+ }) : "null"
332
+ );
333
+ if (!token) {
334
+ this.logger.debug(" >>> matchLiteralToken: FAIL - no token");
335
+ return false;
336
+ }
337
+ const matchType = this.getMatchType(token, patternToken.value);
338
+ this.logger.debug(
339
+ " >>> matchType for",
340
+ token.value,
341
+ "vs",
342
+ patternToken.value,
343
+ ":",
344
+ matchType
345
+ );
346
+ if (matchType !== "none") {
347
+ this.totalKeywordMatches++;
348
+ if (matchType === "stem") {
349
+ this.stemMatchCount++;
350
+ }
351
+ tokens.advance();
352
+ return true;
353
+ }
354
+ if (patternToken.alternatives) {
355
+ for (const alt of patternToken.alternatives) {
356
+ const altMatchType = this.getMatchType(token, alt);
357
+ if (altMatchType !== "none") {
358
+ this.totalKeywordMatches++;
359
+ if (altMatchType === "stem") {
360
+ this.stemMatchCount++;
361
+ }
362
+ tokens.advance();
363
+ return true;
364
+ }
365
+ }
366
+ }
367
+ return false;
368
+ }
369
+ /**
370
+ * Match a role pattern token (captures a semantic value).
371
+ * Handles multi-token expressions like:
372
+ * - 'my value' (possessive keyword + property)
373
+ * - '#dialog.showModal()' (method call)
374
+ * - "#element's *opacity" (possessive selector + property)
375
+ */
376
+ matchRoleToken(tokens, patternToken, captured) {
377
+ this.logger.debug(" >>> matchRoleToken ENTRY: capturing role", patternToken.role);
378
+ this.logger.debug(" >>> matchRoleToken: expected types", patternToken.expectedTypes);
379
+ this.logger.debug(" >>> matchRoleToken: optional?", patternToken.optional);
380
+ this.skipNoiseWords(tokens);
381
+ const token = tokens.peek();
382
+ this.logger.debug(
383
+ " >>> After skipNoiseWords, current token:",
384
+ token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
385
+ );
386
+ if (!token) {
387
+ return patternToken.optional || false;
388
+ }
389
+ const possessiveValue = this.tryMatchPossessiveExpression(tokens);
390
+ if (possessiveValue) {
391
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
392
+ if (!patternToken.expectedTypes.includes(possessiveValue.type) && !patternToken.expectedTypes.includes("expression")) {
393
+ return patternToken.optional || false;
394
+ }
395
+ }
396
+ captured.set(patternToken.role, possessiveValue);
397
+ return true;
398
+ }
399
+ const methodCallValue = this.tryMatchMethodCallExpression(tokens);
400
+ if (methodCallValue) {
401
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
402
+ if (!patternToken.expectedTypes.includes(methodCallValue.type) && !patternToken.expectedTypes.includes("expression")) {
403
+ return patternToken.optional || false;
404
+ }
405
+ }
406
+ captured.set(patternToken.role, methodCallValue);
407
+ return true;
408
+ }
409
+ const possessiveSelectorValue = this.tryMatchPossessiveSelectorExpression(tokens);
410
+ if (possessiveSelectorValue) {
411
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
412
+ if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
413
+ return patternToken.optional || false;
414
+ }
415
+ }
416
+ captured.set(patternToken.role, possessiveSelectorValue);
417
+ return true;
418
+ }
419
+ const propertyAccessValue = this.tryMatchPropertyAccessExpression(tokens);
420
+ if (propertyAccessValue) {
421
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
422
+ if (!patternToken.expectedTypes.includes(propertyAccessValue.type) && !patternToken.expectedTypes.includes("expression")) {
423
+ return patternToken.optional || false;
424
+ }
425
+ }
426
+ captured.set(patternToken.role, propertyAccessValue);
427
+ return true;
428
+ }
429
+ const selectorPropertyValue = this.tryMatchSelectorPropertyExpression(tokens);
430
+ if (selectorPropertyValue) {
431
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
432
+ if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
433
+ return patternToken.optional || false;
434
+ }
435
+ }
436
+ captured.set(patternToken.role, selectorPropertyValue);
437
+ return true;
438
+ }
439
+ this.logger.debug(
440
+ " >>> Trying tokenToSemanticValue for token:",
441
+ token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
442
+ );
443
+ const value = this.tokenToSemanticValue(token);
444
+ this.logger.debug(
445
+ " >>> tokenToSemanticValue returned:",
446
+ value ? JSON.stringify(value) : "null"
447
+ );
448
+ if (!value) {
449
+ return patternToken.optional || false;
450
+ }
451
+ this.logger.debug(
452
+ " >>> Validating type:",
453
+ value.type,
454
+ "against expected:",
455
+ patternToken.expectedTypes
456
+ );
457
+ if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
458
+ if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
459
+ this.logger.debug(" >>> TYPE MISMATCH - returning", patternToken.optional || false);
460
+ return patternToken.optional || false;
461
+ }
462
+ }
463
+ this.logger.debug(" >>> Type validation PASSED");
464
+ captured.set(patternToken.role, value);
465
+ tokens.advance();
466
+ return true;
467
+ }
468
+ /**
469
+ * Try to match a possessive expression like 'my value' or 'its innerHTML'.
470
+ * Returns the PropertyPathValue if matched, or null if not.
471
+ */
472
+ tryMatchPossessiveExpression(tokens) {
473
+ const token = tokens.peek();
474
+ if (!token) return null;
475
+ if (!this.currentProfile) return null;
476
+ const tokenValue = token.normalized || token.value;
477
+ const tokenLower = this.safeToLowerCase(tokenValue);
478
+ const baseRef = getPossessiveReference(this.currentProfile, tokenLower);
479
+ if (!baseRef) return null;
480
+ const mark = tokens.mark();
481
+ tokens.advance();
482
+ const propertyToken = tokens.peek();
483
+ if (!propertyToken) {
484
+ tokens.reset(mark);
485
+ return null;
486
+ }
487
+ 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)) {
488
+ tokens.advance();
489
+ let propertyName = propertyToken.value;
490
+ if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
491
+ propertyName = propertyName.substring(1);
492
+ }
493
+ let chainedProps = propertyName;
494
+ while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
495
+ chainedProps += tokens.peek().value;
496
+ tokens.advance();
497
+ }
498
+ const nextPeek = tokens.peek();
499
+ if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
500
+ chainedProps += nextPeek.value;
501
+ tokens.advance();
502
+ }
503
+ return createPropertyPath(createReference(baseRef), chainedProps);
504
+ }
505
+ tokens.reset(mark);
506
+ return null;
507
+ }
508
+ /**
509
+ * Check if a keyword is a structural keyword (preposition, control flow, etc.)
510
+ * that shouldn't be consumed as a property name.
511
+ */
512
+ isStructuralKeyword(value) {
513
+ const structural = /* @__PURE__ */ new Set([
514
+ // Prepositions
515
+ "into",
516
+ "in",
517
+ "to",
518
+ "from",
519
+ "at",
520
+ "by",
521
+ "with",
522
+ "without",
523
+ "before",
524
+ "after",
525
+ "of",
526
+ "as",
527
+ "on",
528
+ // Control flow
529
+ "then",
530
+ "end",
531
+ "else",
532
+ "if",
533
+ "repeat",
534
+ "while",
535
+ "for",
536
+ // Commands (shouldn't be property names)
537
+ "toggle",
538
+ "add",
539
+ "remove",
540
+ "put",
541
+ "set",
542
+ "show",
543
+ "hide",
544
+ "increment",
545
+ "decrement",
546
+ "send",
547
+ "trigger",
548
+ "call"
549
+ ]);
550
+ return structural.has(value.toLowerCase());
551
+ }
552
+ /**
553
+ * Try to match a method call expression like '#dialog.showModal()'.
554
+ * Pattern: selector + '.' + identifier + '(' + [args] + ')'
555
+ * Returns an expression value if matched, or null if not.
556
+ */
557
+ tryMatchMethodCallExpression(tokens) {
558
+ const token = tokens.peek();
559
+ if (!token || token.kind !== "selector") return null;
560
+ const mark = tokens.mark();
561
+ tokens.advance();
562
+ const dotToken = tokens.peek();
563
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
564
+ tokens.reset(mark);
565
+ return null;
566
+ }
567
+ tokens.advance();
568
+ const methodToken = tokens.peek();
569
+ if (!methodToken || methodToken.kind !== "identifier") {
570
+ tokens.reset(mark);
571
+ return null;
572
+ }
573
+ tokens.advance();
574
+ const openParen = tokens.peek();
575
+ if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
576
+ tokens.reset(mark);
577
+ return null;
578
+ }
579
+ tokens.advance();
580
+ const args = [];
581
+ while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
582
+ const argToken = tokens.peek();
583
+ if (!argToken) break;
584
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
585
+ tokens.advance();
586
+ break;
587
+ }
588
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
589
+ tokens.advance();
590
+ continue;
591
+ }
592
+ args.push(argToken.value);
593
+ tokens.advance();
594
+ }
595
+ const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
596
+ return {
597
+ type: "expression",
598
+ raw: methodCall
599
+ };
600
+ }
601
+ /**
602
+ * Try to match a property access expression like 'userData.name' or 'it.data'.
603
+ * Pattern: (identifier | keyword) + '.' + identifier [+ '.' + identifier ...]
604
+ * Returns an expression value if matched, or null if not.
605
+ */
606
+ tryMatchPropertyAccessExpression(tokens) {
607
+ const token = tokens.peek();
608
+ if (!token) return null;
609
+ if (token.kind !== "identifier" && token.kind !== "keyword") return null;
610
+ const mark = tokens.mark();
611
+ tokens.advance();
612
+ const dotToken = tokens.peek();
613
+ if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
614
+ tokens.reset(mark);
615
+ return null;
616
+ }
617
+ tokens.advance();
618
+ const propertyToken = tokens.peek();
619
+ if (!propertyToken || propertyToken.kind !== "identifier") {
620
+ tokens.reset(mark);
621
+ return null;
622
+ }
623
+ tokens.advance();
624
+ let chain = `${token.value}.${propertyToken.value}`;
625
+ let depth = 1;
626
+ while (!tokens.isAtEnd() && depth < _PatternMatcher.MAX_PROPERTY_DEPTH) {
627
+ const nextDot = tokens.peek();
628
+ if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
629
+ break;
630
+ }
631
+ tokens.advance();
632
+ const nextProp = tokens.peek();
633
+ if (!nextProp || nextProp.kind !== "identifier") {
634
+ break;
635
+ }
636
+ tokens.advance();
637
+ chain += `.${nextProp.value}`;
638
+ depth++;
639
+ }
640
+ const openParen = tokens.peek();
641
+ if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
642
+ tokens.advance();
643
+ const args = [];
644
+ let argDepth = 0;
645
+ while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
646
+ const argToken = tokens.peek();
647
+ if (!argToken) break;
648
+ if (argToken.kind === "punctuation" && argToken.value === ")") {
649
+ if (argDepth === 0) {
650
+ tokens.advance();
651
+ break;
652
+ }
653
+ argDepth--;
654
+ }
655
+ if (argToken.kind === "punctuation" && argToken.value === "(") {
656
+ argDepth++;
657
+ }
658
+ if (argToken.kind === "punctuation" && argToken.value === ",") {
659
+ tokens.advance();
660
+ continue;
661
+ }
662
+ args.push(argToken.value);
663
+ tokens.advance();
664
+ }
665
+ const methodCall = `${chain}(${args.join(", ")})`;
666
+ return {
667
+ type: "expression",
668
+ raw: methodCall
669
+ };
670
+ }
671
+ return {
672
+ type: "expression",
673
+ raw: chain
674
+ };
675
+ }
676
+ /**
677
+ * Try to match a possessive selector expression like "#element's *opacity".
678
+ * Pattern: selector + "'s" + (selector | identifier)
679
+ * Returns a property-path value if matched, or null if not.
680
+ */
681
+ tryMatchPossessiveSelectorExpression(tokens) {
682
+ const token = tokens.peek();
683
+ if (!token || token.kind !== "selector") return null;
684
+ const mark = tokens.mark();
685
+ tokens.advance();
686
+ const possessiveToken = tokens.peek();
687
+ if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
688
+ tokens.reset(mark);
689
+ return null;
690
+ }
691
+ tokens.advance();
692
+ const propertyToken = tokens.peek();
693
+ if (!propertyToken) {
694
+ tokens.reset(mark);
695
+ return null;
696
+ }
697
+ if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
698
+ tokens.reset(mark);
699
+ return null;
700
+ }
701
+ tokens.advance();
702
+ return createPropertyPath(createSelector(token.value), propertyToken.value);
703
+ }
704
+ /**
705
+ * Try to match a selector + property expression like "#output.innerText".
706
+ * This handles cases where the tokenizer produces two selector tokens:
707
+ * - #output (id selector)
708
+ * - .innerText (looks like class selector, but is actually property)
709
+ *
710
+ * Pattern: id-selector + class-selector-that-is-actually-property
711
+ * Returns a property-path value if matched, or null if not.
712
+ */
713
+ tryMatchSelectorPropertyExpression(tokens) {
714
+ const token = tokens.peek();
715
+ if (!token || token.kind !== "selector") return null;
716
+ if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
717
+ const mark = tokens.mark();
718
+ tokens.advance();
719
+ const propertyToken = tokens.peek();
720
+ if (!propertyToken || propertyToken.kind !== "selector") {
721
+ tokens.reset(mark);
722
+ return null;
723
+ }
724
+ if (!propertyToken.value.startsWith(".")) {
725
+ tokens.reset(mark);
726
+ return null;
727
+ }
728
+ const peek2 = tokens.peek(1);
729
+ if (peek2 && peek2.kind === "selector") {
730
+ }
731
+ tokens.advance();
732
+ const propertyName = propertyToken.value.slice(1);
733
+ return createPropertyPath(createSelector(token.value), propertyName);
734
+ }
735
+ /**
736
+ * Match a group pattern token (optional sequence).
737
+ * When the group's leading marker isn't at the current position, scans ahead
738
+ * up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
739
+ * (e.g., a value like 'hello') to sit between groups without blocking later
740
+ * marker matches.
741
+ */
742
+ matchGroupToken(tokens, patternToken, captured) {
743
+ const mark = tokens.mark();
744
+ const capturedBefore = new Set(captured.keys());
745
+ const success = this.matchTokenSequence(tokens, patternToken.tokens, captured);
746
+ if (success) return true;
747
+ tokens.reset(mark);
748
+ for (const role of captured.keys()) {
749
+ if (!capturedBefore.has(role)) captured.delete(role);
750
+ }
751
+ if (!patternToken.optional) return false;
752
+ const leadingMarker = this.getGroupLeadingMarker(patternToken);
753
+ if (leadingMarker) {
754
+ for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
755
+ const ahead = tokens.peek(offset);
756
+ if (!ahead) break;
757
+ const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
758
+ if (aheadValue === leadingMarker) {
759
+ this.logger.debug(
760
+ " >> MARKER SCAN: found",
761
+ leadingMarker,
762
+ "at offset",
763
+ offset,
764
+ "- skipping intervening tokens"
765
+ );
766
+ for (let s = 0; s < offset; s++) tokens.advance();
767
+ const retrySuccess = this.matchTokenSequence(tokens, patternToken.tokens, captured);
768
+ if (retrySuccess) return true;
769
+ tokens.reset(mark);
770
+ for (const role of captured.keys()) {
771
+ if (!capturedBefore.has(role)) captured.delete(role);
772
+ }
773
+ break;
774
+ }
775
+ }
776
+ }
777
+ return true;
778
+ }
779
+ /**
780
+ * Get the leading marker literal from an optional group.
781
+ * Returns the lowercase marker value, or null if the group
782
+ * doesn't start with a literal token (e.g., SOV groups where
783
+ * the role comes before the marker).
784
+ */
785
+ getGroupLeadingMarker(group) {
786
+ const first = group.tokens[0];
787
+ if (first?.type === "literal") return first.value.toLowerCase();
788
+ return null;
789
+ }
790
+ /**
791
+ * Get the type of match for a token against a value.
792
+ * Used for confidence calculation.
793
+ */
794
+ getMatchType(token, value) {
795
+ if (token.value === value) return "exact";
796
+ if (token.normalized === value) return "normalized";
797
+ if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
798
+ return "stem";
799
+ }
800
+ if (token.kind === "keyword" && this.safeToLowerCase(token.value) === value.toLowerCase()) {
801
+ return "case-insensitive";
802
+ }
803
+ return "none";
804
+ }
805
+ /**
806
+ * Collect literal values from upcoming pattern tokens that act as stop markers
807
+ * for greedy role capture. Returns the set of lowercase values.
808
+ */
809
+ collectStopMarkers(patternTokens, startIndex) {
810
+ const markers = /* @__PURE__ */ new Set();
811
+ for (let j = startIndex; j < patternTokens.length; j++) {
812
+ const pt = patternTokens[j];
813
+ if (pt.type === "literal") {
814
+ markers.add(pt.value.toLowerCase());
815
+ if (pt.alternatives) {
816
+ for (const alt of pt.alternatives) {
817
+ markers.add(alt.toLowerCase());
818
+ }
819
+ }
820
+ break;
821
+ }
822
+ if (pt.type === "group") {
823
+ for (const gt of pt.tokens) {
824
+ if (gt.type === "literal") {
825
+ markers.add(gt.value.toLowerCase());
826
+ if (gt.alternatives) {
827
+ for (const alt of gt.alternatives) {
828
+ markers.add(alt.toLowerCase());
829
+ }
830
+ }
831
+ break;
832
+ }
833
+ }
834
+ break;
835
+ }
836
+ }
837
+ return markers;
838
+ }
839
+ /**
840
+ * Check if a token matches any stop marker for greedy capture.
841
+ */
842
+ isStopMarker(token, stopMarkers) {
843
+ if (stopMarkers.size === 0) return false;
844
+ const value = (token.normalized || token.value).toLowerCase();
845
+ return stopMarkers.has(value);
846
+ }
847
+ /**
848
+ * Convert a language token to a semantic value.
849
+ */
850
+ tokenToSemanticValue(token) {
851
+ switch (token.kind) {
852
+ case "selector":
853
+ return createSelector(token.value);
854
+ case "literal":
855
+ return this.parseLiteralValue(token.value);
856
+ case "keyword":
857
+ const tokenValue = token.normalized || token.value;
858
+ const lower = this.safeToLowerCase(tokenValue);
859
+ if (isValidReference(lower)) {
860
+ return createReference(lower);
861
+ }
862
+ return createLiteral(token.normalized || token.value);
863
+ case "identifier":
864
+ if (typeof token.value === "string" && token.value.startsWith(":")) {
865
+ return createReference(token.value);
866
+ }
867
+ const identLower = this.safeToLowerCase(token.value);
868
+ if (isValidReference(identLower)) {
869
+ return createReference(identLower);
870
+ }
871
+ return { type: "expression", raw: token.value };
872
+ case "url":
873
+ return createLiteral(token.value, "string");
874
+ default:
875
+ return null;
876
+ }
877
+ }
878
+ /**
879
+ * Parse a literal value (string, number, boolean).
880
+ */
881
+ parseLiteralValue(value) {
882
+ if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
883
+ const inner = value.slice(1, -1);
884
+ return createLiteral(inner, "string");
885
+ }
886
+ if (value === "true") return createLiteral(true, "boolean");
887
+ if (value === "false") return createLiteral(false, "boolean");
888
+ const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
889
+ if (durationMatch) {
890
+ const num2 = parseFloat(durationMatch[1]);
891
+ const unit = durationMatch[2];
892
+ if (unit) {
893
+ return createLiteral(value, "duration");
894
+ }
895
+ return createLiteral(num2, "number");
896
+ }
897
+ const num = parseFloat(value);
898
+ if (!isNaN(num)) {
899
+ return createLiteral(num, "number");
900
+ }
901
+ return createLiteral(value, "string");
902
+ }
903
+ /**
904
+ * Apply extraction rules to fill in static values and defaults for missing roles.
905
+ */
906
+ applyExtractionRules(pattern, captured) {
907
+ for (const [role, rule] of Object.entries(pattern.extraction)) {
908
+ if (!captured.has(role)) {
909
+ if (rule.value !== void 0) {
910
+ captured.set(role, { type: "literal", value: rule.value });
911
+ } else if (rule.default) {
912
+ captured.set(role, rule.default);
913
+ }
914
+ }
915
+ }
916
+ }
917
+ /**
918
+ * Check if a pattern token is optional.
919
+ */
920
+ isOptional(patternToken) {
921
+ return patternToken.type !== "literal" && patternToken.optional === true;
922
+ }
923
+ /**
924
+ * Calculate confidence score for a match (0-1).
925
+ *
926
+ * Confidence is reduced for:
927
+ * - Stem matches (morphological normalization has inherent uncertainty)
928
+ * - Missing optional roles (but less penalty if role has a default value)
929
+ *
930
+ * Confidence is increased for:
931
+ * - VSO languages (Arabic) when pattern starts with a verb
932
+ */
933
+ calculateConfidence(pattern, captured) {
934
+ let score = 0;
935
+ let maxScore = 0;
936
+ const hasDefault = (role) => {
937
+ return pattern.extraction?.[role]?.default !== void 0;
938
+ };
939
+ for (const token of pattern.template.tokens) {
940
+ if (token.type === "role") {
941
+ maxScore += 1;
942
+ if (captured.has(token.role)) {
943
+ score += 1;
944
+ }
945
+ } else if (token.type === "group") {
946
+ for (const subToken of token.tokens) {
947
+ if (subToken.type === "role") {
948
+ const roleHasDefault = hasDefault(subToken.role);
949
+ const weight = 0.8;
950
+ maxScore += weight;
951
+ if (captured.has(subToken.role)) {
952
+ score += weight;
953
+ } else if (roleHasDefault) {
954
+ score += weight * 0.6;
955
+ }
956
+ }
957
+ }
958
+ }
959
+ }
960
+ let baseConfidence = maxScore > 0 ? score / maxScore : 1;
961
+ if (this.stemMatchCount > 0 && this.totalKeywordMatches > 0) {
962
+ const stemPenalty = this.stemMatchCount / this.totalKeywordMatches * 0.15;
963
+ baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
964
+ }
965
+ const vsoBoost = this.calculateVSOConfidenceBoost(pattern);
966
+ baseConfidence = Math.min(1, baseConfidence + vsoBoost);
967
+ const prepositionAdjustment = this.arabicPrepositionDisambiguation(pattern, captured);
968
+ baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
969
+ return baseConfidence;
970
+ }
971
+ /**
972
+ * Calculate confidence boost for VSO (Verb-Subject-Object) language patterns.
973
+ * Arabic naturally uses VSO word order, so patterns that start with a verb
974
+ * should receive a confidence boost.
975
+ *
976
+ * Returns +0.15 confidence boost if:
977
+ * - Language is Arabic ('ar')
978
+ * - Pattern's first token is a verb keyword
979
+ *
980
+ * @param pattern The language pattern being matched
981
+ * @returns Confidence boost (0 or 0.15)
982
+ */
983
+ calculateVSOConfidenceBoost(pattern) {
984
+ if (pattern.language !== "ar") {
985
+ return 0;
986
+ }
987
+ const firstToken = pattern.template.tokens[0];
988
+ if (!firstToken || firstToken.type !== "literal") {
989
+ return 0;
990
+ }
991
+ const ARABIC_VERBS = /* @__PURE__ */ new Set([
992
+ "\u0628\u062F\u0644",
993
+ "\u063A\u064A\u0631",
994
+ "\u0623\u0636\u0641",
995
+ "\u0623\u0632\u0644",
996
+ "\u0636\u0639",
997
+ "\u0627\u062C\u0639\u0644",
998
+ "\u0639\u064A\u0646",
999
+ "\u0632\u062F",
1000
+ "\u0627\u0646\u0642\u0635",
1001
+ "\u0633\u062C\u0644",
1002
+ "\u0623\u0638\u0647\u0631",
1003
+ "\u0623\u062E\u0641",
1004
+ "\u0634\u063A\u0644",
1005
+ "\u0623\u0631\u0633\u0644",
1006
+ "\u0631\u0643\u0632",
1007
+ "\u0634\u0648\u0634",
1008
+ "\u062A\u0648\u0642\u0641",
1009
+ "\u0627\u0646\u0633\u062E",
1010
+ "\u0627\u062D\u0630\u0641",
1011
+ "\u0627\u0635\u0646\u0639",
1012
+ "\u0627\u0646\u062A\u0638\u0631",
1013
+ "\u0627\u0646\u062A\u0642\u0627\u0644",
1014
+ "\u0623\u0648"
1015
+ ]);
1016
+ if (ARABIC_VERBS.has(firstToken.value)) {
1017
+ return 0.15;
1018
+ }
1019
+ if (firstToken.alternatives) {
1020
+ for (const alt of firstToken.alternatives) {
1021
+ if (ARABIC_VERBS.has(alt)) {
1022
+ return 0.15;
1023
+ }
1024
+ }
1025
+ }
1026
+ return 0;
1027
+ }
1028
+ /**
1029
+ * Arabic preposition disambiguation for confidence adjustment.
1030
+ *
1031
+ * Different Arabic prepositions are more or less natural for different semantic roles:
1032
+ * - على (on/upon) is preferred for patient/target roles (element selectors)
1033
+ * - إلى (to) is preferred for destination roles
1034
+ * - من (from) is preferred for source roles
1035
+ * - في (in) is preferred for location roles
1036
+ *
1037
+ * This method analyzes the prepositions used with captured semantic roles and
1038
+ * adjusts confidence based on idiomaticity:
1039
+ * - +0.10 for highly idiomatic preposition choices
1040
+ * - -0.10 for less natural preposition choices
1041
+ *
1042
+ * @param pattern The language pattern being matched
1043
+ * @param captured The captured semantic values
1044
+ * @returns Confidence adjustment (-0.10 to +0.10)
1045
+ */
1046
+ arabicPrepositionDisambiguation(pattern, captured) {
1047
+ if (pattern.language !== "ar") {
1048
+ return 0;
1049
+ }
1050
+ let adjustment = 0;
1051
+ const PREFERRED_PREPOSITIONS = {
1052
+ patient: ["\u0639\u0644\u0649"],
1053
+ // element selectors prefer على (on/upon)
1054
+ destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1055
+ // destination prefers إلى (to)
1056
+ source: ["\u0645\u0646"],
1057
+ // source prefers من (from)
1058
+ agent: ["\u0645\u0646"],
1059
+ // agent/by prefers من (from/by)
1060
+ manner: ["\u0628"],
1061
+ // manner prefers ب (with/by)
1062
+ style: ["\u0628"],
1063
+ // style prefers ب (with)
1064
+ goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
1065
+ // target state prefers إلى (to)
1066
+ method: ["\u0628"]
1067
+ // method prefers ب (with/by)
1068
+ };
1069
+ for (const [role, value] of captured.entries()) {
1070
+ const preferred = PREFERRED_PREPOSITIONS[role];
1071
+ if (!preferred || preferred.length === 0) {
1072
+ continue;
1073
+ }
1074
+ const metadata = "metadata" in value ? value.metadata : void 0;
1075
+ if (metadata && typeof metadata.prepositionValue === "string") {
1076
+ const usedPreposition = metadata.prepositionValue;
1077
+ if (preferred.includes(usedPreposition)) {
1078
+ adjustment += 0.1;
1079
+ } else {
1080
+ adjustment -= 0.1;
1081
+ }
1082
+ }
1083
+ }
1084
+ return Math.max(-0.1, Math.min(0.1, adjustment));
1085
+ }
1086
+ /**
1087
+ * Skip noise words like "the" before selectors.
1088
+ * This enables more natural English syntax like "toggle the .active".
1089
+ */
1090
+ skipNoiseWords(tokens) {
1091
+ const token = tokens.peek();
1092
+ if (!token) return;
1093
+ const tokenLower = this.safeToLowerCase(token.value);
1094
+ if (_PatternMatcher.ENGLISH_NOISE_WORDS.has(tokenLower)) {
1095
+ const mark = tokens.mark();
1096
+ tokens.advance();
1097
+ const nextToken = tokens.peek();
1098
+ if (nextToken && nextToken.kind === "selector") {
1099
+ return;
1100
+ }
1101
+ tokens.reset(mark);
1102
+ }
1103
+ if (tokenLower === "class") {
1104
+ tokens.advance();
1105
+ }
1106
+ }
1107
+ /**
1108
+ * Extract event modifiers from the token stream.
1109
+ * Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
1110
+ * that can appear after event names.
1111
+ *
1112
+ * Returns EventModifiers object or undefined if no modifiers found.
1113
+ */
1114
+ extractEventModifiers(tokens) {
1115
+ const modifiers = {};
1116
+ let foundModifier = false;
1117
+ while (!tokens.isAtEnd()) {
1118
+ const token = tokens.peek();
1119
+ if (!token || token.kind !== "event-modifier") {
1120
+ break;
1121
+ }
1122
+ const metadata = token.metadata;
1123
+ if (!metadata) {
1124
+ break;
1125
+ }
1126
+ foundModifier = true;
1127
+ switch (metadata.modifierName) {
1128
+ case "once":
1129
+ modifiers.once = true;
1130
+ break;
1131
+ case "debounce":
1132
+ if (typeof metadata.value === "number") {
1133
+ modifiers.debounce = metadata.value;
1134
+ }
1135
+ break;
1136
+ case "throttle":
1137
+ if (typeof metadata.value === "number") {
1138
+ modifiers.throttle = metadata.value;
1139
+ }
1140
+ break;
1141
+ case "queue":
1142
+ if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
1143
+ modifiers.queue = metadata.value;
1144
+ }
1145
+ break;
1146
+ }
1147
+ tokens.advance();
1148
+ }
1149
+ return foundModifier ? modifiers : void 0;
1150
+ }
1151
+ };
1152
+ /** Maximum tokens to scan ahead when looking for a group's leading marker */
1153
+ _PatternMatcher.MAX_MARKER_SCAN = 3;
1154
+ // ==========================================================================
1155
+ // Depth Limits for Expression Parsing (security hardening)
1156
+ // ==========================================================================
1157
+ /** Maximum depth for nested property access (e.g., a.b.c.d...) */
1158
+ _PatternMatcher.MAX_PROPERTY_DEPTH = 10;
1159
+ /** Maximum number of arguments in method calls */
1160
+ _PatternMatcher.MAX_METHOD_ARGS = 20;
1161
+ // ===========================================================================
1162
+ // English Idiom Support - Noise Word Handling
1163
+ // ===========================================================================
1164
+ /**
1165
+ * Noise words that can be skipped in English for more natural syntax.
1166
+ * - "the" before selectors: "toggle the .active" → "toggle .active"
1167
+ * - "class" after class selectors: "add the .visible class" → "add .visible"
1168
+ */
1169
+ _PatternMatcher.ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
1170
+ var PatternMatcher = _PatternMatcher;
1171
+ var patternMatcher = new PatternMatcher();
1172
+
1173
+ // src/generation/pattern-generator.ts
1174
+ var defaultConfig = {
1175
+ basePriority: 100,
1176
+ includeOptionalVariants: true
1177
+ };
1178
+ function generatePattern(schema, profile, config = defaultConfig) {
1179
+ const id = `${schema.action}-${profile.code}-generated`;
1180
+ const priority = config.basePriority ?? 100;
1181
+ const keyword = profile.keywords[schema.action];
1182
+ if (!keyword) {
1183
+ throw new Error(`No keyword translation for '${schema.action}' in ${profile.code}`);
1184
+ }
1185
+ const tokens = buildTokens(schema, profile, keyword.primary, keyword.alternatives);
1186
+ const extraction = buildExtractionRules(schema, profile);
1187
+ const format = buildFormatString(schema, profile, keyword.primary);
1188
+ return {
1189
+ id,
1190
+ language: profile.code,
1191
+ command: schema.action,
1192
+ priority,
1193
+ template: {
1194
+ format,
1195
+ tokens
1196
+ },
1197
+ extraction
1198
+ };
1199
+ }
1200
+ function buildTokens(schema, profile, keyword, alternatives) {
1201
+ const tokens = [];
1202
+ const keywordToken = {
1203
+ type: "literal",
1204
+ value: keyword,
1205
+ ...alternatives?.length && { alternatives }
1206
+ };
1207
+ const sortedRoles = sortRolesByWordOrder(schema.roles, profile.wordOrder);
1208
+ if (profile.wordOrder === "SVO") {
1209
+ tokens.push(keywordToken);
1210
+ for (const role of sortedRoles) {
1211
+ addRoleWithMarker(tokens, role, profile);
1212
+ }
1213
+ } else if (profile.wordOrder === "SOV") {
1214
+ for (const role of sortedRoles) {
1215
+ addRoleWithMarker(tokens, role, profile);
1216
+ }
1217
+ tokens.push(keywordToken);
1218
+ } else if (profile.wordOrder === "VSO") {
1219
+ tokens.push(keywordToken);
1220
+ for (const role of sortedRoles) {
1221
+ addRoleWithMarker(tokens, role, profile);
1222
+ }
1223
+ } else {
1224
+ tokens.push(keywordToken);
1225
+ for (const role of sortedRoles) {
1226
+ addRoleWithMarker(tokens, role, profile);
1227
+ }
1228
+ }
1229
+ return tokens;
1230
+ }
1231
+ function addRoleWithMarker(tokens, roleSpec, profile) {
1232
+ const marker = getMarkerForRole(roleSpec, profile);
1233
+ const isOptional = !roleSpec.required;
1234
+ const roleToken = {
1235
+ type: "role",
1236
+ role: roleSpec.role,
1237
+ optional: isOptional,
1238
+ expectedTypes: roleSpec.expectedTypes,
1239
+ ...roleSpec.greedy && { greedy: true }
1240
+ };
1241
+ if (marker) {
1242
+ const markerInfo = profile.roleMarkers?.[roleSpec.role];
1243
+ const defaultPosition = profile.wordOrder === "SOV" ? "after" : "before";
1244
+ const position = markerInfo?.position ?? defaultPosition;
1245
+ if (isOptional) {
1246
+ if (position === "before") {
1247
+ tokens.push({
1248
+ type: "group",
1249
+ optional: true,
1250
+ tokens: [{ type: "literal", value: marker }, roleToken]
1251
+ });
1252
+ } else {
1253
+ tokens.push({
1254
+ type: "group",
1255
+ optional: true,
1256
+ tokens: [roleToken, { type: "literal", value: marker }]
1257
+ });
1258
+ }
1259
+ } else {
1260
+ if (position === "before") {
1261
+ tokens.push({ type: "literal", value: marker });
1262
+ tokens.push(roleToken);
1263
+ } else {
1264
+ tokens.push(roleToken);
1265
+ tokens.push({ type: "literal", value: marker });
1266
+ }
1267
+ }
1268
+ } else {
1269
+ tokens.push(roleToken);
1270
+ }
1271
+ }
1272
+ function getMarkerForRole(roleSpec, profile) {
1273
+ if (roleSpec.markerOverride?.[profile.code]) {
1274
+ return roleSpec.markerOverride[profile.code];
1275
+ }
1276
+ return profile.roleMarkers?.[roleSpec.role]?.primary;
1277
+ }
1278
+ function sortRolesByWordOrder(roles, wordOrder) {
1279
+ const sorted = [...roles];
1280
+ if (wordOrder === "SVO") {
1281
+ sorted.sort((a, b) => (b.svoPosition ?? 0) - (a.svoPosition ?? 0));
1282
+ } else if (wordOrder === "SOV") {
1283
+ sorted.sort((a, b) => (b.sovPosition ?? 0) - (a.sovPosition ?? 0));
1284
+ }
1285
+ return sorted;
1286
+ }
1287
+ function buildExtractionRules(schema, profile) {
1288
+ const rules = {};
1289
+ for (const roleSpec of schema.roles) {
1290
+ const marker = getMarkerForRole(roleSpec, profile);
1291
+ const rule = {
1292
+ ...roleSpec.default !== void 0 && { default: roleSpec.default },
1293
+ ...marker && { marker }
1294
+ };
1295
+ rules[roleSpec.role] = rule;
1296
+ }
1297
+ return rules;
1298
+ }
1299
+ function buildFormatString(schema, profile, keyword) {
1300
+ const parts = [];
1301
+ if (profile.wordOrder === "SVO" || profile.wordOrder === "VSO") {
1302
+ parts.push(keyword);
1303
+ }
1304
+ for (const role of schema.roles) {
1305
+ const marker = getMarkerForRole(role, profile);
1306
+ const roleName = `{${role.role}}`;
1307
+ if (marker) {
1308
+ const markerInfo = profile.roleMarkers?.[role.role];
1309
+ if (markerInfo?.position === "after") {
1310
+ parts.push(`${roleName} ${marker}`);
1311
+ } else {
1312
+ parts.push(`${marker} ${roleName}`);
1313
+ }
1314
+ } else {
1315
+ parts.push(roleName);
1316
+ }
1317
+ }
1318
+ if (profile.wordOrder === "SOV") {
1319
+ parts.push(keyword);
1320
+ }
1321
+ return parts.join(" ");
1322
+ }
1323
+
1324
+ // src/grammar/types.ts
1325
+ function reorderRoles(roles, targetOrder) {
1326
+ const result = [];
1327
+ const usedRoles = /* @__PURE__ */ new Set();
1328
+ for (const role of targetOrder) {
1329
+ const element = roles.get(role);
1330
+ if (element) {
1331
+ result.push(element);
1332
+ usedRoles.add(role);
1333
+ }
1334
+ }
1335
+ for (const [role, element] of roles) {
1336
+ if (!usedRoles.has(role)) {
1337
+ result.push(element);
1338
+ }
1339
+ }
1340
+ return result;
1341
+ }
1342
+ function insertMarkers(elements, markers, adpositionType) {
1343
+ const result = [];
1344
+ for (const element of elements) {
1345
+ const marker = markers.find((m) => m.role === element.role);
1346
+ if (marker) {
1347
+ if (adpositionType === "preposition") {
1348
+ if (marker.form) result.push(marker.form);
1349
+ result.push(element.translated || element.value);
1350
+ } else if (adpositionType === "postposition") {
1351
+ result.push(element.translated || element.value);
1352
+ if (marker.form) result.push(marker.form);
1353
+ } else {
1354
+ result.push(element.translated || element.value);
1355
+ }
1356
+ } else {
1357
+ result.push(element.translated || element.value);
1358
+ }
1359
+ }
1360
+ return result;
1361
+ }
1362
+ function joinTokens(tokens) {
1363
+ if (tokens.length === 0) return "";
1364
+ let result = "";
1365
+ for (let i = 0; i < tokens.length; i++) {
1366
+ const token = tokens[i];
1367
+ const nextToken = tokens[i + 1];
1368
+ const isPrefix = token.endsWith("-");
1369
+ const isSuffix = token.startsWith("-");
1370
+ let displayToken = token;
1371
+ if (isPrefix) displayToken = token.slice(0, -1);
1372
+ if (isSuffix) displayToken = token.substring(1);
1373
+ result += displayToken;
1374
+ if (nextToken) {
1375
+ const nextIsSuffix = nextToken.startsWith("-");
1376
+ if (!isPrefix && !nextIsSuffix) {
1377
+ result += " ";
1378
+ }
1379
+ }
1380
+ }
1381
+ return result;
1382
+ }
1383
+
1384
+ // src/grammar/transformer.ts
1385
+ var GrammarTransformer = class {
1386
+ constructor(config) {
1387
+ this.dictionary = config.dictionary;
1388
+ this.profileProvider = config.profileProvider;
1389
+ }
1390
+ /**
1391
+ * Transform a statement from one language to another.
1392
+ *
1393
+ * @param input - Statement in source language
1394
+ * @param fromLanguage - Source language code
1395
+ * @param toLanguage - Target language code
1396
+ * @returns Transformed statement in target language
1397
+ *
1398
+ * @throws Error if profiles not found or translation fails
1399
+ */
1400
+ transform(input, fromLanguage, toLanguage) {
1401
+ const fromProfile = this.profileProvider.getProfile(fromLanguage);
1402
+ const toProfile = this.profileProvider.getProfile(toLanguage);
1403
+ if (!fromProfile) {
1404
+ throw new Error(`No profile found for language: ${fromLanguage}`);
1405
+ }
1406
+ if (!toProfile) {
1407
+ throw new Error(`No profile found for language: ${toLanguage}`);
1408
+ }
1409
+ const parsed = this.parseStatement(input, fromLanguage, fromProfile);
1410
+ const translated = this.translateRoles(parsed, fromLanguage, toLanguage);
1411
+ const reordered = reorderRoles(translated, toProfile.canonicalOrder);
1412
+ const withMarkers = insertMarkers(reordered, toProfile.markers, toProfile.adpositionType);
1413
+ return joinTokens(withMarkers);
1414
+ }
1415
+ /**
1416
+ * Parse statement into semantic roles.
1417
+ * This is a simplified generic parser - DSLs can provide custom parsers.
1418
+ */
1419
+ parseStatement(input, language, profile) {
1420
+ const roles = /* @__PURE__ */ new Map();
1421
+ const tokens = input.split(/\s+/);
1422
+ let i = 0;
1423
+ while (i < tokens.length) {
1424
+ const token = tokens[i];
1425
+ const marker = profile.markers.find((m) => m.form === token);
1426
+ if (marker) {
1427
+ if (i + 1 < tokens.length) {
1428
+ roles.set(marker.role, tokens[i + 1]);
1429
+ i += 2;
1430
+ } else {
1431
+ i++;
1432
+ }
1433
+ } else {
1434
+ const canonical = this.dictionary.lookup(token, language);
1435
+ if (canonical) {
1436
+ roles.set("action", canonical);
1437
+ } else {
1438
+ if (token.startsWith("#") || token.startsWith(".")) {
1439
+ roles.set("patient", token);
1440
+ } else {
1441
+ roles.set("patient", token);
1442
+ }
1443
+ }
1444
+ i++;
1445
+ }
1446
+ }
1447
+ return roles;
1448
+ }
1449
+ /**
1450
+ * Translate roles using injected dictionary.
1451
+ * Returns a Map of ParsedElement objects expected by reorderRoles.
1452
+ */
1453
+ translateRoles(roles, _fromLanguage, toLanguage) {
1454
+ const result = /* @__PURE__ */ new Map();
1455
+ for (const [role, value] of roles) {
1456
+ const translated = this.dictionary.translate(value, toLanguage);
1457
+ result.set(role, {
1458
+ role,
1459
+ value,
1460
+ // Original value
1461
+ translated: translated ?? value
1462
+ // Translated value or original if no translation
1463
+ });
1464
+ }
1465
+ return result;
1466
+ }
1467
+ };
1468
+
1469
+ // src/interfaces/dictionary.ts
1470
+ var InMemoryDictionary = class {
1471
+ /**
1472
+ * @param translations - Map of language code to canonical→localized mappings
1473
+ *
1474
+ * @example
1475
+ * new InMemoryDictionary({
1476
+ * en: { select: 'select', insert: 'insert' },
1477
+ * es: { select: 'seleccionar', insert: 'insertar' },
1478
+ * ja: { select: '選択', insert: '挿入' }
1479
+ * })
1480
+ */
1481
+ constructor(translations) {
1482
+ this.translations = translations;
1483
+ }
1484
+ lookup(word, language) {
1485
+ const langDict = this.translations[language];
1486
+ if (!langDict) return void 0;
1487
+ const wordLower = word.toLowerCase();
1488
+ for (const [canonical, localized] of Object.entries(langDict)) {
1489
+ if (localized.toLowerCase() === wordLower) {
1490
+ return canonical;
1491
+ }
1492
+ }
1493
+ return void 0;
1494
+ }
1495
+ translate(canonical, targetLanguage) {
1496
+ return this.translations[targetLanguage]?.[canonical];
1497
+ }
1498
+ getAllTranslations(word, sourceLanguage) {
1499
+ const canonical = this.lookup(word, sourceLanguage);
1500
+ if (!canonical) return {};
1501
+ const result = {};
1502
+ for (const [lang, dict] of Object.entries(this.translations)) {
1503
+ const translation = dict[canonical];
1504
+ if (translation) {
1505
+ result[lang] = translation;
1506
+ }
1507
+ }
1508
+ return result;
1509
+ }
1510
+ };
1511
+
1512
+ // src/interfaces/profile-provider.ts
1513
+ var InMemoryProfileProvider = class {
1514
+ /**
1515
+ * @param profiles - Map of language code to profile
1516
+ *
1517
+ * @example
1518
+ * new InMemoryProfileProvider({
1519
+ * en: { code: 'en', wordOrder: 'SVO', ... },
1520
+ * ja: { code: 'ja', wordOrder: 'SOV', ... }
1521
+ * })
1522
+ */
1523
+ constructor(profiles) {
1524
+ this.profiles = profiles;
1525
+ }
1526
+ getProfile(language) {
1527
+ return this.profiles[language];
1528
+ }
1529
+ getSupportedLanguages() {
1530
+ return Object.keys(this.profiles);
1531
+ }
1532
+ hasLanguage(language) {
1533
+ return language in this.profiles;
1534
+ }
1535
+ };
1536
+
1537
+ // src/api/create-dsl.ts
1538
+ var DSLRegistry = class {
1539
+ constructor(config) {
1540
+ this.patterns = /* @__PURE__ */ new Map();
1541
+ this.tokenizers = /* @__PURE__ */ new Map();
1542
+ this.schemas = config.schemas;
1543
+ for (const lang of config.languages) {
1544
+ this.registerLanguage(lang);
1545
+ }
1546
+ }
1547
+ registerLanguage(lang) {
1548
+ this.tokenizers.set(lang.code, lang.tokenizer);
1549
+ const patterns = [];
1550
+ for (const schema of this.schemas) {
1551
+ const pattern = generatePattern(schema, lang.patternProfile);
1552
+ patterns.push(pattern);
1553
+ }
1554
+ this.patterns.set(lang.code, patterns);
1555
+ }
1556
+ getPatterns(language) {
1557
+ return this.patterns.get(language) || [];
1558
+ }
1559
+ getTokenizer(language) {
1560
+ return this.tokenizers.get(language);
1561
+ }
1562
+ getSupportedLanguages() {
1563
+ return Array.from(this.patterns.keys());
1564
+ }
1565
+ };
1566
+ var MultilingualDSLImpl = class {
1567
+ constructor(config, registry, transformer) {
1568
+ this.registry = registry;
1569
+ this.matcher = new PatternMatcher();
1570
+ this.transformer = transformer;
1571
+ if (config.codeGenerator) {
1572
+ this.codeGenerator = config.codeGenerator;
1573
+ }
1574
+ }
1575
+ parse(input, language) {
1576
+ const result = this.parseWithConfidence(input, language);
1577
+ return result.node;
1578
+ }
1579
+ parseWithConfidence(input, language) {
1580
+ const tokenizer = this.registry.getTokenizer(language);
1581
+ if (!tokenizer) {
1582
+ throw new Error(`No tokenizer registered for language: ${language}`);
1583
+ }
1584
+ const tokens = tokenizer.tokenize(input);
1585
+ const patterns = this.registry.getPatterns(language);
1586
+ const profile = {
1587
+ code: language
1588
+ };
1589
+ for (const pattern of patterns) {
1590
+ const match = this.matcher.matchPattern(tokens, pattern, profile);
1591
+ if (match) {
1592
+ const node = {
1593
+ kind: "command",
1594
+ action: pattern.command,
1595
+ roles: match.captured,
1596
+ metadata: {
1597
+ sourceLanguage: language,
1598
+ sourceText: input,
1599
+ patternId: pattern.id,
1600
+ confidence: match.confidence
1601
+ }
1602
+ };
1603
+ return { node, confidence: match.confidence };
1604
+ }
1605
+ }
1606
+ throw new Error(`No pattern matched for input: ${input}`);
1607
+ }
1608
+ validate(input, language) {
1609
+ try {
1610
+ const result = this.parseWithConfidence(input, language);
1611
+ return {
1612
+ valid: true,
1613
+ node: result.node
1614
+ };
1615
+ } catch (error) {
1616
+ return {
1617
+ valid: false,
1618
+ errors: [error instanceof Error ? error.message : String(error)]
1619
+ };
1620
+ }
1621
+ }
1622
+ translate(input, fromLanguage, toLanguage) {
1623
+ return this.transformer.transform(input, fromLanguage, toLanguage);
1624
+ }
1625
+ compile(input, language) {
1626
+ if (!this.codeGenerator) {
1627
+ return {
1628
+ ok: false,
1629
+ errors: ["No code generator configured for this DSL"]
1630
+ };
1631
+ }
1632
+ try {
1633
+ const result = this.parseWithConfidence(input, language);
1634
+ const code = this.codeGenerator.generate(result.node);
1635
+ return {
1636
+ ok: true,
1637
+ code,
1638
+ node: result.node,
1639
+ metadata: {
1640
+ parser: "semantic",
1641
+ confidence: result.confidence
1642
+ }
1643
+ };
1644
+ } catch (error) {
1645
+ return {
1646
+ ok: false,
1647
+ errors: [error instanceof Error ? error.message : String(error)]
1648
+ };
1649
+ }
1650
+ }
1651
+ getSupportedLanguages() {
1652
+ return this.registry.getSupportedLanguages();
1653
+ }
1654
+ };
1655
+ function createDefaultDictionary(config) {
1656
+ const translations = {};
1657
+ for (const lang of config.languages) {
1658
+ const keywords = {};
1659
+ for (const [canonical, keywordDef] of Object.entries(lang.patternProfile.keywords)) {
1660
+ keywords[canonical] = keywordDef.primary;
1661
+ }
1662
+ translations[lang.code] = keywords;
1663
+ }
1664
+ return new InMemoryDictionary(translations);
1665
+ }
1666
+ function createDefaultProfileProvider(config) {
1667
+ const profiles = {};
1668
+ for (const lang of config.languages) {
1669
+ if (lang.grammarProfile) {
1670
+ profiles[lang.code] = lang.grammarProfile;
1671
+ }
1672
+ }
1673
+ return new InMemoryProfileProvider(profiles);
1674
+ }
1675
+ function createMultilingualDSL(config) {
1676
+ const dictionary = config.dictionary ?? createDefaultDictionary(config);
1677
+ const profileProvider = config.profileProvider ?? createDefaultProfileProvider(config);
1678
+ const transformer = new GrammarTransformer({
1679
+ dictionary,
1680
+ profileProvider
1681
+ });
1682
+ const registry = new DSLRegistry(config);
1683
+ return new MultilingualDSLImpl(config, registry, transformer);
1684
+ }
1685
+
1686
+ // src/api/domain-registry.ts
1687
+ var DomainRegistry = class {
1688
+ constructor() {
1689
+ this.descriptors = /* @__PURE__ */ new Map();
1690
+ this.dslCache = /* @__PURE__ */ new Map();
1691
+ this.rendererCache = /* @__PURE__ */ new Map();
1692
+ }
1693
+ /**
1694
+ * Register a domain.
1695
+ * @throws if a domain with the same name is already registered
1696
+ */
1697
+ register(descriptor) {
1698
+ if (this.descriptors.has(descriptor.name)) {
1699
+ throw new Error(`Domain already registered: ${descriptor.name}`);
1700
+ }
1701
+ this.descriptors.set(descriptor.name, descriptor);
1702
+ }
1703
+ /**
1704
+ * Get all registered domain names.
1705
+ */
1706
+ getDomainNames() {
1707
+ return Array.from(this.descriptors.keys());
1708
+ }
1709
+ /**
1710
+ * Get a specific domain descriptor.
1711
+ */
1712
+ getDescriptor(name) {
1713
+ return this.descriptors.get(name);
1714
+ }
1715
+ /**
1716
+ * Generate MCP tool definitions for all registered domains.
1717
+ */
1718
+ getToolDefinitions() {
1719
+ const tools = [];
1720
+ for (const desc of this.descriptors.values()) {
1721
+ tools.push(...generateToolDefinitions(desc));
1722
+ }
1723
+ return tools;
1724
+ }
1725
+ /**
1726
+ * Check if a tool name belongs to a registered domain.
1727
+ */
1728
+ canHandle(toolName) {
1729
+ const parsed = parseToolName(toolName);
1730
+ if (!parsed) return false;
1731
+ return this.descriptors.has(parsed.domain);
1732
+ }
1733
+ /**
1734
+ * Handle a tool call by dispatching to the appropriate domain.
1735
+ * Returns null if the tool name doesn't match any registered domain.
1736
+ */
1737
+ async handleToolCall(toolName, args) {
1738
+ const parsed = parseToolName(toolName);
1739
+ if (!parsed) return null;
1740
+ const descriptor = this.descriptors.get(parsed.domain);
1741
+ if (!descriptor) return null;
1742
+ try {
1743
+ const dsl = await this.getDSL(descriptor);
1744
+ switch (parsed.operation) {
1745
+ case "parse":
1746
+ return await this.handleParse(descriptor, dsl, args);
1747
+ case "compile":
1748
+ return await this.handleCompile(descriptor, dsl, args);
1749
+ case "validate":
1750
+ return await this.handleValidate(descriptor, dsl, args);
1751
+ case "translate":
1752
+ return await this.handleTranslate(descriptor, dsl, args);
1753
+ default:
1754
+ return jsonResponse({ error: `Unknown operation: ${parsed.operation}` }, true);
1755
+ }
1756
+ } catch (error) {
1757
+ const message = error instanceof Error ? error.message : String(error);
1758
+ return jsonResponse({ error: `${descriptor.name} tool error: ${message}` }, true);
1759
+ }
1760
+ }
1761
+ /**
1762
+ * Get the DSL instance for a named domain.
1763
+ * Returns null if the domain is not registered.
1764
+ * The DSL is lazily created and cached.
1765
+ */
1766
+ async getDSLForDomain(name) {
1767
+ const descriptor = this.descriptors.get(name);
1768
+ if (!descriptor) return null;
1769
+ return this.getDSL(descriptor);
1770
+ }
1771
+ // ---------------------------------------------------------------------------
1772
+ // Private: DSL/renderer lifecycle
1773
+ // ---------------------------------------------------------------------------
1774
+ async getDSL(descriptor) {
1775
+ const cached = this.dslCache.get(descriptor.name);
1776
+ if (cached) return cached;
1777
+ const dsl = await descriptor.getDSL();
1778
+ this.dslCache.set(descriptor.name, dsl);
1779
+ return dsl;
1780
+ }
1781
+ async getRenderer(descriptor) {
1782
+ if (!descriptor.getRenderer) return null;
1783
+ const cached = this.rendererCache.get(descriptor.name);
1784
+ if (cached) return cached;
1785
+ const renderer = await descriptor.getRenderer();
1786
+ this.rendererCache.set(descriptor.name, renderer);
1787
+ return renderer;
1788
+ }
1789
+ // ---------------------------------------------------------------------------
1790
+ // Private: Tool handlers
1791
+ // ---------------------------------------------------------------------------
1792
+ async handleParse(descriptor, dsl, args) {
1793
+ const input = getString(args, descriptor.inputLabel);
1794
+ if (!input) return missingParam(descriptor.inputLabel);
1795
+ const language = getString(args, "language", "en");
1796
+ const node = dsl.parse(input, language);
1797
+ const roles = {};
1798
+ for (const [key, value] of node.roles) {
1799
+ roles[key] = value;
1800
+ }
1801
+ return jsonResponse({
1802
+ action: node.action,
1803
+ roles,
1804
+ language,
1805
+ [descriptor.inputLabel]: input
1806
+ });
1807
+ }
1808
+ async handleCompile(descriptor, dsl, args) {
1809
+ const input = getString(args, descriptor.inputLabel);
1810
+ if (!input) return missingParam(descriptor.inputLabel);
1811
+ const language = getString(args, "language", "en");
1812
+ const result = dsl.compile(input, language);
1813
+ return jsonResponse({
1814
+ ok: result.ok,
1815
+ code: result.code,
1816
+ errors: result.errors,
1817
+ language,
1818
+ input
1819
+ });
1820
+ }
1821
+ async handleValidate(descriptor, dsl, args) {
1822
+ const input = getString(args, descriptor.inputLabel);
1823
+ if (!input) return missingParam(descriptor.inputLabel);
1824
+ const language = getString(args, "language", "en");
1825
+ const result = dsl.validate(input, language);
1826
+ return jsonResponse({
1827
+ valid: result.valid,
1828
+ errors: result.errors,
1829
+ language,
1830
+ [descriptor.inputLabel]: input
1831
+ });
1832
+ }
1833
+ async handleTranslate(descriptor, dsl, args) {
1834
+ const input = getString(args, descriptor.inputLabel);
1835
+ if (!input) return missingParam(descriptor.inputLabel);
1836
+ const from = getString(args, "from");
1837
+ if (!from) return missingParam("from");
1838
+ const to = getString(args, "to");
1839
+ if (!to) return missingParam("to");
1840
+ const node = dsl.parse(input, from);
1841
+ const compiled = dsl.compile(input, from);
1842
+ let rendered = null;
1843
+ const renderer = await this.getRenderer(descriptor);
1844
+ if (renderer) {
1845
+ try {
1846
+ rendered = typeof renderer === "function" ? renderer(node, to) : renderer.render(node, to);
1847
+ } catch {
1848
+ }
1849
+ }
1850
+ const roles = {};
1851
+ for (const [key, value] of node.roles) {
1852
+ roles[key] = value;
1853
+ }
1854
+ return jsonResponse({
1855
+ input: { [descriptor.inputLabel]: input, language: from },
1856
+ ...rendered != null && { rendered: { text: rendered, language: to } },
1857
+ semantic: { action: node.action, roles },
1858
+ ...compiled.ok && compiled.code != null && { compiled: compiled.code }
1859
+ });
1860
+ }
1861
+ };
1862
+ function generateToolDefinitions(desc) {
1863
+ const tools = [];
1864
+ const ops = desc.tools ?? ["parse", "compile", "validate", "translate"];
1865
+ const langList = desc.languages.join(", ");
1866
+ for (const op of ops) {
1867
+ switch (op) {
1868
+ case "parse":
1869
+ tools.push({
1870
+ name: `parse_${desc.name}`,
1871
+ description: `Parse a ${desc.description} input into a semantic representation. Supports: ${langList}.`,
1872
+ inputSchema: {
1873
+ type: "object",
1874
+ properties: {
1875
+ [desc.inputLabel]: {
1876
+ type: "string",
1877
+ description: desc.inputDescription
1878
+ },
1879
+ language: {
1880
+ type: "string",
1881
+ description: `Language code: ${langList}`,
1882
+ default: "en"
1883
+ }
1884
+ },
1885
+ required: [desc.inputLabel]
1886
+ }
1887
+ });
1888
+ break;
1889
+ case "compile":
1890
+ tools.push({
1891
+ name: `compile_${desc.name}`,
1892
+ description: `Compile a ${desc.description} input to target code. Supports: ${langList}.`,
1893
+ inputSchema: {
1894
+ type: "object",
1895
+ properties: {
1896
+ [desc.inputLabel]: {
1897
+ type: "string",
1898
+ description: desc.inputDescription
1899
+ },
1900
+ language: {
1901
+ type: "string",
1902
+ description: `Language code: ${langList}`,
1903
+ default: "en"
1904
+ }
1905
+ },
1906
+ required: [desc.inputLabel]
1907
+ }
1908
+ });
1909
+ break;
1910
+ case "validate":
1911
+ tools.push({
1912
+ name: `validate_${desc.name}`,
1913
+ description: `Validate ${desc.description} syntax. Returns whether it parses successfully and any errors. Supports: ${langList}.`,
1914
+ inputSchema: {
1915
+ type: "object",
1916
+ properties: {
1917
+ [desc.inputLabel]: {
1918
+ type: "string",
1919
+ description: `${desc.inputDescription} to validate`
1920
+ },
1921
+ language: {
1922
+ type: "string",
1923
+ description: `Language code: ${langList}`,
1924
+ default: "en"
1925
+ }
1926
+ },
1927
+ required: [desc.inputLabel]
1928
+ }
1929
+ });
1930
+ break;
1931
+ case "translate":
1932
+ tools.push({
1933
+ name: `translate_${desc.name}`,
1934
+ description: `Translate ${desc.description} input between natural languages. Parses in source language and renders in target language.`,
1935
+ inputSchema: {
1936
+ type: "object",
1937
+ properties: {
1938
+ [desc.inputLabel]: {
1939
+ type: "string",
1940
+ description: `${desc.inputDescription} to translate`
1941
+ },
1942
+ from: {
1943
+ type: "string",
1944
+ description: `Source language code: ${langList}`
1945
+ },
1946
+ to: {
1947
+ type: "string",
1948
+ description: `Target language code: ${langList}`
1949
+ }
1950
+ },
1951
+ required: [desc.inputLabel, "from", "to"]
1952
+ }
1953
+ });
1954
+ break;
1955
+ }
1956
+ }
1957
+ return tools;
1958
+ }
1959
+ function parseToolName(name) {
1960
+ const match = name.match(/^(parse|compile|validate|translate)_(.+)$/);
1961
+ if (!match) return null;
1962
+ return { operation: match[1], domain: match[2] };
1963
+ }
1964
+ function getString(args, name, defaultValue = "") {
1965
+ const value = args[name];
1966
+ return typeof value === "string" ? value : defaultValue;
1967
+ }
1968
+ function jsonResponse(data, isError) {
1969
+ return {
1970
+ content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
1971
+ ...isError && { isError: true }
1972
+ };
1973
+ }
1974
+ function missingParam(param) {
1975
+ return jsonResponse({ error: `Missing required parameter: ${param}` }, true);
1976
+ }
1977
+
1978
+ // src/api/dispatcher.ts
1979
+ var COMMENT_PATTERN = /^\s*(\/\/|--|#)/;
1980
+ function isSkippableLine(line) {
1981
+ const trimmed = line.trim();
1982
+ return trimmed === "" || COMMENT_PATTERN.test(trimmed);
1983
+ }
1984
+ var CrossDomainDispatcher = class {
1985
+ constructor(registry, options) {
1986
+ this.registry = registry;
1987
+ this.minConfidence = options?.minConfidence ?? 0.5;
1988
+ this.priority = options?.priority ?? [];
1989
+ }
1990
+ /**
1991
+ * Auto-detect which domain handles the input.
1992
+ * Tries all registered domains and returns the highest-confidence match.
1993
+ * Returns null if no domain matches above minConfidence.
1994
+ */
1995
+ async detect(input, language = "en") {
1996
+ const domainNames = this.registry.getDomainNames();
1997
+ if (domainNames.length === 0) return null;
1998
+ const candidates = [];
1999
+ for (const name of domainNames) {
2000
+ const dsl = await this.registry.getDSLForDomain(name);
2001
+ if (!dsl) continue;
2002
+ try {
2003
+ const { node, confidence } = dsl.parseWithConfidence(input, language);
2004
+ if (confidence >= this.minConfidence) {
2005
+ candidates.push({ domain: name, node, confidence, dsl });
2006
+ }
2007
+ } catch {
2008
+ }
2009
+ }
2010
+ if (candidates.length === 0) return null;
2011
+ candidates.sort((a, b) => {
2012
+ if (b.confidence !== a.confidence) return b.confidence - a.confidence;
2013
+ return this.getPriorityIndex(a.domain) - this.getPriorityIndex(b.domain);
2014
+ });
2015
+ return candidates[0];
2016
+ }
2017
+ /**
2018
+ * Parse multi-line input where each line may belong to a different domain.
2019
+ * Empty lines and comment lines (starting with //, --, or #) are skipped.
2020
+ */
2021
+ async parseComposite(input, language = "en") {
2022
+ const lines = input.split("\n");
2023
+ const statements = [];
2024
+ const errors = [];
2025
+ for (let i = 0; i < lines.length; i++) {
2026
+ const line = lines[i];
2027
+ if (isSkippableLine(line)) continue;
2028
+ const trimmed = line.trim();
2029
+ const result = await this.detect(trimmed, language);
2030
+ if (result) {
2031
+ statements.push({
2032
+ line: i + 1,
2033
+ input: trimmed,
2034
+ domain: result.domain,
2035
+ node: result.node,
2036
+ confidence: result.confidence
2037
+ });
2038
+ } else {
2039
+ errors.push({
2040
+ line: i + 1,
2041
+ input: trimmed,
2042
+ message: "No domain matched this input"
2043
+ });
2044
+ }
2045
+ }
2046
+ return { statements, errors };
2047
+ }
2048
+ /**
2049
+ * Compile input using auto-detected domain.
2050
+ * Returns the compilation result with the domain name.
2051
+ */
2052
+ async compile(input, language = "en") {
2053
+ const detected = await this.detect(input, language);
2054
+ if (!detected) return null;
2055
+ const result = detected.dsl.compile(input, language);
2056
+ return { ...result, domain: detected.domain };
2057
+ }
2058
+ /**
2059
+ * Validate input against all domains, returning the best match.
2060
+ */
2061
+ async validate(input, language = "en") {
2062
+ const detected = await this.detect(input, language);
2063
+ if (!detected) {
2064
+ return { valid: false, errors: ["No domain matched this input"] };
2065
+ }
2066
+ const result = detected.dsl.validate(input, language);
2067
+ return { ...result, domain: detected.domain };
2068
+ }
2069
+ // ---------------------------------------------------------------------------
2070
+ // Private
2071
+ // ---------------------------------------------------------------------------
2072
+ getPriorityIndex(domain) {
2073
+ const idx = this.priority.indexOf(domain);
2074
+ return idx === -1 ? this.priority.length : idx;
2075
+ }
2076
+ };
2077
+ export {
2078
+ CrossDomainDispatcher,
2079
+ DomainRegistry,
2080
+ createMultilingualDSL
2081
+ };
2082
+ //# sourceMappingURL=index.js.map