Skip to main content

Modern C++ header parser

Project description

cxxheaderparser

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.preprocessor module for how to enable it.
  • 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

See the documentation for anything more complex.

Bugs

This should handle even complex C++ code with few problems, but there are almost certainly weird edge cases that it doesn't handle. Additionally, not all C++17/20 constructs are supported yet (but contributions welcome!).

If you find an bug, we encourage you to submit a pull request! New changes will only be accepted if there are tests to cover the change you made (and if they don’t break existing tests).

It's really easy to add new tests, see the README in the tests directory.

Author

cxxheaderparser was created by Dustin Spicuzza

Credit

  • Partially derived from and inspired by the CppHeaderParser project originally developed by Jashua Cloutier
  • An embedded version of PLY is used for lexing tokens
  • Portions of the lexer grammar and other ideas were derived from pycparser
  • The source code is liberally sprinkled with comments containing C++ parsing grammar mostly derived from the Hyperlinked C++ BNF Grammar
  • cppreference.com has been invaluable for understanding many of the weird quirks of C++, and some of the unit tests use examples from there
  • Compiler Explorer has been invaluable for validating my understanding of C++ by allowing me to quickly type in quirky C++ constructs to see if they actually compile

License

BSD License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cxxheaderparser-1.7.1.tar.gz (54.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cxxheaderparser-1.7.1-py3-none-any.whl (60.7 kB view details)

Uploaded Python 3

File details

Details for the file cxxheaderparser-1.7.1.tar.gz.

File metadata

  • Download URL: cxxheaderparser-1.7.1.tar.gz
  • Upload date:
  • Size: 54.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cxxheaderparser-1.7.1.tar.gz
Algorithm Hash digest
SHA256 d59175a60420d494e4eb5be197efb52a3a76a4e4191aa1314650894e8fa861bb
MD5 5a005a686388dbde70c61afe73c2fba8
BLAKE2b-256 6d27c70f012fd9b02ba1b41a3fb4ec606e2ecf4b49b6e6bb65a40147653b022d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cxxheaderparser-1.7.1.tar.gz:

Publisher: dist.yml on robotpy/cxxheaderparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cxxheaderparser-1.7.1-py3-none-any.whl.

File metadata

  • Download URL: cxxheaderparser-1.7.1-py3-none-any.whl
  • Upload date:
  • Size: 60.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cxxheaderparser-1.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b9394480cf6ea591a0a9f12e5df8601261c6fd86d3a9541097299bc9b92308a7
MD5 3cf59b1ace7bd8b2478b7d873899725a
BLAKE2b-256 724d8ef63155c1a7105c03f3f18f4e333a3a51439ede97821a04a841e370cc09

See more details on using hashes here.

Provenance

The following attestation bundles were made for cxxheaderparser-1.7.1-py3-none-any.whl:

Publisher: dist.yml on robotpy/cxxheaderparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page