tree_stump 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 91b315adc731ea115495d57d4b6cbc0015ed57bbc57da2efc912db78546f466d
4
+ data.tar.gz: 496323196882dc02b34b4e77e7fc84bf91d4fea0d8f52545e843f13ca2a47ca2
5
+ SHA512:
6
+ metadata.gz: 21a4ad35fb28d48167ec9196832c8fb4bec7d941adf56666043818db6cf5423fc21d9a14ca54adb6a584e5700bc7305d4f6b04146c6116853a627c247e22bc20
7
+ data.tar.gz: 15198290a857e5ca222da0c5d570c0ad753cf53fba444b267850742bd1235c5df7b87c02cb83e20d432afdbeab788547332b2e1cbfb4afdbc85944eeb16a2bf3
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Cargo.lock ADDED
@@ -0,0 +1,368 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 3
4
+
5
+ [[package]]
6
+ name = "aho-corasick"
7
+ version = "1.1.3"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
10
+ dependencies = [
11
+ "memchr",
12
+ ]
13
+
14
+ [[package]]
15
+ name = "bindgen"
16
+ version = "0.69.4"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+ checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0"
19
+ dependencies = [
20
+ "bitflags",
21
+ "cexpr",
22
+ "clang-sys",
23
+ "itertools",
24
+ "lazy_static",
25
+ "lazycell",
26
+ "proc-macro2",
27
+ "quote",
28
+ "regex",
29
+ "rustc-hash",
30
+ "shlex",
31
+ "syn",
32
+ ]
33
+
34
+ [[package]]
35
+ name = "bitflags"
36
+ version = "2.6.0"
37
+ source = "registry+https://github.com/rust-lang/crates.io-index"
38
+ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
39
+
40
+ [[package]]
41
+ name = "cc"
42
+ version = "1.1.15"
43
+ source = "registry+https://github.com/rust-lang/crates.io-index"
44
+ checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6"
45
+ dependencies = [
46
+ "shlex",
47
+ ]
48
+
49
+ [[package]]
50
+ name = "cexpr"
51
+ version = "0.6.0"
52
+ source = "registry+https://github.com/rust-lang/crates.io-index"
53
+ checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
54
+ dependencies = [
55
+ "nom",
56
+ ]
57
+
58
+ [[package]]
59
+ name = "cfg-if"
60
+ version = "1.0.0"
61
+ source = "registry+https://github.com/rust-lang/crates.io-index"
62
+ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
63
+
64
+ [[package]]
65
+ name = "clang-sys"
66
+ version = "1.8.1"
67
+ source = "registry+https://github.com/rust-lang/crates.io-index"
68
+ checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
69
+ dependencies = [
70
+ "glob",
71
+ "libc",
72
+ "libloading",
73
+ ]
74
+
75
+ [[package]]
76
+ name = "either"
77
+ version = "1.13.0"
78
+ source = "registry+https://github.com/rust-lang/crates.io-index"
79
+ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
80
+
81
+ [[package]]
82
+ name = "glob"
83
+ version = "0.3.1"
84
+ source = "registry+https://github.com/rust-lang/crates.io-index"
85
+ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
86
+
87
+ [[package]]
88
+ name = "itertools"
89
+ version = "0.12.1"
90
+ source = "registry+https://github.com/rust-lang/crates.io-index"
91
+ checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
92
+ dependencies = [
93
+ "either",
94
+ ]
95
+
96
+ [[package]]
97
+ name = "lazy_static"
98
+ version = "1.5.0"
99
+ source = "registry+https://github.com/rust-lang/crates.io-index"
100
+ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
101
+
102
+ [[package]]
103
+ name = "lazycell"
104
+ version = "1.3.0"
105
+ source = "registry+https://github.com/rust-lang/crates.io-index"
106
+ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
107
+
108
+ [[package]]
109
+ name = "libc"
110
+ version = "0.2.158"
111
+ source = "registry+https://github.com/rust-lang/crates.io-index"
112
+ checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
113
+
114
+ [[package]]
115
+ name = "libloading"
116
+ version = "0.8.5"
117
+ source = "registry+https://github.com/rust-lang/crates.io-index"
118
+ checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
119
+ dependencies = [
120
+ "cfg-if",
121
+ "windows-targets",
122
+ ]
123
+
124
+ [[package]]
125
+ name = "magnus"
126
+ version = "0.7.1"
127
+ source = "registry+https://github.com/rust-lang/crates.io-index"
128
+ checksum = "3d87ae53030f3a22e83879e666cb94e58a7bdf31706878a0ba48752994146dab"
129
+ dependencies = [
130
+ "magnus-macros",
131
+ "rb-sys",
132
+ "rb-sys-env",
133
+ "seq-macro",
134
+ ]
135
+
136
+ [[package]]
137
+ name = "magnus-macros"
138
+ version = "0.6.0"
139
+ source = "registry+https://github.com/rust-lang/crates.io-index"
140
+ checksum = "5968c820e2960565f647819f5928a42d6e874551cab9d88d75e3e0660d7f71e3"
141
+ dependencies = [
142
+ "proc-macro2",
143
+ "quote",
144
+ "syn",
145
+ ]
146
+
147
+ [[package]]
148
+ name = "memchr"
149
+ version = "2.7.4"
150
+ source = "registry+https://github.com/rust-lang/crates.io-index"
151
+ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
152
+
153
+ [[package]]
154
+ name = "minimal-lexical"
155
+ version = "0.2.1"
156
+ source = "registry+https://github.com/rust-lang/crates.io-index"
157
+ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
158
+
159
+ [[package]]
160
+ name = "nom"
161
+ version = "7.1.3"
162
+ source = "registry+https://github.com/rust-lang/crates.io-index"
163
+ checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
164
+ dependencies = [
165
+ "memchr",
166
+ "minimal-lexical",
167
+ ]
168
+
169
+ [[package]]
170
+ name = "proc-macro2"
171
+ version = "1.0.86"
172
+ source = "registry+https://github.com/rust-lang/crates.io-index"
173
+ checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
174
+ dependencies = [
175
+ "unicode-ident",
176
+ ]
177
+
178
+ [[package]]
179
+ name = "quote"
180
+ version = "1.0.37"
181
+ source = "registry+https://github.com/rust-lang/crates.io-index"
182
+ checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
183
+ dependencies = [
184
+ "proc-macro2",
185
+ ]
186
+
187
+ [[package]]
188
+ name = "rb-sys"
189
+ version = "0.9.102"
190
+ source = "registry+https://github.com/rust-lang/crates.io-index"
191
+ checksum = "df4dec4b1d304c3b308a2cd86b1216ea45dd4361f4e9fa056f108332d0a450c1"
192
+ dependencies = [
193
+ "rb-sys-build",
194
+ ]
195
+
196
+ [[package]]
197
+ name = "rb-sys-build"
198
+ version = "0.9.102"
199
+ source = "registry+https://github.com/rust-lang/crates.io-index"
200
+ checksum = "1d71de3e29d174b8fb17b5d4470f27d7aa2605f8a9d05fda0d3aeff30e05a570"
201
+ dependencies = [
202
+ "bindgen",
203
+ "lazy_static",
204
+ "proc-macro2",
205
+ "quote",
206
+ "regex",
207
+ "shell-words",
208
+ "syn",
209
+ ]
210
+
211
+ [[package]]
212
+ name = "rb-sys-env"
213
+ version = "0.1.2"
214
+ source = "registry+https://github.com/rust-lang/crates.io-index"
215
+ checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
216
+
217
+ [[package]]
218
+ name = "regex"
219
+ version = "1.10.6"
220
+ source = "registry+https://github.com/rust-lang/crates.io-index"
221
+ checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619"
222
+ dependencies = [
223
+ "aho-corasick",
224
+ "memchr",
225
+ "regex-automata",
226
+ "regex-syntax",
227
+ ]
228
+
229
+ [[package]]
230
+ name = "regex-automata"
231
+ version = "0.4.7"
232
+ source = "registry+https://github.com/rust-lang/crates.io-index"
233
+ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
234
+ dependencies = [
235
+ "aho-corasick",
236
+ "memchr",
237
+ "regex-syntax",
238
+ ]
239
+
240
+ [[package]]
241
+ name = "regex-syntax"
242
+ version = "0.8.4"
243
+ source = "registry+https://github.com/rust-lang/crates.io-index"
244
+ checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
245
+
246
+ [[package]]
247
+ name = "rustc-hash"
248
+ version = "1.1.0"
249
+ source = "registry+https://github.com/rust-lang/crates.io-index"
250
+ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
251
+
252
+ [[package]]
253
+ name = "seq-macro"
254
+ version = "0.3.5"
255
+ source = "registry+https://github.com/rust-lang/crates.io-index"
256
+ checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4"
257
+
258
+ [[package]]
259
+ name = "shell-words"
260
+ version = "1.1.0"
261
+ source = "registry+https://github.com/rust-lang/crates.io-index"
262
+ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
263
+
264
+ [[package]]
265
+ name = "shlex"
266
+ version = "1.3.0"
267
+ source = "registry+https://github.com/rust-lang/crates.io-index"
268
+ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
269
+
270
+ [[package]]
271
+ name = "syn"
272
+ version = "2.0.77"
273
+ source = "registry+https://github.com/rust-lang/crates.io-index"
274
+ checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
275
+ dependencies = [
276
+ "proc-macro2",
277
+ "quote",
278
+ "unicode-ident",
279
+ ]
280
+
281
+ [[package]]
282
+ name = "tree-sitter"
283
+ version = "0.22.6"
284
+ source = "registry+https://github.com/rust-lang/crates.io-index"
285
+ checksum = "df7cc499ceadd4dcdf7ec6d4cbc34ece92c3fa07821e287aedecd4416c516dca"
286
+ dependencies = [
287
+ "cc",
288
+ "regex",
289
+ ]
290
+
291
+ [[package]]
292
+ name = "tree_stump"
293
+ version = "0.1.0"
294
+ dependencies = [
295
+ "libloading",
296
+ "magnus",
297
+ "tree-sitter",
298
+ ]
299
+
300
+ [[package]]
301
+ name = "unicode-ident"
302
+ version = "1.0.12"
303
+ source = "registry+https://github.com/rust-lang/crates.io-index"
304
+ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
305
+
306
+ [[package]]
307
+ name = "windows-targets"
308
+ version = "0.52.6"
309
+ source = "registry+https://github.com/rust-lang/crates.io-index"
310
+ checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
311
+ dependencies = [
312
+ "windows_aarch64_gnullvm",
313
+ "windows_aarch64_msvc",
314
+ "windows_i686_gnu",
315
+ "windows_i686_gnullvm",
316
+ "windows_i686_msvc",
317
+ "windows_x86_64_gnu",
318
+ "windows_x86_64_gnullvm",
319
+ "windows_x86_64_msvc",
320
+ ]
321
+
322
+ [[package]]
323
+ name = "windows_aarch64_gnullvm"
324
+ version = "0.52.6"
325
+ source = "registry+https://github.com/rust-lang/crates.io-index"
326
+ checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
327
+
328
+ [[package]]
329
+ name = "windows_aarch64_msvc"
330
+ version = "0.52.6"
331
+ source = "registry+https://github.com/rust-lang/crates.io-index"
332
+ checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
333
+
334
+ [[package]]
335
+ name = "windows_i686_gnu"
336
+ version = "0.52.6"
337
+ source = "registry+https://github.com/rust-lang/crates.io-index"
338
+ checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
339
+
340
+ [[package]]
341
+ name = "windows_i686_gnullvm"
342
+ version = "0.52.6"
343
+ source = "registry+https://github.com/rust-lang/crates.io-index"
344
+ checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
345
+
346
+ [[package]]
347
+ name = "windows_i686_msvc"
348
+ version = "0.52.6"
349
+ source = "registry+https://github.com/rust-lang/crates.io-index"
350
+ checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
351
+
352
+ [[package]]
353
+ name = "windows_x86_64_gnu"
354
+ version = "0.52.6"
355
+ source = "registry+https://github.com/rust-lang/crates.io-index"
356
+ checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
357
+
358
+ [[package]]
359
+ name = "windows_x86_64_gnullvm"
360
+ version = "0.52.6"
361
+ source = "registry+https://github.com/rust-lang/crates.io-index"
362
+ checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
363
+
364
+ [[package]]
365
+ name = "windows_x86_64_msvc"
366
+ version = "0.52.6"
367
+ source = "registry+https://github.com/rust-lang/crates.io-index"
368
+ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
data/Cargo.toml ADDED
@@ -0,0 +1,7 @@
1
+ # This Cargo.toml is here to let externals tools (IDEs, etc.) know that this is
2
+ # a Rust project. Your extensions dependencies should be added to the Cargo.toml
3
+ # in the ext/ directory.
4
+
5
+ [workspace]
6
+ members = ["./ext/tree_stump"]
7
+ resolver = "2"
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # TreeStump
2
+ [![RSpec](https://github.com/joker1007/tree_stump/actions/workflows/rspec.yml/badge.svg)](https://github.com/joker1007/tree_stump/actions/workflows/rspec.yml)
3
+
4
+ [tree-sitter](https://github.com/tree-sitter/tree-sitter) binding for Ruby written by Rust.
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add tree_stump
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install tree_stump
15
+
16
+ ## Usage
17
+
18
+ ```ruby
19
+ require "tree_stump"
20
+ require "rouge"
21
+
22
+ TreeStump.register_lang("ruby", "./libtree-sitter-ruby.so")
23
+
24
+ parser = TreeStump::Parser.new
25
+ parser.set_language("ruby")
26
+
27
+ source = File.read("./sample.rb")
28
+
29
+ formatter = Rouge::Formatters::Terminal256.new
30
+ lexer = Rouge::Lexers::Ruby.new
31
+
32
+ puts "== Source =="
33
+ puts formatter.format(lexer.lex(source))
34
+
35
+ puts "\n"
36
+
37
+ puts "== Tree =="
38
+ tree = parser.parse(source)
39
+
40
+ puts tree.root_node.to_sexp
41
+ ```
42
+
43
+ ```
44
+ (program (class name: (constant) superclass: (superclass (constant)) body: (body_statement (call method: (identifier) arguments: (argument_list (simple_symbol) (pair key: (hash_key_symbol) value: (true)))) (singleton_method object: (self) name: (identifier) parameters: (method_parameters (identifier)) body: (body_statement (call receiver: (constant) method: (identifier) arguments: (argument_list (pair key: (hash_key_symbol) value: (identifier)))))))) (call receiver: (constant) method: (identifier) arguments: (argument_list (string (string_content)))))
45
+ ```
46
+
47
+ ## Development
48
+
49
+ ### Requirements
50
+
51
+ - Rust Toolchain
52
+ - tree-sitter-ruby
53
+
54
+ 1. Download source of tree-sitter-ruby from [GitHub Repository](https://github.com/tree-sitter/tree-sitter-ruby).
55
+ 1. Extract tree-sitter-ruby source
56
+ 1. mv tree-sitter-ruby-v{version_num} to tree_stump/tree-sitter-ruby
57
+ 1. Execute `make` in tree-sitter-ruby directory
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joker1007/tree_stump.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rb_sys/extensiontask"
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task build: :compile
10
+
11
+ GEMSPEC = Gem::Specification.load("tree_stump.gemspec")
12
+
13
+ RbSys::ExtensionTask.new("tree_stump", GEMSPEC) do |ext|
14
+ ext.lib_dir = "lib/tree_stump"
15
+ end
16
+
17
+ task spec: :compile
18
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ [package]
2
+ name = "tree_stump"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+ authors = ["joker1007 <kakyoin.hierophant@gmail.com>"]
6
+ publish = false
7
+
8
+ [lib]
9
+ crate-type = ["cdylib"]
10
+
11
+ [dependencies]
12
+ magnus = { version = "0.7.1" }
13
+ tree-sitter = "~0.22"
14
+ libloading = "~0.8.4"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "rb_sys/mkmf"
5
+
6
+ create_rust_makefile("tree_stump/tree_stump")
@@ -0,0 +1,103 @@
1
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
2
+ #[magnus::wrap(class = "TreeStump::Point", free_immediately)]
3
+ pub struct Point {
4
+ pub row: usize,
5
+ pub column: usize,
6
+ }
7
+
8
+ impl Point {
9
+ pub fn new(row: usize, column: usize) -> Self {
10
+ Self { row, column }
11
+ }
12
+
13
+ pub fn inspect(&self) -> String {
14
+ format!("#<Point({}, {})>", self.row, self.column)
15
+ }
16
+
17
+ pub fn to_s(&self) -> String {
18
+ format!("({}, {})", self.row, self.column)
19
+ }
20
+
21
+ pub fn get_row(&self) -> usize {
22
+ self.row
23
+ }
24
+
25
+ pub fn get_column(&self) -> usize {
26
+ self.column
27
+ }
28
+
29
+ pub fn into_raw(self) -> tree_sitter::Point {
30
+ tree_sitter::Point {
31
+ row: self.row,
32
+ column: self.column,
33
+ }
34
+ }
35
+ }
36
+
37
+ impl From<tree_sitter::Point> for Point {
38
+ fn from(point: tree_sitter::Point) -> Self {
39
+ Self {
40
+ row: point.row,
41
+ column: point.column,
42
+ }
43
+ }
44
+ }
45
+
46
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
47
+ #[magnus::wrap(class = "TreeStump::Range", free_immediately)]
48
+ pub struct Range {
49
+ pub start_byte: usize,
50
+ pub end_byte: usize,
51
+ pub start_point: Point,
52
+ pub end_point: Point,
53
+ }
54
+
55
+ impl Range {
56
+ pub fn new(start_byte: usize, end_byte: usize, start_point: &Point, end_point: &Point) -> Self {
57
+ let start_point = start_point.to_owned();
58
+ let end_point = end_point.to_owned();
59
+ Self {
60
+ start_byte,
61
+ end_byte,
62
+ start_point,
63
+ end_point,
64
+ }
65
+ }
66
+
67
+ pub fn get_start_byte(&self) -> usize {
68
+ self.start_byte
69
+ }
70
+
71
+ pub fn get_end_byte(&self) -> usize {
72
+ self.end_byte
73
+ }
74
+
75
+ pub fn get_start_point(&self) -> Point {
76
+ self.start_point
77
+ }
78
+
79
+ pub fn get_end_point(&self) -> Point {
80
+ self.end_point
81
+ }
82
+
83
+ pub fn inspect(&self) -> String {
84
+ format!(
85
+ "#<Range({}, {}, {:?}, {:?})>",
86
+ self.start_byte, self.end_byte, self.start_point, self.end_point
87
+ )
88
+ }
89
+ pub fn to_s(&self) -> String {
90
+ format!("({}..{})", self.start_point.to_s(), self.end_point.to_s())
91
+ }
92
+ }
93
+
94
+ impl From<tree_sitter::Range> for Range {
95
+ fn from(range: tree_sitter::Range) -> Self {
96
+ Self {
97
+ start_byte: range.start_byte,
98
+ end_byte: range.end_byte,
99
+ start_point: range.start_point.into(),
100
+ end_point: range.end_point.into(),
101
+ }
102
+ }
103
+ }
@@ -0,0 +1,74 @@
1
+ use std::cell::RefCell;
2
+
3
+ #[magnus::wrap(class = "TreeStump::LanguageRef", free_immediately, unsafe_generics)]
4
+ pub struct LanguageRef<'a> {
5
+ pub raw_language_ref: tree_sitter::LanguageRef<'a>,
6
+ }
7
+ unsafe impl Send for LanguageRef<'_> {}
8
+
9
+ impl<'a> LanguageRef<'a> {
10
+ pub fn version(&self) -> usize {
11
+ self.raw_language_ref.version()
12
+ }
13
+
14
+ pub fn node_kind_count(&self) -> usize {
15
+ self.raw_language_ref.node_kind_count()
16
+ }
17
+
18
+ pub fn parse_state_count(&self) -> usize {
19
+ self.raw_language_ref.parse_state_count()
20
+ }
21
+
22
+ pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str> {
23
+ self.raw_language_ref.node_kind_for_id(id)
24
+ }
25
+
26
+ pub fn id_for_node_kind(&self, kind: String, named: bool) -> u16 {
27
+ self.raw_language_ref.id_for_node_kind(kind.as_str(), named)
28
+ }
29
+
30
+ pub fn node_kind_is_named(&self, id: u16) -> bool {
31
+ self.raw_language_ref.node_kind_is_named(id)
32
+ }
33
+
34
+ pub fn node_kind_is_visible(&self, id: u16) -> bool {
35
+ self.raw_language_ref.node_kind_is_visible(id)
36
+ }
37
+
38
+ pub fn field_name_for_id(&self, id: u16) -> Option<&'static str> {
39
+ self.raw_language_ref.field_name_for_id(id)
40
+ }
41
+
42
+ pub fn field_id_for_name(&self, name: String) -> Option<u16> {
43
+ self.raw_language_ref
44
+ .field_id_for_name(name.as_str())
45
+ .map(|id| id.into())
46
+ }
47
+
48
+ pub fn next_state(&self, state: u16, id: u16) -> u16 {
49
+ self.raw_language_ref.next_state(state, id)
50
+ }
51
+
52
+ pub fn lookahead_iterator(&self, state: u16) -> Option<LookaheadIterator> {
53
+ self.raw_language_ref
54
+ .lookahead_iterator(state)
55
+ .map(|raw_iterator| LookaheadIterator {
56
+ raw_iterator: RefCell::new(raw_iterator),
57
+ })
58
+ }
59
+ }
60
+
61
+ #[magnus::wrap(class = "TreeStump::LookaheadIterator")]
62
+ pub struct LookaheadIterator {
63
+ raw_iterator: RefCell<tree_sitter::LookaheadIterator>,
64
+ }
65
+
66
+ impl LookaheadIterator {
67
+ pub fn next(&self) -> Option<u16> {
68
+ self.raw_iterator.borrow_mut().next()
69
+ }
70
+
71
+ pub fn current_symbol_name(&self) -> &'static str {
72
+ self.raw_iterator.borrow().current_symbol_name()
73
+ }
74
+ }