@swaggerexpert/jsonpath 1.0.1 → 1.1.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 +39 -2
- package/cjs/grammar.cjs +5 -5
- package/cjs/parse/index.cjs +3 -0
- package/es/grammar.mjs +5 -5
- package/es/parse/index.mjs +4 -1
- package/package.json +1 -1
- package/types/index.d.ts +12 -5
package/README.md
CHANGED
|
@@ -30,6 +30,9 @@ 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
|
+
- [Parse statistics](#parse-statistics)
|
|
33
36
|
- [Errors](#errors)
|
|
34
37
|
- [Grammar](#grammar)
|
|
35
38
|
- [More about JSONPath](#more-about-jsonpath)
|
|
@@ -256,7 +259,27 @@ const parseResult = parse('$.store.book[0].title', { ast: new JSONPathQueryCST()
|
|
|
256
259
|
}
|
|
257
260
|
```
|
|
258
261
|
|
|
259
|
-
######
|
|
262
|
+
###### Concrete Syntax Tree (CST)
|
|
263
|
+
|
|
264
|
+
[Concrete Syntax Tree](https://en.wikipedia.org/wiki/Parse_tree) (Parse tree) is available on parse result via `computed` field.
|
|
265
|
+
Instance of `JSONPathQueryCST` needs to be assigned to `ast` option in `parse` function (default behavior).
|
|
266
|
+
CST is suitable to be consumed by other tools like IDEs, editors, etc...
|
|
267
|
+
|
|
268
|
+
```js
|
|
269
|
+
import { parse } from '@swaggerexpert/jsonpath';
|
|
270
|
+
|
|
271
|
+
const { computed: CST } = parse('$.store.book[0].title');
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
or
|
|
275
|
+
|
|
276
|
+
```js
|
|
277
|
+
import { parse, JSONPathQueryCST } from '@swaggerexpert/jsonpath';
|
|
278
|
+
|
|
279
|
+
const { computed: CST } = parse('$.store.book[0].title', { ast: new JSONPathQueryCST() });
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
###### Interpreting Parse result as XML
|
|
260
283
|
|
|
261
284
|
```js
|
|
262
285
|
import { parse } from '@swaggerexpert/jsonpath';
|
|
@@ -265,6 +288,20 @@ const parseResult = parse('$.store.book[0].title');
|
|
|
265
288
|
const xml = parseResult.ast.toXml();
|
|
266
289
|
```
|
|
267
290
|
|
|
291
|
+
###### Parse statistics
|
|
292
|
+
|
|
293
|
+
`parse` function returns additional statistical information about the parsing process.
|
|
294
|
+
Collection of the statistics can be enabled by setting `stats` option to `true`.
|
|
295
|
+
|
|
296
|
+
```js
|
|
297
|
+
import { parse } from '@swaggerexpert/jsonpath';
|
|
298
|
+
|
|
299
|
+
const { stats } = parse('$.store.book[0].title', { stats: true });
|
|
300
|
+
|
|
301
|
+
stats.displayStats(); // returns operator stats
|
|
302
|
+
stats.displayHits(); // returns rules grouped by hit count
|
|
303
|
+
```
|
|
304
|
+
|
|
268
305
|
#### Errors
|
|
269
306
|
|
|
270
307
|
`@swaggerexpert/jsonpath` provides a structured error class hierarchy,
|
|
@@ -453,8 +490,8 @@ LCALPHA = %x61-7A ; "a".."z"
|
|
|
453
490
|
function-expr = function-name left-paren S [function-argument ; MODIFICATION: surrogate text rule used
|
|
454
491
|
*(S comma S function-argument)] S right-paren ; MODIFICATION: surrogate text rule used
|
|
455
492
|
function-argument = logical-expr / ; MODIFICATION: https://www.rfc-editor.org/errata/eid8343
|
|
456
|
-
function-expr /
|
|
457
493
|
filter-query / ; (includes singular-query)
|
|
494
|
+
function-expr /
|
|
458
495
|
literal
|
|
459
496
|
|
|
460
497
|
|
package/cjs/grammar.cjs
CHANGED
|
@@ -1998,13 +1998,13 @@ function grammar() {
|
|
|
1998
1998
|
index: 27
|
|
1999
1999
|
}; // RNM(logical-expr)
|
|
2000
2000
|
this.rules[58].opcodes[2] = {
|
|
2001
|
-
type: 4,
|
|
2002
|
-
index: 57
|
|
2003
|
-
}; // RNM(function-expr)
|
|
2004
|
-
this.rules[58].opcodes[3] = {
|
|
2005
2001
|
type: 4,
|
|
2006
2002
|
index: 34
|
|
2007
2003
|
}; // RNM(filter-query)
|
|
2004
|
+
this.rules[58].opcodes[3] = {
|
|
2005
|
+
type: 4,
|
|
2006
|
+
index: 57
|
|
2007
|
+
}; // RNM(function-expr)
|
|
2008
2008
|
this.rules[58].opcodes[4] = {
|
|
2009
2009
|
type: 4,
|
|
2010
2010
|
index: 38
|
|
@@ -2749,8 +2749,8 @@ function grammar() {
|
|
|
2749
2749
|
str += "function-expr = function-name left-paren S [function-argument ; MODIFICATION: surrogate text rule used\n";
|
|
2750
2750
|
str += " *(S comma S function-argument)] S right-paren ; MODIFICATION: surrogate text rule used\n";
|
|
2751
2751
|
str += "function-argument = logical-expr / ; MODIFICATION: https://www.rfc-editor.org/errata/eid8343\n";
|
|
2752
|
-
str += " function-expr /\n";
|
|
2753
2752
|
str += " filter-query / ; (includes singular-query)\n";
|
|
2753
|
+
str += " function-expr /\n";
|
|
2754
2754
|
str += " literal\n";
|
|
2755
2755
|
str += "\n";
|
|
2756
2756
|
str += "\n";
|
package/cjs/parse/index.cjs
CHANGED
|
@@ -11,6 +11,7 @@ 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,
|
|
14
15
|
evaluator = _translate.default
|
|
15
16
|
} = {}) => {
|
|
16
17
|
if (typeof jsonPath !== 'string') {
|
|
@@ -19,6 +20,7 @@ const parse = (jsonPath, {
|
|
|
19
20
|
try {
|
|
20
21
|
const parser = new _apgLite.Parser();
|
|
21
22
|
parser.ast = ast;
|
|
23
|
+
if (stats) parser.stats = new _apgLite.Stats();
|
|
22
24
|
const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
|
|
23
25
|
const computed = evaluator(ast, {
|
|
24
26
|
result
|
|
@@ -26,6 +28,7 @@ const parse = (jsonPath, {
|
|
|
26
28
|
return {
|
|
27
29
|
result,
|
|
28
30
|
ast,
|
|
31
|
+
stats: parser.stats,
|
|
29
32
|
computed
|
|
30
33
|
};
|
|
31
34
|
} catch (error) {
|
package/es/grammar.mjs
CHANGED
|
@@ -1994,13 +1994,13 @@ export default function grammar() {
|
|
|
1994
1994
|
index: 27
|
|
1995
1995
|
}; // RNM(logical-expr)
|
|
1996
1996
|
this.rules[58].opcodes[2] = {
|
|
1997
|
-
type: 4,
|
|
1998
|
-
index: 57
|
|
1999
|
-
}; // RNM(function-expr)
|
|
2000
|
-
this.rules[58].opcodes[3] = {
|
|
2001
1997
|
type: 4,
|
|
2002
1998
|
index: 34
|
|
2003
1999
|
}; // RNM(filter-query)
|
|
2000
|
+
this.rules[58].opcodes[3] = {
|
|
2001
|
+
type: 4,
|
|
2002
|
+
index: 57
|
|
2003
|
+
}; // RNM(function-expr)
|
|
2004
2004
|
this.rules[58].opcodes[4] = {
|
|
2005
2005
|
type: 4,
|
|
2006
2006
|
index: 38
|
|
@@ -2745,8 +2745,8 @@ export default function grammar() {
|
|
|
2745
2745
|
str += "function-expr = function-name left-paren S [function-argument ; MODIFICATION: surrogate text rule used\n";
|
|
2746
2746
|
str += " *(S comma S function-argument)] S right-paren ; MODIFICATION: surrogate text rule used\n";
|
|
2747
2747
|
str += "function-argument = logical-expr / ; MODIFICATION: https://www.rfc-editor.org/errata/eid8343\n";
|
|
2748
|
-
str += " function-expr /\n";
|
|
2749
2748
|
str += " filter-query / ; (includes singular-query)\n";
|
|
2749
|
+
str += " function-expr /\n";
|
|
2750
2750
|
str += " literal\n";
|
|
2751
2751
|
str += "\n";
|
|
2752
2752
|
str += "\n";
|
package/es/parse/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Parser } from 'apg-lite';
|
|
1
|
+
import { Parser, Stats } 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,7 @@ 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,
|
|
9
10
|
evaluator = translateEvaluator
|
|
10
11
|
} = {}) => {
|
|
11
12
|
if (typeof jsonPath !== 'string') {
|
|
@@ -14,6 +15,7 @@ const parse = (jsonPath, {
|
|
|
14
15
|
try {
|
|
15
16
|
const parser = new Parser();
|
|
16
17
|
parser.ast = ast;
|
|
18
|
+
if (stats) parser.stats = new Stats();
|
|
17
19
|
const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
|
|
18
20
|
const computed = evaluator(ast, {
|
|
19
21
|
result
|
|
@@ -21,6 +23,7 @@ const parse = (jsonPath, {
|
|
|
21
23
|
return {
|
|
22
24
|
result,
|
|
23
25
|
ast,
|
|
26
|
+
stats: parser.stats,
|
|
24
27
|
computed
|
|
25
28
|
};
|
|
26
29
|
} catch (error) {
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export function parse(jsonpath: string, options?: ParseOptions): ParseResult;
|
|
|
5
5
|
|
|
6
6
|
export interface ParseOptions {
|
|
7
7
|
readonly ast?: AST;
|
|
8
|
+
readonly stats?: boolean;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
export interface AST {
|
|
@@ -18,14 +19,20 @@ export interface ParseResult {
|
|
|
18
19
|
};
|
|
19
20
|
readonly ast: AST;
|
|
20
21
|
readonly computed: Record<string, CSTNode>;
|
|
22
|
+
readonly stats?: Stats;
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export interface CSTNode {
|
|
24
|
-
type: string,
|
|
25
|
-
text: string,
|
|
26
|
-
start: number,
|
|
27
|
-
length: number,
|
|
28
|
-
children: CSTNode[],
|
|
26
|
+
readonly type: string,
|
|
27
|
+
readonly text: string,
|
|
28
|
+
readonly start: number,
|
|
29
|
+
readonly length: number,
|
|
30
|
+
readonly children: CSTNode[],
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Stats {
|
|
34
|
+
displayStats(): string;
|
|
35
|
+
displayHits(): string;
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
/**
|