@lokascript/framework 4.6.0 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -1
- package/dist/api/create-dsl.d.ts +13 -0
- 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 +1756 -710
- 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 +1047 -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 +56 -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 +48 -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 +583 -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 +1979 -907
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1961 -906
- 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 +632 -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 +163 -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 +86 -0
- package/src/api/create-dsl.ts +141 -18
- 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 +181 -0
- package/src/core/matching/stock-reader.ts +186 -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 +48 -0
- package/src/generation/index.ts +2 -0
- package/src/generation/pattern-render.ts +378 -0
- package/src/generation/schema-patterns.test.ts +99 -0
- package/src/generation/schema-patterns.ts +962 -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 +118 -0
- package/src/testing/gate/round-trip.ts +438 -0
|
@@ -1,24 +1,3 @@
|
|
|
1
|
-
// src/core/types.ts
|
|
2
|
-
import {
|
|
3
|
-
createLiteral,
|
|
4
|
-
createSelector,
|
|
5
|
-
createReference,
|
|
6
|
-
createPropertyPath,
|
|
7
|
-
createExpression,
|
|
8
|
-
createFlag,
|
|
9
|
-
createCommandNode,
|
|
10
|
-
createEventHandlerNode,
|
|
11
|
-
createConditionalNode,
|
|
12
|
-
createCompoundNode,
|
|
13
|
-
createLoopNode,
|
|
14
|
-
createTryNode,
|
|
15
|
-
createAsyncNode,
|
|
16
|
-
createMatchNode,
|
|
17
|
-
extractValue,
|
|
18
|
-
extractRoleValue,
|
|
19
|
-
getRoleValue
|
|
20
|
-
} from "@lokascript/intent";
|
|
21
|
-
|
|
22
1
|
// src/core/logger.ts
|
|
23
2
|
var DebugLogger = class {
|
|
24
3
|
constructor(namespace) {
|
|
@@ -86,6 +65,27 @@ function createLogger(namespace) {
|
|
|
86
65
|
return new DebugLogger(namespace);
|
|
87
66
|
}
|
|
88
67
|
|
|
68
|
+
// src/core/types.ts
|
|
69
|
+
import {
|
|
70
|
+
createLiteral,
|
|
71
|
+
createSelector,
|
|
72
|
+
createReference,
|
|
73
|
+
createPropertyPath,
|
|
74
|
+
createExpression,
|
|
75
|
+
createFlag,
|
|
76
|
+
createCommandNode,
|
|
77
|
+
createEventHandlerNode,
|
|
78
|
+
createConditionalNode,
|
|
79
|
+
createCompoundNode,
|
|
80
|
+
createLoopNode,
|
|
81
|
+
createTryNode,
|
|
82
|
+
createAsyncNode,
|
|
83
|
+
createMatchNode,
|
|
84
|
+
extractValue,
|
|
85
|
+
extractRoleValue,
|
|
86
|
+
getRoleValue
|
|
87
|
+
} from "@lokascript/intent";
|
|
88
|
+
|
|
89
89
|
// src/core/pattern-matching/utils/type-validation.ts
|
|
90
90
|
function isTypeCompatible(actualType, expectedTypes) {
|
|
91
91
|
if (!expectedTypes || expectedTypes.length === 0) {
|
|
@@ -150,10 +150,535 @@ function getAllPossessiveKeywords(profile) {
|
|
|
150
150
|
return profile?.possessive?.keywords ?? {};
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
-
// src/core/
|
|
153
|
+
// src/core/matching/stock-reading.ts
|
|
154
154
|
function isValidReference(_value) {
|
|
155
155
|
return false;
|
|
156
156
|
}
|
|
157
|
+
function safeToLowerCase(value) {
|
|
158
|
+
if (typeof value === "string") {
|
|
159
|
+
return value.toLowerCase();
|
|
160
|
+
}
|
|
161
|
+
if (value === null || value === void 0) {
|
|
162
|
+
return "";
|
|
163
|
+
}
|
|
164
|
+
return String(value).toLowerCase();
|
|
165
|
+
}
|
|
166
|
+
function readRoleValue(tokens, patternToken, captured, profile) {
|
|
167
|
+
skipNoiseWords(tokens);
|
|
168
|
+
const token = tokens.peek();
|
|
169
|
+
if (!token) {
|
|
170
|
+
return patternToken.optional || false;
|
|
171
|
+
}
|
|
172
|
+
const possessiveValue = tryMatchPossessiveExpression(tokens, profile);
|
|
173
|
+
if (possessiveValue) {
|
|
174
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
175
|
+
if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
|
|
176
|
+
return patternToken.optional || false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
captured.set(patternToken.role, possessiveValue);
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
const methodCallValue = tryMatchMethodCallExpression(tokens);
|
|
183
|
+
if (methodCallValue) {
|
|
184
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
185
|
+
if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
|
|
186
|
+
return patternToken.optional || false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
captured.set(patternToken.role, methodCallValue);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
const possessiveSelectorValue = tryMatchPossessiveSelectorExpression(tokens);
|
|
193
|
+
if (possessiveSelectorValue) {
|
|
194
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
195
|
+
if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
|
|
196
|
+
return patternToken.optional || false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
captured.set(patternToken.role, possessiveSelectorValue);
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
const propertyAccessValue = tryMatchPropertyAccessExpression(tokens);
|
|
203
|
+
if (propertyAccessValue) {
|
|
204
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
205
|
+
if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
|
|
206
|
+
return patternToken.optional || false;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
captured.set(patternToken.role, propertyAccessValue);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
const selectorPropertyValue = tryMatchSelectorPropertyExpression(tokens);
|
|
213
|
+
if (selectorPropertyValue) {
|
|
214
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
215
|
+
if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
|
|
216
|
+
return patternToken.optional || false;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
captured.set(patternToken.role, selectorPropertyValue);
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
const value = tokenToSemanticValue(token);
|
|
223
|
+
if (!value) {
|
|
224
|
+
return patternToken.optional || false;
|
|
225
|
+
}
|
|
226
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
227
|
+
if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
|
|
228
|
+
return patternToken.optional || false;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
captured.set(patternToken.role, value);
|
|
232
|
+
tokens.advance();
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
function tryMatchPossessiveExpression(tokens, profile) {
|
|
236
|
+
const token = tokens.peek();
|
|
237
|
+
if (!token) return null;
|
|
238
|
+
if (!profile) return null;
|
|
239
|
+
const tokenValue = token.normalized || token.value;
|
|
240
|
+
const tokenLower = safeToLowerCase(tokenValue);
|
|
241
|
+
const baseRef = getPossessiveReference(profile, tokenLower);
|
|
242
|
+
if (!baseRef) return null;
|
|
243
|
+
const mark = tokens.mark();
|
|
244
|
+
tokens.advance();
|
|
245
|
+
const propertyToken = tokens.peek();
|
|
246
|
+
if (!propertyToken) {
|
|
247
|
+
tokens.reset(mark);
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
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)) {
|
|
251
|
+
tokens.advance();
|
|
252
|
+
let propertyName = propertyToken.value;
|
|
253
|
+
if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
|
|
254
|
+
propertyName = propertyName.substring(1);
|
|
255
|
+
}
|
|
256
|
+
let chainedProps = propertyName;
|
|
257
|
+
while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
|
|
258
|
+
chainedProps += tokens.peek().value;
|
|
259
|
+
tokens.advance();
|
|
260
|
+
}
|
|
261
|
+
const nextPeek = tokens.peek();
|
|
262
|
+
if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
|
|
263
|
+
chainedProps += nextPeek.value;
|
|
264
|
+
tokens.advance();
|
|
265
|
+
}
|
|
266
|
+
return createPropertyPath(createReference(baseRef), chainedProps);
|
|
267
|
+
}
|
|
268
|
+
tokens.reset(mark);
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
function isStructuralKeyword(value) {
|
|
272
|
+
const structural = /* @__PURE__ */ new Set([
|
|
273
|
+
// Prepositions
|
|
274
|
+
"into",
|
|
275
|
+
"in",
|
|
276
|
+
"to",
|
|
277
|
+
"from",
|
|
278
|
+
"at",
|
|
279
|
+
"by",
|
|
280
|
+
"with",
|
|
281
|
+
"without",
|
|
282
|
+
"before",
|
|
283
|
+
"after",
|
|
284
|
+
"of",
|
|
285
|
+
"as",
|
|
286
|
+
"on",
|
|
287
|
+
// Control flow
|
|
288
|
+
"then",
|
|
289
|
+
"end",
|
|
290
|
+
"else",
|
|
291
|
+
"if",
|
|
292
|
+
"repeat",
|
|
293
|
+
"while",
|
|
294
|
+
"for",
|
|
295
|
+
// Commands (shouldn't be property names)
|
|
296
|
+
"toggle",
|
|
297
|
+
"add",
|
|
298
|
+
"remove",
|
|
299
|
+
"put",
|
|
300
|
+
"set",
|
|
301
|
+
"show",
|
|
302
|
+
"hide",
|
|
303
|
+
"increment",
|
|
304
|
+
"decrement",
|
|
305
|
+
"send",
|
|
306
|
+
"trigger",
|
|
307
|
+
"call"
|
|
308
|
+
]);
|
|
309
|
+
return structural.has(value.toLowerCase());
|
|
310
|
+
}
|
|
311
|
+
function tryMatchMethodCallExpression(tokens) {
|
|
312
|
+
const token = tokens.peek();
|
|
313
|
+
if (!token || token.kind !== "selector") return null;
|
|
314
|
+
const mark = tokens.mark();
|
|
315
|
+
tokens.advance();
|
|
316
|
+
const dotToken = tokens.peek();
|
|
317
|
+
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
318
|
+
tokens.reset(mark);
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
tokens.advance();
|
|
322
|
+
const methodToken = tokens.peek();
|
|
323
|
+
if (!methodToken || methodToken.kind !== "identifier") {
|
|
324
|
+
tokens.reset(mark);
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
tokens.advance();
|
|
328
|
+
const openParen = tokens.peek();
|
|
329
|
+
if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
|
|
330
|
+
tokens.reset(mark);
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
tokens.advance();
|
|
334
|
+
const args = [];
|
|
335
|
+
while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
|
|
336
|
+
const argToken = tokens.peek();
|
|
337
|
+
if (!argToken) break;
|
|
338
|
+
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
339
|
+
tokens.advance();
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
343
|
+
tokens.advance();
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
args.push(argToken.value);
|
|
347
|
+
tokens.advance();
|
|
348
|
+
}
|
|
349
|
+
const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
|
|
350
|
+
return {
|
|
351
|
+
type: "expression",
|
|
352
|
+
raw: methodCall
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function tryMatchPropertyAccessExpression(tokens) {
|
|
356
|
+
const token = tokens.peek();
|
|
357
|
+
if (!token) return null;
|
|
358
|
+
if (token.kind !== "identifier" && token.kind !== "keyword") return null;
|
|
359
|
+
const mark = tokens.mark();
|
|
360
|
+
tokens.advance();
|
|
361
|
+
const dotToken = tokens.peek();
|
|
362
|
+
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
363
|
+
tokens.reset(mark);
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
tokens.advance();
|
|
367
|
+
const propertyToken = tokens.peek();
|
|
368
|
+
if (!propertyToken || propertyToken.kind !== "identifier") {
|
|
369
|
+
tokens.reset(mark);
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
tokens.advance();
|
|
373
|
+
let chain = `${token.value}.${propertyToken.value}`;
|
|
374
|
+
let depth = 1;
|
|
375
|
+
while (!tokens.isAtEnd() && depth < MAX_PROPERTY_DEPTH) {
|
|
376
|
+
const nextDot = tokens.peek();
|
|
377
|
+
if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
tokens.advance();
|
|
381
|
+
const nextProp = tokens.peek();
|
|
382
|
+
if (!nextProp || nextProp.kind !== "identifier") {
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
tokens.advance();
|
|
386
|
+
chain += `.${nextProp.value}`;
|
|
387
|
+
depth++;
|
|
388
|
+
}
|
|
389
|
+
const openParen = tokens.peek();
|
|
390
|
+
if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
|
|
391
|
+
tokens.advance();
|
|
392
|
+
const args = [];
|
|
393
|
+
let argDepth = 0;
|
|
394
|
+
while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
|
|
395
|
+
const argToken = tokens.peek();
|
|
396
|
+
if (!argToken) break;
|
|
397
|
+
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
398
|
+
if (argDepth === 0) {
|
|
399
|
+
tokens.advance();
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
argDepth--;
|
|
403
|
+
}
|
|
404
|
+
if (argToken.kind === "punctuation" && argToken.value === "(") {
|
|
405
|
+
argDepth++;
|
|
406
|
+
}
|
|
407
|
+
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
408
|
+
tokens.advance();
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
args.push(argToken.value);
|
|
412
|
+
tokens.advance();
|
|
413
|
+
}
|
|
414
|
+
const methodCall = `${chain}(${args.join(", ")})`;
|
|
415
|
+
return {
|
|
416
|
+
type: "expression",
|
|
417
|
+
raw: methodCall
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
return {
|
|
421
|
+
type: "expression",
|
|
422
|
+
raw: chain
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
function tryMatchPossessiveSelectorExpression(tokens) {
|
|
426
|
+
const token = tokens.peek();
|
|
427
|
+
if (!token || token.kind !== "selector") return null;
|
|
428
|
+
const mark = tokens.mark();
|
|
429
|
+
tokens.advance();
|
|
430
|
+
const possessiveToken = tokens.peek();
|
|
431
|
+
if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
|
|
432
|
+
tokens.reset(mark);
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
tokens.advance();
|
|
436
|
+
const propertyToken = tokens.peek();
|
|
437
|
+
if (!propertyToken) {
|
|
438
|
+
tokens.reset(mark);
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
|
|
442
|
+
tokens.reset(mark);
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
tokens.advance();
|
|
446
|
+
return createPropertyPath(createSelector(token.value), propertyToken.value);
|
|
447
|
+
}
|
|
448
|
+
function tryMatchSelectorPropertyExpression(tokens) {
|
|
449
|
+
const token = tokens.peek();
|
|
450
|
+
if (!token || token.kind !== "selector") return null;
|
|
451
|
+
if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
|
|
452
|
+
const mark = tokens.mark();
|
|
453
|
+
tokens.advance();
|
|
454
|
+
const propertyToken = tokens.peek();
|
|
455
|
+
if (!propertyToken || propertyToken.kind !== "selector") {
|
|
456
|
+
tokens.reset(mark);
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
if (!propertyToken.value.startsWith(".")) {
|
|
460
|
+
tokens.reset(mark);
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
const peek2 = tokens.peek(1);
|
|
464
|
+
if (peek2 && peek2.kind === "selector") {
|
|
465
|
+
}
|
|
466
|
+
tokens.advance();
|
|
467
|
+
const propertyName = propertyToken.value.slice(1);
|
|
468
|
+
return createPropertyPath(createSelector(token.value), propertyName);
|
|
469
|
+
}
|
|
470
|
+
var MAX_PROPERTY_DEPTH = 10;
|
|
471
|
+
var MAX_METHOD_ARGS = 20;
|
|
472
|
+
function tokenToSemanticValue(token) {
|
|
473
|
+
switch (token.kind) {
|
|
474
|
+
case "selector":
|
|
475
|
+
return createSelector(token.value);
|
|
476
|
+
case "literal":
|
|
477
|
+
return parseLiteralValue(token.value);
|
|
478
|
+
case "keyword": {
|
|
479
|
+
const tokenValue = token.normalized || token.value;
|
|
480
|
+
const lower = safeToLowerCase(tokenValue);
|
|
481
|
+
if (isValidReference(lower)) {
|
|
482
|
+
return createReference(lower);
|
|
483
|
+
}
|
|
484
|
+
return createLiteral(token.normalized || token.value);
|
|
485
|
+
}
|
|
486
|
+
case "identifier": {
|
|
487
|
+
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
488
|
+
return createReference(token.value);
|
|
489
|
+
}
|
|
490
|
+
const identLower = safeToLowerCase(token.value);
|
|
491
|
+
if (isValidReference(identLower)) {
|
|
492
|
+
return createReference(identLower);
|
|
493
|
+
}
|
|
494
|
+
return { type: "expression", raw: token.value };
|
|
495
|
+
}
|
|
496
|
+
case "url":
|
|
497
|
+
return createLiteral(token.value, "string");
|
|
498
|
+
default:
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function parseLiteralValue(value) {
|
|
503
|
+
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
504
|
+
const inner = value.slice(1, -1);
|
|
505
|
+
return createLiteral(inner, "string");
|
|
506
|
+
}
|
|
507
|
+
if (value === "true") return createLiteral(true, "boolean");
|
|
508
|
+
if (value === "false") return createLiteral(false, "boolean");
|
|
509
|
+
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
510
|
+
if (durationMatch) {
|
|
511
|
+
const num2 = parseFloat(durationMatch[1]);
|
|
512
|
+
const unit = durationMatch[2];
|
|
513
|
+
if (unit) {
|
|
514
|
+
return createLiteral(value, "duration");
|
|
515
|
+
}
|
|
516
|
+
return createLiteral(num2, "number");
|
|
517
|
+
}
|
|
518
|
+
const num = parseFloat(value);
|
|
519
|
+
if (!isNaN(num)) {
|
|
520
|
+
return createLiteral(num, "number");
|
|
521
|
+
}
|
|
522
|
+
return createLiteral(value, "string");
|
|
523
|
+
}
|
|
524
|
+
var ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
525
|
+
function skipNoiseWords(tokens) {
|
|
526
|
+
const token = tokens.peek();
|
|
527
|
+
if (!token) return;
|
|
528
|
+
const tokenLower = safeToLowerCase(token.value);
|
|
529
|
+
if (ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
530
|
+
const mark = tokens.mark();
|
|
531
|
+
tokens.advance();
|
|
532
|
+
const nextToken = tokens.peek();
|
|
533
|
+
if (nextToken && nextToken.kind === "selector") {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
tokens.reset(mark);
|
|
537
|
+
}
|
|
538
|
+
if (tokenLower === "class") {
|
|
539
|
+
tokens.advance();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// src/core/matching/stock-confidence.ts
|
|
544
|
+
function scoreParts(ctx) {
|
|
545
|
+
const { pattern, captured } = ctx;
|
|
546
|
+
let score = 0;
|
|
547
|
+
let maxScore = 0;
|
|
548
|
+
const hasDefault = (role) => {
|
|
549
|
+
return pattern.extraction?.[role]?.default !== void 0;
|
|
550
|
+
};
|
|
551
|
+
for (const token of pattern.template.tokens) {
|
|
552
|
+
if (token.type === "role") {
|
|
553
|
+
maxScore += 1;
|
|
554
|
+
if (captured.has(token.role)) {
|
|
555
|
+
score += 1;
|
|
556
|
+
}
|
|
557
|
+
} else if (token.type === "group") {
|
|
558
|
+
for (const subToken of token.tokens) {
|
|
559
|
+
if (subToken.type === "role") {
|
|
560
|
+
const roleHasDefault = hasDefault(subToken.role);
|
|
561
|
+
const weight = 0.8;
|
|
562
|
+
maxScore += weight;
|
|
563
|
+
if (captured.has(subToken.role)) {
|
|
564
|
+
score += weight;
|
|
565
|
+
} else if (roleHasDefault) {
|
|
566
|
+
score += weight * 0.6;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const roleCoverage = maxScore > 0 ? score / maxScore : 1;
|
|
573
|
+
let baseConfidence = roleCoverage;
|
|
574
|
+
let stemPenalty = 0;
|
|
575
|
+
if (ctx.stemMatchCount > 0 && ctx.totalKeywordMatches > 0) {
|
|
576
|
+
stemPenalty = ctx.stemMatchCount / ctx.totalKeywordMatches * 0.15;
|
|
577
|
+
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
578
|
+
}
|
|
579
|
+
const vsoBoost = calculateVSOConfidenceBoost(pattern);
|
|
580
|
+
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
581
|
+
const prepositionAdjustment = arabicPrepositionDisambiguation(pattern, captured);
|
|
582
|
+
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
583
|
+
return {
|
|
584
|
+
roleCoverage,
|
|
585
|
+
stemPenalty,
|
|
586
|
+
languageBoost: vsoBoost,
|
|
587
|
+
languageAdjustment: prepositionAdjustment,
|
|
588
|
+
final: baseConfidence
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
function calculateVSOConfidenceBoost(pattern) {
|
|
592
|
+
if (pattern.language !== "ar") {
|
|
593
|
+
return 0;
|
|
594
|
+
}
|
|
595
|
+
const firstToken = pattern.template.tokens[0];
|
|
596
|
+
if (!firstToken || firstToken.type !== "literal") {
|
|
597
|
+
return 0;
|
|
598
|
+
}
|
|
599
|
+
const ARABIC_VERBS = /* @__PURE__ */ new Set([
|
|
600
|
+
"\u0628\u062F\u0644",
|
|
601
|
+
"\u063A\u064A\u0631",
|
|
602
|
+
"\u0623\u0636\u0641",
|
|
603
|
+
"\u0623\u0632\u0644",
|
|
604
|
+
"\u0636\u0639",
|
|
605
|
+
"\u0627\u062C\u0639\u0644",
|
|
606
|
+
"\u0639\u064A\u0646",
|
|
607
|
+
"\u0632\u062F",
|
|
608
|
+
"\u0627\u0646\u0642\u0635",
|
|
609
|
+
"\u0633\u062C\u0644",
|
|
610
|
+
"\u0623\u0638\u0647\u0631",
|
|
611
|
+
"\u0623\u062E\u0641",
|
|
612
|
+
"\u0634\u063A\u0644",
|
|
613
|
+
"\u0623\u0631\u0633\u0644",
|
|
614
|
+
"\u0631\u0643\u0632",
|
|
615
|
+
"\u0634\u0648\u0634",
|
|
616
|
+
"\u062A\u0648\u0642\u0641",
|
|
617
|
+
"\u0627\u0646\u0633\u062E",
|
|
618
|
+
"\u0627\u062D\u0630\u0641",
|
|
619
|
+
"\u0627\u0635\u0646\u0639",
|
|
620
|
+
"\u0627\u0646\u062A\u0638\u0631",
|
|
621
|
+
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
622
|
+
"\u0623\u0648"
|
|
623
|
+
]);
|
|
624
|
+
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
625
|
+
return 0.15;
|
|
626
|
+
}
|
|
627
|
+
if (firstToken.alternatives) {
|
|
628
|
+
for (const alt of firstToken.alternatives) {
|
|
629
|
+
if (ARABIC_VERBS.has(alt)) {
|
|
630
|
+
return 0.15;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return 0;
|
|
635
|
+
}
|
|
636
|
+
function arabicPrepositionDisambiguation(pattern, captured) {
|
|
637
|
+
if (pattern.language !== "ar") {
|
|
638
|
+
return 0;
|
|
639
|
+
}
|
|
640
|
+
let adjustment = 0;
|
|
641
|
+
const PREFERRED_PREPOSITIONS = {
|
|
642
|
+
patient: ["\u0639\u0644\u0649"],
|
|
643
|
+
// element selectors prefer على (on/upon)
|
|
644
|
+
destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
645
|
+
// destination prefers إلى (to)
|
|
646
|
+
source: ["\u0645\u0646"],
|
|
647
|
+
// source prefers من (from)
|
|
648
|
+
agent: ["\u0645\u0646"],
|
|
649
|
+
// agent/by prefers من (from/by)
|
|
650
|
+
manner: ["\u0628"],
|
|
651
|
+
// manner prefers ب (with/by)
|
|
652
|
+
style: ["\u0628"],
|
|
653
|
+
// style prefers ب (with)
|
|
654
|
+
goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
655
|
+
// target state prefers إلى (to)
|
|
656
|
+
method: ["\u0628"]
|
|
657
|
+
// method prefers ب (with/by)
|
|
658
|
+
};
|
|
659
|
+
for (const [role, value] of captured.entries()) {
|
|
660
|
+
const preferred = PREFERRED_PREPOSITIONS[role];
|
|
661
|
+
if (!preferred || preferred.length === 0) {
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
const metadata = "metadata" in value ? value.metadata : void 0;
|
|
665
|
+
if (metadata && typeof metadata.prepositionValue === "string") {
|
|
666
|
+
const usedPreposition = metadata.prepositionValue;
|
|
667
|
+
if (preferred.includes(usedPreposition)) {
|
|
668
|
+
adjustment += 0.1;
|
|
669
|
+
} else {
|
|
670
|
+
adjustment -= 0.1;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return Math.max(-0.1, Math.min(0.1, adjustment));
|
|
675
|
+
}
|
|
676
|
+
var stockConfidenceModel = {
|
|
677
|
+
calculate: (ctx) => scoreParts(ctx).final,
|
|
678
|
+
calculateWithBreakdown: (ctx) => scoreParts(ctx)
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
// src/core/pattern-matching/pattern-matcher.ts
|
|
157
682
|
var _PatternMatcher = class _PatternMatcher {
|
|
158
683
|
constructor() {
|
|
159
684
|
/** Debug logger */
|
|
@@ -165,19 +690,6 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
165
690
|
this.stemMatchCount = 0;
|
|
166
691
|
this.totalKeywordMatches = 0;
|
|
167
692
|
}
|
|
168
|
-
/**
|
|
169
|
-
* Safely convert a value to lowercase string.
|
|
170
|
-
* Provides protection against non-string values at runtime.
|
|
171
|
-
*/
|
|
172
|
-
safeToLowerCase(value) {
|
|
173
|
-
if (typeof value === "string") {
|
|
174
|
-
return value.toLowerCase();
|
|
175
|
-
}
|
|
176
|
-
if (value === null || value === void 0) {
|
|
177
|
-
return "";
|
|
178
|
-
}
|
|
179
|
-
return String(value).toLowerCase();
|
|
180
|
-
}
|
|
181
693
|
/**
|
|
182
694
|
* Try to match a single pattern against the token stream.
|
|
183
695
|
* Returns the match result or null if no match.
|
|
@@ -226,7 +738,12 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
226
738
|
tokens.reset(mark);
|
|
227
739
|
return null;
|
|
228
740
|
}
|
|
229
|
-
const confidence =
|
|
741
|
+
const confidence = stockConfidenceModel.calculate({
|
|
742
|
+
pattern,
|
|
743
|
+
captured,
|
|
744
|
+
stemMatchCount: this.stemMatchCount,
|
|
745
|
+
totalKeywordMatches: this.totalKeywordMatches
|
|
746
|
+
});
|
|
230
747
|
this.applyExtractionRules(pattern, captured);
|
|
231
748
|
return {
|
|
232
749
|
pattern,
|
|
@@ -411,7 +928,7 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
411
928
|
case "literal":
|
|
412
929
|
return this.matchLiteralToken(tokens, patternToken);
|
|
413
930
|
case "role":
|
|
414
|
-
return
|
|
931
|
+
return readRoleValue(tokens, patternToken, captured, this.currentProfile);
|
|
415
932
|
case "group":
|
|
416
933
|
return this.matchGroupToken(tokens, patternToken, captured);
|
|
417
934
|
default:
|
|
@@ -468,372 +985,6 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
468
985
|
}
|
|
469
986
|
return false;
|
|
470
987
|
}
|
|
471
|
-
/**
|
|
472
|
-
* Match a role pattern token (captures a semantic value).
|
|
473
|
-
* Handles multi-token expressions like:
|
|
474
|
-
* - 'my value' (possessive keyword + property)
|
|
475
|
-
* - '#dialog.showModal()' (method call)
|
|
476
|
-
* - "#element's *opacity" (possessive selector + property)
|
|
477
|
-
*/
|
|
478
|
-
matchRoleToken(tokens, patternToken, captured) {
|
|
479
|
-
this.logger.debug(" >>> matchRoleToken ENTRY: capturing role", patternToken.role);
|
|
480
|
-
this.logger.debug(" >>> matchRoleToken: expected types", patternToken.expectedTypes);
|
|
481
|
-
this.logger.debug(" >>> matchRoleToken: optional?", patternToken.optional);
|
|
482
|
-
this.skipNoiseWords(tokens);
|
|
483
|
-
const token = tokens.peek();
|
|
484
|
-
this.logger.debug(
|
|
485
|
-
" >>> After skipNoiseWords, current token:",
|
|
486
|
-
token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
|
|
487
|
-
);
|
|
488
|
-
if (!token) {
|
|
489
|
-
return patternToken.optional || false;
|
|
490
|
-
}
|
|
491
|
-
const possessiveValue = this.tryMatchPossessiveExpression(tokens);
|
|
492
|
-
if (possessiveValue) {
|
|
493
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
494
|
-
if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
|
|
495
|
-
return patternToken.optional || false;
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
captured.set(patternToken.role, possessiveValue);
|
|
499
|
-
return true;
|
|
500
|
-
}
|
|
501
|
-
const methodCallValue = this.tryMatchMethodCallExpression(tokens);
|
|
502
|
-
if (methodCallValue) {
|
|
503
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
504
|
-
if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
|
|
505
|
-
return patternToken.optional || false;
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
captured.set(patternToken.role, methodCallValue);
|
|
509
|
-
return true;
|
|
510
|
-
}
|
|
511
|
-
const possessiveSelectorValue = this.tryMatchPossessiveSelectorExpression(tokens);
|
|
512
|
-
if (possessiveSelectorValue) {
|
|
513
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
514
|
-
if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
|
|
515
|
-
return patternToken.optional || false;
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
captured.set(patternToken.role, possessiveSelectorValue);
|
|
519
|
-
return true;
|
|
520
|
-
}
|
|
521
|
-
const propertyAccessValue = this.tryMatchPropertyAccessExpression(tokens);
|
|
522
|
-
if (propertyAccessValue) {
|
|
523
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
524
|
-
if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
|
|
525
|
-
return patternToken.optional || false;
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
captured.set(patternToken.role, propertyAccessValue);
|
|
529
|
-
return true;
|
|
530
|
-
}
|
|
531
|
-
const selectorPropertyValue = this.tryMatchSelectorPropertyExpression(tokens);
|
|
532
|
-
if (selectorPropertyValue) {
|
|
533
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
534
|
-
if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
|
|
535
|
-
return patternToken.optional || false;
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
captured.set(patternToken.role, selectorPropertyValue);
|
|
539
|
-
return true;
|
|
540
|
-
}
|
|
541
|
-
this.logger.debug(
|
|
542
|
-
" >>> Trying tokenToSemanticValue for token:",
|
|
543
|
-
token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
|
|
544
|
-
);
|
|
545
|
-
const value = this.tokenToSemanticValue(token);
|
|
546
|
-
this.logger.debug(
|
|
547
|
-
" >>> tokenToSemanticValue returned:",
|
|
548
|
-
value ? JSON.stringify(value) : "null"
|
|
549
|
-
);
|
|
550
|
-
if (!value) {
|
|
551
|
-
return patternToken.optional || false;
|
|
552
|
-
}
|
|
553
|
-
this.logger.debug(
|
|
554
|
-
" >>> Validating type:",
|
|
555
|
-
value.type,
|
|
556
|
-
"against expected:",
|
|
557
|
-
patternToken.expectedTypes
|
|
558
|
-
);
|
|
559
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
560
|
-
if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
|
|
561
|
-
this.logger.debug(" >>> TYPE MISMATCH - returning", patternToken.optional || false);
|
|
562
|
-
return patternToken.optional || false;
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
this.logger.debug(" >>> Type validation PASSED");
|
|
566
|
-
captured.set(patternToken.role, value);
|
|
567
|
-
tokens.advance();
|
|
568
|
-
return true;
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* Try to match a possessive expression like 'my value' or 'its innerHTML'.
|
|
572
|
-
* Returns the PropertyPathValue if matched, or null if not.
|
|
573
|
-
*/
|
|
574
|
-
tryMatchPossessiveExpression(tokens) {
|
|
575
|
-
const token = tokens.peek();
|
|
576
|
-
if (!token) return null;
|
|
577
|
-
if (!this.currentProfile) return null;
|
|
578
|
-
const tokenValue = token.normalized || token.value;
|
|
579
|
-
const tokenLower = this.safeToLowerCase(tokenValue);
|
|
580
|
-
const baseRef = getPossessiveReference(this.currentProfile, tokenLower);
|
|
581
|
-
if (!baseRef) return null;
|
|
582
|
-
const mark = tokens.mark();
|
|
583
|
-
tokens.advance();
|
|
584
|
-
const propertyToken = tokens.peek();
|
|
585
|
-
if (!propertyToken) {
|
|
586
|
-
tokens.reset(mark);
|
|
587
|
-
return null;
|
|
588
|
-
}
|
|
589
|
-
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)) {
|
|
590
|
-
tokens.advance();
|
|
591
|
-
let propertyName = propertyToken.value;
|
|
592
|
-
if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
|
|
593
|
-
propertyName = propertyName.substring(1);
|
|
594
|
-
}
|
|
595
|
-
let chainedProps = propertyName;
|
|
596
|
-
while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
|
|
597
|
-
chainedProps += tokens.peek().value;
|
|
598
|
-
tokens.advance();
|
|
599
|
-
}
|
|
600
|
-
const nextPeek = tokens.peek();
|
|
601
|
-
if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
|
|
602
|
-
chainedProps += nextPeek.value;
|
|
603
|
-
tokens.advance();
|
|
604
|
-
}
|
|
605
|
-
return createPropertyPath(createReference(baseRef), chainedProps);
|
|
606
|
-
}
|
|
607
|
-
tokens.reset(mark);
|
|
608
|
-
return null;
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* Check if a keyword is a structural keyword (preposition, control flow, etc.)
|
|
612
|
-
* that shouldn't be consumed as a property name.
|
|
613
|
-
*/
|
|
614
|
-
isStructuralKeyword(value) {
|
|
615
|
-
const structural = /* @__PURE__ */ new Set([
|
|
616
|
-
// Prepositions
|
|
617
|
-
"into",
|
|
618
|
-
"in",
|
|
619
|
-
"to",
|
|
620
|
-
"from",
|
|
621
|
-
"at",
|
|
622
|
-
"by",
|
|
623
|
-
"with",
|
|
624
|
-
"without",
|
|
625
|
-
"before",
|
|
626
|
-
"after",
|
|
627
|
-
"of",
|
|
628
|
-
"as",
|
|
629
|
-
"on",
|
|
630
|
-
// Control flow
|
|
631
|
-
"then",
|
|
632
|
-
"end",
|
|
633
|
-
"else",
|
|
634
|
-
"if",
|
|
635
|
-
"repeat",
|
|
636
|
-
"while",
|
|
637
|
-
"for",
|
|
638
|
-
// Commands (shouldn't be property names)
|
|
639
|
-
"toggle",
|
|
640
|
-
"add",
|
|
641
|
-
"remove",
|
|
642
|
-
"put",
|
|
643
|
-
"set",
|
|
644
|
-
"show",
|
|
645
|
-
"hide",
|
|
646
|
-
"increment",
|
|
647
|
-
"decrement",
|
|
648
|
-
"send",
|
|
649
|
-
"trigger",
|
|
650
|
-
"call"
|
|
651
|
-
]);
|
|
652
|
-
return structural.has(value.toLowerCase());
|
|
653
|
-
}
|
|
654
|
-
/**
|
|
655
|
-
* Try to match a method call expression like '#dialog.showModal()'.
|
|
656
|
-
* Pattern: selector + '.' + identifier + '(' + [args] + ')'
|
|
657
|
-
* Returns an expression value if matched, or null if not.
|
|
658
|
-
*/
|
|
659
|
-
tryMatchMethodCallExpression(tokens) {
|
|
660
|
-
const token = tokens.peek();
|
|
661
|
-
if (!token || token.kind !== "selector") return null;
|
|
662
|
-
const mark = tokens.mark();
|
|
663
|
-
tokens.advance();
|
|
664
|
-
const dotToken = tokens.peek();
|
|
665
|
-
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
666
|
-
tokens.reset(mark);
|
|
667
|
-
return null;
|
|
668
|
-
}
|
|
669
|
-
tokens.advance();
|
|
670
|
-
const methodToken = tokens.peek();
|
|
671
|
-
if (!methodToken || methodToken.kind !== "identifier") {
|
|
672
|
-
tokens.reset(mark);
|
|
673
|
-
return null;
|
|
674
|
-
}
|
|
675
|
-
tokens.advance();
|
|
676
|
-
const openParen = tokens.peek();
|
|
677
|
-
if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
|
|
678
|
-
tokens.reset(mark);
|
|
679
|
-
return null;
|
|
680
|
-
}
|
|
681
|
-
tokens.advance();
|
|
682
|
-
const args = [];
|
|
683
|
-
while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
|
|
684
|
-
const argToken = tokens.peek();
|
|
685
|
-
if (!argToken) break;
|
|
686
|
-
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
687
|
-
tokens.advance();
|
|
688
|
-
break;
|
|
689
|
-
}
|
|
690
|
-
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
691
|
-
tokens.advance();
|
|
692
|
-
continue;
|
|
693
|
-
}
|
|
694
|
-
args.push(argToken.value);
|
|
695
|
-
tokens.advance();
|
|
696
|
-
}
|
|
697
|
-
const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
|
|
698
|
-
return {
|
|
699
|
-
type: "expression",
|
|
700
|
-
raw: methodCall
|
|
701
|
-
};
|
|
702
|
-
}
|
|
703
|
-
/**
|
|
704
|
-
* Try to match a property access expression like 'userData.name' or 'it.data'.
|
|
705
|
-
* Pattern: (identifier | keyword) + '.' + identifier [+ '.' + identifier ...]
|
|
706
|
-
* Returns an expression value if matched, or null if not.
|
|
707
|
-
*/
|
|
708
|
-
tryMatchPropertyAccessExpression(tokens) {
|
|
709
|
-
const token = tokens.peek();
|
|
710
|
-
if (!token) return null;
|
|
711
|
-
if (token.kind !== "identifier" && token.kind !== "keyword") return null;
|
|
712
|
-
const mark = tokens.mark();
|
|
713
|
-
tokens.advance();
|
|
714
|
-
const dotToken = tokens.peek();
|
|
715
|
-
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
716
|
-
tokens.reset(mark);
|
|
717
|
-
return null;
|
|
718
|
-
}
|
|
719
|
-
tokens.advance();
|
|
720
|
-
const propertyToken = tokens.peek();
|
|
721
|
-
if (!propertyToken || propertyToken.kind !== "identifier") {
|
|
722
|
-
tokens.reset(mark);
|
|
723
|
-
return null;
|
|
724
|
-
}
|
|
725
|
-
tokens.advance();
|
|
726
|
-
let chain = `${token.value}.${propertyToken.value}`;
|
|
727
|
-
let depth = 1;
|
|
728
|
-
while (!tokens.isAtEnd() && depth < _PatternMatcher.MAX_PROPERTY_DEPTH) {
|
|
729
|
-
const nextDot = tokens.peek();
|
|
730
|
-
if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
|
|
731
|
-
break;
|
|
732
|
-
}
|
|
733
|
-
tokens.advance();
|
|
734
|
-
const nextProp = tokens.peek();
|
|
735
|
-
if (!nextProp || nextProp.kind !== "identifier") {
|
|
736
|
-
break;
|
|
737
|
-
}
|
|
738
|
-
tokens.advance();
|
|
739
|
-
chain += `.${nextProp.value}`;
|
|
740
|
-
depth++;
|
|
741
|
-
}
|
|
742
|
-
const openParen = tokens.peek();
|
|
743
|
-
if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
|
|
744
|
-
tokens.advance();
|
|
745
|
-
const args = [];
|
|
746
|
-
let argDepth = 0;
|
|
747
|
-
while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
|
|
748
|
-
const argToken = tokens.peek();
|
|
749
|
-
if (!argToken) break;
|
|
750
|
-
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
751
|
-
if (argDepth === 0) {
|
|
752
|
-
tokens.advance();
|
|
753
|
-
break;
|
|
754
|
-
}
|
|
755
|
-
argDepth--;
|
|
756
|
-
}
|
|
757
|
-
if (argToken.kind === "punctuation" && argToken.value === "(") {
|
|
758
|
-
argDepth++;
|
|
759
|
-
}
|
|
760
|
-
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
761
|
-
tokens.advance();
|
|
762
|
-
continue;
|
|
763
|
-
}
|
|
764
|
-
args.push(argToken.value);
|
|
765
|
-
tokens.advance();
|
|
766
|
-
}
|
|
767
|
-
const methodCall = `${chain}(${args.join(", ")})`;
|
|
768
|
-
return {
|
|
769
|
-
type: "expression",
|
|
770
|
-
raw: methodCall
|
|
771
|
-
};
|
|
772
|
-
}
|
|
773
|
-
return {
|
|
774
|
-
type: "expression",
|
|
775
|
-
raw: chain
|
|
776
|
-
};
|
|
777
|
-
}
|
|
778
|
-
/**
|
|
779
|
-
* Try to match a possessive selector expression like "#element's *opacity".
|
|
780
|
-
* Pattern: selector + "'s" + (selector | identifier)
|
|
781
|
-
* Returns a property-path value if matched, or null if not.
|
|
782
|
-
*/
|
|
783
|
-
tryMatchPossessiveSelectorExpression(tokens) {
|
|
784
|
-
const token = tokens.peek();
|
|
785
|
-
if (!token || token.kind !== "selector") return null;
|
|
786
|
-
const mark = tokens.mark();
|
|
787
|
-
tokens.advance();
|
|
788
|
-
const possessiveToken = tokens.peek();
|
|
789
|
-
if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
|
|
790
|
-
tokens.reset(mark);
|
|
791
|
-
return null;
|
|
792
|
-
}
|
|
793
|
-
tokens.advance();
|
|
794
|
-
const propertyToken = tokens.peek();
|
|
795
|
-
if (!propertyToken) {
|
|
796
|
-
tokens.reset(mark);
|
|
797
|
-
return null;
|
|
798
|
-
}
|
|
799
|
-
if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
|
|
800
|
-
tokens.reset(mark);
|
|
801
|
-
return null;
|
|
802
|
-
}
|
|
803
|
-
tokens.advance();
|
|
804
|
-
return createPropertyPath(createSelector(token.value), propertyToken.value);
|
|
805
|
-
}
|
|
806
|
-
/**
|
|
807
|
-
* Try to match a selector + property expression like "#output.innerText".
|
|
808
|
-
* This handles cases where the tokenizer produces two selector tokens:
|
|
809
|
-
* - #output (id selector)
|
|
810
|
-
* - .innerText (looks like class selector, but is actually property)
|
|
811
|
-
*
|
|
812
|
-
* Pattern: id-selector + class-selector-that-is-actually-property
|
|
813
|
-
* Returns a property-path value if matched, or null if not.
|
|
814
|
-
*/
|
|
815
|
-
tryMatchSelectorPropertyExpression(tokens) {
|
|
816
|
-
const token = tokens.peek();
|
|
817
|
-
if (!token || token.kind !== "selector") return null;
|
|
818
|
-
if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
|
|
819
|
-
const mark = tokens.mark();
|
|
820
|
-
tokens.advance();
|
|
821
|
-
const propertyToken = tokens.peek();
|
|
822
|
-
if (!propertyToken || propertyToken.kind !== "selector") {
|
|
823
|
-
tokens.reset(mark);
|
|
824
|
-
return null;
|
|
825
|
-
}
|
|
826
|
-
if (!propertyToken.value.startsWith(".")) {
|
|
827
|
-
tokens.reset(mark);
|
|
828
|
-
return null;
|
|
829
|
-
}
|
|
830
|
-
const peek2 = tokens.peek(1);
|
|
831
|
-
if (peek2 && peek2.kind === "selector") {
|
|
832
|
-
}
|
|
833
|
-
tokens.advance();
|
|
834
|
-
const propertyName = propertyToken.value.slice(1);
|
|
835
|
-
return createPropertyPath(createSelector(token.value), propertyName);
|
|
836
|
-
}
|
|
837
988
|
/**
|
|
838
989
|
* Match a group pattern token (optional sequence).
|
|
839
990
|
* When the group's leading marker isn't at the current position, scans ahead
|
|
@@ -909,7 +1060,7 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
909
1060
|
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
910
1061
|
return "stem";
|
|
911
1062
|
}
|
|
912
|
-
if (token.kind === "keyword" &&
|
|
1063
|
+
if (token.kind === "keyword" && safeToLowerCase(token.value) === value.toLowerCase()) {
|
|
913
1064
|
return "case-insensitive";
|
|
914
1065
|
}
|
|
915
1066
|
return "none";
|
|
@@ -957,62 +1108,9 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
957
1108
|
if (token.normalized && stopMarkers.has(token.normalized.toLowerCase())) return true;
|
|
958
1109
|
return false;
|
|
959
1110
|
}
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
tokenToSemanticValue(token) {
|
|
964
|
-
switch (token.kind) {
|
|
965
|
-
case "selector":
|
|
966
|
-
return createSelector(token.value);
|
|
967
|
-
case "literal":
|
|
968
|
-
return this.parseLiteralValue(token.value);
|
|
969
|
-
case "keyword":
|
|
970
|
-
const tokenValue = token.normalized || token.value;
|
|
971
|
-
const lower = this.safeToLowerCase(tokenValue);
|
|
972
|
-
if (isValidReference(lower)) {
|
|
973
|
-
return createReference(lower);
|
|
974
|
-
}
|
|
975
|
-
return createLiteral(token.normalized || token.value);
|
|
976
|
-
case "identifier":
|
|
977
|
-
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
978
|
-
return createReference(token.value);
|
|
979
|
-
}
|
|
980
|
-
const identLower = this.safeToLowerCase(token.value);
|
|
981
|
-
if (isValidReference(identLower)) {
|
|
982
|
-
return createReference(identLower);
|
|
983
|
-
}
|
|
984
|
-
return { type: "expression", raw: token.value };
|
|
985
|
-
case "url":
|
|
986
|
-
return createLiteral(token.value, "string");
|
|
987
|
-
default:
|
|
988
|
-
return null;
|
|
989
|
-
}
|
|
990
|
-
}
|
|
991
|
-
/**
|
|
992
|
-
* Parse a literal value (string, number, boolean).
|
|
993
|
-
*/
|
|
994
|
-
parseLiteralValue(value) {
|
|
995
|
-
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
996
|
-
const inner = value.slice(1, -1);
|
|
997
|
-
return createLiteral(inner, "string");
|
|
998
|
-
}
|
|
999
|
-
if (value === "true") return createLiteral(true, "boolean");
|
|
1000
|
-
if (value === "false") return createLiteral(false, "boolean");
|
|
1001
|
-
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
1002
|
-
if (durationMatch) {
|
|
1003
|
-
const num2 = parseFloat(durationMatch[1]);
|
|
1004
|
-
const unit = durationMatch[2];
|
|
1005
|
-
if (unit) {
|
|
1006
|
-
return createLiteral(value, "duration");
|
|
1007
|
-
}
|
|
1008
|
-
return createLiteral(num2, "number");
|
|
1009
|
-
}
|
|
1010
|
-
const num = parseFloat(value);
|
|
1011
|
-
if (!isNaN(num)) {
|
|
1012
|
-
return createLiteral(num, "number");
|
|
1013
|
-
}
|
|
1014
|
-
return createLiteral(value, "string");
|
|
1015
|
-
}
|
|
1111
|
+
// ==========================================================================
|
|
1112
|
+
// Depth Limits for Expression Parsing (security hardening)
|
|
1113
|
+
// ==========================================================================
|
|
1016
1114
|
/**
|
|
1017
1115
|
* Apply extraction rules to fill in static values and defaults for missing roles.
|
|
1018
1116
|
*/
|
|
@@ -1033,190 +1131,9 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
1033
1131
|
isOptional(patternToken) {
|
|
1034
1132
|
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
1035
1133
|
}
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
* Confidence is reduced for:
|
|
1040
|
-
* - Stem matches (morphological normalization has inherent uncertainty)
|
|
1041
|
-
* - Missing optional roles (but less penalty if role has a default value)
|
|
1042
|
-
*
|
|
1043
|
-
* Confidence is increased for:
|
|
1044
|
-
* - VSO languages (Arabic) when pattern starts with a verb
|
|
1045
|
-
*/
|
|
1046
|
-
calculateConfidence(pattern, captured) {
|
|
1047
|
-
let score = 0;
|
|
1048
|
-
let maxScore = 0;
|
|
1049
|
-
const hasDefault = (role) => {
|
|
1050
|
-
return pattern.extraction?.[role]?.default !== void 0;
|
|
1051
|
-
};
|
|
1052
|
-
for (const token of pattern.template.tokens) {
|
|
1053
|
-
if (token.type === "role") {
|
|
1054
|
-
maxScore += 1;
|
|
1055
|
-
if (captured.has(token.role)) {
|
|
1056
|
-
score += 1;
|
|
1057
|
-
}
|
|
1058
|
-
} else if (token.type === "group") {
|
|
1059
|
-
for (const subToken of token.tokens) {
|
|
1060
|
-
if (subToken.type === "role") {
|
|
1061
|
-
const roleHasDefault = hasDefault(subToken.role);
|
|
1062
|
-
const weight = 0.8;
|
|
1063
|
-
maxScore += weight;
|
|
1064
|
-
if (captured.has(subToken.role)) {
|
|
1065
|
-
score += weight;
|
|
1066
|
-
} else if (roleHasDefault) {
|
|
1067
|
-
score += weight * 0.6;
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
let baseConfidence = maxScore > 0 ? score / maxScore : 1;
|
|
1074
|
-
if (this.stemMatchCount > 0 && this.totalKeywordMatches > 0) {
|
|
1075
|
-
const stemPenalty = this.stemMatchCount / this.totalKeywordMatches * 0.15;
|
|
1076
|
-
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
1077
|
-
}
|
|
1078
|
-
const vsoBoost = this.calculateVSOConfidenceBoost(pattern);
|
|
1079
|
-
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
1080
|
-
const prepositionAdjustment = this.arabicPrepositionDisambiguation(pattern, captured);
|
|
1081
|
-
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
1082
|
-
return baseConfidence;
|
|
1083
|
-
}
|
|
1084
|
-
/**
|
|
1085
|
-
* Calculate confidence boost for VSO (Verb-Subject-Object) language patterns.
|
|
1086
|
-
* Arabic naturally uses VSO word order, so patterns that start with a verb
|
|
1087
|
-
* should receive a confidence boost.
|
|
1088
|
-
*
|
|
1089
|
-
* Returns +0.15 confidence boost if:
|
|
1090
|
-
* - Language is Arabic ('ar')
|
|
1091
|
-
* - Pattern's first token is a verb keyword
|
|
1092
|
-
*
|
|
1093
|
-
* @param pattern The language pattern being matched
|
|
1094
|
-
* @returns Confidence boost (0 or 0.15)
|
|
1095
|
-
*/
|
|
1096
|
-
calculateVSOConfidenceBoost(pattern) {
|
|
1097
|
-
if (pattern.language !== "ar") {
|
|
1098
|
-
return 0;
|
|
1099
|
-
}
|
|
1100
|
-
const firstToken = pattern.template.tokens[0];
|
|
1101
|
-
if (!firstToken || firstToken.type !== "literal") {
|
|
1102
|
-
return 0;
|
|
1103
|
-
}
|
|
1104
|
-
const ARABIC_VERBS = /* @__PURE__ */ new Set([
|
|
1105
|
-
"\u0628\u062F\u0644",
|
|
1106
|
-
"\u063A\u064A\u0631",
|
|
1107
|
-
"\u0623\u0636\u0641",
|
|
1108
|
-
"\u0623\u0632\u0644",
|
|
1109
|
-
"\u0636\u0639",
|
|
1110
|
-
"\u0627\u062C\u0639\u0644",
|
|
1111
|
-
"\u0639\u064A\u0646",
|
|
1112
|
-
"\u0632\u062F",
|
|
1113
|
-
"\u0627\u0646\u0642\u0635",
|
|
1114
|
-
"\u0633\u062C\u0644",
|
|
1115
|
-
"\u0623\u0638\u0647\u0631",
|
|
1116
|
-
"\u0623\u062E\u0641",
|
|
1117
|
-
"\u0634\u063A\u0644",
|
|
1118
|
-
"\u0623\u0631\u0633\u0644",
|
|
1119
|
-
"\u0631\u0643\u0632",
|
|
1120
|
-
"\u0634\u0648\u0634",
|
|
1121
|
-
"\u062A\u0648\u0642\u0641",
|
|
1122
|
-
"\u0627\u0646\u0633\u062E",
|
|
1123
|
-
"\u0627\u062D\u0630\u0641",
|
|
1124
|
-
"\u0627\u0635\u0646\u0639",
|
|
1125
|
-
"\u0627\u0646\u062A\u0638\u0631",
|
|
1126
|
-
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
1127
|
-
"\u0623\u0648"
|
|
1128
|
-
]);
|
|
1129
|
-
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
1130
|
-
return 0.15;
|
|
1131
|
-
}
|
|
1132
|
-
if (firstToken.alternatives) {
|
|
1133
|
-
for (const alt of firstToken.alternatives) {
|
|
1134
|
-
if (ARABIC_VERBS.has(alt)) {
|
|
1135
|
-
return 0.15;
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
}
|
|
1139
|
-
return 0;
|
|
1140
|
-
}
|
|
1141
|
-
/**
|
|
1142
|
-
* Arabic preposition disambiguation for confidence adjustment.
|
|
1143
|
-
*
|
|
1144
|
-
* Different Arabic prepositions are more or less natural for different semantic roles:
|
|
1145
|
-
* - على (on/upon) is preferred for patient/target roles (element selectors)
|
|
1146
|
-
* - إلى (to) is preferred for destination roles
|
|
1147
|
-
* - من (from) is preferred for source roles
|
|
1148
|
-
* - في (in) is preferred for location roles
|
|
1149
|
-
*
|
|
1150
|
-
* This method analyzes the prepositions used with captured semantic roles and
|
|
1151
|
-
* adjusts confidence based on idiomaticity:
|
|
1152
|
-
* - +0.10 for highly idiomatic preposition choices
|
|
1153
|
-
* - -0.10 for less natural preposition choices
|
|
1154
|
-
*
|
|
1155
|
-
* @param pattern The language pattern being matched
|
|
1156
|
-
* @param captured The captured semantic values
|
|
1157
|
-
* @returns Confidence adjustment (-0.10 to +0.10)
|
|
1158
|
-
*/
|
|
1159
|
-
arabicPrepositionDisambiguation(pattern, captured) {
|
|
1160
|
-
if (pattern.language !== "ar") {
|
|
1161
|
-
return 0;
|
|
1162
|
-
}
|
|
1163
|
-
let adjustment = 0;
|
|
1164
|
-
const PREFERRED_PREPOSITIONS = {
|
|
1165
|
-
patient: ["\u0639\u0644\u0649"],
|
|
1166
|
-
// element selectors prefer على (on/upon)
|
|
1167
|
-
destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
1168
|
-
// destination prefers إلى (to)
|
|
1169
|
-
source: ["\u0645\u0646"],
|
|
1170
|
-
// source prefers من (from)
|
|
1171
|
-
agent: ["\u0645\u0646"],
|
|
1172
|
-
// agent/by prefers من (from/by)
|
|
1173
|
-
manner: ["\u0628"],
|
|
1174
|
-
// manner prefers ب (with/by)
|
|
1175
|
-
style: ["\u0628"],
|
|
1176
|
-
// style prefers ب (with)
|
|
1177
|
-
goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
1178
|
-
// target state prefers إلى (to)
|
|
1179
|
-
method: ["\u0628"]
|
|
1180
|
-
// method prefers ب (with/by)
|
|
1181
|
-
};
|
|
1182
|
-
for (const [role, value] of captured.entries()) {
|
|
1183
|
-
const preferred = PREFERRED_PREPOSITIONS[role];
|
|
1184
|
-
if (!preferred || preferred.length === 0) {
|
|
1185
|
-
continue;
|
|
1186
|
-
}
|
|
1187
|
-
const metadata = "metadata" in value ? value.metadata : void 0;
|
|
1188
|
-
if (metadata && typeof metadata.prepositionValue === "string") {
|
|
1189
|
-
const usedPreposition = metadata.prepositionValue;
|
|
1190
|
-
if (preferred.includes(usedPreposition)) {
|
|
1191
|
-
adjustment += 0.1;
|
|
1192
|
-
} else {
|
|
1193
|
-
adjustment -= 0.1;
|
|
1194
|
-
}
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
return Math.max(-0.1, Math.min(0.1, adjustment));
|
|
1198
|
-
}
|
|
1199
|
-
/**
|
|
1200
|
-
* Skip noise words like "the" before selectors.
|
|
1201
|
-
* This enables more natural English syntax like "toggle the .active".
|
|
1202
|
-
*/
|
|
1203
|
-
skipNoiseWords(tokens) {
|
|
1204
|
-
const token = tokens.peek();
|
|
1205
|
-
if (!token) return;
|
|
1206
|
-
const tokenLower = this.safeToLowerCase(token.value);
|
|
1207
|
-
if (_PatternMatcher.ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
1208
|
-
const mark = tokens.mark();
|
|
1209
|
-
tokens.advance();
|
|
1210
|
-
const nextToken = tokens.peek();
|
|
1211
|
-
if (nextToken && nextToken.kind === "selector") {
|
|
1212
|
-
return;
|
|
1213
|
-
}
|
|
1214
|
-
tokens.reset(mark);
|
|
1215
|
-
}
|
|
1216
|
-
if (tokenLower === "class") {
|
|
1217
|
-
tokens.advance();
|
|
1218
|
-
}
|
|
1219
|
-
}
|
|
1134
|
+
// ===========================================================================
|
|
1135
|
+
// English Idiom Support - Noise Word Handling
|
|
1136
|
+
// ===========================================================================
|
|
1220
1137
|
/**
|
|
1221
1138
|
* Extract event modifiers from the token stream.
|
|
1222
1139
|
* Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
|
|
@@ -1264,22 +1181,6 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
1264
1181
|
};
|
|
1265
1182
|
/** Maximum tokens to scan ahead when looking for a group's leading marker */
|
|
1266
1183
|
_PatternMatcher.MAX_MARKER_SCAN = 3;
|
|
1267
|
-
// ==========================================================================
|
|
1268
|
-
// Depth Limits for Expression Parsing (security hardening)
|
|
1269
|
-
// ==========================================================================
|
|
1270
|
-
/** Maximum depth for nested property access (e.g., a.b.c.d...) */
|
|
1271
|
-
_PatternMatcher.MAX_PROPERTY_DEPTH = 10;
|
|
1272
|
-
/** Maximum number of arguments in method calls */
|
|
1273
|
-
_PatternMatcher.MAX_METHOD_ARGS = 20;
|
|
1274
|
-
// ===========================================================================
|
|
1275
|
-
// English Idiom Support - Noise Word Handling
|
|
1276
|
-
// ===========================================================================
|
|
1277
|
-
/**
|
|
1278
|
-
* Noise words that can be skipped in English for more natural syntax.
|
|
1279
|
-
* - "the" before selectors: "toggle the .active" → "toggle .active"
|
|
1280
|
-
* - "class" after class selectors: "add the .visible class" → "add .visible"
|
|
1281
|
-
*/
|
|
1282
|
-
_PatternMatcher.ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
1283
1184
|
var PatternMatcher = _PatternMatcher;
|
|
1284
1185
|
var patternMatcher = new PatternMatcher();
|
|
1285
1186
|
function matchPattern(tokens, pattern) {
|