cppast/example/ast_printer.cpp
2022-02-07 20:43:22 +01:00

34 lines
1.1 KiB
C++

// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
/// \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);
}