forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_printer.cpp
More file actions
35 lines (30 loc) · 1.25 KB
/
Copy pathast_printer.cpp
File metadata and controls
35 lines (30 loc) · 1.25 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
// Copyright (C) 2017-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
/// \file
/// This is a very primitive version of the cppast tool.
///
/// Given an input file it will print the AST.
#include <cppast/visitor.hpp> // visit()
#include "example_parser.hpp"
void print_ast(const cppast::cpp_file& file)
{
std::string prefix;
// visit each entity in the file
cppast::visit(file, [&](const cppast::cpp_entity& e, cppast::visitor_info info) {
if (info.event == cppast::visitor_info::container_entity_exit) // exiting an old container
prefix.pop_back();
else if (info.event == cppast::visitor_info::container_entity_enter)
// entering a new container
{
std::cout << prefix << "'" << e.name() << "' - " << cppast::to_string(e.kind()) << '\n';
prefix += "\t";
}
else // if (info.event == cppast::visitor_info::leaf_entity) // a non-container entity
std::cout << prefix << "'" << e.name() << "' - " << cppast::to_string(e.kind()) << '\n';
});
}
int main(int argc, char* argv[])
{
return example_main(argc, argv, {}, &print_ast);
}