File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,32 @@ The code used in the [Using Parsers](https://tree-sitter.github.io/tree-sitter/u
2828was ported as a spec test at [spec/tree_sitter_spec.cr](spec/tree_sitter_spec.cr), the API documentation is being
2929ported 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
33591. Fork it (<https://github.com/crystal-lang-tools/crystal-tree-sitter/fork>)
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments