astwerk 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +372 -2
- package/dist/index.d.ts +383 -0
- package/dist/index.js +1767 -0
- package/package.json +52 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1767 @@
|
|
|
1
|
+
// src/diagnostics.ts
|
|
2
|
+
const DIAGNOSTIC_CODE = {
|
|
3
|
+
unknownField: "unknownField",
|
|
4
|
+
invalidValue: "invalidValue",
|
|
5
|
+
unknownToken: "unknownToken",
|
|
6
|
+
unsupportedOperator: "unsupportedOperator",
|
|
7
|
+
negatedGroup: "negatedGroup",
|
|
8
|
+
unclosedQuote: "unclosedQuote",
|
|
9
|
+
unbalancedParenthesis: "unbalancedParenthesis",
|
|
10
|
+
nestingTooDeep: "nestingTooDeep",
|
|
11
|
+
emptyRange: "emptyRange"
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* An `error` marks a part that filters nothing. A `warning` marks text that is read and filters, but maybe not as
|
|
15
|
+
* meant.
|
|
16
|
+
*/
|
|
17
|
+
const DIAGNOSTIC_SEVERITY = {
|
|
18
|
+
error: "error",
|
|
19
|
+
warning: "warning"
|
|
20
|
+
};
|
|
21
|
+
const SEVERITY_BY_CODE = {
|
|
22
|
+
unknownField: DIAGNOSTIC_SEVERITY.error,
|
|
23
|
+
invalidValue: DIAGNOSTIC_SEVERITY.error,
|
|
24
|
+
unknownToken: DIAGNOSTIC_SEVERITY.error,
|
|
25
|
+
unsupportedOperator: DIAGNOSTIC_SEVERITY.error,
|
|
26
|
+
negatedGroup: DIAGNOSTIC_SEVERITY.error,
|
|
27
|
+
unclosedQuote: DIAGNOSTIC_SEVERITY.warning,
|
|
28
|
+
unbalancedParenthesis: DIAGNOSTIC_SEVERITY.warning,
|
|
29
|
+
nestingTooDeep: DIAGNOSTIC_SEVERITY.warning,
|
|
30
|
+
emptyRange: DIAGNOSTIC_SEVERITY.warning
|
|
31
|
+
};
|
|
32
|
+
function createDiagnostic(code, span) {
|
|
33
|
+
return {
|
|
34
|
+
code,
|
|
35
|
+
severity: SEVERITY_BY_CODE[code],
|
|
36
|
+
from: span.from,
|
|
37
|
+
to: span.to
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
// src/syntax/highlight-kind.ts
|
|
41
|
+
const HIGHLIGHT_KIND = {
|
|
42
|
+
key: "key",
|
|
43
|
+
value: "value",
|
|
44
|
+
dynamic: "dynamic",
|
|
45
|
+
text: "text",
|
|
46
|
+
keyword: "keyword",
|
|
47
|
+
negation: "negation",
|
|
48
|
+
comparison: "comparison",
|
|
49
|
+
range: "range",
|
|
50
|
+
separator: "separator",
|
|
51
|
+
paren: "paren"
|
|
52
|
+
};
|
|
53
|
+
// src/syntax/syntax-tree.ts
|
|
54
|
+
const COMPARE_OPERATORS = [
|
|
55
|
+
">=",
|
|
56
|
+
"<=",
|
|
57
|
+
">",
|
|
58
|
+
"<"
|
|
59
|
+
];
|
|
60
|
+
// src/syntax/parse.ts
|
|
61
|
+
const MAX_DEPTH = 32;
|
|
62
|
+
const WHITESPACE = /\s/;
|
|
63
|
+
const RANGE_SEPARATOR$1 = "..";
|
|
64
|
+
/** Reads any text into a tree. Broken text gives diagnostics, never an exception. */
|
|
65
|
+
function parse(text) {
|
|
66
|
+
const { tree, diagnostics } = scan(text);
|
|
67
|
+
return {
|
|
68
|
+
tree,
|
|
69
|
+
diagnostics
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function scan(text) {
|
|
73
|
+
const state = {
|
|
74
|
+
text,
|
|
75
|
+
position: 0,
|
|
76
|
+
diagnostics: [],
|
|
77
|
+
highlights: []
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
tree: parseOr(state, 0),
|
|
81
|
+
diagnostics: state.diagnostics,
|
|
82
|
+
highlights: state.highlights
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function parseOr(state, depth) {
|
|
86
|
+
const from = state.position;
|
|
87
|
+
const operands = [parseAnd(state, depth)];
|
|
88
|
+
while (isKeywordAt(state, "OR")) {
|
|
89
|
+
mark(state, HIGHLIGHT_KIND.keyword, 2);
|
|
90
|
+
operands.push(parseAnd(state, depth));
|
|
91
|
+
}
|
|
92
|
+
return combine("or", operands, {
|
|
93
|
+
from,
|
|
94
|
+
to: state.position
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function parseAnd(state, depth) {
|
|
98
|
+
const from = state.position;
|
|
99
|
+
const children = [];
|
|
100
|
+
skipWhitespace(state);
|
|
101
|
+
while (!isAtEnd(state) && !isKeywordAt(state, "OR") && !closesGroup(state, depth)) {
|
|
102
|
+
readAndOperand(state, depth, children);
|
|
103
|
+
skipWhitespace(state);
|
|
104
|
+
}
|
|
105
|
+
return combine("and", children, {
|
|
106
|
+
from,
|
|
107
|
+
to: state.position
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function readAndOperand(state, depth, children) {
|
|
111
|
+
if (currentChar(state) == ")") {
|
|
112
|
+
report$1(state, DIAGNOSTIC_CODE.unbalancedParenthesis, {
|
|
113
|
+
from: state.position,
|
|
114
|
+
to: state.position + 1
|
|
115
|
+
});
|
|
116
|
+
mark(state, HIGHLIGHT_KIND.paren, 1);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (isKeywordAt(state, "AND")) {
|
|
120
|
+
mark(state, HIGHLIGHT_KIND.keyword, 3);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const node = parseNegation(state, depth);
|
|
124
|
+
if (node != void 0) children.push(node);
|
|
125
|
+
}
|
|
126
|
+
function parseNegation(state, depth) {
|
|
127
|
+
const keywordStarts = [];
|
|
128
|
+
while (isKeywordAt(state, "NOT")) {
|
|
129
|
+
keywordStarts.push(state.position);
|
|
130
|
+
mark(state, HIGHLIGHT_KIND.keyword, 3);
|
|
131
|
+
skipWhitespace(state);
|
|
132
|
+
}
|
|
133
|
+
if (!startsOperand(state)) return void 0;
|
|
134
|
+
const operand = parseSign(state, depth);
|
|
135
|
+
return keywordStarts.length % 2 == 0 ? operand : negate(state, keywordStarts[0], operand);
|
|
136
|
+
}
|
|
137
|
+
function parseSign(state, depth) {
|
|
138
|
+
const from = state.position;
|
|
139
|
+
if (currentChar(state) != "-" || !startsOperandAt(state, state.position + 1)) return parseOperand(state, depth);
|
|
140
|
+
mark(state, HIGHLIGHT_KIND.negation, 1);
|
|
141
|
+
return negate(state, from, parseOperand(state, depth));
|
|
142
|
+
}
|
|
143
|
+
function negate(state, from, child) {
|
|
144
|
+
const span = {
|
|
145
|
+
from,
|
|
146
|
+
to: state.position
|
|
147
|
+
};
|
|
148
|
+
if (isGroup(child)) report$1(state, DIAGNOSTIC_CODE.negatedGroup, span);
|
|
149
|
+
return {
|
|
150
|
+
type: "not",
|
|
151
|
+
child,
|
|
152
|
+
span
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function parseOperand(state, depth) {
|
|
156
|
+
return currentChar(state) == "(" ? parseGroup(state, depth) : parseWord(state);
|
|
157
|
+
}
|
|
158
|
+
function parseGroup(state, depth) {
|
|
159
|
+
const open = {
|
|
160
|
+
from: state.position,
|
|
161
|
+
to: state.position + 1
|
|
162
|
+
};
|
|
163
|
+
mark(state, HIGHLIGHT_KIND.paren, 1);
|
|
164
|
+
if (depth >= MAX_DEPTH) {
|
|
165
|
+
report$1(state, DIAGNOSTIC_CODE.nestingTooDeep, open);
|
|
166
|
+
return emptyGroup(open);
|
|
167
|
+
}
|
|
168
|
+
const inner = parseOr(state, depth + 1);
|
|
169
|
+
if (currentChar(state) == ")") mark(state, HIGHLIGHT_KIND.paren, 1);
|
|
170
|
+
else report$1(state, DIAGNOSTIC_CODE.unbalancedParenthesis, open);
|
|
171
|
+
return isGroup(inner) ? {
|
|
172
|
+
...inner,
|
|
173
|
+
span: {
|
|
174
|
+
from: open.from,
|
|
175
|
+
to: state.position
|
|
176
|
+
}
|
|
177
|
+
} : inner;
|
|
178
|
+
}
|
|
179
|
+
function parseWord(state) {
|
|
180
|
+
const from = state.position;
|
|
181
|
+
const key = readChunk(state, isKeyEnd);
|
|
182
|
+
if (currentChar(state) == ":" && state.position > from) return parseTerm(state, from, key);
|
|
183
|
+
state.position = from;
|
|
184
|
+
const text = readChunk(state, isWordEnd);
|
|
185
|
+
const span = {
|
|
186
|
+
from,
|
|
187
|
+
to: state.position
|
|
188
|
+
};
|
|
189
|
+
reportUnclosedQuote(state, text);
|
|
190
|
+
state.highlights.push({
|
|
191
|
+
...span,
|
|
192
|
+
kind: HIGHLIGHT_KIND.text
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
type: "text",
|
|
196
|
+
value: text.value,
|
|
197
|
+
quoted: text.quoted,
|
|
198
|
+
span
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function parseTerm(state, from, key) {
|
|
202
|
+
const keySpan = {
|
|
203
|
+
from,
|
|
204
|
+
to: state.position
|
|
205
|
+
};
|
|
206
|
+
state.highlights.push({
|
|
207
|
+
...keySpan,
|
|
208
|
+
kind: HIGHLIGHT_KIND.key
|
|
209
|
+
});
|
|
210
|
+
mark(state, HIGHLIGHT_KIND.separator, 1);
|
|
211
|
+
const values = [parseValue(state)];
|
|
212
|
+
while (currentChar(state) == ",") {
|
|
213
|
+
mark(state, HIGHLIGHT_KIND.separator, 1);
|
|
214
|
+
values.push(parseValue(state));
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
type: "term",
|
|
218
|
+
key: key.value,
|
|
219
|
+
keySpan,
|
|
220
|
+
values,
|
|
221
|
+
span: {
|
|
222
|
+
from,
|
|
223
|
+
to: state.position
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
function parseValue(state) {
|
|
228
|
+
const from = state.position;
|
|
229
|
+
const operator = COMPARE_OPERATORS.find((candidate) => state.text.startsWith(candidate, from));
|
|
230
|
+
if (operator != void 0) {
|
|
231
|
+
mark(state, HIGHLIGHT_KIND.comparison, operator.length);
|
|
232
|
+
const operand = readValueChunk(state, isValueEnd);
|
|
233
|
+
return {
|
|
234
|
+
kind: "compare",
|
|
235
|
+
operator,
|
|
236
|
+
raw: operand.value,
|
|
237
|
+
quoted: operand.quoted,
|
|
238
|
+
span: {
|
|
239
|
+
from,
|
|
240
|
+
to: state.position
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const min = readValueChunk(state, isBoundEnd);
|
|
245
|
+
if (!state.text.startsWith(RANGE_SEPARATOR$1, state.position)) return {
|
|
246
|
+
kind: "equal",
|
|
247
|
+
raw: min.value,
|
|
248
|
+
quoted: min.quoted,
|
|
249
|
+
span: {
|
|
250
|
+
from,
|
|
251
|
+
to: state.position
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const separator = {
|
|
255
|
+
from: state.position,
|
|
256
|
+
to: state.position + 2
|
|
257
|
+
};
|
|
258
|
+
mark(state, HIGHLIGHT_KIND.range, 2);
|
|
259
|
+
const max = readValueChunk(state, isValueEnd);
|
|
260
|
+
return {
|
|
261
|
+
kind: "range",
|
|
262
|
+
min: toBound(min),
|
|
263
|
+
max: toBound(max),
|
|
264
|
+
separator,
|
|
265
|
+
span: {
|
|
266
|
+
from,
|
|
267
|
+
to: state.position
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function readValueChunk(state, isEnd) {
|
|
272
|
+
const from = state.position;
|
|
273
|
+
const chunk = readChunk(state, isEnd);
|
|
274
|
+
reportUnclosedQuote(state, chunk);
|
|
275
|
+
if (state.position > from) state.highlights.push({
|
|
276
|
+
from,
|
|
277
|
+
to: state.position,
|
|
278
|
+
kind: valueHighlight(chunk)
|
|
279
|
+
});
|
|
280
|
+
return chunk;
|
|
281
|
+
}
|
|
282
|
+
function valueHighlight(chunk) {
|
|
283
|
+
return !chunk.quoted && chunk.value.startsWith("@") ? HIGHLIGHT_KIND.dynamic : HIGHLIGHT_KIND.value;
|
|
284
|
+
}
|
|
285
|
+
function toBound(chunk) {
|
|
286
|
+
if (chunk.value == "" || chunk.value == "*" && !chunk.quoted) return void 0;
|
|
287
|
+
return {
|
|
288
|
+
raw: chunk.value,
|
|
289
|
+
quoted: chunk.quoted
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function readChunk(state, isEnd) {
|
|
293
|
+
const start = state.position;
|
|
294
|
+
const chunk = {
|
|
295
|
+
value: "",
|
|
296
|
+
quoted: false
|
|
297
|
+
};
|
|
298
|
+
while (!isAtEnd(state) && !isEnd(state)) {
|
|
299
|
+
const char = state.text[state.position];
|
|
300
|
+
if (char == "\"" || char == "'" && state.position == start) {
|
|
301
|
+
const quoted = readQuoted(state, char);
|
|
302
|
+
chunk.value += quoted.value;
|
|
303
|
+
chunk.quoted = true;
|
|
304
|
+
chunk.unclosedQuote ??= quoted.unclosedQuote;
|
|
305
|
+
} else {
|
|
306
|
+
chunk.value += char;
|
|
307
|
+
state.position++;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return chunk;
|
|
311
|
+
}
|
|
312
|
+
function readQuoted(state, quote) {
|
|
313
|
+
const from = state.position;
|
|
314
|
+
let value = "";
|
|
315
|
+
state.position++;
|
|
316
|
+
while (!isAtEnd(state)) {
|
|
317
|
+
const char = state.text[state.position];
|
|
318
|
+
const next = state.text.at(state.position + 1);
|
|
319
|
+
if (char == "\\" && (next == quote || next == "\\")) {
|
|
320
|
+
value += next;
|
|
321
|
+
state.position += 2;
|
|
322
|
+
} else {
|
|
323
|
+
state.position++;
|
|
324
|
+
if (char == quote) return { value };
|
|
325
|
+
value += char;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
value,
|
|
330
|
+
unclosedQuote: {
|
|
331
|
+
from,
|
|
332
|
+
to: state.position
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
function reportUnclosedQuote(state, chunk) {
|
|
337
|
+
if (chunk.unclosedQuote != void 0) report$1(state, DIAGNOSTIC_CODE.unclosedQuote, chunk.unclosedQuote);
|
|
338
|
+
}
|
|
339
|
+
function combine(type, nodes, span) {
|
|
340
|
+
const children = nodes.filter((node) => !isEmptyGroup(node));
|
|
341
|
+
if (children.length == 1) return children[0];
|
|
342
|
+
if (children.length == 0) return emptyGroup(span);
|
|
343
|
+
return {
|
|
344
|
+
type,
|
|
345
|
+
children,
|
|
346
|
+
span: {
|
|
347
|
+
from: children[0].span.from,
|
|
348
|
+
to: children[children.length - 1].span.to
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function emptyGroup(span) {
|
|
353
|
+
return {
|
|
354
|
+
type: "and",
|
|
355
|
+
children: [],
|
|
356
|
+
span
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function isGroup(node) {
|
|
360
|
+
return node.type == "and" || node.type == "or";
|
|
361
|
+
}
|
|
362
|
+
function isEmptyGroup(node) {
|
|
363
|
+
return isGroup(node) && node.children.length == 0;
|
|
364
|
+
}
|
|
365
|
+
function isKeywordAt(state, keyword) {
|
|
366
|
+
return state.text.startsWith(keyword, state.position) && isBoundary(state.text.at(state.position + keyword.length));
|
|
367
|
+
}
|
|
368
|
+
function isBoundary(char) {
|
|
369
|
+
return char == void 0 || char == "(" || char == ")" || isWhitespace(char);
|
|
370
|
+
}
|
|
371
|
+
function startsOperand(state) {
|
|
372
|
+
return startsOperandAt(state, state.position) && !isKeywordAt(state, "OR") && !isKeywordAt(state, "AND");
|
|
373
|
+
}
|
|
374
|
+
function startsOperandAt(state, position) {
|
|
375
|
+
const char = state.text.at(position);
|
|
376
|
+
return char != void 0 && char != ")" && !isWhitespace(char);
|
|
377
|
+
}
|
|
378
|
+
function closesGroup(state, depth) {
|
|
379
|
+
return depth > 0 && currentChar(state) == ")";
|
|
380
|
+
}
|
|
381
|
+
function isKeyEnd(state) {
|
|
382
|
+
return isWordEnd(state) || currentChar(state) == ":";
|
|
383
|
+
}
|
|
384
|
+
function isWordEnd(state) {
|
|
385
|
+
const char = currentChar(state);
|
|
386
|
+
return char == "(" || char == ")" || isWhitespace(char);
|
|
387
|
+
}
|
|
388
|
+
function isValueEnd(state) {
|
|
389
|
+
return isWordEnd(state) || currentChar(state) == ",";
|
|
390
|
+
}
|
|
391
|
+
function isBoundEnd(state) {
|
|
392
|
+
return isValueEnd(state) || state.text.startsWith(RANGE_SEPARATOR$1, state.position);
|
|
393
|
+
}
|
|
394
|
+
function skipWhitespace(state) {
|
|
395
|
+
while (isWhitespace(currentChar(state))) state.position++;
|
|
396
|
+
}
|
|
397
|
+
function isWhitespace(char) {
|
|
398
|
+
return char != void 0 && WHITESPACE.test(char);
|
|
399
|
+
}
|
|
400
|
+
function isAtEnd(state) {
|
|
401
|
+
return state.position >= state.text.length;
|
|
402
|
+
}
|
|
403
|
+
function currentChar(state) {
|
|
404
|
+
return state.text.at(state.position);
|
|
405
|
+
}
|
|
406
|
+
function mark(state, kind, length) {
|
|
407
|
+
state.highlights.push({
|
|
408
|
+
from: state.position,
|
|
409
|
+
to: state.position + length,
|
|
410
|
+
kind
|
|
411
|
+
});
|
|
412
|
+
state.position += length;
|
|
413
|
+
}
|
|
414
|
+
function report$1(state, code, span) {
|
|
415
|
+
state.diagnostics.push(createDiagnostic(code, span));
|
|
416
|
+
}
|
|
417
|
+
// src/completion/completion-context.ts
|
|
418
|
+
const FIND_LEAF_BY_TYPE = {
|
|
419
|
+
and: (node, cursor) => findInChildren(node.children, cursor),
|
|
420
|
+
or: (node, cursor) => findInChildren(node.children, cursor),
|
|
421
|
+
not: (node, cursor) => findLeaf(node.child, cursor),
|
|
422
|
+
term: (node, cursor) => contains(node.span, cursor) ? node : void 0,
|
|
423
|
+
text: (node, cursor) => contains(node.span, cursor) ? node : void 0
|
|
424
|
+
};
|
|
425
|
+
const VALUE_SPAN_BY_KIND = {
|
|
426
|
+
equal: (value) => value.span,
|
|
427
|
+
compare: (value) => ({
|
|
428
|
+
from: value.span.from + value.operator.length,
|
|
429
|
+
to: value.span.to,
|
|
430
|
+
operator: value.operator
|
|
431
|
+
}),
|
|
432
|
+
range: (value, cursor) => cursor <= value.separator.from ? {
|
|
433
|
+
from: value.span.from,
|
|
434
|
+
to: value.separator.from
|
|
435
|
+
} : {
|
|
436
|
+
from: value.separator.to,
|
|
437
|
+
to: value.span.to
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
/**
|
|
441
|
+
* What the cursor is on, for autocompletion. Returns `undefined` inside quoted free text, where nothing is suggested.
|
|
442
|
+
*/
|
|
443
|
+
function getCompletionContext(text, cursor) {
|
|
444
|
+
const leaf = findLeaf(parse(text).tree, cursor);
|
|
445
|
+
if (leaf == void 0) return {
|
|
446
|
+
kind: "key",
|
|
447
|
+
prefix: "",
|
|
448
|
+
from: cursor,
|
|
449
|
+
to: cursor
|
|
450
|
+
};
|
|
451
|
+
if (leaf.type == "text") return leaf.quoted ? void 0 : keyCompletion(text, leaf.span, cursor);
|
|
452
|
+
if (cursor <= leaf.keySpan.to) return keyCompletion(text, {
|
|
453
|
+
from: leaf.keySpan.from,
|
|
454
|
+
to: leaf.keySpan.to + 1
|
|
455
|
+
}, cursor);
|
|
456
|
+
return valueCompletion(text, leaf, cursor);
|
|
457
|
+
}
|
|
458
|
+
function keyCompletion(text, span, cursor) {
|
|
459
|
+
const from = text[span.from] == "-" ? span.from + 1 : span.from;
|
|
460
|
+
return {
|
|
461
|
+
kind: "key",
|
|
462
|
+
prefix: text.slice(from, cursor),
|
|
463
|
+
from,
|
|
464
|
+
to: span.to
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
function valueCompletion(text, term, cursor) {
|
|
468
|
+
const value = term.values.find((candidate) => contains(candidate.span, cursor)) ?? term.values[term.values.length - 1];
|
|
469
|
+
const readSpan = VALUE_SPAN_BY_KIND[value.kind];
|
|
470
|
+
const { operator, ...span } = readSpan(value, cursor);
|
|
471
|
+
const from = Math.min(span.from, cursor);
|
|
472
|
+
return {
|
|
473
|
+
kind: "value",
|
|
474
|
+
key: term.key,
|
|
475
|
+
prefix: unquote(text.slice(from, cursor)),
|
|
476
|
+
from,
|
|
477
|
+
to: span.to,
|
|
478
|
+
taken: term.values.flatMap((other) => other != value && other.kind == "equal" && other.raw != "" ? [other.raw] : []),
|
|
479
|
+
atEnd: cursor == term.span.to,
|
|
480
|
+
...operator != void 0 && { operator }
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
function findLeaf(node, cursor) {
|
|
484
|
+
const finder = FIND_LEAF_BY_TYPE[node.type];
|
|
485
|
+
return finder(node, cursor);
|
|
486
|
+
}
|
|
487
|
+
function findInChildren(children, cursor) {
|
|
488
|
+
for (const child of children) {
|
|
489
|
+
const leaf = findLeaf(child, cursor);
|
|
490
|
+
if (leaf != void 0) return leaf;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function contains(span, cursor) {
|
|
494
|
+
return span.from <= cursor && cursor <= span.to;
|
|
495
|
+
}
|
|
496
|
+
function unquote(prefix) {
|
|
497
|
+
return prefix.startsWith("\"") || prefix.startsWith("'") ? prefix.slice(1) : prefix;
|
|
498
|
+
}
|
|
499
|
+
// src/case-insensitive.ts
|
|
500
|
+
function equalsIgnoringCase(a, b) {
|
|
501
|
+
return a.toLowerCase() == b.toLowerCase();
|
|
502
|
+
}
|
|
503
|
+
function startsWithIgnoringCase(text, prefix) {
|
|
504
|
+
return text.toLowerCase().startsWith(prefix.toLowerCase());
|
|
505
|
+
}
|
|
506
|
+
// src/schema/schema.ts
|
|
507
|
+
const PRESENCE_KEYS = ["has", "no"];
|
|
508
|
+
/** Field names are matched exactly, so `Label:bug` is an unknown field when the field is `label`. */
|
|
509
|
+
function defineSchema(definition) {
|
|
510
|
+
const fields = definition.fields;
|
|
511
|
+
const names = /* @__PURE__ */ new Map();
|
|
512
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
513
|
+
names.set(name, name);
|
|
514
|
+
for (const alias of field.aliases ?? []) names.set(alias, name);
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
...definition,
|
|
518
|
+
fields,
|
|
519
|
+
"~names": names
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
/** The field a key stands for, following aliases. `undefined` for a key the schema does not know. */
|
|
523
|
+
function fieldNameOf(schema, key) {
|
|
524
|
+
return schema["~names"].get(key);
|
|
525
|
+
}
|
|
526
|
+
function findField(schema, name) {
|
|
527
|
+
return Object.hasOwn(schema.fields, name) ? schema.fields[name] : void 0;
|
|
528
|
+
}
|
|
529
|
+
function isPresenceKey(key) {
|
|
530
|
+
return PRESENCE_KEYS.some((presenceKey) => presenceKey == key);
|
|
531
|
+
}
|
|
532
|
+
// src/syntax/write.ts
|
|
533
|
+
const PROBE_KEY = "k";
|
|
534
|
+
const PROBE_VALUE = "v";
|
|
535
|
+
const RANGE_SEPARATOR = "..";
|
|
536
|
+
const WRITE_BY_TYPE = {
|
|
537
|
+
and: (node) => joinWritten(node.children.map(writeAndOperand), " "),
|
|
538
|
+
or: (node) => joinWritten(node.children.map(writeSyntax), " OR "),
|
|
539
|
+
not: (node) => writeNegated(node.child),
|
|
540
|
+
term: (node) => `${writeKey(node.key)}:${node.values.map(writeValue).join(",")}`,
|
|
541
|
+
text: (node) => writeText(node.value)
|
|
542
|
+
};
|
|
543
|
+
const WRITE_NEGATED_BY_TYPE = {
|
|
544
|
+
and: (node) => `-(${writeSyntax(node)})`,
|
|
545
|
+
or: (node) => `-(${writeSyntax(node)})`,
|
|
546
|
+
not: (node) => `NOT ${writeSyntax(node)}`,
|
|
547
|
+
term: (node) => `-${writeSyntax(node)}`,
|
|
548
|
+
text: (node) => `-${writeSyntax(node)}`
|
|
549
|
+
};
|
|
550
|
+
const WRITE_VALUE_BY_KIND = {
|
|
551
|
+
equal: writeEqual,
|
|
552
|
+
compare: writeCompare,
|
|
553
|
+
range: writeRange
|
|
554
|
+
};
|
|
555
|
+
/** Writes a tree as text, with parentheses only around an `OR` inside an `AND`. */
|
|
556
|
+
function writeSyntax(node) {
|
|
557
|
+
const writer = WRITE_BY_TYPE[node.type];
|
|
558
|
+
return writer(node);
|
|
559
|
+
}
|
|
560
|
+
function writeKey(key) {
|
|
561
|
+
if (readsAsKey(key, key)) return key;
|
|
562
|
+
const segmented = key.split(".").map((segment) => readsAsKey(segment, segment) ? segment : quote(segment)).join(".");
|
|
563
|
+
return readsAsKey(segmented, key) ? segmented : quote(key);
|
|
564
|
+
}
|
|
565
|
+
function writeValue(value) {
|
|
566
|
+
const writer = WRITE_VALUE_BY_KIND[value.kind];
|
|
567
|
+
return writer(value);
|
|
568
|
+
}
|
|
569
|
+
function writeText(value) {
|
|
570
|
+
const { tree, diagnostics } = parse(value);
|
|
571
|
+
return diagnostics.length == 0 && tree.type == "text" && !tree.quoted && tree.value == value ? value : quote(value);
|
|
572
|
+
}
|
|
573
|
+
function writeAndOperand(node) {
|
|
574
|
+
const text = writeSyntax(node);
|
|
575
|
+
return node.type == "or" && node.children.length > 1 ? `(${text})` : text;
|
|
576
|
+
}
|
|
577
|
+
function writeNegated(node) {
|
|
578
|
+
const writer = WRITE_NEGATED_BY_TYPE[node.type];
|
|
579
|
+
return writer(node);
|
|
580
|
+
}
|
|
581
|
+
function joinWritten(parts, separator) {
|
|
582
|
+
return parts.filter((part) => part != "").join(separator);
|
|
583
|
+
}
|
|
584
|
+
function writeEqual(value) {
|
|
585
|
+
if (value.raw == "") return "";
|
|
586
|
+
const plain = !value.quoted || !value.raw.startsWith("@") && !value.raw.includes("*");
|
|
587
|
+
const read = probeValue(value.raw);
|
|
588
|
+
return plain && read?.kind == "equal" && !read.quoted && read.raw == value.raw ? value.raw : quote(value.raw);
|
|
589
|
+
}
|
|
590
|
+
function writeCompare(value) {
|
|
591
|
+
const read = probeValue(`${value.operator}${value.raw}`);
|
|
592
|
+
const readsBack = isPlainBound(value) && read?.kind == "compare" && read.operator == value.operator && !read.quoted && read.raw == value.raw;
|
|
593
|
+
return `${value.operator}${readsBack ? value.raw : quote(value.raw)}`;
|
|
594
|
+
}
|
|
595
|
+
function writeRange(value) {
|
|
596
|
+
return `${writeBound(value.min, "min")}${RANGE_SEPARATOR}${writeBound(value.max, "max")}`;
|
|
597
|
+
}
|
|
598
|
+
function writeBound(bound, side) {
|
|
599
|
+
if (bound == void 0) return "";
|
|
600
|
+
const read = probeValue(side == "min" ? `${bound.raw}${RANGE_SEPARATOR}` : `${RANGE_SEPARATOR}${bound.raw}`);
|
|
601
|
+
const readBound = read?.kind == "range" ? read[side] : void 0;
|
|
602
|
+
return isPlainBound(bound) && readBound != void 0 && !readBound.quoted && readBound.raw == bound.raw ? bound.raw : quote(bound.raw);
|
|
603
|
+
}
|
|
604
|
+
function isPlainBound(bound) {
|
|
605
|
+
return !bound.quoted || bound.raw != "*" && !bound.raw.startsWith("@");
|
|
606
|
+
}
|
|
607
|
+
function probeValue(written) {
|
|
608
|
+
const { tree, diagnostics } = parse(`${PROBE_KEY}:${written}`);
|
|
609
|
+
if (diagnostics.length > 0 || !isProbeTerm(tree, PROBE_KEY) || tree.values.length != 1) return void 0;
|
|
610
|
+
return tree.values[0];
|
|
611
|
+
}
|
|
612
|
+
function readsAsKey(written, key) {
|
|
613
|
+
const { tree, diagnostics } = parse(`${written}:${PROBE_VALUE}`);
|
|
614
|
+
return diagnostics.length == 0 && isProbeTerm(tree, key) && tree.values.length == 1;
|
|
615
|
+
}
|
|
616
|
+
function isProbeTerm(tree, key) {
|
|
617
|
+
return tree.type == "term" && tree.key == key;
|
|
618
|
+
}
|
|
619
|
+
function quote(raw) {
|
|
620
|
+
return `"${raw.replace(/[\\"]/g, (match) => `\\${match}`)}"`;
|
|
621
|
+
}
|
|
622
|
+
// src/completion/suggestions.ts
|
|
623
|
+
/** Suggests field names for a key, and for a value the field type's values, its tokens and then the loaded values. */
|
|
624
|
+
function getSuggestions(context, schema, options = {}) {
|
|
625
|
+
return context.kind == "key" ? suggestKeys(context, schema) : suggestValues(context, schema, options.values ?? []);
|
|
626
|
+
}
|
|
627
|
+
function suggestKeys(context, schema) {
|
|
628
|
+
const fields = Object.entries(schema.fields).filter(([name, field]) => startsWithIgnoringCase(name, context.prefix) || startsWithIgnoringCase(field.label ?? "", context.prefix)).map(([name, field]) => ({
|
|
629
|
+
label: field.label ?? name,
|
|
630
|
+
insertText: `${writeKey(name)}:`,
|
|
631
|
+
...describe(field.description)
|
|
632
|
+
}));
|
|
633
|
+
const presence = PRESENCE_KEYS.filter((key) => startsWithIgnoringCase(key, context.prefix)).map((key) => ({
|
|
634
|
+
label: key,
|
|
635
|
+
insertText: `${key}:`
|
|
636
|
+
}));
|
|
637
|
+
return [...fields, ...presence];
|
|
638
|
+
}
|
|
639
|
+
function suggestValues(context, schema, loaded) {
|
|
640
|
+
if (isPresenceKey(context.key)) return suggestFieldNames(context, schema);
|
|
641
|
+
const field = findField(schema, fieldNameOf(schema, context.key) ?? "");
|
|
642
|
+
if (field == void 0) return [];
|
|
643
|
+
const own = [...toSuggestions(field.type.suggest?.(context.prefix) ?? [], context, field.type), ...suggestTokens(context, field.type)];
|
|
644
|
+
const added = toSuggestions(loaded, context, field.type).filter((suggestion) => !own.some((shown) => equalsIgnoringCase(shown.insertText, suggestion.insertText)));
|
|
645
|
+
return [...own, ...added];
|
|
646
|
+
}
|
|
647
|
+
function suggestFieldNames(context, schema) {
|
|
648
|
+
return Object.entries(schema.fields).filter(([name]) => startsWithIgnoringCase(name, context.prefix) && !isTaken(name, context)).map(([name, field]) => ({
|
|
649
|
+
label: field.label ?? name,
|
|
650
|
+
insertText: literalValue(name),
|
|
651
|
+
...describe(field.description)
|
|
652
|
+
}));
|
|
653
|
+
}
|
|
654
|
+
function suggestTokens(context, type) {
|
|
655
|
+
return Object.entries(type.tokens ?? {}).map(([name, token]) => ({
|
|
656
|
+
label: `@${name}`,
|
|
657
|
+
insertText: `@${name}`,
|
|
658
|
+
...describe(token.description)
|
|
659
|
+
})).filter((suggestion) => startsWithIgnoringCase(suggestion.insertText, context.prefix) && !isTaken(suggestion.insertText, context));
|
|
660
|
+
}
|
|
661
|
+
function toSuggestions(values, context, type) {
|
|
662
|
+
return values.flatMap((suggestion) => {
|
|
663
|
+
const formatted = type.format(suggestion.value);
|
|
664
|
+
if (isTaken(formatted, context)) return [];
|
|
665
|
+
return [{
|
|
666
|
+
label: suggestion.label ?? formatted,
|
|
667
|
+
insertText: literalValue(formatted),
|
|
668
|
+
...describe(suggestion.description)
|
|
669
|
+
}];
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
function literalValue(raw) {
|
|
673
|
+
return writeValue({
|
|
674
|
+
kind: "equal",
|
|
675
|
+
raw,
|
|
676
|
+
quoted: true,
|
|
677
|
+
span: {
|
|
678
|
+
from: 0,
|
|
679
|
+
to: 0
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
function isTaken(raw, context) {
|
|
684
|
+
return context.taken.some((taken) => equalsIgnoringCase(taken, raw));
|
|
685
|
+
}
|
|
686
|
+
function describe(description) {
|
|
687
|
+
return description == void 0 ? {} : { description };
|
|
688
|
+
}
|
|
689
|
+
// src/query/query.ts
|
|
690
|
+
function emptyQuery() {
|
|
691
|
+
return {
|
|
692
|
+
type: "and",
|
|
693
|
+
children: []
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
function rangeCondition(field, min, max) {
|
|
697
|
+
return {
|
|
698
|
+
type: "range",
|
|
699
|
+
field,
|
|
700
|
+
...definedBounds(min, max)
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
function definedBounds(min, max) {
|
|
704
|
+
return {
|
|
705
|
+
...min != void 0 && { min },
|
|
706
|
+
...max != void 0 && { max }
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
function isQueryGroup(query) {
|
|
710
|
+
return query.type == "and" || query.type == "or";
|
|
711
|
+
}
|
|
712
|
+
function anyOf(queries) {
|
|
713
|
+
return queries.length <= 1 ? queries[0] : {
|
|
714
|
+
type: "or",
|
|
715
|
+
children: queries
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
function allOf(queries) {
|
|
719
|
+
return queries.length <= 1 ? queries[0] : {
|
|
720
|
+
type: "and",
|
|
721
|
+
children: queries
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
// src/flat/flat.ts
|
|
725
|
+
/** Whether a set also takes items with no value (`no:field`) or leaves them out (`has:field`). */
|
|
726
|
+
const FLAT_EMPTY = {
|
|
727
|
+
include: "include",
|
|
728
|
+
exclude: "exclude"
|
|
729
|
+
};
|
|
730
|
+
const ADD_BY_TYPE = {
|
|
731
|
+
and: () => false,
|
|
732
|
+
or: (builder, query) => addSetPart(builder, readSetPart(query), FLAT_EMPTY.include),
|
|
733
|
+
not: (builder, query) => addSetPart(builder, readSetPart(query.child), FLAT_EMPTY.exclude),
|
|
734
|
+
in: (builder, query) => addSetPart(builder, readSetPart(query), FLAT_EMPTY.include),
|
|
735
|
+
range: addRange,
|
|
736
|
+
text: addText,
|
|
737
|
+
empty: (builder, query) => addSetPart(builder, readSetPart(query), FLAT_EMPTY.include),
|
|
738
|
+
match: () => false,
|
|
739
|
+
invalid: addInvalid
|
|
740
|
+
};
|
|
741
|
+
const READ_SET_PART_BY_TYPE = {
|
|
742
|
+
and: () => void 0,
|
|
743
|
+
or: (query) => mergeSetParts(query.children.map(readSetPart)),
|
|
744
|
+
not: () => void 0,
|
|
745
|
+
in: (query) => ({
|
|
746
|
+
field: query.field,
|
|
747
|
+
values: query.values,
|
|
748
|
+
empty: false
|
|
749
|
+
}),
|
|
750
|
+
range: () => void 0,
|
|
751
|
+
text: () => void 0,
|
|
752
|
+
empty: (query) => ({
|
|
753
|
+
field: query.field,
|
|
754
|
+
values: [],
|
|
755
|
+
empty: true
|
|
756
|
+
}),
|
|
757
|
+
match: () => void 0,
|
|
758
|
+
invalid: () => void 0
|
|
759
|
+
};
|
|
760
|
+
const ADD_TO_SET_BY_POLARITY = {
|
|
761
|
+
include: (set, part) => {
|
|
762
|
+
if (set.include.length > 0 || set.empty == FLAT_EMPTY.include) return false;
|
|
763
|
+
set.include = [...part.values];
|
|
764
|
+
return setEmpty(set, part, FLAT_EMPTY.include);
|
|
765
|
+
},
|
|
766
|
+
exclude: (set, part) => {
|
|
767
|
+
set.exclude = [...set.exclude, ...part.values];
|
|
768
|
+
return setEmpty(set, part, FLAT_EMPTY.exclude);
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
const FROM_FLAT_BY_KIND = {
|
|
772
|
+
set: fromFlatSet,
|
|
773
|
+
range: (field, range) => range.min == void 0 && range.max == void 0 ? [] : [rangeCondition(field, range.min, range.max)]
|
|
774
|
+
};
|
|
775
|
+
/**
|
|
776
|
+
* Reads a query as panel state. Returns `undefined` for a query a panel cannot show, such as an `OR` across fields,
|
|
777
|
+
* two lists of values for one field, a negated range, a wildcard or negated free text.
|
|
778
|
+
*/
|
|
779
|
+
function toFlat(query) {
|
|
780
|
+
const builder = {
|
|
781
|
+
fields: /* @__PURE__ */ new Map(),
|
|
782
|
+
text: [],
|
|
783
|
+
invalid: []
|
|
784
|
+
};
|
|
785
|
+
if (!(query.type == "and" ? query.children : [query]).every((part) => addPart(builder, part))) return void 0;
|
|
786
|
+
return {
|
|
787
|
+
fields: Object.fromEntries(builder.fields),
|
|
788
|
+
text: builder.text,
|
|
789
|
+
invalid: builder.invalid
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
/** Builds a query from panel state, field by field in the order of `fields`, then free text and unbound parts. */
|
|
793
|
+
function fromFlat(flat) {
|
|
794
|
+
const fields = Object.entries(flat.fields).flatMap(([field, entry]) => fromFlatField(field, entry));
|
|
795
|
+
const text = flat.text.map((value) => ({
|
|
796
|
+
type: "text",
|
|
797
|
+
value
|
|
798
|
+
}));
|
|
799
|
+
return allOf([
|
|
800
|
+
...fields,
|
|
801
|
+
...text,
|
|
802
|
+
...flat.invalid
|
|
803
|
+
]) ?? emptyQuery();
|
|
804
|
+
}
|
|
805
|
+
function addPart(builder, query) {
|
|
806
|
+
const adder = ADD_BY_TYPE[query.type];
|
|
807
|
+
return adder(builder, query);
|
|
808
|
+
}
|
|
809
|
+
function readSetPart(query) {
|
|
810
|
+
const reader = READ_SET_PART_BY_TYPE[query.type];
|
|
811
|
+
return reader(query);
|
|
812
|
+
}
|
|
813
|
+
function mergeSetParts(parts) {
|
|
814
|
+
const known = compact(parts);
|
|
815
|
+
const [first] = known;
|
|
816
|
+
if (first == void 0 || known.length < parts.length || known.some((part) => part.field != first.field)) return void 0;
|
|
817
|
+
return {
|
|
818
|
+
field: first.field,
|
|
819
|
+
values: known.flatMap((part) => part.values),
|
|
820
|
+
empty: known.some((part) => part.empty)
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
function addSetPart(builder, part, polarity) {
|
|
824
|
+
if (part == void 0) return false;
|
|
825
|
+
const existing = builder.fields.get(part.field) ?? {
|
|
826
|
+
kind: "set",
|
|
827
|
+
include: [],
|
|
828
|
+
exclude: []
|
|
829
|
+
};
|
|
830
|
+
if (existing.kind != "set") return false;
|
|
831
|
+
const set = { ...existing };
|
|
832
|
+
builder.fields.set(part.field, set);
|
|
833
|
+
return ADD_TO_SET_BY_POLARITY[polarity](set, part);
|
|
834
|
+
}
|
|
835
|
+
function setEmpty(set, part, empty) {
|
|
836
|
+
if (!part.empty) return true;
|
|
837
|
+
if (set.empty != void 0 && set.empty != empty) return false;
|
|
838
|
+
set.empty = empty;
|
|
839
|
+
return true;
|
|
840
|
+
}
|
|
841
|
+
function addRange(builder, query) {
|
|
842
|
+
if (builder.fields.has(query.field)) return false;
|
|
843
|
+
builder.fields.set(query.field, {
|
|
844
|
+
kind: "range",
|
|
845
|
+
...definedBounds(query.min, query.max)
|
|
846
|
+
});
|
|
847
|
+
return true;
|
|
848
|
+
}
|
|
849
|
+
function addText(builder, query) {
|
|
850
|
+
builder.text.push(query.value);
|
|
851
|
+
return true;
|
|
852
|
+
}
|
|
853
|
+
function addInvalid(builder, query) {
|
|
854
|
+
builder.invalid.push(query);
|
|
855
|
+
return true;
|
|
856
|
+
}
|
|
857
|
+
function fromFlatField(field, entry) {
|
|
858
|
+
const build = FROM_FLAT_BY_KIND[entry.kind];
|
|
859
|
+
return build(field, entry);
|
|
860
|
+
}
|
|
861
|
+
function fromFlatSet(field, set) {
|
|
862
|
+
const included = compact([valuesIn(field, set.include), emptyIf(field, set.empty == FLAT_EMPTY.include)]);
|
|
863
|
+
const excluded = compact([valuesIn(field, set.exclude), emptyIf(field, set.empty == FLAT_EMPTY.exclude)]);
|
|
864
|
+
return compact([anyOf(included), ...excluded.map((child) => ({
|
|
865
|
+
type: "not",
|
|
866
|
+
child
|
|
867
|
+
}))]);
|
|
868
|
+
}
|
|
869
|
+
function valuesIn(field, values) {
|
|
870
|
+
return values.length == 0 ? void 0 : {
|
|
871
|
+
type: "in",
|
|
872
|
+
field,
|
|
873
|
+
values
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
function emptyIf(field, condition) {
|
|
877
|
+
return condition ? {
|
|
878
|
+
type: "empty",
|
|
879
|
+
field
|
|
880
|
+
} : void 0;
|
|
881
|
+
}
|
|
882
|
+
function compact(items) {
|
|
883
|
+
return items.filter((item) => item != void 0);
|
|
884
|
+
}
|
|
885
|
+
// src/schema/token.ts
|
|
886
|
+
const TOKEN = /^@([A-Za-z][A-Za-z0-9_]*)(?:([+-])(\d+)([A-Za-z]*))?$/;
|
|
887
|
+
function readToken(raw) {
|
|
888
|
+
const match = TOKEN.exec(raw);
|
|
889
|
+
if (match == void 0) return void 0;
|
|
890
|
+
const [, name, sign, digits, unit] = match;
|
|
891
|
+
if (digits == void 0) return {
|
|
892
|
+
kind: "token",
|
|
893
|
+
name,
|
|
894
|
+
raw
|
|
895
|
+
};
|
|
896
|
+
const amount = Number(digits) * (sign == "-" ? -1 : 1);
|
|
897
|
+
return {
|
|
898
|
+
kind: "token",
|
|
899
|
+
name,
|
|
900
|
+
offset: unit == "" ? { amount } : {
|
|
901
|
+
amount,
|
|
902
|
+
unit
|
|
903
|
+
},
|
|
904
|
+
raw
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
function isTokenValue(value) {
|
|
908
|
+
return typeof value == "object" && value != null && "kind" in value && value.kind == "token" && "raw" in value;
|
|
909
|
+
}
|
|
910
|
+
// src/query/ranges.ts
|
|
911
|
+
function intersectRanges(a, b, type) {
|
|
912
|
+
const min = tighterBound(a.min, b.min, "min", type);
|
|
913
|
+
const max = tighterBound(a.max, b.max, "max", type);
|
|
914
|
+
if (min == void 0 || max == void 0) return { kind: "separate" };
|
|
915
|
+
const range = rangeCondition(a.field, min.bound, max.bound);
|
|
916
|
+
return isEmptyRange(range, type) ? { kind: "empty" } : {
|
|
917
|
+
kind: "merged",
|
|
918
|
+
range
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
function isEmptyRange(range, type) {
|
|
922
|
+
if (range.min == void 0 || range.max == void 0) return false;
|
|
923
|
+
const order = compareValues$1(range.min.value, range.max.value, type);
|
|
924
|
+
if (order == void 0) return false;
|
|
925
|
+
return order > 0 || order == 0 && !(range.min.inclusive && range.max.inclusive);
|
|
926
|
+
}
|
|
927
|
+
function tighterBound(a, b, side, type) {
|
|
928
|
+
if (a == void 0) return { bound: b };
|
|
929
|
+
if (b == void 0) return { bound: a };
|
|
930
|
+
const order = compareValues$1(a.value, b.value, type);
|
|
931
|
+
if (order == void 0 || order == 0 && type.format(a.value) != type.format(b.value)) return void 0;
|
|
932
|
+
if (order == 0) return { bound: a.inclusive ? b : a };
|
|
933
|
+
return { bound: (side == "min" ? order > 0 : order < 0) ? a : b };
|
|
934
|
+
}
|
|
935
|
+
function compareValues$1(a, b, type) {
|
|
936
|
+
if (type.compare == void 0 || isTokenValue(a) || isTokenValue(b)) return void 0;
|
|
937
|
+
const order = type.compare(a, b);
|
|
938
|
+
return Number.isNaN(order) ? void 0 : order;
|
|
939
|
+
}
|
|
940
|
+
// src/query/normalize.ts
|
|
941
|
+
const NORMALIZE_BY_TYPE = {
|
|
942
|
+
and: (query, schema) => allOf(mergeRanges(flatten(query, schema), schema)),
|
|
943
|
+
or: (query, schema) => anyOf(flatten(query, schema)),
|
|
944
|
+
not: normalizeNot,
|
|
945
|
+
in: (query) => query.values.length == 0 ? void 0 : query,
|
|
946
|
+
range: (query) => query.min == void 0 && query.max == void 0 ? void 0 : query,
|
|
947
|
+
text: (query) => query.value == "" ? void 0 : query,
|
|
948
|
+
empty: (query) => query,
|
|
949
|
+
match: (query) => query,
|
|
950
|
+
invalid: (query) => query
|
|
951
|
+
};
|
|
952
|
+
/** Flattens nested groups of one type, drops empty parts and double negations, and intersects ranges. */
|
|
953
|
+
function normalize(query, schema) {
|
|
954
|
+
return normalizeNode(query, schema) ?? emptyQuery();
|
|
955
|
+
}
|
|
956
|
+
function normalizeNode(query, schema) {
|
|
957
|
+
const normalizer = NORMALIZE_BY_TYPE[query.type];
|
|
958
|
+
return normalizer(query, schema);
|
|
959
|
+
}
|
|
960
|
+
function flatten(group, schema) {
|
|
961
|
+
return group.children.flatMap((child) => {
|
|
962
|
+
const normalized = normalizeNode(child, schema);
|
|
963
|
+
if (normalized == void 0) return [];
|
|
964
|
+
return isQueryGroup(normalized) && normalized.type == group.type ? normalized.children : [normalized];
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
function normalizeNot(query, schema) {
|
|
968
|
+
const child = normalizeNode(query.child, schema);
|
|
969
|
+
if (child == void 0) return void 0;
|
|
970
|
+
return child.type == "not" ? child.child : {
|
|
971
|
+
type: "not",
|
|
972
|
+
child
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
function mergeRanges(children, schema) {
|
|
976
|
+
const merged = [];
|
|
977
|
+
for (const child of children) {
|
|
978
|
+
const index = child.type == "range" ? merged.findIndex((other) => other.type == "range" && other.field == child.field) : -1;
|
|
979
|
+
const previous = merged[index];
|
|
980
|
+
const range = previous?.type == "range" && child.type == "range" ? mergeRange(previous, child, schema) : void 0;
|
|
981
|
+
if (range == void 0) merged.push(child);
|
|
982
|
+
else merged[index] = range;
|
|
983
|
+
}
|
|
984
|
+
return merged;
|
|
985
|
+
}
|
|
986
|
+
function mergeRange(a, b, schema) {
|
|
987
|
+
const type = findField(schema, a.field)?.type;
|
|
988
|
+
if (type == void 0) return void 0;
|
|
989
|
+
const intersection = intersectRanges(a, b, type);
|
|
990
|
+
return intersection.kind == "merged" ? intersection.range : void 0;
|
|
991
|
+
}
|
|
992
|
+
// src/query/bind.ts
|
|
993
|
+
const BIND_BY_TYPE = {
|
|
994
|
+
and: bindAnd,
|
|
995
|
+
or: (node, context) => anyOf(bindChildren(node.children, context).map((entry) => entry.query)),
|
|
996
|
+
not: bindNot,
|
|
997
|
+
term: bindTerm,
|
|
998
|
+
text: (node) => node.value == "" ? void 0 : {
|
|
999
|
+
type: "text",
|
|
1000
|
+
value: node.value
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
const BIND_VALUE_BY_KIND = {
|
|
1004
|
+
equal: bindEqual,
|
|
1005
|
+
compare: bindCompare,
|
|
1006
|
+
range: bindRange
|
|
1007
|
+
};
|
|
1008
|
+
const IS_EMPTY_BY_KIND = {
|
|
1009
|
+
equal: (value) => value.raw == "",
|
|
1010
|
+
compare: (value) => value.raw == "" || isOpenBound(value),
|
|
1011
|
+
range: (value) => value.min == void 0 && value.max == void 0
|
|
1012
|
+
};
|
|
1013
|
+
const BOUND_BY_OPERATOR = {
|
|
1014
|
+
">=": {
|
|
1015
|
+
side: "min",
|
|
1016
|
+
inclusive: true
|
|
1017
|
+
},
|
|
1018
|
+
">": {
|
|
1019
|
+
side: "min",
|
|
1020
|
+
inclusive: false
|
|
1021
|
+
},
|
|
1022
|
+
"<=": {
|
|
1023
|
+
side: "max",
|
|
1024
|
+
inclusive: true
|
|
1025
|
+
},
|
|
1026
|
+
"<": {
|
|
1027
|
+
side: "max",
|
|
1028
|
+
inclusive: false
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
const PRESENCE_QUERY = {
|
|
1032
|
+
has: (field) => ({
|
|
1033
|
+
type: "not",
|
|
1034
|
+
child: {
|
|
1035
|
+
type: "empty",
|
|
1036
|
+
field
|
|
1037
|
+
}
|
|
1038
|
+
}),
|
|
1039
|
+
no: (field) => ({
|
|
1040
|
+
type: "empty",
|
|
1041
|
+
field
|
|
1042
|
+
})
|
|
1043
|
+
};
|
|
1044
|
+
/**
|
|
1045
|
+
* Applies a schema to a syntax tree. Unknown fields, invalid values and unknown tokens become diagnostics and stay in
|
|
1046
|
+
* the query as written, so they stay in the text and filter nothing.
|
|
1047
|
+
*/
|
|
1048
|
+
function bind(tree, schema) {
|
|
1049
|
+
const context = {
|
|
1050
|
+
schema,
|
|
1051
|
+
diagnostics: []
|
|
1052
|
+
};
|
|
1053
|
+
return {
|
|
1054
|
+
query: normalize(bindNode(tree, context) ?? emptyQuery(), schema),
|
|
1055
|
+
diagnostics: context.diagnostics
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
function bindNode(node, context) {
|
|
1059
|
+
const binder = BIND_BY_TYPE[node.type];
|
|
1060
|
+
return binder(node, context);
|
|
1061
|
+
}
|
|
1062
|
+
function bindChildren(children, context) {
|
|
1063
|
+
return children.flatMap((child) => {
|
|
1064
|
+
const query = bindNode(child, context);
|
|
1065
|
+
return query == void 0 ? [] : [{
|
|
1066
|
+
query,
|
|
1067
|
+
span: child.span
|
|
1068
|
+
}];
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
function bindAnd(node, context) {
|
|
1072
|
+
const entries = bindChildren(flattenAnd(node), context);
|
|
1073
|
+
reportEmptyIntersections(entries, context);
|
|
1074
|
+
return allOf(entries.map((entry) => entry.query));
|
|
1075
|
+
}
|
|
1076
|
+
function flattenAnd(node) {
|
|
1077
|
+
return node.children.flatMap((child) => child.type == "and" ? flattenAnd(child) : [child]);
|
|
1078
|
+
}
|
|
1079
|
+
function reportEmptyIntersections(entries, context) {
|
|
1080
|
+
const ranges = entries.filter((entry) => entry.query.type == "range");
|
|
1081
|
+
ranges.forEach((first, index) => {
|
|
1082
|
+
const type = context.schema.fields[first.query.field].type;
|
|
1083
|
+
for (const second of ranges.slice(index + 1)) {
|
|
1084
|
+
if (second.query.field != first.query.field) continue;
|
|
1085
|
+
if (intersectRanges(first.query, second.query, type).kind == "empty") report(context, DIAGNOSTIC_CODE.emptyRange, {
|
|
1086
|
+
from: first.span.from,
|
|
1087
|
+
to: second.span.to
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
function bindNot(node, context) {
|
|
1093
|
+
if (node.child.type == "and" || node.child.type == "or") return {
|
|
1094
|
+
type: "invalid",
|
|
1095
|
+
syntax: node
|
|
1096
|
+
};
|
|
1097
|
+
const child = bindNode(node.child, context);
|
|
1098
|
+
return child == void 0 ? void 0 : {
|
|
1099
|
+
type: "not",
|
|
1100
|
+
child
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
function bindTerm(node, context) {
|
|
1104
|
+
const values = node.values.filter((value) => !isEmptyValue(value));
|
|
1105
|
+
if (values.length == 0) return void 0;
|
|
1106
|
+
const term = {
|
|
1107
|
+
...node,
|
|
1108
|
+
values
|
|
1109
|
+
};
|
|
1110
|
+
const { key } = term;
|
|
1111
|
+
if (isPresenceKey(key)) return anyOf(values.map((value) => bindPresence(key, value, context)));
|
|
1112
|
+
const field = context.schema["~names"].get(key);
|
|
1113
|
+
if (field == void 0) {
|
|
1114
|
+
report(context, DIAGNOSTIC_CODE.unknownField, term.keySpan);
|
|
1115
|
+
return {
|
|
1116
|
+
type: "invalid",
|
|
1117
|
+
syntax: term
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
const target = {
|
|
1121
|
+
field,
|
|
1122
|
+
type: context.schema.fields[field].type
|
|
1123
|
+
};
|
|
1124
|
+
return anyOf(mergeAdjacentIn(values.map((value) => bindValue(value, target, context))));
|
|
1125
|
+
}
|
|
1126
|
+
function bindPresence(key, value, context) {
|
|
1127
|
+
if (value.kind != "equal") return rejectValue(key, value, DIAGNOSTIC_CODE.unsupportedOperator, context);
|
|
1128
|
+
const field = context.schema["~names"].get(value.raw);
|
|
1129
|
+
if (field == void 0) return rejectValue(key, value, DIAGNOSTIC_CODE.unknownField, context);
|
|
1130
|
+
return PRESENCE_QUERY[key](field);
|
|
1131
|
+
}
|
|
1132
|
+
function bindValue(value, target, context) {
|
|
1133
|
+
const binder = BIND_VALUE_BY_KIND[value.kind];
|
|
1134
|
+
return binder(value, target, context);
|
|
1135
|
+
}
|
|
1136
|
+
function bindEqual(value, target, context) {
|
|
1137
|
+
if (isOpenBound(value)) return PRESENCE_QUERY.has(target.field);
|
|
1138
|
+
if (!value.quoted && value.raw.includes("*")) {
|
|
1139
|
+
if (target.type.wildcards) return {
|
|
1140
|
+
type: "match",
|
|
1141
|
+
field: target.field,
|
|
1142
|
+
pattern: value.raw
|
|
1143
|
+
};
|
|
1144
|
+
return rejectValue(target.field, value, DIAGNOSTIC_CODE.unsupportedOperator, context);
|
|
1145
|
+
}
|
|
1146
|
+
const typed = readValue(value.raw, value.quoted, value.span, target, context);
|
|
1147
|
+
if (typed == void 0) return invalidValue(target.field, value);
|
|
1148
|
+
return {
|
|
1149
|
+
type: "in",
|
|
1150
|
+
field: target.field,
|
|
1151
|
+
values: [typed.value]
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
function bindCompare(value, target, context) {
|
|
1155
|
+
if (target.type.compare == void 0) return rejectValue(target.field, value, DIAGNOSTIC_CODE.unsupportedOperator, context);
|
|
1156
|
+
const typed = readValue(value.raw, value.quoted, value.span, target, context);
|
|
1157
|
+
if (typed == void 0) return invalidValue(target.field, value);
|
|
1158
|
+
const { side, inclusive } = BOUND_BY_OPERATOR[value.operator];
|
|
1159
|
+
const bound = {
|
|
1160
|
+
value: typed.value,
|
|
1161
|
+
inclusive
|
|
1162
|
+
};
|
|
1163
|
+
return side == "min" ? rangeCondition(target.field, bound, void 0) : rangeCondition(target.field, void 0, bound);
|
|
1164
|
+
}
|
|
1165
|
+
function bindRange(value, target, context) {
|
|
1166
|
+
if (target.type.compare == void 0) return rejectValue(target.field, value, DIAGNOSTIC_CODE.unsupportedOperator, context);
|
|
1167
|
+
const min = readBound(value.min, value.span, target, context);
|
|
1168
|
+
const max = readBound(value.max, value.span, target, context);
|
|
1169
|
+
if (min == void 0 || max == void 0) return invalidValue(target.field, value);
|
|
1170
|
+
const range = rangeCondition(target.field, min.bound, max.bound);
|
|
1171
|
+
if (isEmptyRange(range, target.type)) report(context, DIAGNOSTIC_CODE.emptyRange, value.span);
|
|
1172
|
+
return range;
|
|
1173
|
+
}
|
|
1174
|
+
function readBound(bound, span, target, context) {
|
|
1175
|
+
if (bound == void 0) return {};
|
|
1176
|
+
const typed = readValue(bound.raw, bound.quoted, span, target, context);
|
|
1177
|
+
return typed == void 0 ? void 0 : { bound: {
|
|
1178
|
+
value: typed.value,
|
|
1179
|
+
inclusive: true
|
|
1180
|
+
} };
|
|
1181
|
+
}
|
|
1182
|
+
function readValue(raw, quoted, span, target, context) {
|
|
1183
|
+
if (!quoted && raw.startsWith("@")) return readTokenValue(raw, span, target, context);
|
|
1184
|
+
const value = target.type.parse(raw);
|
|
1185
|
+
if (value == void 0) report(context, DIAGNOSTIC_CODE.invalidValue, span);
|
|
1186
|
+
return value == void 0 ? void 0 : { value };
|
|
1187
|
+
}
|
|
1188
|
+
function readTokenValue(raw, span, target, context) {
|
|
1189
|
+
const token = readToken(raw);
|
|
1190
|
+
const tokens = target.type.tokens ?? {};
|
|
1191
|
+
const definition = token != void 0 && Object.hasOwn(tokens, token.name) ? tokens[token.name] : void 0;
|
|
1192
|
+
if (token == void 0 || definition == void 0 || !acceptsOffset(definition, token.offset)) {
|
|
1193
|
+
report(context, DIAGNOSTIC_CODE.unknownToken, span);
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
return { value: token };
|
|
1197
|
+
}
|
|
1198
|
+
function acceptsOffset(definition, offset) {
|
|
1199
|
+
if (offset == void 0) return true;
|
|
1200
|
+
const units = definition.offsetUnits;
|
|
1201
|
+
if (units == void 0 || !Number.isSafeInteger(offset.amount)) return false;
|
|
1202
|
+
return offset.unit == void 0 || units.includes(offset.unit);
|
|
1203
|
+
}
|
|
1204
|
+
function mergeAdjacentIn(queries) {
|
|
1205
|
+
const merged = [];
|
|
1206
|
+
for (const query of queries) {
|
|
1207
|
+
const previous = merged.at(-1);
|
|
1208
|
+
if (query.type == "in" && previous?.type == "in") merged[merged.length - 1] = {
|
|
1209
|
+
...previous,
|
|
1210
|
+
values: [...previous.values, ...query.values]
|
|
1211
|
+
};
|
|
1212
|
+
else merged.push(query);
|
|
1213
|
+
}
|
|
1214
|
+
return merged;
|
|
1215
|
+
}
|
|
1216
|
+
function rejectValue(key, value, code, context) {
|
|
1217
|
+
report(context, code, value.span);
|
|
1218
|
+
return invalidValue(key, value);
|
|
1219
|
+
}
|
|
1220
|
+
function invalidValue(key, value) {
|
|
1221
|
+
return {
|
|
1222
|
+
type: "invalid",
|
|
1223
|
+
syntax: {
|
|
1224
|
+
type: "term",
|
|
1225
|
+
key,
|
|
1226
|
+
keySpan: value.span,
|
|
1227
|
+
values: [value],
|
|
1228
|
+
span: value.span
|
|
1229
|
+
}
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
function isEmptyValue(value) {
|
|
1233
|
+
const isEmpty = IS_EMPTY_BY_KIND[value.kind];
|
|
1234
|
+
return isEmpty(value);
|
|
1235
|
+
}
|
|
1236
|
+
function isOpenBound(value) {
|
|
1237
|
+
return value.raw == "*" && !value.quoted;
|
|
1238
|
+
}
|
|
1239
|
+
function report(context, code, span) {
|
|
1240
|
+
context.diagnostics.push(createDiagnostic(code, span));
|
|
1241
|
+
}
|
|
1242
|
+
// src/query/evaluable.ts
|
|
1243
|
+
const PREPARE_BY_TYPE = {
|
|
1244
|
+
and: (query, schema) => allOf(prepareChildren(query.children, schema)),
|
|
1245
|
+
or: (query, schema) => anyOf(prepareChildren(query.children, schema)),
|
|
1246
|
+
not: (query, schema) => {
|
|
1247
|
+
const child = prepareNode(query.child, schema);
|
|
1248
|
+
return child == void 0 ? void 0 : {
|
|
1249
|
+
type: "not",
|
|
1250
|
+
child
|
|
1251
|
+
};
|
|
1252
|
+
},
|
|
1253
|
+
in: expandCondition,
|
|
1254
|
+
range: expandCondition,
|
|
1255
|
+
text: (query) => query,
|
|
1256
|
+
empty: expandCondition,
|
|
1257
|
+
match: expandCondition,
|
|
1258
|
+
invalid: () => void 0
|
|
1259
|
+
};
|
|
1260
|
+
/**
|
|
1261
|
+
* The query as it filters: unbound parts left out and fields with `expand` replaced by what they stand for. A query
|
|
1262
|
+
* with nothing left is an empty `and`.
|
|
1263
|
+
*/
|
|
1264
|
+
function evaluable(query, schema) {
|
|
1265
|
+
return prepareNode(query, schema) ?? emptyQuery();
|
|
1266
|
+
}
|
|
1267
|
+
function prepareNode(query, schema) {
|
|
1268
|
+
const preparer = PREPARE_BY_TYPE[query.type];
|
|
1269
|
+
return preparer(query, schema);
|
|
1270
|
+
}
|
|
1271
|
+
function prepareChildren(children, schema) {
|
|
1272
|
+
return children.flatMap((child) => prepareNode(child, schema) ?? []);
|
|
1273
|
+
}
|
|
1274
|
+
function expandCondition(condition, schema) {
|
|
1275
|
+
const expand = findField(schema, condition.field)?.expand;
|
|
1276
|
+
if (expand == void 0) return condition;
|
|
1277
|
+
const expanded = expand(condition);
|
|
1278
|
+
return expanded == void 0 ? void 0 : prepareNode(expanded, schema);
|
|
1279
|
+
}
|
|
1280
|
+
// src/query/fold.ts
|
|
1281
|
+
const FOLD_BY_TYPE = {
|
|
1282
|
+
and: (query, visitor) => visitor.and(query.children.map((child) => foldNode(child, visitor))),
|
|
1283
|
+
or: (query, visitor) => visitor.or(query.children.map((child) => foldNode(child, visitor))),
|
|
1284
|
+
not: (query, visitor) => visitor.not(foldNode(query.child, visitor)),
|
|
1285
|
+
in: (query, visitor) => visitor.in(query),
|
|
1286
|
+
range: (query, visitor) => visitor.range(query),
|
|
1287
|
+
text: (query, visitor) => visitor.text(query),
|
|
1288
|
+
empty: (query, visitor) => visitor.empty(query),
|
|
1289
|
+
match: (query, visitor) => visitor.match(query)
|
|
1290
|
+
};
|
|
1291
|
+
/**
|
|
1292
|
+
* Folds a query from its conditions up. Parts that could not be bound are left out first, so they filter nothing,
|
|
1293
|
+
* fields with `expand` are replaced by what they stand for, and a query with nothing left folds as an empty `and`.
|
|
1294
|
+
*/
|
|
1295
|
+
function fold(query, schema, visitor) {
|
|
1296
|
+
return foldNode(evaluable(query, schema), visitor);
|
|
1297
|
+
}
|
|
1298
|
+
function foldNode(query, visitor) {
|
|
1299
|
+
const folder = FOLD_BY_TYPE[query.type];
|
|
1300
|
+
return folder(query, visitor);
|
|
1301
|
+
}
|
|
1302
|
+
// src/query/resolve-tokens.ts
|
|
1303
|
+
const RESOLVE_BY_TYPE = {
|
|
1304
|
+
and: (query, scope) => ({
|
|
1305
|
+
...query,
|
|
1306
|
+
children: query.children.map((child) => resolveNode(child, scope))
|
|
1307
|
+
}),
|
|
1308
|
+
or: (query, scope) => ({
|
|
1309
|
+
...query,
|
|
1310
|
+
children: query.children.map((child) => resolveNode(child, scope))
|
|
1311
|
+
}),
|
|
1312
|
+
not: (query, scope) => ({
|
|
1313
|
+
...query,
|
|
1314
|
+
child: resolveNode(query.child, scope)
|
|
1315
|
+
}),
|
|
1316
|
+
in: (query, scope) => ({
|
|
1317
|
+
...query,
|
|
1318
|
+
values: query.values.map((value) => resolveValue(value, query.field, scope))
|
|
1319
|
+
}),
|
|
1320
|
+
range: (query, scope) => rangeCondition(query.field, resolveBound(query.min, query.field, scope), resolveBound(query.max, query.field, scope)),
|
|
1321
|
+
text: (query) => query,
|
|
1322
|
+
empty: (query) => query,
|
|
1323
|
+
match: (query) => query,
|
|
1324
|
+
invalid: (query) => query
|
|
1325
|
+
};
|
|
1326
|
+
/** Replaces dynamic values such as `@today` with the values they stand for now, for a backend that needs them. */
|
|
1327
|
+
function resolveTokens(query, schema, context = {}) {
|
|
1328
|
+
return resolveNode(query, {
|
|
1329
|
+
schema,
|
|
1330
|
+
context: {
|
|
1331
|
+
...context,
|
|
1332
|
+
now: context.now ?? /* @__PURE__ */ new Date()
|
|
1333
|
+
}
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
function resolveNode(query, scope) {
|
|
1337
|
+
const resolver = RESOLVE_BY_TYPE[query.type];
|
|
1338
|
+
return resolver(query, scope);
|
|
1339
|
+
}
|
|
1340
|
+
function resolveBound(bound, field, scope) {
|
|
1341
|
+
return bound && {
|
|
1342
|
+
...bound,
|
|
1343
|
+
value: resolveValue(bound.value, field, scope)
|
|
1344
|
+
};
|
|
1345
|
+
}
|
|
1346
|
+
function resolveValue(value, field, scope) {
|
|
1347
|
+
if (!isTokenValue(value)) return value;
|
|
1348
|
+
const tokens = findField(scope.schema, field)?.type.tokens ?? {};
|
|
1349
|
+
if (!Object.hasOwn(tokens, value.name)) return value;
|
|
1350
|
+
return tokens[value.name].resolve(value.offset, scope.context);
|
|
1351
|
+
}
|
|
1352
|
+
// src/query/predicate.ts
|
|
1353
|
+
const WORD_CHARACTER = /[\p{L}\p{N}]/u;
|
|
1354
|
+
const WILDCARD = "*";
|
|
1355
|
+
/**
|
|
1356
|
+
* Builds a filter for items in memory. Values match without regard to case, a free word matches the start of a word in
|
|
1357
|
+
* the schema's `text`, parts that could not be bound filter nothing, and fields with `expand` filter by what they
|
|
1358
|
+
* stand for.
|
|
1359
|
+
*/
|
|
1360
|
+
function toPredicate(query, schema, context = {}) {
|
|
1361
|
+
return fold(resolveTokens(evaluable(query, schema), schema, context), schema, {
|
|
1362
|
+
and: (children) => (item) => children.every((test) => test(item)),
|
|
1363
|
+
or: (children) => (item) => children.some((test) => test(item)),
|
|
1364
|
+
not: (child) => (item) => !child(item),
|
|
1365
|
+
in: (condition) => matchesIn(condition, schema),
|
|
1366
|
+
range: (condition) => matchesRange(condition, schema),
|
|
1367
|
+
text: (condition) => matchesText(condition, schema),
|
|
1368
|
+
empty: (condition) => (item) => fieldValues(item, condition.field, schema).length == 0,
|
|
1369
|
+
match: (condition) => matchesPattern(condition, schema)
|
|
1370
|
+
});
|
|
1371
|
+
}
|
|
1372
|
+
function matchesIn(condition, schema) {
|
|
1373
|
+
const type = fieldType(condition.field, schema);
|
|
1374
|
+
return (item) => fieldValues(item, condition.field, schema).some((value) => condition.values.some((expected) => valuesEqual(value, expected, type)));
|
|
1375
|
+
}
|
|
1376
|
+
function matchesRange(condition, schema) {
|
|
1377
|
+
const type = fieldType(condition.field, schema);
|
|
1378
|
+
return (item) => fieldValues(item, condition.field, schema).some((value) => isAbove(value, condition.min, type) && isBelow(value, condition.max, type));
|
|
1379
|
+
}
|
|
1380
|
+
function matchesText(condition, schema) {
|
|
1381
|
+
const word = condition.value.toLowerCase();
|
|
1382
|
+
return (item) => {
|
|
1383
|
+
const text = schema.text?.(item);
|
|
1384
|
+
return text == void 0 || startsAWord(text.toLowerCase(), word);
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
function matchesPattern(condition, schema) {
|
|
1388
|
+
const type = fieldType(condition.field, schema);
|
|
1389
|
+
const pattern = new RegExp(`^${condition.pattern.split(WILDCARD).map(escapeRegExp).join(".*")}$`, "is");
|
|
1390
|
+
return (item) => fieldValues(item, condition.field, schema).some((value) => pattern.test(formatValue(value, type)));
|
|
1391
|
+
}
|
|
1392
|
+
function isAbove(value, bound, type) {
|
|
1393
|
+
if (bound == void 0) return true;
|
|
1394
|
+
const order = compareValues(value, bound.value, type);
|
|
1395
|
+
return order > 0 || order == 0 && bound.inclusive;
|
|
1396
|
+
}
|
|
1397
|
+
function isBelow(value, bound, type) {
|
|
1398
|
+
if (bound == void 0) return true;
|
|
1399
|
+
const order = compareValues(value, bound.value, type);
|
|
1400
|
+
return order < 0 || order == 0 && bound.inclusive;
|
|
1401
|
+
}
|
|
1402
|
+
function valuesEqual(a, b, type) {
|
|
1403
|
+
if (type?.equals != void 0) return type.equals(a, b);
|
|
1404
|
+
if (type?.compare != void 0) return type.compare(a, b) == 0;
|
|
1405
|
+
return a == b;
|
|
1406
|
+
}
|
|
1407
|
+
function compareValues(a, b, type) {
|
|
1408
|
+
return type?.compare?.(a, b) ?? NaN;
|
|
1409
|
+
}
|
|
1410
|
+
function formatValue(value, type) {
|
|
1411
|
+
return type == void 0 ? String(value) : type.format(value);
|
|
1412
|
+
}
|
|
1413
|
+
function startsAWord(text, word) {
|
|
1414
|
+
let index = text.indexOf(word);
|
|
1415
|
+
while (index != -1) {
|
|
1416
|
+
if (index == 0 || !WORD_CHARACTER.test(text[index - 1])) return true;
|
|
1417
|
+
index = text.indexOf(word, index + 1);
|
|
1418
|
+
}
|
|
1419
|
+
return false;
|
|
1420
|
+
}
|
|
1421
|
+
function fieldValues(item, field, schema) {
|
|
1422
|
+
const definition = findField(schema, field);
|
|
1423
|
+
const value = definition?.get == void 0 ? ownProperty(item, field) : definition.get(item);
|
|
1424
|
+
if (value == void 0) return [];
|
|
1425
|
+
return Array.isArray(value) ? value.filter((element) => element != void 0) : [value];
|
|
1426
|
+
}
|
|
1427
|
+
function ownProperty(item, key) {
|
|
1428
|
+
return typeof item == "object" && item != null && Object.hasOwn(item, key) ? item[key] : void 0;
|
|
1429
|
+
}
|
|
1430
|
+
function fieldType(field, schema) {
|
|
1431
|
+
return findField(schema, field)?.type;
|
|
1432
|
+
}
|
|
1433
|
+
function escapeRegExp(text) {
|
|
1434
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1435
|
+
}
|
|
1436
|
+
// src/query/stringify.ts
|
|
1437
|
+
const NO_SPAN = {
|
|
1438
|
+
from: 0,
|
|
1439
|
+
to: 0
|
|
1440
|
+
};
|
|
1441
|
+
const UNBIND_BY_TYPE = {
|
|
1442
|
+
and: (query, schema) => group("and", query.children.map((child) => unbindNode(child, schema))),
|
|
1443
|
+
or: unbindOr,
|
|
1444
|
+
not: (query, schema) => unbindNegated(query.child, schema),
|
|
1445
|
+
in: (query, schema) => term(query.field, query.values.map((value) => equalValue(value, query.field, schema))),
|
|
1446
|
+
range: unbindRange,
|
|
1447
|
+
text: (query) => ({
|
|
1448
|
+
type: "text",
|
|
1449
|
+
value: query.value,
|
|
1450
|
+
quoted: true,
|
|
1451
|
+
span: NO_SPAN
|
|
1452
|
+
}),
|
|
1453
|
+
empty: (query) => term("no", [{
|
|
1454
|
+
kind: "equal",
|
|
1455
|
+
raw: query.field,
|
|
1456
|
+
quoted: true,
|
|
1457
|
+
span: NO_SPAN
|
|
1458
|
+
}]),
|
|
1459
|
+
match: (query) => term(query.field, [{
|
|
1460
|
+
kind: "equal",
|
|
1461
|
+
raw: query.pattern,
|
|
1462
|
+
quoted: false,
|
|
1463
|
+
span: NO_SPAN
|
|
1464
|
+
}]),
|
|
1465
|
+
invalid: (query) => query.syntax
|
|
1466
|
+
};
|
|
1467
|
+
const UNBIND_NEGATED_BY_TYPE = {
|
|
1468
|
+
and: (query, schema) => negateSyntax(unbindNode(query, schema)),
|
|
1469
|
+
or: (query, schema) => negateSyntax(unbindNode(query, schema)),
|
|
1470
|
+
not: (query, schema) => unbindNode(query.child, schema),
|
|
1471
|
+
in: (query, schema) => negateSyntax(unbindNode(query, schema)),
|
|
1472
|
+
range: (query, schema) => negateSyntax(unbindNode(query, schema)),
|
|
1473
|
+
text: (query, schema) => negateSyntax(unbindNode(query, schema)),
|
|
1474
|
+
empty: (query) => term("has", [{
|
|
1475
|
+
kind: "equal",
|
|
1476
|
+
raw: query.field,
|
|
1477
|
+
quoted: true,
|
|
1478
|
+
span: NO_SPAN
|
|
1479
|
+
}]),
|
|
1480
|
+
match: (query, schema) => negateSyntax(unbindNode(query, schema)),
|
|
1481
|
+
invalid: (query) => ({
|
|
1482
|
+
type: "not",
|
|
1483
|
+
child: query.syntax,
|
|
1484
|
+
span: NO_SPAN
|
|
1485
|
+
})
|
|
1486
|
+
};
|
|
1487
|
+
const NEGATE_SYNTAX_BY_TYPE = {
|
|
1488
|
+
and: (node) => group("or", node.children.map(negateSyntax)),
|
|
1489
|
+
or: (node) => group("and", node.children.map(negateSyntax)),
|
|
1490
|
+
not: (node) => node.child,
|
|
1491
|
+
term: (node) => ({
|
|
1492
|
+
type: "not",
|
|
1493
|
+
child: node,
|
|
1494
|
+
span: NO_SPAN
|
|
1495
|
+
}),
|
|
1496
|
+
text: (node) => ({
|
|
1497
|
+
type: "not",
|
|
1498
|
+
child: node,
|
|
1499
|
+
span: NO_SPAN
|
|
1500
|
+
})
|
|
1501
|
+
};
|
|
1502
|
+
/**
|
|
1503
|
+
* Writes a query as canonical text. A negated group is written by negating its parts, and an `OR` of one field's
|
|
1504
|
+
* values as one comma list.
|
|
1505
|
+
*/
|
|
1506
|
+
function stringify(query, schema) {
|
|
1507
|
+
return writeSyntax(unbindNode(normalize(query, schema), schema));
|
|
1508
|
+
}
|
|
1509
|
+
/** The canonical form of a query text. Writing a canonical text again changes nothing. */
|
|
1510
|
+
function canonical(text, schema) {
|
|
1511
|
+
return stringify(bind(parse(text).tree, schema).query, schema);
|
|
1512
|
+
}
|
|
1513
|
+
function unbindNode(query, schema) {
|
|
1514
|
+
const unbinder = UNBIND_BY_TYPE[query.type];
|
|
1515
|
+
return unbinder(query, schema);
|
|
1516
|
+
}
|
|
1517
|
+
function unbindNegated(query, schema) {
|
|
1518
|
+
const unbinder = UNBIND_NEGATED_BY_TYPE[query.type];
|
|
1519
|
+
return unbinder(query, schema);
|
|
1520
|
+
}
|
|
1521
|
+
function negateSyntax(node) {
|
|
1522
|
+
const negator = NEGATE_SYNTAX_BY_TYPE[node.type];
|
|
1523
|
+
return negator(node);
|
|
1524
|
+
}
|
|
1525
|
+
function unbindOr(query, schema) {
|
|
1526
|
+
const children = query.children.map((child) => unbindNode(child, schema));
|
|
1527
|
+
const terms = children.filter((child) => child.type == "term");
|
|
1528
|
+
return terms.length == children.length && terms.every((child) => child.key == terms[0].key) ? term(terms[0].key, terms.flatMap((child) => child.values)) : group("or", children);
|
|
1529
|
+
}
|
|
1530
|
+
function unbindRange(query, schema) {
|
|
1531
|
+
const { min, max, field } = query;
|
|
1532
|
+
if (min != void 0 && max != void 0 && min.inclusive && max.inclusive) return term(field, [{
|
|
1533
|
+
kind: "range",
|
|
1534
|
+
min: rangeBound(min, field, schema),
|
|
1535
|
+
max: rangeBound(max, field, schema),
|
|
1536
|
+
separator: NO_SPAN,
|
|
1537
|
+
span: NO_SPAN
|
|
1538
|
+
}]);
|
|
1539
|
+
return group("and", [min && term(field, [compareValue(min.inclusive ? ">=" : ">", min, field, schema)]), max && term(field, [compareValue(max.inclusive ? "<=" : "<", max, field, schema)])].filter((part) => part != void 0));
|
|
1540
|
+
}
|
|
1541
|
+
function equalValue(value, field, schema) {
|
|
1542
|
+
return {
|
|
1543
|
+
kind: "equal",
|
|
1544
|
+
...writtenValue(value, field, schema),
|
|
1545
|
+
span: NO_SPAN
|
|
1546
|
+
};
|
|
1547
|
+
}
|
|
1548
|
+
function compareValue(operator, bound, field, schema) {
|
|
1549
|
+
return {
|
|
1550
|
+
kind: "compare",
|
|
1551
|
+
operator,
|
|
1552
|
+
...writtenValue(bound.value, field, schema),
|
|
1553
|
+
span: NO_SPAN
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
function rangeBound(bound, field, schema) {
|
|
1557
|
+
return writtenValue(bound.value, field, schema);
|
|
1558
|
+
}
|
|
1559
|
+
function writtenValue(value, field, schema) {
|
|
1560
|
+
if (isTokenValue(value)) return {
|
|
1561
|
+
raw: value.raw,
|
|
1562
|
+
quoted: false
|
|
1563
|
+
};
|
|
1564
|
+
const type = findField(schema, field)?.type;
|
|
1565
|
+
return {
|
|
1566
|
+
raw: type == void 0 ? String(value) : type.format(value),
|
|
1567
|
+
quoted: true
|
|
1568
|
+
};
|
|
1569
|
+
}
|
|
1570
|
+
function term(key, values) {
|
|
1571
|
+
return {
|
|
1572
|
+
type: "term",
|
|
1573
|
+
key,
|
|
1574
|
+
keySpan: NO_SPAN,
|
|
1575
|
+
values,
|
|
1576
|
+
span: NO_SPAN
|
|
1577
|
+
};
|
|
1578
|
+
}
|
|
1579
|
+
function group(type, children) {
|
|
1580
|
+
return children.length == 1 ? children[0] : {
|
|
1581
|
+
type,
|
|
1582
|
+
children,
|
|
1583
|
+
span: NO_SPAN
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
// src/query/values-of.ts
|
|
1587
|
+
/** The values a field is set to for the whole query, such as the value of `sort:`. Values inside `OR` or `NOT` do not count. */
|
|
1588
|
+
function valuesOf(query, field) {
|
|
1589
|
+
return (query.type == "and" ? query.children : [query]).flatMap((part) => part.type == "in" && part.field == field ? part.values : []);
|
|
1590
|
+
}
|
|
1591
|
+
// src/query/without-field.ts
|
|
1592
|
+
const REMOVE_BY_TYPE = {
|
|
1593
|
+
and: (query, field) => allOf(removeFromChildren(query.children, field)),
|
|
1594
|
+
or: (query, field) => anyOf(removeFromChildren(query.children, field)),
|
|
1595
|
+
not: (query, field) => {
|
|
1596
|
+
const child = removeNode(query.child, field);
|
|
1597
|
+
return child == void 0 ? void 0 : {
|
|
1598
|
+
type: "not",
|
|
1599
|
+
child
|
|
1600
|
+
};
|
|
1601
|
+
},
|
|
1602
|
+
in: (query, field) => keepUnlessField(query, field),
|
|
1603
|
+
range: (query, field) => keepUnlessField(query, field),
|
|
1604
|
+
text: (query) => query,
|
|
1605
|
+
empty: (query, field) => keepUnlessField(query, field),
|
|
1606
|
+
match: (query, field) => keepUnlessField(query, field),
|
|
1607
|
+
invalid: (query, field) => query.syntax.type == "term" && query.syntax.key == field ? void 0 : query
|
|
1608
|
+
};
|
|
1609
|
+
/** Removes every condition on a field, `has:` and `no:` included, and keeps the rest of the query as it was. */
|
|
1610
|
+
function withoutField(query, field) {
|
|
1611
|
+
return removeNode(query, field) ?? emptyQuery();
|
|
1612
|
+
}
|
|
1613
|
+
function removeNode(query, field) {
|
|
1614
|
+
const remover = REMOVE_BY_TYPE[query.type];
|
|
1615
|
+
return remover(query, field);
|
|
1616
|
+
}
|
|
1617
|
+
function removeFromChildren(children, field) {
|
|
1618
|
+
return children.flatMap((child) => removeNode(child, field) ?? []);
|
|
1619
|
+
}
|
|
1620
|
+
function keepUnlessField(query, field) {
|
|
1621
|
+
return query.field == field ? void 0 : query;
|
|
1622
|
+
}
|
|
1623
|
+
// src/schema/types/boolean-type.ts
|
|
1624
|
+
const BOOLEANS = [true, false];
|
|
1625
|
+
/** `true` or `false`, in any case. */
|
|
1626
|
+
function boolean() {
|
|
1627
|
+
return {
|
|
1628
|
+
parse: (raw) => BOOLEANS.find((value) => equalsIgnoringCase(String(value), raw)),
|
|
1629
|
+
format: (value) => String(value),
|
|
1630
|
+
suggest: (prefix) => BOOLEANS.filter((value) => startsWithIgnoringCase(String(value), prefix)).map((value) => ({ value }))
|
|
1631
|
+
};
|
|
1632
|
+
}
|
|
1633
|
+
// src/schema/types/date-type.ts
|
|
1634
|
+
const DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
1635
|
+
const DATE_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:\d{2})?$/i;
|
|
1636
|
+
const TIME_ZONE = /(?:Z|[+-]\d{2}:\d{2})$/i;
|
|
1637
|
+
const DAY_LENGTH = 864e5;
|
|
1638
|
+
const MONTHS_IN_YEAR = 12;
|
|
1639
|
+
const LAST_YEAR = 9999;
|
|
1640
|
+
const DAYS_IN_WEEK = 7;
|
|
1641
|
+
const SHIFT_BY_UNIT = {
|
|
1642
|
+
d: (day, amount) => Date.UTC(day.year, day.month, day.day + amount),
|
|
1643
|
+
w: (day, amount) => Date.UTC(day.year, day.month, day.day + amount * DAYS_IN_WEEK),
|
|
1644
|
+
m: (day, amount) => shiftMonths(day, amount),
|
|
1645
|
+
y: (day, amount) => shiftMonths(day, amount * MONTHS_IN_YEAR)
|
|
1646
|
+
};
|
|
1647
|
+
const todayToken = {
|
|
1648
|
+
description: "The current day",
|
|
1649
|
+
offsetUnits: Object.keys(SHIFT_BY_UNIT),
|
|
1650
|
+
resolve: (offset, context) => formatDay(shiftDay(context.now, offset))
|
|
1651
|
+
};
|
|
1652
|
+
/**
|
|
1653
|
+
* A day or an instant in UTC. A day covers all of it, so `created:2026-01-31` is any time that day and
|
|
1654
|
+
* `created:>2026-01-31` starts the day after. `@today` is the current day in UTC.
|
|
1655
|
+
*/
|
|
1656
|
+
function date(options = {}) {
|
|
1657
|
+
return {
|
|
1658
|
+
parse: parseDate,
|
|
1659
|
+
format: (value) => typeof value == "string" ? value : formatInstant(value.getTime()),
|
|
1660
|
+
compare: compareDates,
|
|
1661
|
+
suggest: options.suggest,
|
|
1662
|
+
tokens: {
|
|
1663
|
+
today: todayToken,
|
|
1664
|
+
...options.tokens
|
|
1665
|
+
}
|
|
1666
|
+
};
|
|
1667
|
+
}
|
|
1668
|
+
function parseDate(raw) {
|
|
1669
|
+
if (DAY.test(raw)) return dayStart(raw) == void 0 ? void 0 : raw;
|
|
1670
|
+
if (!DATE_TIME.test(raw)) return void 0;
|
|
1671
|
+
const time = Date.parse(TIME_ZONE.test(raw) ? raw : `${raw}Z`);
|
|
1672
|
+
if (!Number.isFinite(time)) return void 0;
|
|
1673
|
+
const year = new Date(time).getUTCFullYear();
|
|
1674
|
+
return year < 0 || year > LAST_YEAR ? void 0 : formatInstant(time);
|
|
1675
|
+
}
|
|
1676
|
+
function compareDates(a, b) {
|
|
1677
|
+
const first = toInterval(a);
|
|
1678
|
+
const second = toInterval(b);
|
|
1679
|
+
if (first == void 0 || second == void 0) return NaN;
|
|
1680
|
+
if (first.end <= second.start) return -1;
|
|
1681
|
+
if (first.start >= second.end) return 1;
|
|
1682
|
+
return 0;
|
|
1683
|
+
}
|
|
1684
|
+
function toInterval(value) {
|
|
1685
|
+
const start = typeof value == "string" ? dayStart(value) ?? Date.parse(value) : value.getTime();
|
|
1686
|
+
if (!Number.isFinite(start)) return void 0;
|
|
1687
|
+
return {
|
|
1688
|
+
start,
|
|
1689
|
+
end: start + (typeof value == "string" && DAY.test(value) ? DAY_LENGTH : 1)
|
|
1690
|
+
};
|
|
1691
|
+
}
|
|
1692
|
+
function dayStart(day) {
|
|
1693
|
+
const match = DAY.exec(day);
|
|
1694
|
+
if (match == void 0) return void 0;
|
|
1695
|
+
const [year, month, date] = match.slice(1).map(Number);
|
|
1696
|
+
const start = Date.UTC(year, month - 1, date);
|
|
1697
|
+
return formatDay(start) == day ? start : void 0;
|
|
1698
|
+
}
|
|
1699
|
+
function shiftDay(now, offset) {
|
|
1700
|
+
const day = {
|
|
1701
|
+
year: now.getUTCFullYear(),
|
|
1702
|
+
month: now.getUTCMonth(),
|
|
1703
|
+
day: now.getUTCDate()
|
|
1704
|
+
};
|
|
1705
|
+
const shift = SHIFT_BY_UNIT[offset?.unit ?? "d"];
|
|
1706
|
+
return shift(day, offset?.amount ?? 0);
|
|
1707
|
+
}
|
|
1708
|
+
function shiftMonths(day, amount) {
|
|
1709
|
+
const month = day.month + amount;
|
|
1710
|
+
const lastDay = new Date(Date.UTC(day.year, month + 1, 0)).getUTCDate();
|
|
1711
|
+
return Date.UTC(day.year, month, Math.min(day.day, lastDay));
|
|
1712
|
+
}
|
|
1713
|
+
function formatDay(time) {
|
|
1714
|
+
const value = new Date(time);
|
|
1715
|
+
return `${String(value.getUTCFullYear()).padStart(4, "0")}-${String(value.getUTCMonth() + 1).padStart(2, "0")}-${String(value.getUTCDate()).padStart(2, "0")}`;
|
|
1716
|
+
}
|
|
1717
|
+
function formatInstant(time) {
|
|
1718
|
+
return Number.isFinite(time) ? new Date(time).toISOString().replace(".000Z", "Z") : String(time);
|
|
1719
|
+
}
|
|
1720
|
+
// src/schema/types/enum-type.ts
|
|
1721
|
+
/**
|
|
1722
|
+
* One of a fixed list. A value written in another case reads as the listed one, so `is:OPEN` is `is:open`, and a value
|
|
1723
|
+
* outside the list is an invalid value. Values that come from data, such as labels, belong in a `string()` field.
|
|
1724
|
+
*/
|
|
1725
|
+
function enumOf(values, options = {}) {
|
|
1726
|
+
return {
|
|
1727
|
+
parse: (raw) => values.find((value) => equalsIgnoringCase(value, raw)),
|
|
1728
|
+
format: (value) => value,
|
|
1729
|
+
equals: equalsIgnoringCase,
|
|
1730
|
+
wildcards: true,
|
|
1731
|
+
suggest: (prefix) => values.filter((value) => startsWithIgnoringCase(value, prefix)).map((value) => ({ value })),
|
|
1732
|
+
...options
|
|
1733
|
+
};
|
|
1734
|
+
}
|
|
1735
|
+
// src/schema/types/number-type.ts
|
|
1736
|
+
const NUMBER = /^-?(?:\d+(?:\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
|
1737
|
+
/** A number with a dot for decimals. It takes comparisons and ranges. */
|
|
1738
|
+
function number(options = {}) {
|
|
1739
|
+
return {
|
|
1740
|
+
parse: parseNumber,
|
|
1741
|
+
format: (value) => String(value),
|
|
1742
|
+
compare: (a, b) => a - b,
|
|
1743
|
+
...options
|
|
1744
|
+
};
|
|
1745
|
+
}
|
|
1746
|
+
function parseNumber(raw) {
|
|
1747
|
+
if (!NUMBER.test(raw)) return void 0;
|
|
1748
|
+
const value = Number(raw);
|
|
1749
|
+
return Number.isFinite(value) ? value : void 0;
|
|
1750
|
+
}
|
|
1751
|
+
// src/schema/types/string-type.ts
|
|
1752
|
+
/** Any text. Values match without regard to case, and `*` works as a wildcard. */
|
|
1753
|
+
function string(options = {}) {
|
|
1754
|
+
return {
|
|
1755
|
+
parse: (raw) => raw,
|
|
1756
|
+
format: (value) => value,
|
|
1757
|
+
equals: equalsIgnoringCase,
|
|
1758
|
+
wildcards: true,
|
|
1759
|
+
...options
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
// src/syntax/highlight.ts
|
|
1763
|
+
/** Spans to color, in order. Whitespace and nothing else is left out. */
|
|
1764
|
+
function highlight(text) {
|
|
1765
|
+
return scan(text).highlights;
|
|
1766
|
+
}
|
|
1767
|
+
export { DIAGNOSTIC_CODE, DIAGNOSTIC_SEVERITY, FLAT_EMPTY, HIGHLIGHT_KIND, bind, boolean, canonical, date, defineSchema, enumOf, fieldNameOf, fold, fromFlat, getCompletionContext, getSuggestions, highlight, isTokenValue, number, parse, resolveTokens, string, stringify, toFlat, toPredicate, valuesOf, withoutField };
|