@dxos/echo-query 0.8.4-main.e99c46d → 0.8.4-main.ead640a
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/README.md +1 -1
- package/dist/lib/browser/index.mjs +528 -0
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +528 -0
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/query-lite/index.js +387 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/parser/gen/index.d.ts +8 -0
- package/dist/types/src/parser/gen/index.d.ts.map +1 -0
- package/dist/types/src/parser/gen/query.d.ts +3 -0
- package/dist/types/src/parser/gen/query.d.ts.map +1 -0
- package/dist/types/src/parser/gen/query.terms.d.ts +2 -0
- package/dist/types/src/parser/gen/query.terms.d.ts.map +1 -0
- package/dist/types/src/parser/index.d.ts +3 -0
- package/dist/types/src/parser/index.d.ts.map +1 -0
- package/dist/types/src/parser/query-builder.d.ts +74 -0
- package/dist/types/src/parser/query-builder.d.ts.map +1 -0
- package/dist/types/src/parser/query.test.d.ts +2 -0
- package/dist/types/src/parser/query.test.d.ts.map +1 -0
- package/dist/types/src/query-lite/index.d.ts +2 -0
- package/dist/types/src/query-lite/index.d.ts.map +1 -0
- package/dist/types/src/query-lite/query-lite.d.ts +8 -0
- package/dist/types/src/query-lite/query-lite.d.ts.map +1 -0
- package/dist/types/src/sandbox/query-sandbox.d.ts +21 -0
- package/dist/types/src/sandbox/query-sandbox.d.ts.map +1 -0
- package/dist/types/src/sandbox/query-sandbox.test.d.ts +2 -0
- package/dist/types/src/sandbox/query-sandbox.test.d.ts.map +1 -0
- package/dist/types/src/sandbox/quickjs.d.ts +8 -0
- package/dist/types/src/sandbox/quickjs.d.ts.map +1 -0
- package/dist/types/src/sandbox/quickjs.test.d.ts +2 -0
- package/dist/types/src/sandbox/quickjs.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -6
- package/src/env.d.ts +8 -0
- package/src/index.ts +2 -0
- package/src/parser/gen/index.ts +13 -0
- package/src/parser/gen/query.terms.ts +26 -0
- package/src/parser/gen/query.ts +18 -0
- package/src/parser/index.ts +6 -0
- package/src/parser/query-builder.ts +486 -0
- package/src/parser/query.grammar +124 -0
- package/src/parser/query.test.ts +363 -0
- package/src/query-lite/index.ts +5 -0
- package/src/query-lite/query-lite.ts +467 -0
- package/src/sandbox/query-sandbox.test.ts +52 -0
- package/src/sandbox/query-sandbox.ts +72 -0
- package/src/sandbox/quickjs.test.ts +67 -0
- package/src/sandbox/quickjs.ts +34 -0
- package/dist/types/src/search.test.d.ts +0 -2
- package/dist/types/src/search.test.d.ts.map +0 -1
- package/src/search.test.ts +0 -49
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/echo-query",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.ead640a",
|
|
4
4
|
"description": "ECHO queries.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "info@dxos.org",
|
|
9
|
-
"sideEffects":
|
|
9
|
+
"sideEffects": false,
|
|
10
10
|
"type": "module",
|
|
11
|
+
"imports": {
|
|
12
|
+
"#query-lite": "./dist/query-lite/index.js"
|
|
13
|
+
},
|
|
11
14
|
"exports": {
|
|
12
15
|
".": {
|
|
13
16
|
"source": "./src/index.ts",
|
|
@@ -25,13 +28,24 @@
|
|
|
25
28
|
"src"
|
|
26
29
|
],
|
|
27
30
|
"dependencies": {
|
|
31
|
+
"@lezer/common": "^1.2.2",
|
|
32
|
+
"@lezer/lezer": "^1.1.2",
|
|
33
|
+
"@lezer/lr": "^1.4.2",
|
|
28
34
|
"@orama/orama": "^3.1.7",
|
|
29
|
-
"@dxos/
|
|
30
|
-
"@dxos/
|
|
35
|
+
"@dxos/context": "0.8.4-main.ead640a",
|
|
36
|
+
"@dxos/debug": "0.8.4-main.ead640a",
|
|
37
|
+
"@dxos/echo": "0.8.4-main.ead640a",
|
|
38
|
+
"@dxos/echo-protocol": "0.8.4-main.ead640a",
|
|
39
|
+
"@dxos/invariant": "0.8.4-main.ead640a",
|
|
40
|
+
"@dxos/node-std": "0.8.4-main.ead640a",
|
|
41
|
+
"@dxos/errors": "0.8.4-main.ead640a",
|
|
42
|
+
"@dxos/util": "0.8.4-main.ead640a",
|
|
43
|
+
"@dxos/vendor-quickjs": "0.8.4-main.ead640a"
|
|
31
44
|
},
|
|
32
45
|
"devDependencies": {
|
|
33
|
-
"@
|
|
34
|
-
"@dxos/random": "0.8.4-main.
|
|
46
|
+
"@lezer/generator": "^1.7.1",
|
|
47
|
+
"@dxos/random": "0.8.4-main.ead640a",
|
|
48
|
+
"@dxos/echo-generator": "0.8.4-main.ead640a"
|
|
35
49
|
},
|
|
36
50
|
"publishConfig": {
|
|
37
51
|
"access": "public"
|
package/src/env.d.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { LRParser } from '@lezer/lr';
|
|
6
|
+
import { parser } from './query';
|
|
7
|
+
import * as terms from './query.terms';
|
|
8
|
+
|
|
9
|
+
export namespace QueryDSL {
|
|
10
|
+
export const Parser: LRParser = parser;
|
|
11
|
+
export const Node = terms;
|
|
12
|
+
export const Tokens = ['type:', 'AND', 'OR', 'NOT'];
|
|
13
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
2
|
+
export const
|
|
3
|
+
Query = 1,
|
|
4
|
+
Filter = 2,
|
|
5
|
+
TagFilter = 3,
|
|
6
|
+
Tag = 4,
|
|
7
|
+
TextFilter = 5,
|
|
8
|
+
String = 6,
|
|
9
|
+
TypeFilter = 7,
|
|
10
|
+
Identifier = 8,
|
|
11
|
+
TypeKeyword = 9,
|
|
12
|
+
PropertyFilter = 11,
|
|
13
|
+
PropertyPath = 12,
|
|
14
|
+
Value = 14,
|
|
15
|
+
Number = 15,
|
|
16
|
+
Boolean = 16,
|
|
17
|
+
Null = 17,
|
|
18
|
+
ObjectLiteral = 18,
|
|
19
|
+
ObjectProperty = 20,
|
|
20
|
+
ArrayLiteral = 23,
|
|
21
|
+
Not = 26,
|
|
22
|
+
And = 27,
|
|
23
|
+
Or = 28,
|
|
24
|
+
Relation = 29,
|
|
25
|
+
ArrowRight = 30,
|
|
26
|
+
ArrowLeft = 31
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
2
|
+
import {LRParser} from "@lezer/lr"
|
|
3
|
+
const spec_Identifier = {__proto__:null,type:18, NOT:82, not:82, "!":82, AND:84, and:84, OR:86, or:86}
|
|
4
|
+
export const parser = LRParser.deserialize({
|
|
5
|
+
version: 14,
|
|
6
|
+
states: "(QOVQPOOOOQO'#C_'#C_OOQO'#Ca'#CaOnQPO'#CcOsQPO'#ChO{QPO'#CnO!TQPO'#CgOOQO'#C^'#C^OOQO'#Cv'#CvOOQO'#DU'#DUOVQPO'#DUQ!YQPOOOVQPO'#DUO!jQPO,58}O!oQPO'#DOO!tQPO,59SO!|QPO'#CpOOQO,59Y,59YO#RQPO,59YO#ZQQO,59RO#oQPO,59pOOQO'#Cw'#CwOOQO'#Cx'#CxOOQO'#Cy'#CyOVQPO,59pOVQPO,59pOVQPO,59pO$jQPO,59pOOQO1G.i1G.iOOQO,59j,59jOOQO-E6|-E6|O#ZQQO,59[O$}QPO'#DPO%SQPO1G.tOOQO1G.t1G.tO%[QQO'#CsOOQO'#Cj'#CjOOQO1G.m1G.mO%cQPO1G/[O%yQPO1G/[O&aQPO1G/[OOQO1G/[1G/[OOQO1G.v1G.vOOQO,59k,59kOOQO-E6}-E6}OOQO7+$`7+$`OOQO,59_,59_O&wQPO,59_O#ZQQO'#DQO'PQPO1G.yOOQO1G.y1G.yOOQO,59l,59lOOQO-E7O-E7OOOQO7+$e7+$e",
|
|
7
|
+
stateData: "'X~OwOS~OSPOUQOWSOXROcTOp[OyWO~OY]O~O]^OY[X~OW`OfaO~OYcO~OngOogOzeO{fO~PVOWlO~OWmO~O]^OY[a~OYoO~OepOfrO~OUtO_tO`tOatOcTOhsO~OyWOSxaUxaWxaXxacxanxaoxapxauxazxa{xaqxa~OngOogOqyOzeO{fO~PVOW`O~OepOf}O~Oi!OO~P#ZOnxioxiuxizxi{xiqxi~PVOzeOnxioxiuxi{xiqxi~PVOzeO{fOnxioxiuxiqxi~PVOe!QOi!SO~Oe!QOi!VO~O",
|
|
8
|
+
goto: "%TyPPz!XP!XP!XPPP!X!fP!sPPP#PP#dPP#^PP#j#x$O$TPPPP$X$_$ePPP$kgXOYZ[hijkvwxgVOYZ[hijkvwxgUOYZ[hijkvwxQucQzoQ!PsR!T!QfVOYZ[hijkvwxXtcos!QQbTR{piYOYZ[dhijkvwxXhZkwxViZkxTjZkQ_SRn_QqbR|qQ!R!PR!U!RQZO^dYZdkvwxQk[QvhQwiRxj",
|
|
9
|
+
nodeNames: "⚠ Query Filter TagFilter Tag TextFilter String TypeFilter Identifier TypeKeyword : PropertyFilter PropertyPath . Value Number Boolean Null ObjectLiteral { ObjectProperty , } ArrayLiteral [ ] Not And Or Relation ArrowRight ArrowLeft ( )",
|
|
10
|
+
maxTerm: 43,
|
|
11
|
+
skippedNodes: [0],
|
|
12
|
+
repeatNodeCount: 3,
|
|
13
|
+
tokenData: "2w~RrX^#]pq#]rs$Qst%nwx&fxy'}yz(S|}(X}!O(^!O!P)}!Q![(g![!]*S!^!_*X!c!}*d!}#O+O#P#Q+T#R#S*d#T#Y*d#Y#Z+Y#Z#b*d#b#c.i#c#h*d#h#i1Z#i#o*d#o#p2m#q#r2r#y#z#]$f$g#]#BY#BZ#]$IS$I_#]$I|$JO#]$JT$JU#]$KV$KW#]&FU&FV#]~#bYw~X^#]pq#]#y#z#]$f$g#]#BY#BZ#]$IS$I_#]$I|$JO#]$JT$JU#]$KV$KW#]&FU&FV#]~$TVOr$Qrs$js#O$Q#O#P$o#P;'S$Q;'S;=`%h<%lO$Q~$oOU~~$rRO;'S$Q;'S;=`${;=`O$Q~%OWOr$Qrs$js#O$Q#O#P$o#P;'S$Q;'S;=`%h;=`<%l$Q<%lO$Q~%kP;=`<%l$Q~%qT}!O&Q!Q![&Q!c!}&Q#R#S&Q#T#o&Q~&VTS~}!O&Q!Q![&Q!c!}&Q#R#S&Q#T#o&Q~&iVOw&fwx$jx#O&f#O#P'O#P;'S&f;'S;=`'w<%lO&f~'RRO;'S&f;'S;=`'[;=`O&f~'_WOw&fwx$jx#O&f#O#P'O#P;'S&f;'S;=`'w;=`<%l&f<%lO&f~'zP;=`<%l&f~(SOp~~(XOq~~(^Oe~~(aQ!Q![(g!`!a)x~(lS_~!O!P(x!Q![(g!g!h)^#X#Y)^~({P!Q![)O~)TR_~!Q![)O!g!h)^#X#Y)^~)aR{|)j}!O)j!Q![)p~)mP!Q![)p~)uP_~!Q![)p~)}On~~*SO]~~*XOY~~*[P}!O*_~*dOo~P*iVWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#o*d~+TOh~~+YOi~R+_WWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#U+w#U#o*dR+|XWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#`*d#`#a,i#a#o*dR,nXWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#g*d#g#h-Z#h#o*dR-`XWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#X*d#X#Y-{#Y#o*dR.SV`QWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#o*dR.nXWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#i*d#i#j/Z#j#o*dR/`XWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#`*d#`#a/{#a#o*dR0QXWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#`*d#`#a0m#a#o*dR0tVWPaQ}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#o*dR1`XWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#f*d#f#g1{#g#o*dR2QXWP}!O*d!O!P*d!P!Q*d!Q![*d!c!}*d#R#S*d#T#i*d#i#j-Z#j#o*d~2rOc~~2wOf~",
|
|
14
|
+
tokenizers: [0, 1],
|
|
15
|
+
topRules: {"Query":[0,1]},
|
|
16
|
+
specialized: [{term: 8, get: (value: keyof typeof spec_Identifier) => spec_Identifier[value] || -1}],
|
|
17
|
+
tokenPrec: 0
|
|
18
|
+
})
|
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Parser, type Tree, type TreeCursor } from '@lezer/common';
|
|
6
|
+
|
|
7
|
+
import { Filter, type Tag } from '@dxos/echo';
|
|
8
|
+
import { invariant } from '@dxos/invariant';
|
|
9
|
+
|
|
10
|
+
import { QueryDSL } from './gen';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Stateless query builder that parses DSL trees into filters.
|
|
14
|
+
*
|
|
15
|
+
* NOTE: QueryBuilder was largely developed using Claude Sonnet 4.5 (in Windsurf)..
|
|
16
|
+
* To modify the functionality, create a minimal breaking test and direct the LLM to fix either the grammar or builder.
|
|
17
|
+
*/
|
|
18
|
+
export class QueryBuilder {
|
|
19
|
+
private readonly _parser: Parser = QueryDSL.Parser.configure({ strict: true });
|
|
20
|
+
|
|
21
|
+
constructor(private readonly _tags?: Tag.TagMap) {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Check valid input.
|
|
25
|
+
*/
|
|
26
|
+
validate(input: string): boolean {
|
|
27
|
+
try {
|
|
28
|
+
const tree = this._parser.parse(input);
|
|
29
|
+
return tree.cursor().node.name === 'Query';
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Build a query from the input string.
|
|
37
|
+
*/
|
|
38
|
+
build(input: string): Filter.Any | undefined {
|
|
39
|
+
try {
|
|
40
|
+
const tree = this._parser.parse(input);
|
|
41
|
+
return this.buildQuery(tree, input);
|
|
42
|
+
} catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Build a query from a parsed DSL tree.
|
|
49
|
+
*/
|
|
50
|
+
buildQuery(tree: Tree, input: string): Filter.Any | undefined {
|
|
51
|
+
const cursor = tree.cursor();
|
|
52
|
+
|
|
53
|
+
// Start at root (Query node).
|
|
54
|
+
if (cursor.node.name !== 'Query') {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Check if Query has multiple children (binary expression).
|
|
59
|
+
const children: Array<{ name: string; from: number; to: number }> = [];
|
|
60
|
+
if (cursor.firstChild()) {
|
|
61
|
+
do {
|
|
62
|
+
children.push({ name: cursor.node.name, from: cursor.from, to: cursor.to });
|
|
63
|
+
} while (cursor.nextSibling());
|
|
64
|
+
cursor.parent();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// If we have an operator in the children, or multiple expressions (implicit AND), parse as binary expression.
|
|
68
|
+
const hasOperator = children.some((child) => child.name === 'And' || child.name === 'Or');
|
|
69
|
+
const hasMultipleExpressions =
|
|
70
|
+
children.filter((child) => child.name === 'Filter' || child.name === 'Not' || child.name === '(').length > 1;
|
|
71
|
+
if (hasOperator || hasMultipleExpressions) {
|
|
72
|
+
return this._parseBinaryExpression(cursor, input);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Otherwise, parse the single expression.
|
|
76
|
+
if (!cursor.firstChild()) {
|
|
77
|
+
return Filter.nothing();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return this._parseExpression(cursor, input);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Parse an expression node.
|
|
85
|
+
*/
|
|
86
|
+
private _parseExpression(cursor: TreeCursor, input: string): Filter.Any | undefined {
|
|
87
|
+
const nodeName = cursor.node.name;
|
|
88
|
+
|
|
89
|
+
switch (nodeName) {
|
|
90
|
+
case 'Filter':
|
|
91
|
+
return this._parseFilter(cursor, input);
|
|
92
|
+
|
|
93
|
+
case 'Not': {
|
|
94
|
+
// Move past NOT token to the expression.
|
|
95
|
+
cursor.nextSibling();
|
|
96
|
+
const notFilter = this._parseExpression(cursor, input);
|
|
97
|
+
return notFilter ? Filter.not(notFilter) : undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
case 'And':
|
|
101
|
+
case 'Or':
|
|
102
|
+
// This is the operator node, we need to handle the binary expression.
|
|
103
|
+
// The cursor is positioned at the operator, we need to go back to parent.
|
|
104
|
+
cursor.parent();
|
|
105
|
+
return this._parseBinaryExpression(cursor, input);
|
|
106
|
+
|
|
107
|
+
case '(': {
|
|
108
|
+
// Skip opening paren.
|
|
109
|
+
cursor.nextSibling();
|
|
110
|
+
const parenFilter = this._parseExpression(cursor, input);
|
|
111
|
+
// Skip closing paren.
|
|
112
|
+
cursor.nextSibling();
|
|
113
|
+
return parenFilter;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
default: {
|
|
117
|
+
// Check if this is a binary expression (has And/Or as a child).
|
|
118
|
+
const savedPos = cursor.from;
|
|
119
|
+
if (cursor.firstChild()) {
|
|
120
|
+
// Look for And/Or operators or multiple expressions (implicit AND).
|
|
121
|
+
let hasOperator = false;
|
|
122
|
+
let expressionCount = 0;
|
|
123
|
+
do {
|
|
124
|
+
if (cursor.node.name === 'And' || cursor.node.name === 'Or') {
|
|
125
|
+
hasOperator = true;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
if (cursor.node.name === 'Filter' || cursor.node.name === 'Not' || cursor.node.name === '(') {
|
|
129
|
+
expressionCount++;
|
|
130
|
+
}
|
|
131
|
+
} while (cursor.nextSibling());
|
|
132
|
+
hasOperator = hasOperator || expressionCount > 1;
|
|
133
|
+
|
|
134
|
+
// Reset cursor to the saved position.
|
|
135
|
+
cursor.parent();
|
|
136
|
+
cursor.firstChild();
|
|
137
|
+
while (cursor.from !== savedPos && cursor.nextSibling()) {}
|
|
138
|
+
|
|
139
|
+
if (hasOperator) {
|
|
140
|
+
cursor.parent();
|
|
141
|
+
return this._parseBinaryExpression(cursor, input);
|
|
142
|
+
} else {
|
|
143
|
+
const result = this._parseExpression(cursor, input);
|
|
144
|
+
cursor.parent();
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return Filter.nothing();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Parse a binary expression (AND/OR).
|
|
155
|
+
*/
|
|
156
|
+
private _parseBinaryExpression(cursor: TreeCursor, input: string): Filter.Any {
|
|
157
|
+
const filters: Filter.Any[] = [];
|
|
158
|
+
let operator: 'and' | 'or' | null = null;
|
|
159
|
+
|
|
160
|
+
// Collect all filters and operators.
|
|
161
|
+
if (cursor.firstChild()) {
|
|
162
|
+
do {
|
|
163
|
+
const nodeName = cursor.node.name;
|
|
164
|
+
|
|
165
|
+
if (nodeName === 'And' || nodeName === 'Or') {
|
|
166
|
+
operator = nodeName.toLowerCase() as 'and' | 'or';
|
|
167
|
+
} else if (nodeName === '(') {
|
|
168
|
+
// Handle parenthesized expression.
|
|
169
|
+
// Look ahead to see if this is a binary expression.
|
|
170
|
+
const savedPos = cursor.from;
|
|
171
|
+
cursor.nextSibling(); // Move past '('
|
|
172
|
+
|
|
173
|
+
// Check if the parentheses contain a binary expression.
|
|
174
|
+
let hasBinaryOp = false;
|
|
175
|
+
do {
|
|
176
|
+
if (cursor.node.name === 'And' || cursor.node.name === 'Or') {
|
|
177
|
+
hasBinaryOp = true;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
} while (cursor.nextSibling() && cursor.node.name !== ')');
|
|
181
|
+
|
|
182
|
+
// Reset cursor to start of parenthesized content.
|
|
183
|
+
cursor.parent();
|
|
184
|
+
cursor.firstChild();
|
|
185
|
+
while (cursor.from !== savedPos && cursor.nextSibling()) {}
|
|
186
|
+
cursor.nextSibling(); // Move past '(' again.
|
|
187
|
+
|
|
188
|
+
if (hasBinaryOp) {
|
|
189
|
+
// Find the matching closing parenthesis.
|
|
190
|
+
let depth = 1;
|
|
191
|
+
const exprStart = cursor.from;
|
|
192
|
+
let exprEnd = cursor.to;
|
|
193
|
+
|
|
194
|
+
while (cursor.nextSibling() && depth > 0) {
|
|
195
|
+
if (cursor.node.name === '(') depth++;
|
|
196
|
+
else if (cursor.node.name === ')') {
|
|
197
|
+
depth--;
|
|
198
|
+
if (depth === 0) {
|
|
199
|
+
exprEnd = cursor.from;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Parse the expression inside parentheses as a subtree.
|
|
205
|
+
const subInput = input.slice(exprStart, exprEnd);
|
|
206
|
+
const subTree = this._parser.parse(subInput);
|
|
207
|
+
const subFilter = this.buildQuery(subTree, subInput);
|
|
208
|
+
if (subFilter) {
|
|
209
|
+
filters.push(subFilter);
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
// Simple parenthesized expression.
|
|
213
|
+
const subFilter = this._parseExpression(cursor, input);
|
|
214
|
+
if (subFilter) {
|
|
215
|
+
filters.push(subFilter);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Skip until we find the closing parenthesis.
|
|
219
|
+
while (cursor.nextSibling() && cursor.node.name !== ')') {}
|
|
220
|
+
}
|
|
221
|
+
} else if (nodeName !== ')') {
|
|
222
|
+
const subFilter = this._parseExpression(cursor, input);
|
|
223
|
+
if (subFilter) {
|
|
224
|
+
filters.push(subFilter);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
} while (cursor.nextSibling());
|
|
228
|
+
|
|
229
|
+
cursor.parent();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (filters.length === 0) {
|
|
233
|
+
return Filter.nothing();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (filters.length === 1) {
|
|
237
|
+
return filters[0];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return operator === 'or' ? Filter.or(...filters) : Filter.and(...filters);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Parse a Filter node.
|
|
245
|
+
*/
|
|
246
|
+
private _parseFilter(cursor: TreeCursor, input: string): Filter.Any | undefined {
|
|
247
|
+
if (!cursor.firstChild()) {
|
|
248
|
+
return Filter.nothing();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
let result: Filter.Any | undefined = undefined;
|
|
252
|
+
const filterType = cursor.node.name;
|
|
253
|
+
switch (filterType) {
|
|
254
|
+
case 'TagFilter':
|
|
255
|
+
if (this._tags) {
|
|
256
|
+
result = this._parseTagFilter(cursor, input);
|
|
257
|
+
}
|
|
258
|
+
break;
|
|
259
|
+
|
|
260
|
+
case 'TextFilter':
|
|
261
|
+
result = this._parseTextFilter(cursor, input);
|
|
262
|
+
break;
|
|
263
|
+
|
|
264
|
+
case 'TypeFilter':
|
|
265
|
+
result = this._parseTypeFilter(cursor, input);
|
|
266
|
+
break;
|
|
267
|
+
|
|
268
|
+
case 'ObjectLiteral':
|
|
269
|
+
result = this._parseObjectLiteral(cursor, input);
|
|
270
|
+
break;
|
|
271
|
+
|
|
272
|
+
case 'PropertyFilter':
|
|
273
|
+
result = this._parsePropertyFilter(cursor, input);
|
|
274
|
+
break;
|
|
275
|
+
|
|
276
|
+
default:
|
|
277
|
+
result = Filter.nothing();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
cursor.parent();
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Parse a TypeFilter node (type:typename).
|
|
286
|
+
*/
|
|
287
|
+
private _parseTypeFilter(cursor: TreeCursor, input: string): Filter.Any {
|
|
288
|
+
// Skip TypeKeyword.
|
|
289
|
+
cursor.firstChild();
|
|
290
|
+
cursor.nextSibling(); // Skip ':'
|
|
291
|
+
cursor.nextSibling(); // Move to Identifier
|
|
292
|
+
|
|
293
|
+
const typename = this._getNodeText(cursor, input);
|
|
294
|
+
cursor.parent(); // Go back to TypeFilter.
|
|
295
|
+
return Filter.typename(typename);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Parse a TextFilter node (quoted string).
|
|
300
|
+
*/
|
|
301
|
+
private _parseTextFilter(cursor: TreeCursor, input: string): Filter.Any {
|
|
302
|
+
cursor.firstChild(); // Move to String node.
|
|
303
|
+
const text = this._getNodeText(cursor, input);
|
|
304
|
+
cursor.parent(); // Go back to TextFilter.
|
|
305
|
+
// Remove quotes.
|
|
306
|
+
return Filter.text(text.slice(1, -1));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Parse an ObjectLiteral node.
|
|
311
|
+
*/
|
|
312
|
+
private _parseObjectLiteral(cursor: TreeCursor, input: string): Filter.Any {
|
|
313
|
+
const props: Record<string, any> = {};
|
|
314
|
+
|
|
315
|
+
if (cursor.firstChild()) {
|
|
316
|
+
do {
|
|
317
|
+
if (cursor.node.name === 'ObjectProperty') {
|
|
318
|
+
const { key, value } = this._parseObjectProperty(cursor, input);
|
|
319
|
+
if (key) {
|
|
320
|
+
// Convert simple values to Filter.eq for compatibility with Filter.props.
|
|
321
|
+
props[key] = Filter.eq(value);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
} while (cursor.nextSibling());
|
|
325
|
+
|
|
326
|
+
cursor.parent();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return Filter.props(props);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Parse an ObjectProperty node.
|
|
334
|
+
*/
|
|
335
|
+
private _parseObjectProperty(cursor: TreeCursor, input: string): { key: string | null; value: any } {
|
|
336
|
+
let key: string | null = null;
|
|
337
|
+
let value: any = null;
|
|
338
|
+
|
|
339
|
+
if (cursor.firstChild()) {
|
|
340
|
+
// First child should be the property name (Identifier).
|
|
341
|
+
if (cursor.node.name === 'Identifier') {
|
|
342
|
+
key = this._getNodeText(cursor, input);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Skip ':' and move to Value.
|
|
346
|
+
cursor.nextSibling();
|
|
347
|
+
cursor.nextSibling();
|
|
348
|
+
|
|
349
|
+
if (cursor.node.name === 'Value' && cursor.firstChild()) {
|
|
350
|
+
value = this._parseValue(cursor, input);
|
|
351
|
+
cursor.parent();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
cursor.parent();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return { key, value };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Parse a PropertyFilter node (property:value).
|
|
362
|
+
*/
|
|
363
|
+
private _parsePropertyFilter(cursor: TreeCursor, input: string): Filter.Any {
|
|
364
|
+
let path: string | null = null;
|
|
365
|
+
let value: any = null;
|
|
366
|
+
|
|
367
|
+
if (cursor.firstChild()) {
|
|
368
|
+
// First child is PropertyPath.
|
|
369
|
+
if (cursor.node.name === 'PropertyPath') {
|
|
370
|
+
path = this._parsePropertyPath(cursor, input);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Skip ':' and move to Value.
|
|
374
|
+
cursor.nextSibling();
|
|
375
|
+
cursor.nextSibling();
|
|
376
|
+
|
|
377
|
+
if (cursor.node.name === 'Value' && cursor.firstChild()) {
|
|
378
|
+
value = this._parseValue(cursor, input);
|
|
379
|
+
cursor.parent();
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
cursor.parent();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (!path) {
|
|
386
|
+
return Filter.nothing();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return Filter.props({ [path]: value });
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Parse a PropertyPath node (supports dot notation).
|
|
394
|
+
*/
|
|
395
|
+
private _parsePropertyPath(cursor: TreeCursor, input: string): string {
|
|
396
|
+
const parts: string[] = [];
|
|
397
|
+
|
|
398
|
+
if (cursor.firstChild()) {
|
|
399
|
+
do {
|
|
400
|
+
if (cursor.node.name === 'Identifier') {
|
|
401
|
+
parts.push(this._getNodeText(cursor, input));
|
|
402
|
+
}
|
|
403
|
+
} while (cursor.nextSibling());
|
|
404
|
+
|
|
405
|
+
cursor.parent();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return parts.join('.');
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Parse a TagFilter node (#tag).
|
|
413
|
+
*/
|
|
414
|
+
private _parseTagFilter(cursor: TreeCursor, input: string): Filter.Any | undefined {
|
|
415
|
+
invariant(this._tags);
|
|
416
|
+
const str = this._getNodeText(cursor, input).slice(1).toLowerCase();
|
|
417
|
+
const [key] = Object.entries(this._tags!).find(([, value]) => value.label.toLowerCase() === str) ?? [];
|
|
418
|
+
return key ? Filter.tag(key) : undefined;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Parse a Value node.
|
|
423
|
+
*/
|
|
424
|
+
private _parseValue(cursor: TreeCursor, input: string): any {
|
|
425
|
+
const valueType = cursor.node.name;
|
|
426
|
+
|
|
427
|
+
switch (valueType) {
|
|
428
|
+
case 'String': {
|
|
429
|
+
// Remove quotes.
|
|
430
|
+
const str = this._getNodeText(cursor, input);
|
|
431
|
+
return str.slice(1, -1);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
case 'Number':
|
|
435
|
+
return Number(this._getNodeText(cursor, input));
|
|
436
|
+
|
|
437
|
+
case 'Boolean':
|
|
438
|
+
return this._getNodeText(cursor, input) === 'true';
|
|
439
|
+
|
|
440
|
+
case 'Null':
|
|
441
|
+
return null;
|
|
442
|
+
|
|
443
|
+
case 'ObjectLiteral': {
|
|
444
|
+
// For nested objects, parse recursively.
|
|
445
|
+
const props: Record<string, any> = {};
|
|
446
|
+
if (cursor.firstChild()) {
|
|
447
|
+
do {
|
|
448
|
+
if (cursor.node.name === 'ObjectProperty') {
|
|
449
|
+
const { key, value } = this._parseObjectProperty(cursor, input);
|
|
450
|
+
if (key) {
|
|
451
|
+
props[key] = value;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
} while (cursor.nextSibling());
|
|
455
|
+
cursor.parent();
|
|
456
|
+
}
|
|
457
|
+
return props;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
case 'ArrayLiteral': {
|
|
461
|
+
// Parse array values
|
|
462
|
+
const array: any[] = [];
|
|
463
|
+
if (cursor.firstChild()) {
|
|
464
|
+
do {
|
|
465
|
+
if (cursor.node.name === 'Value' && cursor.firstChild()) {
|
|
466
|
+
array.push(this._parseValue(cursor, input));
|
|
467
|
+
cursor.parent();
|
|
468
|
+
}
|
|
469
|
+
} while (cursor.nextSibling());
|
|
470
|
+
cursor.parent();
|
|
471
|
+
}
|
|
472
|
+
return array;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
default:
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Get the text content of the current node.
|
|
482
|
+
*/
|
|
483
|
+
private _getNodeText(cursor: TreeCursor, input: string): string {
|
|
484
|
+
return input.slice(cursor.from, cursor.to);
|
|
485
|
+
}
|
|
486
|
+
}
|