V bindings for the tree-sitter parsing library.
v install https://github.com/spavn-analyzer/v-tree-sitter
Create a parser with a grammar:
import v_tree_sitter.tree_sitter
import v_tree_sitter.languages.tree_sitter_v as v
mut p := tree_sitter.new_parser[v.NodeType](v.type_factory)
p.set_language(v.language)
Parse some code:
code := 'fn main() {}'
tree := p.parse_string(source: code)
Inspect the syntax tree:
root := tree.root_node()
// (source_file (function_declaration name: (identifier) signature:
// (signature parameters: (parameter_list)) body: (block)))
println(root)
fc := root.first_child()?
if fc.type_name == .function_declaration {
if name_node := fc.child_by_field_name('name') {
println('Found function: ${name_node.text(rope)}') // Found function: main
println('Position: ${name_node.range()}')
println('Line: ${name_node.start_point().row}') // Line: 0
}
}
code := 'fn main() {}'
tree := p.parse_string(source: code, tree: old_tree.raw_tree)
See examples in the examples
directory.
examples
— examples of using the librarylanguages
— languages sources (only V for now)lib
— tree-sitter C lib sourcestests
— tests for the bindingstree_sitter
— bindings sources
To update the tree-sitter lib, run:
v update_tree_sitter.vsh
This project initially started by nedpals and after that in 2023 it was heavily modified by the VOSCA.
This project is under the MIT License. See the LICENSE file for the full license text.