Tree-Sitter

Contents

To use the library #include <tree_sitter/tree_sitter.hpp>. Everything lives in the namespace ts.

Usage Examples

First you have to define the ts::Language you want to use:

extern "C" const TSLanguage* tree_sitter_lua();
const ts::Language LUA_LANGUAGE{tree_sitter_lua()};

Now you can create and use a ts::Parser in a function:

void use_parser() {
    ts::Parser parser(LUA_LANGUAGE);
    ts::Tree tree = parser.parse_string("print('Hello, World!')");
}

As you can see parsing returns a ts::Tree. You can walk this tree with a ts::Cursor or query it using ts::Query or manually walk the nodes by calling ts::Tree::root_node. Additionally you can edit the tree using ts::Tree::edit (editing is currently relatively limited).