@openrewrite/rewrite 8.107.1 → 8.107.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/dist/javascript/add-import.d.ts +63 -3
- package/dist/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +238 -40
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/autodetect.d.ts.map +1 -1
- package/dist/javascript/autodetect.js +43 -16
- package/dist/javascript/autodetect.js.map +1 -1
- package/dist/javascript/binding.d.ts.map +1 -1
- package/dist/javascript/binding.js +10 -2
- package/dist/javascript/binding.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/xml/index.d.ts +1 -0
- package/dist/xml/index.d.ts.map +1 -1
- package/dist/xml/index.js +1 -0
- package/dist/xml/index.js.map +1 -1
- package/dist/xml/xpath-compiler.d.ts +64 -0
- package/dist/xml/xpath-compiler.d.ts.map +1 -0
- package/dist/xml/xpath-compiler.js +496 -0
- package/dist/xml/xpath-compiler.js.map +1 -0
- package/dist/xml/xpath.d.ts +8 -0
- package/dist/xml/xpath.d.ts.map +1 -0
- package/dist/xml/xpath.js +736 -0
- package/dist/xml/xpath.js.map +1 -0
- package/package.json +1 -1
- package/src/javascript/add-import.ts +261 -34
- package/src/javascript/autodetect.ts +48 -18
- package/src/javascript/binding.ts +13 -4
- package/src/xml/index.ts +1 -0
- package/src/xml/xpath-compiler.ts +652 -0
- package/src/xml/xpath.ts +776 -0
package/src/xml/index.ts
CHANGED
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
* <p>
|
|
4
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
* <p>
|
|
8
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
9
|
+
* <p>
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
type TokenType =
|
|
18
|
+
"/" | "//" | "::" | "[" | "]" | "(" | ")" | "@" | ".." | "." | "," |
|
|
19
|
+
"=" | "!=" | "<=" | ">=" | "<" | ">" | "*" |
|
|
20
|
+
"and" | "or" | "text" | "comment" | "node" | "processing-instruction" |
|
|
21
|
+
"NUMBER" | "STRING" | "QNAME" | "NCNAME" | "EOF";
|
|
22
|
+
|
|
23
|
+
interface Token {
|
|
24
|
+
readonly type: TokenType
|
|
25
|
+
readonly text: string
|
|
26
|
+
readonly offset: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const KEYWORDS = new Set<TokenType>(["and", "or", "text", "comment", "node", "processing-instruction"]);
|
|
30
|
+
const NODE_TYPES = new Set<TokenType>(["text", "comment", "node", "processing-instruction"]);
|
|
31
|
+
const STEP_STARTS = new Set<TokenType>([".", "..", "@", "*", "QNAME", "NCNAME", ...KEYWORDS]);
|
|
32
|
+
|
|
33
|
+
function isNameStartChar(c: number): boolean {
|
|
34
|
+
return (c >= 0x61 && c <= 0x7A) || (c >= 0x41 && c <= 0x5A) || c === 0x5F ||
|
|
35
|
+
(c >= 0xC0 && c <= 0xD6) || (c >= 0xD8 && c <= 0xF6) || (c >= 0xF8 && c <= 0x2FF) ||
|
|
36
|
+
(c >= 0x370 && c <= 0x37D) || (c >= 0x37F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D) ||
|
|
37
|
+
(c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF) || (c >= 0x3001 && c <= 0xD7FF) ||
|
|
38
|
+
(c >= 0xF900 && c <= 0xFDCF) || (c >= 0xFDF0 && c <= 0xFFFD);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isNameChar(c: number): boolean {
|
|
42
|
+
return isNameStartChar(c) || c === 0x2D || c === 0x2E || (c >= 0x30 && c <= 0x39) || c === 0xB7 ||
|
|
43
|
+
(c >= 0x300 && c <= 0x36F) || (c >= 0x203F && c <= 0x2040);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isDigit(c: number): boolean {
|
|
47
|
+
return c >= 0x30 && c <= 0x39;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function syntaxError(expression: string, offset: number, message: string): Error {
|
|
51
|
+
return new Error(`Syntax error at offset ${offset}: ${message}. Original input: '${expression}'`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function tokenize(expression: string): Token[] {
|
|
55
|
+
const tokens: Token[] = [];
|
|
56
|
+
let i = 0;
|
|
57
|
+
const push = (type: TokenType, length: number) => {
|
|
58
|
+
tokens.push({type, text: expression.substring(i, i + length), offset: i});
|
|
59
|
+
i += length;
|
|
60
|
+
};
|
|
61
|
+
while (i < expression.length) {
|
|
62
|
+
const c = expression[i];
|
|
63
|
+
const next = expression[i + 1];
|
|
64
|
+
switch (c) {
|
|
65
|
+
case " ":
|
|
66
|
+
case "\t":
|
|
67
|
+
case "\r":
|
|
68
|
+
case "\n":
|
|
69
|
+
i++;
|
|
70
|
+
continue;
|
|
71
|
+
case "/":
|
|
72
|
+
push(next === "/" ? "//" : "/", next === "/" ? 2 : 1);
|
|
73
|
+
continue;
|
|
74
|
+
case ".":
|
|
75
|
+
push(next === "." ? ".." : ".", next === "." ? 2 : 1);
|
|
76
|
+
continue;
|
|
77
|
+
case "<":
|
|
78
|
+
case ">":
|
|
79
|
+
if (next === "=") {
|
|
80
|
+
push(c === "<" ? "<=" : ">=", 2);
|
|
81
|
+
} else {
|
|
82
|
+
push(c, 1);
|
|
83
|
+
}
|
|
84
|
+
continue;
|
|
85
|
+
case ":":
|
|
86
|
+
case "!":
|
|
87
|
+
if (next !== (c === ":" ? ":" : "=")) {
|
|
88
|
+
throw syntaxError(expression, i, `token recognition error at: '${c}'`);
|
|
89
|
+
}
|
|
90
|
+
push(c === ":" ? "::" : "!=", 2);
|
|
91
|
+
continue;
|
|
92
|
+
case "[":
|
|
93
|
+
case "]":
|
|
94
|
+
case "(":
|
|
95
|
+
case ")":
|
|
96
|
+
case "@":
|
|
97
|
+
case ",":
|
|
98
|
+
case "=":
|
|
99
|
+
case "*":
|
|
100
|
+
push(c, 1);
|
|
101
|
+
continue;
|
|
102
|
+
case "'":
|
|
103
|
+
case "\"": {
|
|
104
|
+
const end = expression.indexOf(c, i + 1);
|
|
105
|
+
if (end === -1) {
|
|
106
|
+
throw syntaxError(expression, i, `token recognition error at: '${expression.substring(i)}'`);
|
|
107
|
+
}
|
|
108
|
+
push("STRING", end + 1 - i);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const code = expression.charCodeAt(i);
|
|
113
|
+
if (isDigit(code)) {
|
|
114
|
+
let end = i;
|
|
115
|
+
while (end < expression.length && isDigit(expression.charCodeAt(end))) {
|
|
116
|
+
end++;
|
|
117
|
+
}
|
|
118
|
+
if (expression[end] === "." && isDigit(expression.charCodeAt(end + 1))) {
|
|
119
|
+
end++;
|
|
120
|
+
while (end < expression.length && isDigit(expression.charCodeAt(end))) {
|
|
121
|
+
end++;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
push("NUMBER", end - i);
|
|
125
|
+
} else if (isNameStartChar(code)) {
|
|
126
|
+
let end = scanName(expression, i);
|
|
127
|
+
let type: TokenType = "NCNAME";
|
|
128
|
+
if (expression[end] === ":" && isNameStartChar(expression.charCodeAt(end + 1))) {
|
|
129
|
+
end = scanName(expression, end + 1);
|
|
130
|
+
type = "QNAME";
|
|
131
|
+
} else if (KEYWORDS.has(expression.substring(i, end) as TokenType)) {
|
|
132
|
+
type = expression.substring(i, end) as TokenType;
|
|
133
|
+
}
|
|
134
|
+
push(type, end - i);
|
|
135
|
+
} else {
|
|
136
|
+
throw syntaxError(expression, i, `token recognition error at: '${c}'`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
tokens.push({type: "EOF", text: "", offset: expression.length});
|
|
140
|
+
return tokens;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function scanName(expression: string, start: number): number {
|
|
144
|
+
let end = start + 1;
|
|
145
|
+
while (end < expression.length && isNameChar(expression.charCodeAt(end))) {
|
|
146
|
+
end++;
|
|
147
|
+
}
|
|
148
|
+
return end;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface Span {
|
|
152
|
+
readonly start: number
|
|
153
|
+
readonly end: number
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface OrExpr extends Span {
|
|
157
|
+
readonly operands: AndExpr[]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
interface AndExpr extends Span {
|
|
161
|
+
readonly operands: EqualityExpr[]
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
interface EqualityExpr extends Span {
|
|
165
|
+
readonly operands: RelationalExpr[]
|
|
166
|
+
readonly operators: TokenType[]
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface RelationalExpr extends Span {
|
|
170
|
+
readonly operands: PathExpr[]
|
|
171
|
+
readonly operators: TokenType[]
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
type PathExpr = FilterPathExpr | LocationPathExpr;
|
|
175
|
+
|
|
176
|
+
interface FilterPathExpr extends Span {
|
|
177
|
+
readonly kind: "function" | "bracketed" | "literal" | "number"
|
|
178
|
+
readonly functionCall?: FunctionCall
|
|
179
|
+
readonly expr?: OrExpr
|
|
180
|
+
readonly token?: Token
|
|
181
|
+
readonly predicates: OrExpr[]
|
|
182
|
+
readonly trailingSeparator?: "/" | "//"
|
|
183
|
+
readonly trailingPath?: RelativeLocationPath
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
interface LocationPathExpr extends Span {
|
|
187
|
+
readonly kind: "location"
|
|
188
|
+
readonly absolute?: "/" | "//"
|
|
189
|
+
readonly relativePath?: RelativeLocationPath
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
interface RelativeLocationPath extends Span {
|
|
193
|
+
readonly steps: Step[]
|
|
194
|
+
readonly separators: ("/" | "//")[]
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
type Step =
|
|
198
|
+
{ readonly kind: "abbreviated", readonly dotdot: boolean, readonly predicates: OrExpr[] } |
|
|
199
|
+
{ readonly kind: "attribute", readonly name: string, readonly predicates: OrExpr[] } |
|
|
200
|
+
{ readonly kind: "node", readonly axis?: string, readonly nodeTest: NodeTest, readonly predicates: OrExpr[] };
|
|
201
|
+
|
|
202
|
+
type NodeTest = { readonly name: string, readonly nodeType?: never } | { readonly nodeType: string, readonly name?: never };
|
|
203
|
+
|
|
204
|
+
interface FunctionCall {
|
|
205
|
+
readonly name: string
|
|
206
|
+
readonly args: OrExpr[]
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
class Parser {
|
|
210
|
+
private pos = 0;
|
|
211
|
+
|
|
212
|
+
constructor(private readonly expression: string, private readonly tokens: Token[]) {
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
parse(): OrExpr {
|
|
216
|
+
const expr = this.orExpr();
|
|
217
|
+
if (this.peek().type !== "EOF") {
|
|
218
|
+
this.fail(`extraneous input '${this.peek().text}'`);
|
|
219
|
+
}
|
|
220
|
+
return expr;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
text(span: Span): string {
|
|
224
|
+
return this.tokens.slice(span.start, span.end).map(t => t.text).join("");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private peek(ahead: number = 0): Token {
|
|
228
|
+
return this.tokens[Math.min(this.pos + ahead, this.tokens.length - 1)];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private next(): Token {
|
|
232
|
+
return this.tokens[this.pos++];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private accept(...types: TokenType[]): Token | undefined {
|
|
236
|
+
return types.includes(this.peek().type) ? this.next() : undefined;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
private expect(...types: TokenType[]): Token {
|
|
240
|
+
const token = this.accept(...types);
|
|
241
|
+
if (!token) {
|
|
242
|
+
this.fail(`mismatched input '${this.peek().text || "<EOF>"}' expecting ${types.map(t => `'${t}'`).join(" or ")}`);
|
|
243
|
+
}
|
|
244
|
+
return token!;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private fail(message: string): never {
|
|
248
|
+
throw syntaxError(this.expression, this.peek().offset, message);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private orExpr(): OrExpr {
|
|
252
|
+
const start = this.pos;
|
|
253
|
+
const operands = [this.andExpr()];
|
|
254
|
+
while (this.accept("or")) {
|
|
255
|
+
operands.push(this.andExpr());
|
|
256
|
+
}
|
|
257
|
+
return {start, end: this.pos, operands};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private andExpr(): AndExpr {
|
|
261
|
+
const start = this.pos;
|
|
262
|
+
const operands = [this.equalityExpr()];
|
|
263
|
+
while (this.accept("and")) {
|
|
264
|
+
operands.push(this.equalityExpr());
|
|
265
|
+
}
|
|
266
|
+
return {start, end: this.pos, operands};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private equalityExpr(): EqualityExpr {
|
|
270
|
+
const start = this.pos;
|
|
271
|
+
const operands = [this.relationalExpr()];
|
|
272
|
+
const operators: TokenType[] = [];
|
|
273
|
+
for (let op = this.accept("=", "!="); op; op = this.accept("=", "!=")) {
|
|
274
|
+
operators.push(op.type);
|
|
275
|
+
operands.push(this.relationalExpr());
|
|
276
|
+
}
|
|
277
|
+
return {start, end: this.pos, operands, operators};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private relationalExpr(): RelationalExpr {
|
|
281
|
+
const start = this.pos;
|
|
282
|
+
const operands = [this.pathExpr()];
|
|
283
|
+
const operators: TokenType[] = [];
|
|
284
|
+
for (let op = this.accept("<", ">", "<=", ">="); op; op = this.accept("<", ">", "<=", ">=")) {
|
|
285
|
+
operators.push(op.type);
|
|
286
|
+
operands.push(this.pathExpr());
|
|
287
|
+
}
|
|
288
|
+
return {start, end: this.pos, operands, operators};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private pathExpr(): PathExpr {
|
|
292
|
+
const start = this.pos;
|
|
293
|
+
const token = this.peek();
|
|
294
|
+
let filter: Omit<FilterPathExpr, "start" | "end" | "predicates">;
|
|
295
|
+
if ((token.type === "NCNAME" || NODE_TYPES.has(token.type)) && this.peek(1).type === "(") {
|
|
296
|
+
filter = {kind: "function", functionCall: this.functionCall()};
|
|
297
|
+
} else if (token.type === "(") {
|
|
298
|
+
this.next();
|
|
299
|
+
const expr = this.orExpr();
|
|
300
|
+
this.expect(")");
|
|
301
|
+
filter = {kind: "bracketed", expr};
|
|
302
|
+
} else if (token.type === "STRING" || token.type === "NUMBER") {
|
|
303
|
+
this.next();
|
|
304
|
+
filter = {kind: token.type === "STRING" ? "literal" : "number", token};
|
|
305
|
+
} else {
|
|
306
|
+
return this.locationPath();
|
|
307
|
+
}
|
|
308
|
+
const predicates = this.predicates();
|
|
309
|
+
const separator = this.accept("/", "//");
|
|
310
|
+
const trailingPath = separator && this.relativeLocationPath();
|
|
311
|
+
return {
|
|
312
|
+
...filter,
|
|
313
|
+
start,
|
|
314
|
+
end: this.pos,
|
|
315
|
+
predicates,
|
|
316
|
+
trailingSeparator: separator?.type as "/" | "//" | undefined,
|
|
317
|
+
trailingPath
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private functionCall(): FunctionCall {
|
|
322
|
+
const name = this.next().text;
|
|
323
|
+
this.expect("(");
|
|
324
|
+
const args: OrExpr[] = [];
|
|
325
|
+
if (!this.accept(")")) {
|
|
326
|
+
do {
|
|
327
|
+
args.push(this.orExpr());
|
|
328
|
+
} while (this.accept(","));
|
|
329
|
+
this.expect(")");
|
|
330
|
+
}
|
|
331
|
+
return {name, args};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private locationPath(): LocationPathExpr {
|
|
335
|
+
const start = this.pos;
|
|
336
|
+
const absolute = this.accept("/", "//");
|
|
337
|
+
let relativePath: RelativeLocationPath | undefined;
|
|
338
|
+
if (absolute?.type !== "/" || STEP_STARTS.has(this.peek().type)) {
|
|
339
|
+
relativePath = this.relativeLocationPath();
|
|
340
|
+
}
|
|
341
|
+
return {kind: "location", start, end: this.pos, absolute: absolute?.type as "/" | "//" | undefined, relativePath};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private relativeLocationPath(): RelativeLocationPath {
|
|
345
|
+
const start = this.pos;
|
|
346
|
+
const steps = [this.step()];
|
|
347
|
+
const separators: ("/" | "//")[] = [];
|
|
348
|
+
for (let separator = this.accept("/", "//"); separator; separator = this.accept("/", "//")) {
|
|
349
|
+
separators.push(separator.type as "/" | "//");
|
|
350
|
+
steps.push(this.step());
|
|
351
|
+
}
|
|
352
|
+
return {start, end: this.pos, steps, separators};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private step(): Step {
|
|
356
|
+
const abbreviated = this.accept(".", "..");
|
|
357
|
+
if (abbreviated) {
|
|
358
|
+
return {kind: "abbreviated", dotdot: abbreviated.type === "..", predicates: []};
|
|
359
|
+
}
|
|
360
|
+
if (this.accept("@")) {
|
|
361
|
+
const name = this.expect("QNAME", "NCNAME", "*", ...KEYWORDS).text;
|
|
362
|
+
return {kind: "attribute", name, predicates: this.predicates()};
|
|
363
|
+
}
|
|
364
|
+
let axis: string | undefined;
|
|
365
|
+
if (this.peek().type === "NCNAME" && this.peek(1).type === "::") {
|
|
366
|
+
axis = this.next().text;
|
|
367
|
+
this.next();
|
|
368
|
+
}
|
|
369
|
+
const nodeTest = this.nodeTest();
|
|
370
|
+
return {kind: "node", axis, nodeTest, predicates: this.predicates()};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private nodeTest(): NodeTest {
|
|
374
|
+
const name = this.accept("*", "QNAME", "NCNAME", ...(this.peek(1).type === "(" ? [] : [...KEYWORDS]));
|
|
375
|
+
if (name) {
|
|
376
|
+
return {name: name.text};
|
|
377
|
+
}
|
|
378
|
+
const nodeType = this.expect(...NODE_TYPES);
|
|
379
|
+
this.expect("(");
|
|
380
|
+
this.expect(")");
|
|
381
|
+
return {nodeType: nodeType.text};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private predicates(): OrExpr[] {
|
|
385
|
+
const predicates: OrExpr[] = [];
|
|
386
|
+
while (this.accept("[")) {
|
|
387
|
+
predicates.push(this.orExpr());
|
|
388
|
+
this.expect("]");
|
|
389
|
+
}
|
|
390
|
+
return predicates;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export type ComparisonOp = "=" | "!=" | "<" | "<=" | ">" | ">=";
|
|
395
|
+
|
|
396
|
+
export type FunctionType =
|
|
397
|
+
"position" | "last" | "local-name" | "namespace-uri" | "contains" | "starts-with" | "ends-with" |
|
|
398
|
+
"string-length" | "substring-before" | "substring-after" | "count" | "text" | "not" | "comment" | "node" |
|
|
399
|
+
"processing-instruction";
|
|
400
|
+
|
|
401
|
+
const FUNCTIONS = new Set<string>([
|
|
402
|
+
"position", "last", "local-name", "namespace-uri", "contains", "starts-with", "ends-with", "string-length",
|
|
403
|
+
"substring-before", "substring-after", "count", "text", "not", "comment", "node", "processing-instruction"
|
|
404
|
+
]);
|
|
405
|
+
|
|
406
|
+
export type CompiledExpr =
|
|
407
|
+
{ readonly type: "numeric", readonly value: number } |
|
|
408
|
+
{ readonly type: "string", readonly value: string } |
|
|
409
|
+
{ readonly type: "comparison", readonly left: CompiledExpr, readonly op: ComparisonOp, readonly right: CompiledExpr } |
|
|
410
|
+
{ readonly type: "and" | "or", readonly left: CompiledExpr, readonly right: CompiledExpr } |
|
|
411
|
+
{ readonly type: "function", readonly functionType: FunctionType, readonly args: CompiledExpr[] } |
|
|
412
|
+
{ readonly type: "attribute" | "child", readonly name: string } |
|
|
413
|
+
{ readonly type: "path", readonly steps: CompiledExpr[], readonly pathSteps?: CompiledStep[] } |
|
|
414
|
+
{ readonly type: "absolutePath", readonly path: string } |
|
|
415
|
+
{ readonly type: "parent" | "self" };
|
|
416
|
+
|
|
417
|
+
export type StepType = "dot" | "dotdot" | "axis" | "attribute" | "nodeType" | "nodeTest";
|
|
418
|
+
|
|
419
|
+
export type AxisType = "parent" | "self" | "child" | "other";
|
|
420
|
+
|
|
421
|
+
export type NodeTypeTest = "text" | "comment" | "node" | "processing-instruction" | "unknown";
|
|
422
|
+
|
|
423
|
+
export interface CompiledStep {
|
|
424
|
+
readonly type: StepType
|
|
425
|
+
readonly isDescendant: boolean
|
|
426
|
+
readonly name?: string
|
|
427
|
+
readonly axis: AxisType
|
|
428
|
+
readonly nodeType: NodeTypeTest
|
|
429
|
+
readonly predicates: CompiledExpr[]
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
export interface CompiledFilterExpr {
|
|
433
|
+
readonly path: string
|
|
434
|
+
readonly predicates: CompiledExpr[]
|
|
435
|
+
readonly trailingPath?: CompiledStep[]
|
|
436
|
+
readonly trailingIsDescendant: boolean
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export type CompiledXPath =
|
|
440
|
+
{ readonly type: "path", readonly steps: CompiledStep[], readonly absolute: boolean } |
|
|
441
|
+
{ readonly type: "boolean", readonly expr: CompiledExpr } |
|
|
442
|
+
{ readonly type: "filter", readonly filter: CompiledFilterExpr };
|
|
443
|
+
|
|
444
|
+
const CACHE_SIZE = 256;
|
|
445
|
+
const cache = new Map<string, CompiledXPath>();
|
|
446
|
+
|
|
447
|
+
export function compileXPath(expression: string): CompiledXPath {
|
|
448
|
+
let compiled = cache.get(expression);
|
|
449
|
+
if (!compiled) {
|
|
450
|
+
compiled = new Compiler(expression).compile();
|
|
451
|
+
if (cache.size >= CACHE_SIZE) {
|
|
452
|
+
cache.delete(cache.keys().next().value!);
|
|
453
|
+
}
|
|
454
|
+
cache.set(expression, compiled);
|
|
455
|
+
}
|
|
456
|
+
return compiled;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function isBacktrack(step: CompiledStep): boolean {
|
|
460
|
+
return step.type === "dotdot" || (step.type === "axis" && step.axis === "parent");
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
class Compiler {
|
|
464
|
+
private readonly parser: Parser;
|
|
465
|
+
|
|
466
|
+
constructor(expression: string) {
|
|
467
|
+
this.parser = new Parser(expression, tokenize(expression));
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
compile(): CompiledXPath {
|
|
471
|
+
const orExpr = this.parser.parse();
|
|
472
|
+
const single = orExpr.operands.length === 1 &&
|
|
473
|
+
orExpr.operands[0].operands.length === 1 &&
|
|
474
|
+
orExpr.operands[0].operands[0].operands.length === 1 &&
|
|
475
|
+
orExpr.operands[0].operands[0].operands[0].operands.length === 1 ?
|
|
476
|
+
orExpr.operands[0].operands[0].operands[0].operands[0] : undefined;
|
|
477
|
+
if (single?.kind === "location") {
|
|
478
|
+
return this.locationPath(single);
|
|
479
|
+
} else if (single) {
|
|
480
|
+
return this.filterExprAsXPath(single);
|
|
481
|
+
}
|
|
482
|
+
return {type: "boolean", expr: this.orExpr(orExpr)};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
private locationPath(locationPath: LocationPathExpr): CompiledXPath {
|
|
486
|
+
const relativePath = locationPath.relativePath;
|
|
487
|
+
const steps = relativePath ?
|
|
488
|
+
relativePath.steps.map((s, i) => this.step(s, i > 0 && relativePath.separators[i - 1] === "//")) :
|
|
489
|
+
[];
|
|
490
|
+
return {type: "path", steps: normalizeParentSteps(steps), absolute: locationPath.absolute === "/"};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
private filterExprAsXPath(pathExpr: FilterPathExpr): CompiledXPath {
|
|
494
|
+
if (pathExpr.kind === "bracketed") {
|
|
495
|
+
return {
|
|
496
|
+
type: "filter",
|
|
497
|
+
filter: {
|
|
498
|
+
path: this.parser.text(pathExpr.expr!),
|
|
499
|
+
predicates: pathExpr.predicates.map(p => this.orExpr(p)),
|
|
500
|
+
trailingPath: pathExpr.trailingPath && this.relativeSteps(pathExpr.trailingPath),
|
|
501
|
+
trailingIsDescendant: pathExpr.trailingPath !== undefined && pathExpr.trailingSeparator === "//"
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
return {type: "boolean", expr: this.pathExpr(pathExpr)};
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private step(step: Step, isDescendant: boolean): CompiledStep {
|
|
509
|
+
const predicates = step.predicates.map(p => this.orExpr(p));
|
|
510
|
+
const base = {isDescendant, axis: "other" as AxisType, nodeType: "unknown" as NodeTypeTest, predicates};
|
|
511
|
+
switch (step.kind) {
|
|
512
|
+
case "abbreviated":
|
|
513
|
+
return {...base, type: step.dotdot ? "dotdot" : "dot"};
|
|
514
|
+
case "attribute":
|
|
515
|
+
return {...base, type: "attribute", name: step.name};
|
|
516
|
+
}
|
|
517
|
+
if (step.axis !== undefined && step.axis !== "child") {
|
|
518
|
+
const axis: AxisType = step.axis === "parent" || step.axis === "self" ?
|
|
519
|
+
step.axis : "other";
|
|
520
|
+
return {...base, type: "axis", name: nodeTestName(step.nodeTest), axis,
|
|
521
|
+
nodeType: step.nodeTest.nodeType as NodeTypeTest ?? "unknown"};
|
|
522
|
+
}
|
|
523
|
+
if (step.nodeTest.nodeType !== undefined) {
|
|
524
|
+
return {...base, type: "nodeType", nodeType: step.nodeTest.nodeType as NodeTypeTest};
|
|
525
|
+
}
|
|
526
|
+
return {...base, type: "nodeTest", name: nodeTestName(step.nodeTest)};
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
private orExpr(orExpr: OrExpr): CompiledExpr {
|
|
530
|
+
return orExpr.operands.map(a => this.andExpr(a))
|
|
531
|
+
.reduce((left, right) => ({type: "or", left, right}));
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
private andExpr(andExpr: AndExpr): CompiledExpr {
|
|
535
|
+
return andExpr.operands.map(e => this.equalityExpr(e))
|
|
536
|
+
.reduce((left, right) => ({type: "and", left, right}));
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
private equalityExpr(equalityExpr: EqualityExpr): CompiledExpr {
|
|
540
|
+
let result = this.relationalExpr(equalityExpr.operands[0]);
|
|
541
|
+
for (let i = 1; i < equalityExpr.operands.length; i++) {
|
|
542
|
+
const op = equalityExpr.operators[i - 1] as ComparisonOp;
|
|
543
|
+
result = {type: "comparison", left: result, op, right: this.relationalExpr(equalityExpr.operands[i])};
|
|
544
|
+
}
|
|
545
|
+
return result;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
private relationalExpr(relationalExpr: RelationalExpr): CompiledExpr {
|
|
549
|
+
let result = this.pathExpr(relationalExpr.operands[0]);
|
|
550
|
+
for (let i = 1; i < relationalExpr.operands.length; i++) {
|
|
551
|
+
const op = relationalExpr.operators[i - 1] as ComparisonOp;
|
|
552
|
+
result = {type: "comparison", left: result, op, right: this.pathExpr(relationalExpr.operands[i])};
|
|
553
|
+
}
|
|
554
|
+
return result;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
private pathExpr(pathExpr: PathExpr): CompiledExpr {
|
|
558
|
+
switch (pathExpr.kind) {
|
|
559
|
+
case "location":
|
|
560
|
+
if (pathExpr.absolute) {
|
|
561
|
+
return {type: "absolutePath", path: this.parser.text(pathExpr)};
|
|
562
|
+
}
|
|
563
|
+
return this.relativePathAsExpr(pathExpr.relativePath!);
|
|
564
|
+
case "function":
|
|
565
|
+
return this.functionCall(pathExpr.functionCall!);
|
|
566
|
+
case "bracketed":
|
|
567
|
+
return this.orExpr(pathExpr.expr!);
|
|
568
|
+
case "literal":
|
|
569
|
+
return {type: "string", value: stripQuotes(pathExpr.token!.text)};
|
|
570
|
+
case "number": {
|
|
571
|
+
const text = pathExpr.token!.text;
|
|
572
|
+
const value = Number(text);
|
|
573
|
+
return {type: "numeric", value};
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
private relativeSteps(relativePath: RelativeLocationPath): CompiledStep[] {
|
|
579
|
+
return relativePath.steps.map((s, i) => this.step(s, i > 0 && relativePath.separators[i - 1] === "//"));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
private relativePathAsExpr(relativePath: RelativeLocationPath): CompiledExpr {
|
|
583
|
+
const step = relativePath.steps[0];
|
|
584
|
+
if (relativePath.steps.length === 1 && step.predicates.length === 0 &&
|
|
585
|
+
(step.kind !== "node" || step.axis === undefined)) {
|
|
586
|
+
if (step.kind === "abbreviated") {
|
|
587
|
+
return {type: step.dotdot ? "parent" : "self"};
|
|
588
|
+
}
|
|
589
|
+
if (step.kind === "attribute") {
|
|
590
|
+
return {type: "attribute", name: step.name};
|
|
591
|
+
}
|
|
592
|
+
if (step.nodeTest.name !== undefined) {
|
|
593
|
+
return {type: "child", name: step.nodeTest.name};
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return {type: "path", steps: [], pathSteps: this.relativeSteps(relativePath)};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
private functionCall(functionCall: FunctionCall): CompiledExpr {
|
|
600
|
+
if (!FUNCTIONS.has(functionCall.name)) {
|
|
601
|
+
throw new Error(`Unsupported XPath function: ${functionCall.name}()`);
|
|
602
|
+
}
|
|
603
|
+
return {
|
|
604
|
+
type: "function",
|
|
605
|
+
functionType: functionCall.name as FunctionType,
|
|
606
|
+
args: functionCall.args.map(a => this.orExpr(a))
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function nodeTestName(nodeTest: NodeTest): string {
|
|
612
|
+
return nodeTest.nodeType ?? nodeTest.name!;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function stripQuotes(s: string): string {
|
|
616
|
+
if (s.length >= 2 && (s[0] === "'" || s[0] === "\"") && s[s.length - 1] === s[0]) {
|
|
617
|
+
return s.substring(1, s.length - 1);
|
|
618
|
+
}
|
|
619
|
+
return s;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function normalizeParentSteps(steps: CompiledStep[]): CompiledStep[] {
|
|
623
|
+
const isLeading = (index: number) => steps.slice(0, index).every(isBacktrack);
|
|
624
|
+
if (!steps.some((s, i) => i > 0 && isBacktrack(s) && !isLeading(i))) {
|
|
625
|
+
return steps;
|
|
626
|
+
}
|
|
627
|
+
const result: CompiledStep[] = [];
|
|
628
|
+
let i = 0;
|
|
629
|
+
while (i < steps.length) {
|
|
630
|
+
if (!isBacktrack(steps[i]) || isLeading(i)) {
|
|
631
|
+
result.push(steps[i++]);
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
const parentStart = i;
|
|
635
|
+
while (i < steps.length && isBacktrack(steps[i])) {
|
|
636
|
+
i++;
|
|
637
|
+
}
|
|
638
|
+
const parentCount = i - parentStart;
|
|
639
|
+
const predicateStart = parentStart - parentCount;
|
|
640
|
+
if (predicateStart < 0 || predicateStart >= result.length) {
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
const predicateSteps = result.splice(predicateStart, Math.min(parentCount, result.length - predicateStart));
|
|
644
|
+
const children: CompiledExpr[] = predicateSteps.map(s => ({type: "child", name: s.name!}));
|
|
645
|
+
const predicate: CompiledExpr = children.length === 1 ? children[0] : {type: "path", steps: children};
|
|
646
|
+
if (predicateStart > 0 && predicateStart <= result.length) {
|
|
647
|
+
const anchor = result[predicateStart - 1];
|
|
648
|
+
result[predicateStart - 1] = {...anchor, predicates: [...anchor.predicates, predicate]};
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return result;
|
|
652
|
+
}
|