@konomanoasa/tree-sitter-query 0.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/Cargo.toml +36 -0
- package/LICENSE +21 -0
- package/README.md +34 -0
- package/bindings/rust/build.rs +23 -0
- package/bindings/rust/lib.rs +12 -0
- package/grammar.js +191 -0
- package/package.json +43 -0
- package/queries/highlights.scm +59 -0
- package/src/grammar.json +1265 -0
- package/src/node-types.json +478 -0
- package/src/parser.c +5118 -0
- package/src/tree_sitter/alloc.h +54 -0
- package/src/tree_sitter/array.h +336 -0
- package/src/tree_sitter/parser.h +286 -0
- package/test/bindings.test.rs +518 -0
- package/tree-sitter.json +30 -0
package/Cargo.toml
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "konomanoasa-tree-sitter-query"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2024"
|
|
5
|
+
description = "Tree-sitter grammar for Tree-sitter Query."
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
repository = "https://github.com/konomanoasa/tree-sitter-query"
|
|
8
|
+
license = "MIT"
|
|
9
|
+
build = "bindings/rust/build.rs"
|
|
10
|
+
links = "tree-sitter-query"
|
|
11
|
+
include = [
|
|
12
|
+
"/LICENSE",
|
|
13
|
+
"/README.md",
|
|
14
|
+
"/bindings/rust/*.rs",
|
|
15
|
+
"/queries/*.scm",
|
|
16
|
+
"/src/*.c",
|
|
17
|
+
"/src/node-types.json",
|
|
18
|
+
"/src/tree_sitter/*.h",
|
|
19
|
+
"/test/bindings.test.rs",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[lib]
|
|
23
|
+
path = "bindings/rust/lib.rs"
|
|
24
|
+
|
|
25
|
+
[[test]]
|
|
26
|
+
name = "bindings"
|
|
27
|
+
path = "test/bindings.test.rs"
|
|
28
|
+
|
|
29
|
+
[dependencies]
|
|
30
|
+
tree-sitter-language = "0.1.8"
|
|
31
|
+
|
|
32
|
+
[dev-dependencies]
|
|
33
|
+
tree-sitter = "0.27.0"
|
|
34
|
+
|
|
35
|
+
[build-dependencies]
|
|
36
|
+
cc = "1.4"
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 konomanoasa
|
|
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,34 @@
|
|
|
1
|
+
# tree-sitter-query
|
|
2
|
+
|
|
3
|
+
[](https://github.com/konomanoasa/tree-sitter-query/actions/workflows/ci.yaml)
|
|
4
|
+
[](https://crates.io/crates/konomanoasa-tree-sitter-query)
|
|
5
|
+
[](https://www.npmjs.com/package/@konomanoasa/tree-sitter-query)
|
|
6
|
+
|
|
7
|
+
[Tree-sitter](https://tree-sitter.github.io/tree-sitter/) grammar for
|
|
8
|
+
Tree-sitter Query Language.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @konomanoasa/tree-sitter-query
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Development
|
|
17
|
+
|
|
18
|
+
Development uses Node.js 24 or later.
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npm install
|
|
22
|
+
npm run build
|
|
23
|
+
npm test
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Specifications
|
|
27
|
+
|
|
28
|
+
- [Tree-sitter query syntax](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html)
|
|
29
|
+
- [Tree-sitter query operators](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/2-operators.html)
|
|
30
|
+
- [Tree-sitter query predicates and directives](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/3-predicates-and-directives.html)
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
use std::{env, path::Path};
|
|
2
|
+
|
|
3
|
+
fn main() {
|
|
4
|
+
let mut build = cc::Build::new();
|
|
5
|
+
build.std("c17");
|
|
6
|
+
if build.get_compiler().is_like_msvc() {
|
|
7
|
+
build.flag("-utf-8");
|
|
8
|
+
}
|
|
9
|
+
if env::var("TARGET").expect("Cargo must provide TARGET")
|
|
10
|
+
== "wasm32-unknown-unknown"
|
|
11
|
+
{
|
|
12
|
+
let headers = env::var_os("DEP_TREE_SITTER_LANGUAGE_WASM_HEADERS")
|
|
13
|
+
.expect("tree-sitter-language must provide WebAssembly headers");
|
|
14
|
+
build.include(&headers);
|
|
15
|
+
println!("cargo::rerun-if-changed={}", Path::new(&headers).display());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
println!("cargo::rerun-if-changed=src");
|
|
19
|
+
build
|
|
20
|
+
.include("src")
|
|
21
|
+
.file("src/parser.c")
|
|
22
|
+
.compile("tree-sitter-query");
|
|
23
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
use tree_sitter_language::LanguageFn;
|
|
2
|
+
|
|
3
|
+
unsafe extern "C" {
|
|
4
|
+
fn tree_sitter_query() -> *const ();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
pub const LANGUAGE: LanguageFn =
|
|
8
|
+
unsafe { LanguageFn::from_raw(tree_sitter_query) };
|
|
9
|
+
|
|
10
|
+
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
|
|
11
|
+
|
|
12
|
+
pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
|
package/grammar.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
const name = /[A-Za-z0-9_-][A-Za-z0-9_.-]*/;
|
|
2
|
+
const stringContent = /[^"\\\n]+/;
|
|
3
|
+
|
|
4
|
+
const postfixed = ($, body) =>
|
|
5
|
+
seq(
|
|
6
|
+
field("body", body),
|
|
7
|
+
repeat(
|
|
8
|
+
choice(field("capture", $.capture), field("quantifier", $.quantifier)),
|
|
9
|
+
),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const anchored = ($, member) => seq(optional($.anchor), member);
|
|
13
|
+
|
|
14
|
+
const fieldConstraint = ($, target) =>
|
|
15
|
+
seq(
|
|
16
|
+
field(
|
|
17
|
+
"name",
|
|
18
|
+
choice(
|
|
19
|
+
alias($._field_identifier, $.identifier),
|
|
20
|
+
alias("MISSING", $.identifier),
|
|
21
|
+
),
|
|
22
|
+
),
|
|
23
|
+
":",
|
|
24
|
+
field("pattern", target),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// The Query API accepts no layout or comment around the subtype separator.
|
|
28
|
+
const subtyped = ($, name) =>
|
|
29
|
+
seq(
|
|
30
|
+
field("supertype", name),
|
|
31
|
+
token.immediate("/"),
|
|
32
|
+
field(
|
|
33
|
+
"subtype",
|
|
34
|
+
choice(
|
|
35
|
+
alias($._immediate_identifier, $.identifier),
|
|
36
|
+
alias($._immediate_string, $.string),
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const call = ($, marker) =>
|
|
42
|
+
seq(
|
|
43
|
+
"(",
|
|
44
|
+
"#",
|
|
45
|
+
field("name", alias($._immediate_identifier, $.identifier)),
|
|
46
|
+
token.immediate(marker),
|
|
47
|
+
repeat(field("argument", choice($.capture, $.string, $._name))),
|
|
48
|
+
")",
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
export default grammar({
|
|
52
|
+
name: "query",
|
|
53
|
+
extras: ($) => [/[ \t\r\n\v\f]/, $.comment],
|
|
54
|
+
rules: {
|
|
55
|
+
query: ($) => repeat($._member),
|
|
56
|
+
_member: ($) => choice($._child_pattern, $._call),
|
|
57
|
+
_call_only_member: ($) => choice($._call_only_child_pattern, $._call),
|
|
58
|
+
_call: ($) => choice($.predicate, $.directive),
|
|
59
|
+
pattern: ($) => postfixed($, choice($._delimited, $.string, $.wildcard)),
|
|
60
|
+
_call_only_pattern: ($) =>
|
|
61
|
+
postfixed(
|
|
62
|
+
$,
|
|
63
|
+
choice(
|
|
64
|
+
alias($._call_only_group, $.group),
|
|
65
|
+
alias($._call_only_alternation, $.alternation),
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
// "(_" always opens a node pattern, so a group never starts with a bare wildcard.
|
|
69
|
+
_group_pattern: ($) => postfixed($, choice($._delimited, $.string)),
|
|
70
|
+
_delimited: ($) => choice($.node_pattern, $.group, $.alternation),
|
|
71
|
+
node_pattern: ($) =>
|
|
72
|
+
seq(
|
|
73
|
+
"(",
|
|
74
|
+
choice(
|
|
75
|
+
field("node", alias($._untyped_missing_node, $.missing_node)),
|
|
76
|
+
seq(
|
|
77
|
+
field(
|
|
78
|
+
"node",
|
|
79
|
+
choice($.identifier, $.wildcard, $.supertype, $.missing_node),
|
|
80
|
+
),
|
|
81
|
+
repeat($._node_annotation),
|
|
82
|
+
optional(
|
|
83
|
+
seq(
|
|
84
|
+
anchored($, $._matching_child_pattern),
|
|
85
|
+
repeat(
|
|
86
|
+
choice(
|
|
87
|
+
$._node_annotation,
|
|
88
|
+
anchored($, $._matching_child_pattern),
|
|
89
|
+
),
|
|
90
|
+
),
|
|
91
|
+
optional($.anchor),
|
|
92
|
+
),
|
|
93
|
+
),
|
|
94
|
+
),
|
|
95
|
+
),
|
|
96
|
+
")",
|
|
97
|
+
),
|
|
98
|
+
_child_pattern: ($) =>
|
|
99
|
+
choice($._matching_child_pattern, $._call_only_child_pattern),
|
|
100
|
+
_matching_child_pattern: ($) => choice($.pattern, $.field_constraint),
|
|
101
|
+
_call_only_child_pattern: ($) =>
|
|
102
|
+
choice(
|
|
103
|
+
alias($._call_only_pattern, $.pattern),
|
|
104
|
+
alias($._call_only_field_constraint, $.field_constraint),
|
|
105
|
+
),
|
|
106
|
+
_node_annotation: ($) =>
|
|
107
|
+
choice(anchored($, $._call_only_member), $.negated_field),
|
|
108
|
+
field_constraint: ($) => fieldConstraint($, $._matching_child_pattern),
|
|
109
|
+
_call_only_field_constraint: ($) =>
|
|
110
|
+
fieldConstraint($, $._call_only_child_pattern),
|
|
111
|
+
negated_field: ($) => seq("!", field("name", $._name)),
|
|
112
|
+
supertype: ($) =>
|
|
113
|
+
subtyped($, choice($.identifier, alias($.wildcard, $.identifier))),
|
|
114
|
+
_untyped_missing_node: () => "MISSING",
|
|
115
|
+
missing_node: ($) =>
|
|
116
|
+
seq(
|
|
117
|
+
"MISSING",
|
|
118
|
+
field(
|
|
119
|
+
"type",
|
|
120
|
+
choice($._name, $.string, alias($._missing_supertype, $.supertype)),
|
|
121
|
+
),
|
|
122
|
+
),
|
|
123
|
+
_missing_supertype: ($) => subtyped($, $._name),
|
|
124
|
+
group: ($) =>
|
|
125
|
+
seq(
|
|
126
|
+
"(",
|
|
127
|
+
choice(
|
|
128
|
+
alias($._group_pattern, $.pattern),
|
|
129
|
+
seq(
|
|
130
|
+
$._call_only_group_start,
|
|
131
|
+
repeat(anchored($, $._call_only_member)),
|
|
132
|
+
anchored($, $._matching_child_pattern),
|
|
133
|
+
),
|
|
134
|
+
),
|
|
135
|
+
repeat(anchored($, $._member)),
|
|
136
|
+
")",
|
|
137
|
+
),
|
|
138
|
+
_call_only_group: ($) =>
|
|
139
|
+
seq(
|
|
140
|
+
"(",
|
|
141
|
+
$._call_only_group_start,
|
|
142
|
+
repeat(anchored($, $._call_only_member)),
|
|
143
|
+
")",
|
|
144
|
+
),
|
|
145
|
+
_call_only_group_start: ($) =>
|
|
146
|
+
choice($._call, alias($._call_only_pattern, $.pattern)),
|
|
147
|
+
alternation: ($) =>
|
|
148
|
+
seq(
|
|
149
|
+
"[",
|
|
150
|
+
repeat($._call_only_member),
|
|
151
|
+
$._matching_child_pattern,
|
|
152
|
+
repeat($._member),
|
|
153
|
+
"]",
|
|
154
|
+
),
|
|
155
|
+
_call_only_alternation: ($) => seq("[", repeat1($._call_only_member), "]"),
|
|
156
|
+
quantifier: () => token(choice("+", "*", "?")),
|
|
157
|
+
anchor: () => ".",
|
|
158
|
+
capture: ($) =>
|
|
159
|
+
seq("@", field("name", alias($._immediate_identifier, $.identifier))),
|
|
160
|
+
predicate: ($) => call($, "?"),
|
|
161
|
+
directive: ($) => call($, "!"),
|
|
162
|
+
identifier: () => name,
|
|
163
|
+
_field_identifier: () => /[A-Za-z0-9-][A-Za-z0-9_.-]*/,
|
|
164
|
+
_immediate_identifier: () => token.immediate(name),
|
|
165
|
+
_name: ($) =>
|
|
166
|
+
choice(
|
|
167
|
+
$.identifier,
|
|
168
|
+
alias($.wildcard, $.identifier),
|
|
169
|
+
alias("MISSING", $.identifier),
|
|
170
|
+
),
|
|
171
|
+
wildcard: () => "_",
|
|
172
|
+
string: ($) => seq('"', $._string_tail),
|
|
173
|
+
_immediate_string: ($) => seq(token.immediate('"'), $._string_tail),
|
|
174
|
+
_string_tail: ($) =>
|
|
175
|
+
seq(
|
|
176
|
+
repeat(choice($.string_content, $.escape_sequence)),
|
|
177
|
+
token.immediate('"'),
|
|
178
|
+
),
|
|
179
|
+
// Content outranks the comment extra so that ; stays inside the string.
|
|
180
|
+
string_content: () =>
|
|
181
|
+
token.immediate(
|
|
182
|
+
prec(
|
|
183
|
+
1,
|
|
184
|
+
repeat1(choice(stringContent, seq("\0", optional(stringContent)))),
|
|
185
|
+
),
|
|
186
|
+
),
|
|
187
|
+
// Distinct NUL transitions prevent the generated lexer from treating it as EOF.
|
|
188
|
+
escape_sequence: () => token.immediate(choice(/\\(.|\n)/, prec(1, "\\\0"))),
|
|
189
|
+
comment: () => /;[^\r\n]*(\r+[^\r\n]+)*/,
|
|
190
|
+
},
|
|
191
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@konomanoasa/tree-sitter-query",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Tree-sitter grammar for Tree-sitter Query.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": "git+https://github.com/konomanoasa/tree-sitter-query.git",
|
|
8
|
+
"files": [
|
|
9
|
+
"Cargo.toml",
|
|
10
|
+
"bindings/rust/*.rs",
|
|
11
|
+
"grammar.js",
|
|
12
|
+
"queries/*.scm",
|
|
13
|
+
"src/*.c",
|
|
14
|
+
"src/*.json",
|
|
15
|
+
"src/tree_sitter/*.h",
|
|
16
|
+
"test/bindings.test.rs",
|
|
17
|
+
"tree-sitter.json"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "npm run tree-sitter -- build-all",
|
|
24
|
+
"check": "biome check . && node scripts/check-generated.js && npm test",
|
|
25
|
+
"format": "biome check --write .",
|
|
26
|
+
"generate": "npm run tree-sitter -- generate-all",
|
|
27
|
+
"test": "npm run tree-sitter -- test-corpus --rebuild && node --test --test-concurrency=1 \"test/*.test.js\"",
|
|
28
|
+
"test:bindings": "cargo test --locked",
|
|
29
|
+
"test:fuzz": "node --test --test-name-pattern=\"fixed-seed generated histories\" test/incremental.test.js",
|
|
30
|
+
"test:fuzz:corpus": "npm run tree-sitter -- fuzz-all --iterations 100 --edits 5",
|
|
31
|
+
"test:highlights": "node --test test/highlight.test.js",
|
|
32
|
+
"test:incremental": "node --test test/incremental.test.js",
|
|
33
|
+
"tree-sitter": "node scripts/tree-sitter.js"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@biomejs/biome": "2.5.14",
|
|
37
|
+
"@types/node": "26.5.1",
|
|
38
|
+
"tree-sitter-cli": "0.27.0"
|
|
39
|
+
},
|
|
40
|
+
"allowScripts": {
|
|
41
|
+
"tree-sitter-cli": true
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
(identifier) @type
|
|
2
|
+
|
|
3
|
+
(string_content) @string
|
|
4
|
+
|
|
5
|
+
(escape_sequence) @string.escape
|
|
6
|
+
|
|
7
|
+
(comment) @comment
|
|
8
|
+
|
|
9
|
+
[
|
|
10
|
+
"("
|
|
11
|
+
")"
|
|
12
|
+
"["
|
|
13
|
+
"]"
|
|
14
|
+
] @punctuation.bracket
|
|
15
|
+
|
|
16
|
+
[
|
|
17
|
+
"\""
|
|
18
|
+
":"
|
|
19
|
+
"/"
|
|
20
|
+
] @punctuation.delimiter
|
|
21
|
+
|
|
22
|
+
[
|
|
23
|
+
"@"
|
|
24
|
+
"#"
|
|
25
|
+
] @punctuation.special
|
|
26
|
+
|
|
27
|
+
(wildcard) @character.special
|
|
28
|
+
|
|
29
|
+
[
|
|
30
|
+
(quantifier)
|
|
31
|
+
(anchor)
|
|
32
|
+
] @operator
|
|
33
|
+
|
|
34
|
+
(missing_node
|
|
35
|
+
"MISSING" @keyword)
|
|
36
|
+
|
|
37
|
+
(field_constraint
|
|
38
|
+
name: (identifier) @property)
|
|
39
|
+
|
|
40
|
+
(negated_field
|
|
41
|
+
"!" @operator
|
|
42
|
+
name: (identifier) @property)
|
|
43
|
+
|
|
44
|
+
(capture
|
|
45
|
+
name: (identifier) @variable)
|
|
46
|
+
|
|
47
|
+
(predicate
|
|
48
|
+
name: (identifier) @function.call
|
|
49
|
+
"?" @punctuation.special)
|
|
50
|
+
|
|
51
|
+
(directive
|
|
52
|
+
name: (identifier) @function.call
|
|
53
|
+
"!" @punctuation.special)
|
|
54
|
+
|
|
55
|
+
(predicate
|
|
56
|
+
argument: (identifier) @string)
|
|
57
|
+
|
|
58
|
+
(directive
|
|
59
|
+
argument: (identifier) @string)
|