@lokascript/framework 4.8.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -1
- package/dist/api/create-dsl.d.ts +9 -10
- package/dist/api/create-dsl.d.ts.map +1 -1
- package/dist/api/index.js +835 -1670
- package/dist/api/index.js.map +1 -1
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +549 -1132
- package/dist/core/index.js.map +1 -1
- package/dist/core/matching/index.d.ts +2 -0
- package/dist/core/matching/index.d.ts.map +1 -1
- package/dist/core/matching/possessive-keywords.d.ts.map +1 -0
- package/dist/core/matching/stock-confidence.d.ts +3 -4
- package/dist/core/matching/stock-confidence.d.ts.map +1 -1
- package/dist/core/matching/stock-reading.d.ts +4 -4
- package/dist/core/{pattern-matching/utils → matching}/type-validation.d.ts +1 -1
- package/dist/core/matching/type-validation.d.ts.map +1 -0
- package/dist/generation/index.d.ts +1 -1
- package/dist/generation/index.d.ts.map +1 -1
- package/dist/generation/index.js +20 -169
- package/dist/generation/index.js.map +1 -1
- package/dist/generation/profile.d.ts +30 -0
- package/dist/generation/profile.d.ts.map +1 -0
- package/dist/generation/renderer.d.ts +1 -1
- package/dist/generation/renderer.d.ts.map +1 -1
- package/dist/generation/schema-patterns.d.ts +3 -3
- package/dist/index.cjs +929 -1674
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +929 -1668
- package/dist/index.js.map +1 -1
- package/dist/multilingual/builders.d.ts +3 -3
- package/dist/multilingual/builders.d.ts.map +1 -1
- package/dist/multilingual/index.js.map +1 -1
- package/dist/parsing/index.d.ts +0 -1
- package/dist/parsing/index.d.ts.map +1 -1
- package/dist/parsing/index.js +0 -1145
- package/dist/parsing/index.js.map +1 -1
- package/dist/testing/gate/lint/types.d.ts +1 -1
- package/dist/testing/gate/lint/types.d.ts.map +1 -1
- package/package.json +2 -7
- package/src/__test__/test-utils.ts +1 -1
- package/src/api/create-dsl-engine.test.ts +60 -35
- package/src/api/create-dsl.ts +24 -35
- package/src/core/index.ts +0 -1
- package/src/core/matching/index.ts +2 -0
- package/src/core/matching/stock-confidence.ts +3 -4
- package/src/core/matching/stock-reading.ts +6 -6
- package/src/core/{pattern-matching/utils → matching}/type-validation.test.ts +2 -2
- package/src/core/{pattern-matching/utils → matching}/type-validation.ts +1 -1
- package/src/generation/domain-renderer.test.ts +1 -1
- package/src/generation/index.ts +1 -1
- package/src/generation/profile.ts +41 -0
- package/src/generation/renderer.test.ts +1 -1
- package/src/generation/renderer.ts +1 -1
- package/src/generation/schema-patterns.ts +3 -3
- package/src/multilingual/bridge.test.ts +31 -23
- package/src/multilingual/builders.ts +3 -3
- package/src/parsing/index.ts +0 -1
- package/src/testing/gate/lint/types.ts +1 -1
- package/dist/core/pattern-matching/index.d.ts +0 -6
- package/dist/core/pattern-matching/index.d.ts.map +0 -1
- package/dist/core/pattern-matching/index.js +0 -1211
- package/dist/core/pattern-matching/index.js.map +0 -1
- package/dist/core/pattern-matching/pattern-matcher.d.ts +0 -122
- package/dist/core/pattern-matching/pattern-matcher.d.ts.map +0 -1
- package/dist/core/pattern-matching/utils/index.d.ts +0 -6
- package/dist/core/pattern-matching/utils/index.d.ts.map +0 -1
- package/dist/core/pattern-matching/utils/possessive-keywords.d.ts.map +0 -1
- package/dist/core/pattern-matching/utils/type-validation.d.ts.map +0 -1
- package/dist/generation/pattern-generator.d.ts +0 -55
- package/dist/generation/pattern-generator.d.ts.map +0 -1
- package/src/core/pattern-matching/index.ts +0 -6
- package/src/core/pattern-matching/pattern-matcher.test.ts +0 -1031
- package/src/core/pattern-matching/pattern-matcher.ts +0 -765
- package/src/core/pattern-matching/utils/index.ts +0 -6
- package/src/generation/pattern-generator.test.ts +0 -532
- package/src/generation/pattern-generator.ts +0 -327
- /package/dist/core/{pattern-matching/utils → matching}/possessive-keywords.d.ts +0 -0
- /package/src/core/{pattern-matching/utils → matching}/possessive-keywords.ts +0 -0
|
@@ -1,1211 +0,0 @@
|
|
|
1
|
-
// src/core/logger.ts
|
|
2
|
-
var DebugLogger = class {
|
|
3
|
-
constructor(namespace) {
|
|
4
|
-
this.namespace = namespace;
|
|
5
|
-
this.enabled = this.checkEnabled();
|
|
6
|
-
}
|
|
7
|
-
checkEnabled() {
|
|
8
|
-
if (typeof process !== "undefined" && process.env) {
|
|
9
|
-
const DEBUG = process.env.DEBUG || "";
|
|
10
|
-
return DEBUG === "*" || DEBUG.includes("framework:*") || DEBUG.includes(`framework:${this.namespace}`);
|
|
11
|
-
}
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
log(level, message, ...args) {
|
|
15
|
-
if (!this.enabled) return;
|
|
16
|
-
const prefix = `[framework:${this.namespace}]`;
|
|
17
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
18
|
-
switch (level) {
|
|
19
|
-
case "debug":
|
|
20
|
-
console.debug(`${timestamp} ${prefix} DEBUG:`, message, ...args);
|
|
21
|
-
break;
|
|
22
|
-
case "info":
|
|
23
|
-
console.info(`${timestamp} ${prefix} INFO:`, message, ...args);
|
|
24
|
-
break;
|
|
25
|
-
case "warn":
|
|
26
|
-
console.warn(`${timestamp} ${prefix} WARN:`, message, ...args);
|
|
27
|
-
break;
|
|
28
|
-
case "error":
|
|
29
|
-
console.error(`${timestamp} ${prefix} ERROR:`, message, ...args);
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
debug(message, ...args) {
|
|
34
|
-
this.log("debug", message, ...args);
|
|
35
|
-
}
|
|
36
|
-
info(message, ...args) {
|
|
37
|
-
this.log("info", message, ...args);
|
|
38
|
-
}
|
|
39
|
-
warn(message, ...args) {
|
|
40
|
-
this.log("warn", message, ...args);
|
|
41
|
-
}
|
|
42
|
-
error(message, ...args) {
|
|
43
|
-
this.log("error", message, ...args);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Check if logging is enabled for this namespace.
|
|
47
|
-
*/
|
|
48
|
-
isEnabled() {
|
|
49
|
-
return this.enabled;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Programmatically enable logging for this namespace.
|
|
53
|
-
*/
|
|
54
|
-
enable() {
|
|
55
|
-
this.enabled = true;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Programmatically disable logging for this namespace.
|
|
59
|
-
*/
|
|
60
|
-
disable() {
|
|
61
|
-
this.enabled = false;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
function createLogger(namespace) {
|
|
65
|
-
return new DebugLogger(namespace);
|
|
66
|
-
}
|
|
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
|
-
// src/core/pattern-matching/utils/type-validation.ts
|
|
90
|
-
function isTypeCompatible(actualType, expectedTypes) {
|
|
91
|
-
if (!expectedTypes || expectedTypes.length === 0) {
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
if (expectedTypes.includes(actualType)) {
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
if (expectedTypes.includes("expression")) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
if (actualType === "property-path") {
|
|
101
|
-
return expectedTypes.some((t) => ["selector", "reference", "expression"].includes(t));
|
|
102
|
-
}
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
function validateValueType(value, expectedTypes) {
|
|
106
|
-
if (!expectedTypes || expectedTypes.length === 0) {
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
return isTypeCompatible(value.type, expectedTypes);
|
|
110
|
-
}
|
|
111
|
-
function isCSSSelector(value) {
|
|
112
|
-
return value.startsWith(".") || value.startsWith("#") || value.startsWith("<");
|
|
113
|
-
}
|
|
114
|
-
function isClassName(value) {
|
|
115
|
-
return value.startsWith(".");
|
|
116
|
-
}
|
|
117
|
-
function isIdSelector(value) {
|
|
118
|
-
return value.startsWith("#");
|
|
119
|
-
}
|
|
120
|
-
function isCSSPropertyRef(value) {
|
|
121
|
-
return value.startsWith("*");
|
|
122
|
-
}
|
|
123
|
-
function isNumericValue(value) {
|
|
124
|
-
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
125
|
-
if (durationMatch) {
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
const num = parseFloat(value);
|
|
129
|
-
return !isNaN(num) && isFinite(num);
|
|
130
|
-
}
|
|
131
|
-
function isPropertyName(value) {
|
|
132
|
-
return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);
|
|
133
|
-
}
|
|
134
|
-
function isVariableRef(value) {
|
|
135
|
-
return value.startsWith(":");
|
|
136
|
-
}
|
|
137
|
-
function isBuiltInReference(value) {
|
|
138
|
-
const builtIns = /* @__PURE__ */ new Set(["me", "you", "it", "result", "event", "target", "body"]);
|
|
139
|
-
return builtIns.has(value.toLowerCase());
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// src/core/pattern-matching/utils/possessive-keywords.ts
|
|
143
|
-
function getPossessiveReference(profile, keyword) {
|
|
144
|
-
return profile?.possessive?.keywords?.[keyword];
|
|
145
|
-
}
|
|
146
|
-
function isPossessiveKeyword(profile, keyword) {
|
|
147
|
-
return profile?.possessive?.keywords?.[keyword] !== void 0;
|
|
148
|
-
}
|
|
149
|
-
function getAllPossessiveKeywords(profile) {
|
|
150
|
-
return profile?.possessive?.keywords ?? {};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// src/core/matching/stock-reading.ts
|
|
154
|
-
function isValidReference(_value) {
|
|
155
|
-
return false;
|
|
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
|
|
682
|
-
var _PatternMatcher = class _PatternMatcher {
|
|
683
|
-
constructor() {
|
|
684
|
-
/** Debug logger */
|
|
685
|
-
this.logger = createLogger("pattern-matcher");
|
|
686
|
-
/**
|
|
687
|
-
* Track stem matches for confidence calculation.
|
|
688
|
-
* This is set during matching and read during confidence calculation.
|
|
689
|
-
*/
|
|
690
|
-
this.stemMatchCount = 0;
|
|
691
|
-
this.totalKeywordMatches = 0;
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* Try to match a single pattern against the token stream.
|
|
695
|
-
* Returns the match result or null if no match.
|
|
696
|
-
*
|
|
697
|
-
* @param tokens - Token stream to match against
|
|
698
|
-
* @param pattern - Pattern to match
|
|
699
|
-
* @param profile - Optional language profile for possessive handling
|
|
700
|
-
*/
|
|
701
|
-
matchPattern(tokens, pattern, profile) {
|
|
702
|
-
const mark = tokens.mark();
|
|
703
|
-
const captured = /* @__PURE__ */ new Map();
|
|
704
|
-
this.logger.debug("========================================");
|
|
705
|
-
this.logger.debug("matchPattern ENTRY");
|
|
706
|
-
this.logger.debug("Pattern ID:", pattern.id);
|
|
707
|
-
this.logger.debug("Pattern command:", pattern.command);
|
|
708
|
-
this.logger.debug("Pattern language:", pattern.language);
|
|
709
|
-
this.logger.debug("Pattern template:", JSON.stringify(pattern.template, null, 2));
|
|
710
|
-
if (this.logger.isEnabled()) {
|
|
711
|
-
const firstTokens = [];
|
|
712
|
-
for (let i = 0; i < 10; i++) {
|
|
713
|
-
const t = tokens.peek(i);
|
|
714
|
-
if (t)
|
|
715
|
-
firstTokens.push({
|
|
716
|
-
type: t.type,
|
|
717
|
-
value: t.value,
|
|
718
|
-
kind: t.kind
|
|
719
|
-
});
|
|
720
|
-
else break;
|
|
721
|
-
}
|
|
722
|
-
this.logger.debug("Input tokens (first 10):", firstTokens);
|
|
723
|
-
this.logger.debug("Profile code:", profile?.code);
|
|
724
|
-
}
|
|
725
|
-
this.currentProfile = profile;
|
|
726
|
-
this.stemMatchCount = 0;
|
|
727
|
-
this.totalKeywordMatches = 0;
|
|
728
|
-
this.logger.debug("--- Calling matchTokenSequence ---");
|
|
729
|
-
this.logger.debug("Pattern tokens to match:", JSON.stringify(pattern.template.tokens, null, 2));
|
|
730
|
-
const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
|
|
731
|
-
this.logger.debug("matchTokenSequence returned:", success);
|
|
732
|
-
this.logger.debug(
|
|
733
|
-
"Captured roles:",
|
|
734
|
-
Array.from(captured.entries()).map(([k, v]) => [k, JSON.stringify(v)])
|
|
735
|
-
);
|
|
736
|
-
if (!success) {
|
|
737
|
-
this.logger.debug(">>> MATCH FAILED - resetting token position");
|
|
738
|
-
tokens.reset(mark);
|
|
739
|
-
return null;
|
|
740
|
-
}
|
|
741
|
-
const confidence = stockConfidenceModel.calculate({
|
|
742
|
-
pattern,
|
|
743
|
-
captured,
|
|
744
|
-
stemMatchCount: this.stemMatchCount,
|
|
745
|
-
totalKeywordMatches: this.totalKeywordMatches
|
|
746
|
-
});
|
|
747
|
-
this.applyExtractionRules(pattern, captured);
|
|
748
|
-
return {
|
|
749
|
-
pattern,
|
|
750
|
-
captured,
|
|
751
|
-
consumedTokens: tokens.position() - mark.position,
|
|
752
|
-
confidence
|
|
753
|
-
};
|
|
754
|
-
}
|
|
755
|
-
/**
|
|
756
|
-
* Try to match multiple patterns, return the best match.
|
|
757
|
-
*
|
|
758
|
-
* @param tokens - Token stream to match against
|
|
759
|
-
* @param patterns - Candidate patterns to try
|
|
760
|
-
* @param profile - Optional language profile for possessive handling
|
|
761
|
-
*/
|
|
762
|
-
matchBest(tokens, patterns, profile) {
|
|
763
|
-
const matches = [];
|
|
764
|
-
for (const pattern of patterns) {
|
|
765
|
-
const mark = tokens.mark();
|
|
766
|
-
const result = this.matchPattern(tokens, pattern, profile);
|
|
767
|
-
if (result) {
|
|
768
|
-
matches.push(result);
|
|
769
|
-
}
|
|
770
|
-
tokens.reset(mark);
|
|
771
|
-
}
|
|
772
|
-
if (matches.length === 0) {
|
|
773
|
-
return null;
|
|
774
|
-
}
|
|
775
|
-
matches.sort((a, b) => {
|
|
776
|
-
const priorityDiff = b.pattern.priority - a.pattern.priority;
|
|
777
|
-
if (priorityDiff !== 0) return priorityDiff;
|
|
778
|
-
const confidenceDiff = b.confidence - a.confidence;
|
|
779
|
-
if (Math.abs(confidenceDiff) > 1e-3) return confidenceDiff;
|
|
780
|
-
return b.consumedTokens - a.consumedTokens;
|
|
781
|
-
});
|
|
782
|
-
const best = matches[0];
|
|
783
|
-
this.matchPattern(tokens, best.pattern);
|
|
784
|
-
return best;
|
|
785
|
-
}
|
|
786
|
-
/**
|
|
787
|
-
* Match a sequence of pattern tokens against the token stream.
|
|
788
|
-
*
|
|
789
|
-
* Supports bounded single-step backtracking: if an optional role consumes
|
|
790
|
-
* a token and the immediately following pattern token fails, the matcher
|
|
791
|
-
* resets to before the optional role and retries the failed token.
|
|
792
|
-
*/
|
|
793
|
-
matchTokenSequence(tokens, patternTokens, captured, parentStopMarkers) {
|
|
794
|
-
const firstPatternToken = patternTokens[0];
|
|
795
|
-
const patternExpectsConjunction = firstPatternToken?.type === "literal" && (firstPatternToken.value === "and" || firstPatternToken.value === "then" || firstPatternToken.alternatives?.includes("and") || firstPatternToken.alternatives?.includes("then"));
|
|
796
|
-
if (this.currentProfile?.code === "ar" && !patternExpectsConjunction) {
|
|
797
|
-
while (tokens.peek()?.kind === "conjunction") {
|
|
798
|
-
tokens.advance();
|
|
799
|
-
}
|
|
800
|
-
}
|
|
801
|
-
let prevOptionalMark = null;
|
|
802
|
-
let prevOptionalRole = null;
|
|
803
|
-
for (let i = 0; i < patternTokens.length; i++) {
|
|
804
|
-
const patternToken = patternTokens[i];
|
|
805
|
-
this.logger.debug(" >> Matching pattern token:", JSON.stringify(patternToken, null, 2));
|
|
806
|
-
const currTok = tokens.peek();
|
|
807
|
-
this.logger.debug(
|
|
808
|
-
" >> Current input token:",
|
|
809
|
-
currTok ? JSON.stringify({
|
|
810
|
-
type: currTok.type,
|
|
811
|
-
value: currTok.value,
|
|
812
|
-
kind: currTok.kind
|
|
813
|
-
}) : "EOF"
|
|
814
|
-
);
|
|
815
|
-
if (patternToken.type === "role" && patternToken.greedy) {
|
|
816
|
-
const stopMarkers = this.collectStopMarkers(patternTokens, i + 1);
|
|
817
|
-
if (parentStopMarkers) {
|
|
818
|
-
for (const m of parentStopMarkers) stopMarkers.add(m);
|
|
819
|
-
}
|
|
820
|
-
const values = [];
|
|
821
|
-
while (!tokens.isAtEnd()) {
|
|
822
|
-
const nextToken = tokens.peek();
|
|
823
|
-
if (!nextToken) break;
|
|
824
|
-
if (this.isStopMarker(nextToken, stopMarkers)) break;
|
|
825
|
-
values.push(nextToken.value);
|
|
826
|
-
tokens.advance();
|
|
827
|
-
}
|
|
828
|
-
if (values.length > 0) {
|
|
829
|
-
captured.set(patternToken.role, { type: "expression", raw: values.join(" ") });
|
|
830
|
-
prevOptionalMark = null;
|
|
831
|
-
prevOptionalRole = null;
|
|
832
|
-
continue;
|
|
833
|
-
} else if (patternToken.optional) {
|
|
834
|
-
continue;
|
|
835
|
-
} else {
|
|
836
|
-
return false;
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
if (patternToken.type === "group") {
|
|
840
|
-
const siblingStopMarkers = this.collectStopMarkers(patternTokens, i + 1);
|
|
841
|
-
if (parentStopMarkers) {
|
|
842
|
-
for (const m of parentStopMarkers) siblingStopMarkers.add(m);
|
|
843
|
-
}
|
|
844
|
-
const groupMatched = this.matchGroupToken(
|
|
845
|
-
tokens,
|
|
846
|
-
patternToken,
|
|
847
|
-
captured,
|
|
848
|
-
siblingStopMarkers
|
|
849
|
-
);
|
|
850
|
-
if (groupMatched) {
|
|
851
|
-
prevOptionalMark = null;
|
|
852
|
-
prevOptionalRole = null;
|
|
853
|
-
continue;
|
|
854
|
-
}
|
|
855
|
-
if (this.isOptional(patternToken)) {
|
|
856
|
-
continue;
|
|
857
|
-
}
|
|
858
|
-
return false;
|
|
859
|
-
}
|
|
860
|
-
const isOptionalRole = patternToken.type === "role" && patternToken.optional === true;
|
|
861
|
-
const markBefore = isOptionalRole ? tokens.mark() : null;
|
|
862
|
-
const isRoleToken = patternToken.type === "role";
|
|
863
|
-
if (isRoleToken && !isOptionalRole) {
|
|
864
|
-
const nextPT = i + 1 < patternTokens.length ? patternTokens[i + 1] : null;
|
|
865
|
-
if (nextPT?.type === "literal") {
|
|
866
|
-
const currentToken = tokens.peek();
|
|
867
|
-
if (currentToken && this.getMatchType(currentToken, nextPT.value) !== "none") {
|
|
868
|
-
this.logger.debug(
|
|
869
|
-
" >> MARKER-STEAL GUARD: current token matches next literal, skipping role"
|
|
870
|
-
);
|
|
871
|
-
this.logger.debug(" >> Token match FAILED");
|
|
872
|
-
if (this.isOptional(patternToken)) {
|
|
873
|
-
continue;
|
|
874
|
-
}
|
|
875
|
-
if (prevOptionalMark && prevOptionalRole) {
|
|
876
|
-
this.logger.debug(" >> BACKTRACKING: undoing optional role", prevOptionalRole);
|
|
877
|
-
tokens.reset(prevOptionalMark);
|
|
878
|
-
captured.delete(prevOptionalRole);
|
|
879
|
-
prevOptionalMark = null;
|
|
880
|
-
prevOptionalRole = null;
|
|
881
|
-
const retryMatched = this.matchPatternToken(tokens, patternToken, captured);
|
|
882
|
-
this.logger.debug(" >> Backtrack retry result:", retryMatched);
|
|
883
|
-
if (retryMatched) {
|
|
884
|
-
continue;
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
return false;
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
const matched = this.matchPatternToken(tokens, patternToken, captured);
|
|
892
|
-
this.logger.debug(" >> Match result:", matched);
|
|
893
|
-
if (matched) {
|
|
894
|
-
if (isOptionalRole) {
|
|
895
|
-
prevOptionalMark = markBefore;
|
|
896
|
-
prevOptionalRole = patternToken.role;
|
|
897
|
-
} else {
|
|
898
|
-
prevOptionalMark = null;
|
|
899
|
-
prevOptionalRole = null;
|
|
900
|
-
}
|
|
901
|
-
continue;
|
|
902
|
-
}
|
|
903
|
-
this.logger.debug(" >> Token match FAILED");
|
|
904
|
-
if (this.isOptional(patternToken)) {
|
|
905
|
-
continue;
|
|
906
|
-
}
|
|
907
|
-
if (prevOptionalMark && prevOptionalRole) {
|
|
908
|
-
this.logger.debug(" >> BACKTRACKING: undoing optional role", prevOptionalRole);
|
|
909
|
-
tokens.reset(prevOptionalMark);
|
|
910
|
-
captured.delete(prevOptionalRole);
|
|
911
|
-
prevOptionalMark = null;
|
|
912
|
-
prevOptionalRole = null;
|
|
913
|
-
const retryMatched = this.matchPatternToken(tokens, patternToken, captured);
|
|
914
|
-
this.logger.debug(" >> Backtrack retry result:", retryMatched);
|
|
915
|
-
if (retryMatched) {
|
|
916
|
-
continue;
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
return false;
|
|
920
|
-
}
|
|
921
|
-
return true;
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Match a single pattern token against the current position in the stream.
|
|
925
|
-
*/
|
|
926
|
-
matchPatternToken(tokens, patternToken, captured) {
|
|
927
|
-
switch (patternToken.type) {
|
|
928
|
-
case "literal":
|
|
929
|
-
return this.matchLiteralToken(tokens, patternToken);
|
|
930
|
-
case "role":
|
|
931
|
-
return readRoleValue(tokens, patternToken, captured, this.currentProfile);
|
|
932
|
-
case "group":
|
|
933
|
-
return this.matchGroupToken(tokens, patternToken, captured);
|
|
934
|
-
default:
|
|
935
|
-
return false;
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
/**
|
|
939
|
-
* Match a literal pattern token (keyword or particle).
|
|
940
|
-
*/
|
|
941
|
-
matchLiteralToken(tokens, patternToken) {
|
|
942
|
-
const token = tokens.peek();
|
|
943
|
-
this.logger.debug(" >>> matchLiteralToken: expecting", patternToken.value);
|
|
944
|
-
this.logger.debug(
|
|
945
|
-
" >>> matchLiteralToken: got token",
|
|
946
|
-
token ? JSON.stringify({
|
|
947
|
-
type: token.type,
|
|
948
|
-
value: token.value,
|
|
949
|
-
kind: token.kind
|
|
950
|
-
}) : "null"
|
|
951
|
-
);
|
|
952
|
-
if (!token) {
|
|
953
|
-
this.logger.debug(" >>> matchLiteralToken: FAIL - no token");
|
|
954
|
-
return false;
|
|
955
|
-
}
|
|
956
|
-
const matchType = this.getMatchType(token, patternToken.value);
|
|
957
|
-
this.logger.debug(
|
|
958
|
-
" >>> matchType for",
|
|
959
|
-
token.value,
|
|
960
|
-
"vs",
|
|
961
|
-
patternToken.value,
|
|
962
|
-
":",
|
|
963
|
-
matchType
|
|
964
|
-
);
|
|
965
|
-
if (matchType !== "none") {
|
|
966
|
-
this.totalKeywordMatches++;
|
|
967
|
-
if (matchType === "stem") {
|
|
968
|
-
this.stemMatchCount++;
|
|
969
|
-
}
|
|
970
|
-
tokens.advance();
|
|
971
|
-
return true;
|
|
972
|
-
}
|
|
973
|
-
if (patternToken.alternatives) {
|
|
974
|
-
for (const alt of patternToken.alternatives) {
|
|
975
|
-
const altMatchType = this.getMatchType(token, alt);
|
|
976
|
-
if (altMatchType !== "none") {
|
|
977
|
-
this.totalKeywordMatches++;
|
|
978
|
-
if (altMatchType === "stem") {
|
|
979
|
-
this.stemMatchCount++;
|
|
980
|
-
}
|
|
981
|
-
tokens.advance();
|
|
982
|
-
return true;
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
return false;
|
|
987
|
-
}
|
|
988
|
-
/**
|
|
989
|
-
* Match a group pattern token (optional sequence).
|
|
990
|
-
* When the group's leading marker isn't at the current position, scans ahead
|
|
991
|
-
* up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
|
|
992
|
-
* (e.g., a value like 'hello') to sit between groups without blocking later
|
|
993
|
-
* marker matches.
|
|
994
|
-
*/
|
|
995
|
-
matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
|
|
996
|
-
const mark = tokens.mark();
|
|
997
|
-
const capturedBefore = new Set(captured.keys());
|
|
998
|
-
const success = this.matchTokenSequence(
|
|
999
|
-
tokens,
|
|
1000
|
-
patternToken.tokens,
|
|
1001
|
-
captured,
|
|
1002
|
-
parentStopMarkers
|
|
1003
|
-
);
|
|
1004
|
-
if (success) return true;
|
|
1005
|
-
tokens.reset(mark);
|
|
1006
|
-
for (const role of captured.keys()) {
|
|
1007
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
1008
|
-
}
|
|
1009
|
-
if (!patternToken.optional) return false;
|
|
1010
|
-
const leadingMarker = this.getGroupLeadingMarker(patternToken);
|
|
1011
|
-
if (leadingMarker) {
|
|
1012
|
-
for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
|
|
1013
|
-
const ahead = tokens.peek(offset);
|
|
1014
|
-
if (!ahead) break;
|
|
1015
|
-
const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
|
|
1016
|
-
if (aheadValue === leadingMarker) {
|
|
1017
|
-
this.logger.debug(
|
|
1018
|
-
" >> MARKER SCAN: found",
|
|
1019
|
-
leadingMarker,
|
|
1020
|
-
"at offset",
|
|
1021
|
-
offset,
|
|
1022
|
-
"- skipping intervening tokens"
|
|
1023
|
-
);
|
|
1024
|
-
for (let s = 0; s < offset; s++) tokens.advance();
|
|
1025
|
-
const retrySuccess = this.matchTokenSequence(
|
|
1026
|
-
tokens,
|
|
1027
|
-
patternToken.tokens,
|
|
1028
|
-
captured,
|
|
1029
|
-
parentStopMarkers
|
|
1030
|
-
);
|
|
1031
|
-
if (retrySuccess) return true;
|
|
1032
|
-
tokens.reset(mark);
|
|
1033
|
-
for (const role of captured.keys()) {
|
|
1034
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
1035
|
-
}
|
|
1036
|
-
break;
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
return true;
|
|
1041
|
-
}
|
|
1042
|
-
/**
|
|
1043
|
-
* Get the leading marker literal from an optional group.
|
|
1044
|
-
* Returns the lowercase marker value, or null if the group
|
|
1045
|
-
* doesn't start with a literal token (e.g., SOV groups where
|
|
1046
|
-
* the role comes before the marker).
|
|
1047
|
-
*/
|
|
1048
|
-
getGroupLeadingMarker(group) {
|
|
1049
|
-
const first = group.tokens[0];
|
|
1050
|
-
if (first?.type === "literal") return first.value.toLowerCase();
|
|
1051
|
-
return null;
|
|
1052
|
-
}
|
|
1053
|
-
/**
|
|
1054
|
-
* Get the type of match for a token against a value.
|
|
1055
|
-
* Used for confidence calculation.
|
|
1056
|
-
*/
|
|
1057
|
-
getMatchType(token, value) {
|
|
1058
|
-
if (token.value === value) return "exact";
|
|
1059
|
-
if (token.normalized === value) return "normalized";
|
|
1060
|
-
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
1061
|
-
return "stem";
|
|
1062
|
-
}
|
|
1063
|
-
if (token.kind === "keyword" && safeToLowerCase(token.value) === value.toLowerCase()) {
|
|
1064
|
-
return "case-insensitive";
|
|
1065
|
-
}
|
|
1066
|
-
return "none";
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* Collect literal values from upcoming pattern tokens that act as stop markers
|
|
1070
|
-
* for greedy role capture. Returns the set of lowercase values.
|
|
1071
|
-
*/
|
|
1072
|
-
collectStopMarkers(patternTokens, startIndex) {
|
|
1073
|
-
const markers = /* @__PURE__ */ new Set();
|
|
1074
|
-
for (let j = startIndex; j < patternTokens.length; j++) {
|
|
1075
|
-
const pt = patternTokens[j];
|
|
1076
|
-
if (pt.type === "literal") {
|
|
1077
|
-
markers.add(pt.value.toLowerCase());
|
|
1078
|
-
if (pt.alternatives) {
|
|
1079
|
-
for (const alt of pt.alternatives) {
|
|
1080
|
-
markers.add(alt.toLowerCase());
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
break;
|
|
1084
|
-
}
|
|
1085
|
-
if (pt.type === "group") {
|
|
1086
|
-
for (const gt of pt.tokens) {
|
|
1087
|
-
if (gt.type === "literal") {
|
|
1088
|
-
markers.add(gt.value.toLowerCase());
|
|
1089
|
-
if (gt.alternatives) {
|
|
1090
|
-
for (const alt of gt.alternatives) {
|
|
1091
|
-
markers.add(alt.toLowerCase());
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
break;
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
if (!pt.optional) break;
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
return markers;
|
|
1101
|
-
}
|
|
1102
|
-
/**
|
|
1103
|
-
* Check if a token matches any stop marker for greedy capture.
|
|
1104
|
-
*/
|
|
1105
|
-
isStopMarker(token, stopMarkers) {
|
|
1106
|
-
if (stopMarkers.size === 0) return false;
|
|
1107
|
-
if (stopMarkers.has(token.value.toLowerCase())) return true;
|
|
1108
|
-
if (token.normalized && stopMarkers.has(token.normalized.toLowerCase())) return true;
|
|
1109
|
-
return false;
|
|
1110
|
-
}
|
|
1111
|
-
// ==========================================================================
|
|
1112
|
-
// Depth Limits for Expression Parsing (security hardening)
|
|
1113
|
-
// ==========================================================================
|
|
1114
|
-
/**
|
|
1115
|
-
* Apply extraction rules to fill in static values and defaults for missing roles.
|
|
1116
|
-
*/
|
|
1117
|
-
applyExtractionRules(pattern, captured) {
|
|
1118
|
-
for (const [role, rule] of Object.entries(pattern.extraction)) {
|
|
1119
|
-
if (!captured.has(role)) {
|
|
1120
|
-
if (rule.value !== void 0) {
|
|
1121
|
-
captured.set(role, { type: "literal", value: rule.value });
|
|
1122
|
-
} else if (rule.default) {
|
|
1123
|
-
captured.set(role, rule.default);
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
/**
|
|
1129
|
-
* Check if a pattern token is optional.
|
|
1130
|
-
*/
|
|
1131
|
-
isOptional(patternToken) {
|
|
1132
|
-
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
1133
|
-
}
|
|
1134
|
-
// ===========================================================================
|
|
1135
|
-
// English Idiom Support - Noise Word Handling
|
|
1136
|
-
// ===========================================================================
|
|
1137
|
-
/**
|
|
1138
|
-
* Extract event modifiers from the token stream.
|
|
1139
|
-
* Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
|
|
1140
|
-
* that can appear after event names.
|
|
1141
|
-
*
|
|
1142
|
-
* Returns EventModifiers object or undefined if no modifiers found.
|
|
1143
|
-
*/
|
|
1144
|
-
extractEventModifiers(tokens) {
|
|
1145
|
-
const modifiers = {};
|
|
1146
|
-
let foundModifier = false;
|
|
1147
|
-
while (!tokens.isAtEnd()) {
|
|
1148
|
-
const token = tokens.peek();
|
|
1149
|
-
if (!token || token.kind !== "event-modifier") {
|
|
1150
|
-
break;
|
|
1151
|
-
}
|
|
1152
|
-
const metadata = token.metadata;
|
|
1153
|
-
if (!metadata) {
|
|
1154
|
-
break;
|
|
1155
|
-
}
|
|
1156
|
-
foundModifier = true;
|
|
1157
|
-
switch (metadata.modifierName) {
|
|
1158
|
-
case "once":
|
|
1159
|
-
modifiers.once = true;
|
|
1160
|
-
break;
|
|
1161
|
-
case "debounce":
|
|
1162
|
-
if (typeof metadata.value === "number") {
|
|
1163
|
-
modifiers.debounce = metadata.value;
|
|
1164
|
-
}
|
|
1165
|
-
break;
|
|
1166
|
-
case "throttle":
|
|
1167
|
-
if (typeof metadata.value === "number") {
|
|
1168
|
-
modifiers.throttle = metadata.value;
|
|
1169
|
-
}
|
|
1170
|
-
break;
|
|
1171
|
-
case "queue":
|
|
1172
|
-
if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
|
|
1173
|
-
modifiers.queue = metadata.value;
|
|
1174
|
-
}
|
|
1175
|
-
break;
|
|
1176
|
-
}
|
|
1177
|
-
tokens.advance();
|
|
1178
|
-
}
|
|
1179
|
-
return foundModifier ? modifiers : void 0;
|
|
1180
|
-
}
|
|
1181
|
-
};
|
|
1182
|
-
/** Maximum tokens to scan ahead when looking for a group's leading marker */
|
|
1183
|
-
_PatternMatcher.MAX_MARKER_SCAN = 3;
|
|
1184
|
-
var PatternMatcher = _PatternMatcher;
|
|
1185
|
-
var patternMatcher = new PatternMatcher();
|
|
1186
|
-
function matchPattern(tokens, pattern) {
|
|
1187
|
-
return patternMatcher.matchPattern(tokens, pattern);
|
|
1188
|
-
}
|
|
1189
|
-
function matchBest(tokens, patterns) {
|
|
1190
|
-
return patternMatcher.matchBest(tokens, patterns);
|
|
1191
|
-
}
|
|
1192
|
-
export {
|
|
1193
|
-
PatternMatcher,
|
|
1194
|
-
getAllPossessiveKeywords,
|
|
1195
|
-
getPossessiveReference,
|
|
1196
|
-
isBuiltInReference,
|
|
1197
|
-
isCSSPropertyRef,
|
|
1198
|
-
isCSSSelector,
|
|
1199
|
-
isClassName,
|
|
1200
|
-
isIdSelector,
|
|
1201
|
-
isNumericValue,
|
|
1202
|
-
isPossessiveKeyword,
|
|
1203
|
-
isPropertyName,
|
|
1204
|
-
isTypeCompatible,
|
|
1205
|
-
isVariableRef,
|
|
1206
|
-
matchBest,
|
|
1207
|
-
matchPattern,
|
|
1208
|
-
patternMatcher,
|
|
1209
|
-
validateValueType
|
|
1210
|
-
};
|
|
1211
|
-
//# sourceMappingURL=index.js.map
|