Skip to content

galchapman/cpp-tree-sitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tree Sitter C++ binding

This is a cpp binding for tree sitter

Usage

1. Build the core binding

Run make in the root directory by defult the c version is c99 and c++ version is 20

2. Add a language

Inside the languages run make \<language>-lang.o
To install and build your language.

3. Run your program

When you run your compiler include the folowing directories: project root, src and src/core/lib/include.

And don't forget to add the library files: libtree-sitter-cpp.a and your language library (for example cpp-lang.o).

Step by step guide

Clone this repositry

git clone https://github.com/galchapman/cpp-tree-sitter

Build the respoitry

make

Install a language

cd languages
make cpp-lang.o

Write your main program

#include <iostream>
#include <memory>
#include <parser.hpp>
#include <languages/cpp.hpp>

const char* source = \
"#include <iostream>\n" \
"\n" \
"using namespace std;\n" \
"\n" \
"int main(void) {\n" \
"    cout << \"Hello, World\";\n" \
"    return 0;\n" \
"}\n";

int main(void) {
    ts::Parser parser;
    parser.setLanguage(ts::cpp::language());
    
    std::shared_ptr<ts::Tree> tree = parser.parse(source);

    ts::Node rootNode = tree->rootNode();

    std::cout << rootNode.sexp() << std::endl;

    return 0;
}

Than compile your program

g++ -I. -Isrc -Isrc/core/lib/include libtree-sitter-cpp.o languages/cpp-lang.o main.cpp -o main

Than run it

./main.exe

About

C++ binding for Tree Sitter

Resources

Stars

Watchers

Forks