Giter Site home page Giter Site logo

geekrelief / cppparser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from satya-das/cppparser

0.0 1.0 0.0 14.3 MB

A library to parse C/C++ source as AST

License: MIT License

C++ 73.77% C 7.05% Lex 0.07% Yacc 0.19% CMake 0.01% Objective-C 18.91% Shell 0.01%

cppparser's Introduction

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;
  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 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.