@tabnas/directive 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 +60 -0
- package/dist/directive.d.ts +23 -0
- package/dist/directive.js +163 -0
- package/dist/directive.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +51 -0
- package/src/directive.ts +222 -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,60 @@
|
|
|
1
|
+
# @tabnas/directive — directive syntax for the tabnas parser
|
|
2
|
+
|
|
3
|
+
Adds directive syntax to the [tabnas](https://github.com/tabnas/parser)
|
|
4
|
+
parser. A directive is a token sequence (e.g. `@name`, `add<1,2>`) that
|
|
5
|
+
triggers custom parsing behaviour. It is a plugin for the tabnas engine —
|
|
6
|
+
its only dependency — and layers onto whatever host grammar provides the
|
|
7
|
+
standard `val` / `list` / `map` / `pair` rules. TypeScript (canonical)
|
|
8
|
+
and Go ports share the same API shape and test specs.
|
|
9
|
+
|
|
10
|
+
[](https://npmjs.com/package/@tabnas/directive)
|
|
11
|
+
[](https://github.com/tabnas/directive/actions/workflows/build.yml)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Documentation
|
|
15
|
+
|
|
16
|
+
The four-quadrant TypeScript docs live in [`doc/`](doc):
|
|
17
|
+
[tutorial](doc/tutorial.md) · [how-to guide](doc/guide.md) ·
|
|
18
|
+
[reference](doc/reference.md) · [concepts](doc/concepts.md). The Go-port
|
|
19
|
+
docs are in [`../go/doc/`](../go/doc).
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Quickstart
|
|
23
|
+
|
|
24
|
+
The directive modifies host-grammar rules, so apply it to a `Tabnas`
|
|
25
|
+
instance that already has a grammar that provides `val` / `list` / `map`
|
|
26
|
+
/ `pair`. Here the host is [`@tabnas/json`](https://github.com/tabnas/json),
|
|
27
|
+
which is why the input uses JSON syntax (quoted strings):
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const { Tabnas } = require('@tabnas/parser')
|
|
31
|
+
const { json } = require('@tabnas/json')
|
|
32
|
+
const { Directive } = require('@tabnas/directive')
|
|
33
|
+
|
|
34
|
+
const j = new Tabnas({ plugins: [json] }).use(Directive, {
|
|
35
|
+
name: 'upper',
|
|
36
|
+
open: '@',
|
|
37
|
+
action: (rule) => (rule.node = String(rule.child.node).toUpperCase()),
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
j.parse('@"x"') // => 'X'
|
|
41
|
+
j.parse('[@"a", @"b", 1]') // => ['A', 'B', 1]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
A minimal host grammar (used by the tests) lives in
|
|
45
|
+
[`test/mini-grammar.ts`](test/mini-grammar.ts); see the
|
|
46
|
+
[tutorial](doc/tutorial.md) for a step-by-step walkthrough.
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
## Build and test
|
|
50
|
+
|
|
51
|
+
The `tabnas` engine is the only dependency and is consumed from source.
|
|
52
|
+
From the repository root, `make build` / `make test` fetch it into
|
|
53
|
+
`vendor/` and build/test both implementations. The tests bring their own
|
|
54
|
+
small grammar. See the [root README](../README.md) and
|
|
55
|
+
[`../AGENTS.md`](../AGENTS.md).
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Tabnas, StateAction, Plugin, Tin } from '@tabnas/parser';
|
|
2
|
+
type DirectiveOptions = {
|
|
3
|
+
name: string;
|
|
4
|
+
open: string;
|
|
5
|
+
action: StateAction | string;
|
|
6
|
+
close?: string;
|
|
7
|
+
rules?: {
|
|
8
|
+
open?: string | string[] | Record<string, {
|
|
9
|
+
c?: Function;
|
|
10
|
+
}>;
|
|
11
|
+
close?: string | string[] | Record<string, {
|
|
12
|
+
c?: Function;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
15
|
+
custom?: (tabnas: Tabnas, config: {
|
|
16
|
+
OPEN: Tin;
|
|
17
|
+
CLOSE: Tin | null | undefined;
|
|
18
|
+
name: string;
|
|
19
|
+
}) => void;
|
|
20
|
+
};
|
|
21
|
+
declare const Directive: Plugin;
|
|
22
|
+
export { Directive };
|
|
23
|
+
export type { DirectiveOptions };
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2021-2022 Richard Rodger, MIT License */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Directive = void 0;
|
|
5
|
+
const resolveRules = (rules) => {
|
|
6
|
+
const rulemap = {};
|
|
7
|
+
if ('string' == typeof rules) {
|
|
8
|
+
rules = rules.split(/\s*,\s*/);
|
|
9
|
+
}
|
|
10
|
+
if (Array.isArray(rules)) {
|
|
11
|
+
for (const n of rules) {
|
|
12
|
+
if (null != n && '' !== n)
|
|
13
|
+
rulemap[n] = {};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else if (null != rules) {
|
|
17
|
+
for (const k of Object.keys(rules)) {
|
|
18
|
+
if (null != rules[k])
|
|
19
|
+
rulemap[k] = rules[k];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return rulemap;
|
|
23
|
+
};
|
|
24
|
+
const Directive = (tabnas, options) => {
|
|
25
|
+
let rules = {
|
|
26
|
+
open: resolveRules(options?.rules?.open),
|
|
27
|
+
close: resolveRules(options?.rules?.close),
|
|
28
|
+
};
|
|
29
|
+
let name = options.name;
|
|
30
|
+
let open = options.open;
|
|
31
|
+
let close = options.close;
|
|
32
|
+
let action;
|
|
33
|
+
let custom = options.custom;
|
|
34
|
+
if ('string' === typeof options.action) {
|
|
35
|
+
let path = options.action;
|
|
36
|
+
action = (rule) => (rule.node = tabnas.util.prop(tabnas.options, path));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
action = options.action;
|
|
40
|
+
}
|
|
41
|
+
let token = {};
|
|
42
|
+
let openTN = '#OD_' + name;
|
|
43
|
+
let closeTN = '#CD_' + name;
|
|
44
|
+
let OPEN = tabnas.fixed(open);
|
|
45
|
+
let CLOSE = null == close ? null : tabnas.fixed(close);
|
|
46
|
+
// OPEN must be unique
|
|
47
|
+
if (null != OPEN) {
|
|
48
|
+
throw new Error('Directive open token already in use: ' + open);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
token[openTN] = open;
|
|
52
|
+
}
|
|
53
|
+
// Only create CLOSE if not already defined as a fixed token
|
|
54
|
+
if (null == CLOSE && null != close) {
|
|
55
|
+
token[closeTN] = close;
|
|
56
|
+
}
|
|
57
|
+
tabnas.options({
|
|
58
|
+
fixed: {
|
|
59
|
+
token,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
let CA = tabnas.token.CA;
|
|
63
|
+
OPEN = tabnas.fixed(open);
|
|
64
|
+
CLOSE = null == close ? null : tabnas.fixed(close);
|
|
65
|
+
// NOTE: RuleSpec.open|close refers to Rule state, whereas
|
|
66
|
+
// OPEN|CLOSE refers to opening and closing tokens for the directive.
|
|
67
|
+
// Pre-seed the directive rule's hooks (clear existing alts, set bo/bc).
|
|
68
|
+
// `grammar()` below will then install the open/close alts with the
|
|
69
|
+
// `directive` group tag appended via the setting arg.
|
|
70
|
+
tabnas.rule(name, (rs) => rs
|
|
71
|
+
.clear()
|
|
72
|
+
.bo((rule) => ((rule.node = {}), undefined))
|
|
73
|
+
.bc(function (rule, ctx, next, tkn) {
|
|
74
|
+
let out = action.call(this, rule, ctx, next, tkn);
|
|
75
|
+
if (out?.isToken) {
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
}));
|
|
79
|
+
// Build a declarative grammar spec covering every rule modification
|
|
80
|
+
// plus the directive rule's own alts.
|
|
81
|
+
const grammarSpec = { rule: {} };
|
|
82
|
+
const ruleFor = (rn) => (grammarSpec.rule[rn] = grammarSpec.rule[rn] || {});
|
|
83
|
+
Object.entries(rules.open).forEach((entry) => {
|
|
84
|
+
const [rulename, rulespec] = entry;
|
|
85
|
+
const openAlts = [];
|
|
86
|
+
const closeAlts = [];
|
|
87
|
+
if (null != close) {
|
|
88
|
+
// More-specific OPEN+CLOSE alt first so it's tried before OPEN alone.
|
|
89
|
+
openAlts.push({
|
|
90
|
+
s: [OPEN, CLOSE],
|
|
91
|
+
b: 1,
|
|
92
|
+
p: name,
|
|
93
|
+
n: { ['dr_' + name]: 1 },
|
|
94
|
+
g: 'start,end',
|
|
95
|
+
});
|
|
96
|
+
closeAlts.push({
|
|
97
|
+
s: [CLOSE],
|
|
98
|
+
b: 1,
|
|
99
|
+
g: 'end',
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
openAlts.push({
|
|
103
|
+
s: [OPEN],
|
|
104
|
+
p: name,
|
|
105
|
+
n: { ['dr_' + name]: 1 },
|
|
106
|
+
g: 'start',
|
|
107
|
+
c: rulespec.c,
|
|
108
|
+
});
|
|
109
|
+
const r = ruleFor(rulename);
|
|
110
|
+
r.open = openAlts;
|
|
111
|
+
if (closeAlts.length)
|
|
112
|
+
r.close = closeAlts;
|
|
113
|
+
});
|
|
114
|
+
if (null != close) {
|
|
115
|
+
Object.entries(rules.close).forEach((entry) => {
|
|
116
|
+
const [rulename, rulespec] = entry;
|
|
117
|
+
const r = ruleFor(rulename);
|
|
118
|
+
r.close = [
|
|
119
|
+
{
|
|
120
|
+
s: [CLOSE],
|
|
121
|
+
c: (r, ctx) => 1 === r.n['dr_' + name] &&
|
|
122
|
+
(rulespec.c ? rulespec.c(r, ctx) : true),
|
|
123
|
+
b: 1,
|
|
124
|
+
g: 'end',
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
s: [CA, CLOSE],
|
|
128
|
+
c: (r) => 1 === r.n['dr_' + name],
|
|
129
|
+
b: 1,
|
|
130
|
+
g: 'end,comma',
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const directiveOpenAlts = [];
|
|
136
|
+
if (null != close) {
|
|
137
|
+
directiveOpenAlts.push({ s: [CLOSE], b: 1 });
|
|
138
|
+
}
|
|
139
|
+
directiveOpenAlts.push({
|
|
140
|
+
p: 'val',
|
|
141
|
+
// Only accept implicits when there is a CLOSE token,
|
|
142
|
+
// otherwise we'll eat all following siblings.
|
|
143
|
+
n: null == close ? { dlist: 1, dmap: 1 } : { dlist: 0, dmap: 0 },
|
|
144
|
+
});
|
|
145
|
+
const dr = ruleFor(name);
|
|
146
|
+
dr.open = directiveOpenAlts;
|
|
147
|
+
if (null != close) {
|
|
148
|
+
dr.close = [{ s: [CLOSE] }, { s: [CA, CLOSE] }];
|
|
149
|
+
}
|
|
150
|
+
tabnas.grammar(grammarSpec, { rule: { alt: { g: 'directive' } } });
|
|
151
|
+
if (custom) {
|
|
152
|
+
custom(tabnas, { OPEN, CLOSE, name });
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
exports.Directive = Directive;
|
|
156
|
+
Directive.defaults = {
|
|
157
|
+
rules: {
|
|
158
|
+
// By default, directives only operate where vals occur.
|
|
159
|
+
open: 'val',
|
|
160
|
+
close: 'list,elem,map,pair',
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
//# sourceMappingURL=directive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directive.js","sourceRoot":"","sources":["../src/directive.ts"],"names":[],"mappings":";AAAA,yDAAyD;;;AA8BzD,MAAM,YAAY,GAAG,CACnB,KAAuE,EACvE,EAAE;IACF,MAAM,OAAO,GAAwB,EAAE,CAAA;IAEvC,IAAI,QAAQ,IAAI,OAAO,KAAK,EAAE,CAAC;QAC7B,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAChC,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACtB,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;gBAAE,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,CAAA;QAC5C,CAAC;IACH,CAAC;SACI,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;QACvB,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,IAAI,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC;gBAAE,OAAO,CAAC,CAAC,CAAC,GAAG,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AC;IACD,iBAAiB,CAAC,IAAI,CAAC;QACrB,CAAC,EAAE,KAAK;QACR,qDAAqD;QACrD,8CAA8C;QAC9C,CAAC,EAAE,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE;KACjE,CAAC,CAAA;IACF,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACxB,EAAE,CAAC,IAAI,GAAG,iBAAiB,CAAA;IAC3B,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;QAClB,EAAE,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,CAAC,CAAA;IACjD,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC,CAAA;IAElE,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;IACvC,CAAC;AACH,CAAC,CAAA;AAUQ,8BAAS;AARlB,SAAS,CAAC,QAAQ,GAAG;IACnB,KAAK,EAAE;QACL,wDAAwD;QACxD,IAAI,EAAE,KAAK;QACX,KAAK,EAAE,oBAAoB;KAC5B;CACkB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/directive.ts"],"version":"6.0.3"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tabnas/directive",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Directive syntax plugin for the tabnas JSON parser.",
|
|
5
|
+
"main": "dist/directive.js",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"types": "dist/directive.d.ts",
|
|
8
|
+
"homepage": "https://github.com/tabnas/directive",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"tabnas",
|
|
11
|
+
"parser",
|
|
12
|
+
"directive",
|
|
13
|
+
"plugin"
|
|
14
|
+
],
|
|
15
|
+
"author": "Richard Rodger (http://richardrodger.com)",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git://github.com/tabnas/directive.git",
|
|
19
|
+
"directory": "ts"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "node --enable-source-maps --test \"dist-test/*.test.js\"",
|
|
23
|
+
"test-some": "node --enable-source-maps --test-name-pattern=\"$npm_config_pattern\" --test \"dist-test/*.test.js\"",
|
|
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/directive.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/* Copyright (c) 2021-2022 Richard Rodger, MIT License */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Tabnas,
|
|
5
|
+
Rule,
|
|
6
|
+
RuleSpec,
|
|
7
|
+
StateAction,
|
|
8
|
+
Plugin,
|
|
9
|
+
Context,
|
|
10
|
+
Token,
|
|
11
|
+
Tin,
|
|
12
|
+
} from '@tabnas/parser'
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
type DirectiveOptions = {
|
|
16
|
+
name: string
|
|
17
|
+
open: string
|
|
18
|
+
action: StateAction | string
|
|
19
|
+
close?: string
|
|
20
|
+
rules?: {
|
|
21
|
+
open?: string | string[] | Record<string, { c?: Function }>
|
|
22
|
+
close?: string | string[] | Record<string, { c?: Function }>
|
|
23
|
+
}
|
|
24
|
+
custom?: (
|
|
25
|
+
tabnas: Tabnas,
|
|
26
|
+
config: { OPEN: Tin; CLOSE: Tin | null | undefined; name: string },
|
|
27
|
+
) => void
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
const resolveRules = (
|
|
32
|
+
rules: undefined | string | string[] | Record<string, { c?: Function }>
|
|
33
|
+
) => {
|
|
34
|
+
const rulemap: Record<string, any> = {}
|
|
35
|
+
|
|
36
|
+
if ('string' == typeof rules) {
|
|
37
|
+
rules = rules.split(/\s*,\s*/)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (Array.isArray(rules)) {
|
|
41
|
+
for (const n of rules) {
|
|
42
|
+
if (null != n && '' !== n) rulemap[n] = {}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (null != rules) {
|
|
46
|
+
for (const k of Object.keys(rules)) {
|
|
47
|
+
if (null != rules[k]) rulemap[k] = rules[k]
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return rulemap
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
const Directive: Plugin = (tabnas: Tabnas, options: DirectiveOptions) => {
|
|
56
|
+
let rules = {
|
|
57
|
+
open: resolveRules(options?.rules?.open),
|
|
58
|
+
close: resolveRules(options?.rules?.close),
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let name = options.name
|
|
62
|
+
let open = options.open
|
|
63
|
+
let close = options.close
|
|
64
|
+
let action: StateAction
|
|
65
|
+
let custom = options.custom
|
|
66
|
+
|
|
67
|
+
if ('string' === typeof options.action) {
|
|
68
|
+
let path = options.action
|
|
69
|
+
action = (rule: Rule) =>
|
|
70
|
+
(rule.node = tabnas.util.prop(tabnas.options, path))
|
|
71
|
+
} else {
|
|
72
|
+
action = options.action
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let token: Record<string, string> = {}
|
|
76
|
+
|
|
77
|
+
let openTN = '#OD_' + name
|
|
78
|
+
let closeTN = '#CD_' + name
|
|
79
|
+
|
|
80
|
+
let OPEN: Tin = tabnas.fixed(open) as Tin
|
|
81
|
+
let CLOSE = null == close ? null : tabnas.fixed(close)
|
|
82
|
+
|
|
83
|
+
// OPEN must be unique
|
|
84
|
+
if (null != OPEN) {
|
|
85
|
+
throw new Error('Directive open token already in use: ' + open)
|
|
86
|
+
} else {
|
|
87
|
+
token[openTN] = open
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Only create CLOSE if not already defined as a fixed token
|
|
91
|
+
if (null == CLOSE && null != close) {
|
|
92
|
+
token[closeTN] = close
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
tabnas.options({
|
|
96
|
+
fixed: {
|
|
97
|
+
token,
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
let CA = tabnas.token.CA
|
|
102
|
+
OPEN = tabnas.fixed(open) as Tin
|
|
103
|
+
CLOSE = null == close ? null : tabnas.fixed(close)
|
|
104
|
+
|
|
105
|
+
// NOTE: RuleSpec.open|close refers to Rule state, whereas
|
|
106
|
+
// OPEN|CLOSE refers to opening and closing tokens for the directive.
|
|
107
|
+
|
|
108
|
+
// Pre-seed the directive rule's hooks (clear existing alts, set bo/bc).
|
|
109
|
+
// `grammar()` below will then install the open/close alts with the
|
|
110
|
+
// `directive` group tag appended via the setting arg.
|
|
111
|
+
tabnas.rule(name, (rs) =>
|
|
112
|
+
rs
|
|
113
|
+
.clear()
|
|
114
|
+
.bo((rule: Rule) => ((rule.node = {}), undefined))
|
|
115
|
+
.bc(function(
|
|
116
|
+
this: RuleSpec,
|
|
117
|
+
rule: Rule,
|
|
118
|
+
ctx: Context,
|
|
119
|
+
next: Rule,
|
|
120
|
+
tkn?: Token | void,
|
|
121
|
+
) {
|
|
122
|
+
let out = action.call(this, rule, ctx, next, tkn)
|
|
123
|
+
if (out?.isToken) {
|
|
124
|
+
return out
|
|
125
|
+
}
|
|
126
|
+
}),
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
// Build a declarative grammar spec covering every rule modification
|
|
130
|
+
// plus the directive rule's own alts.
|
|
131
|
+
const grammarSpec: any = { rule: {} }
|
|
132
|
+
const ruleFor = (rn: string) =>
|
|
133
|
+
(grammarSpec.rule[rn] = grammarSpec.rule[rn] || {})
|
|
134
|
+
|
|
135
|
+
Object.entries(rules.open).forEach((entry: any[]) => {
|
|
136
|
+
const [rulename, rulespec] = entry
|
|
137
|
+
const openAlts: any[] = []
|
|
138
|
+
const closeAlts: any[] = []
|
|
139
|
+
if (null != close) {
|
|
140
|
+
// More-specific OPEN+CLOSE alt first so it's tried before OPEN alone.
|
|
141
|
+
openAlts.push({
|
|
142
|
+
s: [OPEN, CLOSE],
|
|
143
|
+
b: 1,
|
|
144
|
+
p: name,
|
|
145
|
+
n: { ['dr_' + name]: 1 },
|
|
146
|
+
g: 'start,end',
|
|
147
|
+
})
|
|
148
|
+
closeAlts.push({
|
|
149
|
+
s: [CLOSE],
|
|
150
|
+
b: 1,
|
|
151
|
+
g: 'end',
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
openAlts.push({
|
|
155
|
+
s: [OPEN],
|
|
156
|
+
p: name,
|
|
157
|
+
n: { ['dr_' + name]: 1 },
|
|
158
|
+
g: 'start',
|
|
159
|
+
c: rulespec.c,
|
|
160
|
+
})
|
|
161
|
+
const r = ruleFor(rulename)
|
|
162
|
+
r.open = openAlts
|
|
163
|
+
if (closeAlts.length) r.close = closeAlts
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
if (null != close) {
|
|
167
|
+
Object.entries(rules.close).forEach((entry: any[]) => {
|
|
168
|
+
const [rulename, rulespec] = entry
|
|
169
|
+
const r = ruleFor(rulename)
|
|
170
|
+
r.close = [
|
|
171
|
+
{
|
|
172
|
+
s: [CLOSE],
|
|
173
|
+
c: (r: Rule, ctx: Context) =>
|
|
174
|
+
1 === r.n['dr_' + name] &&
|
|
175
|
+
(rulespec.c ? rulespec.c(r, ctx) : true),
|
|
176
|
+
b: 1,
|
|
177
|
+
g: 'end',
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
s: [CA, CLOSE],
|
|
181
|
+
c: (r: Rule) => 1 === r.n['dr_' + name],
|
|
182
|
+
b: 1,
|
|
183
|
+
g: 'end,comma',
|
|
184
|
+
},
|
|
185
|
+
]
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const directiveOpenAlts: any[] = []
|
|
190
|
+
if (null != close) {
|
|
191
|
+
directiveOpenAlts.push({ s: [CLOSE], b: 1 })
|
|
192
|
+
}
|
|
193
|
+
directiveOpenAlts.push({
|
|
194
|
+
p: 'val',
|
|
195
|
+
// Only accept implicits when there is a CLOSE token,
|
|
196
|
+
// otherwise we'll eat all following siblings.
|
|
197
|
+
n: null == close ? { dlist: 1, dmap: 1 } : { dlist: 0, dmap: 0 },
|
|
198
|
+
})
|
|
199
|
+
const dr = ruleFor(name)
|
|
200
|
+
dr.open = directiveOpenAlts
|
|
201
|
+
if (null != close) {
|
|
202
|
+
dr.close = [{ s: [CLOSE] }, { s: [CA, CLOSE] }]
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
tabnas.grammar(grammarSpec, { rule: { alt: { g: 'directive' } } })
|
|
206
|
+
|
|
207
|
+
if (custom) {
|
|
208
|
+
custom(tabnas, { OPEN, CLOSE, name })
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
Directive.defaults = {
|
|
213
|
+
rules: {
|
|
214
|
+
// By default, directives only operate where vals occur.
|
|
215
|
+
open: 'val',
|
|
216
|
+
close: 'list,elem,map,pair',
|
|
217
|
+
},
|
|
218
|
+
} as DirectiveOptions
|
|
219
|
+
|
|
220
|
+
export { Directive }
|
|
221
|
+
|
|
222
|
+
export type { DirectiveOptions }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"esModuleInterop": true,
|
|
4
|
+
"module": "nodenext",
|
|
5
|
+
"noEmitOnError": true,
|
|
6
|
+
"outDir":"../dist",
|
|
7
|
+
"rootDir":".",
|
|
8
|
+
"resolveJsonModule": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"target": "es2021",
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"declarationDir": "../dist"
|
|
14
|
+
}
|
|
15
|
+
}
|