Giter Club home page Giter Club logo

cmonster's Introduction

I am no longer developing cmonster; please see the fork at github.com/sztomi/cmonster.

cmonster

cmonster is a Python wrapper for the Clang C++ parser.

As well as providing standard preprocessing/parsing capabilities, cmonster adds support for:

  • Inline Python macros;
  • Programmatic #include handling; and
  • (Currently rudimentary) source-to-source translation.

Documentation

There's no proper documentation yet; I'll get to it eventually. In the mean time...

Inline Python macros

You can define inline Python macros like so:

py_def(function_name(arg1, arg2, *args, **kwdargs))
    return [list_of_tokens]
py_end

Python macros must return a string, which will be tokenized, or a sequence of tokens (e.g. some combination of the input arguments). Python macros are used exactly the same as ordinary macros. e.g.

py_def(REVERSED(token))
    return "".join(reversed(str(token)))
py_end
int main() {
    printf("%s\n", REVERSED("Hello, World!"));
    return 0;
}

Source-to-source translation

Source-to-source translation involves parsing a C++ file, generating an AST; then traversing the AST, and making modifications in a rewriter. The rewriter object maintains a history of changes, and can eventually be told to dump the modified file to a stream.

For example, here's a snippet of Python code that shows how to make an insertion at the top of each top-level function declaration's body:

import cmonster
import cmonster.ast
import sys

# Create a parser, and a rewriter. Parse the code.
parser = cmonster.Parser(filename)
ast = parser.parse()
rewriter = cmonster.Rewriter(ast)

# For each top-level function declaration, insert a statement at the top of
# its body.
for decl in ast.translation_unit.declarations:
    if decl.location.in_main_file and \
       isinstance(decl, cmonster.ast.FunctionDecl):
        insertion_loc = decl.body[0]
        rewriter.insert(insertion_loc, 'printf("Tada!\\n");\n')

# Finally, dump the result.
rewriter.dump(sys.stdout)

Installation

cmonster requires Python 3.2, LLVM/Clang 3.0, so first ensure you have them installed. To build and install cmonster from source, you will also need to install Cython.

Now you can use easy_install to install cmonster, like so: easy_install cmonster. This will download and install a prebuilt binary distribution of cmonster.

If you wish to build cmonster yourself, either pull down the git repository, or grab the source distribution from cmonster's PyPI page. When building, make sure you have LLVM 3.0's llvm-config in your execution path. To verify this, run llvm-config --version; you should expect to see 3.0 output. To build from source, simply run python3.2 setup.py install.

cmonster's People

Contributors

axw avatar

Watchers

 avatar  avatar

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.