@tabnas/path 0.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/LICENSE +21 -0
- package/README.md +78 -0
- package/dist/path.d.ts +5 -0
- package/dist/path.js +98 -0
- package/dist/path.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +51 -0
- package/src/path.ts +108 -0
- package/src/tsconfig.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 jsonicjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @tabnas/path (Tabnas parser plugin)
|
|
2
|
+
|
|
3
|
+
This plugin adds property-path tracking to the
|
|
4
|
+
[Tabnas](https://github.com/tabnas/parser) parser so that rule actions can
|
|
5
|
+
see the path (keys and indices) leading to the current value.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
[](https://npmjs.com/package/@tabnas/path)
|
|
9
|
+
[](https://github.com/tabnas/path/actions/workflows/build.yml)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|  | This open source module is sponsored and supported by [Voxgig](https://www.voxgig.com). |
|
|
13
|
+
| ---------------------------------------------------- | --------------------------------------------------------------------------------------- |
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install @tabnas/parser @tabnas/json @tabnas/path
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The Tabnas engine ships no grammar of its own — you bring a grammar plugin
|
|
23
|
+
that defines the `val` / `map` / `pair` / `list` / `elem` rules (here,
|
|
24
|
+
`@tabnas/json`). Install the grammar first, then `Path` on top.
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Tiny example
|
|
28
|
+
|
|
29
|
+
`Path` populates `r.k.path` for every value; a second plugin reads it. Here
|
|
30
|
+
each scalar is tagged `<value:path>` and each map gets a `$` of `<path>`:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const { Tabnas } = require('@tabnas/parser')
|
|
34
|
+
const { json } = require('@tabnas/json')
|
|
35
|
+
const { Path } = require('@tabnas/path')
|
|
36
|
+
|
|
37
|
+
const capture = (tn) => {
|
|
38
|
+
tn.rule('val', (rs) =>
|
|
39
|
+
rs.ac(false, (r) => {
|
|
40
|
+
if (null === r.node || 'object' !== typeof r.node) {
|
|
41
|
+
r.node = `<${r.node}:${r.k.path}>`
|
|
42
|
+
} else if (!Array.isArray(r.node)) {
|
|
43
|
+
r.node.$ = `<${r.k.path}>`
|
|
44
|
+
}
|
|
45
|
+
}),
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const parser = new Tabnas({ plugins: [json] }).use(Path).use(capture)
|
|
50
|
+
const out = parser.parse('{"a":[1,2]}')
|
|
51
|
+
|
|
52
|
+
out.a // => ['<1:a,0>', '<2:a,1>']
|
|
53
|
+
out.a[0] // => '<1:a,0>'
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Each scalar carries its full path: `1` lives at `a, 0` and `2` at `a, 1`.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
## Documentation
|
|
60
|
+
|
|
61
|
+
The docs follow the [Diátaxis](https://diataxis.fr) four-quadrant structure:
|
|
62
|
+
|
|
63
|
+
- [Tutorial](doc/tutorial.md) — zero to a working path-tracking parser, step
|
|
64
|
+
by step.
|
|
65
|
+
- [How-to guides](doc/guide.md) — recipes: read the path, keep a copy, seed
|
|
66
|
+
a base path, classify segments.
|
|
67
|
+
- [Reference](doc/reference.md) — exports, options, the `Rule.k` keys, the
|
|
68
|
+
function refs, meta input.
|
|
69
|
+
- [Concepts](doc/concepts.md) — how it works, the engine relationship, the
|
|
70
|
+
array-pool / mutability trade-off.
|
|
71
|
+
|
|
72
|
+
The Go port lives in [`../go/`](../go/) with its own
|
|
73
|
+
[docs](../go/doc/tutorial.md).
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
MIT. Copyright (c) Richard Rodger.
|
package/dist/path.d.ts
ADDED
package/dist/path.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2022-2026 Richard Rodger, MIT License */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Path = void 0;
|
|
5
|
+
// The host-grammar rules the plugin hooks. The Tabnas engine ships no
|
|
6
|
+
// grammar of its own, so these names must match the rules supplied by
|
|
7
|
+
// whatever grammar plugin the consumer installs. The standard
|
|
8
|
+
// value/map/pair/list/elem rule set uses these names. Each empty rule
|
|
9
|
+
// entry causes tabnas.grammar() to auto-wire any matching
|
|
10
|
+
// @<rulename>-<phase> function refs as state actions.
|
|
11
|
+
const HOOKED_RULES = ['val', 'map', 'pair', 'list', 'elem'];
|
|
12
|
+
// Preallocated array pool for path tracking. Each depth level gets
|
|
13
|
+
// a reusable array of that length. The arrays are mutated in place
|
|
14
|
+
// as the parser traverses the tree — no allocation per pair/elem.
|
|
15
|
+
//
|
|
16
|
+
// IMPORTANT: r.k.path is a mutable, shared array. Client code that
|
|
17
|
+
// needs to retain the path beyond the current parse callback MUST
|
|
18
|
+
// copy it (e.g. r.k.path.slice() or [...r.k.path]).
|
|
19
|
+
const MAX_PATH_DEPTH = 64;
|
|
20
|
+
const pathPool = [];
|
|
21
|
+
for (let i = 0; i <= MAX_PATH_DEPTH; i++) {
|
|
22
|
+
pathPool[i] = new Array(i);
|
|
23
|
+
}
|
|
24
|
+
/* Keeps track of the property path to the current location.
|
|
25
|
+
* Example: {a:{b:1 ## path=["a","b"]
|
|
26
|
+
* Use the Rule.k key-value store so that the path is propagated to children and followers.
|
|
27
|
+
* Depth must be greater than 0 - ensures path only starts once top level implicit is set up.
|
|
28
|
+
*/
|
|
29
|
+
const Path = (tabnas, _options) => {
|
|
30
|
+
const refs = {
|
|
31
|
+
'@val-bo': (r, ctx) => {
|
|
32
|
+
// At top level, create path array, or inherit from meta context.
|
|
33
|
+
if (0 === r.d) {
|
|
34
|
+
const base = ctx.meta.path?.base;
|
|
35
|
+
if (base && base.length > 0) {
|
|
36
|
+
const arr = pathPool[base.length] || new Array(base.length);
|
|
37
|
+
for (let i = 0; i < base.length; i++)
|
|
38
|
+
arr[i] = base[i];
|
|
39
|
+
r.k.path = arr;
|
|
40
|
+
r.k.pathDepth = base.length;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
r.k.path = pathPool[0];
|
|
44
|
+
r.k.pathDepth = 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
'@map-bo': (r) => {
|
|
49
|
+
// Not in an array, so no need to track element index.
|
|
50
|
+
r.k.index = undefined;
|
|
51
|
+
},
|
|
52
|
+
'@pair-ao': (r) => {
|
|
53
|
+
if (0 < r.d && r.u.pair) {
|
|
54
|
+
const depth = (r.k.pathDepth || 0) + 1;
|
|
55
|
+
const arr = pathPool[depth] || (pathPool[depth] = new Array(depth));
|
|
56
|
+
const parent = r.k.path;
|
|
57
|
+
const parentLen = depth - 1;
|
|
58
|
+
for (let i = 0; i < parentLen; i++)
|
|
59
|
+
arr[i] = parent[i];
|
|
60
|
+
arr[parentLen] = r.u.key;
|
|
61
|
+
r.child.k.path = arr;
|
|
62
|
+
r.child.k.pathDepth = depth;
|
|
63
|
+
r.child.k.key = r.u.key;
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
'@list-bo': (r) => {
|
|
67
|
+
// In array, the path property is the element index.
|
|
68
|
+
r.k.index = -1;
|
|
69
|
+
},
|
|
70
|
+
'@elem-ao': (r) => {
|
|
71
|
+
if (0 < r.d) {
|
|
72
|
+
r.k.index = 1 + r.k.index;
|
|
73
|
+
const depth = (r.k.pathDepth || 0) + 1;
|
|
74
|
+
const arr = pathPool[depth] || (pathPool[depth] = new Array(depth));
|
|
75
|
+
const parent = r.k.path;
|
|
76
|
+
const parentLen = depth - 1;
|
|
77
|
+
for (let i = 0; i < parentLen; i++)
|
|
78
|
+
arr[i] = parent[i];
|
|
79
|
+
arr[parentLen] = r.k.index;
|
|
80
|
+
r.child.k.path = arr;
|
|
81
|
+
r.child.k.pathDepth = depth;
|
|
82
|
+
r.child.k.key = r.k.index;
|
|
83
|
+
r.child.k.index = r.k.index;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
// Declare the rules to hook and attach the refs. The empty rule specs
|
|
88
|
+
// leave the host grammar's rules intact; only the @<rule>-<phase> refs
|
|
89
|
+
// are wired in as state actions.
|
|
90
|
+
const grammarDef = {
|
|
91
|
+
rule: Object.fromEntries(HOOKED_RULES.map((name) => [name, {}])),
|
|
92
|
+
ref: refs,
|
|
93
|
+
};
|
|
94
|
+
tabnas.grammar(grammarDef);
|
|
95
|
+
};
|
|
96
|
+
exports.Path = Path;
|
|
97
|
+
Path.defaults = {};
|
|
98
|
+
//# sourceMappingURL=path.js.map
|
package/dist/path.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.js","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":";AAAA,yDAAyD;;;AAMzD,sEAAsE;AACtE,sEAAsE;AACtE,8DAA8D;AAC9D,sEAAsE;AACtE,0DAA0D;AAC1D,sDAAsD;AACtD,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAE3D,mEAAmE;AACnE,mEAAmE;AACnE,kEAAkE;AAClE,EAAE;AACF,mEAAmE;AACnE,kEAAkE;AAClE,oDAAoD;AACpD,MAAM,cAAc,GAAG,EAAE,CAAA;AACzB,MAAM,QAAQ,GAAY,EAAE,CAAA;AAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,cAAc,EAAE,CAAC,EAAE,EAAE,CAAC;IACzC,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,CAAA;AAC5B,CAAC;AAGD;;;;GAIG;AACH,MAAM,IAAI,GAAW,CAAC,MAAc,EAAE,QAAqB,EAAE,EAAE;IAC7D,MAAM,IAAI,GAA6B;QACrC,SAAS,EAAE,CAAC,CAAO,EAAE,GAAY,EAAE,EAAE;YACnC,iEAAiE;YACjE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;gBACd,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAA;gBAChC,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAA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AA;gBACtD,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;gBAC1B,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,GAAG,CAAA;gBACpB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,GAAG,KAAK,CAAA;gBAC3B,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;gBACzB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;YAC7B,CAAC;QACH,CAAC;KACF,CAAA;IAED,sEAAsE;IACtE,uEAAuE;IACvE,iCAAiC;IACjC,MAAM,UAAU,GAAQ;QACtB,IAAI,EAAE,MAAM,CAAC,WAAW,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;QAChE,GAAG,EAAE,IAAI;KACV,CAAA;IACD,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;AAC5B,CAAC,CAAA;AAIQ,oBAAI;AAFb,IAAI,CAAC,QAAQ,GAAG,EAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"fileNames":["../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.es2016.d.ts","../node_modules/typescript/lib/lib.es2017.d.ts","../node_modules/typescript/lib/lib.es2018.d.ts","../node_modules/typescript/lib/lib.es2019.d.ts","../node_modules/typescript/lib/lib.es2020.d.ts","../node_modules/typescript/lib/lib.es2021.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.dom.iterable.d.ts","../node_modules/typescript/lib/lib.dom.asynciterable.d.ts","../node_modules/typescript/lib/lib.webworker.importscripts.d.ts","../node_modules/typescript/lib/lib.scripthost.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../node_modules/typescript/lib/lib.es2016.intl.d.ts","../node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts","../node_modules/typescript/lib/lib.es2017.date.d.ts","../node_modules/typescript/lib/lib.es2017.object.d.ts","../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2017.string.d.ts","../node_modules/typescript/lib/lib.es2017.intl.d.ts","../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../node_modules/typescript/lib/lib.es2018.intl.d.ts","../node_modules/typescript/lib/lib.es2018.promise.d.ts","../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../node_modules/typescript/lib/lib.es2019.array.d.ts","../node_modules/typescript/lib/lib.es2019.object.d.ts","../node_modules/typescript/lib/lib.es2019.string.d.ts","../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../node_modules/typescript/lib/lib.es2019.intl.d.ts","../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../node_modules/typescript/lib/lib.es2020.date.d.ts","../node_modules/typescript/lib/lib.es2020.promise.d.ts","../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2020.string.d.ts","../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2020.intl.d.ts","../node_modules/typescript/lib/lib.es2020.number.d.ts","../node_modules/typescript/lib/lib.es2021.promise.d.ts","../node_modules/typescript/lib/lib.es2021.string.d.ts","../node_modules/typescript/lib/lib.es2021.weakref.d.ts","../node_modules/typescript/lib/lib.es2021.intl.d.ts","../node_modules/typescript/lib/lib.decorators.d.ts","../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../node_modules/typescript/lib/lib.es2021.full.d.ts","../../../parser/ts/dist/rules.d.ts","../../../parser/ts/dist/context.d.ts","../../../parser/ts/dist/lexer.d.ts","../../../parser/ts/dist/parser.d.ts","../../../parser/ts/dist/types.d.ts","../../../parser/ts/dist/utility.d.ts","../../../parser/ts/dist/builtins.d.ts","../../../parser/ts/dist/error.d.ts","../../../parser/ts/dist/tabnas.d.ts","../src/path.ts"],"fileIdsList":[[61],[57,61],[59,60,61,62,63,64],[57,58,59,60,65],[65]],"fileInfos":[{"version":"bcd24271a113971ba9eb71ff8cb01bc6b0f872a85c23fdbe5d93065b375933cd","affectsGlobalScope":true,"impliedFormat":1},{"version":"3f88bedbeb09c6f5a6645cb24c7c55f1aa22d19ae96c8e6959cbd8b85a707bc6","impliedFormat":1},{"version":"7fe93b39b810eadd916be8db880dd7f0f7012a5cc6ffb62de8f62a2117fa6f1f","impliedFormat":1},{"version":"bb0074cc08b84a2374af33d8bf044b80851ccc9e719a5e202eacf40db2c31600","impliedFormat":1},{"version":"1a7daebe4f45fb03d9ec53d60008fbf9ac45a697fdc89e4ce218bc94b94f94d6","impliedFormat":1},{"version":"f94b133a3cb14a288803be545ac2683e0d0ff6661bcd37e31aaaec54fc382aed","impliedFormat":1},{"version":"f59d0650799f8782fd74cf73c19223730c6d1b9198671b1c5b3a38e1188b5953","impliedFormat":1},{"version":"8a15b4607d9a499e2dbeed9ec0d3c0d7372c850b2d5f1fb259e8f6d41d468a84","impliedFormat":1},{"version":"d6b1eba8496bdd0eed6fc8a685768fe01b2da4a0388b5fe7df558290bffcf32f","affectsGlobalScope":true,"impliedFormat":1},{"version":"7f57fc4404ff020bc45b9c620aff2b40f700b95fe31164024c453a5e3c163c54","impliedFormat":1},{"version":"7f57fc4404ff020bc45b9c620aff2b40f700b95fe31164024c453a5e3c163c54","impliedFormat":1},{"version":"2a2de5b9459b3fc44decd9ce6100b72f1b002ef523126c1d3d8b2a4a63d74d78","affectsGlobalScope":true,"impliedFormat":1},{"version":"f13f4b465c99041e912db5c44129a94588e1aafee35a50eab51044833f50b4ee","affectsGlobalScope":true,"impliedFormat":1},{"version":"eadcffda2aa84802c73938e589b9e58248d74c59cb7fcbca6474e3435ac15504","affectsGlobalScope":true,"impliedFormat":1},{"version":"105ba8ff7ba746404fe1a2e189d1d3d2e0eb29a08c18dded791af02f29fb4711","affectsGlobalScope":true,"impliedFormat":1},{"version":"00343ca5b2e3d48fa5df1db6e32ea2a59afab09590274a6cccb1dbae82e60c7c","affectsGlobalScope":true,"impliedFormat":1},{"version":"ebd9f816d4002697cb2864bea1f0b70a103124e18a8cd9645eeccc09bdf80ab4","affectsGlobalScope":true,"impliedFormat":1},{"version":"2c1afac30a01772cd2a9a298a7ce7706b5892e447bb46bdbeef720f7b5da77ad","affectsGlobalScope":true,"impliedFormat":1},{"version":"7b0225f483e4fa685625ebe43dd584bb7973bbd84e66a6ba7bbe175ee1048b4f","affectsGlobalScope":true,"impliedFormat":1},{"version":"c0a4b8ac6ce74679c1da2b3795296f5896e31c38e888469a8e0f99dc3305de60","affectsGlobalScope":true,"impliedFormat":1},{"version":"3084a7b5f569088e0146533a00830e206565de65cae2239509168b11434cd84f","affectsGlobalScope":true,"impliedFormat":1},{"version":"c5079c53f0f141a0698faa903e76cb41cd664e3efb01cc17a5c46ec2eb0bef42","affectsGlobalScope":true,"impliedFormat":1},{"version":"32cafbc484dea6b0ab62cf8473182bbcb23020d70845b406f80b7526f38ae862","affectsGlobalScope":true,"impliedFormat":1},{"version":"fca4cdcb6d6c5ef18a869003d02c9f0fd95df8cfaf6eb431cd3376bc034cad36","affectsGlobalScope":true,"impliedFormat":1},{"version":"b93ec88115de9a9dc1b602291b85baf825c85666bf25985cc5f698073892b467","affectsGlobalScope":true,"impliedFormat":1},{"version":"f5c06dcc3fe849fcb297c247865a161f995cc29de7aa823afdd75aaaddc1419b","affectsGlobalScope":true,"impliedFormat":1},{"version":"b77e16112127a4b169ef0b8c3a4d730edf459c5f25fe52d5e436a6919206c4d7","affectsGlobalScope":true,"impliedFormat":1},{"version":"fbffd9337146eff822c7c00acbb78b01ea7ea23987f6c961eba689349e744f8c","affectsGlobalScope":true,"impliedFormat":1},{"version":"a995c0e49b721312f74fdfb89e4ba29bd9824c770bbb4021d74d2bf560e4c6bd","affectsGlobalScope":true,"impliedFormat":1},{"version":"c7b3542146734342e440a84b213384bfa188835537ddbda50d30766f0593aff9","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce6180fa19b1cccd07ee7f7dbb9a367ac19c0ed160573e4686425060b6df7f57","affectsGlobalScope":true,"impliedFormat":1},{"version":"3f02e2476bccb9dbe21280d6090f0df17d2f66b74711489415a8aa4df73c9675","affectsGlobalScope":true,"impliedFormat":1},{"version":"45e3ab34c1c013c8ab2dc1ba4c80c780744b13b5676800ae2e3be27ae862c40c","affectsGlobalScope":true,"impliedFormat":1},{"version":"805c86f6cca8d7702a62a844856dbaa2a3fd2abef0536e65d48732441dde5b5b","affectsGlobalScope":true,"impliedFormat":1},{"version":"e42e397f1a5a77994f0185fd1466520691456c772d06bf843e5084ceb879a0ad","affectsGlobalScope":true,"impliedFormat":1},{"version":"f4c2b41f90c95b1c532ecc874bd3c111865793b23aebcc1c3cbbabcd5d76ffb0","affectsGlobalScope":true,"impliedFormat":1},{"version":"ab26191cfad5b66afa11b8bf935ef1cd88fabfcb28d30b2dfa6fad877d050332","affectsGlobalScope":true,"impliedFormat":1},{"version":"2088bc26531e38fb05eedac2951480db5309f6be3fa4a08d2221abb0f5b4200d","affectsGlobalScope":true,"impliedFormat":1},{"version":"cb9d366c425fea79716a8fb3af0d78e6b22ebbab3bd64d25063b42dc9f531c1e","affectsGlobalScope":true,"impliedFormat":1},{"version":"500934a8089c26d57ebdb688fc9757389bb6207a3c8f0674d68efa900d2abb34","affectsGlobalScope":true,"impliedFormat":1},{"version":"689da16f46e647cef0d64b0def88910e818a5877ca5379ede156ca3afb780ac3","affectsGlobalScope":true,"impliedFormat":1},{"version":"bc21cc8b6fee4f4c2440d08035b7ea3c06b3511314c8bab6bef7a92de58a2593","affectsGlobalScope":true,"impliedFormat":1},{"version":"7ca53d13d2957003abb47922a71866ba7cb2068f8d154877c596d63c359fed25","affectsGlobalScope":true,"impliedFormat":1},{"version":"54725f8c4df3d900cb4dac84b64689ce29548da0b4e9b7c2de61d41c79293611","affectsGlobalScope":true,"impliedFormat":1},{"version":"e5594bc3076ac29e6c1ebda77939bc4c8833de72f654b6e376862c0473199323","affectsGlobalScope":true,"impliedFormat":1},{"version":"2f3eb332c2d73e729f3364fcc0c2b375e72a121e8157d25a82d67a138c83a95c","affectsGlobalScope":true,"impliedFormat":1},{"version":"6f4427f9642ce8d500970e4e69d1397f64072ab73b97e476b4002a646ac743b1","affectsGlobalScope":true,"impliedFormat":1},{"version":"48915f327cd1dea4d7bd358d9dc7732f58f9e1626a29cc0c05c8c692419d9bb7","affectsGlobalScope":true,"impliedFormat":1},{"version":"b7bf9377723203b5a6a4b920164df22d56a43f593269ba6ae1fdc97774b68855","affectsGlobalScope":true,"impliedFormat":1},{"version":"db9709688f82c9e5f65a119c64d835f906efe5f559d08b11642d56eb85b79357","affectsGlobalScope":true,"impliedFormat":1},{"version":"4b25b8c874acd1a4cf8444c3617e037d444d19080ac9f634b405583fd10ce1f7","affectsGlobalScope":true,"impliedFormat":1},{"version":"37be57d7c90cf1f8112ee2636a068d8fd181289f82b744160ec56a7dc158a9f5","affectsGlobalScope":true,"impliedFormat":1},{"version":"a917a49ac94cd26b754ab84e113369a75d1a47a710661d7cd25e961cc797065f","affectsGlobalScope":true,"impliedFormat":1},{"version":"1ce14b81c5cc821994aa8ec1d42b220dd41b27fcc06373bce3958af7421b77d4","affectsGlobalScope":true,"impliedFormat":1},{"version":"b3a048b3e9302ef9a34ef4ebb9aecfb28b66abb3bce577206a79fee559c230da","affectsGlobalScope":true,"impliedFormat":1},{"version":"e4a70ead7fea07d9abe520cc9e30c04ac035ebceaab28f9e67211610006c65de","impliedFormat":1},{"version":"67e9f72e67c3bf4638813df1278573f055061b4f2a458f97944f15409e75bed3","impliedFormat":1},{"version":"45ddbd362e38b738eeacedfd6d690d85dae0b372c78a6b1f1d84c6cbbb3b291f","impliedFormat":1},{"version":"3596eac004a06c8fb744031cfe0f8cb1c12bc4b7344a316ed28d57657af2d325","impliedFormat":1},{"version":"af70bf78d3d9ed1ff755fbcf4798d924015e7ca1bc4cc7395089b987498e525e","impliedFormat":1},{"version":"14879c1ef48367ab5e3a2672e023c30a9d9f74315900c9bd0c5289901b3dd6ae","impliedFormat":1},{"version":"1174642fa59df7d2f4c48bc16d9b8e34a2e2ae30721bfc419ea8ce3508a41a3d","impliedFormat":1},{"version":"3e7637c55c6d4833a095795bdbe799fed81e7779f97c7f48901e7aca7c6ec2af","impliedFormat":1},{"version":"483bbba125454b8cedd46bb04d2648a8ce420ed360cfea1d2dcd77ab7134ff59","impliedFormat":1},{"version":"c6633319639dedc0c02b88dbd636bd71f9edfa7964e50d9f35433b54a7c56464","impliedFormat":1},{"version":"e28752ce39ff6179f0c8e415ffdd15300e966f4563c5b885479c07c405918e5e","signature":"08cf0d5c8944d3715d0e20dcb6dfbadfad0a72306fb01e540cfab2ccef15897a","impliedFormat":1}],"root":[66],"options":{"composite":true,"declaration":true,"esModuleInterop":true,"module":199,"noEmitOnError":true,"outDir":"./","rootDir":"../src","sourceMap":true,"strict":true,"target":8},"referencedMap":[[63,1],[58,1],[64,1],[59,1],[60,2],[57,1],[65,3],[61,4],[62,1],[66,5]],"latestChangedDtsFile":"./path.d.ts","version":"6.0.3"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tabnas/path",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "This plugin allows the Tabnas parser to determine the path to values.",
|
|
5
|
+
"main": "dist/path.js",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"types": "dist/path.d.ts",
|
|
8
|
+
"homepage": "https://github.com/tabnas/path",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"pattern",
|
|
11
|
+
"matcher",
|
|
12
|
+
"object",
|
|
13
|
+
"property",
|
|
14
|
+
"json"
|
|
15
|
+
],
|
|
16
|
+
"author": "Richard Rodger (http://richardrodger.com)",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git://github.com/tabnas/path.git"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "node --enable-source-maps --test \"dist-test/*.test.js\"",
|
|
23
|
+
"test-some": "node --enable-source-maps --test --test-name-pattern",
|
|
24
|
+
"watch": "tsc --build src test -w",
|
|
25
|
+
"build": "tsc --build src test",
|
|
26
|
+
"clean": "rm -rf dist dist-test node_modules yarn.lock package-lock.json",
|
|
27
|
+
"reset": "npm run clean && npm i && npm run build && npm test",
|
|
28
|
+
"repo-tag": "REPO_VERSION=`node -e \"console.log(require('./package').version)\"` && echo TAG: v$REPO_VERSION && git commit -a -m v$REPO_VERSION && git push && git tag v$REPO_VERSION && git push --tags;",
|
|
29
|
+
"repo-publish": "npm run clean && npm i && npm run repo-publish-quick",
|
|
30
|
+
"repo-publish-quick": "npm run build && npm run test && npm run repo-tag && npm publish --access public --registry https://registry.npmjs.org "
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"files": [
|
|
34
|
+
"src/",
|
|
35
|
+
"dist/",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@tabnas/debug": "file:../../debug/ts",
|
|
40
|
+
"@tabnas/parser": "file:../../parser/ts",
|
|
41
|
+
"@tabnas/railroad": "file:../../railroad/ts",
|
|
42
|
+
"@types/node": "25.6.0",
|
|
43
|
+
"typescript": "6.0.3"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@tabnas/parser": "^0.2.0"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=24"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/path.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/* Copyright (c) 2022-2026 Richard Rodger, MIT License */
|
|
2
|
+
|
|
3
|
+
import { Tabnas, Plugin, Rule, Context } from '@tabnas/parser'
|
|
4
|
+
|
|
5
|
+
type PathOptions = {}
|
|
6
|
+
|
|
7
|
+
// The host-grammar rules the plugin hooks. The Tabnas engine ships no
|
|
8
|
+
// grammar of its own, so these names must match the rules supplied by
|
|
9
|
+
// whatever grammar plugin the consumer installs. The standard
|
|
10
|
+
// value/map/pair/list/elem rule set uses these names. Each empty rule
|
|
11
|
+
// entry causes tabnas.grammar() to auto-wire any matching
|
|
12
|
+
// @<rulename>-<phase> function refs as state actions.
|
|
13
|
+
const HOOKED_RULES = ['val', 'map', 'pair', 'list', 'elem']
|
|
14
|
+
|
|
15
|
+
// Preallocated array pool for path tracking. Each depth level gets
|
|
16
|
+
// a reusable array of that length. The arrays are mutated in place
|
|
17
|
+
// as the parser traverses the tree — no allocation per pair/elem.
|
|
18
|
+
//
|
|
19
|
+
// IMPORTANT: r.k.path is a mutable, shared array. Client code that
|
|
20
|
+
// needs to retain the path beyond the current parse callback MUST
|
|
21
|
+
// copy it (e.g. r.k.path.slice() or [...r.k.path]).
|
|
22
|
+
const MAX_PATH_DEPTH = 64
|
|
23
|
+
const pathPool: any[][] = []
|
|
24
|
+
for (let i = 0; i <= MAX_PATH_DEPTH; i++) {
|
|
25
|
+
pathPool[i] = new Array(i)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/* Keeps track of the property path to the current location.
|
|
30
|
+
* Example: {a:{b:1 ## path=["a","b"]
|
|
31
|
+
* Use the Rule.k key-value store so that the path is propagated to children and followers.
|
|
32
|
+
* Depth must be greater than 0 - ensures path only starts once top level implicit is set up.
|
|
33
|
+
*/
|
|
34
|
+
const Path: Plugin = (tabnas: Tabnas, _options: PathOptions) => {
|
|
35
|
+
const refs: Record<string, Function> = {
|
|
36
|
+
'@val-bo': (r: Rule, ctx: Context) => {
|
|
37
|
+
// At top level, create path array, or inherit from meta context.
|
|
38
|
+
if (0 === r.d) {
|
|
39
|
+
const base = ctx.meta.path?.base
|
|
40
|
+
if (base && base.length > 0) {
|
|
41
|
+
const arr = pathPool[base.length] || new Array(base.length)
|
|
42
|
+
for (let i = 0; i < base.length; i++) arr[i] = base[i]
|
|
43
|
+
r.k.path = arr
|
|
44
|
+
r.k.pathDepth = base.length
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
r.k.path = pathPool[0]
|
|
48
|
+
r.k.pathDepth = 0
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
'@map-bo': (r: Rule) => {
|
|
54
|
+
// Not in an array, so no need to track element index.
|
|
55
|
+
r.k.index = undefined
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
'@pair-ao': (r: Rule) => {
|
|
59
|
+
if (0 < r.d && r.u.pair) {
|
|
60
|
+
const depth = (r.k.pathDepth || 0) + 1
|
|
61
|
+
const arr = pathPool[depth] || (pathPool[depth] = new Array(depth))
|
|
62
|
+
const parent = r.k.path
|
|
63
|
+
const parentLen = depth - 1
|
|
64
|
+
for (let i = 0; i < parentLen; i++) arr[i] = parent[i]
|
|
65
|
+
arr[parentLen] = r.u.key
|
|
66
|
+
r.child.k.path = arr
|
|
67
|
+
r.child.k.pathDepth = depth
|
|
68
|
+
r.child.k.key = r.u.key
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
'@list-bo': (r: Rule) => {
|
|
73
|
+
// In array, the path property is the element index.
|
|
74
|
+
r.k.index = -1
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
'@elem-ao': (r: Rule) => {
|
|
78
|
+
if (0 < r.d) {
|
|
79
|
+
r.k.index = 1 + r.k.index
|
|
80
|
+
const depth = (r.k.pathDepth || 0) + 1
|
|
81
|
+
const arr = pathPool[depth] || (pathPool[depth] = new Array(depth))
|
|
82
|
+
const parent = r.k.path
|
|
83
|
+
const parentLen = depth - 1
|
|
84
|
+
for (let i = 0; i < parentLen; i++) arr[i] = parent[i]
|
|
85
|
+
arr[parentLen] = r.k.index
|
|
86
|
+
r.child.k.path = arr
|
|
87
|
+
r.child.k.pathDepth = depth
|
|
88
|
+
r.child.k.key = r.k.index
|
|
89
|
+
r.child.k.index = r.k.index
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Declare the rules to hook and attach the refs. The empty rule specs
|
|
95
|
+
// leave the host grammar's rules intact; only the @<rule>-<phase> refs
|
|
96
|
+
// are wired in as state actions.
|
|
97
|
+
const grammarDef: any = {
|
|
98
|
+
rule: Object.fromEntries(HOOKED_RULES.map((name) => [name, {}])),
|
|
99
|
+
ref: refs,
|
|
100
|
+
}
|
|
101
|
+
tabnas.grammar(grammarDef)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
Path.defaults = {} as PathOptions
|
|
105
|
+
|
|
106
|
+
export { Path }
|
|
107
|
+
|
|
108
|
+
export type { PathOptions }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"esModuleInterop": true,
|
|
4
|
+
"module": "nodenext",
|
|
5
|
+
"noEmitOnError": true,
|
|
6
|
+
"outDir": "../dist",
|
|
7
|
+
"rootDir": ".",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"resolveJsonModule": true,
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"target": "ES2021",
|
|
13
|
+
"composite": true
|
|
14
|
+
}
|
|
15
|
+
}
|