在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:satya-das/cppparser开源软件地址:https://github.com/satya-das/cppparser开源编程语言:C++ 73.7%开源软件介绍:CppParserAn easy, fast, and robust library to parse C/C++ source. Features
MotivationCppParser can be used to build tools that need parsing of C/C++ files. I am using it to develop cib which implements ABI stable SDK architecture for C++ library. ExampleTo begin with we will see an example of parsing a hello-world program and see what is the AST that #include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
For the above hello-world program we can expect that when it is parsed the generated AST should look like following: So, how we are going to access these elements of AST using #include <catch/catch.hpp>
#include "cppparser.h"
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
TEST_CASE("Parsing hello world program")
{
CppParser parser;
const auto testFilePath = fs::path(__FILE__).parent_path() / "test-files/hello-world.cpp";
const auto ast = parser.parseFile(testFilePath.string());
REQUIRE(ast != nullptr);
const auto& members = ast->members();
REQUIRE(members.size() == 2);
CppIncludeEPtr hashInclude = members[0];
REQUIRE(hashInclude);
CHECK(hashInclude->name_ == "<iostream>");
CppFunctionEPtr func = members[1];
REQUIRE(func);
CHECK(func->name_ == "main");
REQUIRE(func->defn());
const auto& mainBodyMembers = func->defn()->members();
REQUIRE(mainBodyMembers.size() == 2);
CppExprEPtr coutHelloWorld = mainBodyMembers[0];
REQUIRE(coutHelloWorld);
CHECK(coutHelloWorld->oper_ == CppOperator::kInsertion);
}
This example is a real one and is part of actual unit test of CppParser. Building CppParserGet the sourcegit clone https://github.com/satya-das/common.git
git clone https://github.com/satya-das/CppParser.git Configure and buildcd cppparser
mkdir builds
cd builds
cmake ..
make && make test Alternatively, if you prefer cd cppparser
mkdir builds
cd builds
cmake -G Ninja ..
ninja && ninja test |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论