@willbooster/tree-sitter-xml 0.0.0-stage → 1.0.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 +19 -0
- package/README.md +149 -2
- package/common/common.mjs +375 -0
- package/dtd/grammar.js +72 -0
- package/dtd/package.json +4 -0
- package/dtd/src/node-types.json +789 -0
- package/package.json +78 -4
- package/queries/dtd/highlights.scm +129 -0
- package/queries/xml/highlights.scm +168 -0
- package/tree-sitter-dtd.wasm +0 -0
- package/tree-sitter-xml.wasm +0 -0
- package/tree-sitter.json +61 -0
- package/xml/grammar.js +196 -0
- package/xml/package.json +4 -0
- package/xml/src/node-types.json +1111 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2023 ObserverOfTime
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,150 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @willbooster/tree-sitter-xml
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-xml)
|
|
4
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-xml)
|
|
5
|
+
[](https://github.com/WillBooster/tree-sitter-xml/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/WillBooster/tree-sitter-xml/actions/workflows/test-rust.yml)
|
|
7
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
8
|
+
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
9
|
+
[](https://crates.io/crates/willbooster-tree-sitter-xml)
|
|
10
|
+
|
|
11
|
+
XML and DTD grammars for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
12
|
+
[tree-sitter-grammars/tree-sitter-xml](https://github.com/tree-sitter-grammars/tree-sitter-xml). We are grateful
|
|
13
|
+
to its authors and contributors. This is not an official release of that project.
|
|
14
|
+
|
|
15
|
+
This fork builds on the grammars and queries of upstream's version 0.7.0 and generates the parsers with the
|
|
16
|
+
[WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator. The parsers carry that fork's own ABI
|
|
17
|
+
number (`0x57420001`) and load only with that fork's runtime, version 2.0.0 or later: upstream's tree-sitter runtime
|
|
18
|
+
rejects them. The XML scanner adapts to the generated array API with the change upstream itself made after 0.7.0. The
|
|
19
|
+
parsers parse as the parsers in upstream's 0.7.0 packages do, except for NUL characters, which are ordinary characters:
|
|
20
|
+
|
|
21
|
+
- A NUL character inside a quoted literal, such as an attribute value (`AttValue`), an entity value (`EntityValue`),
|
|
22
|
+
or a system literal (`SystemLiteral`), is part of that token, where upstream's lexer reports an error.
|
|
23
|
+
- Where a NUL character cannot be part of any token, the `ERROR` node covers it as it covers any other unexpected
|
|
24
|
+
character, without the unexpected-character node that upstream's parsers put inside it.
|
|
25
|
+
|
|
26
|
+
It holds two grammars, as upstream does: `xml` parses XML documents, including an internal DTD subset, and `dtd`
|
|
27
|
+
parses external DTD files.
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
The npm package ships `tree-sitter-xml.wasm` and `tree-sitter-dtd.wasm` for
|
|
32
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 2.0.0 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. In Node.js and Bun, load the grammar from its path:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { fileURLToPath } from 'node:url';
|
|
36
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
37
|
+
|
|
38
|
+
await Parser.init();
|
|
39
|
+
const parser = new Parser();
|
|
40
|
+
const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-xml/tree-sitter-xml.wasm'));
|
|
41
|
+
parser.setLanguage(await Language.load(wasmPath));
|
|
42
|
+
const tree = parser.parse('<?xml version="1.0"?>\n<note><to>Tove</to></note>\n');
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
49
|
+
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
|
|
50
|
+
import xmlUrl from '@willbooster/tree-sitter-xml/tree-sitter-xml.wasm?url';
|
|
51
|
+
|
|
52
|
+
await Parser.init({ locateFile: () => runtimeUrl });
|
|
53
|
+
const parser = new Parser();
|
|
54
|
+
parser.setLanguage(await Language.load(xmlUrl));
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
|
|
58
|
+
Node.js compatibility:
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
62
|
+
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
|
|
63
|
+
import xml from '@willbooster/tree-sitter-xml/tree-sitter-xml.wasm';
|
|
64
|
+
|
|
65
|
+
await Parser.init({ wasmModule: runtime });
|
|
66
|
+
const parser = new Parser();
|
|
67
|
+
parser.setLanguage(await Language.load(xml));
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The package also ships the grammar definitions (`common/common.mjs`, `xml/grammar.js`, and `dtd/grammar.js`, with the
|
|
71
|
+
`package.json` files that mark the latter two as ES modules), the queries in `queries/`, and the node types in
|
|
72
|
+
`xml/src/node-types.json` and `dtd/src/node-types.json`.
|
|
73
|
+
|
|
74
|
+
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-xml) and on
|
|
75
|
+
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
|
76
|
+
fuzzed with:
|
|
77
|
+
|
|
78
|
+
```toml
|
|
79
|
+
[dependencies]
|
|
80
|
+
tree-sitter = { package = "willbooster-tree-sitter", version = "2" }
|
|
81
|
+
tree-sitter-xml = { package = "willbooster-tree-sitter-xml", version = "1" }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```rust
|
|
85
|
+
let mut parser = tree_sitter::Parser::new();
|
|
86
|
+
parser.set_language(&tree_sitter_xml::LANGUAGE_XML.into())?;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`tree_sitter_xml::LANGUAGE_DTD` is the `dtd` grammar. The crate also exports upstream's `XML_HIGHLIGHT_QUERY`,
|
|
90
|
+
`DTD_HIGHLIGHT_QUERY`, `XML_NODE_TYPES`, and `DTD_NODE_TYPES`.
|
|
91
|
+
|
|
92
|
+
## Development
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
mise install
|
|
96
|
+
bun install --frozen-lockfile
|
|
97
|
+
bun run test/ci-setup # installs Chromium for the browser test
|
|
98
|
+
bun run build/ci
|
|
99
|
+
bun run test
|
|
100
|
+
script/parse-examples
|
|
101
|
+
cargo test
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
|
|
105
|
+
WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
|
|
106
|
+
not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
|
|
107
|
+
with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary that runs here. Run other CLI commands through it as
|
|
108
|
+
well (e.g. `script/tree-sitter parse --grammar-path xml file.xml`).
|
|
109
|
+
|
|
110
|
+
`bun run generate` records a fresh generation profile for each grammar from the `test/corpus` cases of that grammar,
|
|
111
|
+
then generates compact parser tables. It would also profile files tracked in `examples/`, but this repository tracks
|
|
112
|
+
none. The corpus is upstream's, with the blank line between each case's name and its attributes removed, since the
|
|
113
|
+
WillBooster/tree-sitter CLI, like upstream's current CLI, reads no case whose header has one, plus
|
|
114
|
+
`null-characters.txt`, which pins this parser's reading of NUL characters. The parsers also embed metadata from
|
|
115
|
+
`tree-sitter.json`. After changing a grammar, `tree-sitter.json`, or a corpus case, regenerate and commit `xml/src/`
|
|
116
|
+
and `dtd/src/`. Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`,
|
|
117
|
+
`bun run build/ci`, and the release build regenerate the parsers before compiling them.
|
|
118
|
+
|
|
119
|
+
`bun run test` runs:
|
|
120
|
+
|
|
121
|
+
- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK).
|
|
122
|
+
Each case names its grammar with `:language(xml)` or `:language(dtd)`;
|
|
123
|
+
- an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz` for a
|
|
124
|
+
grammar, which edits each of its corpus cases at random, reparses it, undoes the edits, and reparses again.
|
|
125
|
+
`TREE_SITTER_SEED`, `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
|
|
126
|
+
- a check that the real-world files in `examples/` fail to parse exactly as listed in `script/known-failures.txt`, each
|
|
127
|
+
parsed with the grammar its extension belongs to in `tree-sitter.json`. The first run clones the repositories. The
|
|
128
|
+
example repositories are pinned to commits in `script/parse-examples`. After a grammar change or a moved pin alters
|
|
129
|
+
that list, `script/parse-examples` rewrites it; review its diff before committing;
|
|
130
|
+
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear
|
|
131
|
+
time with both grammars, since consumers parse files while they are being edited. It loads the Wasm builds through
|
|
132
|
+
@willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parsers;
|
|
133
|
+
- a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
|
|
134
|
+
`willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
|
|
135
|
+
tests, the fuzzer, and the CLI on the latter;
|
|
136
|
+
- tests that load the Wasm builds with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
|
|
137
|
+
Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
|
|
138
|
+
|
|
139
|
+
The tests compile the parsers into `.tmp/tree-sitter-lib` rather than the CLI's cache shared by every checkout;
|
|
140
|
+
`script/fuzz-corpus` and `script/parse-examples` build parsers of their own in `.tmp/` for each run and delete them
|
|
141
|
+
afterwards.
|
|
142
|
+
|
|
143
|
+
CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parsers and the scanners natively, and
|
|
144
|
+
fuzzes both parsers with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
|
|
145
|
+
|
|
146
|
+
### References
|
|
147
|
+
|
|
148
|
+
- [Extensible Markup Language (XML) 1.0](https://www.w3.org/TR/xml/)
|
|
149
|
+
- [Associating Schemas with XML documents 1.0](https://www.w3.org/TR/xml-model/)
|
|
150
|
+
- [Associating Style Sheets with XML documents 1.0](https://www.w3.org/TR/xml-stylesheet/)
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Reusable constructs
|
|
3
|
+
* @author ObserverOfTime
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/// <reference types="tree-sitter-cli/dsl" />
|
|
8
|
+
// @ts-check
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {"'" | ''} q
|
|
12
|
+
* @see {@link https://www.w3.org/TR/xml/#NT-PubidChar}
|
|
13
|
+
*/
|
|
14
|
+
export const pubid_char = (q) =>
|
|
15
|
+
new RegExp(`[ \\r\\na-zA-Z0-9\\-${q}()+,./:=?;!*#@$_%]*`);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {GrammarSymbols<any>} $
|
|
19
|
+
* @param {'"' | "'"} q
|
|
20
|
+
*/
|
|
21
|
+
export const att_value = ($, q) =>
|
|
22
|
+
seq(
|
|
23
|
+
q,
|
|
24
|
+
field(
|
|
25
|
+
'content',
|
|
26
|
+
repeat(choice(
|
|
27
|
+
new RegExp(`[^<&${q}]`),
|
|
28
|
+
$._Reference
|
|
29
|
+
))
|
|
30
|
+
),
|
|
31
|
+
q
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {GrammarSymbols<any>} $
|
|
36
|
+
* @param {'"' | "'"} q
|
|
37
|
+
*/
|
|
38
|
+
export const entity_value = ($, q) =>
|
|
39
|
+
seq(
|
|
40
|
+
q,
|
|
41
|
+
field(
|
|
42
|
+
'content',
|
|
43
|
+
repeat(choice(
|
|
44
|
+
new RegExp(`[^<%&${q}]`),
|
|
45
|
+
$.PEReference,
|
|
46
|
+
$._Reference
|
|
47
|
+
))
|
|
48
|
+
),
|
|
49
|
+
q
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
/** @param {RuleOrLiteral[]} choices */
|
|
54
|
+
export const str = (...choices) =>
|
|
55
|
+
choice(
|
|
56
|
+
seq("'", ...choices, "'"),
|
|
57
|
+
seq('"', ...choices, '"')
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {GrammarSymbols<any>} $
|
|
62
|
+
* @param {RuleOrLiteral[]} choices
|
|
63
|
+
*/
|
|
64
|
+
export const ref = ($, ...choices) =>
|
|
65
|
+
choice(...choices, $.PEReference);
|
|
66
|
+
|
|
67
|
+
/** @param {RuleOrLiteral[]} rules */
|
|
68
|
+
export const rseq = (...rules) => repeat(seq(...rules));
|
|
69
|
+
|
|
70
|
+
/** @param {RuleOrLiteral[]} rules */
|
|
71
|
+
export const rseq1 = (...rules) => repeat1(seq(...rules));
|
|
72
|
+
|
|
73
|
+
const O = optional;
|
|
74
|
+
|
|
75
|
+
/** @type {Record<string, ($: GrammarSymbols<any>) => RuleOrLiteral>} */
|
|
76
|
+
export const rules = {
|
|
77
|
+
_markupdecl: $ => choice(
|
|
78
|
+
$.elementdecl,
|
|
79
|
+
$.AttlistDecl,
|
|
80
|
+
$._EntityDecl,
|
|
81
|
+
$.NotationDecl,
|
|
82
|
+
$.PI,
|
|
83
|
+
$.Comment
|
|
84
|
+
),
|
|
85
|
+
|
|
86
|
+
_DeclSep: $ => choice($.PEReference, $._S),
|
|
87
|
+
|
|
88
|
+
elementdecl: $ => seq(
|
|
89
|
+
'<!',
|
|
90
|
+
'ELEMENT',
|
|
91
|
+
$._S,
|
|
92
|
+
ref($, $.Name),
|
|
93
|
+
$._S,
|
|
94
|
+
$.contentspec,
|
|
95
|
+
O($._S),
|
|
96
|
+
'>'
|
|
97
|
+
),
|
|
98
|
+
|
|
99
|
+
contentspec: $ => choice(
|
|
100
|
+
'EMPTY',
|
|
101
|
+
'ANY',
|
|
102
|
+
$.Mixed,
|
|
103
|
+
$.children,
|
|
104
|
+
$.PEReference
|
|
105
|
+
),
|
|
106
|
+
|
|
107
|
+
Mixed: $ => choice(
|
|
108
|
+
seq(
|
|
109
|
+
'(',
|
|
110
|
+
O($._S),
|
|
111
|
+
ref($, '#PCDATA'),
|
|
112
|
+
rseq(
|
|
113
|
+
O($._S),
|
|
114
|
+
'|',
|
|
115
|
+
O($._S),
|
|
116
|
+
ref($, $.Name),
|
|
117
|
+
),
|
|
118
|
+
O($._S),
|
|
119
|
+
rseq(
|
|
120
|
+
$.PEReference,
|
|
121
|
+
O($._S)
|
|
122
|
+
),
|
|
123
|
+
')',
|
|
124
|
+
'*'
|
|
125
|
+
),
|
|
126
|
+
prec(-1, seq(
|
|
127
|
+
'(',
|
|
128
|
+
O($._S),
|
|
129
|
+
ref($, '#PCDATA'),
|
|
130
|
+
O($._S),
|
|
131
|
+
rseq(
|
|
132
|
+
$.PEReference,
|
|
133
|
+
O($._S)
|
|
134
|
+
),
|
|
135
|
+
')'
|
|
136
|
+
))
|
|
137
|
+
),
|
|
138
|
+
|
|
139
|
+
children: $ => prec(1, seq(
|
|
140
|
+
$._choice,
|
|
141
|
+
O(choice('?', '*', '+'))
|
|
142
|
+
)),
|
|
143
|
+
|
|
144
|
+
_cp: $ => prec.left(seq(
|
|
145
|
+
ref($, $.Name, $._choice),
|
|
146
|
+
O(choice('?', '*', '+'))
|
|
147
|
+
)),
|
|
148
|
+
|
|
149
|
+
_choice: $ => seq(
|
|
150
|
+
'(',
|
|
151
|
+
O($._S),
|
|
152
|
+
$._cp,
|
|
153
|
+
rseq(
|
|
154
|
+
O($._S),
|
|
155
|
+
choice('|', ','),
|
|
156
|
+
O($._S),
|
|
157
|
+
$._cp
|
|
158
|
+
),
|
|
159
|
+
rseq(
|
|
160
|
+
O($._S),
|
|
161
|
+
$.PEReference,
|
|
162
|
+
),
|
|
163
|
+
O($._S),
|
|
164
|
+
')'
|
|
165
|
+
),
|
|
166
|
+
|
|
167
|
+
AttlistDecl: $ => seq(
|
|
168
|
+
'<!',
|
|
169
|
+
'ATTLIST',
|
|
170
|
+
$._S,
|
|
171
|
+
ref($, $.Name),
|
|
172
|
+
repeat(choice(
|
|
173
|
+
$.AttDef,
|
|
174
|
+
seq($._S, $.PEReference)
|
|
175
|
+
)),
|
|
176
|
+
O($._S),
|
|
177
|
+
'>'
|
|
178
|
+
),
|
|
179
|
+
|
|
180
|
+
AttDef: $ => prec.right(seq(
|
|
181
|
+
$._S,
|
|
182
|
+
ref($, $.Name),
|
|
183
|
+
$._S,
|
|
184
|
+
$._AttType,
|
|
185
|
+
O(seq($._S, $.DefaultDecl))
|
|
186
|
+
)),
|
|
187
|
+
|
|
188
|
+
_AttType: $ => choice(
|
|
189
|
+
$.StringType,
|
|
190
|
+
$.TokenizedType,
|
|
191
|
+
$._EnumeratedType,
|
|
192
|
+
$.PEReference
|
|
193
|
+
),
|
|
194
|
+
|
|
195
|
+
StringType: _ => 'CDATA',
|
|
196
|
+
|
|
197
|
+
TokenizedType: _ => token(choice(
|
|
198
|
+
'ID',
|
|
199
|
+
'IDREF',
|
|
200
|
+
'IDREFS',
|
|
201
|
+
'ENTITY',
|
|
202
|
+
'ENTITIES',
|
|
203
|
+
'NMTOKEN',
|
|
204
|
+
'NMTOKENS',
|
|
205
|
+
)),
|
|
206
|
+
|
|
207
|
+
_EnumeratedType: $ => choice(
|
|
208
|
+
$.NotationType,
|
|
209
|
+
$.Enumeration
|
|
210
|
+
),
|
|
211
|
+
|
|
212
|
+
NotationType: $ => seq(
|
|
213
|
+
'NOTATION',
|
|
214
|
+
$._S,
|
|
215
|
+
'(',
|
|
216
|
+
O($._S),
|
|
217
|
+
ref($, $.Name),
|
|
218
|
+
rseq(
|
|
219
|
+
O($._S),
|
|
220
|
+
'|',
|
|
221
|
+
O($._S)
|
|
222
|
+
),
|
|
223
|
+
ref($, $.Name),
|
|
224
|
+
O($._S),
|
|
225
|
+
')'
|
|
226
|
+
),
|
|
227
|
+
|
|
228
|
+
Enumeration: $ => seq(
|
|
229
|
+
'(',
|
|
230
|
+
O($._S),
|
|
231
|
+
$.Nmtoken,
|
|
232
|
+
rseq(
|
|
233
|
+
O($._S),
|
|
234
|
+
'|',
|
|
235
|
+
O($._S),
|
|
236
|
+
$.Nmtoken
|
|
237
|
+
),
|
|
238
|
+
O($._S),
|
|
239
|
+
')'
|
|
240
|
+
),
|
|
241
|
+
|
|
242
|
+
DefaultDecl: $ => choice(
|
|
243
|
+
'#REQUIRED',
|
|
244
|
+
'#IMPLIED',
|
|
245
|
+
seq(
|
|
246
|
+
O(seq('#FIXED', $._S)),
|
|
247
|
+
$.AttValue
|
|
248
|
+
),
|
|
249
|
+
$.PEReference
|
|
250
|
+
),
|
|
251
|
+
|
|
252
|
+
_EntityDecl: $ => choice(
|
|
253
|
+
$.GEDecl,
|
|
254
|
+
$.PEDecl
|
|
255
|
+
),
|
|
256
|
+
|
|
257
|
+
GEDecl: $ => seq(
|
|
258
|
+
'<!',
|
|
259
|
+
'ENTITY',
|
|
260
|
+
$._S,
|
|
261
|
+
ref($, $.Name),
|
|
262
|
+
$._S,
|
|
263
|
+
choice(
|
|
264
|
+
$.EntityValue,
|
|
265
|
+
seq(
|
|
266
|
+
$.ExternalID,
|
|
267
|
+
O($.NDataDecl)
|
|
268
|
+
)
|
|
269
|
+
),
|
|
270
|
+
O($._S),
|
|
271
|
+
'>'
|
|
272
|
+
),
|
|
273
|
+
|
|
274
|
+
PEDecl: $ => seq(
|
|
275
|
+
'<!',
|
|
276
|
+
'ENTITY',
|
|
277
|
+
$._S,
|
|
278
|
+
'%',
|
|
279
|
+
$._S,
|
|
280
|
+
$.Name,
|
|
281
|
+
$._S,
|
|
282
|
+
choice(
|
|
283
|
+
$.EntityValue,
|
|
284
|
+
$.ExternalID
|
|
285
|
+
),
|
|
286
|
+
O($._S),
|
|
287
|
+
'>'
|
|
288
|
+
),
|
|
289
|
+
|
|
290
|
+
EntityValue: $ => choice(
|
|
291
|
+
entity_value($, '"'),
|
|
292
|
+
entity_value($, "'")
|
|
293
|
+
),
|
|
294
|
+
|
|
295
|
+
NDataDecl: $ => seq($._S, 'NDATA', $._S, ref($, $.Name)),
|
|
296
|
+
|
|
297
|
+
NotationDecl: $ => seq(
|
|
298
|
+
'<!',
|
|
299
|
+
'NOTATION',
|
|
300
|
+
$._S,
|
|
301
|
+
ref($, $.Name),
|
|
302
|
+
$._S,
|
|
303
|
+
choice($.ExternalID, $.PublicID),
|
|
304
|
+
O($._S),
|
|
305
|
+
'>'
|
|
306
|
+
),
|
|
307
|
+
|
|
308
|
+
PEReference: $ => seq('%', $.Name, ';'),
|
|
309
|
+
|
|
310
|
+
_S: _ => /[ \t\r\n]+/,
|
|
311
|
+
|
|
312
|
+
Name: _ => /[a-zA-Z_][a-zA-Z0-9_:.·-]*/,
|
|
313
|
+
|
|
314
|
+
Nmtoken: _ => /[a-zA-Z0-9_:.·-]+/,
|
|
315
|
+
|
|
316
|
+
_Reference: $ => choice($.EntityRef, $.CharRef),
|
|
317
|
+
|
|
318
|
+
EntityRef: $ => seq('&', $.Name, ';'),
|
|
319
|
+
|
|
320
|
+
CharRef: _ => choice(
|
|
321
|
+
seq('&#', /[0-9]+/, ';'),
|
|
322
|
+
seq('&#x', /[0-9a-fA-F]+/, ';')
|
|
323
|
+
),
|
|
324
|
+
|
|
325
|
+
AttValue: $ => choice(
|
|
326
|
+
att_value($, '"'),
|
|
327
|
+
att_value($, "'")
|
|
328
|
+
),
|
|
329
|
+
|
|
330
|
+
ExternalID: $ => choice(
|
|
331
|
+
seq('SYSTEM', $._S, $.SystemLiteral),
|
|
332
|
+
seq('PUBLIC', $._S, $.PubidLiteral, $._S, $.SystemLiteral)
|
|
333
|
+
),
|
|
334
|
+
|
|
335
|
+
PublicID: $ => prec.right(
|
|
336
|
+
seq(ref($, 'PUBLIC'), $._S, $.PubidLiteral)
|
|
337
|
+
),
|
|
338
|
+
|
|
339
|
+
SystemLiteral: $ => choice(
|
|
340
|
+
seq('"', alias(/[^"]*/, $.URI), '"'),
|
|
341
|
+
seq("'", alias(/[^']*/, $.URI), "'")
|
|
342
|
+
),
|
|
343
|
+
|
|
344
|
+
PubidLiteral: _ => choice(
|
|
345
|
+
seq('"', pubid_char("'"), '"'),
|
|
346
|
+
seq("'", pubid_char(''), "'")
|
|
347
|
+
),
|
|
348
|
+
|
|
349
|
+
_VersionInfo: $ => seq(
|
|
350
|
+
$._S,
|
|
351
|
+
'version',
|
|
352
|
+
$._Eq,
|
|
353
|
+
str($.VersionNum)
|
|
354
|
+
),
|
|
355
|
+
|
|
356
|
+
VersionNum: _ => /1\.[0-9]+/,
|
|
357
|
+
|
|
358
|
+
_EncodingDecl: $ => seq(
|
|
359
|
+
$._S,
|
|
360
|
+
'encoding',
|
|
361
|
+
$._Eq,
|
|
362
|
+
str($.EncName)
|
|
363
|
+
),
|
|
364
|
+
|
|
365
|
+
EncName: _ => /[A-Za-z][A-Za-z0-9._\-]*/,
|
|
366
|
+
|
|
367
|
+
PI: $ => seq(
|
|
368
|
+
'<?',
|
|
369
|
+
$.PITarget,
|
|
370
|
+
O(seq($._S, $._pi_content)),
|
|
371
|
+
'?>'
|
|
372
|
+
),
|
|
373
|
+
|
|
374
|
+
_Eq: $ => seq(O($._S), '=', O($._S))
|
|
375
|
+
}
|
package/dtd/grammar.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Tree-sitter grammar definition for DTD
|
|
3
|
+
* @author ObserverOfTime
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/// <reference types="tree-sitter-cli/dsl" />
|
|
8
|
+
// @ts-check
|
|
9
|
+
|
|
10
|
+
import * as c from '../common/common.mjs';
|
|
11
|
+
|
|
12
|
+
const O = optional;
|
|
13
|
+
|
|
14
|
+
export default grammar({
|
|
15
|
+
name: 'dtd',
|
|
16
|
+
|
|
17
|
+
externals: $ => [
|
|
18
|
+
$.PITarget,
|
|
19
|
+
$._pi_content,
|
|
20
|
+
$.Comment,
|
|
21
|
+
],
|
|
22
|
+
|
|
23
|
+
extras: _ => [],
|
|
24
|
+
|
|
25
|
+
supertypes: $ => [
|
|
26
|
+
$._markupdecl,
|
|
27
|
+
$._AttType,
|
|
28
|
+
$._EnumeratedType,
|
|
29
|
+
$._EntityDecl,
|
|
30
|
+
$._Reference,
|
|
31
|
+
],
|
|
32
|
+
|
|
33
|
+
conflicts: $ => [
|
|
34
|
+
[$.AttlistDecl, $.AttDef]
|
|
35
|
+
],
|
|
36
|
+
|
|
37
|
+
word: $ => $.Name,
|
|
38
|
+
|
|
39
|
+
rules: {
|
|
40
|
+
extSubset: $ => seq(
|
|
41
|
+
O($.TextDecl),
|
|
42
|
+
repeat1($._extSubsetDecl)
|
|
43
|
+
),
|
|
44
|
+
|
|
45
|
+
TextDecl: $ => seq(
|
|
46
|
+
'<?',
|
|
47
|
+
'xml',
|
|
48
|
+
$._VersionInfo,
|
|
49
|
+
O($._EncodingDecl),
|
|
50
|
+
O($._S),
|
|
51
|
+
'?>'
|
|
52
|
+
),
|
|
53
|
+
|
|
54
|
+
_extSubsetDecl: $ => choice(
|
|
55
|
+
$._markupdecl,
|
|
56
|
+
$.conditionalSect,
|
|
57
|
+
$._DeclSep
|
|
58
|
+
),
|
|
59
|
+
|
|
60
|
+
conditionalSect: $ => seq(
|
|
61
|
+
'<![',
|
|
62
|
+
O($._S),
|
|
63
|
+
c.ref($, 'IGNORE', 'INCLUDE'),
|
|
64
|
+
O($._S),
|
|
65
|
+
'[',
|
|
66
|
+
repeat($._extSubsetDecl),
|
|
67
|
+
']]>'
|
|
68
|
+
),
|
|
69
|
+
|
|
70
|
+
...c.rules
|
|
71
|
+
}
|
|
72
|
+
});
|
package/dtd/package.json
ADDED