@swaggerexpert/jsonpath 3.0.1 → 3.2.2
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 +33 -4
- package/SECURITY.md +3 -1
- package/package.json +22 -14
- package/types/index.d.ts +5 -3
- package/cjs/apg-lite.cjs +0 -1221
- package/cjs/errors/JSONPathError.cjs +0 -44
- package/cjs/errors/JSONPathParseError.cjs +0 -8
- package/cjs/grammar.cjs +0 -2839
- package/cjs/index.cjs +0 -19
- package/cjs/parse/callbacks/cst.cjs +0 -49
- package/cjs/parse/index.cjs +0 -40
- package/cjs/parse/translators/ASTTranslator/decoders.cjs +0 -16
- package/cjs/parse/translators/ASTTranslator/index.cjs +0 -16
- package/cjs/parse/translators/ASTTranslator/transformers.cjs +0 -410
- package/cjs/parse/translators/CSTOptimizedTranslator.cjs +0 -39
- package/cjs/parse/translators/CSTTranslator.cjs +0 -118
- package/cjs/parse/translators/XMLTranslator.cjs +0 -12
- package/cjs/test/index.cjs +0 -25
- package/es/errors/JSONPathError.mjs +0 -40
- package/es/errors/JSONPathParseError.mjs +0 -3
- package/es/grammar.mjs +0 -2835
- package/es/index.mjs +0 -7
- package/es/parse/callbacks/cst.mjs +0 -44
- package/es/parse/index.mjs +0 -35
- package/es/parse/translators/ASTTranslator/decoders.mjs +0 -9
- package/es/parse/translators/ASTTranslator/index.mjs +0 -9
- package/es/parse/translators/ASTTranslator/transformers.mjs +0 -404
- package/es/parse/translators/CSTOptimizedTranslator.mjs +0 -34
- package/es/parse/translators/CSTTranslator.mjs +0 -113
- package/es/parse/translators/XMLTranslator.mjs +0 -7
- package/es/test/index.mjs +0 -20
package/es/index.mjs
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { default as Grammar } from "./grammar.mjs";
|
|
2
|
-
export { default as parse } from "./parse/index.mjs";
|
|
3
|
-
export { default as CSTTranslator } from "./parse/translators/CSTTranslator.mjs";
|
|
4
|
-
export { default as CSTOptimizedTranslator } from "./parse/translators/CSTOptimizedTranslator.mjs";
|
|
5
|
-
export { default as ASTTranslator } from "./parse/translators/ASTTranslator/index.mjs";
|
|
6
|
-
export { default as XMLTranslator } from "./parse/translators/XMLTranslator.mjs";
|
|
7
|
-
export { default as test } from "./test/index.mjs";
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { utilities, identifiers } from 'apg-lite';
|
|
2
|
-
import JSONPathParseError from "../../errors/JSONPathParseError.mjs";
|
|
3
|
-
const cst = nodeType => {
|
|
4
|
-
return (state, chars, phraseIndex, phraseLength, data) => {
|
|
5
|
-
var _data$options, _data$options2;
|
|
6
|
-
if (!(typeof data === 'object' && data !== null && !Array.isArray(data))) {
|
|
7
|
-
throw new JSONPathParseError("parser's user data must be an object");
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// drop the empty nodes
|
|
11
|
-
if ((_data$options = data.options) !== null && _data$options !== void 0 && _data$options.optimize && phraseLength === 0 && (_data$options2 = data.options) !== null && _data$options2 !== void 0 && (_data$options2 = _data$options2.droppableTypes) !== null && _data$options2 !== void 0 && _data$options2.includes(nodeType)) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
if (state === identifiers.SEM_PRE) {
|
|
15
|
-
const node = {
|
|
16
|
-
type: nodeType,
|
|
17
|
-
text: utilities.charsToString(chars, phraseIndex, phraseLength),
|
|
18
|
-
start: phraseIndex,
|
|
19
|
-
length: phraseLength,
|
|
20
|
-
children: []
|
|
21
|
-
};
|
|
22
|
-
if (data.stack.length > 0) {
|
|
23
|
-
var _data$options3, _data$options4;
|
|
24
|
-
const parent = data.stack[data.stack.length - 1];
|
|
25
|
-
const prevSibling = parent.children[parent.children.length - 1];
|
|
26
|
-
const isTextNodeWithinTextNode = parent.type === 'text' && node.type === 'text';
|
|
27
|
-
const shouldCollapse = ((_data$options3 = data.options) === null || _data$options3 === void 0 ? void 0 : _data$options3.optimize) && ((_data$options4 = data.options) === null || _data$options4 === void 0 || (_data$options4 = _data$options4.collapsibleTypes) === null || _data$options4 === void 0 ? void 0 : _data$options4.includes(node.type)) && (prevSibling === null || prevSibling === void 0 ? void 0 : prevSibling.type) === node.type;
|
|
28
|
-
if (shouldCollapse) {
|
|
29
|
-
prevSibling.text += node.text;
|
|
30
|
-
prevSibling.length += node.length;
|
|
31
|
-
} else if (!isTextNodeWithinTextNode) {
|
|
32
|
-
parent.children.push(node);
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
data.root = node;
|
|
36
|
-
}
|
|
37
|
-
data.stack.push(node);
|
|
38
|
-
}
|
|
39
|
-
if (state === identifiers.SEM_POST) {
|
|
40
|
-
data.stack.pop();
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
export default cst;
|
package/es/parse/index.mjs
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Parser, Stats, Trace } from 'apg-lite';
|
|
2
|
-
import Grammar from "../grammar.mjs";
|
|
3
|
-
import ASTTranslator from "./translators/ASTTranslator/index.mjs";
|
|
4
|
-
import JSONPathParseError from "../errors/JSONPathParseError.mjs";
|
|
5
|
-
const grammar = new Grammar();
|
|
6
|
-
const parse = (jsonPath, {
|
|
7
|
-
normalized = false,
|
|
8
|
-
stats = false,
|
|
9
|
-
trace = false,
|
|
10
|
-
translator = new ASTTranslator()
|
|
11
|
-
} = {}) => {
|
|
12
|
-
if (typeof jsonPath !== 'string') {
|
|
13
|
-
throw new TypeError('JSONPath must be a string');
|
|
14
|
-
}
|
|
15
|
-
try {
|
|
16
|
-
const parser = new Parser();
|
|
17
|
-
if (translator) parser.ast = translator;
|
|
18
|
-
if (stats) parser.stats = new Stats();
|
|
19
|
-
if (trace) parser.trace = new Trace();
|
|
20
|
-
const startRule = normalized ? 'normalized-path' : 'jsonpath-query';
|
|
21
|
-
const result = parser.parse(grammar, startRule, jsonPath);
|
|
22
|
-
return {
|
|
23
|
-
result,
|
|
24
|
-
tree: result.success && translator ? parser.ast.getTree() : undefined,
|
|
25
|
-
stats: parser.stats,
|
|
26
|
-
trace: parser.trace
|
|
27
|
-
};
|
|
28
|
-
} catch (error) {
|
|
29
|
-
throw new JSONPathParseError('Unexpected error during JSONPath parsing', {
|
|
30
|
-
cause: error,
|
|
31
|
-
jsonPath
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
export default parse;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import CSTOptimizedTranslator from "../CSTOptimizedTranslator.mjs";
|
|
2
|
-
import { transformCSTtoAST, default as transformers } from "./transformers.mjs";
|
|
3
|
-
class ASTTranslator extends CSTOptimizedTranslator {
|
|
4
|
-
getTree() {
|
|
5
|
-
const cst = super.getTree();
|
|
6
|
-
return transformCSTtoAST(cst.root, transformers);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
export default ASTTranslator;
|
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
import JSONPathParseError from "../../../errors/JSONPathParseError.mjs";
|
|
2
|
-
import { decodeString, decodeInteger, decodeJSONValue } from "./decoders.mjs";
|
|
3
|
-
export const transformCSTtoAST = (node, transformerMap, ctx = {
|
|
4
|
-
parent: null,
|
|
5
|
-
path: []
|
|
6
|
-
}) => {
|
|
7
|
-
const transformer = transformerMap[node.type];
|
|
8
|
-
if (!transformer) {
|
|
9
|
-
throw new JSONPathParseError(`No transformer for CST node type: ${node.type}`);
|
|
10
|
-
}
|
|
11
|
-
const nextCtx = {
|
|
12
|
-
parent: node,
|
|
13
|
-
path: [...ctx.path, node]
|
|
14
|
-
};
|
|
15
|
-
return transformer(node, nextCtx);
|
|
16
|
-
};
|
|
17
|
-
const transformers = {
|
|
18
|
-
/**
|
|
19
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.1.1
|
|
20
|
-
*/
|
|
21
|
-
['jsonpath-query'](node, ctx) {
|
|
22
|
-
const segments = node.children.find(c => c.type === 'segments');
|
|
23
|
-
return {
|
|
24
|
-
type: 'JsonPathQuery',
|
|
25
|
-
segments: segments ? segments.children.filter(({
|
|
26
|
-
type
|
|
27
|
-
}) => type === 'segment').map(segNode => transformCSTtoAST(segNode, transformers, ctx)) : []
|
|
28
|
-
};
|
|
29
|
-
},
|
|
30
|
-
/**
|
|
31
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.5
|
|
32
|
-
*/
|
|
33
|
-
segment(node, ctx) {
|
|
34
|
-
const child = node.children.find(({
|
|
35
|
-
type
|
|
36
|
-
}) => ['child-segment', 'descendant-segment'].includes(type));
|
|
37
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
38
|
-
},
|
|
39
|
-
/**
|
|
40
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.3
|
|
41
|
-
*/
|
|
42
|
-
selector(node, ctx) {
|
|
43
|
-
const child = node.children.find(({
|
|
44
|
-
type
|
|
45
|
-
}) => ['name-selector', 'wildcard-selector', 'slice-selector', 'index-selector', 'filter-selector'].includes(type));
|
|
46
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
47
|
-
},
|
|
48
|
-
/**
|
|
49
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.3.1.1
|
|
50
|
-
*/
|
|
51
|
-
['name-selector'](node, ctx) {
|
|
52
|
-
const stringLiteralCSTNode = node.children.find(({
|
|
53
|
-
type
|
|
54
|
-
}) => type === 'string-literal');
|
|
55
|
-
const stringLiteralASTNode = transformCSTtoAST(stringLiteralCSTNode, transformers, ctx);
|
|
56
|
-
return {
|
|
57
|
-
type: 'NameSelector',
|
|
58
|
-
value: stringLiteralASTNode.value,
|
|
59
|
-
format: stringLiteralASTNode.format
|
|
60
|
-
};
|
|
61
|
-
},
|
|
62
|
-
['string-literal'](node) {
|
|
63
|
-
const isSingleQuoted = node.children.find(({
|
|
64
|
-
type,
|
|
65
|
-
text
|
|
66
|
-
}) => type === 'text' && text === "'");
|
|
67
|
-
const quoted = node.children.find(({
|
|
68
|
-
type
|
|
69
|
-
}) => ['double-quoted', 'single-quoted'].includes(type));
|
|
70
|
-
return {
|
|
71
|
-
type: 'StringLiteral',
|
|
72
|
-
value: quoted ? decodeString(quoted.text) : '',
|
|
73
|
-
format: isSingleQuoted ? 'single-quoted' : 'double-quoted'
|
|
74
|
-
};
|
|
75
|
-
},
|
|
76
|
-
/**
|
|
77
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.3.2.1
|
|
78
|
-
*/
|
|
79
|
-
['wildcard-selector']() {
|
|
80
|
-
return {
|
|
81
|
-
type: 'WildcardSelector'
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
/**
|
|
85
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.3.3.1
|
|
86
|
-
*/
|
|
87
|
-
['index-selector'](node) {
|
|
88
|
-
return {
|
|
89
|
-
type: 'IndexSelector',
|
|
90
|
-
value: decodeInteger(node.text)
|
|
91
|
-
};
|
|
92
|
-
},
|
|
93
|
-
/**
|
|
94
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.3.4.1
|
|
95
|
-
*/
|
|
96
|
-
['slice-selector'](node) {
|
|
97
|
-
const start = node.children.find(({
|
|
98
|
-
type
|
|
99
|
-
}) => type === 'start');
|
|
100
|
-
const end = node.children.find(({
|
|
101
|
-
type
|
|
102
|
-
}) => type === 'end');
|
|
103
|
-
const step = node.children.find(({
|
|
104
|
-
type
|
|
105
|
-
}) => type === 'step');
|
|
106
|
-
return {
|
|
107
|
-
type: 'SliceSelector',
|
|
108
|
-
start: start ? decodeInteger(start.text) : null,
|
|
109
|
-
end: end ? decodeInteger(end.text) : null,
|
|
110
|
-
step: step ? decodeInteger(step.text) : null
|
|
111
|
-
};
|
|
112
|
-
},
|
|
113
|
-
/**
|
|
114
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.3.5.1
|
|
115
|
-
*/
|
|
116
|
-
['filter-selector'](node, ctx) {
|
|
117
|
-
const child = node.children.find(({
|
|
118
|
-
type
|
|
119
|
-
}) => type === 'logical-expr');
|
|
120
|
-
return {
|
|
121
|
-
type: 'FilterSelector',
|
|
122
|
-
expression: transformCSTtoAST(child, transformers, ctx)
|
|
123
|
-
};
|
|
124
|
-
},
|
|
125
|
-
['logical-expr'](node, ctx) {
|
|
126
|
-
const child = node.children.find(({
|
|
127
|
-
type
|
|
128
|
-
}) => type === 'logical-or-expr');
|
|
129
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
130
|
-
},
|
|
131
|
-
['logical-or-expr'](node, ctx) {
|
|
132
|
-
const logicalAndExprs = node.children.filter(({
|
|
133
|
-
type
|
|
134
|
-
}) => type === 'logical-and-expr');
|
|
135
|
-
if (logicalAndExprs.length === 1) {
|
|
136
|
-
return transformCSTtoAST(logicalAndExprs[0], transformers, ctx);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// fold left for left-associativity
|
|
140
|
-
let left = transformCSTtoAST(logicalAndExprs[0], transformers, ctx);
|
|
141
|
-
for (let i = 1; i < logicalAndExprs.length; i += 1) {
|
|
142
|
-
const right = transformCSTtoAST(logicalAndExprs[i], transformers, ctx);
|
|
143
|
-
left = {
|
|
144
|
-
type: 'LogicalOrExpr',
|
|
145
|
-
left,
|
|
146
|
-
right
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
['logical-and-expr'](node, ctx) {
|
|
151
|
-
const basicExprs = node.children.filter(({
|
|
152
|
-
type
|
|
153
|
-
}) => type === 'basic-expr');
|
|
154
|
-
if (basicExprs.length === 1) {
|
|
155
|
-
return transformCSTtoAST(basicExprs[0], transformers, ctx);
|
|
156
|
-
}
|
|
157
|
-
let left = transformCSTtoAST(basicExprs[0], transformers, ctx);
|
|
158
|
-
for (let i = 1; i < basicExprs.length; i += 1) {
|
|
159
|
-
const right = transformCSTtoAST(basicExprs[i], transformers, ctx);
|
|
160
|
-
left = {
|
|
161
|
-
type: 'LogicalAndExpr',
|
|
162
|
-
left,
|
|
163
|
-
right
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
return left;
|
|
167
|
-
},
|
|
168
|
-
['basic-expr'](node, ctx) {
|
|
169
|
-
const child = node.children.find(({
|
|
170
|
-
type
|
|
171
|
-
}) => ['paren-expr', 'comparison-expr', 'test-expr'].includes(type));
|
|
172
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
173
|
-
},
|
|
174
|
-
['paren-expr'](node, ctx) {
|
|
175
|
-
const isNegated = node.children.some(child => child.type === 'logical-not-op');
|
|
176
|
-
const logicalExprCSTNode = node.children.find(child => child.type === 'logical-expr');
|
|
177
|
-
const logicalExpressionASTNode = transformCSTtoAST(logicalExprCSTNode, transformers, ctx);
|
|
178
|
-
if (isNegated) {
|
|
179
|
-
return {
|
|
180
|
-
type: 'LogicalNotExpr',
|
|
181
|
-
expression: logicalExpressionASTNode
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
return logicalExpressionASTNode;
|
|
185
|
-
},
|
|
186
|
-
['test-expr'](node, ctx) {
|
|
187
|
-
const isNegated = node.children.some(({
|
|
188
|
-
type
|
|
189
|
-
}) => type === 'logical-not-op');
|
|
190
|
-
const expression = node.children.find(({
|
|
191
|
-
type
|
|
192
|
-
}) => ['filter-query', 'function-expr'].includes(type));
|
|
193
|
-
const testExpr = {
|
|
194
|
-
type: 'TestExpr',
|
|
195
|
-
expression: transformCSTtoAST(expression, transformers, ctx)
|
|
196
|
-
};
|
|
197
|
-
return isNegated ? {
|
|
198
|
-
type: 'LogicalNotExpr',
|
|
199
|
-
expression: testExpr
|
|
200
|
-
} : testExpr;
|
|
201
|
-
},
|
|
202
|
-
['filter-query'](node, ctx) {
|
|
203
|
-
const child = node.children.find(({
|
|
204
|
-
type
|
|
205
|
-
}) => ['rel-query', 'jsonpath-query'].includes(type));
|
|
206
|
-
return {
|
|
207
|
-
type: 'FilterQuery',
|
|
208
|
-
query: transformCSTtoAST(child, transformers, ctx)
|
|
209
|
-
};
|
|
210
|
-
},
|
|
211
|
-
['rel-query'](node, ctx) {
|
|
212
|
-
const segments = node.children.find(c => c.type === 'segments');
|
|
213
|
-
return {
|
|
214
|
-
type: 'RelQuery',
|
|
215
|
-
segments: segments ? segments.children.filter(n => n.type === 'segment').map(segNode => transformCSTtoAST(segNode, transformers, ctx)) : []
|
|
216
|
-
};
|
|
217
|
-
},
|
|
218
|
-
['comparison-expr'](node, ctx) {
|
|
219
|
-
const children = node.children.filter(({
|
|
220
|
-
type
|
|
221
|
-
}) => ['comparable', 'comparison-op'].includes(type));
|
|
222
|
-
const [left, op, right] = children;
|
|
223
|
-
return {
|
|
224
|
-
type: 'ComparisonExpr',
|
|
225
|
-
left: transformCSTtoAST(left, transformers, ctx),
|
|
226
|
-
op: op.text,
|
|
227
|
-
right: transformCSTtoAST(right, transformers, ctx)
|
|
228
|
-
};
|
|
229
|
-
},
|
|
230
|
-
['literal'](node, ctx) {
|
|
231
|
-
const child = node.children.find(({
|
|
232
|
-
type
|
|
233
|
-
}) => ['number', 'string-literal', 'true', 'false', 'null'].includes(type));
|
|
234
|
-
if (child.type === 'string-literal') {
|
|
235
|
-
const stringLiteralASTNode = transformCSTtoAST(child, transformers, ctx);
|
|
236
|
-
return {
|
|
237
|
-
type: 'Literal',
|
|
238
|
-
value: stringLiteralASTNode.value
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
return {
|
|
242
|
-
type: 'Literal',
|
|
243
|
-
value: decodeJSONValue(child.text)
|
|
244
|
-
};
|
|
245
|
-
},
|
|
246
|
-
['comparable'](node, ctx) {
|
|
247
|
-
const child = node.children.find(({
|
|
248
|
-
type
|
|
249
|
-
}) => ['singular-query', 'function-expr', 'literal'].includes(type));
|
|
250
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
251
|
-
},
|
|
252
|
-
['singular-query'](node, ctx) {
|
|
253
|
-
const child = node.children.find(({
|
|
254
|
-
type
|
|
255
|
-
}) => ['rel-singular-query', 'abs-singular-query'].includes(type));
|
|
256
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
257
|
-
},
|
|
258
|
-
['rel-singular-query'](node, ctx) {
|
|
259
|
-
const segmentsNode = node.children.find(({
|
|
260
|
-
type
|
|
261
|
-
}) => type === 'singular-query-segments');
|
|
262
|
-
const segments = segmentsNode ? segmentsNode.children.filter(({
|
|
263
|
-
type
|
|
264
|
-
}) => ['name-segment', 'index-segment'].includes(type)).map(segNode => ({
|
|
265
|
-
type: 'SingularQuerySegment',
|
|
266
|
-
selector: transformCSTtoAST(segNode, transformers, ctx)
|
|
267
|
-
})) : [];
|
|
268
|
-
return {
|
|
269
|
-
type: 'RelSingularQuery',
|
|
270
|
-
segments
|
|
271
|
-
};
|
|
272
|
-
},
|
|
273
|
-
['abs-singular-query'](node, ctx) {
|
|
274
|
-
const segmentsNode = node.children.find(({
|
|
275
|
-
type
|
|
276
|
-
}) => type === 'singular-query-segments');
|
|
277
|
-
const segments = segmentsNode ? segmentsNode.children.filter(({
|
|
278
|
-
type
|
|
279
|
-
}) => ['name-segment', 'index-segment'].includes(type)).map(segNode => ({
|
|
280
|
-
type: 'SingularQuerySegment',
|
|
281
|
-
selector: transformCSTtoAST(segNode, transformers, ctx)
|
|
282
|
-
})) : [];
|
|
283
|
-
return {
|
|
284
|
-
type: 'AbsSingularQuery',
|
|
285
|
-
segments
|
|
286
|
-
};
|
|
287
|
-
},
|
|
288
|
-
['name-segment'](node, ctx) {
|
|
289
|
-
const child = node.children.find(({
|
|
290
|
-
type
|
|
291
|
-
}) => ['name-selector', 'member-name-shorthand'].includes(type));
|
|
292
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
293
|
-
},
|
|
294
|
-
['index-segment'](node, ctx) {
|
|
295
|
-
const child = node.children.find(({
|
|
296
|
-
type
|
|
297
|
-
}) => type === 'index-selector');
|
|
298
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
299
|
-
},
|
|
300
|
-
/**
|
|
301
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.4
|
|
302
|
-
*/
|
|
303
|
-
['function-expr'](node, ctx) {
|
|
304
|
-
const name = node.children.find(({
|
|
305
|
-
type
|
|
306
|
-
}) => type === 'function-name');
|
|
307
|
-
const args = node.children.filter(({
|
|
308
|
-
type
|
|
309
|
-
}) => type === 'function-argument');
|
|
310
|
-
return {
|
|
311
|
-
type: 'FunctionExpr',
|
|
312
|
-
name: name.text,
|
|
313
|
-
arguments: args.map(arg => transformCSTtoAST(arg, transformers, ctx))
|
|
314
|
-
};
|
|
315
|
-
},
|
|
316
|
-
['function-argument'](node, ctx) {
|
|
317
|
-
const child = node.children.find(({
|
|
318
|
-
type
|
|
319
|
-
}) => ['logical-expr', 'function-expr', 'filter-query', 'literal'].includes(type));
|
|
320
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
321
|
-
},
|
|
322
|
-
/**
|
|
323
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.5.1.1
|
|
324
|
-
*/
|
|
325
|
-
['child-segment'](node, ctx) {
|
|
326
|
-
const child = node.children.find(({
|
|
327
|
-
type
|
|
328
|
-
}) => ['bracketed-selection', 'wildcard-selector', 'member-name-shorthand'].includes(type));
|
|
329
|
-
return {
|
|
330
|
-
type: 'ChildSegment',
|
|
331
|
-
selector: transformCSTtoAST(child, transformers, ctx)
|
|
332
|
-
};
|
|
333
|
-
},
|
|
334
|
-
['bracketed-selection'](node, ctx) {
|
|
335
|
-
return {
|
|
336
|
-
type: 'BracketedSelection',
|
|
337
|
-
selectors: node.children.filter(({
|
|
338
|
-
type
|
|
339
|
-
}) => type === 'selector').map(selectorNode => transformCSTtoAST(selectorNode, transformers, ctx))
|
|
340
|
-
};
|
|
341
|
-
},
|
|
342
|
-
['member-name-shorthand'](node) {
|
|
343
|
-
return {
|
|
344
|
-
type: 'NameSelector',
|
|
345
|
-
value: node.text,
|
|
346
|
-
format: 'shorthand'
|
|
347
|
-
};
|
|
348
|
-
},
|
|
349
|
-
/**
|
|
350
|
-
* https://www.rfc-editor.org/rfc/rfc9535#section-2.5.2.1
|
|
351
|
-
*/
|
|
352
|
-
['descendant-segment'](node, ctx) {
|
|
353
|
-
const child = node.children.find(({
|
|
354
|
-
type
|
|
355
|
-
}) => ['bracketed-selection', 'wildcard-selector', 'member-name-shorthand'].includes(type));
|
|
356
|
-
return {
|
|
357
|
-
type: 'DescendantSegment',
|
|
358
|
-
selector: transformCSTtoAST(child, transformers, ctx)
|
|
359
|
-
};
|
|
360
|
-
},
|
|
361
|
-
/**
|
|
362
|
-
* https://www.rfc-editor.org/rfc/rfc9535#name-normalized-paths
|
|
363
|
-
*/
|
|
364
|
-
['normalized-path'](node, ctx) {
|
|
365
|
-
return {
|
|
366
|
-
type: 'JsonPathQuery',
|
|
367
|
-
segments: node.children.filter(({
|
|
368
|
-
type
|
|
369
|
-
}) => type === 'normal-index-segment').map(segNode => transformCSTtoAST(segNode, transformers, ctx))
|
|
370
|
-
};
|
|
371
|
-
},
|
|
372
|
-
['normal-index-segment'](node, ctx) {
|
|
373
|
-
const child = node.children.find(({
|
|
374
|
-
type
|
|
375
|
-
}) => type === 'normal-selector');
|
|
376
|
-
return {
|
|
377
|
-
type: 'ChildSegment',
|
|
378
|
-
selector: transformCSTtoAST(child, transformers, ctx)
|
|
379
|
-
};
|
|
380
|
-
},
|
|
381
|
-
['normal-selector'](node, ctx) {
|
|
382
|
-
const child = node.children.find(({
|
|
383
|
-
type
|
|
384
|
-
}) => ['normal-name-selector', 'normal-index-selector'].includes(type));
|
|
385
|
-
return transformCSTtoAST(child, transformers, ctx);
|
|
386
|
-
},
|
|
387
|
-
['normal-name-selector'](node) {
|
|
388
|
-
const child = node.children.find(({
|
|
389
|
-
type
|
|
390
|
-
}) => type === 'normal-single-quoted');
|
|
391
|
-
return {
|
|
392
|
-
type: 'NameSelector',
|
|
393
|
-
value: child ? decodeString(child.text) : '',
|
|
394
|
-
format: 'single-quoted'
|
|
395
|
-
};
|
|
396
|
-
},
|
|
397
|
-
['normal-index-selector'](node) {
|
|
398
|
-
return {
|
|
399
|
-
type: 'IndexSelector',
|
|
400
|
-
value: decodeInteger(node.text)
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
export default transformers;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import CSTTranslator from "./CSTTranslator.mjs";
|
|
2
|
-
class CSTOptimizedTranslator extends CSTTranslator {
|
|
3
|
-
collapsibleTypes = ['single-quoted', 'double-quoted', 'normal-single-quoted'];
|
|
4
|
-
droppableTypes = ['text', 'segments', 'singular-query-segments'];
|
|
5
|
-
constructor({
|
|
6
|
-
collapsibleTypes,
|
|
7
|
-
droppableTypes
|
|
8
|
-
} = {}) {
|
|
9
|
-
super();
|
|
10
|
-
if (Array.isArray(collapsibleTypes)) {
|
|
11
|
-
this.collapsibleTypes = collapsibleTypes;
|
|
12
|
-
}
|
|
13
|
-
if (Array.isArray(droppableTypes)) {
|
|
14
|
-
this.droppableTypes = droppableTypes;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
getTree() {
|
|
18
|
-
const options = {
|
|
19
|
-
optimize: true,
|
|
20
|
-
collapsibleTypes: this.collapsibleTypes,
|
|
21
|
-
droppableTypes: this.droppableTypes
|
|
22
|
-
};
|
|
23
|
-
const data = {
|
|
24
|
-
stack: [],
|
|
25
|
-
root: null,
|
|
26
|
-
options
|
|
27
|
-
};
|
|
28
|
-
this.translate(data);
|
|
29
|
-
delete data.stack;
|
|
30
|
-
delete data.options;
|
|
31
|
-
return data;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
export default CSTOptimizedTranslator;
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Ast as AST } from 'apg-lite';
|
|
2
|
-
import cstCallback from "../callbacks/cst.mjs";
|
|
3
|
-
class CSTTranslator extends AST {
|
|
4
|
-
constructor() {
|
|
5
|
-
super();
|
|
6
|
-
|
|
7
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.1.1
|
|
8
|
-
this.callbacks['jsonpath-query'] = cstCallback('jsonpath-query');
|
|
9
|
-
this.callbacks['segments'] = cstCallback('segments');
|
|
10
|
-
this.callbacks['B'] = cstCallback('text');
|
|
11
|
-
this.callbacks['S'] = cstCallback('text');
|
|
12
|
-
|
|
13
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.2.1
|
|
14
|
-
this.callbacks['root-identifier'] = cstCallback('root-identifier');
|
|
15
|
-
|
|
16
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.3
|
|
17
|
-
this.callbacks['selector'] = cstCallback('selector');
|
|
18
|
-
|
|
19
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.3.1.1
|
|
20
|
-
this.callbacks['name-selector'] = cstCallback('name-selector');
|
|
21
|
-
this.callbacks['string-literal'] = cstCallback('string-literal');
|
|
22
|
-
this.callbacks['double-quoted'] = cstCallback('double-quoted');
|
|
23
|
-
this.callbacks['single-quoted'] = cstCallback('single-quoted');
|
|
24
|
-
|
|
25
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.3.2.1
|
|
26
|
-
this.callbacks['wildcard-selector'] = cstCallback('wildcard-selector');
|
|
27
|
-
|
|
28
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.3.3.1
|
|
29
|
-
this.callbacks['index-selector'] = cstCallback('index-selector');
|
|
30
|
-
|
|
31
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.3.4.1
|
|
32
|
-
this.callbacks['slice-selector'] = cstCallback('slice-selector');
|
|
33
|
-
this.callbacks['start'] = cstCallback('start');
|
|
34
|
-
this.callbacks['end'] = cstCallback('end');
|
|
35
|
-
this.callbacks['step'] = cstCallback('step');
|
|
36
|
-
|
|
37
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.3.5.1
|
|
38
|
-
this.callbacks['filter-selector'] = cstCallback('filter-selector');
|
|
39
|
-
this.callbacks['logical-expr'] = cstCallback('logical-expr');
|
|
40
|
-
this.callbacks['logical-or-expr'] = cstCallback('logical-or-expr');
|
|
41
|
-
this.callbacks['logical-and-expr'] = cstCallback('logical-and-expr');
|
|
42
|
-
this.callbacks['basic-expr'] = cstCallback('basic-expr');
|
|
43
|
-
this.callbacks['paren-expr'] = cstCallback('paren-expr');
|
|
44
|
-
this.callbacks['logical-not-op'] = cstCallback('logical-not-op');
|
|
45
|
-
this.callbacks['test-expr'] = cstCallback('test-expr');
|
|
46
|
-
this.callbacks['filter-query'] = cstCallback('filter-query');
|
|
47
|
-
this.callbacks['rel-query'] = cstCallback('rel-query');
|
|
48
|
-
this.callbacks['current-node-identifier'] = cstCallback('current-node-identifier');
|
|
49
|
-
this.callbacks['comparison-expr'] = cstCallback('comparison-expr');
|
|
50
|
-
this.callbacks['literal'] = cstCallback('literal');
|
|
51
|
-
this.callbacks['comparable'] = cstCallback('comparable');
|
|
52
|
-
this.callbacks['comparison-op'] = cstCallback('comparison-op');
|
|
53
|
-
this.callbacks['singular-query'] = cstCallback('singular-query');
|
|
54
|
-
this.callbacks['rel-singular-query'] = cstCallback('rel-singular-query');
|
|
55
|
-
this.callbacks['abs-singular-query'] = cstCallback('abs-singular-query');
|
|
56
|
-
this.callbacks['singular-query-segments'] = cstCallback('singular-query-segments');
|
|
57
|
-
this.callbacks['name-segment'] = cstCallback('name-segment');
|
|
58
|
-
this.callbacks['index-segment'] = cstCallback('index-segment');
|
|
59
|
-
this.callbacks['number'] = cstCallback('number');
|
|
60
|
-
this.callbacks['true'] = cstCallback('true');
|
|
61
|
-
this.callbacks['false'] = cstCallback('false');
|
|
62
|
-
this.callbacks['null'] = cstCallback('null');
|
|
63
|
-
|
|
64
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.4
|
|
65
|
-
this.callbacks['function-name'] = cstCallback('function-name');
|
|
66
|
-
this.callbacks['function-expr'] = cstCallback('function-expr');
|
|
67
|
-
this.callbacks['function-argument'] = cstCallback('function-argument');
|
|
68
|
-
|
|
69
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.5
|
|
70
|
-
this.callbacks['segment'] = cstCallback('segment');
|
|
71
|
-
|
|
72
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.5.1.1
|
|
73
|
-
this.callbacks['child-segment'] = cstCallback('child-segment');
|
|
74
|
-
this.callbacks['bracketed-selection'] = cstCallback('bracketed-selection');
|
|
75
|
-
this.callbacks['member-name-shorthand'] = cstCallback('member-name-shorthand');
|
|
76
|
-
|
|
77
|
-
// https://www.rfc-editor.org/rfc/rfc9535#section-2.5.2.1
|
|
78
|
-
this.callbacks['descendant-segment'] = cstCallback('descendant-segment');
|
|
79
|
-
|
|
80
|
-
// https://www.rfc-editor.org/rfc/rfc9535#name-normalized-paths
|
|
81
|
-
this.callbacks['normalized-path'] = cstCallback('normalized-path');
|
|
82
|
-
this.callbacks['normal-index-segment'] = cstCallback('normal-index-segment');
|
|
83
|
-
this.callbacks['normal-selector'] = cstCallback('normal-selector');
|
|
84
|
-
this.callbacks['normal-name-selector'] = cstCallback('normal-name-selector');
|
|
85
|
-
this.callbacks['normal-index-selector'] = cstCallback('normal-index-selector');
|
|
86
|
-
this.callbacks['normal-single-quoted'] = cstCallback('normal-single-quoted');
|
|
87
|
-
|
|
88
|
-
// Surrogate named rules
|
|
89
|
-
this.callbacks['dot-prefix'] = cstCallback('text');
|
|
90
|
-
this.callbacks['double-dot-prefix'] = cstCallback('text');
|
|
91
|
-
this.callbacks['left-bracket'] = cstCallback('text');
|
|
92
|
-
this.callbacks['right-bracket'] = cstCallback('text');
|
|
93
|
-
this.callbacks['comma'] = cstCallback('text');
|
|
94
|
-
this.callbacks['colon'] = cstCallback('text');
|
|
95
|
-
this.callbacks['dquote'] = cstCallback('text');
|
|
96
|
-
this.callbacks['squote'] = cstCallback('text');
|
|
97
|
-
this.callbacks['questionmark'] = cstCallback('text');
|
|
98
|
-
this.callbacks['disjunction'] = cstCallback('text');
|
|
99
|
-
this.callbacks['conjunction'] = cstCallback('text');
|
|
100
|
-
this.callbacks['left-paren'] = cstCallback('text');
|
|
101
|
-
this.callbacks['right-paren'] = cstCallback('text');
|
|
102
|
-
}
|
|
103
|
-
getTree() {
|
|
104
|
-
const data = {
|
|
105
|
-
stack: [],
|
|
106
|
-
root: null
|
|
107
|
-
};
|
|
108
|
-
this.translate(data);
|
|
109
|
-
delete data.stack;
|
|
110
|
-
return data;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
export default CSTTranslator;
|