Modern C++ header parser
Actively maintained. Last release 7 weeks ago, commits in 10 of the last 26 weeks. A safe choice.
pip install cxxheaderparsercxxheaderparser
A pure python C++ header parser that parses C++ headers in a mildly naive manner that allows it to handle many C++ constructs, including many modern (C++11 and beyond) features.
This is a complete rewrite of the CppHeaderParser library. CppHeaderParser
is really useful for some tasks, but it's implementation is a truly terrible
ugly hack built on top of other terrible hacks. This rewrite tries to learn
from CppHeaderParser and leave its ugly baggage behind.
Goals:
- Parse syntatically valid C++ and provide a useful (and documented!) pure python API to work with the parsed data
- Process incomplete headers (doesn't need to process includes)
- Provide enough information for binding generators to wrap C++ code
- Handle common C++ features, but it may struggle with obscure or overly complex things (feel free to make a PR to fix it!)
Non-goals:
- Does not produce a full AST, use Clang if you need that
- Not intended to validate C++, which means this will not reject all invalid C++ headers! Use a compiler if you need that
- Parser requires a C++ preprocessor. If you are parsing
headers that contain macros, you should preprocess your code using the
excellent pure python preprocessor pcpp
or your favorite compiler
- We have implemented support for PCPP, GCC, and MSVC -- but it is not enabled
by default. See the
cxxheaderparser.preprocessormodule for how to enable it.
- We have implemented support for PCPP, GCC, and MSVC -- but it is not enabled
by default. See the
- Probably won't be able to parse most IOCCC entries
There are two APIs available:
- A visitor-style interface to build up your own custom data structures
- A simple visitor that stores everything in a giant data structure
Live Demo
A pyodide-powered interactive demo is at https://robotpy.github.io/cxxheaderparser/
Documentation
Documentation can be found at https://cxxheaderparser.readthedocs.io
Install
Requires Python 3.6+, no non-stdlib dependencies if using Python 3.7+.
pip install cxxheaderparser
Usage
To see a dump of the data parsed from a header:
# pprint format
python -m cxxheaderparser myheader.h
# JSON format
python -m cxxheaderparser --mode=json myheader.h
# dataclasses repr format
python -m cxxheaderparser --mode=repr myheader.h
# dataclasses repr format (formatted with black)
python -m cxxheaderparser --mode=brepr myheader.h