Add documentation
This commit is contained in:
parent
e361d4443a
commit
1392fceeb5
33
.github/workflows/dist.yml
vendored
33
.github/workflows/dist.yml
vendored
@ -10,23 +10,23 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: psf/black@stable
|
- uses: psf/black@stable
|
||||||
|
|
||||||
# check-doc:
|
check-doc:
|
||||||
# runs-on: ubuntu-18.04
|
runs-on: ubuntu-18.04
|
||||||
|
|
||||||
# steps:
|
steps:
|
||||||
# - uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
# with:
|
with:
|
||||||
# submodules: recursive
|
submodules: recursive
|
||||||
# fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
# - uses: actions/setup-python@v2
|
- uses: actions/setup-python@v2
|
||||||
# with:
|
with:
|
||||||
# python-version: 3.8
|
python-version: 3.8
|
||||||
# - name: Sphinx
|
- name: Sphinx
|
||||||
# run: |
|
run: |
|
||||||
# pip --disable-pip-version-check install -e .
|
pip --disable-pip-version-check install -e .
|
||||||
# pip --disable-pip-version-check install -r docs/requirements.txt
|
pip --disable-pip-version-check install -r docs/requirements.txt
|
||||||
# cd docs && make clean html SPHINXOPTS="-W --keep-going"
|
cd docs && make clean html SPHINXOPTS="-W --keep-going"
|
||||||
|
|
||||||
test:
|
test:
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
@ -71,8 +71,7 @@ jobs:
|
|||||||
|
|
||||||
publish:
|
publish:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
#needs: [check, check-doc, test]
|
needs: [check, check-doc, test]
|
||||||
needs: [check, test]
|
|
||||||
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
|
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
@ -41,7 +41,7 @@ There are two APIs available:
|
|||||||
Documentation
|
Documentation
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
TODO: documentation site
|
Documentation can be found at https://cxxheaderparser.readthedocs.io
|
||||||
|
|
||||||
Install
|
Install
|
||||||
-------
|
-------
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import typing
|
import typing
|
||||||
|
|
||||||
if typing.TYPE_CHECKING:
|
from .lexer import LexToken
|
||||||
from .lexer import LexToken
|
|
||||||
|
|
||||||
|
|
||||||
class CxxParseError(Exception):
|
class CxxParseError(Exception):
|
||||||
|
@ -4,8 +4,6 @@ import re
|
|||||||
import typing
|
import typing
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
from .errors import CxxParseError
|
|
||||||
from ._ply import lex
|
from ._ply import lex
|
||||||
|
|
||||||
|
|
||||||
@ -340,6 +338,8 @@ class Lexer:
|
|||||||
_discard_types = {"NEWLINE", "COMMENT_SINGLELINE", "COMMENT_MULTILINE"}
|
_discard_types = {"NEWLINE", "COMMENT_SINGLELINE", "COMMENT_MULTILINE"}
|
||||||
|
|
||||||
def _token_limit_exceeded(self):
|
def _token_limit_exceeded(self):
|
||||||
|
from .errors import CxxParseError
|
||||||
|
|
||||||
raise CxxParseError("no more tokens left in this group")
|
raise CxxParseError("no more tokens left in this group")
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
@ -7,6 +7,21 @@ your own parser listener, but you can accomplish most things with it.
|
|||||||
cxxheaderparser's unit tests predominantly use the simple API for parsing,
|
cxxheaderparser's unit tests predominantly use the simple API for parsing,
|
||||||
so you can expect it to be pretty stable.
|
so you can expect it to be pretty stable.
|
||||||
|
|
||||||
|
The :func:`parse_string` and :func:`parse_file` functions are a great place
|
||||||
|
to start:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from cxxheaderparser.simple import parse_string
|
||||||
|
|
||||||
|
content = '''
|
||||||
|
int x;
|
||||||
|
'''
|
||||||
|
|
||||||
|
parsed_data = parse_string(content)
|
||||||
|
|
||||||
|
See below for the contents of the returned :class:`ParsedData`.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
@ -46,7 +61,11 @@ from .options import ParserOptions
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ClassScope:
|
class ClassScope:
|
||||||
|
"""
|
||||||
|
Contains all data collected for a single C++ class
|
||||||
|
"""
|
||||||
|
|
||||||
|
#: Information about the class declaration is here
|
||||||
class_decl: ClassDecl
|
class_decl: ClassDecl
|
||||||
|
|
||||||
#: Nested classes
|
#: Nested classes
|
||||||
@ -64,6 +83,10 @@ class ClassScope:
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class NamespaceScope:
|
class NamespaceScope:
|
||||||
|
"""
|
||||||
|
Contains all data collected for a single namespace. Content for child
|
||||||
|
namespaces are found in the ``namespaces`` attribute.
|
||||||
|
"""
|
||||||
|
|
||||||
name: str = ""
|
name: str = ""
|
||||||
|
|
||||||
@ -108,23 +131,9 @@ class UsingNamespace:
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ParsedData:
|
class ParsedData:
|
||||||
|
|
||||||
namespace: NamespaceScope = field(default_factory=lambda: NamespaceScope())
|
|
||||||
|
|
||||||
defines: typing.List[Define] = field(default_factory=list)
|
|
||||||
pragmas: typing.List[Pragma] = field(default_factory=list)
|
|
||||||
includes: typing.List[Include] = field(default_factory=list)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Visitor implementation
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
class SimpleCxxVisitor:
|
|
||||||
"""
|
"""
|
||||||
A simple visitor that stores all of the C++ elements passed to it
|
Container for information parsed by the :func:`parse_file` and
|
||||||
in an "easy" to use data structure
|
:func:`parse_string` functions.
|
||||||
|
|
||||||
.. warning:: Names are not resolved, so items are stored in the scope that
|
.. warning:: Names are not resolved, so items are stored in the scope that
|
||||||
they are found. For example:
|
they are found. For example:
|
||||||
@ -144,6 +153,33 @@ class SimpleCxxVisitor:
|
|||||||
namespace instead of the 'N' namespace.
|
namespace instead of the 'N' namespace.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
#: Global namespace
|
||||||
|
namespace: NamespaceScope = field(default_factory=lambda: NamespaceScope())
|
||||||
|
|
||||||
|
#: Any ``#define`` preprocessor directives encountered
|
||||||
|
defines: typing.List[Define] = field(default_factory=list)
|
||||||
|
|
||||||
|
#: Any ``#pragma`` directives encountered
|
||||||
|
pragmas: typing.List[Pragma] = field(default_factory=list)
|
||||||
|
|
||||||
|
#: Any ``#include`` directives encountered
|
||||||
|
includes: typing.List[Include] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Visitor implementation
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleCxxVisitor:
|
||||||
|
"""
|
||||||
|
A simple visitor that stores all of the C++ elements passed to it
|
||||||
|
in an "easy" to use data structure
|
||||||
|
|
||||||
|
You probably don't want to use this directly, use :func:`parse_file`
|
||||||
|
or :func:`parse_string` instead.
|
||||||
|
"""
|
||||||
|
|
||||||
data: ParsedData
|
data: ParsedData
|
||||||
namespace: NamespaceScope
|
namespace: NamespaceScope
|
||||||
block: Block
|
block: Block
|
||||||
|
@ -202,7 +202,7 @@ class TemplateSpecialization:
|
|||||||
"""
|
"""
|
||||||
Contains the arguments of a template specialization
|
Contains the arguments of a template specialization
|
||||||
|
|
||||||
.. code-block:: c++s
|
.. code-block:: c++
|
||||||
|
|
||||||
Foo<int, Bar...>
|
Foo<int, Bar...>
|
||||||
~~~~~~~~~~~
|
~~~~~~~~~~~
|
||||||
|
1
docs/.gitignore
vendored
Normal file
1
docs/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/_build
|
20
docs/Makefile
Normal file
20
docs/Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Minimal makefile for Sphinx documentation
|
||||||
|
#
|
||||||
|
|
||||||
|
# You can set these variables from the command line, and also
|
||||||
|
# from the environment for the first two.
|
||||||
|
SPHINXOPTS ?=
|
||||||
|
SPHINXBUILD ?= sphinx-build
|
||||||
|
SOURCEDIR = .
|
||||||
|
BUILDDIR = _build
|
||||||
|
|
||||||
|
# Put it first so that "make" without argument is like "make help".
|
||||||
|
help:
|
||||||
|
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||||
|
|
||||||
|
.PHONY: help Makefile
|
||||||
|
|
||||||
|
# Catch-all target: route all unknown targets to Sphinx using the new
|
||||||
|
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
||||||
|
%: Makefile
|
||||||
|
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
64
docs/conf.py
Normal file
64
docs/conf.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
# Configuration file for the Sphinx documentation builder.
|
||||||
|
#
|
||||||
|
# This file only contains a selection of the most common options. For a full
|
||||||
|
# list see the documentation:
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
||||||
|
|
||||||
|
# -- Path setup --------------------------------------------------------------
|
||||||
|
|
||||||
|
import os
|
||||||
|
import pkg_resources
|
||||||
|
|
||||||
|
# -- Project information -----------------------------------------------------
|
||||||
|
|
||||||
|
project = "cxxheaderparser"
|
||||||
|
copyright = "2020-2021, Dustin Spicuzza"
|
||||||
|
author = "Dustin Spicuzza"
|
||||||
|
|
||||||
|
# The full version, including alpha/beta/rc tags
|
||||||
|
release = pkg_resources.get_distribution("cxxheaderparser").version
|
||||||
|
|
||||||
|
# -- RTD configuration ------------------------------------------------
|
||||||
|
|
||||||
|
# on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org
|
||||||
|
on_rtd = os.environ.get("READTHEDOCS", None) == "True"
|
||||||
|
|
||||||
|
|
||||||
|
# -- General configuration ---------------------------------------------------
|
||||||
|
|
||||||
|
# Add any Sphinx extension module names here, as strings. They can be
|
||||||
|
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||||
|
# ones.
|
||||||
|
extensions = [
|
||||||
|
"sphinx.ext.autodoc",
|
||||||
|
"sphinx_autodoc_typehints",
|
||||||
|
]
|
||||||
|
|
||||||
|
# Add any paths that contain templates here, relative to this directory.
|
||||||
|
templates_path = ["_templates"]
|
||||||
|
|
||||||
|
# List of patterns, relative to source directory, that match files and
|
||||||
|
# directories to ignore when looking for source files.
|
||||||
|
# This pattern also affects html_static_path and html_extra_path.
|
||||||
|
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for HTML output -------------------------------------------------
|
||||||
|
|
||||||
|
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||||
|
# a list of builtin themes.
|
||||||
|
#
|
||||||
|
if not on_rtd: # only import and set the theme if we're building docs locally
|
||||||
|
import sphinx_rtd_theme
|
||||||
|
|
||||||
|
html_theme = "sphinx_rtd_theme"
|
||||||
|
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
|
||||||
|
else:
|
||||||
|
html_theme = "default"
|
||||||
|
|
||||||
|
# Add any paths that contain custom static files (such as style sheets) here,
|
||||||
|
# relative to this directory. They are copied after the builtin static files,
|
||||||
|
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||||
|
html_static_path = ["_static"]
|
||||||
|
|
||||||
|
always_document_param_types = True
|
37
docs/custom.rst
Normal file
37
docs/custom.rst
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
Custom parsing
|
||||||
|
==============
|
||||||
|
|
||||||
|
For many users, the data provided by the simple API is enough. In some advanced
|
||||||
|
cases you may find it necessary to use this more customizable parsing mechanism.
|
||||||
|
|
||||||
|
First, define a visitor that implements the :class:`CxxVisitor` protocol. Then
|
||||||
|
you can create an instance of it and pass it to the :class:`CxxParser`.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
visitor = MyVisitor()
|
||||||
|
parser = CxxParser(filename, content, visitor)
|
||||||
|
parser.parse()
|
||||||
|
|
||||||
|
# do something with the data collected by the visitor
|
||||||
|
|
||||||
|
Your visitor should do something with the data as the various callbacks are
|
||||||
|
called. See the :class:`SimpleCxxVisitor` for inspiration.
|
||||||
|
|
||||||
|
API
|
||||||
|
---
|
||||||
|
|
||||||
|
.. automodule:: cxxheaderparser.parser
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
.. automodule:: cxxheaderparser.visitor
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
Parser state
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. automodule:: cxxheaderparser.parserstate
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
37
docs/index.rst
Normal file
37
docs/index.rst
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
.. cxxheaderparser documentation master file, created by
|
||||||
|
sphinx-quickstart on Thu Dec 31 00:46:02 2020.
|
||||||
|
You can adapt this file completely to your liking, but it should at least
|
||||||
|
contain the root `toctree` directive.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
.. warning:: cxxheaderparser intentionally does not have a C preprocessor
|
||||||
|
implementation! If you are parsing code with macros in it, use
|
||||||
|
a conforming preprocessor like the pure python preprocessor
|
||||||
|
`pcpp`_ or your favorite C++ compiler.
|
||||||
|
|
||||||
|
.. _pcpp: https://github.com/ned14/pcpp
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 2
|
||||||
|
:caption: Contents:
|
||||||
|
|
||||||
|
tools
|
||||||
|
simple
|
||||||
|
custom
|
||||||
|
types
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Indices and tables
|
||||||
|
==================
|
||||||
|
|
||||||
|
* :ref:`genindex`
|
||||||
|
* :ref:`modindex`
|
||||||
|
* :ref:`search`
|
||||||
|
|
35
docs/make.bat
Normal file
35
docs/make.bat
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
@ECHO OFF
|
||||||
|
|
||||||
|
pushd %~dp0
|
||||||
|
|
||||||
|
REM Command file for Sphinx documentation
|
||||||
|
|
||||||
|
if "%SPHINXBUILD%" == "" (
|
||||||
|
set SPHINXBUILD=sphinx-build
|
||||||
|
)
|
||||||
|
set SOURCEDIR=.
|
||||||
|
set BUILDDIR=_build
|
||||||
|
|
||||||
|
if "%1" == "" goto help
|
||||||
|
|
||||||
|
%SPHINXBUILD% >NUL 2>NUL
|
||||||
|
if errorlevel 9009 (
|
||||||
|
echo.
|
||||||
|
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
||||||
|
echo.installed, then set the SPHINXBUILD environment variable to point
|
||||||
|
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
||||||
|
echo.may add the Sphinx directory to PATH.
|
||||||
|
echo.
|
||||||
|
echo.If you don't have Sphinx installed, grab it from
|
||||||
|
echo.http://sphinx-doc.org/
|
||||||
|
exit /b 1
|
||||||
|
)
|
||||||
|
|
||||||
|
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
||||||
|
goto end
|
||||||
|
|
||||||
|
:help
|
||||||
|
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
||||||
|
|
||||||
|
:end
|
||||||
|
popd
|
3
docs/requirements.txt
Normal file
3
docs/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
sphinx >= 3.0
|
||||||
|
sphinx-rtd-theme
|
||||||
|
sphinx-autodoc-typehints
|
12
docs/simple.rst
Normal file
12
docs/simple.rst
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
.. _simple:
|
||||||
|
|
||||||
|
Simple API
|
||||||
|
==========
|
||||||
|
|
||||||
|
.. automodule:: cxxheaderparser.simple
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
.. automodule:: cxxheaderparser.options
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
41
docs/tools.rst
Normal file
41
docs/tools.rst
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
Tools
|
||||||
|
=====
|
||||||
|
|
||||||
|
There are a variety of command line tools provided by the cxxheaderparser
|
||||||
|
project.
|
||||||
|
|
||||||
|
dump tool
|
||||||
|
---------
|
||||||
|
|
||||||
|
Dump data from a header to stdout
|
||||||
|
|
||||||
|
.. code-block:: sh
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
Anything more than that and you should use the python API, start with the
|
||||||
|
:ref:`simple API <simple>` first.
|
||||||
|
|
||||||
|
test generator
|
||||||
|
--------------
|
||||||
|
|
||||||
|
To generate a unit test for cxxheaderparser:
|
||||||
|
|
||||||
|
* Put the C++ header content in a file
|
||||||
|
* Run the following:
|
||||||
|
|
||||||
|
.. code-block:: sh
|
||||||
|
|
||||||
|
python -m cxxheaderparser.gentest FILENAME.h TESTNAME
|
||||||
|
|
||||||
|
You can copy/paste the stdout to one of the test files in the tests directory.
|
16
docs/types.rst
Normal file
16
docs/types.rst
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
Types
|
||||||
|
=====
|
||||||
|
|
||||||
|
parser types
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. automodule:: cxxheaderparser.types
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
exceptions
|
||||||
|
----------
|
||||||
|
|
||||||
|
.. automodule:: cxxheaderparser.errors
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
Loading…
x
Reference in New Issue
Block a user