-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparse_demo.cpp
More file actions
34 lines (27 loc) · 849 Bytes
/
Copy pathparse_demo.cpp
File metadata and controls
34 lines (27 loc) · 849 Bytes
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
#include <cstdlib>
#include <print>
#include <string_view>
#include <cpp-tree-sitter.h>
#include <cts/format.h>
extern "C" TSLanguage* tree_sitter_json();
int
main() {
auto parser = ts::Parser::create(tree_sitter_json());
if (!parser) {
std::println(stderr, "error: {}", parser.error());
return EXIT_FAILURE;
}
std::string_view constexpr sourcecode = "[1, null]";
auto const tree = parser->parse(sourcecode);
if (!tree) {
std::println(stderr, "error: {}", tree.error());
return EXIT_FAILURE;
}
ts::Node const root = tree->getRootNode();
std::println("root: {}", root.getType());
for (ts::Node const child : root.getNamedChild(0)->getNamedChildren()) {
std::println(" {}: '{}'", child, child.getSourceRange(sourcecode));
}
std::println("Syntax tree: {}", root.getSExpr());
return EXIT_SUCCESS;
}