ruby_tree_sitter 0.20.6.4-x86_64-darwin-20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +152 -0
  4. data/ext/tree_sitter/encoding.c +29 -0
  5. data/ext/tree_sitter/extconf.rb +172 -0
  6. data/ext/tree_sitter/input.c +126 -0
  7. data/ext/tree_sitter/input_edit.c +42 -0
  8. data/ext/tree_sitter/language.c +134 -0
  9. data/ext/tree_sitter/logger.c +212 -0
  10. data/ext/tree_sitter/macros.h +163 -0
  11. data/ext/tree_sitter/node.c +310 -0
  12. data/ext/tree_sitter/parser.c +203 -0
  13. data/ext/tree_sitter/point.c +26 -0
  14. data/ext/tree_sitter/quantifier.c +43 -0
  15. data/ext/tree_sitter/query.c +157 -0
  16. data/ext/tree_sitter/query_capture.c +28 -0
  17. data/ext/tree_sitter/query_cursor.c +103 -0
  18. data/ext/tree_sitter/query_error.c +41 -0
  19. data/ext/tree_sitter/query_match.c +44 -0
  20. data/ext/tree_sitter/query_predicate_step.c +83 -0
  21. data/ext/tree_sitter/range.c +35 -0
  22. data/ext/tree_sitter/symbol_type.c +46 -0
  23. data/ext/tree_sitter/tree.c +144 -0
  24. data/ext/tree_sitter/tree_cursor.c +97 -0
  25. data/ext/tree_sitter/tree_sitter.c +32 -0
  26. data/ext/tree_sitter/tree_sitter.h +107 -0
  27. data/lib/tree_sitter/node.rb +164 -0
  28. data/lib/tree_sitter/tree_sitter.bundle +0 -0
  29. data/lib/tree_sitter/version.rb +5 -0
  30. data/lib/tree_sitter.rb +13 -0
  31. data/test/README.md +15 -0
  32. data/test/test_helper.rb +9 -0
  33. data/test/tree_sitter/language_test.rb +68 -0
  34. data/test/tree_sitter/logger_test.rb +69 -0
  35. data/test/tree_sitter/node_test.rb +355 -0
  36. data/test/tree_sitter/parser_test.rb +140 -0
  37. data/test/tree_sitter/query_test.rb +153 -0
  38. data/test/tree_sitter/tree_cursor_test.rb +83 -0
  39. data/test/tree_sitter/tree_test.rb +51 -0
  40. data/tree_sitter.gemspec +32 -0
  41. metadata +192 -0
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper'
4
+
5
+ ruby = TreeSitter.lang('ruby')
6
+ parser = TreeSitter::Parser.new
7
+ parser.language = ruby
8
+
9
+ program = <<~RUBY
10
+ def mul(a, b)
11
+ res = a * b
12
+ puts res.inspect
13
+ return res
14
+ end
15
+ RUBY
16
+
17
+ tree = parser.parse_string(nil, program)
18
+ root = tree.root_node
19
+
20
+ pattern = '(method_parameters)'
21
+ capture = '(method_parameters (_)+ @args)'
22
+ predicate = '(method_parameters (_)+ @args (#match? @args "\w"))'
23
+ combined = "#{pattern} #{capture}"
24
+ # string = '(method_parameters (_)+ @args)'
25
+
26
+ # NOTE: It' still unclear to me what a captured string is.
27
+
28
+ describe 'pattern/capture/string' do
29
+ it 'must return an Integer for pattern count' do
30
+ query = TreeSitter::Query.new(ruby, pattern)
31
+ assert_equal 1, query.pattern_count
32
+ assert_equal 0, query.capture_count
33
+ assert_equal 0, query.string_count
34
+ end
35
+
36
+ it 'must return an Integer for pattern count' do
37
+ query = TreeSitter::Query.new(ruby, capture)
38
+ assert_equal 1, query.pattern_count
39
+ assert_equal 1, query.capture_count
40
+ assert_equal 0, query.string_count
41
+ end
42
+
43
+ it 'must return an Integer for combined patterns' do
44
+ query = TreeSitter::Query.new(ruby, combined)
45
+ assert_equal 2, query.pattern_count
46
+ assert_equal 1, query.capture_count
47
+ assert_equal 0, query.string_count
48
+ end
49
+
50
+ it 'must return an Integer for pattern start byte' do
51
+ query = TreeSitter::Query.new(ruby, combined)
52
+ assert_equal 0, query.start_byte_for_pattern(0)
53
+ assert_equal pattern.bytesize + 1, query.start_byte_for_pattern(1)
54
+ end
55
+
56
+ it 'must return an array of predicates for a pattern' do
57
+ query = TreeSitter::Query.new(ruby, combined)
58
+
59
+ preds0 = query.predicates_for_pattern(0)
60
+ assert_instance_of Array, preds0
61
+ assert_equal 0, preds0.size
62
+
63
+ preds1 = query.predicates_for_pattern(1)
64
+ assert_instance_of Array, preds1
65
+ assert_equal 0, preds1.size
66
+
67
+ query = TreeSitter::Query.new(ruby, predicate)
68
+ preds2 = query.predicates_for_pattern(0)
69
+ assert_instance_of Array, preds2
70
+ assert_equal 4, preds2.size
71
+ end
72
+
73
+ it 'must return string names, quanitfier, and string value for capture id' do
74
+ query = TreeSitter::Query.new(ruby, predicate)
75
+ query.predicates_for_pattern(0).each do |step|
76
+ next if step.type != TreeSitter::QueryPredicateStep::CAPTURE
77
+
78
+ assert_equal 'args', query.capture_name_for_id(step.value_id)
79
+ assert_equal TreeSitter::Quantifier::ONE_OR_MORE, query.capture_quantifier_for_id(0, step.value_id)
80
+ assert_equal 'match?', query.string_value_for_id(step.value_id)
81
+ end
82
+ end
83
+
84
+ it 'must disable captures but keep it in count' do
85
+ query = TreeSitter::Query.new(ruby, capture)
86
+ query.disable_capture('@args')
87
+ assert_equal 1, query.capture_count
88
+ end
89
+
90
+ it 'must disable captures but keep it in count' do
91
+ query = TreeSitter::Query.new(ruby, capture)
92
+ query.disable_pattern(0)
93
+ assert_equal 1, query.pattern_count
94
+ end
95
+ # TODO: pattern guaranteed at step
96
+ end
97
+
98
+ describe 'query_cursor' do
99
+ before do
100
+ @query = TreeSitter::Query.new(ruby, capture)
101
+ @cursor = TreeSitter::QueryCursor.exec(@query, root)
102
+ end
103
+
104
+ it 'must work with limits' do
105
+ @cursor.match_limit = 1
106
+ assert_equal 1, @cursor.match_limit
107
+ refute @cursor.exceed_match_limit?
108
+ refute_nil @cursor.next_capture
109
+ assert @cursor.exceed_match_limit?
110
+ end
111
+
112
+ it 'must work with byte range' do
113
+ child = root.child(0).child(0)
114
+ @cursor.set_byte_range(child.start_byte, child.end_byte)
115
+ assert_nil @cursor.next_capture
116
+ end
117
+
118
+ it 'must work with point range' do
119
+ child = root.child(0).child(0)
120
+ @cursor.set_point_range(child.start_point, child.end_point)
121
+ assert_nil @cursor.next_capture
122
+ end
123
+
124
+ it 'must work with next/remove' do
125
+ assert_equal 0, @cursor.next_match.id
126
+ @cursor.remove_match(1)
127
+ assert_nil @cursor.next_match
128
+ end
129
+ end
130
+
131
+ describe 'querying anonymous nodes' do
132
+ it 'must match & capture the correct nodes' do
133
+ binary = '(binary left: (identifier) operator: "*" right: (identifier)) @binary'
134
+ prog = <<~RUBY
135
+ c + d
136
+ a * b
137
+ e / f
138
+ RUBY
139
+ prog_tree = parser.parse_string(nil, prog)
140
+ prog_root = prog_tree.root_node
141
+ query = TreeSitter::Query.new(ruby, binary)
142
+ cursor = TreeSitter::QueryCursor.exec(query, prog_root)
143
+
144
+ while match = cursor.next_match
145
+ refute_nil(match)
146
+ assert_equal(1, match.captures.size)
147
+
148
+ node = match.captures.first.node
149
+ assert_equal 'a * b', prog[node.start_byte...node.end_byte]
150
+ end
151
+ assert_nil(match)
152
+ end
153
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper'
4
+
5
+ ruby = TreeSitter.lang('ruby')
6
+ parser = TreeSitter::Parser.new
7
+ parser.language = ruby
8
+
9
+ program = <<~RUBY
10
+ def mul(a, b)
11
+ res = a * b
12
+ puts res.inspect
13
+ return res
14
+ end
15
+ RUBY
16
+
17
+ tree = parser.parse_string(nil, program)
18
+ root = tree.root_node
19
+
20
+ describe 'TreeCursor should work properly' do
21
+ before do
22
+ @cursor = TreeSitter::TreeCursor.new(root)
23
+ end
24
+
25
+ it 'must return root node when created' do
26
+ assert_equal root, @cursor.current_node
27
+ end
28
+
29
+ it 'must return root node when reset after creation' do
30
+ @cursor.reset(root)
31
+ assert_equal root, @cursor.current_node
32
+ end
33
+
34
+ it 'must reset to an arbitrary node' do
35
+ @cursor.reset(root.child(0).child(0))
36
+ assert_equal root.child(0).child(0), @cursor.current_node
37
+ end
38
+
39
+ it 'must move on the tree properly' do
40
+ @cursor.goto_first_child
41
+ assert_equal root.child(0), @cursor.current_node
42
+
43
+ @cursor.goto_first_child
44
+ @cursor.goto_next_sibling
45
+ assert_equal root.child(0).child(0).next_sibling, @cursor.current_node
46
+
47
+ @cursor.goto_parent
48
+ assert_equal root.child(0), @cursor.current_node
49
+
50
+ @cursor.reset(root)
51
+ assert_equal root, @cursor.current_node
52
+ end
53
+
54
+ it 'must return a String when current_field_name is called on a named child, or nil otherwise' do
55
+ assert_nil @cursor.current_field_name
56
+
57
+ @cursor.goto_first_child
58
+ @cursor.goto_first_child
59
+ @cursor.goto_next_sibling
60
+
61
+ assert_equal 'name', @cursor.current_field_name
62
+ assert_instance_of Integer, @cursor.current_field_id
63
+ end
64
+
65
+ it 'must return proper child for given byte' do
66
+ @cursor.goto_first_child_for_byte(0)
67
+ assert_equal root.child(0), @cursor.current_node
68
+ end
69
+
70
+ it 'must return proper child for given point' do
71
+ @cursor.goto_first_child_for_point(root.start_point)
72
+ assert_equal root.child(0), @cursor.current_node
73
+ end
74
+
75
+ it 'must return proper child for given point' do
76
+ @cursor.goto_first_child_for_point(root.start_point)
77
+ assert_equal root.child(0), @cursor.current_node
78
+ end
79
+
80
+ it 'must return a distinct copy on copy' do
81
+ refute_equal @cursor, @cursor.copy
82
+ end
83
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper.rb'
4
+
5
+ ruby = TreeSitter.lang('ruby')
6
+ parser = TreeSitter::Parser.new
7
+ parser.language = ruby
8
+
9
+ program = <<~RUBY
10
+ def mul(a, b)
11
+ res = a* b
12
+ puts res.inspect
13
+ return res
14
+ end
15
+ RUBY
16
+
17
+ tree = parser.parse_string(nil, program)
18
+
19
+ describe 'copy' do
20
+ it 'must make a new copy' do
21
+ copy = tree.copy
22
+ refute_equal tree, copy
23
+ end
24
+ end
25
+
26
+ describe 'root_node' do
27
+ it 'must be of type TreeSitter::Node' do
28
+ root = tree.root_node
29
+ assert_instance_of TreeSitter::Node, root
30
+ end
31
+ end
32
+
33
+ describe 'language' do
34
+ it 'must be identical to parser language' do
35
+ assert_equal parser.language, tree.language
36
+ end
37
+ end
38
+
39
+ describe 'print_dot_graph' do
40
+ it 'must save to disk' do
41
+ dot = File.expand_path('tmp/tree-dot.gv', FileUtils.getwd)
42
+ tree.print_dot_graph(dot)
43
+
44
+ assert File.exist?(dot), 'dot file must be exist'
45
+ assert File.file?(dot), 'dot file must be a file'
46
+ refute_equal 0, File.size(dot)
47
+ end
48
+ end
49
+
50
+ # TODO: edit
51
+ # TODO: changed_ranges
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'tree_sitter/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.required_ruby_version = '>= 2.7'
10
+
11
+ spec.authors = ['Firas al-Khalil']
12
+ spec.email = ['firasalkhalil@gmail.com']
13
+ spec.homepage = 'https://www.github.com/Faveod/ruby-tree-sitter'
14
+ spec.license = 'MIT'
15
+ spec.name = 'ruby_tree_sitter'
16
+ spec.summary = 'Ruby bindings for Tree-Sitter'
17
+ spec.version = TreeSitter::VERSION
18
+
19
+ spec.extensions = %(ext/tree_sitter/extconf.rb)
20
+ spec.files = %w(LICENSE README.md tree_sitter.gemspec)
21
+ spec.files += Dir.glob('ext/**/*.[ch]')
22
+ spec.files += Dir.glob('lib/**/*.rb')
23
+ spec.test_files = Dir.glob('test/**/*')
24
+
25
+ spec.add_development_dependency('minitest', '~> 5.16')
26
+ spec.add_development_dependency('minitest-color', '~> 0.0.2')
27
+ spec.add_development_dependency('pry', '~> 0.14')
28
+ spec.add_development_dependency('rake', '~> 13.0')
29
+ spec.add_development_dependency('rake-compiler', '~> 1.2')
30
+ spec.add_development_dependency('rake-compiler-dock', '~> 1.2')
31
+ spec.add_development_dependency('ruby_memcheck', '~> 1.0')
32
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_tree_sitter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.20.6.4
5
+ platform: x86_64-darwin-20
6
+ authors:
7
+ - Firas al-Khalil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-color
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake-compiler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake-compiler-dock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ruby_memcheck
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
111
+ description:
112
+ email:
113
+ - firasalkhalil@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE
119
+ - README.md
120
+ - ext/tree_sitter/encoding.c
121
+ - ext/tree_sitter/extconf.rb
122
+ - ext/tree_sitter/input.c
123
+ - ext/tree_sitter/input_edit.c
124
+ - ext/tree_sitter/language.c
125
+ - ext/tree_sitter/logger.c
126
+ - ext/tree_sitter/macros.h
127
+ - ext/tree_sitter/node.c
128
+ - ext/tree_sitter/parser.c
129
+ - ext/tree_sitter/point.c
130
+ - ext/tree_sitter/quantifier.c
131
+ - ext/tree_sitter/query.c
132
+ - ext/tree_sitter/query_capture.c
133
+ - ext/tree_sitter/query_cursor.c
134
+ - ext/tree_sitter/query_error.c
135
+ - ext/tree_sitter/query_match.c
136
+ - ext/tree_sitter/query_predicate_step.c
137
+ - ext/tree_sitter/range.c
138
+ - ext/tree_sitter/symbol_type.c
139
+ - ext/tree_sitter/tree.c
140
+ - ext/tree_sitter/tree_cursor.c
141
+ - ext/tree_sitter/tree_sitter.c
142
+ - ext/tree_sitter/tree_sitter.h
143
+ - lib/tree_sitter.rb
144
+ - lib/tree_sitter/node.rb
145
+ - lib/tree_sitter/tree_sitter.bundle
146
+ - lib/tree_sitter/version.rb
147
+ - test/README.md
148
+ - test/test_helper.rb
149
+ - test/tree_sitter/language_test.rb
150
+ - test/tree_sitter/logger_test.rb
151
+ - test/tree_sitter/node_test.rb
152
+ - test/tree_sitter/parser_test.rb
153
+ - test/tree_sitter/query_test.rb
154
+ - test/tree_sitter/tree_cursor_test.rb
155
+ - test/tree_sitter/tree_test.rb
156
+ - tree_sitter.gemspec
157
+ homepage: https://www.github.com/Faveod/ruby-tree-sitter
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '3.2'
170
+ - - "<"
171
+ - !ruby/object:Gem::Version
172
+ version: 3.3.dev
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubygems_version: 3.4.10
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: Ruby bindings for Tree-Sitter
183
+ test_files:
184
+ - test/README.md
185
+ - test/test_helper.rb
186
+ - test/tree_sitter/language_test.rb
187
+ - test/tree_sitter/logger_test.rb
188
+ - test/tree_sitter/node_test.rb
189
+ - test/tree_sitter/parser_test.rb
190
+ - test/tree_sitter/query_test.rb
191
+ - test/tree_sitter/tree_cursor_test.rb
192
+ - test/tree_sitter/tree_test.rb