@swaggerexpert/jsonpath 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,6 +30,10 @@ The development of this library contributed to the identification and formal sub
30
30
  - [Installation](#installation)
31
31
  - [Usage](#usage)
32
32
  - [Parsing](#parsing)
33
+ - [Concrete Syntax Tree (CST)](#concrete-syntax-tree-cst)
34
+ - [Interpreting Parse result as XML](#interpreting-parse-result-as-xml)
35
+ - [Statistics](#statistics)
36
+ - [Tracing](#tracing)
33
37
  - [Errors](#errors)
34
38
  - [Grammar](#grammar)
35
39
  - [More about JSONPath](#more-about-jsonpath)
@@ -256,7 +260,27 @@ const parseResult = parse('$.store.book[0].title', { ast: new JSONPathQueryCST()
256
260
  }
257
261
  ```
258
262
 
259
- ###### Interpreting AST as XML
263
+ ###### Concrete Syntax Tree (CST)
264
+
265
+ [Concrete Syntax Tree](https://en.wikipedia.org/wiki/Parse_tree) (Parse tree) is available on parse result via `computed` field.
266
+ Instance of `JSONPathQueryCST` needs to be assigned to `ast` option in `parse` function (default behavior).
267
+ CST is suitable to be consumed by other tools like IDEs, editors, etc...
268
+
269
+ ```js
270
+ import { parse } from '@swaggerexpert/jsonpath';
271
+
272
+ const { computed: CST } = parse('$.store.book[0].title');
273
+ ```
274
+
275
+ or
276
+
277
+ ```js
278
+ import { parse, JSONPathQueryCST } from '@swaggerexpert/jsonpath';
279
+
280
+ const { computed: CST } = parse('$.store.book[0].title', { ast: new JSONPathQueryCST() });
281
+ ```
282
+
283
+ ###### Interpreting Parse result as XML
260
284
 
261
285
  ```js
262
286
  import { parse } from '@swaggerexpert/jsonpath';
@@ -265,6 +289,39 @@ const parseResult = parse('$.store.book[0].title');
265
289
  const xml = parseResult.ast.toXml();
266
290
  ```
267
291
 
292
+ ###### Statistics
293
+
294
+ `parse` function returns additional statistical information about the parsing process.
295
+ Collection of the statistics can be enabled by setting `stats` option to `true`.
296
+
297
+ ```js
298
+ import { parse } from '@swaggerexpert/jsonpath';
299
+
300
+ const { stats } = parse('$.store.book[0].title', { stats: true });
301
+
302
+ stats.displayStats(); // returns operator stats
303
+ stats.displayHits(); // returns rules grouped by hit count
304
+ ```
305
+
306
+ ###### Tracing
307
+
308
+ `parse` function returns additional tracing information about the parsing process.
309
+ Tracing can be enabled by setting `trace` option to `true`. Tracing is essential
310
+ for debugging failed matches or analyzing rule execution flow.
311
+
312
+ ```js
313
+ import { parse } from '@swaggerexpert/jsonpath';
314
+
315
+ const { result, trace } = parse('$fdfadfd', { trace: true });
316
+
317
+ result.success; // returns false
318
+ trace.displayTrace(); // returns trace information
319
+ ```
320
+
321
+ By combining information from `result` and `trace`, it is possible to analyze the parsing process in detail
322
+ and generate a messages like this: `'Syntax error at position 1, expected "[", ".", ".."'`. Please see this
323
+ [test file](https://github.com/swaggerexpert/jsonpath/blob/main/test/parse/trace.js) for more information how to achieve that.
324
+
268
325
  #### Errors
269
326
 
270
327
  `@swaggerexpert/jsonpath` provides a structured error class hierarchy,
@@ -453,8 +510,8 @@ LCALPHA = %x61-7A ; "a".."z"
453
510
  function-expr = function-name left-paren S [function-argument ; MODIFICATION: surrogate text rule used
454
511
  *(S comma S function-argument)] S right-paren ; MODIFICATION: surrogate text rule used
455
512
  function-argument = logical-expr / ; MODIFICATION: https://www.rfc-editor.org/errata/eid8343
456
- function-expr /
457
513
  filter-query / ; (includes singular-query)
514
+ function-expr /
458
515
  literal
459
516
 
460
517
 
@@ -11,6 +11,8 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
11
11
  const grammar = new _grammar.default();
12
12
  const parse = (jsonPath, {
13
13
  ast = new _JSONPathQueryCST.default(),
14
+ stats = false,
15
+ trace = false,
14
16
  evaluator = _translate.default
15
17
  } = {}) => {
16
18
  if (typeof jsonPath !== 'string') {
@@ -19,6 +21,8 @@ const parse = (jsonPath, {
19
21
  try {
20
22
  const parser = new _apgLite.Parser();
21
23
  parser.ast = ast;
24
+ if (stats) parser.stats = new _apgLite.Stats();
25
+ if (trace) parser.trace = new _apgLite.Trace();
22
26
  const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
23
27
  const computed = evaluator(ast, {
24
28
  result
@@ -26,6 +30,8 @@ const parse = (jsonPath, {
26
30
  return {
27
31
  result,
28
32
  ast,
33
+ stats: parser.stats,
34
+ trace: parser.trace,
29
35
  computed
30
36
  };
31
37
  } catch (error) {
@@ -1,4 +1,4 @@
1
- import { Parser } from 'apg-lite';
1
+ import { Parser, Stats, Trace } from 'apg-lite';
2
2
  import Grammar from "../grammar.mjs";
3
3
  import translateEvaluator from "./evaluators/translate.mjs";
4
4
  import JSONPathQueryCST from "./ast/JSONPathQueryCST.mjs";
@@ -6,6 +6,8 @@ import JSONPathParseError from "../errors/JSONPathParseError.mjs";
6
6
  const grammar = new Grammar();
7
7
  const parse = (jsonPath, {
8
8
  ast = new JSONPathQueryCST(),
9
+ stats = false,
10
+ trace = false,
9
11
  evaluator = translateEvaluator
10
12
  } = {}) => {
11
13
  if (typeof jsonPath !== 'string') {
@@ -14,6 +16,8 @@ const parse = (jsonPath, {
14
16
  try {
15
17
  const parser = new Parser();
16
18
  parser.ast = ast;
19
+ if (stats) parser.stats = new Stats();
20
+ if (trace) parser.trace = new Trace();
17
21
  const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
18
22
  const computed = evaluator(ast, {
19
23
  result
@@ -21,6 +25,8 @@ const parse = (jsonPath, {
21
25
  return {
22
26
  result,
23
27
  ast,
28
+ stats: parser.stats,
29
+ trace: parser.trace,
24
30
  computed
25
31
  };
26
32
  } catch (error) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.0.2",
6
+ "version": "1.2.0",
7
7
  "description": "RCF 9535 implementation of JSONPath",
8
8
  "main": "./cjs/index.cjs",
9
9
  "types": "./types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -5,6 +5,8 @@ export function parse(jsonpath: string, options?: ParseOptions): ParseResult;
5
5
 
6
6
  export interface ParseOptions {
7
7
  readonly ast?: AST;
8
+ readonly stats?: boolean;
9
+ readonly trace?: boolean;
8
10
  }
9
11
 
10
12
  export interface AST {
@@ -18,14 +20,25 @@ export interface ParseResult {
18
20
  };
19
21
  readonly ast: AST;
20
22
  readonly computed: Record<string, CSTNode>;
23
+ readonly stats?: Stats;
24
+ readonly trace?: Trace;
21
25
  }
22
26
 
23
27
  export interface CSTNode {
24
- type: string,
25
- text: string,
26
- start: number,
27
- length: number,
28
- children: CSTNode[],
28
+ readonly type: string,
29
+ readonly text: string,
30
+ readonly start: number,
31
+ readonly length: number,
32
+ readonly children: CSTNode[],
33
+ }
34
+
35
+ export interface Stats {
36
+ displayStats(): string;
37
+ displayHits(): string;
38
+ }
39
+
40
+ export interface Trace {
41
+ displayTrace(): string;
29
42
  }
30
43
 
31
44
  /**