Repository navigation
Expand file tree
/
Copy pathParserInit.cpp
More file actions
74 lines (60 loc) · 2.03 KB
/
Copy pathParserInit.cpp
File metadata and controls
74 lines (60 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "TreeSitterParser.hpp"
#include <cassert>
Parser::Parser(const char *filename)
{
auto is = std::ifstream(filename);
if (!is)
throw invalid_argument(string(filename) + " is not a valid .vld file");
// get length of file:
std::string str(std::istreambuf_iterator<char>{is}, {}) ;
// ...buffer contains the entire file...
parser = ts_parser_new();
assert(parser);
auto velva = tree_sitter_Velva();
if (velva == nullptr)
{
throw invalid_argument("aw shucks");
}
ts_parser_set_language(parser, velva);
tree = ts_parser_parse_string(
parser,
NULL,
str.c_str(),
strlen(str.c_str())
);
cursor = TreeSitterCursor(tree, str);
}
Parser::~Parser()
{
ts_tree_delete(tree);
ts_parser_delete(parser);
}
// convert this to ts tree cursor later
void Parser::printTree(bool printNoNamedNodes, std::optional<TSNode> nodeInp, int lvl)
{
TSNode node;
if (!nodeInp)
node = ts_tree_root_node(tree);
else
node = *nodeInp;
bool is_named = ts_node_is_named(node);
TSPoint start = ts_node_start_point(node);
TSPoint end = ts_node_end_point(node);
const char *string = ts_node_type(node);
for (int i = 0; i < lvl; i++)
printf(" ");
printf("%s: start: %d %d end %d %d %s\n", string, start.row, start.column, end.row, end.column, is_named ? "named" : "");
// printf("=============================================\n");
// printf("Start: %d %d End: %d %d\n", start.column, start.row, end.column, end.row);
// printf("Contents: %s\n", str_in_file.c_str());
// printf("=============================================\n");
if (printNoNamedNodes) {
int len = ts_node_child_count(node);
for (int i = 0; i < len; i++)
printTree(printNoNamedNodes, ts_node_child(node, i), lvl+1);
} else {
int len = ts_node_named_child_count(node);
for (int i = 0; i < len; i++)
printTree(printNoNamedNodes, ts_node_named_child(node, i), lvl+1);
}
}