1 #include <fstream> 2 #include <string> 3 #include <cstdlib> 4 #include <tree_sitter/api.h> 5 6 extern "C" { 7 #include "javascript/src/parser.c" 8 #include "javascript/src/scanner.c" 9 } 10 11 #define LANGUAGE tree_sitter_javascript 12 #define SOURCE_PATH "javascript/examples/jquery.js" 13 main()14int main() { 15 TSParser *parser = ts_parser_new(); 16 if (!ts_parser_set_language(parser, LANGUAGE())) { 17 fprintf(stderr, "Invalid language\n"); 18 exit(1); 19 } 20 21 const char *source_path = GRAMMARS_DIR SOURCE_PATH; 22 23 printf("Parsing %s\n", source_path); 24 25 std::ifstream source_file(source_path); 26 if (!source_file.good()) { 27 fprintf(stderr, "Invalid source path %s\n", source_path); 28 exit(1); 29 } 30 31 std::string source_code( 32 (std::istreambuf_iterator<char>(source_file)), 33 std::istreambuf_iterator<char>() 34 ); 35 36 TSTree *tree = ts_parser_parse_string( 37 parser, 38 NULL, 39 source_code.c_str(), 40 source_code.size() 41 ); 42 } 43