ruby_tree_sitter 1.5.0 → 1.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1b2cf41f13a89341d5baceb421b53cb7cdd849044bb703d74d139b29d7768da
4
- data.tar.gz: 15bad1e7513b73efe382ce880fe8e26af6b64707777dd47de39829559b290e9b
3
+ metadata.gz: e0488c0541ab1295e21de7a53a3af6bbfb0d2e5a2340874803037b00956e16e3
4
+ data.tar.gz: 9e5b5d202e3857ee5840322c1899684e12aa8469faa3c199f0cb8b1d1d183913
5
5
  SHA512:
6
- metadata.gz: 34388882b92dbf3dd48be897a57c841e837fabc044c0928782b3b03f424b29657bfc3a1d718a5677a5174187710b884952f07072fe79f73bb92cadcfdd84508a
7
- data.tar.gz: 1a0cf18288d39254beabea8f72522e349a127eab2c4baecd1ba709139fa90629f231760d2eb3f90adcda057f116044abc001fa47bd47d100f292f2d012829e47
6
+ metadata.gz: b0d6108292ce09bed1aca0d83899df812019c5689901c762b0b8f54b951f68dc4256495b1e10ad2faf64523f0cfad7262fd07aad7dab1666d599a0453dd40b20
7
+ data.tar.gz: 6d84ad3d549c81a18a64509d826ae86d12bf90da60cdb0bcff40d5a85008c80ec309afe2d3190707601586d814f8a07ee919887f67c3103ebf352585b69b1e44
data/README.md CHANGED
@@ -25,6 +25,10 @@ require 'tree_sitter'
25
25
 
26
26
  parser = TreeSitter::Parser.new
27
27
  language = TreeSitter::Language.load('javascript', 'path/to/libtree-sitter-javascript.{so,dylib}')
28
+ # Or simply
29
+ language = TreeSitter.lang('javascript')
30
+ # Which will try to look in your local directory and the system for installed parsers.
31
+ # See TreeSitter::Mixin::Language#lib_dirs
28
32
 
29
33
  src = "[1, null]"
30
34
 
@@ -64,12 +68,23 @@ TreeStand provides an idiomatic Ruby interface to work with tree-sitter parsers.
64
68
 
65
69
  ## Dependencies
66
70
 
67
- This gem is a binding for `tree-sitter`. It doesn't have a version of
68
- `tree-sitter` baked in it by default.
71
+ This gem is a binding for `tree-sitter`, and comes with a pre-built version
72
+ of `tree-sitter` bundled with it, so you can start using it without any
73
+ special configuration.
69
74
 
