Skip to content

Commit 6132f0e

Browse files
Small changes
1 parent 5cb761e commit 6132f0e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ The code used in the [Using Parsers](https://tree-sitter.github.io/tree-sitter/u
2828
was ported as a spec test at [spec/tree_sitter_spec.cr](spec/tree_sitter_spec.cr), the API documentation is being
2929
ported as well, not yet on github-pages, but run `crystal doc` and have fun.
3030

31+
```crystal
32+
require "tree_sitter"
33+
34+
parser = TreeSitter::Parser.new("crystal")
35+
36+
source = <<-CRYSTAL
37+
class Name
38+
end
39+
CRYSTAL
40+
41+
tree = parser.parse nil, source
42+
43+
query = TreeSitter::Query.new(parser.language, <<-SCM)
44+
(class_def) @class
45+
46+
(constant) @constant
47+
SCM
48+
49+
cursor = TreeSitter::QueryCursor.new(query)
50+
cursor.exec(tree.root_node)
51+
52+
cursor.each_capture do |capture|
53+
p capture
54+
end
55+
```
56+
3157
## Contributing
3258

3359
1. Fork it (<https://github.com/crystal-lang-tools/crystal-tree-sitter/fork>)

src/tree_sitter/query_cursor.cr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ module TreeSitter
6666
Capture.new(rule, Node.new(capture.node))
6767
end
6868

69+
def each_capture(& : Capture -> Nil)
70+
while capture = next_capture
71+
yield capture
72+
end
73+
end
74+
6975
def to_unsafe
7076
@cursor
7177
end

src/tree_sitter/repository.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module TreeSitter
1010
@@language_paths ||= begin
1111
languages = Hash(String, Path).new
1212
Config.parser_directories.each do |dir|
13-
Dir[dir.join("**", "src", "grammar.json")].each do |grammar_path|
13+
Dir[dir.join("*", "src", "grammar.json")].each do |grammar_path|
1414
languages[$2] = Path.new($1) if grammar_path =~ %r{(.*/tree\-sitter\-([\w\-_]+))/src/grammar.json\z}
1515
end
1616
end

0 commit comments

Comments
 (0)