1 Star 0 Fork 0

超自然祈祷 / cppparser as AST

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

CppParser

Build Status Codacy Badge License: MIT

An easy, fast, and robust library to parse C/C++ source.

Features

  • No pre-processing, and preprocessors are part of the ast.
  • Most comments are preserved too.
  • Developed from scratch and uses back-tracking yacc (BtYacc) to write C++ grammer, that means no dependency on libclang.
  • The result of parsing is an AST where elements of a file are arranged in a tree.
  • Minimum dependency. Only external dependency is a lexer which is by default available on unix like platforms and can be easily get on Windows.
  • Parsing of multi-file program is supported too.

Motivation

CppParser 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.

Example

To begin with we will see an example of parsing a hello-world program and see what is the AST that CppParser creates:

#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: AST for Hello World program

So, how we are going to access these elements of AST using CppParser? Below is the program written as unit-test for validating the correctness of generated AST:

#include <catch/catch.hpp>

#include "cppparser.h"

#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

TEST_CASE(" Parsing hello world program")
{
  CppParser parser;
  auto      testFilePath = fs::path(__FILE__).parent_path() / "test-files/hello-world.cpp";
  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 CppParser

Get the source

git clone https://github.com/satya-das/common.git
git clone https://github.com/satya-das/CppParser.git

Configure and build

cd cppparser
mkdir builds
cd builds
cmake ..
make && make test

Alternatively, if you prefer Ninja instead of make:

cd cppparser
mkdir builds
cd builds
cmake -G Ninja ..
ninja && ninja test
MIT License Copyright (c) 2018 Satya Das Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A library to parse C/C++ source as AST 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/naturalcaduceus/cppparser-as-AST.git
git@gitee.com:naturalcaduceus/cppparser-as-AST.git
naturalcaduceus
cppparser-as-AST
cppparser as AST
master

搜索帮助