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