Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | 4x 4x 4x 186x 186x 186x 186x 181x 5x 186x 186x 186x 1x 185x 185x 185x 302x 302x 301x 297x 181x 301x 301x 301x 14x 14x 301x 301x 118x 118x 118x 5x 5x 113x 113x 183x 170x 170x 170x 196x 196x 193x 193x 193x 26x 26x 167x 167x 167x 1x 166x 13x 13x 1x 1x 12x 12x 297x 14x 283x 210x 210x 14x 14x 196x 95x 95x 95x 95x 22x 22x 22x 95x 94x 22x 22x 22x 94x 101x 27x 26x 74x 70x 69x 73x 19x 19x 19x 15x 15x 19x 7x 7x 7x 7x 19x 54x 54x 98x 98x 97x 97x 2x 2x 2x 2x 2x 97x 100x 99x 99x 5x 5x 5x 5x 5x 99x 105x 106x 106x 1x 1x 1x 1x 105x 3x 3x 3x 3x 3x 3x 3x 102x 102x 101x 101x 64x 64x 64x 64x 37x 14x 23x 23x 23x 126x 126x 681x 126x 60x 60x 60x 60x 60x 60x 60x 60x 82x 82x 81x 81x 81x 22x 22x 59x 59x 59x 59x 59x 265x 64x 35x 35x 29x 2x 2x 27x 1x 1x 26x 2x 2x 24x 11x 11x 13x 13x 13x 248x 248x 248x 66x 66x 66x 66x 60x 60x 59x 6x 188x 105x 105x 102x 101x 3x 83x 83x 31x 52x 46x 6x 2x 2x 4x 1x 1x 3x 3x 3x 102x 102x 224x 224x 224x 101x 123x 122x 101x 123x 123x 123x 123x 12x 12x 123x 99x 99x 99x 99x 99x 24x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 10x 9x 2x 2x 7x 7x 1x 122x 11x 111x 231x 231x 1122x 231x 58x 58x 58x 58x 58x 358x 5x 353x 58x 57x 57x 57x 5x 2x 3x 138x 138x 11x 138x 1x 137x 44x 93x 124x 137x 3x 3x 3x 5x 137x 137x 137x | /** * JSONPath parser implementation based on RFC 9535 * https://www.rfc-editor.org/rfc/rfc9535.html */ import {Parser} from './Parser'; import {Ast} from './ast'; import type * as types from './types'; export class JsonPathParser extends Parser { /** * Parse a JSONPath expression string * * @param pathStr - JSONPath expression string (e.g., "$.store.book[0].title"). * @returns Parse result with structured representation or error information. */ public static parse = (pathStr: string): types.ParseResult => new JsonPathParser().parse(pathStr); /** * Parse a JSONPath expression string into a structured representation */ parse(pathStr: string): types.ParseResult { try { this.reset(pathStr); const path = this.parseJSONPath(); return {success: true, path}; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error), position: this.pos, }; } } private parseJSONPath(): types.JSONPath { const segments: types.PathSegment[] = []; // Root path starts with $ this.ws(); if (!this.is('$')) { throw new Error('JSONPath must start with $'); } this.skip(1); // Skip $ this.ws(); // Skip whitespace after $ // Parse segments while (!this.eof()) { this.ws(); if (this.eof()) break; const segment = this.parsePathSegment(); segments.push(segment); } return Ast.path(segments); } private parsePathSegment(): types.PathSegment { this.ws(); // Check for recursive descent (..) const recursive = this.is('..') as boolean; if (recursive) { this.skip(2); // Skip .. // After .., we should have a selector without requiring . or [ Iif (this.eof()) { throw new Error('Expected selector after ..'); } } // Parse selectors const selectors: types.AnySelector[] = []; // Handle different selector formats if (this.is('.')) { // Dot notation: .name, .*, etc. this.skip(1); // Skip . this.ws(); // Skip whitespace after . if (this.is('*')) { // Wildcard this.skip(1); selectors.push(Ast.selector.wildcard()); } else { // Named selector const name = this.parseIdentifier(); selectors.push(Ast.selector.named(name)); } } else if (this.is('[')) { // Bracket notation: [index], ['name'], [*], [start:end], [a,b,c] etc. this.skip(1); // Skip [ this.ws(); // Parse comma-separated list of selectors do { this.ws(); const selector = this.parseBracketSelector(); selectors.push(selector); this.ws(); // Check for comma to continue, or ] to end if (this.is(',')) { this.skip(1); // Skip comma this.ws(); } else { break; } } while (!this.eof() && !this.is(']')); this.ws(); if (!this.is(']')) { throw new Error('Expected ] to close bracket selector'); } this.skip(1); // Skip ] } else if (recursive && this.match(/[a-zA-Z_*]/)) { // After recursive descent, we can have an identifier or wildcard if (this.is('*')) { this.skip(1); selectors.push(Ast.selector.wildcard()); } else { const name = this.parseIdentifier(); selectors.push(Ast.selector.named(name)); } } else E{ throw new Error('Expected . or [ to start path segment'); } if (recursive && selectors.length === 1) { return Ast.segment([Ast.selector.recursiveDescent(selectors[0])]); } return Ast.segment(selectors, recursive || undefined); } private parseBracketSelector(): types.AnySelector { this.ws(); if (this.is('*')) { // Wildcard this.skip(1); return Ast.selector.wildcard(); } else if (this.is('?')) { // Filter expression this.skip(1); // Skip ? this.ws(); // Parentheses are optional according to RFC 9535, but commonly used let hasParens = false; if (this.is('(')) { hasParens = true; this.skip(1); this.ws(); } const expression = this.parseFilterExpression(); if (hasParens) { this.ws(); Iif (!this.is(')')) { throw new Error('Expected ) to close filter expression'); } this.skip(1); } return Ast.selector.filter(expression); } else if (this.is("'") || this.is('"')) { // Quoted string selector const name = this.parseString(); return Ast.selector.named(name); } else { // Number or slice let first: number | undefined; if (!this.is(':')) { first = this.parseNumber(); this.ws(); } if (this.is(':')) { // Slice selector this.skip(1); // Skip : this.ws(); let end: number | undefined; let step: number | undefined; if (!this.is(']') && !this.is(':') && !this.is(',')) { end = this.parseNumber(); this.ws(); } if (this.is(':')) { this.skip(1); // Skip : this.ws(); if (!this.is(']') && !this.is(',')) { step = this.parseNumber(); } } return Ast.selector.slice(first, end, step); } else { // Index selector Iif (first === undefined) { throw new Error('Expected index or slice'); } return Ast.selector.index(first); } } } private parseFilterExpression(): types.FilterExpression { return this.parseLogicalOrExpression(); } private parseLogicalOrExpression(): types.FilterExpression { let left = this.parseLogicalAndExpression(); this.ws(); while (this.is('||')) { this.skip(2); this.ws(); const right = this.parseLogicalAndExpression(); left = Ast.expression.logical('||', left, right); this.ws(); } return left; } private parseLogicalAndExpression(): types.FilterExpression { let left = this.parseComparisonExpression(); this.ws(); while (this.is('&&')) { this.skip(2); this.ws(); const right = this.parseComparisonExpression(); left = Ast.expression.logical('&&', left, right); this.ws(); } return left; } private parseComparisonExpression(): types.FilterExpression { return this.parseUnaryExpression(); } private parseUnaryExpression(): types.FilterExpression { this.ws(); if (this.is('!')) { // Handle logical NOT operator this.skip(1); this.ws(); const expression = this.parseUnaryExpression(); return Ast.expression.negation(expression); } if (this.is('(')) { // Handle parenthesized expressions this.skip(1); this.ws(); const expression = this.parseFilterExpression(); this.ws(); Iif (!this.is(')')) { throw new Error('Expected ) to close parenthesized expression'); } this.skip(1); return Ast.expression.paren(expression); } return this.parsePrimaryExpression(); } private parsePrimaryExpression(): types.FilterExpression { const left = this.parseValueExpression(); this.ws(); if (this.isComparisonOperator()) { const operator = this.parseComparisonOperator(); this.ws(); const right = this.parseValueExpression(); return Ast.expression.comparison(operator, left, right); } // If no comparison operator, this is an existence test if (left.type === 'path') { return Ast.expression.existence(left.path); } else Iif (left.type === 'current') { // @ by itself is an existence test for current node return Ast.expression.existence(Ast.path([])); } else if (left.type === 'function') { // Function expressions can be used as test expressions (e.g., $[?match(@.name, "pattern")]) return left as types.FilterExpression; } // If no comparison operator, treat as existence test return Ast.expression.existence(Ast.path([])); // TODO: implement proper existence test with the path } private parseFunctionName(): string { const start = this.pos; Iif (!this.match(/[a-z]/)) throw new Error('Expected function name'); while (this.match(/[a-zA-Z0-9_]/)) this.skip(1); return this.str.slice(start, this.pos); } private parseFunctionExpression(): types.FunctionExpression { const name = this.parseFunctionName(); this.ws(); Iif (!this.is('(')) throw new Error('Expected "(" after function name'); this.skip(1); // Skip ( this.ws(); const args: (types.ValueExpression | types.FilterExpression | types.JSONPath)[] = []; if (!this.is(')')) { do { this.ws(); // Try to parse as value expression first const arg = this.parseValueExpression(); args.push(arg); this.ws(); if (this.is(',')) { this.skip(1); this.ws(); } else break; } while (!this.eof() && !this.is(')')); } this.ws(); Iif (!this.is(')')) throw new Error('Expected ")" to close function arguments'); this.skip(1); return Ast.expression.function(name, args); } private isComparisonOperator(): boolean { return this.is('==') || this.is('!=') || this.is('<=') || this.is('>=') || this.is('<') || this.is('>'); } private parseComparisonOperator(): types.ComparisonExpression['operator'] { if (this.is('==')) { this.skip(2); return '=='; } else if (this.is('!=')) { this.skip(2); return '!='; } else if (this.is('<=')) { this.skip(2); return '<='; } else if (this.is('>=')) { this.skip(2); return '>='; } else if (this.is('<')) { this.skip(1); return '<'; } else if (this.is('>')) { this.skip(1); return '>'; } throw new Error('Expected comparison operator'); } private parseValueExpression(): types.ValueExpression { this.ws(); const value = Ast.value; // Check for function expressions first if (this.match(/[a-z]/)) { const start = this.pos; const name = this.parseFunctionName(); this.ws(); if (this.is('(')) { // It's a function call - reset and parse as function this.pos = start; const funcExpr = this.parseFunctionExpression(); return value.function(funcExpr.name, funcExpr.args); } else { // Reset position, it's not a function this.pos = start; } } if (this.is('@')) { this.skip(1); if (this.is('.') || this.is('[')) { const segments = this.parseFilterPathSegments(); return value.path(Ast.path(segments)); } return value.current(); } else Iif (this.is('$')) { // Root node path this.skip(1); const segments = this.parsePathSegments(); return value.path(Ast.path(segments)); } else if (this.is("'") || this.is('"')) { // String literal return value.literal(this.parseString()); } else if (this.match(/[0-9-]/)) { // Number literal return value.literal(this.parseNumber()); } else if (this.is('true')) { this.skip(4); return value.literal(true); } else if (this.is('false')) { this.skip(5); return value.literal(false); } else if (this.is('null')) { this.skip(4); return value.literal(null); } throw new Error('Expected value expression'); } private parsePathSegments(): types.PathSegment[] { const segments: types.PathSegment[] = []; while (!this.eof()) { this.ws(); Iif (this.eof()) break; // Check if we've reached a delimiter that ends the path Iif (this.is(')') || this.is(',') || this.is('&&') || this.is('||') || this.isComparisonOperator()) break; const segment = this.parsePathSegment(); segments.push(segment); } return segments; } private parseFilterPathSegments(): types.PathSegment[] { const segments: types.PathSegment[] = []; while (!this.eof()) { this.ws(); Iif (this.eof()) break; // Check if we've reached a delimiter that ends the path in filter context if (this.is(')') || this.is(',') || this.is('&&') || this.is('||') || this.isComparisonOperator() || this.is(']')) break; const segment = this.parseFilterPathSegment(); segments.push(segment); } return segments; } private parseFilterPathSegment(): types.PathSegment { this.ws(); const selectors: types.AnySelector[] = []; // Check for recursive descent (..) const recursive = this.is('..') as boolean; if (recursive) { this.skip(2); // Skip .. // After .., we should have a selector without requiring . or [ Iif (this.eof()) { throw new Error('Expected selector after ..'); } } if (this.is('.')) { // Dot notation: .name, .*, etc. this.skip(1); // Skip . this.ws(); // Skip whitespace after . Iif (this.is('*')) { // Wildcard this.skip(1); selectors.push(Ast.selector.wildcard()); } else { // Named selector const name = this.parseIdentifier(); selectors.push(Ast.selector.named(name)); } } else if (this.is('[')) { // Bracket notation: [index], ['name'], [*], [start:end], [a,b,c] etc. this.skip(1); // Skip [ this.ws(); // Parse comma-separated list of selectors do { this.ws(); const selector = this.parseBracketSelector(); selectors.push(selector); this.ws(); // Check for comma to continue, or ] to end Iif (this.is(',')) { this.skip(1); // Skip comma this.ws(); } else { break; } } while (!this.eof() && !this.is(']')); this.ws(); Iif (!this.is(']')) { throw new Error('Expected ] to close bracket selector'); } this.skip(1); // Skip ] } else if (recursive && this.match(/[a-zA-Z_*]/)) { // After recursive descent, we can have an identifier or wildcard if (this.is('*')) { this.skip(1); selectors.push(Ast.selector.wildcard()); } else { const name = this.parseIdentifier(); selectors.push(Ast.selector.named(name)); } } else { throw new Error('Expected . or [ to start filter path segment'); } if (recursive && selectors.length === 1) { return Ast.segment([Ast.selector.recursiveDescent(selectors[0])]); } return Ast.segment(selectors, recursive || undefined); } private parseIdentifier(): string { const start = this.pos; Iif (!this.match(/[a-zA-Z_]/)) throw new Error('Expected identifier'); while (this.match(/[a-zA-Z0-9_]/)) this.skip(1); return this.str.slice(start, this.pos); } private parseString(): string { const quote = this.peek(); Iif (quote !== "'" && quote !== '"') throw new Error('Expected string literal'); this.skip(1); // Skip opening quote const start = this.pos; while (!this.eof() && !this.is(quote)) if (this.is('\\')) this.skip(2); // Skip escape sequence else this.skip(1); if (this.eof()) throw new Error('Unterminated string literal'); const value = this.str.slice(start, this.pos); this.skip(1); // Skip closing quote return value.replace(/\\./g, (match: string) => { switch (match[1]) { case 'n': return '\n'; case 't': return '\t'; case 'r': return '\r'; case 'b': return '\b'; case 'f': return '\f'; case '\\': return '\\'; case '/': return '/'; case '"': return '"'; case "'": return "'"; default: return match; } }); } private parseNumber(): number { const start = this.pos; if (this.is('-')) { this.skip(1); } if (!this.match(/[0-9]/)) { throw new Error('Expected number'); } if (this.is('0')) { this.skip(1); } else { while (this.match(/[0-9]/)) { this.skip(1); } } if (this.is('.')) { this.skip(1); Iif (!this.match(/[0-9]/)) { throw new Error('Expected digit after decimal point'); } while (this.match(/[0-9]/)) { this.skip(1); } } Iif (this.match(/[eE]/)) { this.skip(1); Iif (this.match(/[+-]/)) { this.skip(1); } Iif (!this.match(/[0-9]/)) { throw new Error('Expected digit in exponent'); } while (this.match(/[0-9]/)) { this.skip(1); } } const numStr = this.str.slice(start, this.pos); return Number.parseFloat(numStr); } } |