{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# srcmlcpp: C++ code parsing\n", "\n", "litgen provides three separate python packages, srcmlcpp is one of them:\n", "\n", "* [`codemanip`](https://github.com/pthom/litgen/tree/main/packages/codemanip): a python package to perform _textual_ manipulations on C++ and Python code. See [code_utils.py](https://github.com/pthom/litgen/tree/main/packages/codemanip/code_utils.py)\n", "* [`srcmlcpp`](https://github.com/pthom/litgen/tree/main/packages/srcmlcpp): a python package that build on top of srcML in order to interpret the XML tree produced by srcML as a tree of python object resembling a C++ AST.\n", "* [`litgen`](https://github.com/pthom/litgen/tree/main/packages/litgen): a python package that generates python bindings from C++ code.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "[`srcmlcpp`](https://github.com/pthom/litgen/tree/main/packages/srcmlcpp) will transform C++ source into a tree of Python objects (descendants of `CppElement`) that reflect the C++ AST.\n", "\n", "This tree is used by litgen to generate the python bindings. It may also be used to perform automatic C++ code transformations.\n", "\n", "## Transform C++ code into a CppElement tree\n", "Given the C++ code below: " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "code = \"\"\"\n", "// A Demo struct\n", "struct Foo\n", "{\n", " const int answer(int *v=nullptr); // Returns the answer\n", "};\n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "srcmlcpp can produce a tree of `CppElement` with this call:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import srcmlcpp\n", "\n", "options = srcmlcpp.SrcmlcppOptions()\n", "cpp_unit = srcmlcpp.code_to_cpp_unit(options, code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`cpp_unit` is then a tree of Python object (descendants of `CppElement`) that represents the source code.\n", "\n", "Here is what it looks like under a debugger:\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Transform a CppElement tree into C++ code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Transformation to source code from a tree of `CppElement`\n", "\n", "`CppElement` provides a method `str_code()` that can output the C++ code it contains. It is close to the original source code (including comments), but can differ a bit.\n", "\n", "```{note}\n", "Any modification applied to the AST tree by modifying the CppElements objects (CppUnit, CppStruct, etc.) will be visible using this method\n", "```" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", "
// A Demo struct\n",
"struct Foo\n",
"{\n",
"public: // <default_access_type/>\n",
" const int answer(int * v = nullptr); // Returns the answer\n",
"};\n",
"