@lokascript/framework 4.6.0 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +121 -1
- package/dist/api/create-dsl.d.ts +18 -2
- package/dist/api/create-dsl.d.ts.map +1 -1
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +1799 -713
- package/dist/api/index.js.map +1 -1
- package/dist/api/translation-loss.d.ts +84 -0
- package/dist/api/translation-loss.d.ts.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1057 -684
- package/dist/core/index.js.map +1 -1
- package/dist/core/matching/confidence-model.d.ts +54 -0
- package/dist/core/matching/confidence-model.d.ts.map +1 -0
- package/dist/core/matching/index.d.ts +12 -0
- package/dist/core/matching/index.d.ts.map +1 -0
- package/dist/core/matching/match-skeleton.d.ts +114 -0
- package/dist/core/matching/match-skeleton.d.ts.map +1 -0
- package/dist/core/matching/stock-confidence.d.ts +15 -0
- package/dist/core/matching/stock-confidence.d.ts.map +1 -0
- package/dist/core/matching/stock-reader.d.ts +63 -0
- package/dist/core/matching/stock-reader.d.ts.map +1 -0
- package/dist/core/matching/stock-reading.d.ts +44 -0
- package/dist/core/matching/stock-reading.d.ts.map +1 -0
- package/dist/core/matching/value-reader.d.ts +62 -0
- package/dist/core/matching/value-reader.d.ts.map +1 -0
- package/dist/core/pattern-matching/index.js +561 -660
- package/dist/core/pattern-matching/index.js.map +1 -1
- package/dist/core/pattern-matching/pattern-matcher.d.ts +0 -117
- package/dist/core/pattern-matching/pattern-matcher.d.ts.map +1 -1
- package/dist/core/types.d.ts +53 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/generation/index.d.ts +2 -0
- package/dist/generation/index.d.ts.map +1 -1
- package/dist/generation/index.js +593 -1
- package/dist/generation/index.js.map +1 -1
- package/dist/generation/pattern-render.d.ts +72 -0
- package/dist/generation/pattern-render.d.ts.map +1 -0
- package/dist/generation/schema-patterns.d.ts +152 -0
- package/dist/generation/schema-patterns.d.ts.map +1 -0
- package/dist/index.cjs +2016 -904
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1998 -903
- package/dist/index.js.map +1 -1
- package/dist/ir/index.js.map +1 -1
- package/dist/parsing/index.js +561 -660
- package/dist/parsing/index.js.map +1 -1
- package/dist/testing/gate/index.d.ts +8 -0
- package/dist/testing/gate/index.d.ts.map +1 -0
- package/dist/testing/gate/index.js +664 -0
- package/dist/testing/gate/index.js.map +1 -0
- package/dist/testing/gate/lint/format.d.ts +7 -0
- package/dist/testing/gate/lint/format.d.ts.map +1 -0
- package/dist/testing/gate/lint/index.d.ts +30 -0
- package/dist/testing/gate/lint/index.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/extractor-coverage.d.ts +18 -0
- package/dist/testing/gate/lint/rules/extractor-coverage.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/index.d.ts +14 -0
- package/dist/testing/gate/lint/rules/index.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/keyword-classification.d.ts +19 -0
- package/dist/testing/gate/lint/rules/keyword-classification.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/keyword-coverage.d.ts +13 -0
- package/dist/testing/gate/lint/rules/keyword-coverage.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/marker-tokenization.d.ts +20 -0
- package/dist/testing/gate/lint/rules/marker-tokenization.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/position-ordering.d.ts +25 -0
- package/dist/testing/gate/lint/rules/position-ordering.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/renderer-coherence.d.ts +24 -0
- package/dist/testing/gate/lint/rules/renderer-coherence.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/schema-rules.d.ts +10 -0
- package/dist/testing/gate/lint/rules/schema-rules.d.ts.map +1 -0
- package/dist/testing/gate/lint/types.d.ts +74 -0
- package/dist/testing/gate/lint/types.d.ts.map +1 -0
- package/dist/testing/gate/round-trip.d.ts +184 -0
- package/dist/testing/gate/round-trip.d.ts.map +1 -0
- package/package.json +6 -2
- package/src/__test__/fixtures/sql-dsl.ts +8 -2
- package/src/api/create-dsl-engine.test.ts +150 -0
- package/src/api/create-dsl.test.ts +7 -0
- package/src/api/create-dsl.ts +173 -22
- package/src/api/index.ts +1 -0
- package/src/api/translation-loss.test.ts +89 -0
- package/src/api/translation-loss.ts +128 -0
- package/src/core/index.ts +1 -0
- package/src/core/matching/confidence-model.ts +58 -0
- package/src/core/matching/index.ts +12 -0
- package/src/core/matching/match-skeleton.test.ts +179 -0
- package/src/core/matching/match-skeleton.ts +621 -0
- package/src/core/matching/stock-confidence.ts +238 -0
- package/src/core/matching/stock-reader.test.ts +265 -0
- package/src/core/matching/stock-reader.ts +205 -0
- package/src/core/matching/stock-reading.ts +681 -0
- package/src/core/matching/value-reader.ts +85 -0
- package/src/core/pattern-matching/pattern-matcher.ts +11 -876
- package/src/core/types.ts +53 -0
- package/src/generation/index.ts +2 -0
- package/src/generation/pattern-render.ts +378 -0
- package/src/generation/schema-patterns.test.ts +139 -0
- package/src/generation/schema-patterns.ts +974 -0
- package/src/testing/gate/index.ts +8 -0
- package/src/testing/gate/lint/format.ts +23 -0
- package/src/testing/gate/lint/index.ts +73 -0
- package/src/testing/gate/lint/lint.test.ts +131 -0
- package/src/testing/gate/lint/rules/extractor-coverage.ts +63 -0
- package/src/testing/gate/lint/rules/index.ts +37 -0
- package/src/testing/gate/lint/rules/keyword-classification.ts +52 -0
- package/src/testing/gate/lint/rules/keyword-coverage.ts +38 -0
- package/src/testing/gate/lint/rules/marker-tokenization.ts +129 -0
- package/src/testing/gate/lint/rules/position-ordering.ts +87 -0
- package/src/testing/gate/lint/rules/renderer-coherence.ts +100 -0
- package/src/testing/gate/lint/rules/schema-rules.ts +35 -0
- package/src/testing/gate/lint/types.ts +83 -0
- package/src/testing/gate/round-trip.test.ts +208 -0
- package/src/testing/gate/round-trip.ts +503 -0
package/dist/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,644 @@ 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
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
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.
|
|
1403
|
+
*/
|
|
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.
|
|
1311
1413
|
*/
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
}
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
method: ["\u0628"]
|
|
1333
|
-
// method prefers ب (with/by)
|
|
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;
|
|
1347
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;
|
|
1348
1633
|
}
|
|
1349
1634
|
}
|
|
1350
|
-
return
|
|
1635
|
+
return true;
|
|
1351
1636
|
}
|
|
1352
1637
|
/**
|
|
1353
|
-
*
|
|
1354
|
-
* This enables more natural English syntax like "toggle the .active".
|
|
1638
|
+
* Match a single pattern token against the current position in the stream.
|
|
1355
1639
|
*/
|
|
1356
|
-
|
|
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);
|
|
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) {
|
|
1357
1662
|
const token = tokens.peek();
|
|
1358
|
-
if (!token) return;
|
|
1359
|
-
const
|
|
1360
|
-
if (
|
|
1361
|
-
|
|
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++;
|
|
1669
|
+
}
|
|
1362
1670
|
tokens.advance();
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1671
|
+
return true;
|
|
1672
|
+
}
|
|
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
|
+
}
|
|
1366
1692
|
}
|
|
1367
|
-
tokens.reset(mark);
|
|
1368
1693
|
}
|
|
1369
|
-
|
|
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
|
+
}
|
|
1370
1722
|
tokens.advance();
|
|
1371
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
|
+
function isListedWord(token, words) {
|
|
1880
|
+
if (!token) return false;
|
|
1881
|
+
const forms = [token.value, token.normalized].filter((f) => typeof f === "string");
|
|
1882
|
+
return forms.some((form) => words.some((word) => word.toLowerCase() === form.toLowerCase()));
|
|
1883
|
+
}
|
|
1884
|
+
var StockValueReader = class {
|
|
1885
|
+
constructor(view, profiles = {}) {
|
|
1886
|
+
this.view = view;
|
|
1887
|
+
this.profiles = profiles;
|
|
1888
|
+
}
|
|
1889
|
+
beginPattern(pattern) {
|
|
1890
|
+
this.profile = this.profiles[pattern.language] ?? { code: pattern.language };
|
|
1891
|
+
this.lastOptional = void 0;
|
|
1892
|
+
}
|
|
1893
|
+
readRole(tokens, slot, captured, next) {
|
|
1894
|
+
const start = tokens.mark();
|
|
1895
|
+
if (this.readOnce(tokens, slot, captured, next, true)) {
|
|
1896
|
+
if (!slot.optional) {
|
|
1897
|
+
this.lastOptional = void 0;
|
|
1898
|
+
} else if (captured.has(slot.role) && tokens.position() > start.position && !this.view.slotAfterLiteral() && next?.type !== "literal") {
|
|
1899
|
+
this.lastOptional = { role: slot.role, start, end: tokens.position() };
|
|
1900
|
+
}
|
|
1901
|
+
return true;
|
|
1902
|
+
}
|
|
1903
|
+
const before = this.lastOptional;
|
|
1904
|
+
if (slot.optional || !before || before.end !== tokens.position()) return false;
|
|
1905
|
+
this.lastOptional = void 0;
|
|
1906
|
+
tokens.reset(before.start);
|
|
1907
|
+
captured.delete(before.role);
|
|
1908
|
+
return this.readOnce(tokens, slot, captured, next, false);
|
|
1372
1909
|
}
|
|
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()) {
|
|
1910
|
+
/** One read of `slot`; `guard` refuses a required slot the marker the pattern wants next. */
|
|
1911
|
+
readOnce(tokens, slot, captured, next, guard) {
|
|
1912
|
+
if (guard && !slot.optional && next?.type === "literal") {
|
|
1384
1913
|
const token = tokens.peek();
|
|
1385
|
-
if (
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
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;
|
|
1914
|
+
if (token && this.view.wouldMatch(next, token)) return false;
|
|
1915
|
+
}
|
|
1916
|
+
if (slot.words && !isListedWord(tokens.peek(), slot.words)) return slot.optional ?? false;
|
|
1917
|
+
if (slot.optional && !slot.greedy) {
|
|
1918
|
+
const token = tokens.peek();
|
|
1919
|
+
if (token && isStopMarker(token, greedyStopMarkers(this.view.patternRest()))) return true;
|
|
1920
|
+
}
|
|
1921
|
+
if (slot.greedy) {
|
|
1922
|
+
const value = readGreedy(tokens, greedyStopMarkers(this.view.patternRest()));
|
|
1923
|
+
if (value) {
|
|
1924
|
+
captured.set(slot.role, value);
|
|
1925
|
+
return true;
|
|
1412
1926
|
}
|
|
1413
|
-
|
|
1927
|
+
return slot.optional ?? false;
|
|
1414
1928
|
}
|
|
1415
|
-
return
|
|
1929
|
+
return readRoleValue(tokens, slot, captured, this.profile);
|
|
1930
|
+
}
|
|
1931
|
+
maySkipSlot(patternToken, token, next) {
|
|
1932
|
+
const slot = patternToken.type === "group" && patternToken.optional ? patternToken.tokens.every((t) => t.type === "role") ? patternToken.tokens[0] : void 0 : patternToken;
|
|
1933
|
+
if (!slot || slot.type !== "role" || !slot.optional) return false;
|
|
1934
|
+
return !!token && this.view.wouldMatch(next, token);
|
|
1935
|
+
}
|
|
1936
|
+
atClauseEnd(tokens) {
|
|
1937
|
+
return tokens.isAtEnd();
|
|
1416
1938
|
}
|
|
1417
1939
|
};
|
|
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);
|
|
1940
|
+
function createStockReader(profiles = {}) {
|
|
1941
|
+
return (view) => new StockValueReader(view, profiles);
|
|
1443
1942
|
}
|
|
1444
1943
|
|
|
1445
1944
|
// src/generation/pattern-generator.ts
|
|
@@ -1638,79 +2137,664 @@ function buildTablesFromProfiles(schemas, profiles) {
|
|
|
1638
2137
|
}
|
|
1639
2138
|
}
|
|
1640
2139
|
}
|
|
1641
|
-
return { keywords, markers };
|
|
2140
|
+
return { keywords, markers };
|
|
2141
|
+
}
|
|
2142
|
+
function detectWordOrders(profiles) {
|
|
2143
|
+
const sovLanguages = /* @__PURE__ */ new Set();
|
|
2144
|
+
const vsoLanguages = /* @__PURE__ */ new Set();
|
|
2145
|
+
for (const profile of profiles) {
|
|
2146
|
+
if (profile.wordOrder === "SOV") sovLanguages.add(profile.code);
|
|
2147
|
+
if (profile.wordOrder === "VSO") vsoLanguages.add(profile.code);
|
|
2148
|
+
}
|
|
2149
|
+
return { sovLanguages, vsoLanguages };
|
|
2150
|
+
}
|
|
2151
|
+
function createSchemaRenderer(schemas, profiles) {
|
|
2152
|
+
const tables = buildRenderTables(schemas, profiles);
|
|
2153
|
+
return {
|
|
2154
|
+
render(node, language) {
|
|
2155
|
+
return renderFromSchema(node, language, tables) ?? node.action;
|
|
2156
|
+
}
|
|
2157
|
+
};
|
|
2158
|
+
}
|
|
2159
|
+
function createDomainRenderer(config) {
|
|
2160
|
+
const { schemas, profiles, overrides } = config;
|
|
2161
|
+
let tables;
|
|
2162
|
+
return (node, language) => {
|
|
2163
|
+
const override = overrides?.[node.action];
|
|
2164
|
+
if (override) return override(node, language);
|
|
2165
|
+
tables ?? (tables = buildRenderTables(schemas, profiles));
|
|
2166
|
+
return renderFromSchema(node, language, tables);
|
|
2167
|
+
};
|
|
2168
|
+
}
|
|
2169
|
+
function buildRenderTables(schemas, profiles) {
|
|
2170
|
+
const { keywords, markers } = buildTablesFromProfiles(schemas, profiles);
|
|
2171
|
+
const { sovLanguages } = detectWordOrders(profiles);
|
|
2172
|
+
const markerPositions = {};
|
|
2173
|
+
for (const profile of profiles) {
|
|
2174
|
+
for (const [role, markerDef] of Object.entries(profile.roleMarkers ?? {})) {
|
|
2175
|
+
if (!markerDef.position) continue;
|
|
2176
|
+
markerPositions[role] ?? (markerPositions[role] = {});
|
|
2177
|
+
markerPositions[role][profile.code] = markerDef.position;
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
const schemaMap = /* @__PURE__ */ new Map();
|
|
2181
|
+
for (const s of schemas) schemaMap.set(s.action, s);
|
|
2182
|
+
return { keywords, markers, markerPositions, sovLanguages, schemaMap };
|
|
2183
|
+
}
|
|
2184
|
+
function renderFromSchema(node, language, tables) {
|
|
2185
|
+
const schema = tables.schemaMap.get(node.action);
|
|
2186
|
+
if (!schema) return null;
|
|
2187
|
+
const keyword = lookupKeyword(tables.keywords, node.action, language);
|
|
2188
|
+
const isSOV = tables.sovLanguages.has(language);
|
|
2189
|
+
const ordered = sortRolesByWordOrder([...schema.roles], isSOV ? "SOV" : "SVO");
|
|
2190
|
+
const preVerb = [];
|
|
2191
|
+
const postVerb = [];
|
|
2192
|
+
for (const role of ordered) {
|
|
2193
|
+
let value = (0, import_intent.extractRoleValue)(node, role.role);
|
|
2194
|
+
if (!value && role.default !== void 0) {
|
|
2195
|
+
value = String((0, import_intent.extractValue)(role.default));
|
|
2196
|
+
}
|
|
2197
|
+
if (!value) continue;
|
|
2198
|
+
if (role.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
|
|
2199
|
+
value = `"${value}"`;
|
|
2200
|
+
}
|
|
2201
|
+
const markerText = role.renderOverride?.[language] ?? role.markerOverride?.[language] ?? role.renderOverride?.["*"] ?? tables.markers[role.role]?.[language];
|
|
2202
|
+
const markerPosition = role.markerPositionOverride?.[language] ?? role.markerPosition ?? tables.markerPositions[role.role]?.[language] ?? (isSOV ? "after" : "before");
|
|
2203
|
+
const bucket = isSOV && role.sovSlot === "postVerb" ? postVerb : preVerb;
|
|
2204
|
+
if (markerText && markerPosition === "before") {
|
|
2205
|
+
bucket.push(markerText, value);
|
|
2206
|
+
} else if (markerText) {
|
|
2207
|
+
bucket.push(value, markerText);
|
|
2208
|
+
} else {
|
|
2209
|
+
bucket.push(value);
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
return isSOV ? buildPhrase(...preVerb, keyword, ...postVerb) : buildPhrase(keyword, ...preVerb, ...postVerb);
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
// src/generation/schema-patterns.ts
|
|
2216
|
+
function createSchemaPatternGenerator(grammar) {
|
|
2217
|
+
return {
|
|
2218
|
+
generatePattern: (schema, profile, config) => generatePattern2(grammar, schema, profile, config),
|
|
2219
|
+
generateSimplePattern: (schema, profile, config) => generateSimplePattern(grammar, schema, profile, config),
|
|
2220
|
+
generateVerbFirstPattern: (schema, profile, config) => generateVerbFirstPattern(schema, profile, config),
|
|
2221
|
+
generatePatternVariants: (schema, profile, config) => generatePatternVariants2(grammar, schema, profile, config),
|
|
2222
|
+
buildRoleToken
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
function withoutVariantOnlyRoles(schema) {
|
|
2226
|
+
return schema.roles.some((r) => r.variantOnly) ? { ...schema, roles: schema.roles.filter((r) => !r.variantOnly) } : schema;
|
|
2227
|
+
}
|
|
2228
|
+
function sortRolesByPosition(roles, wordOrder, order) {
|
|
2229
|
+
const sortKey = wordOrder === "SOV" ? "sovPosition" : "svoPosition";
|
|
2230
|
+
return [...roles].sort(
|
|
2231
|
+
(a, b) => order === "ascending" ? (a[sortKey] ?? 99) - (b[sortKey] ?? 99) : (b[sortKey] ?? 0) - (a[sortKey] ?? 0)
|
|
2232
|
+
);
|
|
2233
|
+
}
|
|
2234
|
+
function resolveMarkerForRole(roleSpec, profile) {
|
|
2235
|
+
const overrideMarker = roleSpec.markerOverride?.[profile.code];
|
|
2236
|
+
const defaultMarker = profile.roleMarkers[roleSpec.role];
|
|
2237
|
+
if (overrideMarker !== void 0) {
|
|
2238
|
+
const alternatives = legacyMarkerAlternatives(roleSpec, profile.code, overrideMarker);
|
|
2239
|
+
return {
|
|
2240
|
+
primary: overrideMarker,
|
|
2241
|
+
...alternatives && { alternatives },
|
|
2242
|
+
position: defaultMarker?.position ?? "before",
|
|
2243
|
+
isOverride: true
|
|
2244
|
+
};
|
|
2245
|
+
}
|
|
2246
|
+
if (defaultMarker && defaultMarker.primary) {
|
|
2247
|
+
const result = {
|
|
2248
|
+
primary: defaultMarker.primary,
|
|
2249
|
+
position: defaultMarker.position,
|
|
2250
|
+
isOverride: false
|
|
2251
|
+
};
|
|
2252
|
+
if (defaultMarker.alternatives) {
|
|
2253
|
+
result.alternatives = defaultMarker.alternatives;
|
|
2254
|
+
}
|
|
2255
|
+
return result;
|
|
2256
|
+
}
|
|
2257
|
+
return null;
|
|
2258
|
+
}
|
|
2259
|
+
function legacyMarkerAlternatives(roleSpec, languageCode, overrideMarker) {
|
|
2260
|
+
const legacy = roleSpec.markerLegacy?.[languageCode];
|
|
2261
|
+
if (!legacy?.length) return void 0;
|
|
2262
|
+
const alternatives = [...new Set(legacy)].filter((a) => a && a !== overrideMarker);
|
|
2263
|
+
return alternatives.length ? alternatives : void 0;
|
|
2264
|
+
}
|
|
2265
|
+
function schemaMarkerAlternatives(roleSpec, languageCode, marker) {
|
|
2266
|
+
const legacy = roleSpec.markerLegacy?.[languageCode] ?? [];
|
|
2267
|
+
const variants = roleSpec.methodCarrier ? [] : roleSpec.markerVariants?.[languageCode] ?? [];
|
|
2268
|
+
const alternatives = [.../* @__PURE__ */ new Set([...legacy, ...variants])].filter((a) => a && a !== marker);
|
|
2269
|
+
return alternatives.length ? alternatives : void 0;
|
|
2270
|
+
}
|
|
2271
|
+
var defaultConfig2 = {
|
|
2272
|
+
basePriority: 100,
|
|
2273
|
+
generateSimpleVariants: true,
|
|
2274
|
+
generateAlternatives: true
|
|
2275
|
+
};
|
|
2276
|
+
function generatePattern2(grammar, schema, profile, config = defaultConfig2) {
|
|
2277
|
+
const id = `${schema.action}-${profile.code}-generated`;
|
|
2278
|
+
const priority = config.basePriority ?? 100;
|
|
2279
|
+
const keyword = profile.keywords[schema.action];
|
|
2280
|
+
if (!keyword) {
|
|
2281
|
+
throw new Error(`No keyword translation for '${schema.action}' in ${profile.code}`);
|
|
2282
|
+
}
|
|
2283
|
+
const tokens = buildTokens2(grammar, schema, profile, keyword);
|
|
2284
|
+
const extraction = buildExtractionRulesWithDefaults(schema, profile);
|
|
2285
|
+
const format = buildFormatString2(grammar, schema, profile, keyword);
|
|
2286
|
+
return {
|
|
2287
|
+
id,
|
|
2288
|
+
language: profile.code,
|
|
2289
|
+
command: schema.action,
|
|
2290
|
+
priority,
|
|
2291
|
+
template: {
|
|
2292
|
+
format,
|
|
2293
|
+
tokens
|
|
2294
|
+
},
|
|
2295
|
+
extraction
|
|
2296
|
+
};
|
|
2297
|
+
}
|
|
2298
|
+
function generateSimplePattern(grammar, schema, profile, config = defaultConfig2) {
|
|
2299
|
+
const optionalRoles = schema.roles.filter((r) => !r.required);
|
|
2300
|
+
if (optionalRoles.length === 0) {
|
|
2301
|
+
return null;
|
|
2302
|
+
}
|
|
2303
|
+
const requiredRoles = schema.roles.filter((r) => r.required);
|
|
2304
|
+
const simpleSchema = {
|
|
2305
|
+
...schema,
|
|
2306
|
+
roles: requiredRoles
|
|
2307
|
+
};
|
|
2308
|
+
const pattern = generatePattern2(grammar, simpleSchema, profile, config);
|
|
2309
|
+
return {
|
|
2310
|
+
...pattern,
|
|
2311
|
+
id: `${schema.action}-${profile.code}-generated-simple`,
|
|
2312
|
+
priority: (config.basePriority ?? 100) - 5,
|
|
2313
|
+
// Lower priority than full pattern (was -10)
|
|
2314
|
+
extraction: buildExtractionRulesWithDefaults(schema, profile)
|
|
2315
|
+
};
|
|
2316
|
+
}
|
|
2317
|
+
function generateVerbFirstPattern(schema, profile, config = defaultConfig2) {
|
|
2318
|
+
if (profile.wordOrder !== "SOV") return null;
|
|
2319
|
+
const requiredRoles = schema.roles.filter((r) => r.required);
|
|
2320
|
+
if (requiredRoles.length !== 1) return null;
|
|
2321
|
+
const keyword = profile.keywords[schema.action];
|
|
2322
|
+
if (!keyword) return null;
|
|
2323
|
+
const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
|
|
2324
|
+
const roleTokens = requiredRoles.flatMap((r) => {
|
|
2325
|
+
const prefix = r.valuePrefixLiteral?.[profile.code];
|
|
2326
|
+
const words = wordsFor(r, profile.code);
|
|
2327
|
+
const roleToken = {
|
|
2328
|
+
type: "role",
|
|
2329
|
+
role: r.role,
|
|
2330
|
+
optional: false,
|
|
2331
|
+
expectedTypes: r.expectedTypes,
|
|
2332
|
+
...words ? { words } : {}
|
|
2333
|
+
};
|
|
2334
|
+
return prefix ? [{ type: "literal", value: prefix }, roleToken] : [roleToken];
|
|
2335
|
+
});
|
|
2336
|
+
return {
|
|
2337
|
+
id: `${schema.action}-${profile.code}-generated-verb-first`,
|
|
2338
|
+
language: profile.code,
|
|
2339
|
+
command: schema.action,
|
|
2340
|
+
// Strictly below the canonical (basePriority) and simple (basePriority - 5)
|
|
2341
|
+
// patterns so native order is always preferred.
|
|
2342
|
+
priority: (config.basePriority ?? 100) - 20,
|
|
2343
|
+
template: {
|
|
2344
|
+
format: `${keyword.primary} <${requiredRoles[0].role}>`,
|
|
2345
|
+
tokens: [verbToken, ...roleTokens]
|
|
2346
|
+
},
|
|
2347
|
+
extraction: buildExtractionRulesWithDefaults(schema, profile)
|
|
2348
|
+
};
|
|
2349
|
+
}
|
|
2350
|
+
function generatePatternVariants2(grammar, schema, profile, config = defaultConfig2) {
|
|
2351
|
+
const patterns = [];
|
|
2352
|
+
const full = schema;
|
|
2353
|
+
schema = withoutVariantOnlyRoles(schema);
|
|
2354
|
+
patterns.push(generatePattern2(grammar, schema, profile, config));
|
|
2355
|
+
if (config.generateSimpleVariants) {
|
|
2356
|
+
const simple = generateSimplePattern(grammar, schema, profile, config);
|
|
2357
|
+
if (simple) {
|
|
2358
|
+
patterns.push(simple);
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
for (const omitted of schema.omitRoleVariants ?? []) {
|
|
2362
|
+
const variantSchema = {
|
|
2363
|
+
...schema,
|
|
2364
|
+
roles: schema.roles.filter((r) => r.role !== omitted)
|
|
2365
|
+
};
|
|
2366
|
+
const variant = generatePattern2(grammar, variantSchema, profile, config);
|
|
2367
|
+
patterns.push({
|
|
2368
|
+
...variant,
|
|
2369
|
+
id: `${schema.action}-${profile.code}-generated-no-${omitted}`,
|
|
2370
|
+
priority: (config.basePriority ?? 100) - 8
|
|
2371
|
+
});
|
|
2372
|
+
}
|
|
2373
|
+
for (const widen of schema.widenTypeVariants ?? []) {
|
|
2374
|
+
if (widen.excludeLanguages?.includes(profile.code)) continue;
|
|
2375
|
+
const cloneSchema = {
|
|
2376
|
+
...schema,
|
|
2377
|
+
roles: schema.roles.map(
|
|
2378
|
+
(r) => r.role === widen.role ? { ...r, expectedTypes: [...widen.types] } : r
|
|
2379
|
+
)
|
|
2380
|
+
};
|
|
2381
|
+
const main = generatePattern2(grammar, cloneSchema, profile, config);
|
|
2382
|
+
patterns.push({
|
|
2383
|
+
...main,
|
|
2384
|
+
id: `${schema.action}-${profile.code}-generated-${widen.idSuffix}`,
|
|
2385
|
+
priority: (config.basePriority ?? 100) - (widen.priorityDelta ?? 15)
|
|
2386
|
+
});
|
|
2387
|
+
}
|
|
2388
|
+
for (const v of full.requiredRoleVariants ?? []) {
|
|
2389
|
+
const variantSchema = {
|
|
2390
|
+
...full,
|
|
2391
|
+
roles: full.roles.filter((r) => !v.omit?.includes(r.role)).map((r) => {
|
|
2392
|
+
const required = r.role === v.role ? { ...r, required: true } : r;
|
|
2393
|
+
return r.role === v.widen?.role ? { ...required, expectedTypes: [...v.widen.types] } : required;
|
|
2394
|
+
})
|
|
2395
|
+
};
|
|
2396
|
+
const variant = generatePattern2(grammar, variantSchema, profile, config);
|
|
2397
|
+
patterns.push({
|
|
2398
|
+
...variant,
|
|
2399
|
+
id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
|
|
2400
|
+
priority: (config.basePriority ?? 100) + (v.priorityDelta ?? 5)
|
|
2401
|
+
});
|
|
2402
|
+
}
|
|
2403
|
+
if (config.generateVerbFirstVariants !== false) {
|
|
2404
|
+
const verbFirst = generateVerbFirstPattern(schema, profile, config);
|
|
2405
|
+
if (verbFirst) {
|
|
2406
|
+
patterns.push(verbFirst);
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
for (const v of schema.rolePrefixLiteralVariants ?? []) {
|
|
2410
|
+
const literal = v.literal[profile.code];
|
|
2411
|
+
if (!literal) continue;
|
|
2412
|
+
const { rolePrefixLiteralVariants: _omitted, ...baseSchema } = schema;
|
|
2413
|
+
const cloneSchema = {
|
|
2414
|
+
...baseSchema,
|
|
2415
|
+
roles: schema.roles.map(
|
|
2416
|
+
(r) => r.role === v.role ? { ...r, valuePrefixLiteral: { [profile.code]: literal } } : r
|
|
2417
|
+
)
|
|
2418
|
+
};
|
|
2419
|
+
const delta = v.priorityDelta ?? 5;
|
|
2420
|
+
const carrier = v.methodCarrier ? { [v.methodCarrier]: { value: literal } } : {};
|
|
2421
|
+
const main = generatePattern2(grammar, cloneSchema, profile, config);
|
|
2422
|
+
patterns.push({
|
|
2423
|
+
...main,
|
|
2424
|
+
id: `${schema.action}-${profile.code}-generated-${v.idSuffix}`,
|
|
2425
|
+
priority: (config.basePriority ?? 100) + delta,
|
|
2426
|
+
extraction: { ...main.extraction, ...carrier }
|
|
2427
|
+
});
|
|
2428
|
+
if (config.generateVerbFirstVariants !== false) {
|
|
2429
|
+
const verbFirstUrl = generateVerbFirstPattern(cloneSchema, profile, config);
|
|
2430
|
+
if (verbFirstUrl) {
|
|
2431
|
+
patterns.push({
|
|
2432
|
+
...verbFirstUrl,
|
|
2433
|
+
id: `${schema.action}-${profile.code}-generated-verb-first-${v.idSuffix}`,
|
|
2434
|
+
priority: (config.basePriority ?? 100) - 20 + delta,
|
|
2435
|
+
extraction: { ...verbFirstUrl.extraction, ...carrier }
|
|
2436
|
+
});
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
return patterns;
|
|
2441
|
+
}
|
|
2442
|
+
function buildTokens2(grammar, schema, profile, keyword) {
|
|
2443
|
+
const tokens = [];
|
|
2444
|
+
const verbToken = keyword.alternatives ? { type: "literal", value: keyword.primary, alternatives: keyword.alternatives } : { type: "literal", value: keyword.primary };
|
|
2445
|
+
const postVerbRoles = profile.wordOrder === "SOV" ? schema.roles.filter((r) => r.sovSlot === "postVerb") : [];
|
|
2446
|
+
const roleTokens = buildRoleTokens(
|
|
2447
|
+
grammar,
|
|
2448
|
+
postVerbRoles.length ? { ...schema, roles: schema.roles.filter((r) => r.sovSlot !== "postVerb") } : schema,
|
|
2449
|
+
profile
|
|
2450
|
+
);
|
|
2451
|
+
const postVerbTokens = postVerbRoles.length ? buildRoleTokens(grammar, { ...schema, roles: postVerbRoles }, profile) : [];
|
|
2452
|
+
switch (profile.wordOrder) {
|
|
2453
|
+
case "SVO":
|
|
2454
|
+
tokens.push(verbToken);
|
|
2455
|
+
tokens.push(...roleTokens);
|
|
2456
|
+
break;
|
|
2457
|
+
case "SOV":
|
|
2458
|
+
tokens.push(...roleTokens);
|
|
2459
|
+
tokens.push(verbToken);
|
|
2460
|
+
tokens.push(...postVerbTokens);
|
|
2461
|
+
break;
|
|
2462
|
+
case "VSO":
|
|
2463
|
+
tokens.push(verbToken);
|
|
2464
|
+
tokens.push(...roleTokens);
|
|
2465
|
+
break;
|
|
2466
|
+
default:
|
|
2467
|
+
tokens.push(verbToken);
|
|
2468
|
+
tokens.push(...roleTokens);
|
|
2469
|
+
}
|
|
2470
|
+
return collapseAdjacentLiterals(tokens);
|
|
2471
|
+
}
|
|
2472
|
+
function collapseAdjacentLiterals(tokens) {
|
|
2473
|
+
const out = [];
|
|
2474
|
+
for (const tok of tokens) {
|
|
2475
|
+
const prev = out[out.length - 1];
|
|
2476
|
+
if (prev && prev.type === "literal" && tok.type === "literal" && prev.value === tok.value) {
|
|
2477
|
+
const alts = [...prev.alternatives ?? [], ...tok.alternatives ?? []];
|
|
2478
|
+
if (alts.length) out[out.length - 1] = { ...prev, alternatives: [...new Set(alts)] };
|
|
2479
|
+
continue;
|
|
2480
|
+
}
|
|
2481
|
+
out.push(tok);
|
|
2482
|
+
}
|
|
2483
|
+
return out;
|
|
2484
|
+
}
|
|
2485
|
+
function buildRoleTokens(grammar, schema, profile) {
|
|
2486
|
+
const tokens = [];
|
|
2487
|
+
const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
|
|
2488
|
+
for (const roleSpec of sortedRoles) {
|
|
2489
|
+
const roleToken = buildRoleToken(roleSpec, profile);
|
|
2490
|
+
if (!roleSpec.required) {
|
|
2491
|
+
tokens.push({
|
|
2492
|
+
type: "group",
|
|
2493
|
+
optional: true,
|
|
2494
|
+
tokens: roleToken
|
|
2495
|
+
});
|
|
2496
|
+
} else {
|
|
2497
|
+
tokens.push(...roleToken);
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
return tokens;
|
|
2501
|
+
}
|
|
2502
|
+
function wordsFor(roleSpec, language) {
|
|
2503
|
+
if (!roleSpec.words) return void 0;
|
|
2504
|
+
const words = [...roleSpec.words["*"] ?? [], ...roleSpec.words[language] ?? []];
|
|
2505
|
+
return words.length > 0 ? words : void 0;
|
|
2506
|
+
}
|
|
2507
|
+
function buildRoleToken(roleSpec, profile) {
|
|
2508
|
+
const tokens = [];
|
|
2509
|
+
const overrideMarker = roleSpec.markerOverride?.[profile.code];
|
|
2510
|
+
const defaultMarker = profile.roleMarkers[roleSpec.role];
|
|
2511
|
+
const suppressMarker = roleSpec.renderOverride?.[profile.code] === "";
|
|
2512
|
+
const words = wordsFor(roleSpec, profile.code);
|
|
2513
|
+
const roleValueToken = {
|
|
2514
|
+
type: "role",
|
|
2515
|
+
role: roleSpec.role,
|
|
2516
|
+
optional: !roleSpec.required,
|
|
2517
|
+
expectedTypes: roleSpec.expectedTypes,
|
|
2518
|
+
// The matcher reads this too — a `'keyword'`-shaped slot holds a fixed
|
|
2519
|
+
// keyword phrase (`using view transition`) and must not be skipped by the
|
|
2520
|
+
// trailing-slot verb guard.
|
|
2521
|
+
...roleSpec.valueShape !== void 0 ? { valueShape: roleSpec.valueShape } : {},
|
|
2522
|
+
// A greedy role reads up to the next marker the pattern owes (a domain's
|
|
2523
|
+
// SQL condition); its value reader does the reading.
|
|
2524
|
+
...roleSpec.greedy ? { greedy: true } : {},
|
|
2525
|
+
// A role with a word list reads only one of its words.
|
|
2526
|
+
...words ? { words } : {}
|
|
2527
|
+
};
|
|
2528
|
+
const prefixLiteral = roleSpec.valuePrefixLiteral?.[profile.code];
|
|
2529
|
+
const pushPrefixed = () => {
|
|
2530
|
+
if (prefixLiteral) tokens.push({ type: "literal", value: prefixLiteral });
|
|
2531
|
+
tokens.push(roleValueToken);
|
|
2532
|
+
};
|
|
2533
|
+
if (overrideMarker !== void 0) {
|
|
2534
|
+
const markerWords = overrideMarker ? overrideMarker.split(/\s+/).filter(Boolean) : [];
|
|
2535
|
+
const isPassthroughOverride = overrideMarker !== void 0 && overrideMarker === roleSpec.markerOverride?.en;
|
|
2536
|
+
const position = roleSpec.markerPositionOverride?.[profile.code] ?? roleSpec.markerPosition ?? defaultMarker?.position ?? (profile.wordOrder === "SOV" && !isPassthroughOverride ? "after" : "before");
|
|
2537
|
+
const optionalMarker = roleSpec.markerOptional?.[profile.code] === true;
|
|
2538
|
+
const pushWord = (word) => {
|
|
2539
|
+
const alternatives = markerWords.length === 1 ? schemaMarkerAlternatives(roleSpec, profile.code, word) ?? [] : [];
|
|
2540
|
+
const literal = {
|
|
2541
|
+
type: "literal",
|
|
2542
|
+
value: word,
|
|
2543
|
+
...alternatives.length ? { alternatives } : {},
|
|
2544
|
+
...suppressMarker ? { renderSuppress: true } : {}
|
|
2545
|
+
};
|
|
2546
|
+
tokens.push(optionalMarker ? { type: "group", optional: true, tokens: [literal] } : literal);
|
|
2547
|
+
};
|
|
2548
|
+
if (position === "before") {
|
|
2549
|
+
for (const word of markerWords) pushWord(word);
|
|
2550
|
+
pushPrefixed();
|
|
2551
|
+
} else {
|
|
2552
|
+
pushPrefixed();
|
|
2553
|
+
for (const word of markerWords) pushWord(word);
|
|
2554
|
+
}
|
|
2555
|
+
} else if (defaultMarker) {
|
|
2556
|
+
const asMarker = () => {
|
|
2557
|
+
const alternatives = [
|
|
2558
|
+
.../* @__PURE__ */ new Set([
|
|
2559
|
+
...defaultMarker.alternatives ?? [],
|
|
2560
|
+
...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
|
|
2561
|
+
])
|
|
2562
|
+
].filter((a) => a !== defaultMarker.primary);
|
|
2563
|
+
return {
|
|
2564
|
+
type: "literal",
|
|
2565
|
+
value: defaultMarker.primary,
|
|
2566
|
+
...alternatives.length ? { alternatives } : {},
|
|
2567
|
+
...suppressMarker ? { renderSuppress: true } : {}
|
|
2568
|
+
};
|
|
2569
|
+
};
|
|
2570
|
+
const pushMarker = (marker) => {
|
|
2571
|
+
if (!profile.markersOptional && !roleSpec.markerOptional?.[profile.code]) {
|
|
2572
|
+
tokens.push(marker);
|
|
2573
|
+
return;
|
|
2574
|
+
}
|
|
2575
|
+
tokens.push({
|
|
2576
|
+
type: "group",
|
|
2577
|
+
optional: true,
|
|
2578
|
+
...profile.markersOptional && !roleSpec.markerOptional?.[profile.code] ? { renderRequired: true } : {},
|
|
2579
|
+
tokens: [marker]
|
|
2580
|
+
});
|
|
2581
|
+
};
|
|
2582
|
+
if ((roleSpec.markerPosition ?? defaultMarker.position) === "before") {
|
|
2583
|
+
if (defaultMarker.primary) {
|
|
2584
|
+
pushMarker(asMarker());
|
|
2585
|
+
}
|
|
2586
|
+
pushPrefixed();
|
|
2587
|
+
} else {
|
|
2588
|
+
pushPrefixed();
|
|
2589
|
+
pushMarker(asMarker());
|
|
2590
|
+
}
|
|
2591
|
+
} else {
|
|
2592
|
+
pushPrefixed();
|
|
2593
|
+
}
|
|
2594
|
+
return tokens;
|
|
2595
|
+
}
|
|
2596
|
+
function buildExtractionRules2(schema, profile) {
|
|
2597
|
+
const rules = {};
|
|
2598
|
+
for (const roleSpec of schema.roles) {
|
|
2599
|
+
const overrideMarker = roleSpec.markerOverride?.[profile.code];
|
|
2600
|
+
const defaultMarker = profile.roleMarkers[roleSpec.role];
|
|
2601
|
+
if (roleSpec.valuePrefixLiteral?.[profile.code]) {
|
|
2602
|
+
rules[roleSpec.role] = { marker: roleSpec.valuePrefixLiteral[profile.code] };
|
|
2603
|
+
} else if (overrideMarker !== void 0) {
|
|
2604
|
+
if (!overrideMarker) {
|
|
2605
|
+
rules[roleSpec.role] = {};
|
|
2606
|
+
} else {
|
|
2607
|
+
const isSingleWord = !/\s/.test(overrideMarker.trim());
|
|
2608
|
+
const markerAlternatives = isSingleWord ? schemaMarkerAlternatives(roleSpec, profile.code, overrideMarker) ?? [] : [];
|
|
2609
|
+
rules[roleSpec.role] = markerAlternatives.length ? { marker: overrideMarker, markerAlternatives } : { marker: overrideMarker };
|
|
2610
|
+
}
|
|
2611
|
+
} else if (defaultMarker && defaultMarker.primary) {
|
|
2612
|
+
const markerAlternatives = [
|
|
2613
|
+
.../* @__PURE__ */ new Set([
|
|
2614
|
+
...defaultMarker.alternatives ?? [],
|
|
2615
|
+
...schemaMarkerAlternatives(roleSpec, profile.code, defaultMarker.primary) ?? []
|
|
2616
|
+
])
|
|
2617
|
+
].filter((a) => a !== defaultMarker.primary);
|
|
2618
|
+
rules[roleSpec.role] = markerAlternatives.length ? { marker: defaultMarker.primary, markerAlternatives } : { marker: defaultMarker.primary };
|
|
2619
|
+
} else {
|
|
2620
|
+
rules[roleSpec.role] = {};
|
|
2621
|
+
}
|
|
2622
|
+
}
|
|
2623
|
+
return rules;
|
|
1642
2624
|
}
|
|
1643
|
-
function
|
|
1644
|
-
const
|
|
1645
|
-
const
|
|
1646
|
-
for (const
|
|
1647
|
-
|
|
1648
|
-
if (
|
|
2625
|
+
function buildExtractionRulesWithDefaults(schema, profile) {
|
|
2626
|
+
const baseRules = buildExtractionRules2(schema, profile);
|
|
2627
|
+
const rules = {};
|
|
2628
|
+
for (const roleSpec of schema.roles) {
|
|
2629
|
+
const baseRule = baseRules[roleSpec.role] || {};
|
|
2630
|
+
if (!roleSpec.required && roleSpec.default) {
|
|
2631
|
+
rules[roleSpec.role] = { ...baseRule, default: roleSpec.default };
|
|
2632
|
+
} else {
|
|
2633
|
+
rules[roleSpec.role] = baseRule;
|
|
2634
|
+
}
|
|
1649
2635
|
}
|
|
1650
|
-
return
|
|
2636
|
+
return rules;
|
|
1651
2637
|
}
|
|
1652
|
-
function
|
|
1653
|
-
const
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
2638
|
+
function buildFormatString2(grammar, schema, profile, keyword) {
|
|
2639
|
+
const parts = [];
|
|
2640
|
+
const sortedRoles = sortRolesByPosition(schema.roles, profile.wordOrder, grammar.positionOrder);
|
|
2641
|
+
const roleParts = sortedRoles.map((roleSpec) => {
|
|
2642
|
+
const resolved = resolveMarkerForRole(roleSpec, profile);
|
|
2643
|
+
let part = "";
|
|
2644
|
+
if (resolved && resolved.primary) {
|
|
2645
|
+
if (resolved.position === "before") {
|
|
2646
|
+
part = `${resolved.primary} {${roleSpec.role}}`;
|
|
2647
|
+
} else {
|
|
2648
|
+
part = `{${roleSpec.role}} ${resolved.primary}`;
|
|
2649
|
+
}
|
|
2650
|
+
} else {
|
|
2651
|
+
part = `{${roleSpec.role}}`;
|
|
1657
2652
|
}
|
|
1658
|
-
|
|
2653
|
+
return roleSpec.required ? part : `[${part}]`;
|
|
2654
|
+
});
|
|
2655
|
+
switch (profile.wordOrder) {
|
|
2656
|
+
case "SVO":
|
|
2657
|
+
case "VSO":
|
|
2658
|
+
parts.push(keyword.primary, ...roleParts);
|
|
2659
|
+
break;
|
|
2660
|
+
case "SOV":
|
|
2661
|
+
parts.push(...roleParts, keyword.primary);
|
|
2662
|
+
break;
|
|
2663
|
+
default:
|
|
2664
|
+
parts.push(keyword.primary, ...roleParts);
|
|
2665
|
+
}
|
|
2666
|
+
return parts.join(" ");
|
|
1659
2667
|
}
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
};
|
|
2668
|
+
|
|
2669
|
+
// src/generation/pattern-render.ts
|
|
2670
|
+
var NESTED_ROLE_BONUS = 5;
|
|
2671
|
+
var UNCOVERED_ROLE_PENALTY = 30;
|
|
2672
|
+
function pinnedLiteral(rule) {
|
|
2673
|
+
if (rule?.default?.type === "literal") return String(rule.default.value).trim().toLowerCase();
|
|
2674
|
+
if (rule?.value !== void 0) return String(rule.value).trim().toLowerCase();
|
|
2675
|
+
return void 0;
|
|
1669
2676
|
}
|
|
1670
|
-
function
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
2677
|
+
function selectRenderPattern(node, patterns, hooks) {
|
|
2678
|
+
let candidates = patterns.filter((pattern) => !pattern.parseOnly);
|
|
2679
|
+
if (candidates.length === 0) candidates = patterns;
|
|
2680
|
+
if (hooks.candidates) candidates = hooks.candidates(node, candidates);
|
|
2681
|
+
const pinnedValues = /* @__PURE__ */ new Map();
|
|
2682
|
+
for (const pattern of candidates) {
|
|
2683
|
+
for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
|
|
2684
|
+
const key = pinnedLiteral(rule);
|
|
2685
|
+
if (key === void 0) continue;
|
|
2686
|
+
const set = pinnedValues.get(role) ?? /* @__PURE__ */ new Set();
|
|
2687
|
+
set.add(key);
|
|
2688
|
+
pinnedValues.set(role, set);
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
const scored = candidates.map((pattern) => {
|
|
2692
|
+
let score = pattern.priority;
|
|
2693
|
+
const hasRealRole = (role) => hooks.hasRealRole(node, role);
|
|
2694
|
+
const scoreTokens = (tokens, inGroup) => {
|
|
2695
|
+
for (const token of tokens) {
|
|
2696
|
+
if (token.type === "group") {
|
|
2697
|
+
scoreTokens(token.tokens, true);
|
|
2698
|
+
} else if (token.type === "role") {
|
|
2699
|
+
if (hasRealRole(token.role)) {
|
|
2700
|
+
score += inGroup ? NESTED_ROLE_BONUS : 10;
|
|
2701
|
+
} else if (!inGroup && !token.optional) {
|
|
2702
|
+
score -= 50;
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
};
|
|
2707
|
+
scoreTokens(pattern.template.tokens, false);
|
|
2708
|
+
const slots = /* @__PURE__ */ new Set();
|
|
2709
|
+
const collectSlots = (tokens) => {
|
|
2710
|
+
for (const token of tokens) {
|
|
2711
|
+
if (token.type === "group") collectSlots(token.tokens);
|
|
2712
|
+
else if (token.type === "role") slots.add(token.role);
|
|
2713
|
+
}
|
|
2714
|
+
};
|
|
2715
|
+
collectSlots(pattern.template.tokens);
|
|
2716
|
+
for (const role of node.roles.keys()) {
|
|
2717
|
+
if (hasRealRole(role) && !slots.has(role)) score -= UNCOVERED_ROLE_PENALTY;
|
|
2718
|
+
}
|
|
2719
|
+
for (const [role, rule] of Object.entries(pattern.extraction ?? {})) {
|
|
2720
|
+
const pinnedKey = pinnedLiteral(rule);
|
|
2721
|
+
if (pinnedKey === void 0) continue;
|
|
2722
|
+
const actual = node.roles.get(role);
|
|
2723
|
+
const actualKey = actual?.type === (rule.valueIsExpression ? "expression" : "literal") ? String(
|
|
2724
|
+
actual.type === "expression" ? actual.raw : actual.value
|
|
2725
|
+
).trim().toLowerCase() : void 0;
|
|
2726
|
+
if (actualKey === pinnedKey) {
|
|
2727
|
+
score += 60;
|
|
2728
|
+
} else if (actualKey === void 0 || pinnedValues.get(role)?.has(actualKey)) {
|
|
2729
|
+
score -= 200;
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
score += hooks.adjustScore?.(pattern, node) ?? 0;
|
|
2733
|
+
return { pattern, score };
|
|
2734
|
+
});
|
|
2735
|
+
scored.sort((a, b) => b.score - a.score);
|
|
2736
|
+
return scored.length > 0 ? scored[0].pattern : null;
|
|
1684
2737
|
}
|
|
1685
|
-
function
|
|
1686
|
-
const
|
|
1687
|
-
|
|
1688
|
-
const
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
value = String((0, import_intent.extractValue)(role.default));
|
|
2738
|
+
function renderPattern(node, pattern, hooks) {
|
|
2739
|
+
const parts = [];
|
|
2740
|
+
const language = pattern.language;
|
|
2741
|
+
const tokens = pattern.template.tokens;
|
|
2742
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
2743
|
+
hooks.beforeToken?.(tokens, i, parts, node, language);
|
|
2744
|
+
const span = hooks.span?.(tokens, i, node, language, true);
|
|
2745
|
+
if (span !== void 0) {
|
|
2746
|
+
parts.push(span.text);
|
|
2747
|
+
i += span.tokens - 1;
|
|
2748
|
+
continue;
|
|
1697
2749
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
2750
|
+
const token = tokens[i];
|
|
2751
|
+
const rendered = renderPatternToken(token, node, language, hooks);
|
|
2752
|
+
if (rendered !== null) {
|
|
2753
|
+
pushPart(hooks, parts, rendered, token, tokens[i - 1], node, language);
|
|
2754
|
+
hooks.afterToken?.(token, parts.length - 1, node);
|
|
1701
2755
|
}
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
2756
|
+
}
|
|
2757
|
+
return parts;
|
|
2758
|
+
}
|
|
2759
|
+
function pushPart(hooks, parts, text, token, previous, node, language) {
|
|
2760
|
+
if (hooks.pushPart) hooks.pushPart(parts, text, token, previous, node, language);
|
|
2761
|
+
else parts.push(text);
|
|
2762
|
+
}
|
|
2763
|
+
function renderPatternToken(token, node, language, hooks) {
|
|
2764
|
+
switch (token.type) {
|
|
2765
|
+
case "literal":
|
|
2766
|
+
return token.renderSuppress ? null : token.value;
|
|
2767
|
+
case "role":
|
|
2768
|
+
return hooks.writeRole(token, node, language);
|
|
2769
|
+
case "group": {
|
|
2770
|
+
const hasRequired = token.tokens.filter((t) => t.type === "role" && !t.optional).every((t) => node.roles.has(t.role));
|
|
2771
|
+
if (!hasRequired && token.optional) {
|
|
2772
|
+
return null;
|
|
2773
|
+
}
|
|
2774
|
+
if (hooks.writeGroup && !hooks.writeGroup(token, node, language)) return null;
|
|
2775
|
+
const groupParts = [];
|
|
2776
|
+
let hasRoleValue = false;
|
|
2777
|
+
for (let i = 0; i < token.tokens.length; i++) {
|
|
2778
|
+
const span = hooks.span?.(token.tokens, i, node, language, false);
|
|
2779
|
+
if (span !== void 0) {
|
|
2780
|
+
groupParts.push(span.text);
|
|
2781
|
+
hasRoleValue = true;
|
|
2782
|
+
i += span.tokens - 1;
|
|
2783
|
+
continue;
|
|
2784
|
+
}
|
|
2785
|
+
const subToken = token.tokens[i];
|
|
2786
|
+
const rendered = renderPatternToken(subToken, node, language, hooks);
|
|
2787
|
+
if (rendered !== null) {
|
|
2788
|
+
pushPart(hooks, groupParts, rendered, subToken, token.tokens[i - 1], node, language);
|
|
2789
|
+
if (subToken.type === "role") hasRoleValue = true;
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
if (token.optional && !hasRoleValue && !token.renderRequired) return null;
|
|
2793
|
+
return groupParts.length > 0 ? groupParts.join(" ") : null;
|
|
1711
2794
|
}
|
|
2795
|
+
default:
|
|
2796
|
+
return null;
|
|
1712
2797
|
}
|
|
1713
|
-
return isSOV ? buildPhrase(...preVerb, keyword, ...postVerb) : buildPhrase(keyword, ...preVerb, ...postVerb);
|
|
1714
2798
|
}
|
|
1715
2799
|
|
|
1716
2800
|
// src/grammar/types.ts
|
|
@@ -2980,12 +4064,15 @@ function readingDifference(source, back) {
|
|
|
2980
4064
|
|
|
2981
4065
|
// src/api/create-dsl.ts
|
|
2982
4066
|
var UnreadInputError = class extends Error {
|
|
2983
|
-
constructor(input, language, reading, unread) {
|
|
2984
|
-
super(
|
|
4067
|
+
constructor(input, language, reading, unread, hint) {
|
|
4068
|
+
super(
|
|
4069
|
+
`"${input}" (${language}) reads as ${reading.action} only by leaving "${unread}" unread` + (hint ? `; ${hint}` : "")
|
|
4070
|
+
);
|
|
2985
4071
|
this.input = input;
|
|
2986
4072
|
this.language = language;
|
|
2987
4073
|
this.reading = reading;
|
|
2988
4074
|
this.unread = unread;
|
|
4075
|
+
this.hint = hint;
|
|
2989
4076
|
this.code = "unread-input";
|
|
2990
4077
|
this.name = "UnreadInputError";
|
|
2991
4078
|
}
|
|
@@ -3010,7 +4097,8 @@ var DSLRegistry = class {
|
|
|
3010
4097
|
* subset of the DSL's languages. A built-in command missing a keyword stays
|
|
3011
4098
|
* an error, since that is a domain-authoring mistake.
|
|
3012
4099
|
*/
|
|
3013
|
-
constructor(config, partiallyTranslatedActions
|
|
4100
|
+
constructor(config, partiallyTranslatedActions, generate) {
|
|
4101
|
+
this.generate = generate;
|
|
3014
4102
|
this.patterns = /* @__PURE__ */ new Map();
|
|
3015
4103
|
this.tokenizers = /* @__PURE__ */ new Map();
|
|
3016
4104
|
this.schemas = config.schemas;
|
|
@@ -3026,7 +4114,7 @@ var DSLRegistry = class {
|
|
|
3026
4114
|
if (this.partiallyTranslatedActions.has(schema.action) && !lang.patternProfile.keywords[schema.action]) {
|
|
3027
4115
|
continue;
|
|
3028
4116
|
}
|
|
3029
|
-
const pattern =
|
|
4117
|
+
const pattern = this.generate(schema, lang.patternProfile);
|
|
3030
4118
|
patterns.push(pattern);
|
|
3031
4119
|
}
|
|
3032
4120
|
this.patterns.set(lang.code, patterns);
|
|
@@ -3042,9 +4130,9 @@ var DSLRegistry = class {
|
|
|
3042
4130
|
}
|
|
3043
4131
|
};
|
|
3044
4132
|
var MultilingualDSLImpl = class {
|
|
3045
|
-
constructor(config, registry, transformer, profileProvider) {
|
|
4133
|
+
constructor(config, registry, transformer, profileProvider, engine) {
|
|
3046
4134
|
this.registry = registry;
|
|
3047
|
-
this.
|
|
4135
|
+
this.match = engine.match;
|
|
3048
4136
|
this.transformer = transformer;
|
|
3049
4137
|
this.profileProvider = profileProvider;
|
|
3050
4138
|
if (config.codeGenerator) {
|
|
@@ -3057,10 +4145,7 @@ var MultilingualDSLImpl = class {
|
|
|
3057
4145
|
for (const extension of config.extensions ?? []) {
|
|
3058
4146
|
if (extension.render) this.extensionRenderers.set(extension.schema.action, extension.render);
|
|
3059
4147
|
}
|
|
3060
|
-
this.schemaFallbackRenderer =
|
|
3061
|
-
schemas: config.schemas,
|
|
3062
|
-
profiles: config.languages.map((l) => l.patternProfile)
|
|
3063
|
-
});
|
|
4148
|
+
this.schemaFallbackRenderer = engine.fallbackRenderer(config, registry);
|
|
3064
4149
|
const schemaMap = new Map(config.schemas.map((s) => [s.action, s]));
|
|
3065
4150
|
this.schemaLookup = {
|
|
3066
4151
|
getSchema: (action) => schemaMap.get(action)
|
|
@@ -3084,15 +4169,12 @@ var MultilingualDSLImpl = class {
|
|
|
3084
4169
|
}
|
|
3085
4170
|
const tokens = tokenizer.tokenize(input);
|
|
3086
4171
|
const patterns = this.registry.getPatterns(language);
|
|
3087
|
-
const profile = {
|
|
3088
|
-
code: language
|
|
3089
|
-
};
|
|
3090
4172
|
const total = tokens.tokens.length;
|
|
3091
4173
|
let best = null;
|
|
3092
4174
|
let longestPartial = null;
|
|
3093
4175
|
for (const pattern of patterns) {
|
|
3094
4176
|
const mark = tokens.mark();
|
|
3095
|
-
const match = this.
|
|
4177
|
+
const match = this.match(tokens, pattern, language);
|
|
3096
4178
|
tokens.reset(mark);
|
|
3097
4179
|
if (!match) continue;
|
|
3098
4180
|
if (match.consumedTokens < total) {
|
|
@@ -3109,15 +4191,32 @@ var MultilingualDSLImpl = class {
|
|
|
3109
4191
|
}
|
|
3110
4192
|
if (longestPartial) {
|
|
3111
4193
|
const unread = tokens.tokens.slice(longestPartial.consumedTokens).map((t) => t.value).join(" ");
|
|
4194
|
+
const hint = this.quoteHint(longestPartial, tokens.tokens);
|
|
3112
4195
|
throw new UnreadInputError(
|
|
3113
4196
|
input,
|
|
3114
4197
|
language,
|
|
3115
4198
|
this.nodeOf(longestPartial, input, language),
|
|
3116
|
-
unread
|
|
4199
|
+
unread,
|
|
4200
|
+
...hint ? [hint] : []
|
|
3117
4201
|
);
|
|
3118
4202
|
}
|
|
3119
4203
|
throw new Error(`No pattern matched for input: ${input}`);
|
|
3120
4204
|
}
|
|
4205
|
+
/**
|
|
4206
|
+
* `if "buy milk" is one value, quote it`, where the last token a match read is a bare word it took
|
|
4207
|
+
* as a role's value (a role with no word list) and the first it left is a bare word too.
|
|
4208
|
+
*/
|
|
4209
|
+
quoteHint(match, tokens) {
|
|
4210
|
+
const last = tokens[match.consumedTokens - 1];
|
|
4211
|
+
const next = tokens[match.consumedTokens];
|
|
4212
|
+
if (last?.kind !== "identifier" || next?.kind !== "identifier") return void 0;
|
|
4213
|
+
const schema = this.schemaLookup.getSchema(match.pattern.command);
|
|
4214
|
+
const role = [...match.captured].find(
|
|
4215
|
+
([, v]) => v.raw === last.value
|
|
4216
|
+
)?.[0];
|
|
4217
|
+
if (!role || schema?.roles.find((r) => r.role === role)?.words) return void 0;
|
|
4218
|
+
return `if "${last.value} ${next.value}" is one value, quote it`;
|
|
4219
|
+
}
|
|
3121
4220
|
nodeOf(match, input, language) {
|
|
3122
4221
|
return {
|
|
3123
4222
|
kind: "command",
|
|
@@ -3336,8 +4435,76 @@ function createMultilingualDSL(config) {
|
|
|
3336
4435
|
dictionary,
|
|
3337
4436
|
profileProvider
|
|
3338
4437
|
});
|
|
3339
|
-
const
|
|
3340
|
-
|
|
4438
|
+
const engine = engineFor(config);
|
|
4439
|
+
const registry = new DSLRegistry(effectiveConfig, extensionActions, engine.generate);
|
|
4440
|
+
return new MultilingualDSLImpl(effectiveConfig, registry, transformer, profileProvider, engine);
|
|
4441
|
+
}
|
|
4442
|
+
function engineFor(config) {
|
|
4443
|
+
const name = config.engine ?? "classic";
|
|
4444
|
+
if (name === "classic") return classicEngine();
|
|
4445
|
+
if (name === "core") return coreEngine();
|
|
4446
|
+
throw new Error(`Unknown DSL engine "${String(name)}" (expected "classic" or "core")`);
|
|
4447
|
+
}
|
|
4448
|
+
function classicEngine() {
|
|
4449
|
+
const matcher = new PatternMatcher();
|
|
4450
|
+
return {
|
|
4451
|
+
generate: (schema, profile) => generatePattern(schema, profile),
|
|
4452
|
+
match: (tokens, pattern, language) => matcher.matchPattern(tokens, pattern, { code: language }),
|
|
4453
|
+
fallbackRenderer: (config) => createDomainRenderer({
|
|
4454
|
+
schemas: config.schemas,
|
|
4455
|
+
profiles: config.languages.map((l) => l.patternProfile)
|
|
4456
|
+
})
|
|
4457
|
+
};
|
|
4458
|
+
}
|
|
4459
|
+
function schemaProfileOf(profile) {
|
|
4460
|
+
const side = profile.wordOrder === "SOV" ? "after" : "before";
|
|
4461
|
+
const roleMarkers = {};
|
|
4462
|
+
for (const [role, marker] of Object.entries(profile.roleMarkers ?? {})) {
|
|
4463
|
+
roleMarkers[role] = {
|
|
4464
|
+
primary: marker.primary,
|
|
4465
|
+
...marker.alternatives ? { alternatives: marker.alternatives } : {},
|
|
4466
|
+
position: marker.position ?? side
|
|
4467
|
+
};
|
|
4468
|
+
}
|
|
4469
|
+
return {
|
|
4470
|
+
code: profile.code,
|
|
4471
|
+
wordOrder: profile.wordOrder,
|
|
4472
|
+
keywords: profile.keywords,
|
|
4473
|
+
roleMarkers
|
|
4474
|
+
};
|
|
4475
|
+
}
|
|
4476
|
+
function coreEngine() {
|
|
4477
|
+
const generator = createSchemaPatternGenerator({ positionOrder: "descending" });
|
|
4478
|
+
const matcher = new MatchSkeleton(createStockReader(), stockConfidenceModel);
|
|
4479
|
+
return {
|
|
4480
|
+
generate: (schema, profile) => generator.generatePattern(schema, schemaProfileOf(profile)),
|
|
4481
|
+
match: (tokens, pattern) => matcher.matchPattern(tokens, pattern),
|
|
4482
|
+
fallbackRenderer: (config, registry) => {
|
|
4483
|
+
const schemas = new Map(config.schemas.map((s) => [s.action, s]));
|
|
4484
|
+
const hooks = {
|
|
4485
|
+
hasRealRole: (node, role) => {
|
|
4486
|
+
const value = node.roles.get(role);
|
|
4487
|
+
return value !== void 0 && value.implicit !== true;
|
|
4488
|
+
},
|
|
4489
|
+
writeRole: (token, node) => {
|
|
4490
|
+
if (!node.roles.has(token.role)) return null;
|
|
4491
|
+
let value = (0, import_intent.extractRoleValue)(node, token.role);
|
|
4492
|
+
const spec = schemas.get(node.action)?.roles.find((r) => r.role === token.role);
|
|
4493
|
+
if (spec?.quoteMultiword && /\s/.test(value) && !/^(["']).*\1$/.test(value)) {
|
|
4494
|
+
value = `"${value}"`;
|
|
4495
|
+
}
|
|
4496
|
+
return value || null;
|
|
4497
|
+
}
|
|
4498
|
+
};
|
|
4499
|
+
return (node, language) => {
|
|
4500
|
+
const patterns = registry.getPatterns(language).filter((p) => p.command === node.action);
|
|
4501
|
+
const pattern = selectRenderPattern(node, patterns, hooks);
|
|
4502
|
+
if (!pattern) return null;
|
|
4503
|
+
const text = renderPattern(node, pattern, hooks).join(" ");
|
|
4504
|
+
return text || null;
|
|
4505
|
+
};
|
|
4506
|
+
}
|
|
4507
|
+
};
|
|
3341
4508
|
}
|
|
3342
4509
|
|
|
3343
4510
|
// src/prompts/prompt-generator.ts
|
|
@@ -4643,6 +5810,50 @@ var CrossDomainDispatcher = class {
|
|
|
4643
5810
|
}
|
|
4644
5811
|
};
|
|
4645
5812
|
|
|
5813
|
+
// src/api/translation-loss.ts
|
|
5814
|
+
var LossyTranslationError = class extends Error {
|
|
5815
|
+
constructor(partial, loss, from, to) {
|
|
5816
|
+
super(
|
|
5817
|
+
`translation ${from} \u2192 ${to} would lose part of the script (${loss.kind}): ` + loss.lost.join(", ")
|
|
5818
|
+
);
|
|
5819
|
+
this.partial = partial;
|
|
5820
|
+
this.loss = loss;
|
|
5821
|
+
this.from = from;
|
|
5822
|
+
this.to = to;
|
|
5823
|
+
this.name = "LossyTranslationError";
|
|
5824
|
+
}
|
|
5825
|
+
};
|
|
5826
|
+
function unconsumedSpans(node) {
|
|
5827
|
+
const out = [];
|
|
5828
|
+
for (const d of node.diagnostics ?? []) {
|
|
5829
|
+
if (d.code !== "unconsumed-input") continue;
|
|
5830
|
+
out.push(/"(.*)"\s*$/s.exec(d.message)?.[1] ?? d.message);
|
|
5831
|
+
}
|
|
5832
|
+
return out;
|
|
5833
|
+
}
|
|
5834
|
+
function findTranslationLoss(input, node, output, readBack, rules) {
|
|
5835
|
+
const unread = unconsumedSpans(node);
|
|
5836
|
+
if (unread.length) return { kind: "truncation", lost: unread };
|
|
5837
|
+
const unwritable = rules.unwritable?.(node) ?? [];
|
|
5838
|
+
if (unwritable.length) return { kind: "core-only", lost: unwritable };
|
|
5839
|
+
let back;
|
|
5840
|
+
try {
|
|
5841
|
+
back = readBack(output);
|
|
5842
|
+
} catch (e) {
|
|
5843
|
+
return { kind: "read-back", lost: [`the output does not read: ${e.message}`] };
|
|
5844
|
+
}
|
|
5845
|
+
const misread = unconsumedSpans(back);
|
|
5846
|
+
if (misread.length) return { kind: "read-back", lost: misread };
|
|
5847
|
+
const written = rules.written ? rules.written(node) : node;
|
|
5848
|
+
for (const difference of rules.differences) {
|
|
5849
|
+
const lost = difference(written, back);
|
|
5850
|
+
if (lost.length) return { kind: "read-back", lost };
|
|
5851
|
+
}
|
|
5852
|
+
const values = rules.missingValues?.(input, output) ?? [];
|
|
5853
|
+
if (values.length) return { kind: "invariant", lost: values };
|
|
5854
|
+
return void 0;
|
|
5855
|
+
}
|
|
5856
|
+
|
|
4646
5857
|
// src/aot/domain-scanner.ts
|
|
4647
5858
|
function buildAttributePatterns(attributes) {
|
|
4648
5859
|
const patterns = [];
|
|
@@ -4934,122 +6145,6 @@ var import_intent6 = require("@lokascript/intent");
|
|
|
4934
6145
|
// src/generation/diagnostics.ts
|
|
4935
6146
|
var import_intent7 = require("@lokascript/intent");
|
|
4936
6147
|
|
|
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
6148
|
// src/core/tokenization/extractors.ts
|
|
5054
6149
|
function extractCssSelector(input, startPos) {
|
|
5055
6150
|
if (startPos >= input.length) return null;
|
|
@@ -6895,6 +7990,8 @@ function accumulateIndented(statements, config) {
|
|
|
6895
7990
|
InMemoryDictionary,
|
|
6896
7991
|
InMemoryProfileProvider,
|
|
6897
7992
|
LatinExtendedIdentifierExtractor,
|
|
7993
|
+
LossyTranslationError,
|
|
7994
|
+
MatchSkeleton,
|
|
6898
7995
|
NullDictionary,
|
|
6899
7996
|
NullProfileProvider,
|
|
6900
7997
|
NumberExtractor,
|
|
@@ -6902,6 +7999,7 @@ function accumulateIndented(statements, config) {
|
|
|
6902
7999
|
PatternMatcher,
|
|
6903
8000
|
PatternTracker,
|
|
6904
8001
|
PunctuationExtractor,
|
|
8002
|
+
StockValueReader,
|
|
6905
8003
|
StringLiteralExtractor,
|
|
6906
8004
|
TokenStreamImpl,
|
|
6907
8005
|
TranslationReadBackError,
|
|
@@ -6936,9 +8034,11 @@ function accumulateIndented(statements, config) {
|
|
|
6936
8034
|
createPosition,
|
|
6937
8035
|
createPropertyPath,
|
|
6938
8036
|
createReference,
|
|
8037
|
+
createSchemaPatternGenerator,
|
|
6939
8038
|
createSchemaRenderer,
|
|
6940
8039
|
createSelector,
|
|
6941
8040
|
createSimpleTokenizer,
|
|
8041
|
+
createStockReader,
|
|
6942
8042
|
createToken,
|
|
6943
8043
|
createTokenizerContext,
|
|
6944
8044
|
createTryNode,
|
|
@@ -6954,6 +8054,7 @@ function accumulateIndented(statements, config) {
|
|
|
6954
8054
|
extractUrl,
|
|
6955
8055
|
extractValue,
|
|
6956
8056
|
filterBySeverity,
|
|
8057
|
+
findTranslationLoss,
|
|
6957
8058
|
fromEnvelopeJSON,
|
|
6958
8059
|
fromError,
|
|
6959
8060
|
fromInterchangeNode,
|
|
@@ -6968,6 +8069,7 @@ function accumulateIndented(statements, config) {
|
|
|
6968
8069
|
getPossessiveReference,
|
|
6969
8070
|
getRoleSpec,
|
|
6970
8071
|
getRoleValue,
|
|
8072
|
+
greedyStopMarkers,
|
|
6971
8073
|
insertMarkers,
|
|
6972
8074
|
isAsciiIdentifierChar,
|
|
6973
8075
|
isAsciiLetter,
|
|
@@ -6995,6 +8097,7 @@ function accumulateIndented(statements, config) {
|
|
|
6995
8097
|
isWhitespace,
|
|
6996
8098
|
joinTokens,
|
|
6997
8099
|
jsonToSemanticNode,
|
|
8100
|
+
legacyMarkerAlternatives,
|
|
6998
8101
|
lookupKeyword,
|
|
6999
8102
|
lookupMarker,
|
|
7000
8103
|
matchBest,
|
|
@@ -7007,24 +8110,33 @@ function accumulateIndented(statements, config) {
|
|
|
7007
8110
|
parseExplicit,
|
|
7008
8111
|
parseJSONL,
|
|
7009
8112
|
patternMatcher,
|
|
8113
|
+
readGreedy,
|
|
8114
|
+
readRoleValue,
|
|
7010
8115
|
registryToAOTBackends,
|
|
7011
8116
|
renderDocument,
|
|
7012
8117
|
renderExplicit,
|
|
7013
8118
|
renderExpr,
|
|
8119
|
+
renderPattern,
|
|
7014
8120
|
reorderRoles,
|
|
8121
|
+
resolveMarkerForRole,
|
|
8122
|
+
schemaMarkerAlternatives,
|
|
8123
|
+
selectRenderPattern,
|
|
7015
8124
|
semanticNodeToJSON,
|
|
7016
8125
|
semanticNodeToRuntimeAST,
|
|
7017
8126
|
semanticValueToAST,
|
|
7018
8127
|
sortRolesByWordOrder,
|
|
8128
|
+
stockConfidenceModel,
|
|
7019
8129
|
stripOptionalDiacritics,
|
|
7020
8130
|
synthesizeFromSchemas,
|
|
7021
8131
|
toEnvelopeJSON,
|
|
7022
8132
|
toJSONL,
|
|
7023
8133
|
toJSONLRow,
|
|
7024
8134
|
toProtocolJSON,
|
|
8135
|
+
unconsumedSpans,
|
|
7025
8136
|
validateProtocolJSON,
|
|
7026
8137
|
validateSemanticJSON,
|
|
7027
8138
|
validateValueType,
|
|
7028
|
-
withDefaultExtractors
|
|
8139
|
+
withDefaultExtractors,
|
|
8140
|
+
withoutVariantOnlyRoles
|
|
7029
8141
|
});
|
|
7030
8142
|
//# sourceMappingURL=index.cjs.map
|