@lokascript/framework 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +142 -0
- package/dist/aot/aot-orchestrator.d.ts +75 -0
- package/dist/aot/aot-orchestrator.d.ts.map +1 -0
- package/dist/aot/domain-scanner.d.ts +27 -0
- package/dist/aot/domain-scanner.d.ts.map +1 -0
- package/dist/aot/index.d.ts +8 -0
- package/dist/aot/index.d.ts.map +1 -0
- package/dist/aot/types.d.ts +103 -0
- package/dist/aot/types.d.ts.map +1 -0
- package/dist/api/create-dsl.d.ts +91 -0
- package/dist/api/create-dsl.d.ts.map +1 -0
- package/dist/api/dispatcher.d.ts +108 -0
- package/dist/api/dispatcher.d.ts.map +1 -0
- package/dist/api/domain-registry.d.ts +152 -0
- package/dist/api/domain-registry.d.ts.map +1 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +2082 -0
- package/dist/api/index.js.map +1 -0
- package/dist/core/index.d.ts +7 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +2674 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/logger.d.ts +32 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/pattern-matching/index.d.ts +6 -0
- package/dist/core/pattern-matching/index.d.ts.map +1 -0
- package/dist/core/pattern-matching/index.js +1239 -0
- package/dist/core/pattern-matching/index.js.map +1 -0
- package/dist/core/pattern-matching/pattern-matcher.d.ts +239 -0
- package/dist/core/pattern-matching/pattern-matcher.d.ts.map +1 -0
- package/dist/core/pattern-matching/utils/index.d.ts +6 -0
- package/dist/core/pattern-matching/utils/index.d.ts.map +1 -0
- package/dist/core/pattern-matching/utils/possessive-keywords.d.ts +38 -0
- package/dist/core/pattern-matching/utils/possessive-keywords.d.ts.map +1 -0
- package/dist/core/pattern-matching/utils/type-validation.d.ts +63 -0
- package/dist/core/pattern-matching/utils/type-validation.d.ts.map +1 -0
- package/dist/core/tokenization/base-tokenizer.d.ts +344 -0
- package/dist/core/tokenization/base-tokenizer.d.ts.map +1 -0
- package/dist/core/tokenization/char-classifiers.d.ts +56 -0
- package/dist/core/tokenization/char-classifiers.d.ts.map +1 -0
- package/dist/core/tokenization/default-extractors.d.ts +48 -0
- package/dist/core/tokenization/default-extractors.d.ts.map +1 -0
- package/dist/core/tokenization/extractors/index.d.ts +9 -0
- package/dist/core/tokenization/extractors/index.d.ts.map +1 -0
- package/dist/core/tokenization/extractors/operator.d.ts +23 -0
- package/dist/core/tokenization/extractors/operator.d.ts.map +1 -0
- package/dist/core/tokenization/extractors/punctuation.d.ts +22 -0
- package/dist/core/tokenization/extractors/punctuation.d.ts.map +1 -0
- package/dist/core/tokenization/extractors.d.ts +61 -0
- package/dist/core/tokenization/extractors.d.ts.map +1 -0
- package/dist/core/tokenization/index.d.ts +11 -0
- package/dist/core/tokenization/index.d.ts.map +1 -0
- package/dist/core/tokenization/index.js +1345 -0
- package/dist/core/tokenization/index.js.map +1 -0
- package/dist/core/tokenization/morphology/index.d.ts +5 -0
- package/dist/core/tokenization/morphology/index.d.ts.map +1 -0
- package/dist/core/tokenization/morphology/types.d.ts +110 -0
- package/dist/core/tokenization/morphology/types.d.ts.map +1 -0
- package/dist/core/tokenization/token-utils.d.ts +111 -0
- package/dist/core/tokenization/token-utils.d.ts.map +1 -0
- package/dist/core/types.d.ts +382 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +108 -0
- package/dist/core/types.js.map +1 -0
- package/dist/generation/diagnostics.d.ts +120 -0
- package/dist/generation/diagnostics.d.ts.map +1 -0
- package/dist/generation/index.d.ts +7 -0
- package/dist/generation/index.d.ts.map +1 -0
- package/dist/generation/index.js +339 -0
- package/dist/generation/index.js.map +1 -0
- package/dist/generation/pattern-generator.d.ts +48 -0
- package/dist/generation/pattern-generator.d.ts.map +1 -0
- package/dist/generation/renderer.d.ts +115 -0
- package/dist/generation/renderer.d.ts.map +1 -0
- package/dist/grammar/index.d.ts +10 -0
- package/dist/grammar/index.d.ts.map +1 -0
- package/dist/grammar/index.js +391 -0
- package/dist/grammar/index.js.map +1 -0
- package/dist/grammar/transformer.d.ts +56 -0
- package/dist/grammar/transformer.d.ts.map +1 -0
- package/dist/grammar/types.d.ts +236 -0
- package/dist/grammar/types.d.ts.map +1 -0
- package/dist/index.cjs +4454 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4336 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/dictionary.d.ts +82 -0
- package/dist/interfaces/dictionary.d.ts.map +1 -0
- package/dist/interfaces/index.d.ts +10 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/profile-provider.d.ts +67 -0
- package/dist/interfaces/profile-provider.d.ts.map +1 -0
- package/dist/interfaces/value-extractor.d.ts +168 -0
- package/dist/interfaces/value-extractor.d.ts.map +1 -0
- package/dist/multilingual/index.d.ts +8 -0
- package/dist/multilingual/index.d.ts.map +1 -0
- package/dist/multilingual/index.js +1 -0
- package/dist/multilingual/index.js.map +1 -0
- package/dist/parsing/index.d.ts +8 -0
- package/dist/parsing/index.d.ts.map +1 -0
- package/dist/parsing/index.js +1415 -0
- package/dist/parsing/index.js.map +1 -0
- package/dist/parsing/multi-statement.d.ts +265 -0
- package/dist/parsing/multi-statement.d.ts.map +1 -0
- package/dist/schema/command-schema.d.ts +78 -0
- package/dist/schema/command-schema.d.ts.map +1 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +25 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/test-setup.d.ts +9 -0
- package/dist/test-setup.d.ts.map +1 -0
- package/dist/testing/index.d.ts +50 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +16969 -0
- package/dist/testing/index.js.map +1 -0
- package/package.json +122 -0
- package/src/__test__/fixtures/sql-dsl.ts +232 -0
- package/src/__test__/sql-integration.test.ts +189 -0
- package/src/__test__/test-utils.ts +260 -0
- package/src/aot/aot-orchestrator.test.ts +413 -0
- package/src/aot/aot-orchestrator.ts +238 -0
- package/src/aot/domain-scanner.ts +178 -0
- package/src/aot/index.ts +8 -0
- package/src/aot/types.ts +124 -0
- package/src/api/create-dsl.ts +367 -0
- package/src/api/dispatcher.test.ts +336 -0
- package/src/api/dispatcher.ts +222 -0
- package/src/api/domain-registry.test.ts +336 -0
- package/src/api/domain-registry.ts +500 -0
- package/src/api/index.ts +7 -0
- package/src/core/index.ts +7 -0
- package/src/core/logger.ts +130 -0
- package/src/core/pattern-matching/index.ts +6 -0
- package/src/core/pattern-matching/pattern-matcher.test.ts +900 -0
- package/src/core/pattern-matching/pattern-matcher.ts +1548 -0
- package/src/core/pattern-matching/pattern-matcher.ts.backup +1267 -0
- package/src/core/pattern-matching/utils/index.ts +6 -0
- package/src/core/pattern-matching/utils/possessive-keywords.ts +55 -0
- package/src/core/pattern-matching/utils/type-validation.test.ts +316 -0
- package/src/core/pattern-matching/utils/type-validation.ts +134 -0
- package/src/core/tokenization/base-tokenizer.ts +916 -0
- package/src/core/tokenization/char-classifiers.ts +79 -0
- package/src/core/tokenization/create-simple-tokenizer.test.ts +260 -0
- package/src/core/tokenization/default-extractors.ts +69 -0
- package/src/core/tokenization/extractors/index.ts +9 -0
- package/src/core/tokenization/extractors/operator.ts +75 -0
- package/src/core/tokenization/extractors/punctuation.ts +39 -0
- package/src/core/tokenization/extractors.ts +452 -0
- package/src/core/tokenization/index.ts +11 -0
- package/src/core/tokenization/morphology/index.ts +5 -0
- package/src/core/tokenization/morphology/types.ts +211 -0
- package/src/core/tokenization/token-utils.ts +252 -0
- package/src/core/types.ts +589 -0
- package/src/generation/diagnostics.test.ts +171 -0
- package/src/generation/diagnostics.ts +239 -0
- package/src/generation/index.ts +7 -0
- package/src/generation/pattern-generator.test.ts +430 -0
- package/src/generation/pattern-generator.ts +315 -0
- package/src/generation/renderer.test.ts +266 -0
- package/src/generation/renderer.ts +244 -0
- package/src/grammar/index.ts +12 -0
- package/src/grammar/transformer.ts +159 -0
- package/src/grammar/types.ts +630 -0
- package/src/index.ts +157 -0
- package/src/interfaces/dictionary.ts +123 -0
- package/src/interfaces/index.ts +10 -0
- package/src/interfaces/profile-provider.ts +88 -0
- package/src/interfaces/value-extractor.ts +435 -0
- package/src/multilingual/index.ts +9 -0
- package/src/parsing/index.ts +27 -0
- package/src/parsing/multi-statement.test.ts +480 -0
- package/src/parsing/multi-statement.ts +648 -0
- package/src/schema/command-schema.ts +118 -0
- package/src/schema/index.ts +5 -0
- package/src/test-setup.ts +45 -0
- package/src/testing/index.ts +137 -0
|
@@ -0,0 +1,1345 @@
|
|
|
1
|
+
// src/core/tokenization/token-utils.ts
|
|
2
|
+
var TokenStreamImpl = class {
|
|
3
|
+
constructor(tokens, language) {
|
|
4
|
+
this.pos = 0;
|
|
5
|
+
this.tokens = tokens;
|
|
6
|
+
this.language = language;
|
|
7
|
+
}
|
|
8
|
+
peek(offset = 0) {
|
|
9
|
+
const index = this.pos + offset;
|
|
10
|
+
if (index < 0 || index >= this.tokens.length) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return this.tokens[index];
|
|
14
|
+
}
|
|
15
|
+
advance() {
|
|
16
|
+
if (this.isAtEnd()) {
|
|
17
|
+
throw new Error("Unexpected end of token stream");
|
|
18
|
+
}
|
|
19
|
+
return this.tokens[this.pos++];
|
|
20
|
+
}
|
|
21
|
+
isAtEnd() {
|
|
22
|
+
return this.pos >= this.tokens.length;
|
|
23
|
+
}
|
|
24
|
+
mark() {
|
|
25
|
+
return { position: this.pos };
|
|
26
|
+
}
|
|
27
|
+
reset(mark) {
|
|
28
|
+
this.pos = mark.position;
|
|
29
|
+
}
|
|
30
|
+
position() {
|
|
31
|
+
return this.pos;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get remaining tokens as an array.
|
|
35
|
+
*/
|
|
36
|
+
remaining() {
|
|
37
|
+
return this.tokens.slice(this.pos);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Consume tokens while predicate is true.
|
|
41
|
+
*/
|
|
42
|
+
takeWhile(predicate) {
|
|
43
|
+
const result = [];
|
|
44
|
+
while (!this.isAtEnd() && predicate(this.peek())) {
|
|
45
|
+
result.push(this.advance());
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Skip tokens while predicate is true.
|
|
51
|
+
*/
|
|
52
|
+
skipWhile(predicate) {
|
|
53
|
+
while (!this.isAtEnd() && predicate(this.peek())) {
|
|
54
|
+
this.advance();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
function createPosition(start, end) {
|
|
59
|
+
return { start, end };
|
|
60
|
+
}
|
|
61
|
+
function createToken(valueOrParams, kind, position, normalizedOrOptions) {
|
|
62
|
+
if (typeof valueOrParams === "object") {
|
|
63
|
+
const { value: value2, kind: kind2, position: position2, normalized: normalized2, stem, stemConfidence, metadata } = valueOrParams;
|
|
64
|
+
return {
|
|
65
|
+
value: value2,
|
|
66
|
+
kind: kind2,
|
|
67
|
+
position: position2,
|
|
68
|
+
...normalized2 !== void 0 && { normalized: normalized2 },
|
|
69
|
+
...stem !== void 0 && { stem },
|
|
70
|
+
...stemConfidence !== void 0 && { stemConfidence },
|
|
71
|
+
...metadata !== void 0 && { metadata }
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const value = valueOrParams;
|
|
75
|
+
if (!kind || !position) {
|
|
76
|
+
throw new Error("createToken requires kind and position parameters");
|
|
77
|
+
}
|
|
78
|
+
if (typeof normalizedOrOptions === "string") {
|
|
79
|
+
return { value, kind, position, normalized: normalizedOrOptions };
|
|
80
|
+
}
|
|
81
|
+
if (normalizedOrOptions) {
|
|
82
|
+
const { normalized: normalized2, stem, stemConfidence, metadata } = normalizedOrOptions;
|
|
83
|
+
return {
|
|
84
|
+
value,
|
|
85
|
+
kind,
|
|
86
|
+
position,
|
|
87
|
+
...normalized2 !== void 0 && { normalized: normalized2 },
|
|
88
|
+
...stem !== void 0 && { stem },
|
|
89
|
+
...stemConfidence !== void 0 && { stemConfidence },
|
|
90
|
+
...metadata !== void 0 && { metadata }
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return { value, kind, position };
|
|
94
|
+
}
|
|
95
|
+
function isWhitespace(char) {
|
|
96
|
+
return /\s/.test(char);
|
|
97
|
+
}
|
|
98
|
+
function isSelectorStart(char) {
|
|
99
|
+
return char === "#" || char === "." || char === "[" || char === "@" || char === "*" || char === "<";
|
|
100
|
+
}
|
|
101
|
+
function isQuote(char) {
|
|
102
|
+
return char === '"' || char === "'" || char === "`" || char === "\u300C" || char === "\u300D";
|
|
103
|
+
}
|
|
104
|
+
function isDigit(char) {
|
|
105
|
+
return /\d/.test(char);
|
|
106
|
+
}
|
|
107
|
+
function isAsciiLetter(char) {
|
|
108
|
+
return /[a-zA-Z]/.test(char);
|
|
109
|
+
}
|
|
110
|
+
function isAsciiIdentifierChar(char) {
|
|
111
|
+
return /[a-zA-Z0-9_-]/.test(char);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/core/tokenization/extractors.ts
|
|
115
|
+
function extractCssSelector(input, startPos) {
|
|
116
|
+
if (startPos >= input.length) return null;
|
|
117
|
+
const char = input[startPos];
|
|
118
|
+
if (!isSelectorStart(char)) return null;
|
|
119
|
+
let pos = startPos;
|
|
120
|
+
let selector = "";
|
|
121
|
+
if (char === "#" || char === ".") {
|
|
122
|
+
selector += input[pos++];
|
|
123
|
+
while (pos < input.length && isAsciiIdentifierChar(input[pos])) {
|
|
124
|
+
selector += input[pos++];
|
|
125
|
+
}
|
|
126
|
+
if (selector.length <= 1) return null;
|
|
127
|
+
if (pos < input.length && input[pos] === "." && char === "#") {
|
|
128
|
+
const methodStart = pos + 1;
|
|
129
|
+
let methodEnd = methodStart;
|
|
130
|
+
while (methodEnd < input.length && isAsciiIdentifierChar(input[methodEnd])) {
|
|
131
|
+
methodEnd++;
|
|
132
|
+
}
|
|
133
|
+
if (methodEnd < input.length && input[methodEnd] === "(") {
|
|
134
|
+
return selector;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} else if (char === "[") {
|
|
138
|
+
let depth = 1;
|
|
139
|
+
let inQuote = false;
|
|
140
|
+
let quoteChar = null;
|
|
141
|
+
let escaped = false;
|
|
142
|
+
selector += input[pos++];
|
|
143
|
+
while (pos < input.length && depth > 0) {
|
|
144
|
+
const c = input[pos];
|
|
145
|
+
selector += c;
|
|
146
|
+
if (escaped) {
|
|
147
|
+
escaped = false;
|
|
148
|
+
} else if (c === "\\") {
|
|
149
|
+
escaped = true;
|
|
150
|
+
} else if (inQuote) {
|
|
151
|
+
if (c === quoteChar) {
|
|
152
|
+
inQuote = false;
|
|
153
|
+
quoteChar = null;
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
157
|
+
inQuote = true;
|
|
158
|
+
quoteChar = c;
|
|
159
|
+
} else if (c === "[") {
|
|
160
|
+
depth++;
|
|
161
|
+
} else if (c === "]") {
|
|
162
|
+
depth--;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
pos++;
|
|
166
|
+
}
|
|
167
|
+
if (depth !== 0) return null;
|
|
168
|
+
} else if (char === "@") {
|
|
169
|
+
selector += input[pos++];
|
|
170
|
+
while (pos < input.length && isAsciiIdentifierChar(input[pos])) {
|
|
171
|
+
selector += input[pos++];
|
|
172
|
+
}
|
|
173
|
+
if (selector.length <= 1) return null;
|
|
174
|
+
} else if (char === "*") {
|
|
175
|
+
selector += input[pos++];
|
|
176
|
+
while (pos < input.length && isAsciiIdentifierChar(input[pos])) {
|
|
177
|
+
selector += input[pos++];
|
|
178
|
+
}
|
|
179
|
+
if (selector.length <= 1) return null;
|
|
180
|
+
} else if (char === "<") {
|
|
181
|
+
selector += input[pos++];
|
|
182
|
+
if (pos >= input.length || !isAsciiLetter(input[pos])) return null;
|
|
183
|
+
while (pos < input.length && isAsciiIdentifierChar(input[pos])) {
|
|
184
|
+
selector += input[pos++];
|
|
185
|
+
}
|
|
186
|
+
while (pos < input.length) {
|
|
187
|
+
const modChar = input[pos];
|
|
188
|
+
if (modChar === ".") {
|
|
189
|
+
selector += input[pos++];
|
|
190
|
+
if (pos >= input.length || !isAsciiIdentifierChar(input[pos])) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
while (pos < input.length && isAsciiIdentifierChar(input[pos])) {
|
|
194
|
+
selector += input[pos++];
|
|
195
|
+
}
|
|
196
|
+
} else if (modChar === "#") {
|
|
197
|
+
selector += input[pos++];
|
|
198
|
+
if (pos >= input.length || !isAsciiIdentifierChar(input[pos])) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
while (pos < input.length && isAsciiIdentifierChar(input[pos])) {
|
|
202
|
+
selector += input[pos++];
|
|
203
|
+
}
|
|
204
|
+
} else if (modChar === "[") {
|
|
205
|
+
let depth = 1;
|
|
206
|
+
let inQuote = false;
|
|
207
|
+
let quoteChar = null;
|
|
208
|
+
let escaped = false;
|
|
209
|
+
selector += input[pos++];
|
|
210
|
+
while (pos < input.length && depth > 0) {
|
|
211
|
+
const c = input[pos];
|
|
212
|
+
selector += c;
|
|
213
|
+
if (escaped) {
|
|
214
|
+
escaped = false;
|
|
215
|
+
} else if (c === "\\") {
|
|
216
|
+
escaped = true;
|
|
217
|
+
} else if (inQuote) {
|
|
218
|
+
if (c === quoteChar) {
|
|
219
|
+
inQuote = false;
|
|
220
|
+
quoteChar = null;
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
224
|
+
inQuote = true;
|
|
225
|
+
quoteChar = c;
|
|
226
|
+
} else if (c === "[") {
|
|
227
|
+
depth++;
|
|
228
|
+
} else if (c === "]") {
|
|
229
|
+
depth--;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
pos++;
|
|
233
|
+
}
|
|
234
|
+
if (depth !== 0) return null;
|
|
235
|
+
} else {
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
while (pos < input.length && isWhitespace(input[pos])) {
|
|
240
|
+
selector += input[pos++];
|
|
241
|
+
}
|
|
242
|
+
if (pos < input.length && input[pos] === "/") {
|
|
243
|
+
selector += input[pos++];
|
|
244
|
+
while (pos < input.length && isWhitespace(input[pos])) {
|
|
245
|
+
selector += input[pos++];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (pos >= input.length || input[pos] !== ">") return null;
|
|
249
|
+
selector += input[pos++];
|
|
250
|
+
}
|
|
251
|
+
return selector || null;
|
|
252
|
+
}
|
|
253
|
+
function isPossessiveMarker(input, pos) {
|
|
254
|
+
if (pos >= input.length || input[pos] !== "'") return false;
|
|
255
|
+
if (pos + 1 >= input.length) return false;
|
|
256
|
+
const nextChar = input[pos + 1].toLowerCase();
|
|
257
|
+
if (nextChar !== "s") return false;
|
|
258
|
+
if (pos + 2 >= input.length) return true;
|
|
259
|
+
const afterS = input[pos + 2];
|
|
260
|
+
return isWhitespace(afterS) || afterS === "*" || !isAsciiIdentifierChar(afterS);
|
|
261
|
+
}
|
|
262
|
+
function extractStringLiteral(input, startPos) {
|
|
263
|
+
if (startPos >= input.length) return null;
|
|
264
|
+
const openQuote = input[startPos];
|
|
265
|
+
if (!isQuote(openQuote)) return null;
|
|
266
|
+
if (openQuote === "'" && isPossessiveMarker(input, startPos)) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
const closeQuoteMap = {
|
|
270
|
+
'"': '"',
|
|
271
|
+
"'": "'",
|
|
272
|
+
"`": "`",
|
|
273
|
+
"\u300C": "\u300D"
|
|
274
|
+
};
|
|
275
|
+
const closeQuote = closeQuoteMap[openQuote];
|
|
276
|
+
if (!closeQuote) return null;
|
|
277
|
+
let pos = startPos + 1;
|
|
278
|
+
let literal = openQuote;
|
|
279
|
+
let escaped = false;
|
|
280
|
+
while (pos < input.length) {
|
|
281
|
+
const char = input[pos];
|
|
282
|
+
literal += char;
|
|
283
|
+
if (escaped) {
|
|
284
|
+
escaped = false;
|
|
285
|
+
} else if (char === "\\") {
|
|
286
|
+
escaped = true;
|
|
287
|
+
} else if (char === closeQuote) {
|
|
288
|
+
return literal;
|
|
289
|
+
}
|
|
290
|
+
pos++;
|
|
291
|
+
}
|
|
292
|
+
return literal;
|
|
293
|
+
}
|
|
294
|
+
function isUrlStart(input, pos) {
|
|
295
|
+
if (pos >= input.length) return false;
|
|
296
|
+
const char = input[pos];
|
|
297
|
+
const next = input[pos + 1] || "";
|
|
298
|
+
const third = input[pos + 2] || "";
|
|
299
|
+
if (char === "/" && next !== "/" && /[a-zA-Z0-9._-]/.test(next)) {
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
if (char === "/" && next === "/" && /[a-zA-Z]/.test(third)) {
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
if (char === "." && (next === "/" || next === "." && third === "/")) {
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
const slice = input.slice(pos, pos + 8).toLowerCase();
|
|
309
|
+
if (slice.startsWith("http://") || slice.startsWith("https://")) {
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
function extractUrl(input, startPos) {
|
|
315
|
+
if (!isUrlStart(input, startPos)) return null;
|
|
316
|
+
let pos = startPos;
|
|
317
|
+
let url = "";
|
|
318
|
+
const urlChars = /[a-zA-Z0-9/:._\-?&=%@+~!$'()*,;[\]]/;
|
|
319
|
+
while (pos < input.length) {
|
|
320
|
+
const char = input[pos];
|
|
321
|
+
if (char === "#") {
|
|
322
|
+
if (url.length > 0 && /[a-zA-Z0-9/.]$/.test(url)) {
|
|
323
|
+
url += char;
|
|
324
|
+
pos++;
|
|
325
|
+
while (pos < input.length && /[a-zA-Z0-9_-]/.test(input[pos])) {
|
|
326
|
+
url += input[pos++];
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
if (urlChars.test(char)) {
|
|
332
|
+
url += char;
|
|
333
|
+
pos++;
|
|
334
|
+
} else {
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (url.length < 2) return null;
|
|
339
|
+
return url;
|
|
340
|
+
}
|
|
341
|
+
function extractNumber(input, startPos) {
|
|
342
|
+
if (startPos >= input.length) return null;
|
|
343
|
+
const char = input[startPos];
|
|
344
|
+
if (!isDigit(char) && char !== "-" && char !== "+") return null;
|
|
345
|
+
let pos = startPos;
|
|
346
|
+
let number = "";
|
|
347
|
+
if (input[pos] === "-" || input[pos] === "+") {
|
|
348
|
+
number += input[pos++];
|
|
349
|
+
}
|
|
350
|
+
if (pos >= input.length || !isDigit(input[pos])) {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
while (pos < input.length && isDigit(input[pos])) {
|
|
354
|
+
number += input[pos++];
|
|
355
|
+
}
|
|
356
|
+
if (pos < input.length && input[pos] === ".") {
|
|
357
|
+
number += input[pos++];
|
|
358
|
+
while (pos < input.length && isDigit(input[pos])) {
|
|
359
|
+
number += input[pos++];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (pos < input.length) {
|
|
363
|
+
const suffix = input.slice(pos, pos + 2);
|
|
364
|
+
if (suffix === "ms") {
|
|
365
|
+
number += "ms";
|
|
366
|
+
} else if (input[pos] === "s" || input[pos] === "m" || input[pos] === "h") {
|
|
367
|
+
number += input[pos];
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return number;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// src/core/tokenization/extractors/operator.ts
|
|
374
|
+
var DEFAULT_OPERATORS = [
|
|
375
|
+
// Three-character operators
|
|
376
|
+
"===",
|
|
377
|
+
"!==",
|
|
378
|
+
"->",
|
|
379
|
+
// Two-character operators
|
|
380
|
+
"==",
|
|
381
|
+
"!=",
|
|
382
|
+
"<=",
|
|
383
|
+
">=",
|
|
384
|
+
"&&",
|
|
385
|
+
"||",
|
|
386
|
+
"**",
|
|
387
|
+
"+=",
|
|
388
|
+
"-=",
|
|
389
|
+
"*=",
|
|
390
|
+
"/=",
|
|
391
|
+
// Single-character operators
|
|
392
|
+
"+",
|
|
393
|
+
"-",
|
|
394
|
+
"*",
|
|
395
|
+
"/",
|
|
396
|
+
"=",
|
|
397
|
+
">",
|
|
398
|
+
"<",
|
|
399
|
+
"!",
|
|
400
|
+
"&",
|
|
401
|
+
"|",
|
|
402
|
+
"%",
|
|
403
|
+
"^",
|
|
404
|
+
"~"
|
|
405
|
+
];
|
|
406
|
+
var OperatorExtractor = class {
|
|
407
|
+
constructor(operators = DEFAULT_OPERATORS) {
|
|
408
|
+
this.operators = operators;
|
|
409
|
+
this.name = "operator";
|
|
410
|
+
this.operators = [...operators].sort((a, b) => b.length - a.length);
|
|
411
|
+
}
|
|
412
|
+
canExtract(input, position) {
|
|
413
|
+
return this.operators.some((op) => input.startsWith(op, position));
|
|
414
|
+
}
|
|
415
|
+
extract(input, position) {
|
|
416
|
+
for (const op of this.operators) {
|
|
417
|
+
if (input.startsWith(op, position)) {
|
|
418
|
+
return {
|
|
419
|
+
value: op,
|
|
420
|
+
length: op.length
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// src/core/tokenization/extractors/punctuation.ts
|
|
429
|
+
var DEFAULT_PUNCTUATION = "()[]{},:;";
|
|
430
|
+
var PunctuationExtractor = class {
|
|
431
|
+
constructor(punctuation = DEFAULT_PUNCTUATION) {
|
|
432
|
+
this.punctuation = punctuation;
|
|
433
|
+
this.name = "punctuation";
|
|
434
|
+
}
|
|
435
|
+
canExtract(input, position) {
|
|
436
|
+
return this.punctuation.includes(input[position]);
|
|
437
|
+
}
|
|
438
|
+
extract(input, position) {
|
|
439
|
+
const char = input[position];
|
|
440
|
+
if (this.punctuation.includes(char)) {
|
|
441
|
+
return {
|
|
442
|
+
value: char,
|
|
443
|
+
length: 1
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// src/interfaces/value-extractor.ts
|
|
451
|
+
var StringLiteralExtractor = class {
|
|
452
|
+
constructor() {
|
|
453
|
+
this.name = "string-literal";
|
|
454
|
+
}
|
|
455
|
+
canExtract(input, position) {
|
|
456
|
+
const char = input[position];
|
|
457
|
+
return char === '"' || char === "'" || char === "`" || char === "\u201C" || // Chinese double quote open "
|
|
458
|
+
char === "\u2018";
|
|
459
|
+
}
|
|
460
|
+
extract(input, position) {
|
|
461
|
+
const quote = input[position];
|
|
462
|
+
if (quote === "\u201C") {
|
|
463
|
+
let length2 = 1;
|
|
464
|
+
while (position + length2 < input.length) {
|
|
465
|
+
if (input[position + length2] === "\u201D") {
|
|
466
|
+
length2++;
|
|
467
|
+
return { value: input.substring(position, position + length2), length: length2 };
|
|
468
|
+
}
|
|
469
|
+
length2++;
|
|
470
|
+
}
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
if (quote === "\u2018") {
|
|
474
|
+
let length2 = 1;
|
|
475
|
+
while (position + length2 < input.length) {
|
|
476
|
+
if (input[position + length2] === "\u2019") {
|
|
477
|
+
length2++;
|
|
478
|
+
return { value: input.substring(position, position + length2), length: length2 };
|
|
479
|
+
}
|
|
480
|
+
length2++;
|
|
481
|
+
}
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
let length = 1;
|
|
485
|
+
let escaped = false;
|
|
486
|
+
while (position + length < input.length) {
|
|
487
|
+
const char = input[position + length];
|
|
488
|
+
if (escaped) {
|
|
489
|
+
escaped = false;
|
|
490
|
+
length++;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (char === "\\") {
|
|
494
|
+
escaped = true;
|
|
495
|
+
length++;
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
if (char === quote) {
|
|
499
|
+
length++;
|
|
500
|
+
return {
|
|
501
|
+
value: input.substring(position, position + length),
|
|
502
|
+
length
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
length++;
|
|
506
|
+
}
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
var NumberExtractor = class {
|
|
511
|
+
constructor() {
|
|
512
|
+
this.name = "number";
|
|
513
|
+
}
|
|
514
|
+
canExtract(input, position) {
|
|
515
|
+
return /\d/.test(input[position]);
|
|
516
|
+
}
|
|
517
|
+
extract(input, position) {
|
|
518
|
+
let length = 0;
|
|
519
|
+
let hasDecimal = false;
|
|
520
|
+
while (position + length < input.length) {
|
|
521
|
+
const char = input[position + length];
|
|
522
|
+
if (/\d/.test(char)) {
|
|
523
|
+
length++;
|
|
524
|
+
} else if (char === "." && !hasDecimal) {
|
|
525
|
+
hasDecimal = true;
|
|
526
|
+
length++;
|
|
527
|
+
} else {
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
if (length === 0) return null;
|
|
532
|
+
const numValue = input.substring(position, position + length);
|
|
533
|
+
const afterNum = position + length;
|
|
534
|
+
if (afterNum < input.length) {
|
|
535
|
+
const remaining = input.slice(afterNum);
|
|
536
|
+
const cjkMultiUnits = [
|
|
537
|
+
{ pattern: "\u6BEB\u79D2", suffix: "ms" },
|
|
538
|
+
// Chinese milliseconds
|
|
539
|
+
{ pattern: "\u5206\u949F", suffix: "m" },
|
|
540
|
+
// Chinese minutes
|
|
541
|
+
{ pattern: "\u5C0F\u65F6", suffix: "h" },
|
|
542
|
+
// Chinese hours
|
|
543
|
+
{ pattern: "\u30DF\u30EA\u79D2", suffix: "ms" },
|
|
544
|
+
// Japanese milliseconds
|
|
545
|
+
{ pattern: "\u6642\u9593", suffix: "h" }
|
|
546
|
+
// Japanese hours
|
|
547
|
+
];
|
|
548
|
+
for (const unit of cjkMultiUnits) {
|
|
549
|
+
if (remaining.startsWith(unit.pattern)) {
|
|
550
|
+
return {
|
|
551
|
+
value: numValue + unit.suffix,
|
|
552
|
+
length: length + unit.pattern.length,
|
|
553
|
+
metadata: { hasTimeUnit: true }
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (remaining.startsWith("ms")) {
|
|
558
|
+
return {
|
|
559
|
+
value: numValue + "ms",
|
|
560
|
+
length: length + 2,
|
|
561
|
+
metadata: { hasTimeUnit: true }
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
const cjkSingleUnits = [
|
|
565
|
+
{ pattern: "\u79D2", suffix: "s" },
|
|
566
|
+
// CJK seconds
|
|
567
|
+
{ pattern: "\u5206", suffix: "m" }
|
|
568
|
+
// CJK minutes
|
|
569
|
+
];
|
|
570
|
+
for (const unit of cjkSingleUnits) {
|
|
571
|
+
if (remaining.startsWith(unit.pattern)) {
|
|
572
|
+
return {
|
|
573
|
+
value: numValue + unit.suffix,
|
|
574
|
+
length: length + 1,
|
|
575
|
+
metadata: { hasTimeUnit: true }
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
if (/^[smh](?![a-zA-Z])/.test(remaining)) {
|
|
580
|
+
return {
|
|
581
|
+
value: numValue + remaining[0],
|
|
582
|
+
length: length + 1,
|
|
583
|
+
metadata: { hasTimeUnit: true }
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return { value: numValue, length };
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
var IdentifierExtractor = class {
|
|
591
|
+
constructor() {
|
|
592
|
+
this.name = "identifier";
|
|
593
|
+
}
|
|
594
|
+
canExtract(input, position) {
|
|
595
|
+
return /[a-zA-Z_]/.test(input[position]);
|
|
596
|
+
}
|
|
597
|
+
extract(input, position) {
|
|
598
|
+
let length = 0;
|
|
599
|
+
while (position + length < input.length) {
|
|
600
|
+
const char = input[position + length];
|
|
601
|
+
if (/[a-zA-Z0-9_]/.test(char)) {
|
|
602
|
+
length++;
|
|
603
|
+
} else {
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
return length > 0 ? {
|
|
608
|
+
value: input.substring(position, position + length),
|
|
609
|
+
length
|
|
610
|
+
} : null;
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
var UnicodeIdentifierExtractor = class {
|
|
614
|
+
constructor() {
|
|
615
|
+
this.name = "unicode-identifier";
|
|
616
|
+
}
|
|
617
|
+
canExtract(input, position) {
|
|
618
|
+
const code = input.charCodeAt(position);
|
|
619
|
+
if (code < 128) return false;
|
|
620
|
+
return /\p{L}/u.test(input[position]);
|
|
621
|
+
}
|
|
622
|
+
extract(input, position) {
|
|
623
|
+
let length = 0;
|
|
624
|
+
while (position + length < input.length) {
|
|
625
|
+
const char = input[position + length];
|
|
626
|
+
if (/[\p{L}\p{N}\p{M}]/u.test(char)) {
|
|
627
|
+
length++;
|
|
628
|
+
} else {
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return length > 0 ? { value: input.substring(position, position + length), length } : null;
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
function isContextAwareExtractor(extractor) {
|
|
636
|
+
return "setContext" in extractor && typeof extractor.setContext === "function";
|
|
637
|
+
}
|
|
638
|
+
function createTokenizerContext(tokenizer) {
|
|
639
|
+
const ctx = {
|
|
640
|
+
language: tokenizer.language,
|
|
641
|
+
direction: tokenizer.direction,
|
|
642
|
+
lookupKeyword: tokenizer.lookupKeyword.bind(tokenizer),
|
|
643
|
+
isKeyword: tokenizer.isKeyword.bind(tokenizer),
|
|
644
|
+
isKeywordStart: tokenizer.isKeywordStart.bind(tokenizer)
|
|
645
|
+
};
|
|
646
|
+
if (tokenizer.normalizer) {
|
|
647
|
+
return { ...ctx, normalizer: tokenizer.normalizer };
|
|
648
|
+
}
|
|
649
|
+
return ctx;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// src/core/tokenization/default-extractors.ts
|
|
653
|
+
function getDefaultExtractors() {
|
|
654
|
+
return [
|
|
655
|
+
new StringLiteralExtractor(),
|
|
656
|
+
// "strings", 'strings', `strings`
|
|
657
|
+
new NumberExtractor(),
|
|
658
|
+
// 123, 45.67
|
|
659
|
+
new OperatorExtractor(),
|
|
660
|
+
// +, -, *, /, =, >, <, etc.
|
|
661
|
+
new PunctuationExtractor(),
|
|
662
|
+
// ( ) [ ] { } , : ;
|
|
663
|
+
new IdentifierExtractor(),
|
|
664
|
+
// variable_names, functionNames (ASCII)
|
|
665
|
+
new UnicodeIdentifierExtractor()
|
|
666
|
+
// CJK, Arabic, Cyrillic, etc.
|
|
667
|
+
];
|
|
668
|
+
}
|
|
669
|
+
function withDefaultExtractors(tokenizer) {
|
|
670
|
+
tokenizer.registerExtractors(getDefaultExtractors());
|
|
671
|
+
return tokenizer;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// src/core/tokenization/char-classifiers.ts
|
|
675
|
+
function createUnicodeRangeClassifier(ranges) {
|
|
676
|
+
return (char) => {
|
|
677
|
+
const code = char.charCodeAt(0);
|
|
678
|
+
return ranges.some(([start, end]) => code >= start && code <= end);
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
function combineClassifiers(...classifiers) {
|
|
682
|
+
return (char) => classifiers.some((fn) => fn(char));
|
|
683
|
+
}
|
|
684
|
+
function createLatinCharClassifiers(letterPattern) {
|
|
685
|
+
const isLetter = (char) => letterPattern.test(char);
|
|
686
|
+
const isIdentifierChar = (char) => isLetter(char) || /[0-9_-]/.test(char);
|
|
687
|
+
return { isLetter, isIdentifierChar };
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// src/core/tokenization/base-tokenizer.ts
|
|
691
|
+
var SIMPLE_TOKENIZER_OPERATOR_SET = new Set(DEFAULT_OPERATORS);
|
|
692
|
+
var _BaseTokenizer = class _BaseTokenizer {
|
|
693
|
+
constructor() {
|
|
694
|
+
/** Keywords derived from profile, sorted longest-first for greedy matching */
|
|
695
|
+
this.profileKeywords = [];
|
|
696
|
+
/** Map for O(1) keyword lookups by lowercase native word */
|
|
697
|
+
this.profileKeywordMap = /* @__PURE__ */ new Map();
|
|
698
|
+
/**
|
|
699
|
+
* Pluggable value extractors for domain-specific syntax.
|
|
700
|
+
* When registered, BaseTokenizer will use extractor-based tokenization instead of legacy methods.
|
|
701
|
+
*/
|
|
702
|
+
this.extractors = [];
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Tokenize input string to token stream.
|
|
706
|
+
* Delegates to extractor-based tokenization if extractors are registered,
|
|
707
|
+
* otherwise subclass must override this method.
|
|
708
|
+
*
|
|
709
|
+
* @param input - Input string to tokenize
|
|
710
|
+
* @returns Token stream
|
|
711
|
+
*/
|
|
712
|
+
tokenize(input) {
|
|
713
|
+
if (this.isUsingExtractors()) {
|
|
714
|
+
return this.tokenizeWithExtractors(input);
|
|
715
|
+
}
|
|
716
|
+
throw new Error(
|
|
717
|
+
`${this.constructor.name}: tokenize() not implemented and no extractors registered. Either register extractors or override tokenize() method.`
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Register a value extractor for domain-specific syntax.
|
|
722
|
+
* Extractors are tried in registration order during tokenization.
|
|
723
|
+
* Context-aware extractors automatically receive the tokenizer context.
|
|
724
|
+
*
|
|
725
|
+
* @param extractor - Value extractor to register
|
|
726
|
+
*/
|
|
727
|
+
registerExtractor(extractor) {
|
|
728
|
+
if (isContextAwareExtractor(extractor)) {
|
|
729
|
+
extractor.setContext(createTokenizerContext(this));
|
|
730
|
+
}
|
|
731
|
+
this.extractors.push(extractor);
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Register multiple value extractors at once.
|
|
735
|
+
*
|
|
736
|
+
* @param extractors - Array of value extractors to register
|
|
737
|
+
*/
|
|
738
|
+
registerExtractors(extractors) {
|
|
739
|
+
for (const extractor of extractors) {
|
|
740
|
+
this.registerExtractor(extractor);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Clear all registered extractors.
|
|
745
|
+
* Returns tokenizer to legacy mode.
|
|
746
|
+
*/
|
|
747
|
+
clearExtractors() {
|
|
748
|
+
this.extractors = [];
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Check if this tokenizer is using extractor-based tokenization.
|
|
752
|
+
* Returns true if any extractors are registered.
|
|
753
|
+
*/
|
|
754
|
+
isUsingExtractors() {
|
|
755
|
+
return this.extractors.length > 0;
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Tokenize input using registered value extractors.
|
|
759
|
+
* This is the new path - extractors handle all syntax detection.
|
|
760
|
+
*
|
|
761
|
+
* @param input - Input string to tokenize
|
|
762
|
+
* @returns Token stream
|
|
763
|
+
*/
|
|
764
|
+
tokenizeWithExtractors(input) {
|
|
765
|
+
const tokens = [];
|
|
766
|
+
let pos = 0;
|
|
767
|
+
while (pos < input.length) {
|
|
768
|
+
while (pos < input.length && isWhitespace(input[pos])) {
|
|
769
|
+
pos++;
|
|
770
|
+
}
|
|
771
|
+
if (pos >= input.length) break;
|
|
772
|
+
let extracted = false;
|
|
773
|
+
for (const extractor of this.extractors) {
|
|
774
|
+
if (extractor.canExtract(input, pos)) {
|
|
775
|
+
const result = extractor.extract(input, pos);
|
|
776
|
+
if (result) {
|
|
777
|
+
const normalized2 = result.metadata?.normalized;
|
|
778
|
+
const stem = result.metadata?.stem;
|
|
779
|
+
const stemConfidence = result.metadata?.stemConfidence;
|
|
780
|
+
const cleanMetadata = {};
|
|
781
|
+
if (result.metadata) {
|
|
782
|
+
for (const [key, value] of Object.entries(result.metadata)) {
|
|
783
|
+
if (key !== "normalized" && key !== "stem" && key !== "stemConfidence") {
|
|
784
|
+
cleanMetadata[key] = value;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
const options = {};
|
|
789
|
+
if (normalized2) options.normalized = normalized2;
|
|
790
|
+
if (stem) options.stem = stem;
|
|
791
|
+
if (stemConfidence !== void 0) options.stemConfidence = stemConfidence;
|
|
792
|
+
if (Object.keys(cleanMetadata).length > 0) options.metadata = cleanMetadata;
|
|
793
|
+
tokens.push(
|
|
794
|
+
createToken(
|
|
795
|
+
result.value,
|
|
796
|
+
this.classifyToken(result.value),
|
|
797
|
+
createPosition(pos, pos + result.length),
|
|
798
|
+
Object.keys(options).length > 0 ? options : void 0
|
|
799
|
+
)
|
|
800
|
+
);
|
|
801
|
+
pos += result.length;
|
|
802
|
+
extracted = true;
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
if (!extracted) {
|
|
808
|
+
const char = input[pos];
|
|
809
|
+
const kind = this.classifyUnknownChar(char);
|
|
810
|
+
tokens.push(createToken(char, kind, createPosition(pos, pos + 1)));
|
|
811
|
+
pos++;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return new TokenStreamImpl(tokens, this.language);
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Classify an unknown character when no extractor matches.
|
|
818
|
+
* Provides sensible defaults for common syntax.
|
|
819
|
+
*
|
|
820
|
+
* @param char - Character to classify
|
|
821
|
+
* @returns Token kind
|
|
822
|
+
*/
|
|
823
|
+
classifyUnknownChar(char) {
|
|
824
|
+
if ("()[]{},:;".includes(char)) return "punctuation";
|
|
825
|
+
if ("+-*/<>=!&|".includes(char)) return "operator";
|
|
826
|
+
return "identifier";
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Check if current position is a property access (obj.prop) vs CSS selector (.active).
|
|
830
|
+
* Property access: no whitespace before '.', previous token is identifier/keyword/selector.
|
|
831
|
+
* Also detects standalone method calls: .identifier( pattern.
|
|
832
|
+
*
|
|
833
|
+
* Returns true if '.' was emitted as an operator token and pos should advance by 1.
|
|
834
|
+
* Returns false if this is a CSS selector and should be handled by trySelector().
|
|
835
|
+
*/
|
|
836
|
+
tryPropertyAccess(input, pos, tokens) {
|
|
837
|
+
if (input[pos] !== ".") return false;
|
|
838
|
+
const lastToken = tokens[tokens.length - 1];
|
|
839
|
+
const hasWhitespaceBefore = lastToken && lastToken.position.end < pos;
|
|
840
|
+
const isPropertyAccess = lastToken && !hasWhitespaceBefore && (lastToken.kind === "identifier" || lastToken.kind === "keyword" || lastToken.kind === "selector");
|
|
841
|
+
if (isPropertyAccess) {
|
|
842
|
+
tokens.push(createToken(".", "operator", createPosition(pos, pos + 1)));
|
|
843
|
+
return true;
|
|
844
|
+
}
|
|
845
|
+
const methodStart = pos + 1;
|
|
846
|
+
let methodEnd = methodStart;
|
|
847
|
+
while (methodEnd < input.length && isAsciiIdentifierChar(input[methodEnd])) {
|
|
848
|
+
methodEnd++;
|
|
849
|
+
}
|
|
850
|
+
if (methodEnd < input.length && input[methodEnd] === "(") {
|
|
851
|
+
tokens.push(createToken(".", "operator", createPosition(pos, pos + 1)));
|
|
852
|
+
return true;
|
|
853
|
+
}
|
|
854
|
+
return false;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Initialize keyword mappings from a language profile.
|
|
858
|
+
* Builds a list of native→english mappings from:
|
|
859
|
+
* - profile.keywords (primary + alternatives)
|
|
860
|
+
* - profile.references (me, it, you, etc.)
|
|
861
|
+
* - profile.roleMarkers (into, from, with, etc.)
|
|
862
|
+
*
|
|
863
|
+
* Results are sorted longest-first for greedy matching (important for non-space languages).
|
|
864
|
+
* Extras take precedence over profile entries when there are duplicates.
|
|
865
|
+
*
|
|
866
|
+
* @param profile - Language profile containing keyword translations
|
|
867
|
+
* @param extras - Additional keyword entries to include (literals, positional, events)
|
|
868
|
+
*/
|
|
869
|
+
initializeKeywordsFromProfile(profile, extras = []) {
|
|
870
|
+
const keywordMap = /* @__PURE__ */ new Map();
|
|
871
|
+
if (profile.keywords) {
|
|
872
|
+
for (const [normalized2, translation] of Object.entries(profile.keywords)) {
|
|
873
|
+
keywordMap.set(translation.primary, {
|
|
874
|
+
native: translation.primary,
|
|
875
|
+
normalized: translation.normalized || normalized2
|
|
876
|
+
});
|
|
877
|
+
if (translation.alternatives) {
|
|
878
|
+
for (const alt of translation.alternatives) {
|
|
879
|
+
keywordMap.set(alt, {
|
|
880
|
+
native: alt,
|
|
881
|
+
normalized: translation.normalized || normalized2
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
if (profile.references) {
|
|
888
|
+
for (const [normalized2, native] of Object.entries(profile.references)) {
|
|
889
|
+
keywordMap.set(native, { native, normalized: normalized2 });
|
|
890
|
+
}
|
|
891
|
+
for (const canonical of Object.keys(profile.references)) {
|
|
892
|
+
if (!keywordMap.has(canonical)) {
|
|
893
|
+
keywordMap.set(canonical, { native: canonical, normalized: canonical });
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
if (profile.roleMarkers) {
|
|
898
|
+
for (const [role, marker] of Object.entries(profile.roleMarkers)) {
|
|
899
|
+
if (marker.primary) {
|
|
900
|
+
keywordMap.set(marker.primary, { native: marker.primary, normalized: role });
|
|
901
|
+
}
|
|
902
|
+
if (marker.alternatives) {
|
|
903
|
+
for (const alt of marker.alternatives) {
|
|
904
|
+
keywordMap.set(alt, { native: alt, normalized: role });
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
if (profile.possessive?.keywords) {
|
|
910
|
+
for (const [native, normalized2] of Object.entries(profile.possessive.keywords)) {
|
|
911
|
+
keywordMap.set(native, { native, normalized: normalized2 });
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
for (const extra of extras) {
|
|
915
|
+
keywordMap.set(extra.native, extra);
|
|
916
|
+
}
|
|
917
|
+
this.profileKeywords = Array.from(keywordMap.values()).sort(
|
|
918
|
+
(a, b) => b.native.length - a.native.length
|
|
919
|
+
);
|
|
920
|
+
this.profileKeywordMap = /* @__PURE__ */ new Map();
|
|
921
|
+
for (const keyword of this.profileKeywords) {
|
|
922
|
+
this.profileKeywordMap.set(keyword.native.toLowerCase(), keyword);
|
|
923
|
+
const normalized2 = this.removeDiacritics(keyword.native);
|
|
924
|
+
if (normalized2 !== keyword.native && !this.profileKeywordMap.has(normalized2.toLowerCase())) {
|
|
925
|
+
this.profileKeywordMap.set(normalized2.toLowerCase(), keyword);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Remove diacritical marks from a word for normalization.
|
|
931
|
+
* Primarily for Arabic (shadda, fatha, kasra, damma, sukun, etc.)
|
|
932
|
+
* but could be extended for other languages.
|
|
933
|
+
*
|
|
934
|
+
* @param word - Word to normalize
|
|
935
|
+
* @returns Word without diacritics
|
|
936
|
+
*/
|
|
937
|
+
removeDiacritics(word) {
|
|
938
|
+
return word.replace(/[\u064B-\u0652\u0670]/g, "");
|
|
939
|
+
}
|
|
940
|
+
/**
|
|
941
|
+
* Try to match a keyword from profile at the current position.
|
|
942
|
+
* Uses longest-first greedy matching (important for non-space languages).
|
|
943
|
+
*
|
|
944
|
+
* @param input - Input string
|
|
945
|
+
* @param pos - Current position
|
|
946
|
+
* @returns Token if matched, null otherwise
|
|
947
|
+
*/
|
|
948
|
+
tryProfileKeyword(input, pos) {
|
|
949
|
+
for (const entry of this.profileKeywords) {
|
|
950
|
+
if (input.slice(pos).startsWith(entry.native)) {
|
|
951
|
+
return createToken(
|
|
952
|
+
entry.native,
|
|
953
|
+
"keyword",
|
|
954
|
+
createPosition(pos, pos + entry.native.length),
|
|
955
|
+
entry.normalized
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
return null;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* Check if the remaining input starts with any known keyword.
|
|
963
|
+
* Useful for non-space languages to detect word boundaries.
|
|
964
|
+
*
|
|
965
|
+
* @param input - Input string
|
|
966
|
+
* @param pos - Current position
|
|
967
|
+
* @returns true if a keyword starts at this position
|
|
968
|
+
*/
|
|
969
|
+
isKeywordStart(input, pos) {
|
|
970
|
+
const remaining = input.slice(pos);
|
|
971
|
+
return this.profileKeywords.some((entry) => remaining.startsWith(entry.native));
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* Look up a keyword by native word (case-insensitive).
|
|
975
|
+
* O(1) lookup using the keyword map.
|
|
976
|
+
*
|
|
977
|
+
* @param native - Native word to look up
|
|
978
|
+
* @returns KeywordEntry if found, undefined otherwise
|
|
979
|
+
*/
|
|
980
|
+
lookupKeyword(native) {
|
|
981
|
+
return this.profileKeywordMap.get(native.toLowerCase());
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Check if a word is a known keyword (case-insensitive).
|
|
985
|
+
* O(1) lookup using the keyword map.
|
|
986
|
+
*
|
|
987
|
+
* @param native - Native word to check
|
|
988
|
+
* @returns true if the word is a keyword
|
|
989
|
+
*/
|
|
990
|
+
isKeyword(native) {
|
|
991
|
+
return this.profileKeywordMap.has(native.toLowerCase());
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Set the morphological normalizer for this tokenizer.
|
|
995
|
+
*/
|
|
996
|
+
setNormalizer(normalizer) {
|
|
997
|
+
this.normalizer = normalizer;
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Try to normalize a word using the morphological normalizer.
|
|
1001
|
+
* Returns null if no normalizer is set or normalization fails.
|
|
1002
|
+
*
|
|
1003
|
+
* Note: We don't check isNormalizable() here because the individual tokenizers
|
|
1004
|
+
* historically called normalize() directly without that check. The normalize()
|
|
1005
|
+
* method itself handles returning noChange() for words that can't be normalized.
|
|
1006
|
+
*/
|
|
1007
|
+
tryNormalize(word) {
|
|
1008
|
+
if (!this.normalizer) return null;
|
|
1009
|
+
const result = this.normalizer.normalize(word);
|
|
1010
|
+
if (result.stem !== word && result.confidence >= 0.7) {
|
|
1011
|
+
return result;
|
|
1012
|
+
}
|
|
1013
|
+
return null;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Try morphological normalization and keyword lookup.
|
|
1017
|
+
*
|
|
1018
|
+
* If the word can be normalized to a stem that matches a known keyword,
|
|
1019
|
+
* returns a keyword token with morphological metadata (stem, stemConfidence).
|
|
1020
|
+
*
|
|
1021
|
+
* This is the common pattern for handling conjugated verbs across languages:
|
|
1022
|
+
* 1. Normalize the word (e.g., "toggled" → "toggle")
|
|
1023
|
+
* 2. Look up the stem in the keyword map
|
|
1024
|
+
* 3. Create a token with both the original form and stem metadata
|
|
1025
|
+
*
|
|
1026
|
+
* @param word - The word to normalize and look up
|
|
1027
|
+
* @param startPos - Start position for the token
|
|
1028
|
+
* @param endPos - End position for the token
|
|
1029
|
+
* @returns Token if stem matches a keyword, null otherwise
|
|
1030
|
+
*/
|
|
1031
|
+
tryMorphKeywordMatch(word, startPos, endPos) {
|
|
1032
|
+
const result = this.tryNormalize(word);
|
|
1033
|
+
if (!result) return null;
|
|
1034
|
+
const stemEntry = this.lookupKeyword(result.stem);
|
|
1035
|
+
if (!stemEntry) return null;
|
|
1036
|
+
const tokenOptions = {
|
|
1037
|
+
normalized: stemEntry.normalized,
|
|
1038
|
+
stem: result.stem,
|
|
1039
|
+
stemConfidence: result.confidence
|
|
1040
|
+
};
|
|
1041
|
+
return createToken(word, "keyword", createPosition(startPos, endPos), tokenOptions);
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Try to extract a CSS selector at the current position.
|
|
1045
|
+
*/
|
|
1046
|
+
trySelector(input, pos) {
|
|
1047
|
+
const selector = extractCssSelector(input, pos);
|
|
1048
|
+
if (selector) {
|
|
1049
|
+
return createToken(selector, "selector", createPosition(pos, pos + selector.length));
|
|
1050
|
+
}
|
|
1051
|
+
return null;
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Try to extract an event modifier at the current position.
|
|
1055
|
+
* Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
|
|
1056
|
+
*/
|
|
1057
|
+
tryEventModifier(input, pos) {
|
|
1058
|
+
if (input[pos] !== ".") {
|
|
1059
|
+
return null;
|
|
1060
|
+
}
|
|
1061
|
+
const match = input.slice(pos).match(/^\.(?:once|debounce|throttle|queue)(?:\(([^)]+)\))?(?:\s|$|\.)/);
|
|
1062
|
+
if (!match) {
|
|
1063
|
+
return null;
|
|
1064
|
+
}
|
|
1065
|
+
const fullMatch = match[0].replace(/(\s|\.)$/, "");
|
|
1066
|
+
const modifierName = fullMatch.slice(1).split("(")[0];
|
|
1067
|
+
const value = match[1];
|
|
1068
|
+
const token = createToken(
|
|
1069
|
+
fullMatch,
|
|
1070
|
+
"event-modifier",
|
|
1071
|
+
createPosition(pos, pos + fullMatch.length)
|
|
1072
|
+
);
|
|
1073
|
+
return {
|
|
1074
|
+
...token,
|
|
1075
|
+
metadata: {
|
|
1076
|
+
modifierName,
|
|
1077
|
+
value: value ? modifierName === "queue" ? value : parseInt(value, 10) : void 0
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Try to extract a string literal at the current position.
|
|
1083
|
+
*/
|
|
1084
|
+
tryString(input, pos) {
|
|
1085
|
+
const literal = extractStringLiteral(input, pos);
|
|
1086
|
+
if (literal) {
|
|
1087
|
+
return createToken(literal, "literal", createPosition(pos, pos + literal.length));
|
|
1088
|
+
}
|
|
1089
|
+
return null;
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* Try to extract a number at the current position.
|
|
1093
|
+
*/
|
|
1094
|
+
tryNumber(input, pos) {
|
|
1095
|
+
const number = extractNumber(input, pos);
|
|
1096
|
+
if (number) {
|
|
1097
|
+
return createToken(number, "literal", createPosition(pos, pos + number.length));
|
|
1098
|
+
}
|
|
1099
|
+
return null;
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Try to match a time unit from a list of patterns.
|
|
1103
|
+
*
|
|
1104
|
+
* @param input - Input string
|
|
1105
|
+
* @param pos - Position after the number
|
|
1106
|
+
* @param timeUnits - Array of time unit mappings (native pattern → standard suffix)
|
|
1107
|
+
* @param skipWhitespace - Whether to skip whitespace before time unit (default: false)
|
|
1108
|
+
* @returns Object with matched suffix and new position, or null if no match
|
|
1109
|
+
*/
|
|
1110
|
+
tryMatchTimeUnit(input, pos, timeUnits, skipWhitespace = false) {
|
|
1111
|
+
let unitPos = pos;
|
|
1112
|
+
if (skipWhitespace) {
|
|
1113
|
+
while (unitPos < input.length && isWhitespace(input[unitPos])) {
|
|
1114
|
+
unitPos++;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
const remaining = input.slice(unitPos);
|
|
1118
|
+
for (const unit of timeUnits) {
|
|
1119
|
+
const candidate = remaining.slice(0, unit.length);
|
|
1120
|
+
const matches = unit.caseInsensitive ? candidate.toLowerCase() === unit.pattern.toLowerCase() : candidate === unit.pattern;
|
|
1121
|
+
if (matches) {
|
|
1122
|
+
if (unit.notFollowedBy) {
|
|
1123
|
+
const nextChar = remaining[unit.length] || "";
|
|
1124
|
+
if (nextChar === unit.notFollowedBy) continue;
|
|
1125
|
+
}
|
|
1126
|
+
if (unit.checkBoundary) {
|
|
1127
|
+
const nextChar = remaining[unit.length] || "";
|
|
1128
|
+
if (isAsciiIdentifierChar(nextChar)) continue;
|
|
1129
|
+
}
|
|
1130
|
+
return { suffix: unit.suffix, endPos: unitPos + unit.length };
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
return null;
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Parse a base number (sign, integer, decimal) without time units.
|
|
1137
|
+
* Returns the number string and end position.
|
|
1138
|
+
*
|
|
1139
|
+
* @param input - Input string
|
|
1140
|
+
* @param startPos - Start position
|
|
1141
|
+
* @param allowSign - Whether to allow +/- sign (default: true)
|
|
1142
|
+
* @returns Object with number string and end position, or null
|
|
1143
|
+
*/
|
|
1144
|
+
parseBaseNumber(input, startPos, allowSign = true) {
|
|
1145
|
+
let pos = startPos;
|
|
1146
|
+
let number = "";
|
|
1147
|
+
if (allowSign && (input[pos] === "-" || input[pos] === "+")) {
|
|
1148
|
+
number += input[pos++];
|
|
1149
|
+
}
|
|
1150
|
+
if (pos >= input.length || !isDigit(input[pos])) {
|
|
1151
|
+
return null;
|
|
1152
|
+
}
|
|
1153
|
+
while (pos < input.length && isDigit(input[pos])) {
|
|
1154
|
+
number += input[pos++];
|
|
1155
|
+
}
|
|
1156
|
+
if (pos < input.length && input[pos] === ".") {
|
|
1157
|
+
number += input[pos++];
|
|
1158
|
+
while (pos < input.length && isDigit(input[pos])) {
|
|
1159
|
+
number += input[pos++];
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
if (!number || number === "-" || number === "+") return null;
|
|
1163
|
+
return { number, endPos: pos };
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Try to extract a number with native language time units.
|
|
1167
|
+
*
|
|
1168
|
+
* This is a template method that handles the common pattern:
|
|
1169
|
+
* 1. Parse the base number (sign, integer, decimal)
|
|
1170
|
+
* 2. Try to match native language time units
|
|
1171
|
+
* 3. Fall back to standard time units (ms, s, m, h)
|
|
1172
|
+
*
|
|
1173
|
+
* @param input - Input string
|
|
1174
|
+
* @param pos - Start position
|
|
1175
|
+
* @param nativeTimeUnits - Language-specific time unit mappings
|
|
1176
|
+
* @param options - Configuration options
|
|
1177
|
+
* @returns Token if number found, null otherwise
|
|
1178
|
+
*/
|
|
1179
|
+
tryNumberWithTimeUnits(input, pos, nativeTimeUnits, options = {}) {
|
|
1180
|
+
const { allowSign = true, skipWhitespace = false } = options;
|
|
1181
|
+
const baseResult = this.parseBaseNumber(input, pos, allowSign);
|
|
1182
|
+
if (!baseResult) return null;
|
|
1183
|
+
let { number, endPos } = baseResult;
|
|
1184
|
+
const allUnits = [...nativeTimeUnits, ..._BaseTokenizer.STANDARD_TIME_UNITS];
|
|
1185
|
+
const timeMatch = this.tryMatchTimeUnit(input, endPos, allUnits, skipWhitespace);
|
|
1186
|
+
if (timeMatch) {
|
|
1187
|
+
number += timeMatch.suffix;
|
|
1188
|
+
endPos = timeMatch.endPos;
|
|
1189
|
+
}
|
|
1190
|
+
return createToken(number, "literal", createPosition(pos, endPos));
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Try to extract a URL at the current position.
|
|
1194
|
+
* Handles /path, ./path, ../path, //domain.com, http://, https://
|
|
1195
|
+
*/
|
|
1196
|
+
tryUrl(input, pos) {
|
|
1197
|
+
const url = extractUrl(input, pos);
|
|
1198
|
+
if (url) {
|
|
1199
|
+
return createToken(url, "url", createPosition(pos, pos + url.length));
|
|
1200
|
+
}
|
|
1201
|
+
return null;
|
|
1202
|
+
}
|
|
1203
|
+
/**
|
|
1204
|
+
* Try to extract a variable reference (:varname) at the current position.
|
|
1205
|
+
* In hyperscript, :x refers to a local variable named x.
|
|
1206
|
+
*/
|
|
1207
|
+
tryVariableRef(input, pos) {
|
|
1208
|
+
if (input[pos] !== ":") return null;
|
|
1209
|
+
if (pos + 1 >= input.length) return null;
|
|
1210
|
+
if (!isAsciiIdentifierChar(input[pos + 1])) return null;
|
|
1211
|
+
let endPos = pos + 1;
|
|
1212
|
+
while (endPos < input.length && isAsciiIdentifierChar(input[endPos])) {
|
|
1213
|
+
endPos++;
|
|
1214
|
+
}
|
|
1215
|
+
const varRef = input.slice(pos, endPos);
|
|
1216
|
+
return createToken(varRef, "identifier", createPosition(pos, endPos));
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Try to extract an operator or punctuation token at the current position.
|
|
1220
|
+
* Handles two-character operators (==, !=, etc.) and single-character operators.
|
|
1221
|
+
*/
|
|
1222
|
+
tryOperator(input, pos) {
|
|
1223
|
+
const twoChar = input.slice(pos, pos + 2);
|
|
1224
|
+
if (["==", "!=", "<=", ">=", "&&", "||", "->"].includes(twoChar)) {
|
|
1225
|
+
return createToken(twoChar, "operator", createPosition(pos, pos + 2));
|
|
1226
|
+
}
|
|
1227
|
+
const oneChar = input[pos];
|
|
1228
|
+
if (["<", ">", "!", "+", "-", "*", "/", "="].includes(oneChar)) {
|
|
1229
|
+
return createToken(oneChar, "operator", createPosition(pos, pos + 1));
|
|
1230
|
+
}
|
|
1231
|
+
if (["(", ")", "{", "}", ",", ";", ":"].includes(oneChar)) {
|
|
1232
|
+
return createToken(oneChar, "punctuation", createPosition(pos, pos + 1));
|
|
1233
|
+
}
|
|
1234
|
+
return null;
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Try to match a multi-character particle from a list.
|
|
1238
|
+
*
|
|
1239
|
+
* Used by languages like Japanese, Korean, and Chinese that have
|
|
1240
|
+
* multi-character particles (e.g., Japanese から, まで, より).
|
|
1241
|
+
*
|
|
1242
|
+
* @param input - Input string
|
|
1243
|
+
* @param pos - Current position
|
|
1244
|
+
* @param particles - Array of multi-character particles to match
|
|
1245
|
+
* @returns Token if matched, null otherwise
|
|
1246
|
+
*/
|
|
1247
|
+
tryMultiCharParticle(input, pos, particles) {
|
|
1248
|
+
for (const particle of particles) {
|
|
1249
|
+
if (input.slice(pos, pos + particle.length) === particle) {
|
|
1250
|
+
return createToken(particle, "particle", createPosition(pos, pos + particle.length));
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
return null;
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
/**
|
|
1257
|
+
* Configuration for native language time units.
|
|
1258
|
+
* Maps patterns to their standard suffix (ms, s, m, h).
|
|
1259
|
+
*/
|
|
1260
|
+
_BaseTokenizer.STANDARD_TIME_UNITS = [
|
|
1261
|
+
{ pattern: "ms", suffix: "ms", length: 2 },
|
|
1262
|
+
{ pattern: "s", suffix: "s", length: 1, checkBoundary: true },
|
|
1263
|
+
{ pattern: "m", suffix: "m", length: 1, checkBoundary: true, notFollowedBy: "s" },
|
|
1264
|
+
{ pattern: "h", suffix: "h", length: 1, checkBoundary: true }
|
|
1265
|
+
];
|
|
1266
|
+
var BaseTokenizer = _BaseTokenizer;
|
|
1267
|
+
function createSimpleTokenizer(config) {
|
|
1268
|
+
const {
|
|
1269
|
+
language,
|
|
1270
|
+
direction = "ltr",
|
|
1271
|
+
keywords,
|
|
1272
|
+
keywordExtras,
|
|
1273
|
+
keywordProfile,
|
|
1274
|
+
includeOperators = false,
|
|
1275
|
+
caseInsensitive = true,
|
|
1276
|
+
customExtractors
|
|
1277
|
+
} = config;
|
|
1278
|
+
const keywordSet = new Set(caseInsensitive ? keywords.map((k) => k.toLowerCase()) : keywords);
|
|
1279
|
+
class SimpleTokenizer extends BaseTokenizer {
|
|
1280
|
+
constructor() {
|
|
1281
|
+
super();
|
|
1282
|
+
this.language = language;
|
|
1283
|
+
this.direction = direction;
|
|
1284
|
+
if (customExtractors) {
|
|
1285
|
+
this.registerExtractors(customExtractors);
|
|
1286
|
+
}
|
|
1287
|
+
this.registerExtractors(getDefaultExtractors());
|
|
1288
|
+
if (keywordProfile) {
|
|
1289
|
+
this.initializeKeywordsFromProfile(keywordProfile, keywordExtras);
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
classifyToken(token) {
|
|
1293
|
+
const lookup = caseInsensitive ? token.toLowerCase() : token;
|
|
1294
|
+
if (keywordSet.has(lookup)) return "keyword";
|
|
1295
|
+
if (this.isKeyword(token)) return "keyword";
|
|
1296
|
+
if (/^\d/.test(token)) return "literal";
|
|
1297
|
+
if (/^['"]/.test(token)) return "literal";
|
|
1298
|
+
if (includeOperators && SIMPLE_TOKENIZER_OPERATOR_SET.has(token)) return "operator";
|
|
1299
|
+
return "identifier";
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
return new SimpleTokenizer();
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
// src/core/tokenization/morphology/types.ts
|
|
1306
|
+
function noChange(word) {
|
|
1307
|
+
return { stem: word, confidence: 1 };
|
|
1308
|
+
}
|
|
1309
|
+
function normalized(stem, confidence, metadata) {
|
|
1310
|
+
if (metadata) {
|
|
1311
|
+
return { stem, confidence, metadata };
|
|
1312
|
+
}
|
|
1313
|
+
return { stem, confidence };
|
|
1314
|
+
}
|
|
1315
|
+
export {
|
|
1316
|
+
BaseTokenizer,
|
|
1317
|
+
DEFAULT_OPERATORS,
|
|
1318
|
+
DEFAULT_PUNCTUATION,
|
|
1319
|
+
OperatorExtractor,
|
|
1320
|
+
PunctuationExtractor,
|
|
1321
|
+
TokenStreamImpl,
|
|
1322
|
+
combineClassifiers,
|
|
1323
|
+
createLatinCharClassifiers,
|
|
1324
|
+
createPosition,
|
|
1325
|
+
createSimpleTokenizer,
|
|
1326
|
+
createToken,
|
|
1327
|
+
createUnicodeRangeClassifier,
|
|
1328
|
+
extractCssSelector,
|
|
1329
|
+
extractNumber,
|
|
1330
|
+
extractStringLiteral,
|
|
1331
|
+
extractUrl,
|
|
1332
|
+
getDefaultExtractors,
|
|
1333
|
+
isAsciiIdentifierChar,
|
|
1334
|
+
isAsciiLetter,
|
|
1335
|
+
isDigit,
|
|
1336
|
+
isPossessiveMarker,
|
|
1337
|
+
isQuote,
|
|
1338
|
+
isSelectorStart,
|
|
1339
|
+
isUrlStart,
|
|
1340
|
+
isWhitespace,
|
|
1341
|
+
noChange,
|
|
1342
|
+
normalized,
|
|
1343
|
+
withDefaultExtractors
|
|
1344
|
+
};
|
|
1345
|
+
//# sourceMappingURL=index.js.map
|