@lokascript/framework 4.6.0 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -1
- package/dist/api/create-dsl.d.ts +13 -0
- package/dist/api/create-dsl.d.ts.map +1 -1
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +1756 -710
- package/dist/api/index.js.map +1 -1
- package/dist/api/translation-loss.d.ts +84 -0
- package/dist/api/translation-loss.d.ts.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1047 -684
- package/dist/core/index.js.map +1 -1
- package/dist/core/matching/confidence-model.d.ts +54 -0
- package/dist/core/matching/confidence-model.d.ts.map +1 -0
- package/dist/core/matching/index.d.ts +12 -0
- package/dist/core/matching/index.d.ts.map +1 -0
- package/dist/core/matching/match-skeleton.d.ts +114 -0
- package/dist/core/matching/match-skeleton.d.ts.map +1 -0
- package/dist/core/matching/stock-confidence.d.ts +15 -0
- package/dist/core/matching/stock-confidence.d.ts.map +1 -0
- package/dist/core/matching/stock-reader.d.ts +56 -0
- package/dist/core/matching/stock-reader.d.ts.map +1 -0
- package/dist/core/matching/stock-reading.d.ts +44 -0
- package/dist/core/matching/stock-reading.d.ts.map +1 -0
- package/dist/core/matching/value-reader.d.ts +62 -0
- package/dist/core/matching/value-reader.d.ts.map +1 -0
- package/dist/core/pattern-matching/index.js +561 -660
- package/dist/core/pattern-matching/index.js.map +1 -1
- package/dist/core/pattern-matching/pattern-matcher.d.ts +0 -117
- package/dist/core/pattern-matching/pattern-matcher.d.ts.map +1 -1
- package/dist/core/types.d.ts +48 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/generation/index.d.ts +2 -0
- package/dist/generation/index.d.ts.map +1 -1
- package/dist/generation/index.js +583 -1
- package/dist/generation/index.js.map +1 -1
- package/dist/generation/pattern-render.d.ts +72 -0
- package/dist/generation/pattern-render.d.ts.map +1 -0
- package/dist/generation/schema-patterns.d.ts +152 -0
- package/dist/generation/schema-patterns.d.ts.map +1 -0
- package/dist/index.cjs +1979 -907
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1961 -906
- package/dist/index.js.map +1 -1
- package/dist/ir/index.js.map +1 -1
- package/dist/parsing/index.js +561 -660
- package/dist/parsing/index.js.map +1 -1
- package/dist/testing/gate/index.d.ts +8 -0
- package/dist/testing/gate/index.d.ts.map +1 -0
- package/dist/testing/gate/index.js +632 -0
- package/dist/testing/gate/index.js.map +1 -0
- package/dist/testing/gate/lint/format.d.ts +7 -0
- package/dist/testing/gate/lint/format.d.ts.map +1 -0
- package/dist/testing/gate/lint/index.d.ts +30 -0
- package/dist/testing/gate/lint/index.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/extractor-coverage.d.ts +18 -0
- package/dist/testing/gate/lint/rules/extractor-coverage.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/index.d.ts +14 -0
- package/dist/testing/gate/lint/rules/index.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/keyword-classification.d.ts +19 -0
- package/dist/testing/gate/lint/rules/keyword-classification.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/keyword-coverage.d.ts +13 -0
- package/dist/testing/gate/lint/rules/keyword-coverage.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/marker-tokenization.d.ts +20 -0
- package/dist/testing/gate/lint/rules/marker-tokenization.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/position-ordering.d.ts +25 -0
- package/dist/testing/gate/lint/rules/position-ordering.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/renderer-coherence.d.ts +24 -0
- package/dist/testing/gate/lint/rules/renderer-coherence.d.ts.map +1 -0
- package/dist/testing/gate/lint/rules/schema-rules.d.ts +10 -0
- package/dist/testing/gate/lint/rules/schema-rules.d.ts.map +1 -0
- package/dist/testing/gate/lint/types.d.ts +74 -0
- package/dist/testing/gate/lint/types.d.ts.map +1 -0
- package/dist/testing/gate/round-trip.d.ts +163 -0
- package/dist/testing/gate/round-trip.d.ts.map +1 -0
- package/package.json +6 -2
- package/src/__test__/fixtures/sql-dsl.ts +8 -2
- package/src/api/create-dsl-engine.test.ts +86 -0
- package/src/api/create-dsl.ts +141 -18
- package/src/api/index.ts +1 -0
- package/src/api/translation-loss.test.ts +89 -0
- package/src/api/translation-loss.ts +128 -0
- package/src/core/index.ts +1 -0
- package/src/core/matching/confidence-model.ts +58 -0
- package/src/core/matching/index.ts +12 -0
- package/src/core/matching/match-skeleton.test.ts +179 -0
- package/src/core/matching/match-skeleton.ts +621 -0
- package/src/core/matching/stock-confidence.ts +238 -0
- package/src/core/matching/stock-reader.test.ts +181 -0
- package/src/core/matching/stock-reader.ts +186 -0
- package/src/core/matching/stock-reading.ts +681 -0
- package/src/core/matching/value-reader.ts +85 -0
- package/src/core/pattern-matching/pattern-matcher.ts +11 -876
- package/src/core/types.ts +48 -0
- package/src/generation/index.ts +2 -0
- package/src/generation/pattern-render.ts +378 -0
- package/src/generation/schema-patterns.test.ts +99 -0
- package/src/generation/schema-patterns.ts +962 -0
- package/src/testing/gate/index.ts +8 -0
- package/src/testing/gate/lint/format.ts +23 -0
- package/src/testing/gate/lint/index.ts +73 -0
- package/src/testing/gate/lint/lint.test.ts +131 -0
- package/src/testing/gate/lint/rules/extractor-coverage.ts +63 -0
- package/src/testing/gate/lint/rules/index.ts +37 -0
- package/src/testing/gate/lint/rules/keyword-classification.ts +52 -0
- package/src/testing/gate/lint/rules/keyword-coverage.ts +38 -0
- package/src/testing/gate/lint/rules/marker-tokenization.ts +129 -0
- package/src/testing/gate/lint/rules/position-ordering.ts +87 -0
- package/src/testing/gate/lint/rules/renderer-coherence.ts +100 -0
- package/src/testing/gate/lint/rules/schema-rules.ts +35 -0
- package/src/testing/gate/lint/types.ts +83 -0
- package/src/testing/gate/round-trip.test.ts +118 -0
- package/src/testing/gate/round-trip.ts +438 -0
package/dist/core/index.js
CHANGED
|
@@ -1809,10 +1809,535 @@ function getAllPossessiveKeywords(profile) {
|
|
|
1809
1809
|
return profile?.possessive?.keywords ?? {};
|
|
1810
1810
|
}
|
|
1811
1811
|
|
|
1812
|
-
// src/core/
|
|
1812
|
+
// src/core/matching/stock-reading.ts
|
|
1813
1813
|
function isValidReference(_value) {
|
|
1814
1814
|
return false;
|
|
1815
1815
|
}
|
|
1816
|
+
function safeToLowerCase(value) {
|
|
1817
|
+
if (typeof value === "string") {
|
|
1818
|
+
return value.toLowerCase();
|
|
1819
|
+
}
|
|
1820
|
+
if (value === null || value === void 0) {
|
|
1821
|
+
return "";
|
|
1822
|
+
}
|
|
1823
|
+
return String(value).toLowerCase();
|
|
1824
|
+
}
|
|
1825
|
+
function readRoleValue(tokens, patternToken, captured, profile) {
|
|
1826
|
+
skipNoiseWords(tokens);
|
|
1827
|
+
const token = tokens.peek();
|
|
1828
|
+
if (!token) {
|
|
1829
|
+
return patternToken.optional || false;
|
|
1830
|
+
}
|
|
1831
|
+
const possessiveValue = tryMatchPossessiveExpression(tokens, profile);
|
|
1832
|
+
if (possessiveValue) {
|
|
1833
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
1834
|
+
if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
|
|
1835
|
+
return patternToken.optional || false;
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
captured.set(patternToken.role, possessiveValue);
|
|
1839
|
+
return true;
|
|
1840
|
+
}
|
|
1841
|
+
const methodCallValue = tryMatchMethodCallExpression(tokens);
|
|
1842
|
+
if (methodCallValue) {
|
|
1843
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
1844
|
+
if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
|
|
1845
|
+
return patternToken.optional || false;
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
captured.set(patternToken.role, methodCallValue);
|
|
1849
|
+
return true;
|
|
1850
|
+
}
|
|
1851
|
+
const possessiveSelectorValue = tryMatchPossessiveSelectorExpression(tokens);
|
|
1852
|
+
if (possessiveSelectorValue) {
|
|
1853
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
1854
|
+
if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
|
|
1855
|
+
return patternToken.optional || false;
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
captured.set(patternToken.role, possessiveSelectorValue);
|
|
1859
|
+
return true;
|
|
1860
|
+
}
|
|
1861
|
+
const propertyAccessValue = tryMatchPropertyAccessExpression(tokens);
|
|
1862
|
+
if (propertyAccessValue) {
|
|
1863
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
1864
|
+
if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
|
|
1865
|
+
return patternToken.optional || false;
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
captured.set(patternToken.role, propertyAccessValue);
|
|
1869
|
+
return true;
|
|
1870
|
+
}
|
|
1871
|
+
const selectorPropertyValue = tryMatchSelectorPropertyExpression(tokens);
|
|
1872
|
+
if (selectorPropertyValue) {
|
|
1873
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
1874
|
+
if (!isTypeCompatible(selectorPropertyValue.type, patternToken.expectedTypes)) {
|
|
1875
|
+
return patternToken.optional || false;
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
captured.set(patternToken.role, selectorPropertyValue);
|
|
1879
|
+
return true;
|
|
1880
|
+
}
|
|
1881
|
+
const value = tokenToSemanticValue(token);
|
|
1882
|
+
if (!value) {
|
|
1883
|
+
return patternToken.optional || false;
|
|
1884
|
+
}
|
|
1885
|
+
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
1886
|
+
if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
|
|
1887
|
+
return patternToken.optional || false;
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
captured.set(patternToken.role, value);
|
|
1891
|
+
tokens.advance();
|
|
1892
|
+
return true;
|
|
1893
|
+
}
|
|
1894
|
+
function tryMatchPossessiveExpression(tokens, profile) {
|
|
1895
|
+
const token = tokens.peek();
|
|
1896
|
+
if (!token) return null;
|
|
1897
|
+
if (!profile) return null;
|
|
1898
|
+
const tokenValue = token.normalized || token.value;
|
|
1899
|
+
const tokenLower = safeToLowerCase(tokenValue);
|
|
1900
|
+
const baseRef = getPossessiveReference(profile, tokenLower);
|
|
1901
|
+
if (!baseRef) return null;
|
|
1902
|
+
const mark = tokens.mark();
|
|
1903
|
+
tokens.advance();
|
|
1904
|
+
const propertyToken = tokens.peek();
|
|
1905
|
+
if (!propertyToken) {
|
|
1906
|
+
tokens.reset(mark);
|
|
1907
|
+
return null;
|
|
1908
|
+
}
|
|
1909
|
+
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)) {
|
|
1910
|
+
tokens.advance();
|
|
1911
|
+
let propertyName = propertyToken.value;
|
|
1912
|
+
if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
|
|
1913
|
+
propertyName = propertyName.substring(1);
|
|
1914
|
+
}
|
|
1915
|
+
let chainedProps = propertyName;
|
|
1916
|
+
while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
|
|
1917
|
+
chainedProps += tokens.peek().value;
|
|
1918
|
+
tokens.advance();
|
|
1919
|
+
}
|
|
1920
|
+
const nextPeek = tokens.peek();
|
|
1921
|
+
if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
|
|
1922
|
+
chainedProps += nextPeek.value;
|
|
1923
|
+
tokens.advance();
|
|
1924
|
+
}
|
|
1925
|
+
return createPropertyPath(createReference(baseRef), chainedProps);
|
|
1926
|
+
}
|
|
1927
|
+
tokens.reset(mark);
|
|
1928
|
+
return null;
|
|
1929
|
+
}
|
|
1930
|
+
function isStructuralKeyword(value) {
|
|
1931
|
+
const structural = /* @__PURE__ */ new Set([
|
|
1932
|
+
// Prepositions
|
|
1933
|
+
"into",
|
|
1934
|
+
"in",
|
|
1935
|
+
"to",
|
|
1936
|
+
"from",
|
|
1937
|
+
"at",
|
|
1938
|
+
"by",
|
|
1939
|
+
"with",
|
|
1940
|
+
"without",
|
|
1941
|
+
"before",
|
|
1942
|
+
"after",
|
|
1943
|
+
"of",
|
|
1944
|
+
"as",
|
|
1945
|
+
"on",
|
|
1946
|
+
// Control flow
|
|
1947
|
+
"then",
|
|
1948
|
+
"end",
|
|
1949
|
+
"else",
|
|
1950
|
+
"if",
|
|
1951
|
+
"repeat",
|
|
1952
|
+
"while",
|
|
1953
|
+
"for",
|
|
1954
|
+
// Commands (shouldn't be property names)
|
|
1955
|
+
"toggle",
|
|
1956
|
+
"add",
|
|
1957
|
+
"remove",
|
|
1958
|
+
"put",
|
|
1959
|
+
"set",
|
|
1960
|
+
"show",
|
|
1961
|
+
"hide",
|
|
1962
|
+
"increment",
|
|
1963
|
+
"decrement",
|
|
1964
|
+
"send",
|
|
1965
|
+
"trigger",
|
|
1966
|
+
"call"
|
|
1967
|
+
]);
|
|
1968
|
+
return structural.has(value.toLowerCase());
|
|
1969
|
+
}
|
|
1970
|
+
function tryMatchMethodCallExpression(tokens) {
|
|
1971
|
+
const token = tokens.peek();
|
|
1972
|
+
if (!token || token.kind !== "selector") return null;
|
|
1973
|
+
const mark = tokens.mark();
|
|
1974
|
+
tokens.advance();
|
|
1975
|
+
const dotToken = tokens.peek();
|
|
1976
|
+
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
1977
|
+
tokens.reset(mark);
|
|
1978
|
+
return null;
|
|
1979
|
+
}
|
|
1980
|
+
tokens.advance();
|
|
1981
|
+
const methodToken = tokens.peek();
|
|
1982
|
+
if (!methodToken || methodToken.kind !== "identifier") {
|
|
1983
|
+
tokens.reset(mark);
|
|
1984
|
+
return null;
|
|
1985
|
+
}
|
|
1986
|
+
tokens.advance();
|
|
1987
|
+
const openParen = tokens.peek();
|
|
1988
|
+
if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
|
|
1989
|
+
tokens.reset(mark);
|
|
1990
|
+
return null;
|
|
1991
|
+
}
|
|
1992
|
+
tokens.advance();
|
|
1993
|
+
const args = [];
|
|
1994
|
+
while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
|
|
1995
|
+
const argToken = tokens.peek();
|
|
1996
|
+
if (!argToken) break;
|
|
1997
|
+
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
1998
|
+
tokens.advance();
|
|
1999
|
+
break;
|
|
2000
|
+
}
|
|
2001
|
+
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
2002
|
+
tokens.advance();
|
|
2003
|
+
continue;
|
|
2004
|
+
}
|
|
2005
|
+
args.push(argToken.value);
|
|
2006
|
+
tokens.advance();
|
|
2007
|
+
}
|
|
2008
|
+
const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
|
|
2009
|
+
return {
|
|
2010
|
+
type: "expression",
|
|
2011
|
+
raw: methodCall
|
|
2012
|
+
};
|
|
2013
|
+
}
|
|
2014
|
+
function tryMatchPropertyAccessExpression(tokens) {
|
|
2015
|
+
const token = tokens.peek();
|
|
2016
|
+
if (!token) return null;
|
|
2017
|
+
if (token.kind !== "identifier" && token.kind !== "keyword") return null;
|
|
2018
|
+
const mark = tokens.mark();
|
|
2019
|
+
tokens.advance();
|
|
2020
|
+
const dotToken = tokens.peek();
|
|
2021
|
+
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
2022
|
+
tokens.reset(mark);
|
|
2023
|
+
return null;
|
|
2024
|
+
}
|
|
2025
|
+
tokens.advance();
|
|
2026
|
+
const propertyToken = tokens.peek();
|
|
2027
|
+
if (!propertyToken || propertyToken.kind !== "identifier") {
|
|
2028
|
+
tokens.reset(mark);
|
|
2029
|
+
return null;
|
|
2030
|
+
}
|
|
2031
|
+
tokens.advance();
|
|
2032
|
+
let chain = `${token.value}.${propertyToken.value}`;
|
|
2033
|
+
let depth = 1;
|
|
2034
|
+
while (!tokens.isAtEnd() && depth < MAX_PROPERTY_DEPTH) {
|
|
2035
|
+
const nextDot = tokens.peek();
|
|
2036
|
+
if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
|
|
2037
|
+
break;
|
|
2038
|
+
}
|
|
2039
|
+
tokens.advance();
|
|
2040
|
+
const nextProp = tokens.peek();
|
|
2041
|
+
if (!nextProp || nextProp.kind !== "identifier") {
|
|
2042
|
+
break;
|
|
2043
|
+
}
|
|
2044
|
+
tokens.advance();
|
|
2045
|
+
chain += `.${nextProp.value}`;
|
|
2046
|
+
depth++;
|
|
2047
|
+
}
|
|
2048
|
+
const openParen = tokens.peek();
|
|
2049
|
+
if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
|
|
2050
|
+
tokens.advance();
|
|
2051
|
+
const args = [];
|
|
2052
|
+
let argDepth = 0;
|
|
2053
|
+
while (!tokens.isAtEnd() && args.length < MAX_METHOD_ARGS) {
|
|
2054
|
+
const argToken = tokens.peek();
|
|
2055
|
+
if (!argToken) break;
|
|
2056
|
+
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
2057
|
+
if (argDepth === 0) {
|
|
2058
|
+
tokens.advance();
|
|
2059
|
+
break;
|
|
2060
|
+
}
|
|
2061
|
+
argDepth--;
|
|
2062
|
+
}
|
|
2063
|
+
if (argToken.kind === "punctuation" && argToken.value === "(") {
|
|
2064
|
+
argDepth++;
|
|
2065
|
+
}
|
|
2066
|
+
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
2067
|
+
tokens.advance();
|
|
2068
|
+
continue;
|
|
2069
|
+
}
|
|
2070
|
+
args.push(argToken.value);
|
|
2071
|
+
tokens.advance();
|
|
2072
|
+
}
|
|
2073
|
+
const methodCall = `${chain}(${args.join(", ")})`;
|
|
2074
|
+
return {
|
|
2075
|
+
type: "expression",
|
|
2076
|
+
raw: methodCall
|
|
2077
|
+
};
|
|
2078
|
+
}
|
|
2079
|
+
return {
|
|
2080
|
+
type: "expression",
|
|
2081
|
+
raw: chain
|
|
2082
|
+
};
|
|
2083
|
+
}
|
|
2084
|
+
function tryMatchPossessiveSelectorExpression(tokens) {
|
|
2085
|
+
const token = tokens.peek();
|
|
2086
|
+
if (!token || token.kind !== "selector") return null;
|
|
2087
|
+
const mark = tokens.mark();
|
|
2088
|
+
tokens.advance();
|
|
2089
|
+
const possessiveToken = tokens.peek();
|
|
2090
|
+
if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
|
|
2091
|
+
tokens.reset(mark);
|
|
2092
|
+
return null;
|
|
2093
|
+
}
|
|
2094
|
+
tokens.advance();
|
|
2095
|
+
const propertyToken = tokens.peek();
|
|
2096
|
+
if (!propertyToken) {
|
|
2097
|
+
tokens.reset(mark);
|
|
2098
|
+
return null;
|
|
2099
|
+
}
|
|
2100
|
+
if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
|
|
2101
|
+
tokens.reset(mark);
|
|
2102
|
+
return null;
|
|
2103
|
+
}
|
|
2104
|
+
tokens.advance();
|
|
2105
|
+
return createPropertyPath(createSelector(token.value), propertyToken.value);
|
|
2106
|
+
}
|
|
2107
|
+
function tryMatchSelectorPropertyExpression(tokens) {
|
|
2108
|
+
const token = tokens.peek();
|
|
2109
|
+
if (!token || token.kind !== "selector") return null;
|
|
2110
|
+
if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
|
|
2111
|
+
const mark = tokens.mark();
|
|
2112
|
+
tokens.advance();
|
|
2113
|
+
const propertyToken = tokens.peek();
|
|
2114
|
+
if (!propertyToken || propertyToken.kind !== "selector") {
|
|
2115
|
+
tokens.reset(mark);
|
|
2116
|
+
return null;
|
|
2117
|
+
}
|
|
2118
|
+
if (!propertyToken.value.startsWith(".")) {
|
|
2119
|
+
tokens.reset(mark);
|
|
2120
|
+
return null;
|
|
2121
|
+
}
|
|
2122
|
+
const peek2 = tokens.peek(1);
|
|
2123
|
+
if (peek2 && peek2.kind === "selector") {
|
|
2124
|
+
}
|
|
2125
|
+
tokens.advance();
|
|
2126
|
+
const propertyName = propertyToken.value.slice(1);
|
|
2127
|
+
return createPropertyPath(createSelector(token.value), propertyName);
|
|
2128
|
+
}
|
|
2129
|
+
var MAX_PROPERTY_DEPTH = 10;
|
|
2130
|
+
var MAX_METHOD_ARGS = 20;
|
|
2131
|
+
function tokenToSemanticValue(token) {
|
|
2132
|
+
switch (token.kind) {
|
|
2133
|
+
case "selector":
|
|
2134
|
+
return createSelector(token.value);
|
|
2135
|
+
case "literal":
|
|
2136
|
+
return parseLiteralValue(token.value);
|
|
2137
|
+
case "keyword": {
|
|
2138
|
+
const tokenValue = token.normalized || token.value;
|
|
2139
|
+
const lower = safeToLowerCase(tokenValue);
|
|
2140
|
+
if (isValidReference(lower)) {
|
|
2141
|
+
return createReference(lower);
|
|
2142
|
+
}
|
|
2143
|
+
return createLiteral(token.normalized || token.value);
|
|
2144
|
+
}
|
|
2145
|
+
case "identifier": {
|
|
2146
|
+
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
2147
|
+
return createReference(token.value);
|
|
2148
|
+
}
|
|
2149
|
+
const identLower = safeToLowerCase(token.value);
|
|
2150
|
+
if (isValidReference(identLower)) {
|
|
2151
|
+
return createReference(identLower);
|
|
2152
|
+
}
|
|
2153
|
+
return { type: "expression", raw: token.value };
|
|
2154
|
+
}
|
|
2155
|
+
case "url":
|
|
2156
|
+
return createLiteral(token.value, "string");
|
|
2157
|
+
default:
|
|
2158
|
+
return null;
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
function parseLiteralValue(value) {
|
|
2162
|
+
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
2163
|
+
const inner = value.slice(1, -1);
|
|
2164
|
+
return createLiteral(inner, "string");
|
|
2165
|
+
}
|
|
2166
|
+
if (value === "true") return createLiteral(true, "boolean");
|
|
2167
|
+
if (value === "false") return createLiteral(false, "boolean");
|
|
2168
|
+
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
2169
|
+
if (durationMatch) {
|
|
2170
|
+
const num2 = parseFloat(durationMatch[1]);
|
|
2171
|
+
const unit = durationMatch[2];
|
|
2172
|
+
if (unit) {
|
|
2173
|
+
return createLiteral(value, "duration");
|
|
2174
|
+
}
|
|
2175
|
+
return createLiteral(num2, "number");
|
|
2176
|
+
}
|
|
2177
|
+
const num = parseFloat(value);
|
|
2178
|
+
if (!isNaN(num)) {
|
|
2179
|
+
return createLiteral(num, "number");
|
|
2180
|
+
}
|
|
2181
|
+
return createLiteral(value, "string");
|
|
2182
|
+
}
|
|
2183
|
+
var ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
2184
|
+
function skipNoiseWords(tokens) {
|
|
2185
|
+
const token = tokens.peek();
|
|
2186
|
+
if (!token) return;
|
|
2187
|
+
const tokenLower = safeToLowerCase(token.value);
|
|
2188
|
+
if (ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
2189
|
+
const mark = tokens.mark();
|
|
2190
|
+
tokens.advance();
|
|
2191
|
+
const nextToken = tokens.peek();
|
|
2192
|
+
if (nextToken && nextToken.kind === "selector") {
|
|
2193
|
+
return;
|
|
2194
|
+
}
|
|
2195
|
+
tokens.reset(mark);
|
|
2196
|
+
}
|
|
2197
|
+
if (tokenLower === "class") {
|
|
2198
|
+
tokens.advance();
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
// src/core/matching/stock-confidence.ts
|
|
2203
|
+
function scoreParts(ctx) {
|
|
2204
|
+
const { pattern, captured } = ctx;
|
|
2205
|
+
let score = 0;
|
|
2206
|
+
let maxScore = 0;
|
|
2207
|
+
const hasDefault = (role) => {
|
|
2208
|
+
return pattern.extraction?.[role]?.default !== void 0;
|
|
2209
|
+
};
|
|
2210
|
+
for (const token of pattern.template.tokens) {
|
|
2211
|
+
if (token.type === "role") {
|
|
2212
|
+
maxScore += 1;
|
|
2213
|
+
if (captured.has(token.role)) {
|
|
2214
|
+
score += 1;
|
|
2215
|
+
}
|
|
2216
|
+
} else if (token.type === "group") {
|
|
2217
|
+
for (const subToken of token.tokens) {
|
|
2218
|
+
if (subToken.type === "role") {
|
|
2219
|
+
const roleHasDefault = hasDefault(subToken.role);
|
|
2220
|
+
const weight = 0.8;
|
|
2221
|
+
maxScore += weight;
|
|
2222
|
+
if (captured.has(subToken.role)) {
|
|
2223
|
+
score += weight;
|
|
2224
|
+
} else if (roleHasDefault) {
|
|
2225
|
+
score += weight * 0.6;
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
}
|
|
2230
|
+
}
|
|
2231
|
+
const roleCoverage = maxScore > 0 ? score / maxScore : 1;
|
|
2232
|
+
let baseConfidence = roleCoverage;
|
|
2233
|
+
let stemPenalty = 0;
|
|
2234
|
+
if (ctx.stemMatchCount > 0 && ctx.totalKeywordMatches > 0) {
|
|
2235
|
+
stemPenalty = ctx.stemMatchCount / ctx.totalKeywordMatches * 0.15;
|
|
2236
|
+
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
2237
|
+
}
|
|
2238
|
+
const vsoBoost = calculateVSOConfidenceBoost(pattern);
|
|
2239
|
+
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
2240
|
+
const prepositionAdjustment = arabicPrepositionDisambiguation(pattern, captured);
|
|
2241
|
+
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
2242
|
+
return {
|
|
2243
|
+
roleCoverage,
|
|
2244
|
+
stemPenalty,
|
|
2245
|
+
languageBoost: vsoBoost,
|
|
2246
|
+
languageAdjustment: prepositionAdjustment,
|
|
2247
|
+
final: baseConfidence
|
|
2248
|
+
};
|
|
2249
|
+
}
|
|
2250
|
+
function calculateVSOConfidenceBoost(pattern) {
|
|
2251
|
+
if (pattern.language !== "ar") {
|
|
2252
|
+
return 0;
|
|
2253
|
+
}
|
|
2254
|
+
const firstToken = pattern.template.tokens[0];
|
|
2255
|
+
if (!firstToken || firstToken.type !== "literal") {
|
|
2256
|
+
return 0;
|
|
2257
|
+
}
|
|
2258
|
+
const ARABIC_VERBS = /* @__PURE__ */ new Set([
|
|
2259
|
+
"\u0628\u062F\u0644",
|
|
2260
|
+
"\u063A\u064A\u0631",
|
|
2261
|
+
"\u0623\u0636\u0641",
|
|
2262
|
+
"\u0623\u0632\u0644",
|
|
2263
|
+
"\u0636\u0639",
|
|
2264
|
+
"\u0627\u062C\u0639\u0644",
|
|
2265
|
+
"\u0639\u064A\u0646",
|
|
2266
|
+
"\u0632\u062F",
|
|
2267
|
+
"\u0627\u0646\u0642\u0635",
|
|
2268
|
+
"\u0633\u062C\u0644",
|
|
2269
|
+
"\u0623\u0638\u0647\u0631",
|
|
2270
|
+
"\u0623\u062E\u0641",
|
|
2271
|
+
"\u0634\u063A\u0644",
|
|
2272
|
+
"\u0623\u0631\u0633\u0644",
|
|
2273
|
+
"\u0631\u0643\u0632",
|
|
2274
|
+
"\u0634\u0648\u0634",
|
|
2275
|
+
"\u062A\u0648\u0642\u0641",
|
|
2276
|
+
"\u0627\u0646\u0633\u062E",
|
|
2277
|
+
"\u0627\u062D\u0630\u0641",
|
|
2278
|
+
"\u0627\u0635\u0646\u0639",
|
|
2279
|
+
"\u0627\u0646\u062A\u0638\u0631",
|
|
2280
|
+
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
2281
|
+
"\u0623\u0648"
|
|
2282
|
+
]);
|
|
2283
|
+
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
2284
|
+
return 0.15;
|
|
2285
|
+
}
|
|
2286
|
+
if (firstToken.alternatives) {
|
|
2287
|
+
for (const alt of firstToken.alternatives) {
|
|
2288
|
+
if (ARABIC_VERBS.has(alt)) {
|
|
2289
|
+
return 0.15;
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
return 0;
|
|
2294
|
+
}
|
|
2295
|
+
function arabicPrepositionDisambiguation(pattern, captured) {
|
|
2296
|
+
if (pattern.language !== "ar") {
|
|
2297
|
+
return 0;
|
|
2298
|
+
}
|
|
2299
|
+
let adjustment = 0;
|
|
2300
|
+
const PREFERRED_PREPOSITIONS = {
|
|
2301
|
+
patient: ["\u0639\u0644\u0649"],
|
|
2302
|
+
// element selectors prefer على (on/upon)
|
|
2303
|
+
destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2304
|
+
// destination prefers إلى (to)
|
|
2305
|
+
source: ["\u0645\u0646"],
|
|
2306
|
+
// source prefers من (from)
|
|
2307
|
+
agent: ["\u0645\u0646"],
|
|
2308
|
+
// agent/by prefers من (from/by)
|
|
2309
|
+
manner: ["\u0628"],
|
|
2310
|
+
// manner prefers ب (with/by)
|
|
2311
|
+
style: ["\u0628"],
|
|
2312
|
+
// style prefers ب (with)
|
|
2313
|
+
goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2314
|
+
// target state prefers إلى (to)
|
|
2315
|
+
method: ["\u0628"]
|
|
2316
|
+
// method prefers ب (with/by)
|
|
2317
|
+
};
|
|
2318
|
+
for (const [role, value] of captured.entries()) {
|
|
2319
|
+
const preferred = PREFERRED_PREPOSITIONS[role];
|
|
2320
|
+
if (!preferred || preferred.length === 0) {
|
|
2321
|
+
continue;
|
|
2322
|
+
}
|
|
2323
|
+
const metadata = "metadata" in value ? value.metadata : void 0;
|
|
2324
|
+
if (metadata && typeof metadata.prepositionValue === "string") {
|
|
2325
|
+
const usedPreposition = metadata.prepositionValue;
|
|
2326
|
+
if (preferred.includes(usedPreposition)) {
|
|
2327
|
+
adjustment += 0.1;
|
|
2328
|
+
} else {
|
|
2329
|
+
adjustment -= 0.1;
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
return Math.max(-0.1, Math.min(0.1, adjustment));
|
|
2334
|
+
}
|
|
2335
|
+
var stockConfidenceModel = {
|
|
2336
|
+
calculate: (ctx) => scoreParts(ctx).final,
|
|
2337
|
+
calculateWithBreakdown: (ctx) => scoreParts(ctx)
|
|
2338
|
+
};
|
|
2339
|
+
|
|
2340
|
+
// src/core/pattern-matching/pattern-matcher.ts
|
|
1816
2341
|
var _PatternMatcher = class _PatternMatcher {
|
|
1817
2342
|
constructor() {
|
|
1818
2343
|
/** Debug logger */
|
|
@@ -1824,19 +2349,6 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
1824
2349
|
this.stemMatchCount = 0;
|
|
1825
2350
|
this.totalKeywordMatches = 0;
|
|
1826
2351
|
}
|
|
1827
|
-
/**
|
|
1828
|
-
* Safely convert a value to lowercase string.
|
|
1829
|
-
* Provides protection against non-string values at runtime.
|
|
1830
|
-
*/
|
|
1831
|
-
safeToLowerCase(value) {
|
|
1832
|
-
if (typeof value === "string") {
|
|
1833
|
-
return value.toLowerCase();
|
|
1834
|
-
}
|
|
1835
|
-
if (value === null || value === void 0) {
|
|
1836
|
-
return "";
|
|
1837
|
-
}
|
|
1838
|
-
return String(value).toLowerCase();
|
|
1839
|
-
}
|
|
1840
2352
|
/**
|
|
1841
2353
|
* Try to match a single pattern against the token stream.
|
|
1842
2354
|
* Returns the match result or null if no match.
|
|
@@ -1885,7 +2397,12 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
1885
2397
|
tokens.reset(mark);
|
|
1886
2398
|
return null;
|
|
1887
2399
|
}
|
|
1888
|
-
const confidence =
|
|
2400
|
+
const confidence = stockConfidenceModel.calculate({
|
|
2401
|
+
pattern,
|
|
2402
|
+
captured,
|
|
2403
|
+
stemMatchCount: this.stemMatchCount,
|
|
2404
|
+
totalKeywordMatches: this.totalKeywordMatches
|
|
2405
|
+
});
|
|
1889
2406
|
this.applyExtractionRules(pattern, captured);
|
|
1890
2407
|
return {
|
|
1891
2408
|
pattern,
|
|
@@ -2070,7 +2587,7 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
2070
2587
|
case "literal":
|
|
2071
2588
|
return this.matchLiteralToken(tokens, patternToken);
|
|
2072
2589
|
case "role":
|
|
2073
|
-
return
|
|
2590
|
+
return readRoleValue(tokens, patternToken, captured, this.currentProfile);
|
|
2074
2591
|
case "group":
|
|
2075
2592
|
return this.matchGroupToken(tokens, patternToken, captured);
|
|
2076
2593
|
default:
|
|
@@ -2128,420 +2645,54 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
2128
2645
|
return false;
|
|
2129
2646
|
}
|
|
2130
2647
|
/**
|
|
2131
|
-
* Match a
|
|
2132
|
-
*
|
|
2133
|
-
*
|
|
2134
|
-
*
|
|
2135
|
-
*
|
|
2648
|
+
* Match a group pattern token (optional sequence).
|
|
2649
|
+
* When the group's leading marker isn't at the current position, scans ahead
|
|
2650
|
+
* up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
|
|
2651
|
+
* (e.g., a value like 'hello') to sit between groups without blocking later
|
|
2652
|
+
* marker matches.
|
|
2136
2653
|
*/
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
token ? JSON.stringify({ value: token.value, kind: token.kind }) : "null"
|
|
2654
|
+
matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
|
|
2655
|
+
const mark = tokens.mark();
|
|
2656
|
+
const capturedBefore = new Set(captured.keys());
|
|
2657
|
+
const success = this.matchTokenSequence(
|
|
2658
|
+
tokens,
|
|
2659
|
+
patternToken.tokens,
|
|
2660
|
+
captured,
|
|
2661
|
+
parentStopMarkers
|
|
2146
2662
|
);
|
|
2147
|
-
if (
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
if (possessiveValue) {
|
|
2152
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
2153
|
-
if (!isTypeCompatible(possessiveValue.type, patternToken.expectedTypes)) {
|
|
2154
|
-
return patternToken.optional || false;
|
|
2155
|
-
}
|
|
2156
|
-
}
|
|
2157
|
-
captured.set(patternToken.role, possessiveValue);
|
|
2158
|
-
return true;
|
|
2159
|
-
}
|
|
2160
|
-
const methodCallValue = this.tryMatchMethodCallExpression(tokens);
|
|
2161
|
-
if (methodCallValue) {
|
|
2162
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
2163
|
-
if (!isTypeCompatible(methodCallValue.type, patternToken.expectedTypes)) {
|
|
2164
|
-
return patternToken.optional || false;
|
|
2165
|
-
}
|
|
2166
|
-
}
|
|
2167
|
-
captured.set(patternToken.role, methodCallValue);
|
|
2168
|
-
return true;
|
|
2169
|
-
}
|
|
2170
|
-
const possessiveSelectorValue = this.tryMatchPossessiveSelectorExpression(tokens);
|
|
2171
|
-
if (possessiveSelectorValue) {
|
|
2172
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
2173
|
-
if (!isTypeCompatible(possessiveSelectorValue.type, patternToken.expectedTypes)) {
|
|
2174
|
-
return patternToken.optional || false;
|
|
2175
|
-
}
|
|
2176
|
-
}
|
|
2177
|
-
captured.set(patternToken.role, possessiveSelectorValue);
|
|
2178
|
-
return true;
|
|
2179
|
-
}
|
|
2180
|
-
const propertyAccessValue = this.tryMatchPropertyAccessExpression(tokens);
|
|
2181
|
-
if (propertyAccessValue) {
|
|
2182
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
2183
|
-
if (!isTypeCompatible(propertyAccessValue.type, patternToken.expectedTypes)) {
|
|
2184
|
-
return patternToken.optional || false;
|
|
2185
|
-
}
|
|
2186
|
-
}
|
|
2187
|
-
captured.set(patternToken.role, propertyAccessValue);
|
|
2188
|
-
return true;
|
|
2663
|
+
if (success) return true;
|
|
2664
|
+
tokens.reset(mark);
|
|
2665
|
+
for (const role of captured.keys()) {
|
|
2666
|
+
if (!capturedBefore.has(role)) captured.delete(role);
|
|
2189
2667
|
}
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
if (patternToken.expectedTypes && patternToken.expectedTypes.length > 0) {
|
|
2219
|
-
if (!isTypeCompatible(value.type, patternToken.expectedTypes)) {
|
|
2220
|
-
this.logger.debug(" >>> TYPE MISMATCH - returning", patternToken.optional || false);
|
|
2221
|
-
return patternToken.optional || false;
|
|
2222
|
-
}
|
|
2223
|
-
}
|
|
2224
|
-
this.logger.debug(" >>> Type validation PASSED");
|
|
2225
|
-
captured.set(patternToken.role, value);
|
|
2226
|
-
tokens.advance();
|
|
2227
|
-
return true;
|
|
2228
|
-
}
|
|
2229
|
-
/**
|
|
2230
|
-
* Try to match a possessive expression like 'my value' or 'its innerHTML'.
|
|
2231
|
-
* Returns the PropertyPathValue if matched, or null if not.
|
|
2232
|
-
*/
|
|
2233
|
-
tryMatchPossessiveExpression(tokens) {
|
|
2234
|
-
const token = tokens.peek();
|
|
2235
|
-
if (!token) return null;
|
|
2236
|
-
if (!this.currentProfile) return null;
|
|
2237
|
-
const tokenValue = token.normalized || token.value;
|
|
2238
|
-
const tokenLower = this.safeToLowerCase(tokenValue);
|
|
2239
|
-
const baseRef = getPossessiveReference(this.currentProfile, tokenLower);
|
|
2240
|
-
if (!baseRef) return null;
|
|
2241
|
-
const mark = tokens.mark();
|
|
2242
|
-
tokens.advance();
|
|
2243
|
-
const propertyToken = tokens.peek();
|
|
2244
|
-
if (!propertyToken) {
|
|
2245
|
-
tokens.reset(mark);
|
|
2246
|
-
return null;
|
|
2247
|
-
}
|
|
2248
|
-
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)) {
|
|
2249
|
-
tokens.advance();
|
|
2250
|
-
let propertyName = propertyToken.value;
|
|
2251
|
-
if (propertyToken.kind === "selector" && propertyName.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(propertyName)) {
|
|
2252
|
-
propertyName = propertyName.substring(1);
|
|
2253
|
-
}
|
|
2254
|
-
let chainedProps = propertyName;
|
|
2255
|
-
while (tokens.peek()?.kind === "selector" && tokens.peek().value.startsWith(".") && /^\.[a-zA-Z_]\w*/.test(tokens.peek().value)) {
|
|
2256
|
-
chainedProps += tokens.peek().value;
|
|
2257
|
-
tokens.advance();
|
|
2258
|
-
}
|
|
2259
|
-
const nextPeek = tokens.peek();
|
|
2260
|
-
if (nextPeek?.kind === "literal" && nextPeek.value.startsWith("(")) {
|
|
2261
|
-
chainedProps += nextPeek.value;
|
|
2262
|
-
tokens.advance();
|
|
2263
|
-
}
|
|
2264
|
-
return createPropertyPath(createReference(baseRef), chainedProps);
|
|
2265
|
-
}
|
|
2266
|
-
tokens.reset(mark);
|
|
2267
|
-
return null;
|
|
2268
|
-
}
|
|
2269
|
-
/**
|
|
2270
|
-
* Check if a keyword is a structural keyword (preposition, control flow, etc.)
|
|
2271
|
-
* that shouldn't be consumed as a property name.
|
|
2272
|
-
*/
|
|
2273
|
-
isStructuralKeyword(value) {
|
|
2274
|
-
const structural = /* @__PURE__ */ new Set([
|
|
2275
|
-
// Prepositions
|
|
2276
|
-
"into",
|
|
2277
|
-
"in",
|
|
2278
|
-
"to",
|
|
2279
|
-
"from",
|
|
2280
|
-
"at",
|
|
2281
|
-
"by",
|
|
2282
|
-
"with",
|
|
2283
|
-
"without",
|
|
2284
|
-
"before",
|
|
2285
|
-
"after",
|
|
2286
|
-
"of",
|
|
2287
|
-
"as",
|
|
2288
|
-
"on",
|
|
2289
|
-
// Control flow
|
|
2290
|
-
"then",
|
|
2291
|
-
"end",
|
|
2292
|
-
"else",
|
|
2293
|
-
"if",
|
|
2294
|
-
"repeat",
|
|
2295
|
-
"while",
|
|
2296
|
-
"for",
|
|
2297
|
-
// Commands (shouldn't be property names)
|
|
2298
|
-
"toggle",
|
|
2299
|
-
"add",
|
|
2300
|
-
"remove",
|
|
2301
|
-
"put",
|
|
2302
|
-
"set",
|
|
2303
|
-
"show",
|
|
2304
|
-
"hide",
|
|
2305
|
-
"increment",
|
|
2306
|
-
"decrement",
|
|
2307
|
-
"send",
|
|
2308
|
-
"trigger",
|
|
2309
|
-
"call"
|
|
2310
|
-
]);
|
|
2311
|
-
return structural.has(value.toLowerCase());
|
|
2312
|
-
}
|
|
2313
|
-
/**
|
|
2314
|
-
* Try to match a method call expression like '#dialog.showModal()'.
|
|
2315
|
-
* Pattern: selector + '.' + identifier + '(' + [args] + ')'
|
|
2316
|
-
* Returns an expression value if matched, or null if not.
|
|
2317
|
-
*/
|
|
2318
|
-
tryMatchMethodCallExpression(tokens) {
|
|
2319
|
-
const token = tokens.peek();
|
|
2320
|
-
if (!token || token.kind !== "selector") return null;
|
|
2321
|
-
const mark = tokens.mark();
|
|
2322
|
-
tokens.advance();
|
|
2323
|
-
const dotToken = tokens.peek();
|
|
2324
|
-
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
2325
|
-
tokens.reset(mark);
|
|
2326
|
-
return null;
|
|
2327
|
-
}
|
|
2328
|
-
tokens.advance();
|
|
2329
|
-
const methodToken = tokens.peek();
|
|
2330
|
-
if (!methodToken || methodToken.kind !== "identifier") {
|
|
2331
|
-
tokens.reset(mark);
|
|
2332
|
-
return null;
|
|
2333
|
-
}
|
|
2334
|
-
tokens.advance();
|
|
2335
|
-
const openParen = tokens.peek();
|
|
2336
|
-
if (!openParen || openParen.kind !== "punctuation" || openParen.value !== "(") {
|
|
2337
|
-
tokens.reset(mark);
|
|
2338
|
-
return null;
|
|
2339
|
-
}
|
|
2340
|
-
tokens.advance();
|
|
2341
|
-
const args = [];
|
|
2342
|
-
while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
|
|
2343
|
-
const argToken = tokens.peek();
|
|
2344
|
-
if (!argToken) break;
|
|
2345
|
-
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
2346
|
-
tokens.advance();
|
|
2347
|
-
break;
|
|
2348
|
-
}
|
|
2349
|
-
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
2350
|
-
tokens.advance();
|
|
2351
|
-
continue;
|
|
2352
|
-
}
|
|
2353
|
-
args.push(argToken.value);
|
|
2354
|
-
tokens.advance();
|
|
2355
|
-
}
|
|
2356
|
-
const methodCall = `${token.value}.${methodToken.value}(${args.join(", ")})`;
|
|
2357
|
-
return {
|
|
2358
|
-
type: "expression",
|
|
2359
|
-
raw: methodCall
|
|
2360
|
-
};
|
|
2361
|
-
}
|
|
2362
|
-
/**
|
|
2363
|
-
* Try to match a property access expression like 'userData.name' or 'it.data'.
|
|
2364
|
-
* Pattern: (identifier | keyword) + '.' + identifier [+ '.' + identifier ...]
|
|
2365
|
-
* Returns an expression value if matched, or null if not.
|
|
2366
|
-
*/
|
|
2367
|
-
tryMatchPropertyAccessExpression(tokens) {
|
|
2368
|
-
const token = tokens.peek();
|
|
2369
|
-
if (!token) return null;
|
|
2370
|
-
if (token.kind !== "identifier" && token.kind !== "keyword") return null;
|
|
2371
|
-
const mark = tokens.mark();
|
|
2372
|
-
tokens.advance();
|
|
2373
|
-
const dotToken = tokens.peek();
|
|
2374
|
-
if (!dotToken || dotToken.kind !== "operator" || dotToken.value !== ".") {
|
|
2375
|
-
tokens.reset(mark);
|
|
2376
|
-
return null;
|
|
2377
|
-
}
|
|
2378
|
-
tokens.advance();
|
|
2379
|
-
const propertyToken = tokens.peek();
|
|
2380
|
-
if (!propertyToken || propertyToken.kind !== "identifier") {
|
|
2381
|
-
tokens.reset(mark);
|
|
2382
|
-
return null;
|
|
2383
|
-
}
|
|
2384
|
-
tokens.advance();
|
|
2385
|
-
let chain = `${token.value}.${propertyToken.value}`;
|
|
2386
|
-
let depth = 1;
|
|
2387
|
-
while (!tokens.isAtEnd() && depth < _PatternMatcher.MAX_PROPERTY_DEPTH) {
|
|
2388
|
-
const nextDot = tokens.peek();
|
|
2389
|
-
if (!nextDot || nextDot.kind !== "operator" || nextDot.value !== ".") {
|
|
2390
|
-
break;
|
|
2391
|
-
}
|
|
2392
|
-
tokens.advance();
|
|
2393
|
-
const nextProp = tokens.peek();
|
|
2394
|
-
if (!nextProp || nextProp.kind !== "identifier") {
|
|
2395
|
-
break;
|
|
2396
|
-
}
|
|
2397
|
-
tokens.advance();
|
|
2398
|
-
chain += `.${nextProp.value}`;
|
|
2399
|
-
depth++;
|
|
2400
|
-
}
|
|
2401
|
-
const openParen = tokens.peek();
|
|
2402
|
-
if (openParen && openParen.kind === "punctuation" && openParen.value === "(") {
|
|
2403
|
-
tokens.advance();
|
|
2404
|
-
const args = [];
|
|
2405
|
-
let argDepth = 0;
|
|
2406
|
-
while (!tokens.isAtEnd() && args.length < _PatternMatcher.MAX_METHOD_ARGS) {
|
|
2407
|
-
const argToken = tokens.peek();
|
|
2408
|
-
if (!argToken) break;
|
|
2409
|
-
if (argToken.kind === "punctuation" && argToken.value === ")") {
|
|
2410
|
-
if (argDepth === 0) {
|
|
2411
|
-
tokens.advance();
|
|
2412
|
-
break;
|
|
2413
|
-
}
|
|
2414
|
-
argDepth--;
|
|
2415
|
-
}
|
|
2416
|
-
if (argToken.kind === "punctuation" && argToken.value === "(") {
|
|
2417
|
-
argDepth++;
|
|
2418
|
-
}
|
|
2419
|
-
if (argToken.kind === "punctuation" && argToken.value === ",") {
|
|
2420
|
-
tokens.advance();
|
|
2421
|
-
continue;
|
|
2422
|
-
}
|
|
2423
|
-
args.push(argToken.value);
|
|
2424
|
-
tokens.advance();
|
|
2425
|
-
}
|
|
2426
|
-
const methodCall = `${chain}(${args.join(", ")})`;
|
|
2427
|
-
return {
|
|
2428
|
-
type: "expression",
|
|
2429
|
-
raw: methodCall
|
|
2430
|
-
};
|
|
2431
|
-
}
|
|
2432
|
-
return {
|
|
2433
|
-
type: "expression",
|
|
2434
|
-
raw: chain
|
|
2435
|
-
};
|
|
2436
|
-
}
|
|
2437
|
-
/**
|
|
2438
|
-
* Try to match a possessive selector expression like "#element's *opacity".
|
|
2439
|
-
* Pattern: selector + "'s" + (selector | identifier)
|
|
2440
|
-
* Returns a property-path value if matched, or null if not.
|
|
2441
|
-
*/
|
|
2442
|
-
tryMatchPossessiveSelectorExpression(tokens) {
|
|
2443
|
-
const token = tokens.peek();
|
|
2444
|
-
if (!token || token.kind !== "selector") return null;
|
|
2445
|
-
const mark = tokens.mark();
|
|
2446
|
-
tokens.advance();
|
|
2447
|
-
const possessiveToken = tokens.peek();
|
|
2448
|
-
if (!possessiveToken || possessiveToken.kind !== "punctuation" || possessiveToken.value !== "'s") {
|
|
2449
|
-
tokens.reset(mark);
|
|
2450
|
-
return null;
|
|
2451
|
-
}
|
|
2452
|
-
tokens.advance();
|
|
2453
|
-
const propertyToken = tokens.peek();
|
|
2454
|
-
if (!propertyToken) {
|
|
2455
|
-
tokens.reset(mark);
|
|
2456
|
-
return null;
|
|
2457
|
-
}
|
|
2458
|
-
if (propertyToken.kind !== "selector" && propertyToken.kind !== "identifier") {
|
|
2459
|
-
tokens.reset(mark);
|
|
2460
|
-
return null;
|
|
2461
|
-
}
|
|
2462
|
-
tokens.advance();
|
|
2463
|
-
return createPropertyPath(createSelector(token.value), propertyToken.value);
|
|
2464
|
-
}
|
|
2465
|
-
/**
|
|
2466
|
-
* Try to match a selector + property expression like "#output.innerText".
|
|
2467
|
-
* This handles cases where the tokenizer produces two selector tokens:
|
|
2468
|
-
* - #output (id selector)
|
|
2469
|
-
* - .innerText (looks like class selector, but is actually property)
|
|
2470
|
-
*
|
|
2471
|
-
* Pattern: id-selector + class-selector-that-is-actually-property
|
|
2472
|
-
* Returns a property-path value if matched, or null if not.
|
|
2473
|
-
*/
|
|
2474
|
-
tryMatchSelectorPropertyExpression(tokens) {
|
|
2475
|
-
const token = tokens.peek();
|
|
2476
|
-
if (!token || token.kind !== "selector") return null;
|
|
2477
|
-
if (typeof token.value !== "string" || !token.value.startsWith("#")) return null;
|
|
2478
|
-
const mark = tokens.mark();
|
|
2479
|
-
tokens.advance();
|
|
2480
|
-
const propertyToken = tokens.peek();
|
|
2481
|
-
if (!propertyToken || propertyToken.kind !== "selector") {
|
|
2482
|
-
tokens.reset(mark);
|
|
2483
|
-
return null;
|
|
2484
|
-
}
|
|
2485
|
-
if (!propertyToken.value.startsWith(".")) {
|
|
2486
|
-
tokens.reset(mark);
|
|
2487
|
-
return null;
|
|
2488
|
-
}
|
|
2489
|
-
const peek2 = tokens.peek(1);
|
|
2490
|
-
if (peek2 && peek2.kind === "selector") {
|
|
2491
|
-
}
|
|
2492
|
-
tokens.advance();
|
|
2493
|
-
const propertyName = propertyToken.value.slice(1);
|
|
2494
|
-
return createPropertyPath(createSelector(token.value), propertyName);
|
|
2495
|
-
}
|
|
2496
|
-
/**
|
|
2497
|
-
* Match a group pattern token (optional sequence).
|
|
2498
|
-
* When the group's leading marker isn't at the current position, scans ahead
|
|
2499
|
-
* up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
|
|
2500
|
-
* (e.g., a value like 'hello') to sit between groups without blocking later
|
|
2501
|
-
* marker matches.
|
|
2502
|
-
*/
|
|
2503
|
-
matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
|
|
2504
|
-
const mark = tokens.mark();
|
|
2505
|
-
const capturedBefore = new Set(captured.keys());
|
|
2506
|
-
const success = this.matchTokenSequence(
|
|
2507
|
-
tokens,
|
|
2508
|
-
patternToken.tokens,
|
|
2509
|
-
captured,
|
|
2510
|
-
parentStopMarkers
|
|
2511
|
-
);
|
|
2512
|
-
if (success) return true;
|
|
2513
|
-
tokens.reset(mark);
|
|
2514
|
-
for (const role of captured.keys()) {
|
|
2515
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
2516
|
-
}
|
|
2517
|
-
if (!patternToken.optional) return false;
|
|
2518
|
-
const leadingMarker = this.getGroupLeadingMarker(patternToken);
|
|
2519
|
-
if (leadingMarker) {
|
|
2520
|
-
for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
|
|
2521
|
-
const ahead = tokens.peek(offset);
|
|
2522
|
-
if (!ahead) break;
|
|
2523
|
-
const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
|
|
2524
|
-
if (aheadValue === leadingMarker) {
|
|
2525
|
-
this.logger.debug(
|
|
2526
|
-
" >> MARKER SCAN: found",
|
|
2527
|
-
leadingMarker,
|
|
2528
|
-
"at offset",
|
|
2529
|
-
offset,
|
|
2530
|
-
"- skipping intervening tokens"
|
|
2531
|
-
);
|
|
2532
|
-
for (let s = 0; s < offset; s++) tokens.advance();
|
|
2533
|
-
const retrySuccess = this.matchTokenSequence(
|
|
2534
|
-
tokens,
|
|
2535
|
-
patternToken.tokens,
|
|
2536
|
-
captured,
|
|
2537
|
-
parentStopMarkers
|
|
2538
|
-
);
|
|
2539
|
-
if (retrySuccess) return true;
|
|
2540
|
-
tokens.reset(mark);
|
|
2541
|
-
for (const role of captured.keys()) {
|
|
2542
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
2543
|
-
}
|
|
2544
|
-
break;
|
|
2668
|
+
if (!patternToken.optional) return false;
|
|
2669
|
+
const leadingMarker = this.getGroupLeadingMarker(patternToken);
|
|
2670
|
+
if (leadingMarker) {
|
|
2671
|
+
for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
|
|
2672
|
+
const ahead = tokens.peek(offset);
|
|
2673
|
+
if (!ahead) break;
|
|
2674
|
+
const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
|
|
2675
|
+
if (aheadValue === leadingMarker) {
|
|
2676
|
+
this.logger.debug(
|
|
2677
|
+
" >> MARKER SCAN: found",
|
|
2678
|
+
leadingMarker,
|
|
2679
|
+
"at offset",
|
|
2680
|
+
offset,
|
|
2681
|
+
"- skipping intervening tokens"
|
|
2682
|
+
);
|
|
2683
|
+
for (let s = 0; s < offset; s++) tokens.advance();
|
|
2684
|
+
const retrySuccess = this.matchTokenSequence(
|
|
2685
|
+
tokens,
|
|
2686
|
+
patternToken.tokens,
|
|
2687
|
+
captured,
|
|
2688
|
+
parentStopMarkers
|
|
2689
|
+
);
|
|
2690
|
+
if (retrySuccess) return true;
|
|
2691
|
+
tokens.reset(mark);
|
|
2692
|
+
for (const role of captured.keys()) {
|
|
2693
|
+
if (!capturedBefore.has(role)) captured.delete(role);
|
|
2694
|
+
}
|
|
2695
|
+
break;
|
|
2545
2696
|
}
|
|
2546
2697
|
}
|
|
2547
2698
|
}
|
|
@@ -2568,7 +2719,7 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
2568
2719
|
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
2569
2720
|
return "stem";
|
|
2570
2721
|
}
|
|
2571
|
-
if (token.kind === "keyword" &&
|
|
2722
|
+
if (token.kind === "keyword" && safeToLowerCase(token.value) === value.toLowerCase()) {
|
|
2572
2723
|
return "case-insensitive";
|
|
2573
2724
|
}
|
|
2574
2725
|
return "none";
|
|
@@ -2616,62 +2767,9 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
2616
2767
|
if (token.normalized && stopMarkers.has(token.normalized.toLowerCase())) return true;
|
|
2617
2768
|
return false;
|
|
2618
2769
|
}
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
tokenToSemanticValue(token) {
|
|
2623
|
-
switch (token.kind) {
|
|
2624
|
-
case "selector":
|
|
2625
|
-
return createSelector(token.value);
|
|
2626
|
-
case "literal":
|
|
2627
|
-
return this.parseLiteralValue(token.value);
|
|
2628
|
-
case "keyword":
|
|
2629
|
-
const tokenValue = token.normalized || token.value;
|
|
2630
|
-
const lower = this.safeToLowerCase(tokenValue);
|
|
2631
|
-
if (isValidReference(lower)) {
|
|
2632
|
-
return createReference(lower);
|
|
2633
|
-
}
|
|
2634
|
-
return createLiteral(token.normalized || token.value);
|
|
2635
|
-
case "identifier":
|
|
2636
|
-
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
2637
|
-
return createReference(token.value);
|
|
2638
|
-
}
|
|
2639
|
-
const identLower = this.safeToLowerCase(token.value);
|
|
2640
|
-
if (isValidReference(identLower)) {
|
|
2641
|
-
return createReference(identLower);
|
|
2642
|
-
}
|
|
2643
|
-
return { type: "expression", raw: token.value };
|
|
2644
|
-
case "url":
|
|
2645
|
-
return createLiteral(token.value, "string");
|
|
2646
|
-
default:
|
|
2647
|
-
return null;
|
|
2648
|
-
}
|
|
2649
|
-
}
|
|
2650
|
-
/**
|
|
2651
|
-
* Parse a literal value (string, number, boolean).
|
|
2652
|
-
*/
|
|
2653
|
-
parseLiteralValue(value) {
|
|
2654
|
-
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
2655
|
-
const inner = value.slice(1, -1);
|
|
2656
|
-
return createLiteral(inner, "string");
|
|
2657
|
-
}
|
|
2658
|
-
if (value === "true") return createLiteral(true, "boolean");
|
|
2659
|
-
if (value === "false") return createLiteral(false, "boolean");
|
|
2660
|
-
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
2661
|
-
if (durationMatch) {
|
|
2662
|
-
const num2 = parseFloat(durationMatch[1]);
|
|
2663
|
-
const unit = durationMatch[2];
|
|
2664
|
-
if (unit) {
|
|
2665
|
-
return createLiteral(value, "duration");
|
|
2666
|
-
}
|
|
2667
|
-
return createLiteral(num2, "number");
|
|
2668
|
-
}
|
|
2669
|
-
const num = parseFloat(value);
|
|
2670
|
-
if (!isNaN(num)) {
|
|
2671
|
-
return createLiteral(num, "number");
|
|
2672
|
-
}
|
|
2673
|
-
return createLiteral(value, "string");
|
|
2674
|
-
}
|
|
2770
|
+
// ==========================================================================
|
|
2771
|
+
// Depth Limits for Expression Parsing (security hardening)
|
|
2772
|
+
// ==========================================================================
|
|
2675
2773
|
/**
|
|
2676
2774
|
* Apply extraction rules to fill in static values and defaults for missing roles.
|
|
2677
2775
|
*/
|
|
@@ -2692,190 +2790,9 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
2692
2790
|
isOptional(patternToken) {
|
|
2693
2791
|
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
2694
2792
|
}
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
* Confidence is reduced for:
|
|
2699
|
-
* - Stem matches (morphological normalization has inherent uncertainty)
|
|
2700
|
-
* - Missing optional roles (but less penalty if role has a default value)
|
|
2701
|
-
*
|
|
2702
|
-
* Confidence is increased for:
|
|
2703
|
-
* - VSO languages (Arabic) when pattern starts with a verb
|
|
2704
|
-
*/
|
|
2705
|
-
calculateConfidence(pattern, captured) {
|
|
2706
|
-
let score = 0;
|
|
2707
|
-
let maxScore = 0;
|
|
2708
|
-
const hasDefault = (role) => {
|
|
2709
|
-
return pattern.extraction?.[role]?.default !== void 0;
|
|
2710
|
-
};
|
|
2711
|
-
for (const token of pattern.template.tokens) {
|
|
2712
|
-
if (token.type === "role") {
|
|
2713
|
-
maxScore += 1;
|
|
2714
|
-
if (captured.has(token.role)) {
|
|
2715
|
-
score += 1;
|
|
2716
|
-
}
|
|
2717
|
-
} else if (token.type === "group") {
|
|
2718
|
-
for (const subToken of token.tokens) {
|
|
2719
|
-
if (subToken.type === "role") {
|
|
2720
|
-
const roleHasDefault = hasDefault(subToken.role);
|
|
2721
|
-
const weight = 0.8;
|
|
2722
|
-
maxScore += weight;
|
|
2723
|
-
if (captured.has(subToken.role)) {
|
|
2724
|
-
score += weight;
|
|
2725
|
-
} else if (roleHasDefault) {
|
|
2726
|
-
score += weight * 0.6;
|
|
2727
|
-
}
|
|
2728
|
-
}
|
|
2729
|
-
}
|
|
2730
|
-
}
|
|
2731
|
-
}
|
|
2732
|
-
let baseConfidence = maxScore > 0 ? score / maxScore : 1;
|
|
2733
|
-
if (this.stemMatchCount > 0 && this.totalKeywordMatches > 0) {
|
|
2734
|
-
const stemPenalty = this.stemMatchCount / this.totalKeywordMatches * 0.15;
|
|
2735
|
-
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
2736
|
-
}
|
|
2737
|
-
const vsoBoost = this.calculateVSOConfidenceBoost(pattern);
|
|
2738
|
-
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
2739
|
-
const prepositionAdjustment = this.arabicPrepositionDisambiguation(pattern, captured);
|
|
2740
|
-
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
2741
|
-
return baseConfidence;
|
|
2742
|
-
}
|
|
2743
|
-
/**
|
|
2744
|
-
* Calculate confidence boost for VSO (Verb-Subject-Object) language patterns.
|
|
2745
|
-
* Arabic naturally uses VSO word order, so patterns that start with a verb
|
|
2746
|
-
* should receive a confidence boost.
|
|
2747
|
-
*
|
|
2748
|
-
* Returns +0.15 confidence boost if:
|
|
2749
|
-
* - Language is Arabic ('ar')
|
|
2750
|
-
* - Pattern's first token is a verb keyword
|
|
2751
|
-
*
|
|
2752
|
-
* @param pattern The language pattern being matched
|
|
2753
|
-
* @returns Confidence boost (0 or 0.15)
|
|
2754
|
-
*/
|
|
2755
|
-
calculateVSOConfidenceBoost(pattern) {
|
|
2756
|
-
if (pattern.language !== "ar") {
|
|
2757
|
-
return 0;
|
|
2758
|
-
}
|
|
2759
|
-
const firstToken = pattern.template.tokens[0];
|
|
2760
|
-
if (!firstToken || firstToken.type !== "literal") {
|
|
2761
|
-
return 0;
|
|
2762
|
-
}
|
|
2763
|
-
const ARABIC_VERBS = /* @__PURE__ */ new Set([
|
|
2764
|
-
"\u0628\u062F\u0644",
|
|
2765
|
-
"\u063A\u064A\u0631",
|
|
2766
|
-
"\u0623\u0636\u0641",
|
|
2767
|
-
"\u0623\u0632\u0644",
|
|
2768
|
-
"\u0636\u0639",
|
|
2769
|
-
"\u0627\u062C\u0639\u0644",
|
|
2770
|
-
"\u0639\u064A\u0646",
|
|
2771
|
-
"\u0632\u062F",
|
|
2772
|
-
"\u0627\u0646\u0642\u0635",
|
|
2773
|
-
"\u0633\u062C\u0644",
|
|
2774
|
-
"\u0623\u0638\u0647\u0631",
|
|
2775
|
-
"\u0623\u062E\u0641",
|
|
2776
|
-
"\u0634\u063A\u0644",
|
|
2777
|
-
"\u0623\u0631\u0633\u0644",
|
|
2778
|
-
"\u0631\u0643\u0632",
|
|
2779
|
-
"\u0634\u0648\u0634",
|
|
2780
|
-
"\u062A\u0648\u0642\u0641",
|
|
2781
|
-
"\u0627\u0646\u0633\u062E",
|
|
2782
|
-
"\u0627\u062D\u0630\u0641",
|
|
2783
|
-
"\u0627\u0635\u0646\u0639",
|
|
2784
|
-
"\u0627\u0646\u062A\u0638\u0631",
|
|
2785
|
-
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
2786
|
-
"\u0623\u0648"
|
|
2787
|
-
]);
|
|
2788
|
-
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
2789
|
-
return 0.15;
|
|
2790
|
-
}
|
|
2791
|
-
if (firstToken.alternatives) {
|
|
2792
|
-
for (const alt of firstToken.alternatives) {
|
|
2793
|
-
if (ARABIC_VERBS.has(alt)) {
|
|
2794
|
-
return 0.15;
|
|
2795
|
-
}
|
|
2796
|
-
}
|
|
2797
|
-
}
|
|
2798
|
-
return 0;
|
|
2799
|
-
}
|
|
2800
|
-
/**
|
|
2801
|
-
* Arabic preposition disambiguation for confidence adjustment.
|
|
2802
|
-
*
|
|
2803
|
-
* Different Arabic prepositions are more or less natural for different semantic roles:
|
|
2804
|
-
* - على (on/upon) is preferred for patient/target roles (element selectors)
|
|
2805
|
-
* - إلى (to) is preferred for destination roles
|
|
2806
|
-
* - من (from) is preferred for source roles
|
|
2807
|
-
* - في (in) is preferred for location roles
|
|
2808
|
-
*
|
|
2809
|
-
* This method analyzes the prepositions used with captured semantic roles and
|
|
2810
|
-
* adjusts confidence based on idiomaticity:
|
|
2811
|
-
* - +0.10 for highly idiomatic preposition choices
|
|
2812
|
-
* - -0.10 for less natural preposition choices
|
|
2813
|
-
*
|
|
2814
|
-
* @param pattern The language pattern being matched
|
|
2815
|
-
* @param captured The captured semantic values
|
|
2816
|
-
* @returns Confidence adjustment (-0.10 to +0.10)
|
|
2817
|
-
*/
|
|
2818
|
-
arabicPrepositionDisambiguation(pattern, captured) {
|
|
2819
|
-
if (pattern.language !== "ar") {
|
|
2820
|
-
return 0;
|
|
2821
|
-
}
|
|
2822
|
-
let adjustment = 0;
|
|
2823
|
-
const PREFERRED_PREPOSITIONS = {
|
|
2824
|
-
patient: ["\u0639\u0644\u0649"],
|
|
2825
|
-
// element selectors prefer على (on/upon)
|
|
2826
|
-
destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2827
|
-
// destination prefers إلى (to)
|
|
2828
|
-
source: ["\u0645\u0646"],
|
|
2829
|
-
// source prefers من (from)
|
|
2830
|
-
agent: ["\u0645\u0646"],
|
|
2831
|
-
// agent/by prefers من (from/by)
|
|
2832
|
-
manner: ["\u0628"],
|
|
2833
|
-
// manner prefers ب (with/by)
|
|
2834
|
-
style: ["\u0628"],
|
|
2835
|
-
// style prefers ب (with)
|
|
2836
|
-
goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2837
|
-
// target state prefers إلى (to)
|
|
2838
|
-
method: ["\u0628"]
|
|
2839
|
-
// method prefers ب (with/by)
|
|
2840
|
-
};
|
|
2841
|
-
for (const [role, value] of captured.entries()) {
|
|
2842
|
-
const preferred = PREFERRED_PREPOSITIONS[role];
|
|
2843
|
-
if (!preferred || preferred.length === 0) {
|
|
2844
|
-
continue;
|
|
2845
|
-
}
|
|
2846
|
-
const metadata = "metadata" in value ? value.metadata : void 0;
|
|
2847
|
-
if (metadata && typeof metadata.prepositionValue === "string") {
|
|
2848
|
-
const usedPreposition = metadata.prepositionValue;
|
|
2849
|
-
if (preferred.includes(usedPreposition)) {
|
|
2850
|
-
adjustment += 0.1;
|
|
2851
|
-
} else {
|
|
2852
|
-
adjustment -= 0.1;
|
|
2853
|
-
}
|
|
2854
|
-
}
|
|
2855
|
-
}
|
|
2856
|
-
return Math.max(-0.1, Math.min(0.1, adjustment));
|
|
2857
|
-
}
|
|
2858
|
-
/**
|
|
2859
|
-
* Skip noise words like "the" before selectors.
|
|
2860
|
-
* This enables more natural English syntax like "toggle the .active".
|
|
2861
|
-
*/
|
|
2862
|
-
skipNoiseWords(tokens) {
|
|
2863
|
-
const token = tokens.peek();
|
|
2864
|
-
if (!token) return;
|
|
2865
|
-
const tokenLower = this.safeToLowerCase(token.value);
|
|
2866
|
-
if (_PatternMatcher.ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
2867
|
-
const mark = tokens.mark();
|
|
2868
|
-
tokens.advance();
|
|
2869
|
-
const nextToken = tokens.peek();
|
|
2870
|
-
if (nextToken && nextToken.kind === "selector") {
|
|
2871
|
-
return;
|
|
2872
|
-
}
|
|
2873
|
-
tokens.reset(mark);
|
|
2874
|
-
}
|
|
2875
|
-
if (tokenLower === "class") {
|
|
2876
|
-
tokens.advance();
|
|
2877
|
-
}
|
|
2878
|
-
}
|
|
2793
|
+
// ===========================================================================
|
|
2794
|
+
// English Idiom Support - Noise Word Handling
|
|
2795
|
+
// ===========================================================================
|
|
2879
2796
|
/**
|
|
2880
2797
|
* Extract event modifiers from the token stream.
|
|
2881
2798
|
* Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
|
|
@@ -2923,22 +2840,6 @@ var _PatternMatcher = class _PatternMatcher {
|
|
|
2923
2840
|
};
|
|
2924
2841
|
/** Maximum tokens to scan ahead when looking for a group's leading marker */
|
|
2925
2842
|
_PatternMatcher.MAX_MARKER_SCAN = 3;
|
|
2926
|
-
// ==========================================================================
|
|
2927
|
-
// Depth Limits for Expression Parsing (security hardening)
|
|
2928
|
-
// ==========================================================================
|
|
2929
|
-
/** Maximum depth for nested property access (e.g., a.b.c.d...) */
|
|
2930
|
-
_PatternMatcher.MAX_PROPERTY_DEPTH = 10;
|
|
2931
|
-
/** Maximum number of arguments in method calls */
|
|
2932
|
-
_PatternMatcher.MAX_METHOD_ARGS = 20;
|
|
2933
|
-
// ===========================================================================
|
|
2934
|
-
// English Idiom Support - Noise Word Handling
|
|
2935
|
-
// ===========================================================================
|
|
2936
|
-
/**
|
|
2937
|
-
* Noise words that can be skipped in English for more natural syntax.
|
|
2938
|
-
* - "the" before selectors: "toggle the .active" → "toggle .active"
|
|
2939
|
-
* - "class" after class selectors: "add the .visible class" → "add .visible"
|
|
2940
|
-
*/
|
|
2941
|
-
_PatternMatcher.ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
2942
2843
|
var PatternMatcher = _PatternMatcher;
|
|
2943
2844
|
var patternMatcher = new PatternMatcher();
|
|
2944
2845
|
function matchPattern(tokens, pattern) {
|
|
@@ -2947,14 +2848,471 @@ function matchPattern(tokens, pattern) {
|
|
|
2947
2848
|
function matchBest(tokens, patterns) {
|
|
2948
2849
|
return patternMatcher.matchBest(tokens, patterns);
|
|
2949
2850
|
}
|
|
2851
|
+
|
|
2852
|
+
// src/core/matching/match-skeleton.ts
|
|
2853
|
+
function groupHasRole(group) {
|
|
2854
|
+
return group.tokens.some((t) => t.type === "role" || t.type === "group" && groupHasRole(t));
|
|
2855
|
+
}
|
|
2856
|
+
var MatchSkeleton = class {
|
|
2857
|
+
/**
|
|
2858
|
+
* @param makeReader - builds the grammar's reader, handed this matcher's view
|
|
2859
|
+
* of the match in progress (one reader per matcher).
|
|
2860
|
+
*/
|
|
2861
|
+
constructor(makeReader, confidenceModel) {
|
|
2862
|
+
/**
|
|
2863
|
+
* The pattern tokens still to match after the one being matched, through
|
|
2864
|
+
* every enclosing group: a value stops at any marker the pattern still owes.
|
|
2865
|
+
*/
|
|
2866
|
+
this.patternRest = [];
|
|
2867
|
+
/**
|
|
2868
|
+
* Whether the role slot being matched directly follows a literal the pattern
|
|
2869
|
+
* matched: its marker (`[por {quantity}]`), or the command's verb.
|
|
2870
|
+
*/
|
|
2871
|
+
this.slotAfterLiteral = false;
|
|
2872
|
+
/**
|
|
2873
|
+
* Selective memoization cache (Phase 6.1).
|
|
2874
|
+
* Caches pattern match results keyed by `tokenPosition:patternId`.
|
|
2875
|
+
* Cleared before each top-level matchBest() call.
|
|
2876
|
+
*/
|
|
2877
|
+
this.matchCache = /* @__PURE__ */ new Map();
|
|
2878
|
+
/**
|
|
2879
|
+
* Stream index where each role's capture began, for the current
|
|
2880
|
+
* `matchPattern` call. Rebuilt per call, written on the same success path as
|
|
2881
|
+
* `captured` — see `PatternMatchResult.roleStarts`.
|
|
2882
|
+
*/
|
|
2883
|
+
this.roleStarts = /* @__PURE__ */ new Map();
|
|
2884
|
+
/**
|
|
2885
|
+
* Track stem matches for confidence calculation.
|
|
2886
|
+
* This is set during matching and read during confidence calculation.
|
|
2887
|
+
*/
|
|
2888
|
+
this.stemMatchCount = 0;
|
|
2889
|
+
this.totalKeywordMatches = 0;
|
|
2890
|
+
this.confidenceModel = confidenceModel;
|
|
2891
|
+
this.reader = makeReader({
|
|
2892
|
+
patternRest: () => this.patternRest,
|
|
2893
|
+
slotAfterLiteral: () => this.slotAfterLiteral,
|
|
2894
|
+
wouldMatch: (pt, token) => this.patternTokenWouldMatch(pt, token)
|
|
2895
|
+
});
|
|
2896
|
+
}
|
|
2897
|
+
/**
|
|
2898
|
+
* Try to match a single pattern against the token stream.
|
|
2899
|
+
* Returns the match result or null if no match.
|
|
2900
|
+
*/
|
|
2901
|
+
matchPattern(tokens, pattern) {
|
|
2902
|
+
const mark = tokens.mark();
|
|
2903
|
+
const captured = /* @__PURE__ */ new Map();
|
|
2904
|
+
this.reader.beginPattern(pattern);
|
|
2905
|
+
this.stemMatchCount = 0;
|
|
2906
|
+
this.totalKeywordMatches = 0;
|
|
2907
|
+
this.roleStarts = /* @__PURE__ */ new Map();
|
|
2908
|
+
this.patternRest = [];
|
|
2909
|
+
const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
|
|
2910
|
+
if (!success) {
|
|
2911
|
+
tokens.reset(mark);
|
|
2912
|
+
return null;
|
|
2913
|
+
}
|
|
2914
|
+
const confidence = this.confidenceModel.calculate({
|
|
2915
|
+
pattern,
|
|
2916
|
+
captured,
|
|
2917
|
+
stemMatchCount: this.stemMatchCount,
|
|
2918
|
+
totalKeywordMatches: this.totalKeywordMatches
|
|
2919
|
+
});
|
|
2920
|
+
this.applyExtractionRules(pattern, captured);
|
|
2921
|
+
return {
|
|
2922
|
+
pattern,
|
|
2923
|
+
captured,
|
|
2924
|
+
consumedTokens: tokens.position() - mark.position,
|
|
2925
|
+
confidence,
|
|
2926
|
+
roleStarts: this.roleStarts,
|
|
2927
|
+
...this.reader.resultFields?.()
|
|
2928
|
+
};
|
|
2929
|
+
}
|
|
2930
|
+
/**
|
|
2931
|
+
* Try to match multiple patterns, return the best match.
|
|
2932
|
+
*/
|
|
2933
|
+
matchBest(tokens, patterns) {
|
|
2934
|
+
this.matchCache.clear();
|
|
2935
|
+
const matches = [];
|
|
2936
|
+
const startPos = tokens.position();
|
|
2937
|
+
for (const pattern of patterns) {
|
|
2938
|
+
const mark = tokens.mark();
|
|
2939
|
+
const cacheKey = `${startPos}:${pattern.id}`;
|
|
2940
|
+
let result;
|
|
2941
|
+
if (this.matchCache.has(cacheKey)) {
|
|
2942
|
+
result = this.matchCache.get(cacheKey);
|
|
2943
|
+
} else {
|
|
2944
|
+
result = this.matchPattern(tokens, pattern);
|
|
2945
|
+
this.matchCache.set(cacheKey, result);
|
|
2946
|
+
}
|
|
2947
|
+
if (result) {
|
|
2948
|
+
matches.push(result);
|
|
2949
|
+
}
|
|
2950
|
+
tokens.reset(mark);
|
|
2951
|
+
}
|
|
2952
|
+
if (matches.length === 0) {
|
|
2953
|
+
return null;
|
|
2954
|
+
}
|
|
2955
|
+
matches.sort((a, b) => {
|
|
2956
|
+
const priorityDiff = b.pattern.priority - a.pattern.priority;
|
|
2957
|
+
if (priorityDiff !== 0) return priorityDiff;
|
|
2958
|
+
return b.confidence - a.confidence;
|
|
2959
|
+
});
|
|
2960
|
+
const best = matches[0];
|
|
2961
|
+
this.matchPattern(tokens, best.pattern);
|
|
2962
|
+
return best;
|
|
2963
|
+
}
|
|
2964
|
+
/**
|
|
2965
|
+
* Match a sequence of pattern tokens against the token stream.
|
|
2966
|
+
*/
|
|
2967
|
+
matchTokenSequence(tokens, patternTokens, captured, nextAfterSequence) {
|
|
2968
|
+
this.reader.skipLeading?.(tokens, patternTokens);
|
|
2969
|
+
for (let i = 0; i < patternTokens.length; i++) {
|
|
2970
|
+
const patternToken = patternTokens[i];
|
|
2971
|
+
this.reader.beforePatternToken?.(tokens, patternTokens, i, captured);
|
|
2972
|
+
if (this.reader.maySkipSlot(
|
|
2973
|
+
patternToken,
|
|
2974
|
+
tokens.peek(),
|
|
2975
|
+
patternTokens[i + 1] ?? nextAfterSequence
|
|
2976
|
+
)) {
|
|
2977
|
+
const skipMark = tokens.mark();
|
|
2978
|
+
const speculative = new Map(captured);
|
|
2979
|
+
if (this.matchTokenSequence(
|
|
2980
|
+
tokens,
|
|
2981
|
+
patternTokens.slice(i + 1),
|
|
2982
|
+
speculative,
|
|
2983
|
+
nextAfterSequence
|
|
2984
|
+
) && this.reader.atClauseEnd(tokens)) {
|
|
2985
|
+
captured.clear();
|
|
2986
|
+
for (const [role, value] of speculative) captured.set(role, value);
|
|
2987
|
+
return true;
|
|
2988
|
+
}
|
|
2989
|
+
tokens.reset(skipMark);
|
|
2990
|
+
}
|
|
2991
|
+
const outerRest = this.patternRest;
|
|
2992
|
+
this.patternRest = [...patternTokens.slice(i + 1), ...outerRest];
|
|
2993
|
+
this.slotAfterLiteral = patternToken.type === "role" && patternTokens[i - 1]?.type === "literal";
|
|
2994
|
+
const matched = this.matchPatternToken(
|
|
2995
|
+
tokens,
|
|
2996
|
+
patternToken,
|
|
2997
|
+
captured,
|
|
2998
|
+
patternTokens[i + 1] ?? nextAfterSequence
|
|
2999
|
+
);
|
|
3000
|
+
this.patternRest = outerRest;
|
|
3001
|
+
if (!matched) {
|
|
3002
|
+
if (this.isOptional(patternToken)) {
|
|
3003
|
+
continue;
|
|
3004
|
+
}
|
|
3005
|
+
return false;
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
return true;
|
|
3009
|
+
}
|
|
3010
|
+
/**
|
|
3011
|
+
* Match a single pattern token against the current position in the stream.
|
|
3012
|
+
*/
|
|
3013
|
+
matchPatternToken(tokens, patternToken, captured, nextPatternToken) {
|
|
3014
|
+
switch (patternToken.type) {
|
|
3015
|
+
case "literal":
|
|
3016
|
+
return this.matchLiteralToken(tokens, patternToken);
|
|
3017
|
+
case "role": {
|
|
3018
|
+
const roleStart = tokens.position();
|
|
3019
|
+
const matchedRole = this.reader.readRole(tokens, patternToken, captured, nextPatternToken);
|
|
3020
|
+
if (matchedRole && captured.has(patternToken.role)) {
|
|
3021
|
+
this.roleStarts.set(patternToken.role, roleStart);
|
|
3022
|
+
}
|
|
3023
|
+
return matchedRole;
|
|
3024
|
+
}
|
|
3025
|
+
case "group":
|
|
3026
|
+
return this.matchGroupToken(tokens, patternToken, captured, nextPatternToken);
|
|
3027
|
+
default:
|
|
3028
|
+
return false;
|
|
3029
|
+
}
|
|
3030
|
+
}
|
|
3031
|
+
/**
|
|
3032
|
+
* Match a literal pattern token (keyword or particle).
|
|
3033
|
+
*/
|
|
3034
|
+
matchLiteralToken(tokens, patternToken) {
|
|
3035
|
+
const token = tokens.peek();
|
|
3036
|
+
if (!token) return false;
|
|
3037
|
+
const matchType = this.getMatchType(token, patternToken.value);
|
|
3038
|
+
if (matchType !== "none") {
|
|
3039
|
+
this.totalKeywordMatches++;
|
|
3040
|
+
if (matchType === "stem") {
|
|
3041
|
+
this.stemMatchCount++;
|
|
3042
|
+
}
|
|
3043
|
+
tokens.advance();
|
|
3044
|
+
return true;
|
|
3045
|
+
}
|
|
3046
|
+
if (patternToken.alternatives) {
|
|
3047
|
+
for (const alt of patternToken.alternatives) {
|
|
3048
|
+
const altMatchType = this.getMatchType(token, alt);
|
|
3049
|
+
if (altMatchType !== "none") {
|
|
3050
|
+
this.totalKeywordMatches++;
|
|
3051
|
+
if (altMatchType === "stem") {
|
|
3052
|
+
this.stemMatchCount++;
|
|
3053
|
+
}
|
|
3054
|
+
tokens.advance();
|
|
3055
|
+
return true;
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
}
|
|
3059
|
+
for (const value of [patternToken.value, ...patternToken.alternatives ?? []]) {
|
|
3060
|
+
for (const sep of ["-", "_"]) {
|
|
3061
|
+
if (value.includes(sep) && this.tryMatchShatteredCompound(tokens, value, sep)) {
|
|
3062
|
+
this.totalKeywordMatches++;
|
|
3063
|
+
return true;
|
|
3064
|
+
}
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
return false;
|
|
3068
|
+
}
|
|
3069
|
+
/**
|
|
3070
|
+
* Match a compound keyword (`titik-henti`, `по_умолчанию`) against a
|
|
3071
|
+
* shattered token run (`titik` `-` `henti` / `по` `_` `умолчанию`): word
|
|
3072
|
+
* segments must be identifier/keyword/particle tokens equal to the expected
|
|
3073
|
+
* segments (case-insensitive; particle because prepositions like ru `по` /
|
|
3074
|
+
* uk `за` lead some underscore compounds), separators lone `sep` tokens.
|
|
3075
|
+
* Consumes the run on success, resets the stream on failure.
|
|
3076
|
+
*/
|
|
3077
|
+
tryMatchShatteredCompound(tokens, expected, sep) {
|
|
3078
|
+
const segments = expected.split(sep);
|
|
3079
|
+
if (segments.length < 2 || segments.some((s) => s.length === 0)) return false;
|
|
3080
|
+
const mark = tokens.mark();
|
|
3081
|
+
for (let i = 0; i < segments.length; i++) {
|
|
3082
|
+
if (i > 0) {
|
|
3083
|
+
const sepToken = tokens.peek();
|
|
3084
|
+
if (!sepToken || sepToken.value !== sep) {
|
|
3085
|
+
tokens.reset(mark);
|
|
3086
|
+
return false;
|
|
3087
|
+
}
|
|
3088
|
+
tokens.advance();
|
|
3089
|
+
}
|
|
3090
|
+
const word = tokens.peek();
|
|
3091
|
+
if (!word || word.kind !== "identifier" && word.kind !== "keyword" && word.kind !== "particle" || word.value.toLowerCase() !== segments[i].toLowerCase()) {
|
|
3092
|
+
tokens.reset(mark);
|
|
3093
|
+
return false;
|
|
3094
|
+
}
|
|
3095
|
+
tokens.advance();
|
|
3096
|
+
}
|
|
3097
|
+
return true;
|
|
3098
|
+
}
|
|
3099
|
+
/**
|
|
3100
|
+
* Match a group pattern token (optional sequence).
|
|
3101
|
+
*/
|
|
3102
|
+
matchGroupToken(tokens, patternToken, captured, nextPatternToken) {
|
|
3103
|
+
const mark = tokens.mark();
|
|
3104
|
+
const capturedBefore = new Map(captured);
|
|
3105
|
+
let success = this.matchTokenSequence(tokens, patternToken.tokens, captured, nextPatternToken);
|
|
3106
|
+
if (success && tokens.position() > mark.position && groupHasRole(patternToken) && [...captured].every(([role, value]) => capturedBefore.get(role) === value) && captured.size === capturedBefore.size) {
|
|
3107
|
+
success = false;
|
|
3108
|
+
}
|
|
3109
|
+
if (success) {
|
|
3110
|
+
const markerLiterals = [];
|
|
3111
|
+
for (const inner of patternToken.tokens) {
|
|
3112
|
+
if (inner.type === "group" && inner.renderRequired) {
|
|
3113
|
+
for (const t of inner.tokens) {
|
|
3114
|
+
if (t.type === "literal") markerLiterals.push(t.value, ...t.alternatives ?? []);
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
if (markerLiterals.length > 0) {
|
|
3119
|
+
const addedRole = [...captured.keys()].some((role) => !capturedBefore.has(role));
|
|
3120
|
+
if (addedRole) {
|
|
3121
|
+
const last = tokens.tokens[tokens.position() - 1];
|
|
3122
|
+
const markerSeen = last !== void 0 && tokens.position() > mark.position && markerLiterals.some((lit) => this.getMatchType(last, lit) !== "none");
|
|
3123
|
+
if (!markerSeen) success = false;
|
|
3124
|
+
}
|
|
3125
|
+
}
|
|
3126
|
+
}
|
|
3127
|
+
if (!success) {
|
|
3128
|
+
tokens.reset(mark);
|
|
3129
|
+
for (const role of captured.keys()) {
|
|
3130
|
+
if (!capturedBefore.has(role)) {
|
|
3131
|
+
captured.delete(role);
|
|
3132
|
+
}
|
|
3133
|
+
}
|
|
3134
|
+
for (const [role, value] of capturedBefore) {
|
|
3135
|
+
if (captured.get(role) !== value) {
|
|
3136
|
+
captured.set(role, value);
|
|
3137
|
+
}
|
|
3138
|
+
}
|
|
3139
|
+
return patternToken.optional || false;
|
|
3140
|
+
}
|
|
3141
|
+
return true;
|
|
3142
|
+
}
|
|
3143
|
+
/**
|
|
3144
|
+
* Whether a pattern token (literal, or a group starting with a literal) would
|
|
3145
|
+
* match the given stream token. Used to keep the event-head source-clause
|
|
3146
|
+
* consumption from stealing a marker the pattern explicitly expects.
|
|
3147
|
+
*/
|
|
3148
|
+
patternTokenWouldMatch(pt, token) {
|
|
3149
|
+
if (!pt) return false;
|
|
3150
|
+
if (pt.type === "literal") {
|
|
3151
|
+
if (this.getMatchType(token, pt.value) !== "none") return true;
|
|
3152
|
+
return !!pt.alternatives?.some((a) => this.getMatchType(token, a) !== "none");
|
|
3153
|
+
}
|
|
3154
|
+
if (pt.type === "group") {
|
|
3155
|
+
return this.patternTokenWouldMatch(pt.tokens?.[0], token);
|
|
3156
|
+
}
|
|
3157
|
+
return false;
|
|
3158
|
+
}
|
|
3159
|
+
/**
|
|
3160
|
+
* Get the type of match for a token against a value.
|
|
3161
|
+
* Used for confidence calculation.
|
|
3162
|
+
*/
|
|
3163
|
+
getMatchType(token, value) {
|
|
3164
|
+
if (token.value === value) return "exact";
|
|
3165
|
+
if (token.normalized === value) return "normalized";
|
|
3166
|
+
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
3167
|
+
return "stem";
|
|
3168
|
+
}
|
|
3169
|
+
if (token.kind === "keyword" && token.value.toLowerCase() === value.toLowerCase()) {
|
|
3170
|
+
return "case-insensitive";
|
|
3171
|
+
}
|
|
3172
|
+
if (token.kind === "keyword") {
|
|
3173
|
+
const strippedToken = stripOptionalDiacritics(token.value).toLowerCase();
|
|
3174
|
+
if (strippedToken && strippedToken === stripOptionalDiacritics(value).toLowerCase()) {
|
|
3175
|
+
return "case-insensitive";
|
|
3176
|
+
}
|
|
3177
|
+
}
|
|
3178
|
+
return "none";
|
|
3179
|
+
}
|
|
3180
|
+
/**
|
|
3181
|
+
* Apply extraction rules to fill in static values and defaults for missing
|
|
3182
|
+
* roles, and re-shape captured roles that declare a `transform` (the
|
|
3183
|
+
* ExtractionRule field was documented but unwired — go-qu-url-dest is the
|
|
3184
|
+
* first consumer: it re-types the marker-bound capture to the expression
|
|
3185
|
+
* shape the en reference produces).
|
|
3186
|
+
*/
|
|
3187
|
+
applyExtractionRules(pattern, captured) {
|
|
3188
|
+
for (const [role, rule] of Object.entries(pattern.extraction)) {
|
|
3189
|
+
const existing = captured.get(role);
|
|
3190
|
+
if (existing) {
|
|
3191
|
+
if (rule.transform) {
|
|
3192
|
+
const raw = existing.type === "literal" ? String(existing.value) : String(existing.raw ?? "");
|
|
3193
|
+
captured.set(role, rule.transform(raw));
|
|
3194
|
+
}
|
|
3195
|
+
} else if (rule.value !== void 0) {
|
|
3196
|
+
captured.set(
|
|
3197
|
+
role,
|
|
3198
|
+
rule.valueIsExpression ? { type: "expression", raw: rule.value } : { type: "literal", value: rule.value }
|
|
3199
|
+
);
|
|
3200
|
+
} else if (rule.default) {
|
|
3201
|
+
const implicitDefault = {
|
|
3202
|
+
...rule.default,
|
|
3203
|
+
implicit: true
|
|
3204
|
+
};
|
|
3205
|
+
captured.set(role, implicitDefault);
|
|
3206
|
+
}
|
|
3207
|
+
}
|
|
3208
|
+
}
|
|
3209
|
+
/**
|
|
3210
|
+
* Check if a pattern token is optional.
|
|
3211
|
+
*/
|
|
3212
|
+
isOptional(patternToken) {
|
|
3213
|
+
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
3214
|
+
}
|
|
3215
|
+
};
|
|
3216
|
+
|
|
3217
|
+
// src/core/matching/stock-reader.ts
|
|
3218
|
+
function greedyStopMarkers(rest) {
|
|
3219
|
+
const markers = /* @__PURE__ */ new Set();
|
|
3220
|
+
const add = (pt) => {
|
|
3221
|
+
markers.add(pt.value.toLowerCase());
|
|
3222
|
+
for (const alt of pt.alternatives ?? []) markers.add(alt.toLowerCase());
|
|
3223
|
+
};
|
|
3224
|
+
for (const pt of rest) {
|
|
3225
|
+
if (pt.type === "literal") {
|
|
3226
|
+
add(pt);
|
|
3227
|
+
break;
|
|
3228
|
+
}
|
|
3229
|
+
if (pt.type === "group") {
|
|
3230
|
+
const lead = pt.tokens.find((t) => t.type === "literal");
|
|
3231
|
+
if (lead?.type === "literal") add(lead);
|
|
3232
|
+
if (!pt.optional) break;
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
return markers;
|
|
3236
|
+
}
|
|
3237
|
+
function isStopMarker(token, stopMarkers) {
|
|
3238
|
+
if (stopMarkers.size === 0) return false;
|
|
3239
|
+
if (stopMarkers.has(token.value.toLowerCase())) return true;
|
|
3240
|
+
return !!token.normalized && stopMarkers.has(token.normalized.toLowerCase());
|
|
3241
|
+
}
|
|
3242
|
+
function readGreedy(tokens, stopMarkers) {
|
|
3243
|
+
const values = [];
|
|
3244
|
+
while (!tokens.isAtEnd()) {
|
|
3245
|
+
const next = tokens.peek();
|
|
3246
|
+
if (!next || isStopMarker(next, stopMarkers)) break;
|
|
3247
|
+
values.push(next.value);
|
|
3248
|
+
tokens.advance();
|
|
3249
|
+
}
|
|
3250
|
+
return values.length > 0 ? { type: "expression", raw: values.join(" ") } : null;
|
|
3251
|
+
}
|
|
3252
|
+
var StockValueReader = class {
|
|
3253
|
+
constructor(view, profiles = {}) {
|
|
3254
|
+
this.view = view;
|
|
3255
|
+
this.profiles = profiles;
|
|
3256
|
+
}
|
|
3257
|
+
beginPattern(pattern) {
|
|
3258
|
+
this.profile = this.profiles[pattern.language] ?? { code: pattern.language };
|
|
3259
|
+
this.lastOptional = void 0;
|
|
3260
|
+
}
|
|
3261
|
+
readRole(tokens, slot, captured, next) {
|
|
3262
|
+
const start = tokens.mark();
|
|
3263
|
+
if (this.readOnce(tokens, slot, captured, next, true)) {
|
|
3264
|
+
if (!slot.optional) {
|
|
3265
|
+
this.lastOptional = void 0;
|
|
3266
|
+
} else if (captured.has(slot.role) && tokens.position() > start.position && !this.view.slotAfterLiteral() && next?.type !== "literal") {
|
|
3267
|
+
this.lastOptional = { role: slot.role, start, end: tokens.position() };
|
|
3268
|
+
}
|
|
3269
|
+
return true;
|
|
3270
|
+
}
|
|
3271
|
+
const before = this.lastOptional;
|
|
3272
|
+
if (slot.optional || !before || before.end !== tokens.position()) return false;
|
|
3273
|
+
this.lastOptional = void 0;
|
|
3274
|
+
tokens.reset(before.start);
|
|
3275
|
+
captured.delete(before.role);
|
|
3276
|
+
return this.readOnce(tokens, slot, captured, next, false);
|
|
3277
|
+
}
|
|
3278
|
+
/** One read of `slot`; `guard` refuses a required slot the marker the pattern wants next. */
|
|
3279
|
+
readOnce(tokens, slot, captured, next, guard) {
|
|
3280
|
+
if (guard && !slot.optional && next?.type === "literal") {
|
|
3281
|
+
const token = tokens.peek();
|
|
3282
|
+
if (token && this.view.wouldMatch(next, token)) return false;
|
|
3283
|
+
}
|
|
3284
|
+
if (slot.greedy) {
|
|
3285
|
+
const value = readGreedy(tokens, greedyStopMarkers(this.view.patternRest()));
|
|
3286
|
+
if (value) {
|
|
3287
|
+
captured.set(slot.role, value);
|
|
3288
|
+
return true;
|
|
3289
|
+
}
|
|
3290
|
+
return slot.optional ?? false;
|
|
3291
|
+
}
|
|
3292
|
+
return readRoleValue(tokens, slot, captured, this.profile);
|
|
3293
|
+
}
|
|
3294
|
+
maySkipSlot(patternToken, token, next) {
|
|
3295
|
+
const slot = patternToken.type === "group" && patternToken.optional ? patternToken.tokens.every((t) => t.type === "role") ? patternToken.tokens[0] : void 0 : patternToken;
|
|
3296
|
+
if (!slot || slot.type !== "role" || !slot.optional) return false;
|
|
3297
|
+
return !!token && this.view.wouldMatch(next, token);
|
|
3298
|
+
}
|
|
3299
|
+
atClauseEnd(tokens) {
|
|
3300
|
+
return tokens.isAtEnd();
|
|
3301
|
+
}
|
|
3302
|
+
};
|
|
3303
|
+
function createStockReader(profiles = {}) {
|
|
3304
|
+
return (view) => new StockValueReader(view, profiles);
|
|
3305
|
+
}
|
|
2950
3306
|
export {
|
|
2951
3307
|
BaseMorphologicalNormalizer,
|
|
2952
3308
|
BaseTokenizer,
|
|
2953
3309
|
DEFAULT_OPERATORS,
|
|
2954
3310
|
DEFAULT_PUNCTUATION,
|
|
3311
|
+
MatchSkeleton,
|
|
2955
3312
|
OperatorExtractor,
|
|
2956
3313
|
PatternMatcher,
|
|
2957
3314
|
PunctuationExtractor,
|
|
3315
|
+
StockValueReader,
|
|
2958
3316
|
TokenStreamImpl,
|
|
2959
3317
|
combineClassifiers,
|
|
2960
3318
|
createAsyncNode,
|
|
@@ -2973,6 +3331,7 @@ export {
|
|
|
2973
3331
|
createReference,
|
|
2974
3332
|
createSelector,
|
|
2975
3333
|
createSimpleTokenizer,
|
|
3334
|
+
createStockReader,
|
|
2976
3335
|
createToken,
|
|
2977
3336
|
createTryNode,
|
|
2978
3337
|
createUnicodeRangeClassifier,
|
|
@@ -2986,6 +3345,7 @@ export {
|
|
|
2986
3345
|
getDefaultExtractors,
|
|
2987
3346
|
getPossessiveReference,
|
|
2988
3347
|
getRoleValue,
|
|
3348
|
+
greedyStopMarkers,
|
|
2989
3349
|
isAsciiIdentifierChar,
|
|
2990
3350
|
isAsciiLetter,
|
|
2991
3351
|
isBuiltInReference,
|
|
@@ -3009,6 +3369,9 @@ export {
|
|
|
3009
3369
|
noChange,
|
|
3010
3370
|
normalized,
|
|
3011
3371
|
patternMatcher,
|
|
3372
|
+
readGreedy,
|
|
3373
|
+
readRoleValue,
|
|
3374
|
+
stockConfidenceModel,
|
|
3012
3375
|
stripOptionalDiacritics,
|
|
3013
3376
|
validateValueType,
|
|
3014
3377
|
withDefaultExtractors
|