tree-sitter-metal 0.1.0__tar.gz
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.
- tree_sitter_metal-0.1.0/Cargo.toml +34 -0
- tree_sitter_metal-0.1.0/LICENSE +22 -0
- tree_sitter_metal-0.1.0/MANIFEST.in +14 -0
- tree_sitter_metal-0.1.0/PKG-INFO +150 -0
- tree_sitter_metal-0.1.0/README.md +105 -0
- tree_sitter_metal-0.1.0/binding.gyp +35 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal/__init__.py +43 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal/__init__.pyi +6 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal/binding.c +36 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal/py.typed +0 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal.egg-info/PKG-INFO +150 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal.egg-info/SOURCES.txt +28 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal.egg-info/dependency_links.txt +1 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal.egg-info/not-zip-safe +1 -0
- tree_sitter_metal-0.1.0/bindings/python/tree_sitter_metal.egg-info/top_level.txt +2 -0
- tree_sitter_metal-0.1.0/grammar.js +140 -0
- tree_sitter_metal-0.1.0/package.json +40 -0
- tree_sitter_metal-0.1.0/setup.cfg +4 -0
- tree_sitter_metal-0.1.0/setup.py +93 -0
- tree_sitter_metal-0.1.0/src/parser.c +549441 -0
- tree_sitter_metal-0.1.0/src/scanner.c +156 -0
- tree_sitter_metal-0.1.0/src/tree_sitter/alloc.h +54 -0
- tree_sitter_metal-0.1.0/src/tree_sitter/array.h +290 -0
- tree_sitter_metal-0.1.0/src/tree_sitter/parser.h +265 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "tree-sitter-metal"
|
|
3
|
+
description = "tree-sitter grammar for Metal Shading Langage"
|
|
4
|
+
version = "0.1.0"
|
|
5
|
+
authors = ["Sasha Krassovsky <krassovskysasha@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
keywords = ["incremental", "parsing", "tree-sitter", "metal"]
|
|
9
|
+
categories = ["parser-implementations", "parsing", "text-editors"]
|
|
10
|
+
repository = "https://github.com/save-buffer/tree-sitter-metal"
|
|
11
|
+
edition = "2021"
|
|
12
|
+
autoexamples = false
|
|
13
|
+
|
|
14
|
+
build = "bindings/rust/build.rs"
|
|
15
|
+
include = [
|
|
16
|
+
"bindings/rust/*",
|
|
17
|
+
"grammar.js",
|
|
18
|
+
"queries/*",
|
|
19
|
+
"src/*",
|
|
20
|
+
"tree-sitter.json",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[lib]
|
|
25
|
+
path = "bindings/rust/lib.rs"
|
|
26
|
+
|
|
27
|
+
[dependencies]
|
|
28
|
+
tree-sitter-language = "0.1"
|
|
29
|
+
|
|
30
|
+
[build-dependencies]
|
|
31
|
+
cc = "1.2"
|
|
32
|
+
|
|
33
|
+
[dev-dependencies]
|
|
34
|
+
tree-sitter = "0.25.4"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 save-buffer and tree-sitter-metal contributors
|
|
4
|
+
Copyright (c) 2014 Max Brunsfeld (tree-sitter-cpp, which this grammar extends)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# The generated parser and its headers are the source of the extension, so the
|
|
2
|
+
# sdist has to carry all of them: setuptools includes only the .c files by
|
|
3
|
+
# default, and without `parser.h`/`array.h` a `pip install` from the sdist fails
|
|
4
|
+
# with "tree_sitter/parser.h: No such file or directory".
|
|
5
|
+
recursive-include src *.c *.h
|
|
6
|
+
recursive-include src/tree_sitter *.h
|
|
7
|
+
recursive-include bindings/python/tree_sitter_metal *.c *.py *.pyi py.typed
|
|
8
|
+
recursive-include queries *.scm
|
|
9
|
+
include grammar.js
|
|
10
|
+
include binding.gyp
|
|
11
|
+
include package.json
|
|
12
|
+
include Cargo.toml
|
|
13
|
+
include LICENSE
|
|
14
|
+
include README.md
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tree-sitter-metal
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Metal Shading Language grammar for tree-sitter
|
|
5
|
+
Home-page: https://github.com/Time-Coder/tree-sitter-metal
|
|
6
|
+
Author: Binghui Wang
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Source, https://github.com/Time-Coder/tree-sitter-metal
|
|
9
|
+
Project-URL: Upstream grammar, https://github.com/save-buffer/tree-sitter-metal
|
|
10
|
+
Project-URL: Parser, https://github.com/Time-Coder/PyGPGPU
|
|
11
|
+
Keywords: tree-sitter metal msl grammar parser shader apple
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: MacOS
|
|
16
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
17
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
18
|
+
Classifier: Programming Language :: C
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
28
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
29
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
30
|
+
Classifier: Typing :: Typed
|
|
31
|
+
Requires-Python: >=3.7
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Dynamic: author
|
|
35
|
+
Dynamic: classifier
|
|
36
|
+
Dynamic: description
|
|
37
|
+
Dynamic: description-content-type
|
|
38
|
+
Dynamic: home-page
|
|
39
|
+
Dynamic: keywords
|
|
40
|
+
Dynamic: license
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
Dynamic: project-url
|
|
43
|
+
Dynamic: requires-python
|
|
44
|
+
Dynamic: summary
|
|
45
|
+
|
|
46
|
+
# tree-sitter-metal
|
|
47
|
+
|
|
48
|
+
A [tree-sitter](https://tree-sitter.github.io/) grammar for **Metal Shading
|
|
49
|
+
Language** (MSL), packaged for Python -- with an extension for the address-space
|
|
50
|
+
cast form MSL's atomics need.
|
|
51
|
+
|
|
52
|
+
This is the grammar [PyGPGPU](https://github.com/Time-Coder/PyGPGPU) reads its
|
|
53
|
+
Metal sources with. It exists as its own distribution because no MSL grammar was
|
|
54
|
+
installable from PyPI: the `tree-sitter-metal` names found on GitHub are Neovim
|
|
55
|
+
highlighting plugins with no grammar at all.
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
pip install tree-sitter-metal
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import tree_sitter, tree_sitter_metal
|
|
65
|
+
|
|
66
|
+
parser = tree_sitter.Parser()
|
|
67
|
+
parser.set_language(tree_sitter_metal.language()) # tree-sitter 0.21+
|
|
68
|
+
# legacy (0.20.x): parser.set_language(tree_sitter_metal.language())
|
|
69
|
+
tree = parser.parse(b"kernel void k(device float* a) { a[0] = 1.0f; }")
|
|
70
|
+
print(tree.root_node.has_error) # False
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`language()` answers a native `tree_sitter.Language` on both the legacy 0.20.x
|
|
74
|
+
line and 0.21+, so one wheel serves every supported interpreter -- see
|
|
75
|
+
[ABI 13](#abi-13-and-why) below.
|
|
76
|
+
|
|
77
|
+
## Origin
|
|
78
|
+
|
|
79
|
+
Built from [`save-buffer/tree-sitter-metal`](https://github.com/save-buffer/tree-sitter-metal),
|
|
80
|
+
a tree-sitter-cpp descendant that already handles MSL's scalar and vector types,
|
|
81
|
+
address-space words and `[[attributes]]`. Two changes are carried on top:
|
|
82
|
+
|
|
83
|
+
1. **An address space may qualify a cast's type** -- `(device atomic_uint*)&x[0]`.
|
|
84
|
+
The base grammar accepted those words only in a declaration position, so MSL's
|
|
85
|
+
atomics (which are all cast-and-update) parsed as `ERROR` nodes. The extension
|
|
86
|
+
adds a `type_descriptor` alternative taking a `storage_class_specifier` head,
|
|
87
|
+
wrapped in `prec.right` to settle the shift/reduce conflict.
|
|
88
|
+
2. The grammar is generated at **ABI 13** rather than the current 15.
|
|
89
|
+
|
|
90
|
+
The generated `src/parser.c` is committed, so building the wheel needs a C
|
|
91
|
+
compiler and nothing else.
|
|
92
|
+
|
|
93
|
+
## Building from source
|
|
94
|
+
|
|
95
|
+
The wheel is a plain setuptools extension:
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
pip install -e .
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
To regenerate `src/parser.c` after editing `grammar.js` you need the tree-sitter
|
|
102
|
+
CLI and the two grammars this one descends from:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
git clone https://github.com/tree-sitter/tree-sitter-cpp node_modules/tree-sitter-cpp
|
|
106
|
+
git clone https://github.com/tree-sitter/tree-sitter-c node_modules/tree-sitter-c
|
|
107
|
+
tree-sitter generate --abi 13
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**The CLI version matters.** `--abi 13` needs the 0.22.6 CLI: 0.27 refuses
|
|
111
|
+
anything below 14, and the CLI bundled with 0.20.x crashes under Node 24 while
|
|
112
|
+
loading `grammar.js`. Generate with 0.22.6 and a Node new enough to load a modern
|
|
113
|
+
`grammar.js` but old enough for that CLI -- Node 18 works:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
tree-sitter-0.22.6 generate --abi 13 --js-runtime /path/to/node18/bin/node
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### ABI 13, and why
|
|
120
|
+
|
|
121
|
+
[PyGPGPU](https://github.com/Time-Coder/PyGPGPU) supports Python 3.7, and the
|
|
122
|
+
last tree-sitter line with CPython 3.7 wheels is 0.20.4 -- which accepts
|
|
123
|
+
language ABI 13. A parser generated at 13 also loads on 0.21+, so **one wheel
|
|
124
|
+
serves CPython 3.7 through the current release**:
|
|
125
|
+
|
|
126
|
+
| interpreter | tree-sitter | loads |
|
|
127
|
+
| --- | --- | --- |
|
|
128
|
+
| CPython 3.7.9 | 0.20.4 | yes |
|
|
129
|
+
| CPython 3.14 | 0.25.2 | yes |
|
|
130
|
+
|
|
131
|
+
A parser generated at ABI 14 or 15 would be invisible to every 3.7 interpreter.
|
|
132
|
+
|
|
133
|
+
## Building the wheel matrix
|
|
134
|
+
|
|
135
|
+
`build_matrix.py` builds a wheel per interpreter and one sdist, and
|
|
136
|
+
`verify_dist.py` checks what came out:
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
python build_matrix.py # edit INTERPRETERS at the top first
|
|
140
|
+
python verify_dist.py dist
|
|
141
|
+
python test_wheels.py dist # imports each wheel in its own interpreter
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`upload.py` uploads the wheels with `twine`.
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT. See [LICENSE](LICENSE). The grammar descends from
|
|
149
|
+
[tree-sitter-cpp](https://github.com/tree-sitter/tree-sitter-cpp) and
|
|
150
|
+
[tree-sitter-c](https://github.com/tree-sitter/tree-sitter-c), both MIT.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# tree-sitter-metal
|
|
2
|
+
|
|
3
|
+
A [tree-sitter](https://tree-sitter.github.io/) grammar for **Metal Shading
|
|
4
|
+
Language** (MSL), packaged for Python -- with an extension for the address-space
|
|
5
|
+
cast form MSL's atomics need.
|
|
6
|
+
|
|
7
|
+
This is the grammar [PyGPGPU](https://github.com/Time-Coder/PyGPGPU) reads its
|
|
8
|
+
Metal sources with. It exists as its own distribution because no MSL grammar was
|
|
9
|
+
installable from PyPI: the `tree-sitter-metal` names found on GitHub are Neovim
|
|
10
|
+
highlighting plugins with no grammar at all.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pip install tree-sitter-metal
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
import tree_sitter, tree_sitter_metal
|
|
20
|
+
|
|
21
|
+
parser = tree_sitter.Parser()
|
|
22
|
+
parser.set_language(tree_sitter_metal.language()) # tree-sitter 0.21+
|
|
23
|
+
# legacy (0.20.x): parser.set_language(tree_sitter_metal.language())
|
|
24
|
+
tree = parser.parse(b"kernel void k(device float* a) { a[0] = 1.0f; }")
|
|
25
|
+
print(tree.root_node.has_error) # False
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`language()` answers a native `tree_sitter.Language` on both the legacy 0.20.x
|
|
29
|
+
line and 0.21+, so one wheel serves every supported interpreter -- see
|
|
30
|
+
[ABI 13](#abi-13-and-why) below.
|
|
31
|
+
|
|
32
|
+
## Origin
|
|
33
|
+
|
|
34
|
+
Built from [`save-buffer/tree-sitter-metal`](https://github.com/save-buffer/tree-sitter-metal),
|
|
35
|
+
a tree-sitter-cpp descendant that already handles MSL's scalar and vector types,
|
|
36
|
+
address-space words and `[[attributes]]`. Two changes are carried on top:
|
|
37
|
+
|
|
38
|
+
1. **An address space may qualify a cast's type** -- `(device atomic_uint*)&x[0]`.
|
|
39
|
+
The base grammar accepted those words only in a declaration position, so MSL's
|
|
40
|
+
atomics (which are all cast-and-update) parsed as `ERROR` nodes. The extension
|
|
41
|
+
adds a `type_descriptor` alternative taking a `storage_class_specifier` head,
|
|
42
|
+
wrapped in `prec.right` to settle the shift/reduce conflict.
|
|
43
|
+
2. The grammar is generated at **ABI 13** rather than the current 15.
|
|
44
|
+
|
|
45
|
+
The generated `src/parser.c` is committed, so building the wheel needs a C
|
|
46
|
+
compiler and nothing else.
|
|
47
|
+
|
|
48
|
+
## Building from source
|
|
49
|
+
|
|
50
|
+
The wheel is a plain setuptools extension:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install -e .
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
To regenerate `src/parser.c` after editing `grammar.js` you need the tree-sitter
|
|
57
|
+
CLI and the two grammars this one descends from:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
git clone https://github.com/tree-sitter/tree-sitter-cpp node_modules/tree-sitter-cpp
|
|
61
|
+
git clone https://github.com/tree-sitter/tree-sitter-c node_modules/tree-sitter-c
|
|
62
|
+
tree-sitter generate --abi 13
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**The CLI version matters.** `--abi 13` needs the 0.22.6 CLI: 0.27 refuses
|
|
66
|
+
anything below 14, and the CLI bundled with 0.20.x crashes under Node 24 while
|
|
67
|
+
loading `grammar.js`. Generate with 0.22.6 and a Node new enough to load a modern
|
|
68
|
+
`grammar.js` but old enough for that CLI -- Node 18 works:
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
tree-sitter-0.22.6 generate --abi 13 --js-runtime /path/to/node18/bin/node
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### ABI 13, and why
|
|
75
|
+
|
|
76
|
+
[PyGPGPU](https://github.com/Time-Coder/PyGPGPU) supports Python 3.7, and the
|
|
77
|
+
last tree-sitter line with CPython 3.7 wheels is 0.20.4 -- which accepts
|
|
78
|
+
language ABI 13. A parser generated at 13 also loads on 0.21+, so **one wheel
|
|
79
|
+
serves CPython 3.7 through the current release**:
|
|
80
|
+
|
|
81
|
+
| interpreter | tree-sitter | loads |
|
|
82
|
+
| --- | --- | --- |
|
|
83
|
+
| CPython 3.7.9 | 0.20.4 | yes |
|
|
84
|
+
| CPython 3.14 | 0.25.2 | yes |
|
|
85
|
+
|
|
86
|
+
A parser generated at ABI 14 or 15 would be invisible to every 3.7 interpreter.
|
|
87
|
+
|
|
88
|
+
## Building the wheel matrix
|
|
89
|
+
|
|
90
|
+
`build_matrix.py` builds a wheel per interpreter and one sdist, and
|
|
91
|
+
`verify_dist.py` checks what came out:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
python build_matrix.py # edit INTERPRETERS at the top first
|
|
95
|
+
python verify_dist.py dist
|
|
96
|
+
python test_wheels.py dist # imports each wheel in its own interpreter
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`upload.py` uploads the wheels with `twine`.
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT. See [LICENSE](LICENSE). The grammar descends from
|
|
104
|
+
[tree-sitter-cpp](https://github.com/tree-sitter/tree-sitter-cpp) and
|
|
105
|
+
[tree-sitter-c](https://github.com/tree-sitter/tree-sitter-c), both MIT.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"targets": [
|
|
3
|
+
{
|
|
4
|
+
"target_name": "tree_sitter_metal_binding",
|
|
5
|
+
"dependencies": [
|
|
6
|
+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
|
|
7
|
+
],
|
|
8
|
+
"include_dirs": [
|
|
9
|
+
"src",
|
|
10
|
+
],
|
|
11
|
+
"sources": [
|
|
12
|
+
"bindings/node/binding.cc",
|
|
13
|
+
"src/parser.c",
|
|
14
|
+
],
|
|
15
|
+
"variables": {
|
|
16
|
+
"has_scanner": "<!(node -p \"fs.existsSync('src/scanner.c')\")"
|
|
17
|
+
},
|
|
18
|
+
"conditions": [
|
|
19
|
+
["has_scanner=='true'", {
|
|
20
|
+
"sources+": ["src/scanner.c"],
|
|
21
|
+
}],
|
|
22
|
+
["OS!='win'", {
|
|
23
|
+
"cflags_c": [
|
|
24
|
+
"-std=c11",
|
|
25
|
+
],
|
|
26
|
+
}, { # OS == "win"
|
|
27
|
+
"cflags_c": [
|
|
28
|
+
"/std:c11",
|
|
29
|
+
"/utf-8",
|
|
30
|
+
],
|
|
31
|
+
}],
|
|
32
|
+
],
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Metal Shading Language grammar for tree-sitter
|
|
2
|
+
|
|
3
|
+
``language()`` returns a native ``tree_sitter.Language`` object that is
|
|
4
|
+
compatible with both the legacy tree-sitter 0.20.x line (the last line that
|
|
5
|
+
ships CPython 3.7+ wheels) and the modern tree-sitter 0.21+ line.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import tree_sitter
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _library():
|
|
12
|
+
"""Resolve the shared-extension file backing this package."""
|
|
13
|
+
from . import _binding
|
|
14
|
+
|
|
15
|
+
return _binding.__file__
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def language():
|
|
19
|
+
"""Return a tree-sitter Language object for Metal Shading Language.
|
|
20
|
+
|
|
21
|
+
On the legacy tree-sitter 0.20.x line the grammar module exports the raw
|
|
22
|
+
``tree_sitter_metal`` symbol, so the shared extension is loaded directly
|
|
23
|
+
through ``ctypes``. On the modern line the ``_binding`` module returns a
|
|
24
|
+
``PyCapsule`` that ``tree_sitter.Language`` wraps. Both forms return the
|
|
25
|
+
same native ``tree_sitter.Language`` stream so callers can hand it to a
|
|
26
|
+
``tree_sitter.Parser`` regardless of the installed tree-sitter version.
|
|
27
|
+
"""
|
|
28
|
+
try:
|
|
29
|
+
# Modern path: _binding.language() returns a PyCapsule.
|
|
30
|
+
from ._binding import language as _lang_capsule
|
|
31
|
+
except (AttributeError, ImportError): # pragma: no cover - defensive
|
|
32
|
+
_lang_capsule = None
|
|
33
|
+
|
|
34
|
+
if _lang_capsule is not None:
|
|
35
|
+
# 0.21+ wraps the capsule with Language(capsule).
|
|
36
|
+
try:
|
|
37
|
+
return tree_sitter.Language(_lang_capsule())
|
|
38
|
+
except (TypeError, ValueError):
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
# Legacy path: tree-sitter 0.20.x Language(library_path, name) loads the
|
|
42
|
+
# dynamic library and looks up the tree_sitter_metal symbol.
|
|
43
|
+
return tree_sitter.Language(_library(), "metal")
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#include <Python.h>
|
|
2
|
+
|
|
3
|
+
typedef struct TSLanguage TSLanguage;
|
|
4
|
+
|
|
5
|
+
TSLanguage *tree_sitter_metal(void);
|
|
6
|
+
|
|
7
|
+
static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
|
|
8
|
+
return PyCapsule_New(tree_sitter_metal(), "tree_sitter.Language", NULL);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static struct PyModuleDef_Slot slots[] = {
|
|
12
|
+
#ifdef Py_GIL_DISABLED
|
|
13
|
+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
|
14
|
+
#endif
|
|
15
|
+
{0, NULL}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
static PyMethodDef methods[] = {
|
|
19
|
+
{"language", _binding_language, METH_NOARGS,
|
|
20
|
+
"Get the tree-sitter language for this grammar."},
|
|
21
|
+
{NULL, NULL, 0, NULL}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static struct PyModuleDef module = {
|
|
25
|
+
.m_base = PyModuleDef_HEAD_INIT,
|
|
26
|
+
.m_name = "_binding",
|
|
27
|
+
.m_doc = NULL,
|
|
28
|
+
.m_size = 0,
|
|
29
|
+
.m_methods = methods,
|
|
30
|
+
.m_slots = slots,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
PyMODINIT_FUNC PyInit__binding(void) {
|
|
34
|
+
return PyModuleDef_Init(&module);
|
|
35
|
+
}
|
|
36
|
+
|
|
File without changes
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tree-sitter-metal
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Metal Shading Language grammar for tree-sitter
|
|
5
|
+
Home-page: https://github.com/Time-Coder/tree-sitter-metal
|
|
6
|
+
Author: Binghui Wang
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Source, https://github.com/Time-Coder/tree-sitter-metal
|
|
9
|
+
Project-URL: Upstream grammar, https://github.com/save-buffer/tree-sitter-metal
|
|
10
|
+
Project-URL: Parser, https://github.com/Time-Coder/PyGPGPU
|
|
11
|
+
Keywords: tree-sitter metal msl grammar parser shader apple
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: MacOS
|
|
16
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
17
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
18
|
+
Classifier: Programming Language :: C
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
28
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
29
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
30
|
+
Classifier: Typing :: Typed
|
|
31
|
+
Requires-Python: >=3.7
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Dynamic: author
|
|
35
|
+
Dynamic: classifier
|
|
36
|
+
Dynamic: description
|
|
37
|
+
Dynamic: description-content-type
|
|
38
|
+
Dynamic: home-page
|
|
39
|
+
Dynamic: keywords
|
|
40
|
+
Dynamic: license
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
Dynamic: project-url
|
|
43
|
+
Dynamic: requires-python
|
|
44
|
+
Dynamic: summary
|
|
45
|
+
|
|
46
|
+
# tree-sitter-metal
|
|
47
|
+
|
|
48
|
+
A [tree-sitter](https://tree-sitter.github.io/) grammar for **Metal Shading
|
|
49
|
+
Language** (MSL), packaged for Python -- with an extension for the address-space
|
|
50
|
+
cast form MSL's atomics need.
|
|
51
|
+
|
|
52
|
+
This is the grammar [PyGPGPU](https://github.com/Time-Coder/PyGPGPU) reads its
|
|
53
|
+
Metal sources with. It exists as its own distribution because no MSL grammar was
|
|
54
|
+
installable from PyPI: the `tree-sitter-metal` names found on GitHub are Neovim
|
|
55
|
+
highlighting plugins with no grammar at all.
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
pip install tree-sitter-metal
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import tree_sitter, tree_sitter_metal
|
|
65
|
+
|
|
66
|
+
parser = tree_sitter.Parser()
|
|
67
|
+
parser.set_language(tree_sitter_metal.language()) # tree-sitter 0.21+
|
|
68
|
+
# legacy (0.20.x): parser.set_language(tree_sitter_metal.language())
|
|
69
|
+
tree = parser.parse(b"kernel void k(device float* a) { a[0] = 1.0f; }")
|
|
70
|
+
print(tree.root_node.has_error) # False
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`language()` answers a native `tree_sitter.Language` on both the legacy 0.20.x
|
|
74
|
+
line and 0.21+, so one wheel serves every supported interpreter -- see
|
|
75
|
+
[ABI 13](#abi-13-and-why) below.
|
|
76
|
+
|
|
77
|
+
## Origin
|
|
78
|
+
|
|
79
|
+
Built from [`save-buffer/tree-sitter-metal`](https://github.com/save-buffer/tree-sitter-metal),
|
|
80
|
+
a tree-sitter-cpp descendant that already handles MSL's scalar and vector types,
|
|
81
|
+
address-space words and `[[attributes]]`. Two changes are carried on top:
|
|
82
|
+
|
|
83
|
+
1. **An address space may qualify a cast's type** -- `(device atomic_uint*)&x[0]`.
|
|
84
|
+
The base grammar accepted those words only in a declaration position, so MSL's
|
|
85
|
+
atomics (which are all cast-and-update) parsed as `ERROR` nodes. The extension
|
|
86
|
+
adds a `type_descriptor` alternative taking a `storage_class_specifier` head,
|
|
87
|
+
wrapped in `prec.right` to settle the shift/reduce conflict.
|
|
88
|
+
2. The grammar is generated at **ABI 13** rather than the current 15.
|
|
89
|
+
|
|
90
|
+
The generated `src/parser.c` is committed, so building the wheel needs a C
|
|
91
|
+
compiler and nothing else.
|
|
92
|
+
|
|
93
|
+
## Building from source
|
|
94
|
+
|
|
95
|
+
The wheel is a plain setuptools extension:
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
pip install -e .
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
To regenerate `src/parser.c` after editing `grammar.js` you need the tree-sitter
|
|
102
|
+
CLI and the two grammars this one descends from:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
git clone https://github.com/tree-sitter/tree-sitter-cpp node_modules/tree-sitter-cpp
|
|
106
|
+
git clone https://github.com/tree-sitter/tree-sitter-c node_modules/tree-sitter-c
|
|
107
|
+
tree-sitter generate --abi 13
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**The CLI version matters.** `--abi 13` needs the 0.22.6 CLI: 0.27 refuses
|
|
111
|
+
anything below 14, and the CLI bundled with 0.20.x crashes under Node 24 while
|
|
112
|
+
loading `grammar.js`. Generate with 0.22.6 and a Node new enough to load a modern
|
|
113
|
+
`grammar.js` but old enough for that CLI -- Node 18 works:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
tree-sitter-0.22.6 generate --abi 13 --js-runtime /path/to/node18/bin/node
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### ABI 13, and why
|
|
120
|
+
|
|
121
|
+
[PyGPGPU](https://github.com/Time-Coder/PyGPGPU) supports Python 3.7, and the
|
|
122
|
+
last tree-sitter line with CPython 3.7 wheels is 0.20.4 -- which accepts
|
|
123
|
+
language ABI 13. A parser generated at 13 also loads on 0.21+, so **one wheel
|
|
124
|
+
serves CPython 3.7 through the current release**:
|
|
125
|
+
|
|
126
|
+
| interpreter | tree-sitter | loads |
|
|
127
|
+
| --- | --- | --- |
|
|
128
|
+
| CPython 3.7.9 | 0.20.4 | yes |
|
|
129
|
+
| CPython 3.14 | 0.25.2 | yes |
|
|
130
|
+
|
|
131
|
+
A parser generated at ABI 14 or 15 would be invisible to every 3.7 interpreter.
|
|
132
|
+
|
|
133
|
+
## Building the wheel matrix
|
|
134
|
+
|
|
135
|
+
`build_matrix.py` builds a wheel per interpreter and one sdist, and
|
|
136
|
+
`verify_dist.py` checks what came out:
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
python build_matrix.py # edit INTERPRETERS at the top first
|
|
140
|
+
python verify_dist.py dist
|
|
141
|
+
python test_wheels.py dist # imports each wheel in its own interpreter
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`upload.py` uploads the wheels with `twine`.
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT. See [LICENSE](LICENSE). The grammar descends from
|
|
149
|
+
[tree-sitter-cpp](https://github.com/tree-sitter/tree-sitter-cpp) and
|
|
150
|
+
[tree-sitter-c](https://github.com/tree-sitter/tree-sitter-c), both MIT.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Cargo.toml
|
|
2
|
+
LICENSE
|
|
3
|
+
MANIFEST.in
|
|
4
|
+
README.md
|
|
5
|
+
binding.gyp
|
|
6
|
+
grammar.js
|
|
7
|
+
package.json
|
|
8
|
+
setup.py
|
|
9
|
+
bindings/python/tree_sitter_metal/binding.c
|
|
10
|
+
bindings/python/tree_sitter_metal.egg-info/PKG-INFO
|
|
11
|
+
bindings/python/tree_sitter_metal.egg-info/SOURCES.txt
|
|
12
|
+
bindings/python/tree_sitter_metal.egg-info/dependency_links.txt
|
|
13
|
+
bindings/python/tree_sitter_metal.egg-info/not-zip-safe
|
|
14
|
+
bindings/python/tree_sitter_metal.egg-info/top_level.txt
|
|
15
|
+
bindings/python/tree_sitter_metal/__init__.py
|
|
16
|
+
bindings/python/tree_sitter_metal/__init__.pyi
|
|
17
|
+
bindings/python/tree_sitter_metal/binding.c
|
|
18
|
+
bindings/python/tree_sitter_metal/py.typed
|
|
19
|
+
bindings/python/tree_sitter_metal.egg-info/PKG-INFO
|
|
20
|
+
bindings/python/tree_sitter_metal.egg-info/SOURCES.txt
|
|
21
|
+
bindings/python/tree_sitter_metal.egg-info/dependency_links.txt
|
|
22
|
+
bindings/python/tree_sitter_metal.egg-info/not-zip-safe
|
|
23
|
+
bindings/python/tree_sitter_metal.egg-info/top_level.txt
|
|
24
|
+
src/parser.c
|
|
25
|
+
src/scanner.c
|
|
26
|
+
src/tree_sitter/alloc.h
|
|
27
|
+
src/tree_sitter/array.h
|
|
28
|
+
src/tree_sitter/parser.h
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|