70
- You must install `tree-sitter` and make sure that their dynamic library is
71
- accessible from `$PATH`, or build the gem with `--disable-sys-libs`, which will
72
- download the latest tagged `tree-sitter` and build against it (see [Build from source](docs/Contributing.md#build-from-source) .)
75
+ We support the following platforms:
76
+
77
+ - `aarch64-linux-gnu`
78
+ - `aarch64-linux-musl`
79
+ - `arm-linux-gnu`
80
+ - `arm-linux-musl`
81
+ - `x86_64-linux-gnu`
82
+ - `x86_64-linux-musl`
83
+ - `x86-linux-musl`
84
+ - `arm64-darwin`
85
+ - `x86_64-darwin`
86
+
87
+ (see [Build from source](docs/Contributing.md#build-from-source)).
73
88
 
74
89
  You can either install `tree-sitter` from source or through your go-to package manager.
75
90
 
@@ -104,7 +119,7 @@ brew install tree-sitter
104
119
  From [rubygems](https://rubygems.org/gems/ruby_tree_sitter), in your `Gemfile`:
105
120
 
106
121
  ```ruby
107
- gem 'ruby_tree_sitter', '~> 1.0'
122
+ gem 'ruby_tree_sitter', '~> 1.6'
108
123
  ```
109
124
 
110
125
  Or manually:
@@ -119,18 +134,19 @@ Or from `git` sources, which will compile on installation:
119
134
  gem 'ruby_tree_sitter', git: 'https://github.com/Faveod/ruby-tree-sitter'
120
135
  ```
121
136
 
122
- ### Disable system libraries
137
+ ### Enable system libraries
123
138
 
124
- To install with `--disable-sys-lib`, you can either:
139
+ To install with `--enable-sys-libs`, you can either:
125
140
 
126
141
  ```sh
127
- gem install ruby_tree_sitter -- --disable-sys-libs
142
+ gem install ruby_tree_sitter --platform=ruby -- --enable-sys-libs
128
143
  ```
129
144
 
130
145
  Or via bundle:
131
146
 
132
147
  ```sh
133
- bundle config set build.ruby_tree_sitter --disable-sys-libs
148
+ bundle config force_ruby_platform true
149
+ bundle config set build.ruby_tree_sitter --enable-sys-libs
134
150
  ```
135
151
 
136
152
  ### No compilation
@@ -159,16 +175,6 @@ You will have to install parsers yourself, either by:
159
175
  [Faveod/tree-sitter-parsers](https://github.com/Faveod/tree-sitter-parsers)
160
176
  which supports numerous architectures.
161
177
 
162
- ### A note on static vs dynamic linking
163
-
164
- This extension will statically link against a downloaded version of
165
- `tree-sitter` when you use the `--disable-sys-lib`. So any installed version of
166
- `tree-sitter` will not be loaded.
167
-
168
- The native gems are also statically linked.
169
-
170
- All other methods will dynamically link against the installed `tree-sitter`.
171
-
172
178
  ## Examples
173
179
 
174
180
  See `examples` directory.
@@ -15,7 +15,7 @@ cflags = []
15
15
  ldflags = []
16
16
 
17
17
  def system_tree_sitter?
18
- enable_config('sys-libs', true)
18
+ enable_config('sys-libs', false)
19
19
  end
20
20
 
21
21
  def env_var_on?(var)
@@ -26,6 +26,8 @@ end
26
26
  # System lib vs Downloaded lib #
27
27
  # ################################## #
28
28
 
29
+ repo = TreeSitter::Repo.new
30
+
29
31
  dir_include, dir_lib =
30
32
  if system_tree_sitter?
31
33
  [
@@ -33,7 +35,6 @@ dir_include, dir_lib =
33
35
  %w[/opt/lib /opt/local/lib /usr/lib /usr/local/lib],
34
36
  ]
35
37
  else
36
- repo = TreeSitter::Repo.new
37
38
  if !repo.download
38
39
  msg = <<~MSG
39
40
 
@@ -48,19 +49,19 @@ dir_include, dir_lib =
48
49
  # We need to make sure we're selecting the proper toolchain.
49
50
  # Especially needed for corss-compilation.
50
51
  ENV.store('CC', RbConfig::CONFIG['CC'])
52
+ repo.patch
51
53
  repo.compile
52
- repo.keep_static_lib
53
54
  repo.include_and_lib_dirs
54
55
  end
55
56
 
57
+ dir_config('tree-sitter', dir_include&.first, dir_lib&.first)
58
+
56
59
  # ################################## #
57
60
  # Generate Makefile #
58
61
  # ################################## #
59
62
 
60
- header = find_header('tree_sitter/api.h', *dir_include)
61
- library = find_library('tree-sitter', # libtree-sitter
62
- 'ts_parser_new', # a symbol
63
- *dir_lib)
63
+ header = find_header('tree_sitter/api.h')
64
+ library = find_library('tree-sitter', 'ts_parser_new')
64
65
 
65
66
  if !header || !library
66
67
  abort <<~MSG
@@ -144,6 +145,5 @@ cflags.concat %w[-std=c99 -fPIC -Wall]
144
145
  append_cflags(cflags)
145
146
  append_ldflags(ldflags)
146
147
 
147
- dir_config('tree-sitter')
148
148
  create_header
149
149
  create_makefile('tree_sitter/tree_sitter')
@@ -54,7 +54,7 @@ static VALUE language_load(VALUE self, VALUE name, VALUE path) {
54
54
  dlclose(lib);
55
55
  rb_raise(rb_eRuntimeError,
56
56
  "Could not load symbol `%s' from library `%s'.\nReason:%s",
57
- StringValueCStr(name), StringValueCStr(path), err);
57
+ StringValueCStr(name), path_cstr, err);
58
58
  }
59
59
 
60
60
  TSLanguage *lang = make_ts_language();
@@ -63,7 +63,7 @@ static VALUE language_load(VALUE self, VALUE name, VALUE path) {
63
63
  rb_raise(rb_eRuntimeError,
64
64
  "TSLanguage = NULL for language `%s' in library `%s'.\nCall your "
65
65
  "local TSLanguage supplier.",
66
- StringValueCStr(name), StringValueCStr(path));
66
+ StringValueCStr(name), path_cstr);
67
67
  }
68
68
 
69
69
  uint32_t version = ts_language_version(lang);
@@ -71,7 +71,7 @@ static VALUE language_load(VALUE self, VALUE name, VALUE path) {
71
71
  rb_raise(rb_eRuntimeError,
72
72
  "Language %s (v%d) from `%s' is old.\nMinimum supported ABI: "
73
73
  "v%d.\nCurrent ABI: v%d.",
74
- StringValueCStr(name), version, StringValueCStr(path),
74
+ StringValueCStr(name), version, path_cstr,
75
75
  TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION,
76
76
  TREE_SITTER_LANGUAGE_VERSION);
77
77
  }
@@ -29,7 +29,9 @@ module TreeSitter
29
29
  def compile
30
30
  # We need to clean because the same folder is used over and over
31
31
  # by rake-compiler-dock
32
- sh "cd #{src} && make clean && make"
32
+ ar = RbConfig::MAKEFILE_CONFIG['AR']
33
+ ar = " AR=#{ar}" if ar
34
+ sh "cd #{src} && make clean &&#{ar} make libtree-sitter.a"
33
35
  end
34
36
 
35
37
  def exe?(name)
@@ -59,14 +61,14 @@ module TreeSitter
59
61
  end
60
62
 
61
63
  def include_and_lib_dirs
62
- [[src / 'lib' / 'include'], [src.to_s]]
64
+ [[(src / 'lib' / 'include').to_s], [src.to_s]]
63
65
  end
64
66
 
65
- def keep_static_lib
66
- src
67
- .children
68
- .filter { |f| /\.(dylib|so)/ =~ f.basename.to_s }
69
- .each(&:unlink)
67
+ def patch
68
+ root = Pathname.new(File.expand_path(__dir__)).realpath.parent.parent
69
+ patch = root / 'patches' / 'cross-compile.patch'
70
+
71
+ sh "patch --forward #{src / 'Makefile'} #{patch}"
70
72
  end
71
73
 
72
74
  def sh(cmd)
@@ -98,9 +100,14 @@ module TreeSitter
98
100
  def sources_from_git
99
101
  return false if !exe?(:git)
100
102
 
101
- sh "git clone #{url[:git]} #{src}"
102
- sh "cd #{src} && git checkout tags/v#{version}"
103
-
103
+ sh <<~SHELL.chomp
104
+ mkdir #{src} \\
105
+ && cd #{src} \\
106
+ && git init \\
107
+ && git remote add origin "#{url[:git]}" \\
108
+ && git fetch origin --depth 1 tags/v#{version} \\
109
+ && git reset --hard FETCH_HEAD
110
+ SHELL
104
111
  true
105
112
  end
106
113
 
@@ -19,6 +19,7 @@ module TreeSitter
19
19
  'vendor/tree-sitter-parsers',
20
20
  'parsers',
21
21
  'tree-sitter-parsers',
22
+ '.',
22
23
  '/opt/local/lib',
23
24
  '/opt/lib',
24
25
  '/usr/local/lib',
@@ -67,6 +68,9 @@ module TreeSitter
67
68
  # TreeStand.config.parser_path = '/my/path'
68
69
  # java = TreeStand::Parser.language('java')
69
70
  #
71
+ # @note the name is case sensitive, but library lookup is not: if your parser is defined as `COBOL`,
72
+ # then you have to call `language('COBOL')`, but the parser can be `cobol.so/COBOL.so/…`.
73
+ #
70
74
  # @param name [String] the name of the parser.
71
75
  # This name is used to load the symbol from the compiled parser, replacing `-` with `_`.
72
76
  #
@@ -120,11 +124,13 @@ module TreeSitter
120
124
  # If a {TreeStand::Config#parser_path} is `nil`, {LIBDIRS} is used.
121
125
  # If a {TreeStand::Config#parser_path} is a {::Pathname}, {LIBDIRS} is ignored.
122
126
  def search_for_lib(name)
123
- files = [
124
- name,
125
- "tree-sitter-#{name}",
126
- "libtree-sitter-#{name}",
127
- ].map { |v| "#{v}.#{ext}" }
127
+ files =
128
+ [name, name.upcase, name.downcase]
129
+ .uniq
130
+ .flat_map do |n|
131
+ base = "#{n}.#{ext}"
132
+ [base, "tree-sitter-#{base}", "libtree-sitter-#{base}"]
133
+ end
128
134
 
129
135
  lib_dirs
130
136
  .product(files)
@@ -4,5 +4,5 @@ module TreeSitter
4
4
  # The version of the tree-sitter library.
5
5
  TREESITTER_VERSION = '0.22.6'
6
6
  # The current version of the gem.
7
- VERSION = '1.5.0'
7
+ VERSION = '1.6.0'
8
8
  end
data/lib/tree_sitter.rb CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  require 'set'
4
4
 
5
- require 'tree_sitter/tree_sitter'
5
+ begin
6
+ RUBY_VERSION =~ /(\d+\.\d+)/
7
+ require "tree_sitter/#{Regexp.last_match(1)}/tree_sitter"
8
+ rescue LoadError
9
+ require 'tree_sitter/tree_sitter'
10
+ end
11
+
6
12
  require 'tree_sitter/version'
7
13
 
8
14
  require 'tree_sitter/mixins/language'
data/tree_sitter.gemspec CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.version = TreeSitter::VERSION
18
18
 
19
19
  spec.metadata = {
20
- 'allowed_push_host' => 'https://rubygems.org',
21
20
  'homepage_uri' => spec.homepage,
22
21
  'source_code_uri' => spec.homepage,
23
22
  'changelog_uri' => spec.homepage,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_tree_sitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Firas al-Khalil
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-07-04 00:00:00.000000000 Z
12
+ date: 2024-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sorbet-runtime
@@ -103,7 +103,6 @@ homepage: https://www.github.com/Faveod/ruby-tree-sitter
103
103
  licenses:
104
104
  - MIT
105
105
  metadata:
106
- allowed_push_host: https://rubygems.org
107
106
  homepage_uri: https://www.github.com/Faveod/ruby-tree-sitter
108
107
  source_code_uri: https://www.github.com/Faveod/ruby-tree-sitter
109
108
  changelog_uri: https://www.github.com/Faveod/ruby-tree-sitter
@@ -123,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
122
  - !ruby/object:Gem::Version
124
123
  version: '0'
125
124
  requirements: []
126
- rubygems_version: 3.5.11
125
+ rubygems_version: 3.3.26
127
126
  signing_key:
128
127
  specification_version: 4
129
128
  summary: Ruby bindings for Tree-Sitter