@willbooster/tree-sitter-cpp 1.2.1 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -12
- package/package.json +9 -8
- package/queries/c/highlights.scm +81 -0
- package/queries/highlights.scm +89 -0
- package/queries/injections.scm +3 -0
- package/queries/tags.scm +15 -0
- package/src/node-types.json +4 -0
- package/tree-sitter-cpp.wasm +0 -0
- package/tree-sitter.json +2 -2
package/README.md
CHANGED
|
@@ -5,14 +5,15 @@
|
|
|
5
5
|
[](https://github.com/WillBooster/tree-sitter-cpp/actions/workflows/test.yml)
|
|
6
6
|
[](https://github.com/WillBooster/tree-sitter-cpp/actions/workflows/test-rust.yml)
|
|
7
7
|
[](https://github.com/semantic-release/semantic-release)
|
|
8
|
-
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
9
9
|
[](https://crates.io/crates/willbooster-tree-sitter-cpp)
|
|
10
10
|
|
|
11
11
|
C++ grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
12
12
|
[tree-sitter/tree-sitter-cpp](https://github.com/tree-sitter/tree-sitter-cpp). We are grateful
|
|
13
13
|
to its authors and contributors. This is not an official release of that project.
|
|
14
14
|
|
|
15
|
-
This fork fixes parsing bugs and raises conformance with the ISO C++ standard
|
|
15
|
+
This fork fixes parsing bugs and raises conformance with the ISO C++ standard
|
|
16
|
+
([working draft](https://eel.is/c++draft/)).
|
|
16
17
|
|
|
17
18
|
## Usage
|
|
18
19
|
|
|
@@ -61,7 +62,8 @@ const parser = new Parser();
|
|
|
61
62
|
parser.setLanguage(await Language.load(cpp));
|
|
62
63
|
```
|
|
63
64
|
|
|
64
|
-
The package also ships the node types in `src/node-types.json
|
|
65
|
+
The package also ships the node types in `src/node-types.json`, and the queries that `tree-sitter.json` lists: this
|
|
66
|
+
grammar's in `queries/` and, in `queries/c/`, the highlights of the C grammar it extends.
|
|
65
67
|
|
|
66
68
|
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-cpp) and on
|
|
67
69
|
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
|
@@ -79,6 +81,8 @@ let mut parser = tree_sitter::Parser::new();
|
|
|
79
81
|
parser.set_language(&tree_sitter_cpp::LANGUAGE.into())?;
|
|
80
82
|
```
|
|
81
83
|
|
|
84
|
+
The crate ships the same `tree-sitter.json` and query files as the npm package.
|
|
85
|
+
|
|
82
86
|
## Development
|
|
83
87
|
|
|
84
88
|
```sh
|
|
@@ -90,24 +94,39 @@ script/parse-examples
|
|
|
90
94
|
cargo test --locked
|
|
91
95
|
```
|
|
92
96
|
|
|
97
|
+
The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
|
|
98
|
+
WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
|
|
99
|
+
not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
|
|
100
|
+
with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary
|
|
101
|
+
that runs here. Run other CLI commands through it as well (e.g. `script/tree-sitter parse file.cpp`).
|
|
102
|
+
|
|
93
103
|
`bun run test` runs:
|
|
94
104
|
|
|
95
105
|
- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
|
|
96
|
-
- an incremental-parsing check (`test/unit/incremental.test.ts`): `tree-sitter fuzz
|
|
97
|
-
reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`, `TREE_SITTER_ITERATIONS`, and
|
|
106
|
+
- an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
|
|
107
|
+
edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`, `TREE_SITTER_ITERATIONS`, and
|
|
98
108
|
`TREE_SITTER_EDITS` run other or more edits;
|
|
99
109
|
- a check that the real-world C++ files in `examples/`, the checked-in ones and those of the cloned repositories,
|
|
100
|
-
fail to parse exactly as listed in `script/known-failures.txt`. The first run clones the repositories. The example
|
|
101
|
-
`script/parse-examples`. After a grammar change or a moved pin alters that
|
|
102
|
-
it; review its diff before committing;
|
|
103
|
-
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each
|
|
104
|
-
|
|
110
|
+
fail to parse exactly as listed in `script/known-failures.txt`. The first run clones the repositories. The example
|
|
111
|
+
repositories are pinned to commits in `script/parse-examples`. After a grammar change or a moved pin alters that
|
|
112
|
+
list, `script/parse-examples` rewrites it; review its diff before committing;
|
|
113
|
+
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear
|
|
114
|
+
time, since consumers parse files while they are being edited. It loads the Wasm build through
|
|
105
115
|
@willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
|
|
116
|
+
- a check (`test/unit/expressionKeywordParameter.test.ts`) that every keyword that can start an expression but not a
|
|
117
|
+
parameter is expected in the parameters of the function declarators that compete with a direct initialization, so
|
|
118
|
+
that `long(n)(sizeof(b));` stays an initialization;
|
|
119
|
+
- a check (`test/unit/queries.test.ts`) that the queries `tree-sitter.json` lists compile against the grammar, are
|
|
120
|
+
published in the npm package and the crate, and that `queries/c/` matches the queries of the installed
|
|
121
|
+
@willbooster/tree-sitter-c; after updating that dependency, `script/copy-c-queries` refreshes the copy;
|
|
122
|
+
- a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
|
|
123
|
+
`willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
|
|
124
|
+
tests, the fuzzer, and the CLI on the latter;
|
|
106
125
|
- checks that the Wasm build parses in Chromium (`test/unit/browser/`) and in Cloudflare Workers with and without
|
|
107
126
|
Node.js compatibility (`test/unit/workers.test.ts`). Run `bun run test/ci-setup` once to install Chromium.
|
|
108
127
|
|
|
109
|
-
CI also runs these tests on Linux arm64 and macOS, where the parser and scanner are compiled natively against each
|
|
110
|
-
(`.github/workflows/robustness.yml`).
|
|
128
|
+
CI also runs these tests on Linux arm64 and macOS, where the parser and scanner are compiled natively against each
|
|
129
|
+
platform's C library, and fuzzes the parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
|
|
111
130
|
|
|
112
131
|
### References
|
|
113
132
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-cpp",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "C++ grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -15,13 +15,14 @@
|
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"author": "WillBooster Inc.",
|
|
17
17
|
"files": [
|
|
18
|
+
"queries/**/*.scm",
|
|
18
19
|
"tree-sitter.json",
|
|
19
20
|
"src/node-types.json",
|
|
20
21
|
"*.wasm"
|
|
21
22
|
],
|
|
22
23
|
"scripts": {
|
|
23
|
-
"build-wasm": "tree-sitter build --wasm",
|
|
24
|
-
"build/ci": "tree-sitter generate && tree-sitter build --wasm",
|
|
24
|
+
"build-wasm": "script/tree-sitter build --wasm",
|
|
25
|
+
"build/ci": "script/tree-sitter generate && script/tree-sitter build --wasm",
|
|
25
26
|
"cleanup": "bun wb lint --fix --format",
|
|
26
27
|
"format": "bun wb lint --format",
|
|
27
28
|
"lint": "bun wb lint",
|
|
@@ -29,7 +30,7 @@
|
|
|
29
30
|
"prepare": "(bun lefthook install || true) && (test ! -f .lefthook/normalize-bun-lockfile.sh || bash .lefthook/normalize-bun-lockfile.sh)",
|
|
30
31
|
"release": "wb release",
|
|
31
32
|
"prestart": "bun run build-wasm",
|
|
32
|
-
"start": "tree-sitter playground",
|
|
33
|
+
"start": "script/tree-sitter playground",
|
|
33
34
|
"test": "bun wb test",
|
|
34
35
|
"test/ci": "bun wb test-on-ci",
|
|
35
36
|
"test/ci-setup": "bun playwright install --with-deps chromium",
|
|
@@ -43,14 +44,14 @@
|
|
|
43
44
|
"@vitest/browser-playwright": "5.0.1",
|
|
44
45
|
"@willbooster/oxfmt-config": "1.2.2",
|
|
45
46
|
"@willbooster/oxlint-config": "1.4.8",
|
|
46
|
-
"@willbooster/tree-sitter-c": "1.1.
|
|
47
|
-
"@willbooster/wb": "22.13.
|
|
48
|
-
"@willbooster/web-tree-sitter": "1.0.
|
|
47
|
+
"@willbooster/tree-sitter-c": "1.1.6",
|
|
48
|
+
"@willbooster/wb": "22.13.2",
|
|
49
|
+
"@willbooster/web-tree-sitter": "1.0.17",
|
|
49
50
|
"conventional-changelog-conventionalcommits": "10.4.0",
|
|
50
51
|
"lefthook": "2.1.14",
|
|
51
52
|
"oxfmt": "0.70.0",
|
|
52
53
|
"oxlint": "1.85.0",
|
|
53
|
-
"oxlint-tsgolint": "7.0.
|
|
54
|
+
"oxlint-tsgolint": "7.0.2003",
|
|
54
55
|
"playwright": "1.63.0",
|
|
55
56
|
"semantic-release": "25.0.9",
|
|
56
57
|
"sort-package-json": "4.0.0",
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
(identifier) @variable
|
|
2
|
+
|
|
3
|
+
((identifier) @constant
|
|
4
|
+
(#match? @constant "^[A-Z][A-Z\\d_]*$"))
|
|
5
|
+
|
|
6
|
+
"break" @keyword
|
|
7
|
+
"case" @keyword
|
|
8
|
+
"const" @keyword
|
|
9
|
+
"continue" @keyword
|
|
10
|
+
"default" @keyword
|
|
11
|
+
"do" @keyword
|
|
12
|
+
"else" @keyword
|
|
13
|
+
"enum" @keyword
|
|
14
|
+
"extern" @keyword
|
|
15
|
+
"for" @keyword
|
|
16
|
+
"if" @keyword
|
|
17
|
+
"inline" @keyword
|
|
18
|
+
"return" @keyword
|
|
19
|
+
"sizeof" @keyword
|
|
20
|
+
"static" @keyword
|
|
21
|
+
"struct" @keyword
|
|
22
|
+
"switch" @keyword
|
|
23
|
+
"typedef" @keyword
|
|
24
|
+
"union" @keyword
|
|
25
|
+
"volatile" @keyword
|
|
26
|
+
"while" @keyword
|
|
27
|
+
|
|
28
|
+
"#define" @keyword
|
|
29
|
+
"#elif" @keyword
|
|
30
|
+
"#else" @keyword
|
|
31
|
+
"#endif" @keyword
|
|
32
|
+
"#if" @keyword
|
|
33
|
+
"#ifdef" @keyword
|
|
34
|
+
"#ifndef" @keyword
|
|
35
|
+
"#include" @keyword
|
|
36
|
+
(preproc_directive) @keyword
|
|
37
|
+
|
|
38
|
+
"--" @operator
|
|
39
|
+
"-" @operator
|
|
40
|
+
"-=" @operator
|
|
41
|
+
"->" @operator
|
|
42
|
+
"=" @operator
|
|
43
|
+
"!=" @operator
|
|
44
|
+
"*" @operator
|
|
45
|
+
"&" @operator
|
|
46
|
+
"&&" @operator
|
|
47
|
+
"+" @operator
|
|
48
|
+
"++" @operator
|
|
49
|
+
"+=" @operator
|
|
50
|
+
"<" @operator
|
|
51
|
+
"==" @operator
|
|
52
|
+
">" @operator
|
|
53
|
+
"||" @operator
|
|
54
|
+
|
|
55
|
+
"." @delimiter
|
|
56
|
+
";" @delimiter
|
|
57
|
+
|
|
58
|
+
(string_literal) @string
|
|
59
|
+
(system_lib_string) @string
|
|
60
|
+
|
|
61
|
+
(null) @constant
|
|
62
|
+
(number_literal) @number
|
|
63
|
+
(char_literal) @number
|
|
64
|
+
|
|
65
|
+
(field_identifier) @property
|
|
66
|
+
(statement_identifier) @label
|
|
67
|
+
(type_identifier) @type
|
|
68
|
+
(primitive_type) @type
|
|
69
|
+
(sized_type_specifier) @type
|
|
70
|
+
|
|
71
|
+
(call_expression
|
|
72
|
+
function: (identifier) @function)
|
|
73
|
+
(call_expression
|
|
74
|
+
function: (field_expression
|
|
75
|
+
field: (field_identifier) @function))
|
|
76
|
+
(function_declarator
|
|
77
|
+
declarator: (identifier) @function)
|
|
78
|
+
(preproc_function_def
|
|
79
|
+
name: (identifier) @function.special)
|
|
80
|
+
|
|
81
|
+
(comment) @comment
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
; inherits: c
|
|
2
|
+
|
|
3
|
+
; Functions
|
|
4
|
+
|
|
5
|
+
(call_expression
|
|
6
|
+
function: (qualified_identifier
|
|
7
|
+
name: (identifier) @function))
|
|
8
|
+
|
|
9
|
+
(template_function
|
|
10
|
+
name: (identifier) @function)
|
|
11
|
+
|
|
12
|
+
(template_method
|
|
13
|
+
name: (field_identifier) @function)
|
|
14
|
+
|
|
15
|
+
(template_function
|
|
16
|
+
name: (identifier) @function)
|
|
17
|
+
|
|
18
|
+
(function_declarator
|
|
19
|
+
declarator: (qualified_identifier
|
|
20
|
+
name: (identifier) @function))
|
|
21
|
+
|
|
22
|
+
(function_declarator
|
|
23
|
+
declarator: (field_identifier) @function)
|
|
24
|
+
|
|
25
|
+
; Types
|
|
26
|
+
|
|
27
|
+
((namespace_identifier) @type
|
|
28
|
+
(#match? @type "^[A-Z]"))
|
|
29
|
+
|
|
30
|
+
(auto) @type
|
|
31
|
+
|
|
32
|
+
; Constants
|
|
33
|
+
|
|
34
|
+
(this) @variable.builtin
|
|
35
|
+
(null "nullptr" @constant)
|
|
36
|
+
|
|
37
|
+
; Modules
|
|
38
|
+
(module_name
|
|
39
|
+
(identifier) @module)
|
|
40
|
+
|
|
41
|
+
; Keywords
|
|
42
|
+
|
|
43
|
+
[
|
|
44
|
+
"catch"
|
|
45
|
+
"class"
|
|
46
|
+
"co_await"
|
|
47
|
+
"co_return"
|
|
48
|
+
"co_yield"
|
|
49
|
+
"constexpr"
|
|
50
|
+
"constinit"
|
|
51
|
+
"consteval"
|
|
52
|
+
"delete"
|
|
53
|
+
"explicit"
|
|
54
|
+
"final"
|
|
55
|
+
"friend"
|
|
56
|
+
"mutable"
|
|
57
|
+
"namespace"
|
|
58
|
+
"noexcept"
|
|
59
|
+
"new"
|
|
60
|
+
"override"
|
|
61
|
+
"private"
|
|
62
|
+
"protected"
|
|
63
|
+
"public"
|
|
64
|
+
"template"
|
|
65
|
+
"throw"
|
|
66
|
+
"try"
|
|
67
|
+
"typename"
|
|
68
|
+
"using"
|
|
69
|
+
"concept"
|
|
70
|
+
"requires"
|
|
71
|
+
"virtual"
|
|
72
|
+
"import"
|
|
73
|
+
"export"
|
|
74
|
+
"module"
|
|
75
|
+
] @keyword
|
|
76
|
+
|
|
77
|
+
; Strings
|
|
78
|
+
|
|
79
|
+
(raw_string_literal) @string
|
|
80
|
+
|
|
81
|
+
; Keywords with the shape of a call
|
|
82
|
+
|
|
83
|
+
((template_function
|
|
84
|
+
name: (identifier) @keyword)
|
|
85
|
+
(#any-of? @keyword "static_cast" "dynamic_cast" "const_cast" "reinterpret_cast"))
|
|
86
|
+
|
|
87
|
+
((call_expression
|
|
88
|
+
function: (identifier) @keyword)
|
|
89
|
+
(#any-of? @keyword "typeid" "noexcept"))
|
package/queries/tags.scm
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
(struct_specifier name: (type_identifier) @name body:(_)) @definition.class
|
|
2
|
+
|
|
3
|
+
(declaration type: (union_specifier name: (type_identifier) @name)) @definition.class
|
|
4
|
+
|
|
5
|
+
(function_declarator declarator: (identifier) @name) @definition.function
|
|
6
|
+
|
|
7
|
+
(function_declarator declarator: (field_identifier) @name) @definition.function
|
|
8
|
+
|
|
9
|
+
(function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @local.scope name: (identifier) @name)) @definition.method
|
|
10
|
+
|
|
11
|
+
(type_definition declarator: (type_identifier) @name) @definition.type
|
|
12
|
+
|
|
13
|
+
(enum_specifier name: (type_identifier) @name) @definition.type
|
|
14
|
+
|
|
15
|
+
(class_specifier name: (type_identifier) @name) @definition.class
|
package/src/node-types.json
CHANGED
package/tree-sitter-cpp.wasm
CHANGED
|
Binary file
|
package/tree-sitter.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"h"
|
|
16
16
|
],
|
|
17
17
|
"highlights": [
|
|
18
|
-
"
|
|
18
|
+
"queries/c/highlights.scm",
|
|
19
19
|
"queries/highlights.scm"
|
|
20
20
|
],
|
|
21
21
|
"injections": "queries/injections.scm",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
}
|
|
25
25
|
],
|
|
26
26
|
"metadata": {
|
|
27
|
-
"version": "1.2.
|
|
27
|
+
"version": "1.2.3",
|
|
28
28
|
"license": "MIT",
|
|
29
29
|
"description": "C++ grammar for tree-sitter",
|
|
30
30
|
"authors": [
|