@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
package/dist/index.cjs
CHANGED
|
@@ -35,6 +35,8 @@ __export(index_exports, {
|
|
|
35
35
|
InMemoryDictionary: () => InMemoryDictionary,
|
|
36
36
|
InMemoryProfileProvider: () => InMemoryProfileProvider,
|
|
37
37
|
LatinExtendedIdentifierExtractor: () => LatinExtendedIdentifierExtractor,
|
|
38
|
+
LossyTranslationError: () => LossyTranslationError,
|
|
39
|
+
MatchSkeleton: () => MatchSkeleton,
|
|
38
40
|
NullDictionary: () => NullDictionary,
|
|
39
41
|
NullProfileProvider: () => NullProfileProvider,
|
|
40
42
|
NumberExtractor: () => NumberExtractor,
|
|
@@ -42,6 +44,7 @@ __export(index_exports, {
|
|
|
42
44
|
PatternMatcher: () => PatternMatcher,
|
|
43
45
|
PatternTracker: () => PatternTracker,
|
|
44
46
|
PunctuationExtractor: () => PunctuationExtractor,
|
|
47
|
+
StockValueReader: () => StockValueReader,
|
|
45
48
|
StringLiteralExtractor: () => StringLiteralExtractor,
|
|
46
49
|
TokenStreamImpl: () => TokenStreamImpl,
|
|
47
50
|
TranslationReadBackError: () => TranslationReadBackError,
|
|
@@ -76,9 +79,11 @@ __export(index_exports, {
|
|
|
76
79
|
createPosition: () => createPosition,
|
|
77
80
|
createPropertyPath: () => import_intent.createPropertyPath,
|
|
78
81
|
createReference: () => import_intent.createReference,
|
|
82
|
+
createSchemaPatternGenerator: () => createSchemaPatternGenerator,
|
|
79
83
|
createSchemaRenderer: () => createSchemaRenderer,
|
|
80
84
|
createSelector: () => import_intent.createSelector,
|
|
81
85
|
createSimpleTokenizer: () => createSimpleTokenizer,
|
|
86
|
+
createStockReader: () => createStockReader,
|
|
82
87
|
createToken: () => createToken,
|
|
83
88
|
createTokenizerContext: () => createTokenizerContext,
|
|
84
89
|
createTryNode: () => import_intent.createTryNode,
|
|
@@ -94,6 +99,7 @@ __export(index_exports, {
|
|
|
94
99
|
extractUrl: () => extractUrl,
|
|
95
100
|
extractValue: () => import_intent.extractValue,
|
|
96
101
|
filterBySeverity: () => import_intent7.filterBySeverity,
|
|
102
|
+
findTranslationLoss: () => findTranslationLoss,
|
|
97
103
|
fromEnvelopeJSON: () => import_intent5.fromEnvelopeJSON,
|
|
98
104
|
fromError: () => import_intent7.fromError,
|
|
99
105
|
fromInterchangeNode: () => fromInterchangeNode,
|
|
@@ -108,6 +114,7 @@ __export(index_exports, {
|
|
|
108
114
|
getPossessiveReference: () => getPossessiveReference,
|
|
109
115
|
getRoleSpec: () => import_intent6.getRoleSpec,
|
|
110
116
|
getRoleValue: () => import_intent.getRoleValue,
|
|
117
|
+
greedyStopMarkers: () => greedyStopMarkers,
|
|
111
118
|
insertMarkers: () => insertMarkers,
|
|
112
119
|
isAsciiIdentifierChar: () => isAsciiIdentifierChar,
|
|
113
120
|
isAsciiLetter: () => isAsciiLetter,
|
|
@@ -135,6 +142,7 @@ __export(index_exports, {
|
|
|
135
142
|
isWhitespace: () => isWhitespace,
|
|
136
143
|
joinTokens: () => joinTokens,
|
|
137
144
|
jsonToSemanticNode: () => jsonToSemanticNode,
|
|
145
|
+
legacyMarkerAlternatives: () => legacyMarkerAlternatives,
|
|
138
146
|
lookupKeyword: () => lookupKeyword,
|
|
139
147
|
lookupMarker: () => lookupMarker,
|
|
140
148
|
matchBest: () => matchBest,
|
|
@@ -147,25 +155,34 @@ __export(index_exports, {
|
|
|
147
155
|
parseExplicit: () => import_intent3.parseExplicit,
|
|
148
156
|
parseJSONL: () => parseJSONL,
|
|
149
157
|
patternMatcher: () => patternMatcher,
|
|
158
|
+
readGreedy: () => readGreedy,
|
|
159
|
+
readRoleValue: () => readRoleValue,
|
|
150
160
|
registryToAOTBackends: () => registryToAOTBackends,
|
|
151
161
|
renderDocument: () => import_intent4.renderDocument,
|
|
152
162
|
renderExplicit: () => import_intent4.renderExplicit,
|
|
153
163
|
renderExpr: () => renderExpr,
|
|
164
|
+
renderPattern: () => renderPattern,
|
|
154
165
|
reorderRoles: () => reorderRoles,
|
|
166
|
+
resolveMarkerForRole: () => resolveMarkerForRole,
|
|
167
|
+
schemaMarkerAlternatives: () => schemaMarkerAlternatives,
|
|
168
|
+
selectRenderPattern: () => selectRenderPattern,
|
|
155
169
|
semanticNodeToJSON: () => semanticNodeToJSON,
|
|
156
170
|
semanticNodeToRuntimeAST: () => semanticNodeToRuntimeAST,
|
|
157
171
|
semanticValueToAST: () => semanticValueToAST,
|
|
158
172
|
sortRolesByWordOrder: () => sortRolesByWordOrder,
|
|
173
|
+
stockConfidenceModel: () => stockConfidenceModel,
|
|
159
174
|
stripOptionalDiacritics: () => stripOptionalDiacritics,
|
|
160
175
|
synthesizeFromSchemas: () => synthesizeFromSchemas,
|
|
161
176
|
toEnvelopeJSON: () => import_intent5.toEnvelopeJSON,
|
|
162
177
|
toJSONL: () => toJSONL,
|
|
163
178
|
toJSONLRow: () => toJSONLRow,
|
|
164
179
|
toProtocolJSON: () => import_intent5.toProtocolJSON,
|
|
180
|
+
unconsumedSpans: () => unconsumedSpans,
|
|
165
181
|
validateProtocolJSON: () => import_intent5.validateProtocolJSON,
|
|
166
182
|
validateSemanticJSON: () => validateSemanticJSON,
|
|
167
183
|
validateValueType: () => validateValueType,
|
|
168
|
-
withDefaultExtractors: () => withDefaultExtractors
|
|
184
|
+
withDefaultExtractors: () => withDefaultExtractors,
|
|
185
|
+
withoutVariantOnlyRoles: () => withoutVariantOnlyRoles
|
|
169
186
|
});
|
|
170
187
|
module.exports = __toCommonJS(index_exports);
|
|
171
188
|
|
|
@@ -303,10 +320,535 @@ function getAllPossessiveKeywords(profile) {
|
|
|
303
320
|
return profile?.possessive?.keywords ?? {};
|
|
304
321
|
}
|
|
305
322
|
|
|
306
|
-
// src/core/
|
|
323
|
+
// src/core/matching/stock-reading.ts
|
|
307
324
|
function isValidReference(_value) {
|
|
308
325
|
return false;
|
|
309
326
|
}
|
|
327
|
+
function safeToLowerCase(value) {
|
|
328
|
+
if (typeof value === "string") {
|
|
329
|
+
return value.toLowerCase();
|
|
330
|
+
}
|
|
331
|
+
if (value === null || value === void 0) {
|
|
332
|
+
return "";
|
|
333
|
+
}
|
|
334
|
+
return String(value).toLowerCase();
|
|
335
|
+
}
|
|
336
|
+
function readRoleValue(tokens, patternToken, captured, profile) {
|
|
337
|
+
skipNoiseWords(tokens);
|
|
338
|
+
const token = tokens.peek();
|
|
339
|
+
if (!token) {
|
|
340
|
+
return patternToken.optional || false;
|
|
341
|
+
}
|
|
342
|
+
const possessiveValue = tryMatchPossessiveExpression(tokens, profile);
|
|
343
|
+
if (possessiveValue) {
|
|
344
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
345
|
+
if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
|
|
346
|
+
return patternToken.optional || false;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
captured.set(patternToken.role, possessiveValue);
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
const methodCallValue = tryMatchMethodCallExpression(tokens);
|
|
353
|
+
if (methodCallValue) {
|
|
354
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
355
|
+
if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
|
|
356
|
+
return patternToken.optional || false;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
captured.set(patternToken.role, methodCallValue);
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
const possessiveSelectorValue = tryMatchPossessiveSelectorExpression(tokens);
|
|
363
|
+
if (possessiveSelectorValue) {
|
|
364
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
365
|
+
if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
|
|
366
|
+
return patternToken.optional || false;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
captured.set(patternToken.role, possessiveSelectorValue);
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
const propertyAccessValue = tryMatchPropertyAccessExpression(tokens);
|
|
373
|
+
if (propertyAccessValue) {
|
|
374
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
375
|
+
if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
|
|
376
|
+
return patternToken.optional || false;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
captured.set(patternToken.role, propertyAccessValue);
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
const selectorPropertyValue = tryMatchSelectorPropertyExpression(tokens);
|
|
383
|
+
if (selectorPropertyValue) {
|
|
384
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
385
|
+
if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
|
|
386
|
+
return patternToken.optional || false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
captured.set(patternToken.role, selectorPropertyValue);
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
const value = tokenToSemanticValue(token);
|
|
393
|
+
if (!value) {
|
|
394
|
+
return patternToken.optional || false;
|
|
395
|
+
}
|
|
396
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
397
|
+
if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
|
|
398
|
+
return patternToken.optional || false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
captured.set(patternToken.role, value);
|
|
402
|
+
tokens.advance();
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
function tryMatchPossessiveExpression(tokens, profile) {
|
|
406
|
+
const token = tokens.peek();
|
|
407
|
+
if (!token) return null;
|
|
408
|
+
if (!profile) return null;
|
|
409
|
+
const tokenValue = token.normalized || token.value;
|
|
410
|
+
const tokenLower = safeToLowerCase(tokenValue);
|
|
411
|
+
const baseRef = getPossessiveReference(profile, tokenLower);
|
|
412
|
+
if (!baseRef) return null;
|
|
413
|
+
const mark = tokens.mark();
|
|
414
|
+
tokens.advance();
|
|
415
|
+
const propertyToken = tokens.peek();
|
|
416
|
+
if (!propertyToken) {
|
|
417
|
+
tokens.reset(mark);
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
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)) {
|
|
421
|
+
tokens.advance();
|
|
422
|
+
let propertyName = propertyToken.value;
|
|
423
|
+
if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
|
|
424
|
+
propertyName = propertyName.substring(1);
|
|
425
|
+
}
|
|
426
|
+
let chainedProps = propertyName;
|
|
427
|
+
while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
|
|
428
|
+
chainedProps += tokens.peek().value;
|
|
429
|
+
tokens.advance();
|
|
430
|
+
}
|
|
431
|
+
const nextPeek = tokens.peek();
|
|
432
|
+
if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
|
|
433
|
+
chainedProps += nextPeek.value;
|
|
434
|
+
tokens.advance();
|
|
435
|
+
}
|
|
436
|
+
return (0, import_intent.createPropertyPath)((0, import_intent.createReference)(baseRef), chainedProps);
|
|
437
|
+
}
|
|
438
|
+
tokens.reset(mark);
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
function isStructuralKeyword(value) {
|
|
442
|
+
const structural = /* @__PURE__ */ new Set([
|
|
443
|
+
// Prepositions
|
|
444
|
+
"into",
|
|
445
|
+
"in",
|
|
446
|
+
"to",
|
|
447
|
+
"from",
|
|
448
|
+
"at",
|
|
449
|
+
"by",
|
|
450
|
+
"with",
|
|
451
|
+
"without",
|
|
452
|
+
"before",
|
|
453
|
+
"after",
|
|
454
|
+
"of",
|
|
455
|
+
"as",
|
|
456
|
+
"on",
|
|
457
|
+
// Control flow
|
|
458
|
+
"then",
|
|
459
|
+
"end",
|
|
460
|
+
"else",
|
|
461
|
+
"if",
|
|
462
|
+
"repeat",
|
|
463
|
+
"while",
|
|
464
|
+
"for",
|
|
465
|
+
// Commands (shouldn't be property names)
|
|
466
|
+
"toggle",
|
|
467
|
+
"add",
|
|
468
|
+
"remove",
|
|
469
|
+
"put",
|
|
470
|
+
"set",
|
|
471
|
+
"show",
|
|
472
|
+
"hide",
|
|
473
|
+
"increment",
|
|
474
|
+
"decrement",
|
|
475
|
+
"send",
|
|
476
|
+
"trigger",
|
|
477
|
+
"call"
|
|
478
|
+
]);
|
|
479
|
+
return structural.has(value.toLowerCase());
|
|
480
|
+
}
|
|
481
|
+
function tryMatchMethodCallExpression(tokens) {
|
|
482
|
+
const token = tokens.peek();
|
|
483
|
+
if (!token || token.kind !== "selector") return null;
|
|
484
|
+
const mark = tokens.mark();
|
|
485
|
+
tokens.advance();
|
|
486
|
+
const dotToken = tokens.peek();
|
|
487
|
+
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
488
|
+
tokens.reset(mark);
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
tokens.advance();
|
|
492
|
+
const methodToken = tokens.peek();
|
|
493
|
+
if (!methodToken || methodToken.kind !== "identifier") {
|
|
494
|
+
tokens.reset(mark);
|
|
495
|
+
return null;
|
|
496
|
+
}
|
|
497
|
+
tokens.advance();
|
|
498
|
+
const openParen = tokens.peek();
|
|
499
|
+
if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
|
|
500
|
+
tokens.reset(mark);
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
tokens.advance();
|
|
504
|
+
const args = [];
|
|
505
|
+
while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
|
|
506
|
+
const argToken = tokens.peek();
|
|
507
|
+
if (!argToken) break;
|
|
508
|
+
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
509
|
+
tokens.advance();
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
513
|
+
tokens.advance();
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
args.push(argToken.value);
|
|
517
|
+
tokens.advance();
|
|
518
|
+
}
|
|
519
|
+
const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
|
|
520
|
+
return {
|
|
521
|
+
type: "expression",
|
|
522
|
+
raw: methodCall
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
function tryMatchPropertyAccessExpression(tokens) {
|
|
526
|
+
const token = tokens.peek();
|
|
527
|
+
if (!token) return null;
|
|
528
|
+
if (token.kind !== "identifier" && token.kind !== "keyword") return null;
|
|
529
|
+
const mark = tokens.mark();
|
|
530
|
+
tokens.advance();
|
|
531
|
+
const dotToken = tokens.peek();
|
|
532
|
+
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
533
|
+
tokens.reset(mark);
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
tokens.advance();
|
|
537
|
+
const propertyToken = tokens.peek();
|
|
538
|
+
if (!propertyToken || propertyToken.kind !== "identifier") {
|
|
539
|
+
tokens.reset(mark);
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
tokens.advance();
|
|
543
|
+
let chain = `${token.value}.${propertyToken.value}`;
|
|
544
|
+
let depth = 1;
|
|
545
|
+
while (!tokens.isAtEnd() && depth < MAX_PROPERTY_DEPTH) {
|
|
546
|
+
const nextDot = tokens.peek();
|
|
547
|
+
if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
tokens.advance();
|
|
551
|
+
const nextProp = tokens.peek();
|
|
552
|
+
if (!nextProp || nextProp.kind !== "identifier") {
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
tokens.advance();
|
|
556
|
+
chain += `.${nextProp.value}`;
|
|
557
|
+
depth++;
|
|
558
|
+
}
|
|
559
|
+
const openParen = tokens.peek();
|
|
560
|
+
if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
|
|
561
|
+
tokens.advance();
|
|
562
|
+
const args = [];
|
|
563
|
+
let argDepth = 0;
|
|
564
|
+
while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
|
|
565
|
+
const argToken = tokens.peek();
|
|
566
|
+
if (!argToken) break;
|
|
567
|
+
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
568
|
+
if (argDepth === 0) {
|
|
569
|
+
tokens.advance();
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
argDepth--;
|
|
573
|
+
}
|
|
574
|
+
if (argToken.kind === "punctuation" && argToken.value === "(") {
|
|
575
|
+
argDepth++;
|
|
576
|
+
}
|
|
577
|
+
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
578
|
+
tokens.advance();
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
args.push(argToken.value);
|
|
582
|
+
tokens.advance();
|
|
583
|
+
}
|
|
584
|
+
const methodCall = `${chain}(${args.join(", ")})`;
|
|
585
|
+
return {
|
|
586
|
+
type: "expression",
|
|
587
|
+
raw: methodCall
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
return {
|
|
591
|
+
type: "expression",
|
|
592
|
+
raw: chain
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
function tryMatchPossessiveSelectorExpression(tokens) {
|
|
596
|
+
const token = tokens.peek();
|
|
597
|
+
if (!token || token.kind !== "selector") return null;
|
|
598
|
+
const mark = tokens.mark();
|
|
599
|
+
tokens.advance();
|
|
600
|
+
const possessiveToken = tokens.peek();
|
|
601
|
+
if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
|
|
602
|
+
tokens.reset(mark);
|
|
603
|
+
return null;
|
|
604
|
+
}
|
|
605
|
+
tokens.advance();
|
|
606
|
+
const propertyToken = tokens.peek();
|
|
607
|
+
if (!propertyToken) {
|
|
608
|
+
tokens.reset(mark);
|
|
609
|
+
return null;
|
|
610
|
+
}
|
|
611
|
+
if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
|
|
612
|
+
tokens.reset(mark);
|
|
613
|
+
return null;
|
|
614
|
+
}
|
|
615
|
+
tokens.advance();
|
|
616
|
+
return (0, import_intent.createPropertyPath)((0, import_intent.createSelector)(token.value), propertyToken.value);
|
|
617
|
+
}
|
|
618
|
+
function tryMatchSelectorPropertyExpression(tokens) {
|
|
619
|
+
const token = tokens.peek();
|
|
620
|
+
if (!token || token.kind !== "selector") return null;
|
|
621
|
+
if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
|
|
622
|
+
const mark = tokens.mark();
|
|
623
|
+
tokens.advance();
|
|
624
|
+
const propertyToken = tokens.peek();
|
|
625
|
+
if (!propertyToken || propertyToken.kind !== "selector") {
|
|
626
|
+
tokens.reset(mark);
|
|
627
|
+
return null;
|
|
628
|
+
}
|
|
629
|
+
if (!propertyToken.value.startsWith(".")) {
|
|
630
|
+
tokens.reset(mark);
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
633
|
+
const peek2 = tokens.peek(1);
|
|
634
|
+
if (peek2 && peek2.kind === "selector") {
|
|
635
|
+
}
|
|
636
|
+
tokens.advance();
|
|
637
|
+
const propertyName = propertyToken.value.slice(1);
|
|
638
|
+
return (0, import_intent.createPropertyPath)((0, import_intent.createSelector)(token.value), propertyName);
|
|
639
|
+
}
|
|
640
|
+
var MAX_PROPERTY_DEPTH = 10;
|
|
641
|
+
var MAX_METHOD_ARGS = 20;
|
|
642
|
+
function tokenToSemanticValue(token) {
|
|
643
|
+
switch (token.kind) {
|
|
644
|
+
case "selector":
|
|
645
|
+
return (0, import_intent.createSelector)(token.value);
|
|
646
|
+
case "literal":
|
|
647
|
+
return parseLiteralValue(token.value);
|
|
648
|
+
case "keyword": {
|
|
649
|
+
const tokenValue = token.normalized || token.value;
|
|
650
|
+
const lower = safeToLowerCase(tokenValue);
|
|
651
|
+
if (isValidReference(lower)) {
|
|
652
|
+
return (0, import_intent.createReference)(lower);
|
|
653
|
+
}
|
|
654
|
+
return (0, import_intent.createLiteral)(token.normalized || token.value);
|
|
655
|
+
}
|
|
656
|
+
case "identifier": {
|
|
657
|
+
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
658
|
+
return (0, import_intent.createReference)(token.value);
|
|
659
|
+
}
|
|
660
|
+
const identLower = safeToLowerCase(token.value);
|
|
661
|
+
if (isValidReference(identLower)) {
|
|
662
|
+
return (0, import_intent.createReference)(identLower);
|
|
663
|
+
}
|
|
664
|
+
return { type: "expression", raw: token.value };
|
|
665
|
+
}
|
|
666
|
+
case "url":
|
|
667
|
+
return (0, import_intent.createLiteral)(token.value, "string");
|
|
668
|
+
default:
|
|
669
|
+
return null;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
function parseLiteralValue(value) {
|
|
673
|
+
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
674
|
+
const inner = value.slice(1, -1);
|
|
675
|
+
return (0, import_intent.createLiteral)(inner, "string");
|
|
676
|
+
}
|
|
677
|
+
if (value === "true") return (0, import_intent.createLiteral)(true, "boolean");
|
|
678
|
+
if (value === "false") return (0, import_intent.createLiteral)(false, "boolean");
|
|
679
|
+
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
680
|
+
if (durationMatch) {
|
|
681
|
+
const num2 = parseFloat(durationMatch[1]);
|
|
682
|
+
const unit = durationMatch[2];
|
|
683
|
+
if (unit) {
|
|
684
|
+
return (0, import_intent.createLiteral)(value, "duration");
|
|
685
|
+
}
|
|
686
|
+
return (0, import_intent.createLiteral)(num2, "number");
|
|
687
|
+
}
|
|
688
|
+
const num = parseFloat(value);
|
|
689
|
+
if (!isNaN(num)) {
|
|
690
|
+
return (0, import_intent.createLiteral)(num, "number");
|
|
691
|
+
}
|
|
692
|
+
return (0, import_intent.createLiteral)(value, "string");
|
|
693
|
+
}
|
|
694
|
+
var ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
695
|
+
function skipNoiseWords(tokens) {
|
|
696
|
+
const token = tokens.peek();
|
|
697
|
+
if (!token) return;
|
|
698
|
+
const tokenLower = safeToLowerCase(token.value);
|
|
699
|
+
if (ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
700
|
+
const mark = tokens.mark();
|
|
701
|
+
tokens.advance();
|
|
702
|
+
const nextToken = tokens.peek();
|
|
703
|
+
if (nextToken && nextToken.kind === "selector") {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
tokens.reset(mark);
|
|
707
|
+
}
|
|
708
|
+
if (tokenLower === "class") {
|
|
709
|
+
tokens.advance();
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// src/core/matching/stock-confidence.ts
|
|
714
|
+
function scoreParts(ctx) {
|
|
715
|
+
const { pattern, captured } = ctx;
|
|
716
|
+
let score = 0;
|
|
717
|
+
let maxScore = 0;
|
|
718
|
+
const hasDefault = (role) => {
|
|
719
|
+
return pattern.extraction?.[role]?.default !== void 0;
|
|
720
|
+
};
|
|
721
|
+
for (const token of pattern.template.tokens) {
|
|
722
|
+
if (token.type === "role") {
|
|
723
|
+
maxScore += 1;
|
|
724
|
+
if (captured.has(token.role)) {
|
|
725
|
+
score += 1;
|
|
726
|
+
}
|
|
727
|
+
} else if (token.type === "group") {
|
|
728
|
+
for (const subToken of token.tokens) {
|
|
729
|
+
if (subToken.type === "role") {
|
|
730
|
+
const roleHasDefault = hasDefault(subToken.role);
|
|
731
|
+
const weight = 0.8;
|
|
732
|
+
maxScore += weight;
|
|
733
|
+
if (captured.has(subToken.role)) {
|
|
734
|
+
score += weight;
|
|
735
|
+
} else if (roleHasDefault) {
|
|
736
|
+
score += weight * 0.6;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
const roleCoverage = maxScore > 0 ? score / maxScore : 1;
|
|
743
|
+
let baseConfidence = roleCoverage;
|
|
744
|
+
let stemPenalty = 0;
|
|
745
|
+
if (ctx.stemMatchCount > 0 && ctx.totalKeywordMatches > 0) {
|
|
746
|
+
stemPenalty = ctx.stemMatchCount / ctx.totalKeywordMatches * 0.15;
|
|
747
|
+
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
748
|
+
}
|
|
749
|
+
const vsoBoost = calculateVSOConfidenceBoost(pattern);
|
|
750
|
+
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
751
|
+
const prepositionAdjustment = arabicPrepositionDisambiguation(pattern, captured);
|
|
752
|
+
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
753
|
+
return {
|
|
754
|
+
roleCoverage,
|
|
755
|
+
stemPenalty,
|
|
756
|
+
languageBoost: vsoBoost,
|
|
757
|
+
languageAdjustment: prepositionAdjustment,
|
|
758
|
+
final: baseConfidence
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
function calculateVSOConfidenceBoost(pattern) {
|
|
762
|
+
if (pattern.language !== "ar") {
|
|
763
|
+
return 0;
|
|
764
|
+
}
|
|
765
|
+
const firstToken = pattern.template.tokens[0];
|
|
766
|
+
if (!firstToken || firstToken.type !== "literal") {
|
|
767
|
+
return 0;
|
|
768
|
+
}
|
|
769
|
+
const ARABIC_VERBS = /* @__PURE__ */ new Set([
|
|
770
|
+
"\u0628\u062F\u0644",
|
|
771
|
+
"\u063A\u064A\u0631",
|
|
772
|
+
"\u0623\u0636\u0641",
|
|
773
|
+
"\u0623\u0632\u0644",
|
|
774
|
+
"\u0636\u0639",
|
|
775
|
+
"\u0627\u062C\u0639\u0644",
|
|
776
|
+
"\u0639\u064A\u0646",
|
|
777
|
+
"\u0632\u062F",
|
|
778
|
+
"\u0627\u0646\u0642\u0635",
|
|
779
|
+
"\u0633\u062C\u0644",
|
|
780
|
+
"\u0623\u0638\u0647\u0631",
|
|
781
|
+
"\u0623\u062E\u0641",
|
|
782
|
+
"\u0634\u063A\u0644",
|
|
783
|
+
"\u0623\u0631\u0633\u0644",
|
|
784
|
+
"\u0631\u0643\u0632",
|
|
785
|
+
"\u0634\u0648\u0634",
|
|
786
|
+
"\u062A\u0648\u0642\u0641",
|
|
787
|
+
"\u0627\u0646\u0633\u062E",
|
|
788
|
+
"\u0627\u062D\u0630\u0641",
|
|
789
|
+
"\u0627\u0635\u0646\u0639",
|
|
790
|
+
"\u0627\u0646\u062A\u0638\u0631",
|
|
791
|
+
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
792
|
+
"\u0623\u0648"
|
|
793
|
+
]);
|
|
794
|
+
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
795
|
+
return 0.15;
|
|
796
|
+
}
|
|
797
|
+
if (firstToken.alternatives) {
|
|
798
|
+
for (const alt of firstToken.alternatives) {
|
|
799
|
+
if (ARABIC_VERBS.has(alt)) {
|
|
800
|
+
return 0.15;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
return 0;
|
|
805
|
+
}
|
|
806
|
+
function arabicPrepositionDisambiguation(pattern, captured) {
|
|
807
|
+
if (pattern.language !== "ar") {
|
|
808
|
+
return 0;
|
|
809
|
+
}
|
|
810
|
+
let adjustment = 0;
|
|
811
|
+
const PREFERRED_PREPOSITIONS = {
|
|
812
|
+
patient: ["\u0639\u0644\u0649"],
|
|
813
|
+
// element selectors prefer على (on/upon)
|
|
814
|
+
destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
815
|
+
// destination prefers إلى (to)
|
|
816
|
+
source: ["\u0645\u0646"],
|
|
817
|
+
// source prefers من (from)
|
|
818
|
+
agent: ["\u0645\u0646"],
|
|
819
|
+
// agent/by prefers من (from/by)
|
|
820
|
+
manner: ["\u0628"],
|
|
821
|
+
// manner prefers ب (with/by)
|
|
822
|
+
style: ["\u0628"],
|
|
823
|
+
// style prefers ب (with)
|
|
824
|
+
goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
825
|
+
// target state prefers إلى (to)
|
|
826
|
+
method: ["\u0628"]
|
|
827
|
+
// method prefers ب (with/by)
|
|
828
|
+
};
|
|
829
|
+
for (const [role, value] of captured.entries()) {
|
|
830
|
+
const preferred = PREFERRED_PREPOSITIONS[role];
|
|
831
|
+
if (!preferred || preferred.length === 0) {
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
const metadata = "metadata" in value ? value.metadata : void 0;
|
|
835
|
+
if (metadata && typeof metadata.prepositionValue === "string") {
|
|
836
|
+
const usedPreposition = metadata.prepositionValue;
|
|
837
|
+
if (preferred.includes(usedPreposition)) {
|
|
838
|
+
adjustment += 0.1;
|
|
839
|
+
} else {
|
|
840
|
+
adjustment -= 0.1;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
return Math.max(-0.1, Math.min(0.1, adjustment));
|
|
845
|
+
}
|
|
846
|
+
var stockConfidenceModel = {
|
|
847
|
+
calculate: (ctx) => scoreParts(ctx).final,
|
|
848
|
+
calculateWithBreakdown: (ctx) => scoreParts(ctx)
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
// src/core/pattern-matching/pattern-matcher.ts
|
|
310
852
|
var _PatternMatcher = class _PatternMatcher {
|
|
311
853
|
constructor() {
|
|
312
854
|
/** Debug logger */
|
|
@@ -318,19 +860,6 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
318
860
|
this.stemMatchCount = 0;
|
|
319
861
|
this.totalKeywordMatches = 0;
|
|
320
862
|
}
|
|
321
|
-
/**
|
|
322
|
-
* Safely convert a value to lowercase string.
|
|
323
|
-
* Provides protection against non-string values at runtime.
|
|
324
|
-
*/
|
|
325
|
-
safeToLowerCase(value) {
|
|
326
|
-
if (typeof value === "string") {
|
|
327
|
-
return value.toLowerCase();
|
|
328
|
-
}
|
|
329
|
-
if (value === null || value === void 0) {
|
|
330
|
-
return "";
|
|
331
|
-
}
|
|
332
|
-
return String(value).toLowerCase();
|
|
333
|
-
}
|
|
334
863
|
/**
|
|
335
864
|
* Try to match a single pattern against the token stream.
|
|
336
865
|
* Returns the match result or null if no match.
|
|
@@ -379,7 +908,12 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
379
908
|
tokens.reset(mark);
|
|
380
909
|
return null;
|
|
381
910
|
}
|
|
382
|
-
const confidence =
|
|
911
|
+
const confidence = stockConfidenceModel.calculate({
|
|
912
|
+
pattern,
|
|
913
|
+
captured,
|
|
914
|
+
stemMatchCount: this.stemMatchCount,
|
|
915
|
+
totalKeywordMatches: this.totalKeywordMatches
|
|
916
|
+
});
|
|
383
917
|
this.applyExtractionRules(pattern, captured);
|
|
384
918
|
return {
|
|
385
919
|
pattern,
|
|
@@ -564,7 +1098,7 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
564
1098
|
case "literal":
|
|
565
1099
|
return this.matchLiteralToken(tokens, patternToken);
|
|
566
1100
|
case "role":
|
|
567
|
-
return
|
|
1101
|
+
return readRoleValue(tokens, patternToken, captured, this.currentProfile);
|
|
568
1102
|
case "group":
|
|
569
1103
|
return this.matchGroupToken(tokens, patternToken, captured);
|
|
570
1104
|
default:
|
|
@@ -622,420 +1156,54 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
622
1156
|
return false;
|
|
623
1157
|
}
|
|
624
1158
|
/**
|
|
625
|
-
* Match a
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
*
|
|
629
|
-
*
|
|
1159
|
+
* Match a group pattern token (optional sequence).
|
|
1160
|
+
* When the group's leading marker isn't at the current position, scans ahead
|
|
1161
|
+
* up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
|
|
1162
|
+
* (e.g., a value like 'hello') to sit between groups without blocking later
|
|
1163
|
+
* marker matches.
|
|
630
1164
|
*/
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
|
|
1165
|
+
matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
|
|
1166
|
+
const mark = tokens.mark();
|
|
1167
|
+
const capturedBefore = new Set(captured.keys());
|
|
1168
|
+
const success = this.matchTokenSequence(
|
|
1169
|
+
tokens,
|
|
1170
|
+
patternToken.tokens,
|
|
1171
|
+
captured,
|
|
1172
|
+
parentStopMarkers
|
|
640
1173
|
);
|
|
641
|
-
if (
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (possessiveValue) {
|
|
646
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
647
|
-
if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
|
|
648
|
-
return patternToken.optional || false;
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
captured.set(patternToken.role, possessiveValue);
|
|
652
|
-
return true;
|
|
653
|
-
}
|
|
654
|
-
const methodCallValue = this.tryMatchMethodCallExpression(tokens);
|
|
655
|
-
if (methodCallValue) {
|
|
656
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
657
|
-
if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
|
|
658
|
-
return patternToken.optional || false;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
captured.set(patternToken.role, methodCallValue);
|
|
662
|
-
return true;
|
|
663
|
-
}
|
|
664
|
-
const possessiveSelectorValue = this.tryMatchPossessiveSelectorExpression(tokens);
|
|
665
|
-
if (possessiveSelectorValue) {
|
|
666
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
667
|
-
if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
|
|
668
|
-
return patternToken.optional || false;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
captured.set(patternToken.role, possessiveSelectorValue);
|
|
672
|
-
return true;
|
|
673
|
-
}
|
|
674
|
-
const propertyAccessValue = this.tryMatchPropertyAccessExpression(tokens);
|
|
675
|
-
if (propertyAccessValue) {
|
|
676
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
677
|
-
if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
|
|
678
|
-
return patternToken.optional || false;
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
captured.set(patternToken.role, propertyAccessValue);
|
|
682
|
-
return true;
|
|
1174
|
+
if (success) return true;
|
|
1175
|
+
tokens.reset(mark);
|
|
1176
|
+
for (const role of captured.keys()) {
|
|
1177
|
+
if (!capturedBefore.has(role)) captured.delete(role);
|
|
683
1178
|
}
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
713
|
-
if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
|
|
714
|
-
this.logger.debug(" >>> TYPE MISMATCH - returning", patternToken.optional || false);
|
|
715
|
-
return patternToken.optional || false;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
this.logger.debug(" >>> Type validation PASSED");
|
|
719
|
-
captured.set(patternToken.role, value);
|
|
720
|
-
tokens.advance();
|
|
721
|
-
return true;
|
|
722
|
-
}
|
|
723
|
-
/**
|
|
724
|
-
* Try to match a possessive expression like 'my value' or 'its innerHTML'.
|
|
725
|
-
* Returns the PropertyPathValue if matched, or null if not.
|
|
726
|
-
*/
|
|
727
|
-
tryMatchPossessiveExpression(tokens) {
|
|
728
|
-
const token = tokens.peek();
|
|
729
|
-
if (!token) return null;
|
|
730
|
-
if (!this.currentProfile) return null;
|
|
731
|
-
const tokenValue = token.normalized || token.value;
|
|
732
|
-
const tokenLower = this.safeToLowerCase(tokenValue);
|
|
733
|
-
const baseRef = getPossessiveReference(this.currentProfile, tokenLower);
|
|
734
|
-
if (!baseRef) return null;
|
|
735
|
-
const mark = tokens.mark();
|
|
736
|
-
tokens.advance();
|
|
737
|
-
const propertyToken = tokens.peek();
|
|
738
|
-
if (!propertyToken) {
|
|
739
|
-
tokens.reset(mark);
|
|
740
|
-
return null;
|
|
741
|
-
}
|
|
742
|
-
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)) {
|
|
743
|
-
tokens.advance();
|
|
744
|
-
let propertyName = propertyToken.value;
|
|
745
|
-
if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
|
|
746
|
-
propertyName = propertyName.substring(1);
|
|
747
|
-
}
|
|
748
|
-
let chainedProps = propertyName;
|
|
749
|
-
while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
|
|
750
|
-
chainedProps += tokens.peek().value;
|
|
751
|
-
tokens.advance();
|
|
752
|
-
}
|
|
753
|
-
const nextPeek = tokens.peek();
|
|
754
|
-
if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
|
|
755
|
-
chainedProps += nextPeek.value;
|
|
756
|
-
tokens.advance();
|
|
757
|
-
}
|
|
758
|
-
return (0, import_intent.createPropertyPath)((0, import_intent.createReference)(baseRef), chainedProps);
|
|
759
|
-
}
|
|
760
|
-
tokens.reset(mark);
|
|
761
|
-
return null;
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* Check if a keyword is a structural keyword (preposition, control flow, etc.)
|
|
765
|
-
* that shouldn't be consumed as a property name.
|
|
766
|
-
*/
|
|
767
|
-
isStructuralKeyword(value) {
|
|
768
|
-
const structural = /* @__PURE__ */ new Set([
|
|
769
|
-
// Prepositions
|
|
770
|
-
"into",
|
|
771
|
-
"in",
|
|
772
|
-
"to",
|
|
773
|
-
"from",
|
|
774
|
-
"at",
|
|
775
|
-
"by",
|
|
776
|
-
"with",
|
|
777
|
-
"without",
|
|
778
|
-
"before",
|
|
779
|
-
"after",
|
|
780
|
-
"of",
|
|
781
|
-
"as",
|
|
782
|
-
"on",
|
|
783
|
-
// Control flow
|
|
784
|
-
"then",
|
|
785
|
-
"end",
|
|
786
|
-
"else",
|
|
787
|
-
"if",
|
|
788
|
-
"repeat",
|
|
789
|
-
"while",
|
|
790
|
-
"for",
|
|
791
|
-
// Commands (shouldn't be property names)
|
|
792
|
-
"toggle",
|
|
793
|
-
"add",
|
|
794
|
-
"remove",
|
|
795
|
-
"put",
|
|
796
|
-
"set",
|
|
797
|
-
"show",
|
|
798
|
-
"hide",
|
|
799
|
-
"increment",
|
|
800
|
-
"decrement",
|
|
801
|
-
"send",
|
|
802
|
-
"trigger",
|
|
803
|
-
"call"
|
|
804
|
-
]);
|
|
805
|
-
return structural.has(value.toLowerCase());
|
|
806
|
-
}
|
|
807
|
-
/**
|
|
808
|
-
* Try to match a method call expression like '#dialog.showModal()'.
|
|
809
|
-
* Pattern: selector + '.' + identifier + '(' + [args] + ')'
|
|
810
|
-
* Returns an expression value if matched, or null if not.
|
|
811
|
-
*/
|
|
812
|
-
tryMatchMethodCallExpression(tokens) {
|
|
813
|
-
const token = tokens.peek();
|
|
814
|
-
if (!token || token.kind !== "selector") return null;
|
|
815
|
-
const mark = tokens.mark();
|
|
816
|
-
tokens.advance();
|
|
817
|
-
const dotToken = tokens.peek();
|
|
818
|
-
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
819
|
-
tokens.reset(mark);
|
|
820
|
-
return null;
|
|
821
|
-
}
|
|
822
|
-
tokens.advance();
|
|
823
|
-
const methodToken = tokens.peek();
|
|
824
|
-
if (!methodToken || methodToken.kind !== "identifier") {
|
|
825
|
-
tokens.reset(mark);
|
|
826
|
-
return null;
|
|
827
|
-
}
|
|
828
|
-
tokens.advance();
|
|
829
|
-
const openParen = tokens.peek();
|
|
830
|
-
if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
|
|
831
|
-
tokens.reset(mark);
|
|
832
|
-
return null;
|
|
833
|
-
}
|
|
834
|
-
tokens.advance();
|
|
835
|
-
const args = [];
|
|
836
|
-
while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
|
|
837
|
-
const argToken = tokens.peek();
|
|
838
|
-
if (!argToken) break;
|
|
839
|
-
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
840
|
-
tokens.advance();
|
|
841
|
-
break;
|
|
842
|
-
}
|
|
843
|
-
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
844
|
-
tokens.advance();
|
|
845
|
-
continue;
|
|
846
|
-
}
|
|
847
|
-
args.push(argToken.value);
|
|
848
|
-
tokens.advance();
|
|
849
|
-
}
|
|
850
|
-
const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
|
|
851
|
-
return {
|
|
852
|
-
type: "expression",
|
|
853
|
-
raw: methodCall
|
|
854
|
-
};
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* Try to match a property access expression like 'userData.name' or 'it.data'.
|
|
858
|
-
* Pattern: (identifier | keyword) + '.' + identifier [+ '.' + identifier ...]
|
|
859
|
-
* Returns an expression value if matched, or null if not.
|
|
860
|
-
*/
|
|
861
|
-
tryMatchPropertyAccessExpression(tokens) {
|
|
862
|
-
const token = tokens.peek();
|
|
863
|
-
if (!token) return null;
|
|
864
|
-
if (token.kind !== "identifier" && token.kind !== "keyword") return null;
|
|
865
|
-
const mark = tokens.mark();
|
|
866
|
-
tokens.advance();
|
|
867
|
-
const dotToken = tokens.peek();
|
|
868
|
-
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
869
|
-
tokens.reset(mark);
|
|
870
|
-
return null;
|
|
871
|
-
}
|
|
872
|
-
tokens.advance();
|
|
873
|
-
const propertyToken = tokens.peek();
|
|
874
|
-
if (!propertyToken || propertyToken.kind !== "identifier") {
|
|
875
|
-
tokens.reset(mark);
|
|
876
|
-
return null;
|
|
877
|
-
}
|
|
878
|
-
tokens.advance();
|
|
879
|
-
let chain = `${token.value}.${propertyToken.value}`;
|
|
880
|
-
let depth = 1;
|
|
881
|
-
while (!tokens.isAtEnd() && depth < _PatternMatcher.MAX_PROPERTY_DEPTH) {
|
|
882
|
-
const nextDot = tokens.peek();
|
|
883
|
-
if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
|
|
884
|
-
break;
|
|
885
|
-
}
|
|
886
|
-
tokens.advance();
|
|
887
|
-
const nextProp = tokens.peek();
|
|
888
|
-
if (!nextProp || nextProp.kind !== "identifier") {
|
|
889
|
-
break;
|
|
890
|
-
}
|
|
891
|
-
tokens.advance();
|
|
892
|
-
chain += `.${nextProp.value}`;
|
|
893
|
-
depth++;
|
|
894
|
-
}
|
|
895
|
-
const openParen = tokens.peek();
|
|
896
|
-
if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
|
|
897
|
-
tokens.advance();
|
|
898
|
-
const args = [];
|
|
899
|
-
let argDepth = 0;
|
|
900
|
-
while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
|
|
901
|
-
const argToken = tokens.peek();
|
|
902
|
-
if (!argToken) break;
|
|
903
|
-
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
904
|
-
if (argDepth === 0) {
|
|
905
|
-
tokens.advance();
|
|
906
|
-
break;
|
|
907
|
-
}
|
|
908
|
-
argDepth--;
|
|
909
|
-
}
|
|
910
|
-
if (argToken.kind === "punctuation" && argToken.value === "(") {
|
|
911
|
-
argDepth++;
|
|
912
|
-
}
|
|
913
|
-
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
914
|
-
tokens.advance();
|
|
915
|
-
continue;
|
|
916
|
-
}
|
|
917
|
-
args.push(argToken.value);
|
|
918
|
-
tokens.advance();
|
|
919
|
-
}
|
|
920
|
-
const methodCall = `${chain}(${args.join(", ")})`;
|
|
921
|
-
return {
|
|
922
|
-
type: "expression",
|
|
923
|
-
raw: methodCall
|
|
924
|
-
};
|
|
925
|
-
}
|
|
926
|
-
return {
|
|
927
|
-
type: "expression",
|
|
928
|
-
raw: chain
|
|
929
|
-
};
|
|
930
|
-
}
|
|
931
|
-
/**
|
|
932
|
-
* Try to match a possessive selector expression like "#element's *opacity".
|
|
933
|
-
* Pattern: selector + "'s" + (selector | identifier)
|
|
934
|
-
* Returns a property-path value if matched, or null if not.
|
|
935
|
-
*/
|
|
936
|
-
tryMatchPossessiveSelectorExpression(tokens) {
|
|
937
|
-
const token = tokens.peek();
|
|
938
|
-
if (!token || token.kind !== "selector") return null;
|
|
939
|
-
const mark = tokens.mark();
|
|
940
|
-
tokens.advance();
|
|
941
|
-
const possessiveToken = tokens.peek();
|
|
942
|
-
if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
|
|
943
|
-
tokens.reset(mark);
|
|
944
|
-
return null;
|
|
945
|
-
}
|
|
946
|
-
tokens.advance();
|
|
947
|
-
const propertyToken = tokens.peek();
|
|
948
|
-
if (!propertyToken) {
|
|
949
|
-
tokens.reset(mark);
|
|
950
|
-
return null;
|
|
951
|
-
}
|
|
952
|
-
if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
|
|
953
|
-
tokens.reset(mark);
|
|
954
|
-
return null;
|
|
955
|
-
}
|
|
956
|
-
tokens.advance();
|
|
957
|
-
return (0, import_intent.createPropertyPath)((0, import_intent.createSelector)(token.value), propertyToken.value);
|
|
958
|
-
}
|
|
959
|
-
/**
|
|
960
|
-
* Try to match a selector + property expression like "#output.innerText".
|
|
961
|
-
* This handles cases where the tokenizer produces two selector tokens:
|
|
962
|
-
* - #output (id selector)
|
|
963
|
-
* - .innerText (looks like class selector, but is actually property)
|
|
964
|
-
*
|
|
965
|
-
* Pattern: id-selector + class-selector-that-is-actually-property
|
|
966
|
-
* Returns a property-path value if matched, or null if not.
|
|
967
|
-
*/
|
|
968
|
-
tryMatchSelectorPropertyExpression(tokens) {
|
|
969
|
-
const token = tokens.peek();
|
|
970
|
-
if (!token || token.kind !== "selector") return null;
|
|
971
|
-
if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
|
|
972
|
-
const mark = tokens.mark();
|
|
973
|
-
tokens.advance();
|
|
974
|
-
const propertyToken = tokens.peek();
|
|
975
|
-
if (!propertyToken || propertyToken.kind !== "selector") {
|
|
976
|
-
tokens.reset(mark);
|
|
977
|
-
return null;
|
|
978
|
-
}
|
|
979
|
-
if (!propertyToken.value.startsWith(".")) {
|
|
980
|
-
tokens.reset(mark);
|
|
981
|
-
return null;
|
|
982
|
-
}
|
|
983
|
-
const peek2 = tokens.peek(1);
|
|
984
|
-
if (peek2 && peek2.kind === "selector") {
|
|
985
|
-
}
|
|
986
|
-
tokens.advance();
|
|
987
|
-
const propertyName = propertyToken.value.slice(1);
|
|
988
|
-
return (0, import_intent.createPropertyPath)((0, import_intent.createSelector)(token.value), propertyName);
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* Match a group pattern token (optional sequence).
|
|
992
|
-
* When the group's leading marker isn't at the current position, scans ahead
|
|
993
|
-
* up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
|
|
994
|
-
* (e.g., a value like 'hello') to sit between groups without blocking later
|
|
995
|
-
* marker matches.
|
|
996
|
-
*/
|
|
997
|
-
matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
|
|
998
|
-
const mark = tokens.mark();
|
|
999
|
-
const capturedBefore = new Set(captured.keys());
|
|
1000
|
-
const success = this.matchTokenSequence(
|
|
1001
|
-
tokens,
|
|
1002
|
-
patternToken.tokens,
|
|
1003
|
-
captured,
|
|
1004
|
-
parentStopMarkers
|
|
1005
|
-
);
|
|
1006
|
-
if (success) return true;
|
|
1007
|
-
tokens.reset(mark);
|
|
1008
|
-
for (const role of captured.keys()) {
|
|
1009
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
1010
|
-
}
|
|
1011
|
-
if (!patternToken.optional) return false;
|
|
1012
|
-
const leadingMarker = this.getGroupLeadingMarker(patternToken);
|
|
1013
|
-
if (leadingMarker) {
|
|
1014
|
-
for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
|
|
1015
|
-
const ahead = tokens.peek(offset);
|
|
1016
|
-
if (!ahead) break;
|
|
1017
|
-
const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
|
|
1018
|
-
if (aheadValue === leadingMarker) {
|
|
1019
|
-
this.logger.debug(
|
|
1020
|
-
" >> MARKER SCAN: found",
|
|
1021
|
-
leadingMarker,
|
|
1022
|
-
"at offset",
|
|
1023
|
-
offset,
|
|
1024
|
-
"- skipping intervening tokens"
|
|
1025
|
-
);
|
|
1026
|
-
for (let s = 0; s < offset; s++) tokens.advance();
|
|
1027
|
-
const retrySuccess = this.matchTokenSequence(
|
|
1028
|
-
tokens,
|
|
1029
|
-
patternToken.tokens,
|
|
1030
|
-
captured,
|
|
1031
|
-
parentStopMarkers
|
|
1032
|
-
);
|
|
1033
|
-
if (retrySuccess) return true;
|
|
1034
|
-
tokens.reset(mark);
|
|
1035
|
-
for (const role of captured.keys()) {
|
|
1036
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
1037
|
-
}
|
|
1038
|
-
break;
|
|
1179
|
+
if (!patternToken.optional) return false;
|
|
1180
|
+
const leadingMarker = this.getGroupLeadingMarker(patternToken);
|
|
1181
|
+
if (leadingMarker) {
|
|
1182
|
+
for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
|
|
1183
|
+
const ahead = tokens.peek(offset);
|
|
1184
|
+
if (!ahead) break;
|
|
1185
|
+
const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
|
|
1186
|
+
if (aheadValue === leadingMarker) {
|
|
1187
|
+
this.logger.debug(
|
|
1188
|
+
" >> MARKER SCAN: found",
|
|
1189
|
+
leadingMarker,
|
|
1190
|
+
"at offset",
|
|
1191
|
+
offset,
|
|
1192
|
+
"- skipping intervening tokens"
|
|
1193
|
+
);
|
|
1194
|
+
for (let s = 0; s < offset; s++) tokens.advance();
|
|
1195
|
+
const retrySuccess = this.matchTokenSequence(
|
|
1196
|
+
tokens,
|
|
1197
|
+
patternToken.tokens,
|
|
1198
|
+
captured,
|
|
1199
|
+
parentStopMarkers
|
|
1200
|
+
);
|
|
1201
|
+
if (retrySuccess) return true;
|
|
1202
|
+
tokens.reset(mark);
|
|
1203
|
+
for (const role of captured.keys()) {
|
|
1204
|
+
if (!capturedBefore.has(role)) captured.delete(role);
|
|
1205
|
+
}
|
|
1206
|
+
break;
|
|
1039
1207
|
}
|
|
1040
1208
|
}
|
|
1041
1209
|
}
|
|
@@ -1062,7 +1230,7 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
1062
1230
|
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
1063
1231
|
return "stem";
|
|
1064
1232
|
}
|
|
1065
|
-
if (token.kind === "keyword" &&
|
|
1233
|
+
if (token.kind === "keyword" && safeToLowerCase(token.value) === value.toLowerCase()) {
|
|
1066
1234
|
return "case-insensitive";
|
|
1067
1235
|
}
|
|
1068
1236
|
return "none";
|
|
@@ -1110,62 +1278,9 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
1110
1278
|
if (token.normalized && stopMarkers.has(token.normalized.toLowerCase())) return true;
|
|
1111
1279
|
return false;
|
|
1112
1280
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
tokenToSemanticValue(token) {
|
|
1117
|
-
switch (token.kind) {
|
|
1118
|
-
case "selector":
|
|
1119
|
-
return (0, import_intent.createSelector)(token.value);
|
|
1120
|
-
case "literal":
|
|
1121
|
-
return this.parseLiteralValue(token.value);
|
|
1122
|
-
case "keyword":
|
|
1123
|
-
const tokenValue = token.normalized || token.value;
|
|
1124
|
-
const lower = this.safeToLowerCase(tokenValue);
|
|
1125
|
-
if (isValidReference(lower)) {
|
|
1126
|
-
return (0, import_intent.createReference)(lower);
|
|
1127
|
-
}
|
|
1128
|
-
return (0, import_intent.createLiteral)(token.normalized || token.value);
|
|
1129
|
-
case "identifier":
|
|
1130
|
-
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
1131
|
-
return (0, import_intent.createReference)(token.value);
|
|
1132
|
-
}
|
|
1133
|
-
const identLower = this.safeToLowerCase(token.value);
|
|
1134
|
-
if (isValidReference(identLower)) {
|
|
1135
|
-
return (0, import_intent.createReference)(identLower);
|
|
1136
|
-
}
|
|
1137
|
-
return { type: "expression", raw: token.value };
|
|
1138
|
-
case "url":
|
|
1139
|
-
return (0, import_intent.createLiteral)(token.value, "string");
|
|
1140
|
-
default:
|
|
1141
|
-
return null;
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
/**
|
|
1145
|
-
* Parse a literal value (string, number, boolean).
|
|
1146
|
-
*/
|
|
1147
|
-
parseLiteralValue(value) {
|
|
1148
|
-
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
1149
|
-
const inner = value.slice(1, -1);
|
|
1150
|
-
return (0, import_intent.createLiteral)(inner, "string");
|
|
1151
|
-
}
|
|
1152
|
-
if (value === "true") return (0, import_intent.createLiteral)(true, "boolean");
|
|
1153
|
-
if (value === "false") return (0, import_intent.createLiteral)(false, "boolean");
|
|
1154
|
-
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
1155
|
-
if (durationMatch) {
|
|
1156
|
-
const num2 = parseFloat(durationMatch[1]);
|
|
1157
|
-
const unit = durationMatch[2];
|
|
1158
|
-
if (unit) {
|
|
1159
|
-
return (0, import_intent.createLiteral)(value, "duration");
|
|
1160
|
-
}
|
|
1161
|
-
return (0, import_intent.createLiteral)(num2, "number");
|
|
1162
|
-
}
|
|
1163
|
-
const num = parseFloat(value);
|
|
1164
|
-
if (!isNaN(num)) {
|
|
1165
|
-
return (0, import_intent.createLiteral)(num, "number");
|
|
1166
|
-
}
|
|
1167
|
-
return (0, import_intent.createLiteral)(value, "string");
|
|
1168
|
-
}
|
|
1281
|
+
// ==========================================================================
|
|
1282
|
+
// Depth Limits for Expression Parsing (security hardening)
|
|
1283
|
+
// ==========================================================================
|
|
1169
1284
|
/**
|
|
1170
1285
|
* Apply extraction rules to fill in static values and defaults for missing roles.
|
|
1171
1286
|
*/
|
|
@@ -1186,260 +1301,634 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
1186
1301
|
isOptional(patternToken) {
|
|
1187
1302
|
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
1188
1303
|
}
|
|
1304
|
+
// ===========================================================================
|
|
1305
|
+
// English Idiom Support - Noise Word Handling
|
|
1306
|
+
// ===========================================================================
|
|
1189
1307
|
/**
|
|
1190
|
-
*
|
|
1191
|
-
*
|
|
1192
|
-
*
|
|
1193
|
-
* - Stem matches (morphological normalization has inherent uncertainty)
|
|
1194
|
-
* - Missing optional roles (but less penalty if role has a default value)
|
|
1308
|
+
* Extract event modifiers from the token stream.
|
|
1309
|
+
* Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
|
|
1310
|
+
* that can appear after event names.
|
|
1195
1311
|
*
|
|
1196
|
-
*
|
|
1197
|
-
* - VSO languages (Arabic) when pattern starts with a verb
|
|
1312
|
+
* Returns EventModifiers object or undefined if no modifiers found.
|
|
1198
1313
|
*/
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
let
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
} else if (roleHasDefault) {
|
|
1220
|
-
score += weight * 0.6;
|
|
1221
|
-
}
|
|
1314
|
+
extractEventModifiers(tokens) {
|
|
1315
|
+
const modifiers = {};
|
|
1316
|
+
let foundModifier = false;
|
|
1317
|
+
while (!tokens.isAtEnd()) {
|
|
1318
|
+
const token = tokens.peek();
|
|
1319
|
+
if (!token || token.kind !== "event-modifier") {
|
|
1320
|
+
break;
|
|
1321
|
+
}
|
|
1322
|
+
const metadata = token.metadata;
|
|
1323
|
+
if (!metadata) {
|
|
1324
|
+
break;
|
|
1325
|
+
}
|
|
1326
|
+
foundModifier = true;
|
|
1327
|
+
switch (metadata.modifierName) {
|
|
1328
|
+
case "once":
|
|
1329
|
+
modifiers.once = true;
|
|
1330
|
+
break;
|
|
1331
|
+
case "debounce":
|
|
1332
|
+
if (typeof metadata.value === "number") {
|
|
1333
|
+
modifiers.debounce = metadata.value;
|
|
1222
1334
|
}
|
|
1223
|
-
|
|
1335
|
+
break;
|
|
1336
|
+
case "throttle":
|
|
1337
|
+
if (typeof metadata.value === "number") {
|
|
1338
|
+
modifiers.throttle = metadata.value;
|
|
1339
|
+
}
|
|
1340
|
+
break;
|
|
1341
|
+
case "queue":
|
|
1342
|
+
if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
|
|
1343
|
+
modifiers.queue = metadata.value;
|
|
1344
|
+
}
|
|
1345
|
+
break;
|
|
1224
1346
|
}
|
|
1347
|
+
tokens.advance();
|
|
1225
1348
|
}
|
|
1226
|
-
|
|
1227
|
-
if (this.stemMatchCount > 0 && this.totalKeywordMatches > 0) {
|
|
1228
|
-
const stemPenalty = this.stemMatchCount / this.totalKeywordMatches * 0.15;
|
|
1229
|
-
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
1230
|
-
}
|
|
1231
|
-
const vsoBoost = this.calculateVSOConfidenceBoost(pattern);
|
|
1232
|
-
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
1233
|
-
const prepositionAdjustment = this.arabicPrepositionDisambiguation(pattern, captured);
|
|
1234
|
-
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
1235
|
-
return baseConfidence;
|
|
1349
|
+
return foundModifier ? modifiers : void 0;
|
|
1236
1350
|
}
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
const
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
"\u0623\u0636\u0641",
|
|
1261
|
-
"\u0623\u0632\u0644",
|
|
1262
|
-
"\u0636\u0639",
|
|
1263
|
-
"\u0627\u062C\u0639\u0644",
|
|
1264
|
-
"\u0639\u064A\u0646",
|
|
1265
|
-
"\u0632\u062F",
|
|
1266
|
-
"\u0627\u0646\u0642\u0635",
|
|
1267
|
-
"\u0633\u062C\u0644",
|
|
1268
|
-
"\u0623\u0638\u0647\u0631",
|
|
1269
|
-
"\u0623\u062E\u0641",
|
|
1270
|
-
"\u0634\u063A\u0644",
|
|
1271
|
-
"\u0623\u0631\u0633\u0644",
|
|
1272
|
-
"\u0631\u0643\u0632",
|
|
1273
|
-
"\u0634\u0648\u0634",
|
|
1274
|
-
"\u062A\u0648\u0642\u0641",
|
|
1275
|
-
"\u0627\u0646\u0633\u062E",
|
|
1276
|
-
"\u0627\u062D\u0630\u0641",
|
|
1277
|
-
"\u0627\u0635\u0646\u0639",
|
|
1278
|
-
"\u0627\u0646\u062A\u0638\u0631",
|
|
1279
|
-
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
1280
|
-
"\u0623\u0648"
|
|
1281
|
-
]);
|
|
1282
|
-
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
1283
|
-
return 0.15;
|
|
1284
|
-
}
|
|
1285
|
-
if (firstToken.alternatives) {
|
|
1286
|
-
for (const alt of firstToken.alternatives) {
|
|
1287
|
-
if (ARABIC_VERBS.has(alt)) {
|
|
1288
|
-
return 0.15;
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1351
|
+
};
|
|
1352
|
+
/** Maximum tokens to scan ahead when looking for a group's leading marker */
|
|
1353
|
+
_PatternMatcher.MAX_MARKER_SCAN = 3;
|
|
1354
|
+
var PatternMatcher = _PatternMatcher;
|
|
1355
|
+
var patternMatcher = new PatternMatcher();
|
|
1356
|
+
function matchPattern(tokens, pattern) {
|
|
1357
|
+
return patternMatcher.matchPattern(tokens, pattern);
|
|
1358
|
+
}
|
|
1359
|
+
function matchBest(tokens, patterns) {
|
|
1360
|
+
return patternMatcher.matchBest(tokens, patterns);
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
// src/core/tokenization/token-utils.ts
|
|
1364
|
+
var TokenStreamImpl = class {
|
|
1365
|
+
constructor(tokens, language) {
|
|
1366
|
+
this.pos = 0;
|
|
1367
|
+
this.tokens = tokens;
|
|
1368
|
+
this.language = language;
|
|
1369
|
+
}
|
|
1370
|
+
peek(offset = 0) {
|
|
1371
|
+
const index = this.pos + offset;
|
|
1372
|
+
if (index < 0 || index >= this.tokens.length) {
|
|
1373
|
+
return null;
|
|
1291
1374
|
}
|
|
1292
|
-
return
|
|
1375
|
+
return this.tokens[index];
|
|
1376
|
+
}
|
|
1377
|
+
advance() {
|
|
1378
|
+
if (this.isAtEnd()) {
|
|
1379
|
+
throw new Error("Unexpected end of token stream");
|
|
1380
|
+
}
|
|
1381
|
+
return this.tokens[this.pos++];
|
|
1382
|
+
}
|
|
1383
|
+
isAtEnd() {
|
|
1384
|
+
return this.pos >= this.tokens.length;
|
|
1385
|
+
}
|
|
1386
|
+
mark() {
|
|
1387
|
+
return { position: this.pos };
|
|
1388
|
+
}
|
|
1389
|
+
reset(mark) {
|
|
1390
|
+
this.pos = mark.position;
|
|
1391
|
+
}
|
|
1392
|
+
position() {
|
|
1393
|
+
return this.pos;
|
|
1293
1394
|
}
|
|
1294
1395
|
/**
|
|
1295
|
-
*
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
*
|
|
1302
|
-
*
|
|
1303
|
-
* This method analyzes the prepositions used with captured semantic roles and
|
|
1304
|
-
* adjusts confidence based on idiomaticity:
|
|
1305
|
-
* - +0.10 for highly idiomatic preposition choices
|
|
1306
|
-
* - -0.10 for less natural preposition choices
|
|
1307
|
-
*
|
|
1308
|
-
* @param pattern The language pattern being matched
|
|
1309
|
-
* @param captured The captured semantic values
|
|
1310
|
-
* @returns Confidence adjustment (-0.10 to +0.10)
|
|
1396
|
+
* Get remaining tokens as an array.
|
|
1397
|
+
*/
|
|
1398
|
+
remaining() {
|
|
1399
|
+
return this.tokens.slice(this.pos);
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Consume tokens while predicate is true.
|
|
1311
1403
|
*/
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1404
|
+
takeWhile(predicate) {
|
|
1405
|
+
const result = [];
|
|
1406
|
+
while (!this.isAtEnd() && predicate(this.peek())) {
|
|
1407
|
+
result.push(this.advance());
|
|
1408
|
+
}
|
|
1409
|
+
return result;
|
|
1410
|
+
}
|
|
1411
|
+
/**
|
|
1412
|
+
* Skip tokens while predicate is true.
|
|
1413
|
+
*/
|
|
1414
|
+
skipWhile(predicate) {
|
|
1415
|
+
while (!this.isAtEnd() && predicate(this.peek())) {
|
|
1416
|
+
this.advance();
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
};
|
|
1420
|
+
function createPosition(start, end) {
|
|
1421
|
+
return { start, end };
|
|
1422
|
+
}
|
|
1423
|
+
function createToken(valueOrParams, kind, position, normalizedOrOptions) {
|
|
1424
|
+
if (typeof valueOrParams === "object") {
|
|
1425
|
+
const { value: value2, kind: kind2, position: position2, normalized: normalized2, stem, stemConfidence, metadata } = valueOrParams;
|
|
1426
|
+
return {
|
|
1427
|
+
value: value2,
|
|
1428
|
+
kind: kind2,
|
|
1429
|
+
position: position2,
|
|
1430
|
+
...normalized2 !== void 0 && { normalized: normalized2 },
|
|
1431
|
+
...stem !== void 0 && { stem },
|
|
1432
|
+
...stemConfidence !== void 0 && { stemConfidence },
|
|
1433
|
+
...metadata !== void 0 && { metadata }
|
|
1334
1434
|
};
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1435
|
+
}
|
|
1436
|
+
const value = valueOrParams;
|
|
1437
|
+
if (!kind || !position) {
|
|
1438
|
+
throw new Error("createToken requires kind and position parameters");
|
|
1439
|
+
}
|
|
1440
|
+
if (typeof normalizedOrOptions === "string") {
|
|
1441
|
+
return { value, kind, position, normalized: normalizedOrOptions };
|
|
1442
|
+
}
|
|
1443
|
+
if (normalizedOrOptions) {
|
|
1444
|
+
const { normalized: normalized2, stem, stemConfidence, metadata } = normalizedOrOptions;
|
|
1445
|
+
return {
|
|
1446
|
+
value,
|
|
1447
|
+
kind,
|
|
1448
|
+
position,
|
|
1449
|
+
...normalized2 !== void 0 && { normalized: normalized2 },
|
|
1450
|
+
...stem !== void 0 && { stem },
|
|
1451
|
+
...stemConfidence !== void 0 && { stemConfidence },
|
|
1452
|
+
...metadata !== void 0 && { metadata }
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
return { value, kind, position };
|
|
1456
|
+
}
|
|
1457
|
+
function isWhitespace(char) {
|
|
1458
|
+
return /\s/.test(char);
|
|
1459
|
+
}
|
|
1460
|
+
function isSelectorStart(char) {
|
|
1461
|
+
return char === "#" || char === "." || char === "[" || char === "@" || char === "*" || char === "<";
|
|
1462
|
+
}
|
|
1463
|
+
function isQuote(char) {
|
|
1464
|
+
return char === '"' || char === "'" || char === "`" || char === "\u300C" || char === "\u300D";
|
|
1465
|
+
}
|
|
1466
|
+
function isDigit(char) {
|
|
1467
|
+
return /\d/.test(char);
|
|
1468
|
+
}
|
|
1469
|
+
function stripOptionalDiacritics(word) {
|
|
1470
|
+
return word.replace(/[\u064b-\u0652\u0670]/g, "");
|
|
1471
|
+
}
|
|
1472
|
+
function isAsciiLetter(char) {
|
|
1473
|
+
return /[a-zA-Z]/.test(char);
|
|
1474
|
+
}
|
|
1475
|
+
function isAsciiIdentifierChar(char) {
|
|
1476
|
+
return /[a-zA-Z0-9_-]/.test(char);
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// src/core/matching/match-skeleton.ts
|
|
1480
|
+
function groupHasRole(group) {
|
|
1481
|
+
return group.tokens.some((t) => t.type === "role" || t.type === "group" && groupHasRole(t));
|
|
1482
|
+
}
|
|
1483
|
+
var MatchSkeleton = class {
|
|
1484
|
+
/**
|
|
1485
|
+
* @param makeReader - builds the grammar's reader, handed this matcher's view
|
|
1486
|
+
* of the match in progress (one reader per matcher).
|
|
1487
|
+
*/
|
|
1488
|
+
constructor(makeReader, confidenceModel) {
|
|
1489
|
+
/**
|
|
1490
|
+
* The pattern tokens still to match after the one being matched, through
|
|
1491
|
+
* every enclosing group: a value stops at any marker the pattern still owes.
|
|
1492
|
+
*/
|
|
1493
|
+
this.patternRest = [];
|
|
1494
|
+
/**
|
|
1495
|
+
* Whether the role slot being matched directly follows a literal the pattern
|
|
1496
|
+
* matched: its marker (`[por {quantity}]`), or the command's verb.
|
|
1497
|
+
*/
|
|
1498
|
+
this.slotAfterLiteral = false;
|
|
1499
|
+
/**
|
|
1500
|
+
* Selective memoization cache (Phase 6.1).
|
|
1501
|
+
* Caches pattern match results keyed by `tokenPosition:patternId`.
|
|
1502
|
+
* Cleared before each top-level matchBest() call.
|
|
1503
|
+
*/
|
|
1504
|
+
this.matchCache = /* @__PURE__ */ new Map();
|
|
1505
|
+
/**
|
|
1506
|
+
* Stream index where each role's capture began, for the current
|
|
1507
|
+
* `matchPattern` call. Rebuilt per call, written on the same success path as
|
|
1508
|
+
* `captured` — see `PatternMatchResult.roleStarts`.
|
|
1509
|
+
*/
|
|
1510
|
+
this.roleStarts = /* @__PURE__ */ new Map();
|
|
1511
|
+
/**
|
|
1512
|
+
* Track stem matches for confidence calculation.
|
|
1513
|
+
* This is set during matching and read during confidence calculation.
|
|
1514
|
+
*/
|
|
1515
|
+
this.stemMatchCount = 0;
|
|
1516
|
+
this.totalKeywordMatches = 0;
|
|
1517
|
+
this.confidenceModel = confidenceModel;
|
|
1518
|
+
this.reader = makeReader({
|
|
1519
|
+
patternRest: () => this.patternRest,
|
|
1520
|
+
slotAfterLiteral: () => this.slotAfterLiteral,
|
|
1521
|
+
wouldMatch: (pt, token) => this.patternTokenWouldMatch(pt, token)
|
|
1522
|
+
});
|
|
1523
|
+
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Try to match a single pattern against the token stream.
|
|
1526
|
+
* Returns the match result or null if no match.
|
|
1527
|
+
*/
|
|
1528
|
+
matchPattern(tokens, pattern) {
|
|
1529
|
+
const mark = tokens.mark();
|
|
1530
|
+
const captured = /* @__PURE__ */ new Map();
|
|
1531
|
+
this.reader.beginPattern(pattern);
|
|
1532
|
+
this.stemMatchCount = 0;
|
|
1533
|
+
this.totalKeywordMatches = 0;
|
|
1534
|
+
this.roleStarts = /* @__PURE__ */ new Map();
|
|
1535
|
+
this.patternRest = [];
|
|
1536
|
+
const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
|
|
1537
|
+
if (!success) {
|
|
1538
|
+
tokens.reset(mark);
|
|
1539
|
+
return null;
|
|
1540
|
+
}
|
|
1541
|
+
const confidence = this.confidenceModel.calculate({
|
|
1542
|
+
pattern,
|
|
1543
|
+
captured,
|
|
1544
|
+
stemMatchCount: this.stemMatchCount,
|
|
1545
|
+
totalKeywordMatches: this.totalKeywordMatches
|
|
1546
|
+
});
|
|
1547
|
+
this.applyExtractionRules(pattern, captured);
|
|
1548
|
+
return {
|
|
1549
|
+
pattern,
|
|
1550
|
+
captured,
|
|
1551
|
+
consumedTokens: tokens.position() - mark.position,
|
|
1552
|
+
confidence,
|
|
1553
|
+
roleStarts: this.roleStarts,
|
|
1554
|
+
...this.reader.resultFields?.()
|
|
1555
|
+
};
|
|
1556
|
+
}
|
|
1557
|
+
/**
|
|
1558
|
+
* Try to match multiple patterns, return the best match.
|
|
1559
|
+
*/
|
|
1560
|
+
matchBest(tokens, patterns) {
|
|
1561
|
+
this.matchCache.clear();
|
|
1562
|
+
const matches = [];
|
|
1563
|
+
const startPos = tokens.position();
|
|
1564
|
+
for (const pattern of patterns) {
|
|
1565
|
+
const mark = tokens.mark();
|
|
1566
|
+
const cacheKey = `${startPos}:${pattern.id}`;
|
|
1567
|
+
let result;
|
|
1568
|
+
if (this.matchCache.has(cacheKey)) {
|
|
1569
|
+
result = this.matchCache.get(cacheKey);
|
|
1570
|
+
} else {
|
|
1571
|
+
result = this.matchPattern(tokens, pattern);
|
|
1572
|
+
this.matchCache.set(cacheKey, result);
|
|
1339
1573
|
}
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1574
|
+
if (result) {
|
|
1575
|
+
matches.push(result);
|
|
1576
|
+
}
|
|
1577
|
+
tokens.reset(mark);
|
|
1578
|
+
}
|
|
1579
|
+
if (matches.length === 0) {
|
|
1580
|
+
return null;
|
|
1581
|
+
}
|
|
1582
|
+
matches.sort((a, b) => {
|
|
1583
|
+
const priorityDiff = b.pattern.priority - a.pattern.priority;
|
|
1584
|
+
if (priorityDiff !== 0) return priorityDiff;
|
|
1585
|
+
return b.confidence - a.confidence;
|
|
1586
|
+
});
|
|
1587
|
+
const best = matches[0];
|
|
1588
|
+
this.matchPattern(tokens, best.pattern);
|
|
1589
|
+
return best;
|
|
1590
|
+
}
|
|
1591
|
+
/**
|
|
1592
|
+
* Match a sequence of pattern tokens against the token stream.
|
|
1593
|
+
*/
|
|
1594
|
+
matchTokenSequence(tokens, patternTokens, captured, nextAfterSequence) {
|
|
1595
|
+
this.reader.skipLeading?.(tokens, patternTokens);
|
|
1596
|
+
for (let i = 0; i < patternTokens.length; i++) {
|
|
1597
|
+
const patternToken = patternTokens[i];
|
|
1598
|
+
this.reader.beforePatternToken?.(tokens, patternTokens, i, captured);
|
|
1599
|
+
if (this.reader.maySkipSlot(
|
|
1600
|
+
patternToken,
|
|
1601
|
+
tokens.peek(),
|
|
1602
|
+
patternTokens[i + 1] ?? nextAfterSequence
|
|
1603
|
+
)) {
|
|
1604
|
+
const skipMark = tokens.mark();
|
|
1605
|
+
const speculative = new Map(captured);
|
|
1606
|
+
if (this.matchTokenSequence(
|
|
1607
|
+
tokens,
|
|
1608
|
+
patternTokens.slice(i + 1),
|
|
1609
|
+
speculative,
|
|
1610
|
+
nextAfterSequence
|
|
1611
|
+
) && this.reader.atClauseEnd(tokens)) {
|
|
1612
|
+
captured.clear();
|
|
1613
|
+
for (const [role, value] of speculative) captured.set(role, value);
|
|
1614
|
+
return true;
|
|
1615
|
+
}
|
|
1616
|
+
tokens.reset(skipMark);
|
|
1617
|
+
}
|
|
1618
|
+
const outerRest = this.patternRest;
|
|
1619
|
+
this.patternRest = [...patternTokens.slice(i + 1), ...outerRest];
|
|
1620
|
+
this.slotAfterLiteral = patternToken.type === "role" && patternTokens[i - 1]?.type === "literal";
|
|
1621
|
+
const matched = this.matchPatternToken(
|
|
1622
|
+
tokens,
|
|
1623
|
+
patternToken,
|
|
1624
|
+
captured,
|
|
1625
|
+
patternTokens[i + 1] ?? nextAfterSequence
|
|
1626
|
+
);
|
|
1627
|
+
this.patternRest = outerRest;
|
|
1628
|
+
if (!matched) {
|
|
1629
|
+
if (this.isOptional(patternToken)) {
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
return false;
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
return true;
|
|
1636
|
+
}
|
|
1637
|
+
/**
|
|
1638
|
+
* Match a single pattern token against the current position in the stream.
|
|
1639
|
+
*/
|
|
1640
|
+
matchPatternToken(tokens, patternToken, captured, nextPatternToken) {
|
|
1641
|
+
switch (patternToken.type) {
|
|
1642
|
+
case "literal":
|
|
1643
|
+
return this.matchLiteralToken(tokens, patternToken);
|
|
1644
|
+
case "role": {
|
|
1645
|
+
const roleStart = tokens.position();
|
|
1646
|
+
const matchedRole = this.reader.readRole(tokens, patternToken, captured, nextPatternToken);
|
|
1647
|
+
if (matchedRole && captured.has(patternToken.role)) {
|
|
1648
|
+
this.roleStarts.set(patternToken.role, roleStart);
|
|
1347
1649
|
}
|
|
1650
|
+
return matchedRole;
|
|
1651
|
+
}
|
|
1652
|
+
case "group":
|
|
1653
|
+
return this.matchGroupToken(tokens, patternToken, captured, nextPatternToken);
|
|
1654
|
+
default:
|
|
1655
|
+
return false;
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Match a literal pattern token (keyword or particle).
|
|
1660
|
+
*/
|
|
1661
|
+
matchLiteralToken(tokens, patternToken) {
|
|
1662
|
+
const token = tokens.peek();
|
|
1663
|
+
if (!token) return false;
|
|
1664
|
+
const matchType = this.getMatchType(token, patternToken.value);
|
|
1665
|
+
if (matchType !== "none") {
|
|
1666
|
+
this.totalKeywordMatches++;
|
|
1667
|
+
if (matchType === "stem") {
|
|
1668
|
+
this.stemMatchCount++;
|
|
1348
1669
|
}
|
|
1670
|
+
tokens.advance();
|
|
1671
|
+
return true;
|
|
1349
1672
|
}
|
|
1350
|
-
|
|
1673
|
+
if (patternToken.alternatives) {
|
|
1674
|
+
for (const alt of patternToken.alternatives) {
|
|
1675
|
+
const altMatchType = this.getMatchType(token, alt);
|
|
1676
|
+
if (altMatchType !== "none") {
|
|
1677
|
+
this.totalKeywordMatches++;
|
|
1678
|
+
if (altMatchType === "stem") {
|
|
1679
|
+
this.stemMatchCount++;
|
|
1680
|
+
}
|
|
1681
|
+
tokens.advance();
|
|
1682
|
+
return true;
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
for (const value of [patternToken.value, ...patternToken.alternatives ?? []]) {
|
|
1687
|
+
for (const sep of ["-", "_"]) {
|
|
1688
|
+
if (value.includes(sep) && this.tryMatchShatteredCompound(tokens, value, sep)) {
|
|
1689
|
+
this.totalKeywordMatches++;
|
|
1690
|
+
return true;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
return false;
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Match a compound keyword (`titik-henti`, `по_умолчанию`) against a
|
|
1698
|
+
* shattered token run (`titik` `-` `henti` / `по` `_` `умолчанию`): word
|
|
1699
|
+
* segments must be identifier/keyword/particle tokens equal to the expected
|
|
1700
|
+
* segments (case-insensitive; particle because prepositions like ru `по` /
|
|
1701
|
+
* uk `за` lead some underscore compounds), separators lone `sep` tokens.
|
|
1702
|
+
* Consumes the run on success, resets the stream on failure.
|
|
1703
|
+
*/
|
|
1704
|
+
tryMatchShatteredCompound(tokens, expected, sep) {
|
|
1705
|
+
const segments = expected.split(sep);
|
|
1706
|
+
if (segments.length < 2 || segments.some((s) => s.length === 0)) return false;
|
|
1707
|
+
const mark = tokens.mark();
|
|
1708
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1709
|
+
if (i > 0) {
|
|
1710
|
+
const sepToken = tokens.peek();
|
|
1711
|
+
if (!sepToken || sepToken.value !== sep) {
|
|
1712
|
+
tokens.reset(mark);
|
|
1713
|
+
return false;
|
|
1714
|
+
}
|
|
1715
|
+
tokens.advance();
|
|
1716
|
+
}
|
|
1717
|
+
const word = tokens.peek();
|
|
1718
|
+
if (!word || word.kind !== "identifier" && word.kind !== "keyword" && word.kind !== "particle" || word.value.toLowerCase() !== segments[i].toLowerCase()) {
|
|
1719
|
+
tokens.reset(mark);
|
|
1720
|
+
return false;
|
|
1721
|
+
}
|
|
1722
|
+
tokens.advance();
|
|
1723
|
+
}
|
|
1724
|
+
return true;
|
|
1725
|
+
}
|
|
1726
|
+
/**
|
|
1727
|
+
* Match a group pattern token (optional sequence).
|
|
1728
|
+
*/
|
|
1729
|
+
matchGroupToken(tokens, patternToken, captured, nextPatternToken) {
|
|
1730
|
+
const mark = tokens.mark();
|
|
1731
|
+
const capturedBefore = new Map(captured);
|
|
1732
|
+
let success = this.matchTokenSequence(tokens, patternToken.tokens, captured, nextPatternToken);
|
|
1733
|
+
if (success && tokens.position() > mark.position && groupHasRole(patternToken) && [...captured].every(([role, value]) => capturedBefore.get(role) === value) && captured.size === capturedBefore.size) {
|
|
1734
|
+
success = false;
|
|
1735
|
+
}
|
|
1736
|
+
if (success) {
|
|
1737
|
+
const markerLiterals = [];
|
|
1738
|
+
for (const inner of patternToken.tokens) {
|
|
1739
|
+
if (inner.type === "group" && inner.renderRequired) {
|
|
1740
|
+
for (const t of inner.tokens) {
|
|
1741
|
+
if (t.type === "literal") markerLiterals.push(t.value, ...t.alternatives ?? []);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
if (markerLiterals.length > 0) {
|
|
1746
|
+
const addedRole = [...captured.keys()].some((role) => !capturedBefore.has(role));
|
|
1747
|
+
if (addedRole) {
|
|
1748
|
+
const last = tokens.tokens[tokens.position() - 1];
|
|
1749
|
+
const markerSeen = last !== void 0 && tokens.position() > mark.position && markerLiterals.some((lit) => this.getMatchType(last, lit) !== "none");
|
|
1750
|
+
if (!markerSeen) success = false;
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
if (!success) {
|
|
1755
|
+
tokens.reset(mark);
|
|
1756
|
+
for (const role of captured.keys()) {
|
|
1757
|
+
if (!capturedBefore.has(role)) {
|
|
1758
|
+
captured.delete(role);
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
for (const [role, value] of capturedBefore) {
|
|
1762
|
+
if (captured.get(role) !== value) {
|
|
1763
|
+
captured.set(role, value);
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
return patternToken.optional || false;
|
|
1767
|
+
}
|
|
1768
|
+
return true;
|
|
1769
|
+
}
|
|
1770
|
+
/**
|
|
1771
|
+
* Whether a pattern token (literal, or a group starting with a literal) would
|
|
1772
|
+
* match the given stream token. Used to keep the event-head source-clause
|
|
1773
|
+
* consumption from stealing a marker the pattern explicitly expects.
|
|
1774
|
+
*/
|
|
1775
|
+
patternTokenWouldMatch(pt, token) {
|
|
1776
|
+
if (!pt) return false;
|
|
1777
|
+
if (pt.type === "literal") {
|
|
1778
|
+
if (this.getMatchType(token, pt.value) !== "none") return true;
|
|
1779
|
+
return !!pt.alternatives?.some((a) => this.getMatchType(token, a) !== "none");
|
|
1780
|
+
}
|
|
1781
|
+
if (pt.type === "group") {
|
|
1782
|
+
return this.patternTokenWouldMatch(pt.tokens?.[0], token);
|
|
1783
|
+
}
|
|
1784
|
+
return false;
|
|
1785
|
+
}
|
|
1786
|
+
/**
|
|
1787
|
+
* Get the type of match for a token against a value.
|
|
1788
|
+
* Used for confidence calculation.
|
|
1789
|
+
*/
|
|
1790
|
+
getMatchType(token, value) {
|
|
1791
|
+
if (token.value === value) return "exact";
|
|
1792
|
+
if (token.normalized === value) return "normalized";
|
|
1793
|
+
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
1794
|
+
return "stem";
|
|
1795
|
+
}
|
|
1796
|
+
if (token.kind === "keyword" && token.value.toLowerCase() === value.toLowerCase()) {
|
|
1797
|
+
return "case-insensitive";
|
|
1798
|
+
}
|
|
1799
|
+
if (token.kind === "keyword") {
|
|
1800
|
+
const strippedToken = stripOptionalDiacritics(token.value).toLowerCase();
|
|
1801
|
+
if (strippedToken && strippedToken === stripOptionalDiacritics(value).toLowerCase()) {
|
|
1802
|
+
return "case-insensitive";
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
return "none";
|
|
1806
|
+
}
|
|
1807
|
+
/**
|
|
1808
|
+
* Apply extraction rules to fill in static values and defaults for missing
|
|
1809
|
+
* roles, and re-shape captured roles that declare a `transform` (the
|
|
1810
|
+
* ExtractionRule field was documented but unwired — go-qu-url-dest is the
|
|
1811
|
+
* first consumer: it re-types the marker-bound capture to the expression
|
|
1812
|
+
* shape the en reference produces).
|
|
1813
|
+
*/
|
|
1814
|
+
applyExtractionRules(pattern, captured) {
|
|
1815
|
+
for (const [role, rule] of Object.entries(pattern.extraction)) {
|
|
1816
|
+
const existing = captured.get(role);
|
|
1817
|
+
if (existing) {
|
|
1818
|
+
if (rule.transform) {
|
|
1819
|
+
const raw = existing.type === "literal" ? String(existing.value) : String(existing.raw ?? "");
|
|
1820
|
+
captured.set(role, rule.transform(raw));
|
|
1821
|
+
}
|
|
1822
|
+
} else if (rule.value !== void 0) {
|
|
1823
|
+
captured.set(
|
|
1824
|
+
role,
|
|
1825
|
+
rule.valueIsExpression ? { type: "expression", raw: rule.value } : { type: "literal", value: rule.value }
|
|
1826
|
+
);
|
|
1827
|
+
} else if (rule.default) {
|
|
1828
|
+
const implicitDefault = {
|
|
1829
|
+
...rule.default,
|
|
1830
|
+
implicit: true
|
|
1831
|
+
};
|
|
1832
|
+
captured.set(role, implicitDefault);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
/**
|
|
1837
|
+
* Check if a pattern token is optional.
|
|
1838
|
+
*/
|
|
1839
|
+
isOptional(patternToken) {
|
|
1840
|
+
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
1841
|
+
}
|
|
1842
|
+
};
|
|
1843
|
+
|
|
1844
|
+
// src/core/matching/stock-reader.ts
|
|
1845
|
+
function greedyStopMarkers(rest) {
|
|
1846
|
+
const markers = /* @__PURE__ */ new Set();
|
|
1847
|
+
const add = (pt) => {
|
|
1848
|
+
markers.add(pt.value.toLowerCase());
|
|
1849
|
+
for (const alt of pt.alternatives ?? []) markers.add(alt.toLowerCase());
|
|
1850
|
+
};
|
|
1851
|
+
for (const pt of rest) {
|
|
1852
|
+
if (pt.type === "literal") {
|
|
1853
|
+
add(pt);
|
|
1854
|
+
break;
|
|
1855
|
+
}
|
|
1856
|
+
if (pt.type === "group") {
|
|
1857
|
+
const lead = pt.tokens.find((t) => t.type === "literal");
|
|
1858
|
+
if (lead?.type === "literal") add(lead);
|
|
1859
|
+
if (!pt.optional) break;
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
return markers;
|
|
1863
|
+
}
|
|
1864
|
+
function isStopMarker(token, stopMarkers) {
|
|
1865
|
+
if (stopMarkers.size === 0) return false;
|
|
1866
|
+
if (stopMarkers.has(token.value.toLowerCase())) return true;
|
|
1867
|
+
return !!token.normalized && stopMarkers.has(token.normalized.toLowerCase());
|
|
1868
|
+
}
|
|
1869
|
+
function readGreedy(tokens, stopMarkers) {
|
|
1870
|
+
const values = [];
|
|
1871
|
+
while (!tokens.isAtEnd()) {
|
|
1872
|
+
const next = tokens.peek();
|
|
1873
|
+
if (!next || isStopMarker(next, stopMarkers)) break;
|
|
1874
|
+
values.push(next.value);
|
|
1875
|
+
tokens.advance();
|
|
1876
|
+
}
|
|
1877
|
+
return values.length > 0 ? { type: "expression", raw: values.join(" ") } : null;
|
|
1878
|
+
}
|
|
1879
|
+
var StockValueReader = class {
|
|
1880
|
+
constructor(view, profiles = {}) {
|
|
1881
|
+
this.view = view;
|
|
1882
|
+
this.profiles = profiles;
|
|
1883
|
+
}
|
|
1884
|
+
beginPattern(pattern) {
|
|
1885
|
+
this.profile = this.profiles[pattern.language] ?? { code: pattern.language };
|
|
1886
|
+
this.lastOptional = void 0;
|
|
1351
1887
|
}
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
const tokenLower = this.safeToLowerCase(token.value);
|
|
1360
|
-
if (_PatternMatcher.ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
1361
|
-
const mark = tokens.mark();
|
|
1362
|
-
tokens.advance();
|
|
1363
|
-
const nextToken = tokens.peek();
|
|
1364
|
-
if (nextToken && nextToken.kind === "selector") {
|
|
1365
|
-
return;
|
|
1888
|
+
readRole(tokens, slot, captured, next) {
|
|
1889
|
+
const start = tokens.mark();
|
|
1890
|
+
if (this.readOnce(tokens, slot, captured, next, true)) {
|
|
1891
|
+
if (!slot.optional) {
|
|
1892
|
+
this.lastOptional = void 0;
|
|
1893
|
+
} else if (captured.has(slot.role) && tokens.position() > start.position && !this.view.slotAfterLiteral() && next?.type !== "literal") {
|
|
1894
|
+
this.lastOptional = { role: slot.role, start, end: tokens.position() };
|
|
1366
1895
|
}
|
|
1367
|
-
|
|
1368
|
-
}
|
|
1369
|
-
if (tokenLower === "class") {
|
|
1370
|
-
tokens.advance();
|
|
1896
|
+
return true;
|
|
1371
1897
|
}
|
|
1898
|
+
const before = this.lastOptional;
|
|
1899
|
+
if (slot.optional || !before || before.end !== tokens.position()) return false;
|
|
1900
|
+
this.lastOptional = void 0;
|
|
1901
|
+
tokens.reset(before.start);
|
|
1902
|
+
captured.delete(before.role);
|
|
1903
|
+
return this.readOnce(tokens, slot, captured, next, false);
|
|
1372
1904
|
}
|
|
1373
|
-
/**
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
* that can appear after event names.
|
|
1377
|
-
*
|
|
1378
|
-
* Returns EventModifiers object or undefined if no modifiers found.
|
|
1379
|
-
*/
|
|
1380
|
-
extractEventModifiers(tokens) {
|
|
1381
|
-
const modifiers = {};
|
|
1382
|
-
let foundModifier = false;
|
|
1383
|
-
while (!tokens.isAtEnd()) {
|
|
1905
|
+
/** One read of `slot`; `guard` refuses a required slot the marker the pattern wants next. */
|
|
1906
|
+
readOnce(tokens, slot, captured, next, guard) {
|
|
1907
|
+
if (guard && !slot.optional && next?.type === "literal") {
|
|
1384
1908
|
const token = tokens.peek();
|
|
1385
|
-
if (
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
const
|
|
1389
|
-
if (
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
foundModifier = true;
|
|
1393
|
-
switch (metadata.modifierName) {
|
|
1394
|
-
case "once":
|
|
1395
|
-
modifiers.once = true;
|
|
1396
|
-
break;
|
|
1397
|
-
case "debounce":
|
|
1398
|
-
if (typeof metadata.value === "number") {
|
|
1399
|
-
modifiers.debounce = metadata.value;
|
|
1400
|
-
}
|
|
1401
|
-
break;
|
|
1402
|
-
case "throttle":
|
|
1403
|
-
if (typeof metadata.value === "number") {
|
|
1404
|
-
modifiers.throttle = metadata.value;
|
|
1405
|
-
}
|
|
1406
|
-
break;
|
|
1407
|
-
case "queue":
|
|
1408
|
-
if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
|
|
1409
|
-
modifiers.queue = metadata.value;
|
|
1410
|
-
}
|
|
1411
|
-
break;
|
|
1909
|
+
if (token && this.view.wouldMatch(next, token)) return false;
|
|
1910
|
+
}
|
|
1911
|
+
if (slot.greedy) {
|
|
1912
|
+
const value = readGreedy(tokens, greedyStopMarkers(this.view.patternRest()));
|
|
1913
|
+
if (value) {
|
|
1914
|
+
captured.set(slot.role, value);
|
|
1915
|
+
return true;
|
|
1412
1916
|
}
|
|
1413
|
-
|
|
1917
|
+
return slot.optional ?? false;
|
|
1414
1918
|
}
|
|
1415
|
-
return
|
|
1919
|
+
return readRoleValue(tokens, slot, captured, this.profile);
|
|
1920
|
+
}
|
|
1921
|
+
maySkipSlot(patternToken, token, next) {
|
|
1922
|
+
const slot = patternToken.type === "group" && patternToken.optional ? patternToken.tokens.every((t) => t.type === "role") ? patternToken.tokens[0] : void 0 : patternToken;
|
|
1923
|
+
if (!slot || slot.type !== "role" || !slot.optional) return false;
|
|
1924
|
+
return !!token && this.view.wouldMatch(next, token);
|
|
1925
|
+
}
|
|
1926
|
+
atClauseEnd(tokens) {
|
|
1927
|
+
return tokens.isAtEnd();
|
|
1416
1928
|
}
|
|
1417
1929
|
};
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
// ==========================================================================
|
|
1421
|
-
// Depth Limits for Expression Parsing (security hardening)
|
|
1422
|
-
// ==========================================================================
|
|
1423
|
-
/** Maximum depth for nested property access (e.g., a.b.c.d...) */
|
|
1424
|
-
_PatternMatcher.MAX_PROPERTY_DEPTH = 10;
|
|
1425
|
-
/** Maximum number of arguments in method calls */
|
|
1426
|
-
_PatternMatcher.MAX_METHOD_ARGS = 20;
|
|
1427
|
-
// ===========================================================================
|
|
1428
|
-
// English Idiom Support - Noise Word Handling
|
|
1429
|
-
// ===========================================================================
|
|
1430
|
-
/**
|
|
1431
|
-
* Noise words that can be skipped in English for more natural syntax.
|
|
1432
|
-
* - "the" before selectors: "toggle the .active" → "toggle .active"
|
|
1433
|
-
* - "class" after class selectors: "add the .visible class" → "add .visible"
|
|
1434
|
-
*/
|
|
1435
|
-
_PatternMatcher.ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
1436
|
-
var PatternMatcher = _PatternMatcher;
|
|
1437
|
-
var patternMatcher = new PatternMatcher();
|
|
1438
|
-
function matchPattern(tokens, pattern) {
|
|
1439
|
-
return patternMatcher.matchPattern(tokens, pattern);
|
|
1440
|
-
}
|
|
1441
|
-
function matchBest(tokens, patterns) {
|
|
1442
|
-
return patternMatcher.matchBest(tokens, patterns);
|
|
1930
|
+
function createStockReader(profiles = {}) {
|
|
1931
|
+
return (view) => new StockValueReader(view, profiles);
|
|
1443
1932
|
}
|
|
1444
1933
|
|
|
1445
1934
|
// src/generation/pattern-generator.ts
|
|
@@ -1638,79 +2127,654 @@ function buildTablesFromProfiles(schemas, profiles) {
|
|
|
1638
2127
|
}
|
|
1639
2128
|
}
|
|
1640
2129
|
}
|
|
1641
|
-
return { keywords, markers };
|
|
2130
|
+
return { keywords, markers };
|
|
2131
|
+
}
|
|
2132
|
+
function detectWordOrders(profiles) {
|
|
2133
|
+
const sovLanguages = /* @__PURE__ */ new Set();
|
|
2134
|
+
const vsoLanguages = /* @__PURE__ */ new Set();
|
|
2135
|
+
for (const profile of profiles) {
|
|
2136
|
+
if (profile.wordOrder === "SOV") sovLanguages.add(profile.code);
|
|
2137
|
+
if (profile.wordOrder === "VSO") vsoLanguages.add(profile.code);
|
|
2138
|
+
}
|
|
2139
|
+
return { sovLanguages, vsoLanguages };
|
|
2140
|
+
}
|
|
2141
|
+
function createSchemaRenderer(schemas, profiles) {
|
|
2142
|
+
const tables = buildRenderTables(schemas, profiles);
|
|
2143
|
+
return {
|
|
2144
|
+
render(node, language) {
|
|
2145
|
+
return renderFromSchema(node, language, tables) ?? node.action;
|
|
2146
|
+
}
|
|
2147
|
+
};
|
|
2148
|
+
}
|
|
2149
|
+
function createDomainRenderer(config) {
|
|
2150
|
+
const { schemas, profiles, overrides } = config;
|
|
2151
|
+
let tables;
|
|
2152
|
+
return (node, language) => {
|
|
2153
|
+
const override = overrides?.[node.action];
|
|
2154
|
+
if (override) return override(node, language);
|
|
2155
|
+
tables ?? (tables = buildRenderTables(schemas, profiles));
|
|
2156
|
+
return renderFromSchema(node, language, tables);
|
|
2157
|
+
};
|
|
2158
|
+
}
|
|
2159
|
+
function buildRenderTables(schemas, profiles) {
|
|
2160
|
+
const { keywords, markers } = buildTablesFromProfiles(schemas, profiles);
|
|
2161
|
+
const { sovLanguages } = detectWordOrders(profiles);
|
|
2162
|
+
const markerPositions = {};
|
|
2163
|
+
for (const profile of profiles) {
|
|
2164
|
+
for (const [role, markerDef] of Object.entries(profile.roleMarkers ?? {})) {
|
|
2165
|
+
if (!markerDef.position) continue;
|
|
2166
|
+
markerPositions[role] ?? (markerPositions[role] = {});
|
|
2167
|
+
markerPositions[role][profile.code] = markerDef.position;
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
const schemaMap = /* @__PURE__ */ new Map();
|
|
2171
|
+
for (const s of schemas) schemaMap.set(s.action, s);
|
|
2172
|
+
return { keywords, markers, markerPositions, sovLanguages, schemaMap };
|
|
2173
|
+
}
|
|
2174
|
+
function renderFromSchema(node, language, tables) {
|
|
2175
|
+
const schema = tables.schemaMap.get(node.action);
|
|
2176
|
+
if (!schema) return null;
|
|
2177
|
+
const keyword = lookupKeyword(tables.keywords, node.action, language);
|
|
2178
|
+
const isSOV = tables.sovLanguages.has(language);
|
|
2179
|
+
const ordered = sortRolesByWordOrder([...schema.roles], isSOV ? "SOV" : "SVO");
|
|
2180
|
+
const preVerb = [];
|
|
2181
|
+
const postVerb = [];
|
|
2182
|
+
for (const role of ordered) {
|
|
2183
|
+
let value = (0, import_intent.extractRoleValue)(node, role.role);
|
|
2184
|
+
if (!value && role.default !== void 0) {
|
|
2185
|
+
value = String((0, import_intent.extractValue)(role.default));
|
|
2186
|
+
}
|
|
2187
|
+
if (!value) continue;
|
|
2188
|
+
if (role.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
|
|
2189
|
+
value = `"${value}"`;
|
|
2190
|
+
}
|
|
2191
|
+
const markerText = role.renderOverride?.[language] ?? role.markerOverride?.[language] ?? role.renderOverride?.["*"] ?? tables.markers[role.role]?.[language];
|
|
2192
|
+
const markerPosition = role.markerPositionOverride?.[language] ?? role.markerPosition ?? tables.markerPositions[role.role]?.[language] ?? (isSOV ? "after" : "before");
|
|
2193
|
+
const bucket = isSOV && role.sovSlot === "postVerb" ? postVerb : preVerb;
|
|
2194
|
+
if (markerText && markerPosition === "before") {
|
|
2195
|
+
bucket.push(markerText, value);
|
|
2196
|
+
} else if (markerText) {
|
|
2197
|
+
bucket.push(value, markerText);
|
|
2198
|
+
} else {
|
|
2199
|
+
bucket.push(value);
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
return isSOV ? buildPhrase(...preVerb, keyword, ...postVerb) : buildPhrase(keyword, ...preVerb, ...postVerb);
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
// src/generation/schema-patterns.ts
|
|
2206
|
+
function createSchemaPatternGenerator(grammar) {
|
|
2207
|
+
return {
|
|
2208
|
+
generatePattern: (schema, profile, config) => generatePattern2(grammar, schema, profile, config),
|
|
2209
|
+
generateSimplePattern: (schema, profile, config) => generateSimplePattern(grammar, schema, profile, config),
|
|
2210
|
+
generateVerbFirstPattern: (schema, profile, config) => generateVerbFirstPattern(schema, profile, config),
|
|
2211
|
+
generatePatternVariants: (schema, profile, config) => generatePatternVariants2(grammar, schema, profile, config),
|
|
2212
|
+
buildRoleToken
|
|
2213
|
+
};
|
|
2214
|
+
}
|
|
2215
|
+
function withoutVariantOnlyRoles(schema) {
|
|
2216
|
+
return schema.roles.some((r) => r.variantOnly) ? { ...schema, roles: schema.roles.filter((r) => !r.variantOnly) } : schema;
|
|
2217
|
+
}
|
|
2218
|
+
function sortRolesByPosition(roles, wordOrder, order) {
|
|
2219
|
+
const sortKey = wordOrder === "SOV" ? "sovPosition" : "svoPosition";
|
|
2220
|
+
return [...roles].sort(
|
|
2221
|
+
(a, b) => order === "ascending" ? (a[sortKey] ?? 99) - (b[sortKey] ?? 99) : (b[sortKey] ?? 0) - (a[sortKey] ?? 0)
|
|
2222
|
+
);
|
|
2223
|
+
}
|
|
2224
|
+
function resolveMarkerForRole(roleSpec, profile) {
|
|
2225
|
+
const overrideMarker = roleSpec.markerOverride?.[profile.code];
|
|
2226
|
+
const defaultMarker = profile.roleMarkers[roleSpec.role];
|
|
2227
|
+
if (overrideMarker !== void 0) {
|
|
2228
|
+
const alternatives = legacyMarkerAlternatives(roleSpec, profile.code, overrideMarker);
|
|
2229
|
+
return {
|
|
2230
|
+
primary: overrideMarker,
|
|
2231
|
+
...alternatives && { alternatives },
|
|
2232
|
+
position: defaultMarker?.position ?? "before",
|
|
2233
|
+
isOverride: true
|
|
2234
|
+
};
|
|
2235
|
+
}
|
|
2236
|
+
if (defaultMarker && defaultMarker.primary) {
|
|
2237
|
+
const result = {
|
|
2238
|
+
primary: defaultMarker.primary,
|
|
2239
|
+
position: defaultMarker.position,
|
|
2240
|
+
isOverride: false
|
|
2241
|
+
};
|
|
2242
|
+
if (defaultMarker.alternatives) {
|
|
2243
|
+
result.alternatives = defaultMarker.alternatives;
|
|
2244
|
+
}
|
|
2245
|
+
return result;
|
|
2246
|
+
}
|
|
2247
|
+
return null;
|
|
2248
|
+
}
|
|
2249
|
+
function legacyMarkerAlternatives(roleSpec, languageCode, overrideMarker) {
|
|
2250
|
+
const legacy = roleSpec.markerLegacy?.[languageCode];
|
|
2251
|
+
if (!legacy?.length) return void 0;
|
|
2252
|
+
const alternatives = [...new Set(legacy)].filter((a) => a && a !== overrideMarker);
|
|
2253
|
+
return alternatives.length ? alternatives : void 0;
|
|
2254
|
+
}
|
|
2255
|
+
function schemaMarkerAlternatives(roleSpec, languageCode, marker) {
|
|
2256
|
+
const legacy = roleSpec.markerLegacy?.[languageCode] ?? [];
|
|
2257
|
+
const variants = roleSpec.methodCarrier ? [] : roleSpec.markerVariants?.[languageCode] ?? [];
|
|
2258
|
+
const alternatives = [.../* @__PURE__ */ new Set([...legacy, ...variants])].filter((a) => a && a !== marker);
|
|
2259
|
+
return alternatives.length ? alternatives : void 0;
|
|
2260
|
+
}
|
|
2261
|
+
var defaultConfig2 = {
|
|
2262
|
+
basePriority: 100,
|
|
2263
|
+
generateSimpleVariants: true,
|
|
2264
|
+
generateAlternatives: true
|
|
2265
|
+
};
|
|
2266
|
+
function generatePattern2(grammar, schema, profile, config = defaultConfig2) {
|
|
2267
|
+
const id = `${schema.action}-${profile.code}-generated`;
|
|
2268
|
+
const priority = config.basePriority ?? 100;
|
|
2269
|
+
const keyword = profile.keywords[schema.action];
|
|
2270
|
+
if (!keyword) {
|
|
2271
|
+
throw new Error(`No keyword translation for '${schema.action}' in ${profile.code}`);
|
|
2272
|
+
}
|
|
2273
|
+
const tokens = buildTokens2(grammar, schema, profile, keyword);
|
|
2274
|
+
const extraction = buildExtractionRulesWithDefaults(schema, profile);
|
|
2275
|
+
const format = buildFormatString2(grammar, schema, profile, keyword);
|
|
2276
|
+
return {
|
|
2277
|
+
id,
|
|
2278
|
+
language: profile.code,
|
|
2279
|
+
command: schema.action,
|
|
2280
|
+
priority,
|
|
2281
|
+
template: {
|
|
2282
|
+
format,
|
|
2283
|
+
tokens
|
|
2284
|
+
},
|
|
2285
|
+
extraction
|
|
2286
|
+
};
|
|
2287
|
+
}
|
|
2288
|
+
function generateSimplePattern(grammar, schema, profile, config = defaultConfig2) {
|
|
2289
|
+
const optionalRoles = schema.roles.filter((r) => !r.required);
|
|
2290
|
+
if (optionalRoles.length === 0) {
|
|
2291
|
+
return null;
|
|
2292
|
+
}
|
|
2293
|
+
const requiredRoles = schema.roles.filter((r) => r.required);
|
|
2294
|
+
const simpleSchema = {
|
|
2295
|
+
...schema,
|
|
2296
|
+
roles: requiredRoles
|
|
2297
|
+
};
|
|
2298
|
+
const pattern = generatePattern2(grammar, simpleSchema, profile, config);
|
|
2299
|
+
return {
|
|
2300
|
+
...pattern,
|
|
2301
|
+
id: `${schema.action}-${profile.code}-generated-simple`,
|
|
2302
|
+
priority: (config.basePriority ?? 100) - 5,
|
|
2303
|
+
// Lower priority than full pattern (was -10)
|
|
2304
|
+
extraction: buildExtractionRulesWithDefaults(schema, profile)
|
|
2305
|
+
};
|
|
2306
|
+
}
|
|
2307
|
+
function generateVerbFirstPattern(schema, profile, config = defaultConfig2) {
|
|
2308
|
+
if (profile.wordOrder !== "SOV") return null;
|
|
2309
|
+
const requiredRoles = schema.roles.filter((r) => r.required);
|
|
2310
|
+
if (requiredRoles.length !== 1) return null;
|
|
2311
|
+
const keyword = profile.keywords[schema.action];
|
|
2312
|
+
if (!keyword) return null;
|
|
2313
|
+
const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
|
|
2314
|
+
const roleTokens = requiredRoles.flatMap((r) => {
|
|
2315
|
+
const prefix = r.valuePrefixLiteral?.[profile.code];
|
|
2316
|
+
const roleToken = {
|
|
2317
|
+
type: "role",
|
|
2318
|
+
role: r.role,
|
|
2319
|
+
optional: false,
|
|
2320
|
+
expectedTypes: r.expectedTypes
|
|
2321
|
+
};
|
|
2322
|
+
return prefix ? [{ type: "literal", value: prefix }, roleToken] : [roleToken];
|
|
2323
|
+
});
|
|
2324
|
+
return {
|
|
2325
|
+
id: `${schema.action}-${profile.code}-generated-verb-first`,
|
|
2326
|
+
language: profile.code,
|
|
2327
|
+
command: schema.action,
|
|
2328
|
+
// Strictly below the canonical (basePriority) and simple (basePriority - 5)
|
|
2329
|
+
// patterns so native order is always preferred.
|
|
2330
|
+
priority: (config.basePriority ?? 100) - 20,
|
|
2331
|
+
template: {
|
|
2332
|
+
format: `${keyword.primary} <${requiredRoles[0].role}>`,
|
|
2333
|
+
tokens: [verbToken, ...roleTokens]
|
|
2334
|
+
},
|
|
2335
|
+
extraction: buildExtractionRulesWithDefaults(schema, profile)
|
|
2336
|
+
};
|
|
2337
|
+
}
|
|
2338
|
+
function generatePatternVariants2(grammar, schema, profile, config = defaultConfig2) {
|
|
2339
|
+
const patterns = [];
|
|
2340
|
+
const full = schema;
|
|
2341
|
+
schema = withoutVariantOnlyRoles(schema);
|
|
2342
|
+
patterns.push(generatePattern2(grammar, schema, profile, config));
|
|
2343
|
+
if (config.generateSimpleVariants) {
|
|
2344
|
+
const simple = generateSimplePattern(grammar, schema, profile, config);
|
|
2345
|
+
if (simple) {
|
|
2346
|
+
patterns.push(simple);
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
for (const omitted of schema.omitRoleVariants ?? []) {
|
|
2350
|
+
const variantSchema = {
|
|
2351
|
+
...schema,
|
|
2352
|
+
roles: schema.roles.filter((r) => r.role !== omitted)
|
|
2353
|
+
};
|
|
2354
|
+
const variant = generatePattern2(grammar, variantSchema, profile, config);
|
|
2355
|
+
patterns.push({
|
|
2356
|
+
...variant,
|
|
2357
|
+
id: `${schema.action}-${profile.code}-generated-no-${omitted}`,
|
|
2358
|
+
priority: (config.basePriority ?? 100) - 8
|
|
2359
|
+
});
|
|
2360
|
+
}
|
|
2361
|
+
for (const widen of schema.widenTypeVariants ?? []) {
|
|
2362
|
+
if (widen.excludeLanguages?.includes(profile.code)) continue;
|
|
2363
|
+
const cloneSchema = {
|
|
2364
|
+
...schema,
|
|
2365
|
+
roles: schema.roles.map(
|
|
2366
|
+
(r) => r.role === widen.role ? { ...r, expectedTypes: [...widen.types] } : r
|
|
2367
|
+
)
|
|
2368
|
+
};
|
|
2369
|
+
const main = generatePattern2(grammar, cloneSchema, profile, config);
|
|
2370
|
+
patterns.push({
|
|
2371
|
+
...main,
|
|
2372
|
+
id: `${schema.action}-${profile.code}-generated-${widen.idSuffix}`,
|
|
2373
|
+
priority: (config.basePriority ?? 100) - (widen.priorityDelta ?? 15)
|
|
2374
|
+
});
|
|
2375
|
+
}
|
|
2376
|
+
for (const v of full.requiredRoleVariants ?? []) {
|
|
2377
|
+
const variantSchema = {
|
|
2378
|
+
...full,
|
|
2379
|
+
roles: full.roles.filter((r) => !v.omit?.includes(r.role)).map((r) => {
|
|
2380
|
+
const required = r.role === v.role ? { ...r, required: true } : r;
|
|
2381
|
+
return r.role === v.widen?.role ? { ...required, expectedTypes: [...v.widen.types] } : required;
|
|
2382
|
+
})
|
|
2383
|
+
};
|
|
2384
|
+
const variant = generatePattern2(grammar, variantSchema, profile, config);
|
|
2385
|
+
patterns.push({
|
|
2386
|
+
...variant,
|
|
2387
|
+
id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
|
|
2388
|
+
priority: (config.basePriority ?? 100) + (v.priorityDelta ?? 5)
|
|
2389
|
+
});
|
|
2390
|
+
}
|
|
2391
|
+
if (config.generateVerbFirstVariants !== false) {
|
|
2392
|
+
const verbFirst = generateVerbFirstPattern(schema, profile, config);
|
|
2393
|
+
if (verbFirst) {
|
|
2394
|
+
patterns.push(verbFirst);
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
for (const v of schema.rolePrefixLiteralVariants ?? []) {
|
|
2398
|
+
const literal = v.literal[profile.code];
|
|
2399
|
+
if (!literal) continue;
|
|
2400
|
+
const { rolePrefixLiteralVariants: _omitted, ...baseSchema } = schema;
|
|
2401
|
+
const cloneSchema = {
|
|
2402
|
+
...baseSchema,
|
|
2403
|
+
roles: schema.roles.map(
|
|
2404
|
+
(r) => r.role === v.role ? { ...r, valuePrefixLiteral: { [profile.code]: literal } } : r
|
|
2405
|
+
)
|
|
2406
|
+
};
|
|
2407
|
+
const delta = v.priorityDelta ?? 5;
|
|
2408
|
+
const carrier = v.methodCarrier ? { [v.methodCarrier]: { value: literal } } : {};
|
|
2409
|
+
const main = generatePattern2(grammar, cloneSchema, profile, config);
|
|
2410
|
+
patterns.push({
|
|
2411
|
+
...main,
|
|
2412
|
+
id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
|
|
2413
|
+
priority: (config.basePriority ?? 100) + delta,
|
|
2414
|
+
extraction: { ...main.extraction, ...carrier }
|
|
2415
|
+
});
|
|
2416
|
+
if (config.generateVerbFirstVariants !== false) {
|
|
2417
|
+
const verbFirstUrl = generateVerbFirstPattern(cloneSchema, profile, config);
|
|
2418
|
+
if (verbFirstUrl) {
|
|
2419
|
+
patterns.push({
|
|
2420
|
+
...verbFirstUrl,
|
|
2421
|
+
id: `${schema.action}-${profile.code}-generated-verb-first-${v.idSuffix}`,
|
|
2422
|
+
priority: (config.basePriority ?? 100) - 20 + delta,
|
|
2423
|
+
extraction: { ...verbFirstUrl.extraction, ...carrier }
|
|
2424
|
+
});
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
return patterns;
|
|
2429
|
+
}
|
|
2430
|
+
function buildTokens2(grammar, schema, profile, keyword) {
|
|
2431
|
+
const tokens = [];
|
|
2432
|
+
const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
|
|
2433
|
+
const postVerbRoles = profile.wordOrder === "SOV" ? schema.roles.filter((r) => r.sovSlot === "postVerb") : [];
|
|
2434
|
+
const roleTokens = buildRoleTokens(
|
|
2435
|
+
grammar,
|
|
2436
|
+
postVerbRoles.length ? { ...schema, roles: schema.roles.filter((r) => r.sovSlot !== "postVerb") } : schema,
|
|
2437
|
+
profile
|
|
2438
|
+
);
|
|
2439
|
+
const postVerbTokens = postVerbRoles.length ? buildRoleTokens(grammar, { ...schema, roles: postVerbRoles }, profile) : [];
|
|
2440
|
+
switch (profile.wordOrder) {
|
|
2441
|
+
case "SVO":
|
|
2442
|
+
tokens.push(verbToken);
|
|
2443
|
+
tokens.push(...roleTokens);
|
|
2444
|
+
break;
|
|
2445
|
+
case "SOV":
|
|
2446
|
+
tokens.push(...roleTokens);
|
|
2447
|
+
tokens.push(verbToken);
|
|
2448
|
+
tokens.push(...postVerbTokens);
|
|
2449
|
+
break;
|
|
2450
|
+
case "VSO":
|
|
2451
|
+
tokens.push(verbToken);
|
|
2452
|
+
tokens.push(...roleTokens);
|
|
2453
|
+
break;
|
|
2454
|
+
default:
|
|
2455
|
+
tokens.push(verbToken);
|
|
2456
|
+
tokens.push(...roleTokens);
|
|
2457
|
+
}
|
|
2458
|
+
return collapseAdjacentLiterals(tokens);
|
|
2459
|
+
}
|
|
2460
|
+
function collapseAdjacentLiterals(tokens) {
|
|
2461
|
+
const out = [];
|
|
2462
|
+
for (const tok of tokens) {
|
|
2463
|
+
const prev = out[out.length - 1];
|
|
2464
|
+
if (prev && prev.type === "literal" && tok.type === "literal" && prev.value === tok.value) {
|
|
2465
|
+
const alts = [...prev.alternatives ?? [], ...tok.alternatives ?? []];
|
|
2466
|
+
if (alts.length) out[out.length - 1] = { ...prev, alternatives: [...new Set(alts)] };
|
|
2467
|
+
continue;
|
|
2468
|
+
}
|
|
2469
|
+
out.push(tok);
|
|
2470
|
+
}
|
|
2471
|
+
return out;
|
|
2472
|
+
}
|
|
2473
|
+
function buildRoleTokens(grammar, schema, profile) {
|
|
2474
|
+
const tokens = [];
|
|
2475
|
+
const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
|
|
2476
|
+
for (const roleSpec of sortedRoles) {
|
|
2477
|
+
const roleToken = buildRoleToken(roleSpec, profile);
|
|
2478
|
+
if (!roleSpec.required) {
|
|
2479
|
+
tokens.push({
|
|
2480
|
+
type: "group",
|
|
2481
|
+
optional: true,
|
|
2482
|
+
tokens: roleToken
|
|
2483
|
+
});
|
|
2484
|
+
} else {
|
|
2485
|
+
tokens.push(...roleToken);
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
return tokens;
|
|
2489
|
+
}
|
|
2490
|
+
function buildRoleToken(roleSpec, profile) {
|
|
2491
|
+
const tokens = [];
|
|
2492
|
+
const overrideMarker = roleSpec.markerOverride?.[profile.code];
|
|
2493
|
+
const defaultMarker = profile.roleMarkers[roleSpec.role];
|
|
2494
|
+
const suppressMarker = roleSpec.renderOverride?.[profile.code] === "";
|
|
2495
|
+
const roleValueToken = {
|
|
2496
|
+
type: "role",
|
|
2497
|
+
role: roleSpec.role,
|
|
2498
|
+
optional: !roleSpec.required,
|
|
2499
|
+
expectedTypes: roleSpec.expectedTypes,
|
|
2500
|
+
// The matcher reads this too — a `'keyword'`-shaped slot holds a fixed
|
|
2501
|
+
// keyword phrase (`using view transition`) and must not be skipped by the
|
|
2502
|
+
// trailing-slot verb guard.
|
|
2503
|
+
...roleSpec.valueShape !== void 0 ? { valueShape: roleSpec.valueShape } : {},
|
|
2504
|
+
// A greedy role reads up to the next marker the pattern owes (a domain's
|
|
2505
|
+
// SQL condition); its value reader does the reading.
|
|
2506
|
+
...roleSpec.greedy ? { greedy: true } : {}
|
|
2507
|
+
};
|
|
2508
|
+
const prefixLiteral = roleSpec.valuePrefixLiteral?.[profile.code];
|
|
2509
|
+
const pushPrefixed = () => {
|
|
2510
|
+
if (prefixLiteral) tokens.push({ type: "literal", value: prefixLiteral });
|
|
2511
|
+
tokens.push(roleValueToken);
|
|
2512
|
+
};
|
|
2513
|
+
if (overrideMarker !== void 0) {
|
|
2514
|
+
const markerWords = overrideMarker ? overrideMarker.split(/\s+/).filter(Boolean) : [];
|
|
2515
|
+
const isPassthroughOverride = overrideMarker !== void 0 && overrideMarker === roleSpec.markerOverride?.en;
|
|
2516
|
+
const position = roleSpec.markerPositionOverride?.[profile.code] ?? roleSpec.markerPosition ?? defaultMarker?.position ?? (profile.wordOrder === "SOV" && !isPassthroughOverride ? "after" : "before");
|
|
2517
|
+
const optionalMarker = roleSpec.markerOptional?.[profile.code] === true;
|
|
2518
|
+
const pushWord = (word) => {
|
|
2519
|
+
const alternatives = markerWords.length === 1 ? schemaMarkerAlternatives(roleSpec, profile.code, word) ?? [] : [];
|
|
2520
|
+
const literal = {
|
|
2521
|
+
type: "literal",
|
|
2522
|
+
value: word,
|
|
2523
|
+
...alternatives.length ? { alternatives } : {},
|
|
2524
|
+
...suppressMarker ? { renderSuppress: true } : {}
|
|
2525
|
+
};
|
|
2526
|
+
tokens.push(optionalMarker ? { type: "group", optional: true, tokens: [literal] } : literal);
|
|
2527
|
+
};
|
|
2528
|
+
if (position === "before") {
|
|
2529
|
+
for (const word of markerWords) pushWord(word);
|
|
2530
|
+
pushPrefixed();
|
|
2531
|
+
} else {
|
|
2532
|
+
pushPrefixed();
|
|
2533
|
+
for (const word of markerWords) pushWord(word);
|
|
2534
|
+
}
|
|
2535
|
+
} else if (defaultMarker) {
|
|
2536
|
+
const asMarker = () => {
|
|
2537
|
+
const alternatives = [
|
|
2538
|
+
.../* @__PURE__ */ new Set([
|
|
2539
|
+
...defaultMarker.alternatives ?? [],
|
|
2540
|
+
...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
|
|
2541
|
+
])
|
|
2542
|
+
].filter((a) => a !== defaultMarker.primary);
|
|
2543
|
+
return {
|
|
2544
|
+
type: "literal",
|
|
2545
|
+
value: defaultMarker.primary,
|
|
2546
|
+
...alternatives.length ? { alternatives } : {},
|
|
2547
|
+
...suppressMarker ? { renderSuppress: true } : {}
|
|
2548
|
+
};
|
|
2549
|
+
};
|
|
2550
|
+
const pushMarker = (marker) => {
|
|
2551
|
+
if (!profile.markersOptional && !roleSpec.markerOptional?.[profile.code]) {
|
|
2552
|
+
tokens.push(marker);
|
|
2553
|
+
return;
|
|
2554
|
+
}
|
|
2555
|
+
tokens.push({
|
|
2556
|
+
type: "group",
|
|
2557
|
+
optional: true,
|
|
2558
|
+
...profile.markersOptional && !roleSpec.markerOptional?.[profile.code] ? { renderRequired: true } : {},
|
|
2559
|
+
tokens: [marker]
|
|
2560
|
+
});
|
|
2561
|
+
};
|
|
2562
|
+
if ((roleSpec.markerPosition ?? defaultMarker.position) === "before") {
|
|
2563
|
+
if (defaultMarker.primary) {
|
|
2564
|
+
pushMarker(asMarker());
|
|
2565
|
+
}
|
|
2566
|
+
pushPrefixed();
|
|
2567
|
+
} else {
|
|
2568
|
+
pushPrefixed();
|
|
2569
|
+
pushMarker(asMarker());
|
|
2570
|
+
}
|
|
2571
|
+
} else {
|
|
2572
|
+
pushPrefixed();
|
|
2573
|
+
}
|
|
2574
|
+
return tokens;
|
|
2575
|
+
}
|
|
2576
|
+
function buildExtractionRules2(schema, profile) {
|
|
2577
|
+
const rules = {};
|
|
2578
|
+
for (const roleSpec of schema.roles) {
|
|
2579
|
+
const overrideMarker = roleSpec.markerOverride?.[profile.code];
|
|
2580
|
+
const defaultMarker = profile.roleMarkers[roleSpec.role];
|
|
2581
|
+
if (roleSpec.valuePrefixLiteral?.[profile.code]) {
|
|
2582
|
+
rules[roleSpec.role] = { marker: roleSpec.valuePrefixLiteral[profile.code] };
|
|
2583
|
+
} else if (overrideMarker !== void 0) {
|
|
2584
|
+
if (!overrideMarker) {
|
|
2585
|
+
rules[roleSpec.role] = {};
|
|
2586
|
+
} else {
|
|
2587
|
+
const isSingleWord = !/\s/.test(overrideMarker.trim());
|
|
2588
|
+
const markerAlternatives = isSingleWord ? schemaMarkerAlternatives(roleSpec, profile.code, overrideMarker) ?? [] : [];
|
|
2589
|
+
rules[roleSpec.role] = markerAlternatives.length ? { marker: overrideMarker, markerAlternatives } : { marker: overrideMarker };
|
|
2590
|
+
}
|
|
2591
|
+
} else if (defaultMarker && defaultMarker.primary) {
|
|
2592
|
+
const markerAlternatives = [
|
|
2593
|
+
.../* @__PURE__ */ new Set([
|
|
2594
|
+
...defaultMarker.alternatives ?? [],
|
|
2595
|
+
...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
|
|
2596
|
+
])
|
|
2597
|
+
].filter((a) => a !== defaultMarker.primary);
|
|
2598
|
+
rules[roleSpec.role] = markerAlternatives.length ? { marker: defaultMarker.primary, markerAlternatives } : { marker: defaultMarker.primary };
|
|
2599
|
+
} else {
|
|
2600
|
+
rules[roleSpec.role] = {};
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
return rules;
|
|
1642
2604
|
}
|
|
1643
|
-
function
|
|
1644
|
-
const
|
|
1645
|
-
const
|
|
1646
|
-
for (const
|
|
1647
|
-
|
|
1648
|
-
if (
|
|
2605
|
+
function buildExtractionRulesWithDefaults(schema, profile) {
|
|
2606
|
+
const baseRules = buildExtractionRules2(schema, profile);
|
|
2607
|
+
const rules = {};
|
|
2608
|
+
for (const roleSpec of schema.roles) {
|
|
2609
|
+
const baseRule = baseRules[roleSpec.role] || {};
|
|
2610
|
+
if (!roleSpec.required && roleSpec.default) {
|
|
2611
|
+
rules[roleSpec.role] = { ...baseRule, default: roleSpec.default };
|
|
2612
|
+
} else {
|
|
2613
|
+
rules[roleSpec.role] = baseRule;
|
|
2614
|
+
}
|
|
1649
2615
|
}
|
|
1650
|
-
return
|
|
2616
|
+
return rules;
|
|
1651
2617
|
}
|
|
1652
|
-
function
|
|
1653
|
-
const
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
2618
|
+
function buildFormatString2(grammar, schema, profile, keyword) {
|
|
2619
|
+
const parts = [];
|
|
2620
|
+
const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
|
|
2621
|
+
const roleParts = sortedRoles.map((roleSpec) => {
|
|
2622
|
+
const resolved = resolveMarkerForRole(roleSpec, profile);
|
|
2623
|
+
let part = "";
|
|
2624
|
+
if (resolved && resolved.primary) {
|
|
2625
|
+
if (resolved.position === "before") {
|
|
2626
|
+
part = `${resolved.primary} {${roleSpec.role}}`;
|
|
2627
|
+
} else {
|
|
2628
|
+
part = `{${roleSpec.role}} ${resolved.primary}`;
|
|
2629
|
+
}
|
|
2630
|
+
} else {
|
|
2631
|
+
part = `{${roleSpec.role}}`;
|
|
1657
2632
|
}
|
|
1658
|
-
|
|
2633
|
+
return roleSpec.required ? part : `[${part}]`;
|
|
2634
|
+
});
|
|
2635
|
+
switch (profile.wordOrder) {
|
|
2636
|
+
case "SVO":
|
|
2637
|
+
case "VSO":
|
|
2638
|
+
parts.push(keyword.primary, ...roleParts);
|
|
2639
|
+
break;
|
|
2640
|
+
case "SOV":
|
|
2641
|
+
parts.push(...roleParts, keyword.primary);
|
|
2642
|
+
break;
|
|
2643
|
+
default:
|
|
2644
|
+
parts.push(keyword.primary, ...roleParts);
|
|
2645
|
+
}
|
|
2646
|
+
return parts.join(" ");
|
|
1659
2647
|
}
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
};
|
|
2648
|
+
|
|
2649
|
+
// src/generation/pattern-render.ts
|
|
2650
|
+
var NESTED_ROLE_BONUS = 5;
|
|
2651
|
+
var UNCOVERED_ROLE_PENALTY = 30;
|
|
2652
|
+
function pinnedLiteral(rule) {
|
|
2653
|
+
if (rule?.default?.type === "literal") return String(rule.default.value).trim().toLowerCase();
|
|
2654
|
+
if (rule?.value !== void 0) return String(rule.value).trim().toLowerCase();
|
|
2655
|
+
return void 0;
|
|
1669
2656
|
}
|
|
1670
|
-
function
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
2657
|
+
function selectRenderPattern(node, patterns, hooks) {
|
|
2658
|
+
let candidates = patterns.filter((pattern) => !pattern.parseOnly);
|
|
2659
|
+
if (candidates.length === 0) candidates = patterns;
|
|
2660
|
+
if (hooks.candidates) candidates = hooks.candidates(node, candidates);
|
|
2661
|
+
const pinnedValues = /* @__PURE__ */ new Map();
|
|
2662
|
+
for (const pattern of candidates) {
|
|
2663
|
+
for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
|
|
2664
|
+
const key = pinnedLiteral(rule);
|
|
2665
|
+
if (key === void 0) continue;
|
|
2666
|
+
const set = pinnedValues.get(role) ?? /* @__PURE__ */ new Set();
|
|
2667
|
+
set.add(key);
|
|
2668
|
+
pinnedValues.set(role, set);
|
|
2669
|
+
}
|
|
2670
|
+
}
|
|
2671
|
+
const scored = candidates.map((pattern) => {
|
|
2672
|
+
let score = pattern.priority;
|
|
2673
|
+
const hasRealRole = (role) => hooks.hasRealRole(node, role);
|
|
2674
|
+
const scoreTokens = (tokens, inGroup) => {
|
|
2675
|
+
for (const token of tokens) {
|
|
2676
|
+
if (token.type === "group") {
|
|
2677
|
+
scoreTokens(token.tokens, true);
|
|
2678
|
+
} else if (token.type === "role") {
|
|
2679
|
+
if (hasRealRole(token.role)) {
|
|
2680
|
+
score += inGroup ? NESTED_ROLE_BONUS : 10;
|
|
2681
|
+
} else if (!inGroup && !token.optional) {
|
|
2682
|
+
score -= 50;
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
};
|
|
2687
|
+
scoreTokens(pattern.template.tokens, false);
|
|
2688
|
+
const slots = /* @__PURE__ */ new Set();
|
|
2689
|
+
const collectSlots = (tokens) => {
|
|
2690
|
+
for (const token of tokens) {
|
|
2691
|
+
if (token.type === "group") collectSlots(token.tokens);
|
|
2692
|
+
else if (token.type === "role") slots.add(token.role);
|
|
2693
|
+
}
|
|
2694
|
+
};
|
|
2695
|
+
collectSlots(pattern.template.tokens);
|
|
2696
|
+
for (const role of node.roles.keys()) {
|
|
2697
|
+
if (hasRealRole(role) && !slots.has(role)) score -= UNCOVERED_ROLE_PENALTY;
|
|
2698
|
+
}
|
|
2699
|
+
for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
|
|
2700
|
+
const pinnedKey = pinnedLiteral(rule);
|
|
2701
|
+
if (pinnedKey === void 0) continue;
|
|
2702
|
+
const actual = node.roles.get(role);
|
|
2703
|
+
const actualKey = actual?.type === (rule.valueIsExpression ? "expression" : "literal") ? String(
|
|
2704
|
+
actual.type === "expression" ? actual.raw : actual.value
|
|
2705
|
+
).trim().toLowerCase() : void 0;
|
|
2706
|
+
if (actualKey === pinnedKey) {
|
|
2707
|
+
score += 60;
|
|
2708
|
+
} else if (actualKey === void 0 || pinnedValues.get(role)?.has(actualKey)) {
|
|
2709
|
+
score -= 200;
|
|
2710
|
+
}
|
|
2711
|
+
}
|
|
2712
|
+
score += hooks.adjustScore?.(pattern, node) ?? 0;
|
|
2713
|
+
return { pattern, score };
|
|
2714
|
+
});
|
|
2715
|
+
scored.sort((a, b) => b.score - a.score);
|
|
2716
|
+
return scored.length > 0 ? scored[0].pattern : null;
|
|
1684
2717
|
}
|
|
1685
|
-
function
|
|
1686
|
-
const
|
|
1687
|
-
|
|
1688
|
-
const
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
value = String((0, import_intent.extractValue)(role.default));
|
|
2718
|
+
function renderPattern(node, pattern, hooks) {
|
|
2719
|
+
const parts = [];
|
|
2720
|
+
const language = pattern.language;
|
|
2721
|
+
const tokens = pattern.template.tokens;
|
|
2722
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
2723
|
+
hooks.beforeToken?.(tokens, i, parts, node, language);
|
|
2724
|
+
const span = hooks.span?.(tokens, i, node, language, true);
|
|
2725
|
+
if (span !== void 0) {
|
|
2726
|
+
parts.push(span.text);
|
|
2727
|
+
i += span.tokens - 1;
|
|
2728
|
+
continue;
|
|
1697
2729
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
2730
|
+
const token = tokens[i];
|
|
2731
|
+
const rendered = renderPatternToken(token, node, language, hooks);
|
|
2732
|
+
if (rendered !== null) {
|
|
2733
|
+
pushPart(hooks, parts, rendered, token, tokens[i - 1], node, language);
|
|
2734
|
+
hooks.afterToken?.(token, parts.length - 1, node);
|
|
1701
2735
|
}
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
2736
|
+
}
|
|
2737
|
+
return parts;
|
|
2738
|
+
}
|
|
2739
|
+
function pushPart(hooks, parts, text, token, previous, node, language) {
|
|
2740
|
+
if (hooks.pushPart) hooks.pushPart(parts, text, token, previous, node, language);
|
|
2741
|
+
else parts.push(text);
|
|
2742
|
+
}
|
|
2743
|
+
function renderPatternToken(token, node, language, hooks) {
|
|
2744
|
+
switch (token.type) {
|
|
2745
|
+
case "literal":
|
|
2746
|
+
return token.renderSuppress ? null : token.value;
|
|
2747
|
+
case "role":
|
|
2748
|
+
return hooks.writeRole(token, node, language);
|
|
2749
|
+
case "group": {
|
|
2750
|
+
const hasRequired = token.tokens.filter((t) => t.type === "role" && !t.optional).every((t) => node.roles.has(t.role));
|
|
2751
|
+
if (!hasRequired && token.optional) {
|
|
2752
|
+
return null;
|
|
2753
|
+
}
|
|
2754
|
+
if (hooks.writeGroup && !hooks.writeGroup(token, node, language)) return null;
|
|
2755
|
+
const groupParts = [];
|
|
2756
|
+
let hasRoleValue = false;
|
|
2757
|
+
for (let i = 0; i < token.tokens.length; i++) {
|
|
2758
|
+
const span = hooks.span?.(token.tokens, i, node, language, false);
|
|
2759
|
+
if (span !== void 0) {
|
|
2760
|
+
groupParts.push(span.text);
|
|
2761
|
+
hasRoleValue = true;
|
|
2762
|
+
i += span.tokens - 1;
|
|
2763
|
+
continue;
|
|
2764
|
+
}
|
|
2765
|
+
const subToken = token.tokens[i];
|
|
2766
|
+
const rendered = renderPatternToken(subToken, node, language, hooks);
|
|
2767
|
+
if (rendered !== null) {
|
|
2768
|
+
pushPart(hooks, groupParts, rendered, subToken, token.tokens[i - 1], node, language);
|
|
2769
|
+
if (subToken.type === "role") hasRoleValue = true;
|
|
2770
|
+
}
|
|
2771
|
+
}
|
|
2772
|
+
if (token.optional && !hasRoleValue && !token.renderRequired) return null;
|
|
2773
|
+
return groupParts.length > 0 ? groupParts.join(" ") : null;
|
|
1711
2774
|
}
|
|
2775
|
+
default:
|
|
2776
|
+
return null;
|
|
1712
2777
|
}
|
|
1713
|
-
return isSOV ? buildPhrase(...preVerb, keyword, ...postVerb) : buildPhrase(keyword, ...preVerb, ...postVerb);
|
|
1714
2778
|
}
|
|
1715
2779
|
|
|
1716
2780
|
// src/grammar/types.ts
|
|
@@ -3010,7 +4074,8 @@ var DSLRegistry = class {
|
|
|
3010
4074
|
* subset of the DSL's languages. A built-in command missing a keyword stays
|
|
3011
4075
|
* an error, since that is a domain-authoring mistake.
|
|
3012
4076
|
*/
|
|
3013
|
-
constructor(config, partiallyTranslatedActions
|
|
4077
|
+
constructor(config, partiallyTranslatedActions, generate) {
|
|
4078
|
+
this.generate = generate;
|
|
3014
4079
|
this.patterns = /* @__PURE__ */ new Map();
|
|
3015
4080
|
this.tokenizers = /* @__PURE__ */ new Map();
|
|
3016
4081
|
this.schemas = config.schemas;
|
|
@@ -3026,7 +4091,7 @@ var DSLRegistry = class {
|
|
|
3026
4091
|
if (this.partiallyTranslatedActions.has(schema.action) && !lang.patternProfile.keywords[schema.action]) {
|
|
3027
4092
|
continue;
|
|
3028
4093
|
}
|
|
3029
|
-
const pattern =
|
|
4094
|
+
const pattern = this.generate(schema, lang.patternProfile);
|
|
3030
4095
|
patterns.push(pattern);
|
|
3031
4096
|
}
|
|
3032
4097
|
this.patterns.set(lang.code, patterns);
|
|
@@ -3042,9 +4107,9 @@ var DSLRegistry = class {
|
|
|
3042
4107
|
}
|
|
3043
4108
|
};
|
|
3044
4109
|
var MultilingualDSLImpl = class {
|
|
3045
|
-
constructor(config, registry, transformer, profileProvider) {
|
|
4110
|
+
constructor(config, registry, transformer, profileProvider, engine) {
|
|
3046
4111
|
this.registry = registry;
|
|
3047
|
-
this.
|
|
4112
|
+
this.match = engine.match;
|
|
3048
4113
|
this.transformer = transformer;
|
|
3049
4114
|
this.profileProvider = profileProvider;
|
|
3050
4115
|
if (config.codeGenerator) {
|
|
@@ -3057,10 +4122,7 @@ var MultilingualDSLImpl = class {
|
|
|
3057
4122
|
for (const extension of config.extensions ?? []) {
|
|
3058
4123
|
if (extension.render) this.extensionRenderers.set(extension.schema.action, extension.render);
|
|
3059
4124
|
}
|
|
3060
|
-
this.schemaFallbackRenderer =
|
|
3061
|
-
schemas: config.schemas,
|
|
3062
|
-
profiles: config.languages.map((l) => l.patternProfile)
|
|
3063
|
-
});
|
|
4125
|
+
this.schemaFallbackRenderer = engine.fallbackRenderer(config, registry);
|
|
3064
4126
|
const schemaMap = new Map(config.schemas.map((s) => [s.action, s]));
|
|
3065
4127
|
this.schemaLookup = {
|
|
3066
4128
|
getSchema: (action) => schemaMap.get(action)
|
|
@@ -3084,15 +4146,12 @@ var MultilingualDSLImpl = class {
|
|
|
3084
4146
|
}
|
|
3085
4147
|
const tokens = tokenizer.tokenize(input);
|
|
3086
4148
|
const patterns = this.registry.getPatterns(language);
|
|
3087
|
-
const profile = {
|
|
3088
|
-
code: language
|
|
3089
|
-
};
|
|
3090
4149
|
const total = tokens.tokens.length;
|
|
3091
4150
|
let best = null;
|
|
3092
4151
|
let longestPartial = null;
|
|
3093
4152
|
for (const pattern of patterns) {
|
|
3094
4153
|
const mark = tokens.mark();
|
|
3095
|
-
const match = this.
|
|
4154
|
+
const match = this.match(tokens, pattern, language);
|
|
3096
4155
|
tokens.reset(mark);
|
|
3097
4156
|
if (!match) continue;
|
|
3098
4157
|
if (match.consumedTokens < total) {
|
|
@@ -3336,8 +4395,76 @@ function createMultilingualDSL(config) {
|
|
|
3336
4395
|
dictionary,
|
|
3337
4396
|
profileProvider
|
|
3338
4397
|
});
|
|
3339
|
-
const
|
|
3340
|
-
|
|
4398
|
+
const engine = engineFor(config);
|
|
4399
|
+
const registry = new DSLRegistry(effectiveConfig, extensionActions, engine.generate);
|
|
4400
|
+
return new MultilingualDSLImpl(effectiveConfig, registry, transformer, profileProvider, engine);
|
|
4401
|
+
}
|
|
4402
|
+
function engineFor(config) {
|
|
4403
|
+
const name = config.engine ?? "classic";
|
|
4404
|
+
if (name === "classic") return classicEngine();
|
|
4405
|
+
if (name === "core") return coreEngine();
|
|
4406
|
+
throw new Error(`Unknown DSL engine "${String(name)}" (expected "classic" or "core")`);
|
|
4407
|
+
}
|
|
4408
|
+
function classicEngine() {
|
|
4409
|
+
const matcher = new PatternMatcher();
|
|
4410
|
+
return {
|
|
4411
|
+
generate: (schema, profile) => generatePattern(schema, profile),
|
|
4412
|
+
match: (tokens, pattern, language) => matcher.matchPattern(tokens, pattern, { code: language }),
|
|
4413
|
+
fallbackRenderer: (config) => createDomainRenderer({
|
|
4414
|
+
schemas: config.schemas,
|
|
4415
|
+
profiles: config.languages.map((l) => l.patternProfile)
|
|
4416
|
+
})
|
|
4417
|
+
};
|
|
4418
|
+
}
|
|
4419
|
+
function schemaProfileOf(profile) {
|
|
4420
|
+
const side = profile.wordOrder === "SOV" ? "after" : "before";
|
|
4421
|
+
const roleMarkers = {};
|
|
4422
|
+
for (const [role, marker] of Object.entries(profile.roleMarkers ?? {})) {
|
|
4423
|
+
roleMarkers[role] = {
|
|
4424
|
+
primary: marker.primary,
|
|
4425
|
+
...marker.alternatives ? { alternatives: marker.alternatives } : {},
|
|
4426
|
+
position: marker.position ?? side
|
|
4427
|
+
};
|
|
4428
|
+
}
|
|
4429
|
+
return {
|
|
4430
|
+
code: profile.code,
|
|
4431
|
+
wordOrder: profile.wordOrder,
|
|
4432
|
+
keywords: profile.keywords,
|
|
4433
|
+
roleMarkers
|
|
4434
|
+
};
|
|
4435
|
+
}
|
|
4436
|
+
function coreEngine() {
|
|
4437
|
+
const generator = createSchemaPatternGenerator({ positionOrder: "descending" });
|
|
4438
|
+
const matcher = new MatchSkeleton(createStockReader(), stockConfidenceModel);
|
|
4439
|
+
return {
|
|
4440
|
+
generate: (schema, profile) => generator.generatePattern(schema, schemaProfileOf(profile)),
|
|
4441
|
+
match: (tokens, pattern) => matcher.matchPattern(tokens, pattern),
|
|
4442
|
+
fallbackRenderer: (config, registry) => {
|
|
4443
|
+
const schemas = new Map(config.schemas.map((s) => [s.action, s]));
|
|
4444
|
+
const hooks = {
|
|
4445
|
+
hasRealRole: (node, role) => {
|
|
4446
|
+
const value = node.roles.get(role);
|
|
4447
|
+
return value !== void 0 && value.implicit !== true;
|
|
4448
|
+
},
|
|
4449
|
+
writeRole: (token, node) => {
|
|
4450
|
+
if (!node.roles.has(token.role)) return null;
|
|
4451
|
+
let value = (0, import_intent.extractRoleValue)(node, token.role);
|
|
4452
|
+
const spec = schemas.get(node.action)?.roles.find((r) => r.role === token.role);
|
|
4453
|
+
if (spec?.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
|
|
4454
|
+
value = `"${value}"`;
|
|
4455
|
+
}
|
|
4456
|
+
return value || null;
|
|
4457
|
+
}
|
|
4458
|
+
};
|
|
4459
|
+
return (node, language) => {
|
|
4460
|
+
const patterns = registry.getPatterns(language).filter((p) => p.command === node.action);
|
|
4461
|
+
const pattern = selectRenderPattern(node, patterns, hooks);
|
|
4462
|
+
if (!pattern) return null;
|
|
4463
|
+
const text = renderPattern(node, pattern, hooks).join(" ");
|
|
4464
|
+
return text || null;
|
|
4465
|
+
};
|
|
4466
|
+
}
|
|
4467
|
+
};
|
|
3341
4468
|
}
|
|
3342
4469
|
|
|
3343
4470
|
// src/prompts/prompt-generator.ts
|
|
@@ -4643,6 +5770,50 @@ var CrossDomainDispatcher = class {
|
|
|
4643
5770
|
}
|
|
4644
5771
|
};
|
|
4645
5772
|
|
|
5773
|
+
// src/api/translation-loss.ts
|
|
5774
|
+
var LossyTranslationError = class extends Error {
|
|
5775
|
+
constructor(partial, loss, from, to) {
|
|
5776
|
+
super(
|
|
5777
|
+
`translation ${from} \u2192 ${to} would lose part of the script (${loss.kind}): ` + loss.lost.join(", ")
|
|
5778
|
+
);
|
|
5779
|
+
this.partial = partial;
|
|
5780
|
+
this.loss = loss;
|
|
5781
|
+
this.from = from;
|
|
5782
|
+
this.to = to;
|
|
5783
|
+
this.name = "LossyTranslationError";
|
|
5784
|
+
}
|
|
5785
|
+
};
|
|
5786
|
+
function unconsumedSpans(node) {
|
|
5787
|
+
const out = [];
|
|
5788
|
+
for (const d of node.diagnostics ?? []) {
|
|
5789
|
+
if (d.code !== "unconsumed-input") continue;
|
|
5790
|
+
out.push(/"(.*)"\s*$/s.exec(d.message)?.[1] ?? d.message);
|
|
5791
|
+
}
|
|
5792
|
+
return out;
|
|
5793
|
+
}
|
|
5794
|
+
function findTranslationLoss(input, node, output, readBack, rules) {
|
|
5795
|
+
const unread = unconsumedSpans(node);
|
|
5796
|
+
if (unread.length) return { kind: "truncation", lost: unread };
|
|
5797
|
+
const unwritable = rules.unwritable?.(node) ?? [];
|
|
5798
|
+
if (unwritable.length) return { kind: "core-only", lost: unwritable };
|
|
5799
|
+
let back;
|
|
5800
|
+
try {
|
|
5801
|
+
back = readBack(output);
|
|
5802
|
+
} catch (e) {
|
|
5803
|
+
return { kind: "read-back", lost: [`the output does not read: ${e.message}`] };
|
|
5804
|
+
}
|
|
5805
|
+
const misread = unconsumedSpans(back);
|
|
5806
|
+
if (misread.length) return { kind: "read-back", lost: misread };
|
|
5807
|
+
const written = rules.written ? rules.written(node) : node;
|
|
5808
|
+
for (const difference of rules.differences) {
|
|
5809
|
+
const lost = difference(written, back);
|
|
5810
|
+
if (lost.length) return { kind: "read-back", lost };
|
|
5811
|
+
}
|
|
5812
|
+
const values = rules.missingValues?.(input, output) ?? [];
|
|
5813
|
+
if (values.length) return { kind: "invariant", lost: values };
|
|
5814
|
+
return void 0;
|
|
5815
|
+
}
|
|
5816
|
+
|
|
4646
5817
|
// src/aot/domain-scanner.ts
|
|
4647
5818
|
function buildAttributePatterns(attributes) {
|
|
4648
5819
|
const patterns = [];
|
|
@@ -4934,122 +6105,6 @@ var import_intent6 = require("@lokascript/intent");
|
|
|
4934
6105
|
// src/generation/diagnostics.ts
|
|
4935
6106
|
var import_intent7 = require("@lokascript/intent");
|
|
4936
6107
|
|
|
4937
|
-
// src/core/tokenization/token-utils.ts
|
|
4938
|
-
var TokenStreamImpl = class {
|
|
4939
|
-
constructor(tokens, language) {
|
|
4940
|
-
this.pos = 0;
|
|
4941
|
-
this.tokens = tokens;
|
|
4942
|
-
this.language = language;
|
|
4943
|
-
}
|
|
4944
|
-
peek(offset = 0) {
|
|
4945
|
-
const index = this.pos + offset;
|
|
4946
|
-
if (index < 0 || index >= this.tokens.length) {
|
|
4947
|
-
return null;
|
|
4948
|
-
}
|
|
4949
|
-
return this.tokens[index];
|
|
4950
|
-
}
|
|
4951
|
-
advance() {
|
|
4952
|
-
if (this.isAtEnd()) {
|
|
4953
|
-
throw new Error("Unexpected end of token stream");
|
|
4954
|
-
}
|
|
4955
|
-
return this.tokens[this.pos++];
|
|
4956
|
-
}
|
|
4957
|
-
isAtEnd() {
|
|
4958
|
-
return this.pos >= this.tokens.length;
|
|
4959
|
-
}
|
|
4960
|
-
mark() {
|
|
4961
|
-
return { position: this.pos };
|
|
4962
|
-
}
|
|
4963
|
-
reset(mark) {
|
|
4964
|
-
this.pos = mark.position;
|
|
4965
|
-
}
|
|
4966
|
-
position() {
|
|
4967
|
-
return this.pos;
|
|
4968
|
-
}
|
|
4969
|
-
/**
|
|
4970
|
-
* Get remaining tokens as an array.
|
|
4971
|
-
*/
|
|
4972
|
-
remaining() {
|
|
4973
|
-
return this.tokens.slice(this.pos);
|
|
4974
|
-
}
|
|
4975
|
-
/**
|
|
4976
|
-
* Consume tokens while predicate is true.
|
|
4977
|
-
*/
|
|
4978
|
-
takeWhile(predicate) {
|
|
4979
|
-
const result = [];
|
|
4980
|
-
while (!this.isAtEnd() && predicate(this.peek())) {
|
|
4981
|
-
result.push(this.advance());
|
|
4982
|
-
}
|
|
4983
|
-
return result;
|
|
4984
|
-
}
|
|
4985
|
-
/**
|
|
4986
|
-
* Skip tokens while predicate is true.
|
|
4987
|
-
*/
|
|
4988
|
-
skipWhile(predicate) {
|
|
4989
|
-
while (!this.isAtEnd() && predicate(this.peek())) {
|
|
4990
|
-
this.advance();
|
|
4991
|
-
}
|
|
4992
|
-
}
|
|
4993
|
-
};
|
|
4994
|
-
function createPosition(start, end) {
|
|
4995
|
-
return { start, end };
|
|
4996
|
-
}
|
|
4997
|
-
function createToken(valueOrParams, kind, position, normalizedOrOptions) {
|
|
4998
|
-
if (typeof valueOrParams === "object") {
|
|
4999
|
-
const { value: value2, kind: kind2, position: position2, normalized: normalized2, stem, stemConfidence, metadata } = valueOrParams;
|
|
5000
|
-
return {
|
|
5001
|
-
value: value2,
|
|
5002
|
-
kind: kind2,
|
|
5003
|
-
position: position2,
|
|
5004
|
-
...normalized2 !== void 0 && { normalized: normalized2 },
|
|
5005
|
-
...stem !== void 0 && { stem },
|
|
5006
|
-
...stemConfidence !== void 0 && { stemConfidence },
|
|
5007
|
-
...metadata !== void 0 && { metadata }
|
|
5008
|
-
};
|
|
5009
|
-
}
|
|
5010
|
-
const value = valueOrParams;
|
|
5011
|
-
if (!kind || !position) {
|
|
5012
|
-
throw new Error("createToken requires kind and position parameters");
|
|
5013
|
-
}
|
|
5014
|
-
if (typeof normalizedOrOptions === "string") {
|
|
5015
|
-
return { value, kind, position, normalized: normalizedOrOptions };
|
|
5016
|
-
}
|
|
5017
|
-
if (normalizedOrOptions) {
|
|
5018
|
-
const { normalized: normalized2, stem, stemConfidence, metadata } = normalizedOrOptions;
|
|
5019
|
-
return {
|
|
5020
|
-
value,
|
|
5021
|
-
kind,
|
|
5022
|
-
position,
|
|
5023
|
-
...normalized2 !== void 0 && { normalized: normalized2 },
|
|
5024
|
-
...stem !== void 0 && { stem },
|
|
5025
|
-
...stemConfidence !== void 0 && { stemConfidence },
|
|
5026
|
-
...metadata !== void 0 && { metadata }
|
|
5027
|
-
};
|
|
5028
|
-
}
|
|
5029
|
-
return { value, kind, position };
|
|
5030
|
-
}
|
|
5031
|
-
function isWhitespace(char) {
|
|
5032
|
-
return /\s/.test(char);
|
|
5033
|
-
}
|
|
5034
|
-
function isSelectorStart(char) {
|
|
5035
|
-
return char === "#" || char === "." || char === "[" || char === "@" || char === "*" || char === "<";
|
|
5036
|
-
}
|
|
5037
|
-
function isQuote(char) {
|
|
5038
|
-
return char === '"' || char === "'" || char === "`" || char === "\u300C" || char === "\u300D";
|
|
5039
|
-
}
|
|
5040
|
-
function isDigit(char) {
|
|
5041
|
-
return /\d/.test(char);
|
|
5042
|
-
}
|
|
5043
|
-
function stripOptionalDiacritics(word) {
|
|
5044
|
-
return word.replace(/[\u064b-\u0652\u0670]/g, "");
|
|
5045
|
-
}
|
|
5046
|
-
function isAsciiLetter(char) {
|
|
5047
|
-
return /[a-zA-Z]/.test(char);
|
|
5048
|
-
}
|
|
5049
|
-
function isAsciiIdentifierChar(char) {
|
|
5050
|
-
return /[a-zA-Z0-9_-]/.test(char);
|
|
5051
|
-
}
|
|
5052
|
-
|
|
5053
6108
|
// src/core/tokenization/extractors.ts
|
|
5054
6109
|
function extractCssSelector(input, startPos) {
|
|
5055
6110
|
if (startPos >= input.length) return null;
|
|
@@ -6895,6 +7950,8 @@ function accumulateIndented(statements, config) {
|
|
|
6895
7950
|
InMemoryDictionary,
|
|
6896
7951
|
InMemoryProfileProvider,
|
|
6897
7952
|
LatinExtendedIdentifierExtractor,
|
|
7953
|
+
LossyTranslationError,
|
|
7954
|
+
MatchSkeleton,
|
|
6898
7955
|
NullDictionary,
|
|
6899
7956
|
NullProfileProvider,
|
|
6900
7957
|
NumberExtractor,
|
|
@@ -6902,6 +7959,7 @@ function accumulateIndented(statements, config) {
|
|
|
6902
7959
|
PatternMatcher,
|
|
6903
7960
|
PatternTracker,
|
|
6904
7961
|
PunctuationExtractor,
|
|
7962
|
+
StockValueReader,
|
|
6905
7963
|
StringLiteralExtractor,
|
|
6906
7964
|
TokenStreamImpl,
|
|
6907
7965
|
TranslationReadBackError,
|
|
@@ -6936,9 +7994,11 @@ function accumulateIndented(statements, config) {
|
|
|
6936
7994
|
createPosition,
|
|
6937
7995
|
createPropertyPath,
|
|
6938
7996
|
createReference,
|
|
7997
|
+
createSchemaPatternGenerator,
|
|
6939
7998
|
createSchemaRenderer,
|
|
6940
7999
|
createSelector,
|
|
6941
8000
|
createSimpleTokenizer,
|
|
8001
|
+
createStockReader,
|
|
6942
8002
|
createToken,
|
|
6943
8003
|
createTokenizerContext,
|
|
6944
8004
|
createTryNode,
|
|
@@ -6954,6 +8014,7 @@ function accumulateIndented(statements, config) {
|
|
|
6954
8014
|
extractUrl,
|
|
6955
8015
|
extractValue,
|
|
6956
8016
|
filterBySeverity,
|
|
8017
|
+
findTranslationLoss,
|
|
6957
8018
|
fromEnvelopeJSON,
|
|
6958
8019
|
fromError,
|
|
6959
8020
|
fromInterchangeNode,
|
|
@@ -6968,6 +8029,7 @@ function accumulateIndented(statements, config) {
|
|
|
6968
8029
|
getPossessiveReference,
|
|
6969
8030
|
getRoleSpec,
|
|
6970
8031
|
getRoleValue,
|
|
8032
|
+
greedyStopMarkers,
|
|
6971
8033
|
insertMarkers,
|
|
6972
8034
|
isAsciiIdentifierChar,
|
|
6973
8035
|
isAsciiLetter,
|
|
@@ -6995,6 +8057,7 @@ function accumulateIndented(statements, config) {
|
|
|
6995
8057
|
isWhitespace,
|
|
6996
8058
|
joinTokens,
|
|
6997
8059
|
jsonToSemanticNode,
|
|
8060
|
+
legacyMarkerAlternatives,
|
|
6998
8061
|
lookupKeyword,
|
|
6999
8062
|
lookupMarker,
|
|
7000
8063
|
matchBest,
|
|
@@ -7007,24 +8070,33 @@ function accumulateIndented(statements, config) {
|
|
|
7007
8070
|
parseExplicit,
|
|
7008
8071
|
parseJSONL,
|
|
7009
8072
|
patternMatcher,
|
|
8073
|
+
readGreedy,
|
|
8074
|
+
readRoleValue,
|
|
7010
8075
|
registryToAOTBackends,
|
|
7011
8076
|
renderDocument,
|
|
7012
8077
|
renderExplicit,
|
|
7013
8078
|
renderExpr,
|
|
8079
|
+
renderPattern,
|
|
7014
8080
|
reorderRoles,
|
|
8081
|
+
resolveMarkerForRole,
|
|
8082
|
+
schemaMarkerAlternatives,
|
|
8083
|
+
selectRenderPattern,
|
|
7015
8084
|
semanticNodeToJSON,
|
|
7016
8085
|
semanticNodeToRuntimeAST,
|
|
7017
8086
|
semanticValueToAST,
|
|
7018
8087
|
sortRolesByWordOrder,
|
|
8088
|
+
stockConfidenceModel,
|
|
7019
8089
|
stripOptionalDiacritics,
|
|
7020
8090
|
synthesizeFromSchemas,
|
|
7021
8091
|
toEnvelopeJSON,
|
|
7022
8092
|
toJSONL,
|
|
7023
8093
|
toJSONLRow,
|
|
7024
8094
|
toProtocolJSON,
|
|
8095
|
+
unconsumedSpans,
|
|
7025
8096
|
validateProtocolJSON,
|
|
7026
8097
|
validateSemanticJSON,
|
|
7027
8098
|
validateValueType,
|
|
7028
|
-
withDefaultExtractors
|
|
8099
|
+
withDefaultExtractors,
|
|
8100
|
+
withoutVariantOnlyRoles
|
|
7029
8101
|
});
|
|
7030
8102
|
//# sourceMappingURL=index.cjs.map
|