Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into add_languages_to_cm…
Browse files Browse the repository at this point in the history
…ake_project
  • Loading branch information
rturrado committed Sep 26, 2023
2 parents c9f41d1 + 6481988 commit 6a1339e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
submodules: true
- uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.11
- name: Install dependencies
if: matrix.os == 'macos-latest'
run: |
Expand Down
36 changes: 30 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ set(CMAKE_CXX_EXTENSIONS OFF)
SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)


#=============================================================================#
# Package dependencies #
#=============================================================================#
include(FetchContent)

# Require flex/bison; if not installed, this will try to build from source.
set(FLEX_VERSION_REQUIRED 2.6.1)
set(BISON_VERSION_REQUIRED 3.0)
set(BISON_VERSION_TO_BUILD 3.8)
include(cmake/flex-bison.cmake)

set(fmt_VERSION_REQUIRED 10.1.1)
find_package(fmt ${fmt_VERSION_REQUIRED})
if(NOT fmt_FOUND)
# fmt 10.1.1
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG "f5e54359df4c26b6230fc61d38aa294581393084"
)
FetchContent_MakeAvailable(fmt)
endif()


#=============================================================================#
# tree-lib support library target #
#=============================================================================#
Expand All @@ -104,6 +127,9 @@ target_include_directories(
PRIVATE ${TREE_LIB_PRIVATE_INCLUDE}
PUBLIC ${TREE_LIB_PUBLIC_INCLUDE}
)
target_link_libraries(tree-lib-obj
PRIVATE fmt::fmt
)

# Set variables at possible parent projects.
get_directory_property(hasParent PARENT_DIRECTORY)
Expand All @@ -125,12 +151,6 @@ target_link_libraries(tree-lib PUBLIC $<TARGET_PROPERTY:tree-lib-obj,LINK_LIBRAR
# tree-gen code generator tool #
#=============================================================================#

# Require flex/bison; if not installed, this will try to build from source.
set(FLEX_VERSION_REQUIRED 2.6.1)
set(BISON_VERSION_REQUIRED 3.0)
set(BISON_VERSION_TO_BUILD 3.8)
include(cmake/flex-bison.cmake)

# Generate the lexer.
if(WIN32)
set(flex_win_compat --wincompat)
Expand Down Expand Up @@ -182,6 +202,10 @@ target_include_directories(
PRIVATE "${CMAKE_CURRENT_BINARY_DIR}"
)

target_link_libraries(tree-gen
PRIVATE fmt::fmt
)

# Utility function for generating a C++-only tree with tree-gen.
function(generate_tree TREE HDR SRC)

Expand Down
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
[![Documentation Status](https://readthedocs.org/projects/tree-gen/badge/?version=latest)](https://tree-gen.readthedocs.io/en/latest)
[![GitHub Actions Build Status](https://github.com/QuTech-Delft/tree-gen/workflows/Test/badge.svg)](https://github.com/qutech-delft/tree-gen/actions)

`tree-gen` is a C++ and Python code generator for tree-like structures common in
parser and compiler codebases. The tree is described using a simple and concise
language, so all the repetitive C++ and Python stuff can be generated. This
includes serialization and deserialization to [CBOR](https://cbor.io/), allowing
easy interoperation between C++ and Python.
`tree-gen` is a C++ and Python code generator for tree-like structures common in parser and compiler codebases.
The tree is described using a simple and concise language, so all the repetitive C++ and Python stuff can be generated.
This includes serialization and deserialization to [CBOR](https://cbor.io/),
allowing easy interoperation between C++ and Python.

## Usage and "installation"

`tree-gen` is a very lightweight tool, intended to be compiled along with your
project rather than to actually be installed. The assumption is that your main
project will be written in C++ and that you're using CMake. If these assumptions
are invalid, you'll have to do some extra work on your own. If they *are* valid,
however, it should be really easy. Just include `tree-gen` as a submodule or
copy it into your project, then do something like
`tree-gen` is a very lightweight tool,
intended to be compiled along with your project rather than to actually be installed.
The assumption is that your main project will be written in C++ and that you're using CMake.
If these assumptions are invalid, you'll have to do some extra work on your own. If they *are* valid,
however, it should be really easy.
Just include `tree-gen` as a submodule or copy it into your project, then do something like

```cmake
add_subdirectory(tree-gen)
Expand All @@ -41,21 +40,20 @@ target_include_directories(
PRIVATE "${CMAKE_CURRENT_BINARY_DIR}"
)
# To use tree-gen's support library (you could substitute your own if you feel
# up to the task):
# To use tree-gen's support library (you could substitute your own if you feel up to the task):
target_link_libraries(my-software tree-lib)
```

and CMake *Should*™ handle everything for you.

`tree-gen` does have some dependencies:

- A compiler with C++11 support (MSVC, GCC, and Clang are tested in CI);
- A compiler with C++11 support (MSVC, GCC, and Clang are tested in CI)
- Flex 2.6.1+
- Bison 3.0+

If you're on a POSIX system and Flex/Bison are too old or not installed, the
buildsystem will attempt to download and build them from source for you.
If you're on a POSIX system and Flex/Bison are too old or not installed,
the buildsystem will attempt to download and build them from source for you.

For usage information beyond this, [Read The Docs](https://tree-gen.readthedocs.io/).

Expand Down
3 changes: 2 additions & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Welcome to tree-gen's documentation!
====================================

tree-gen is a program that generates C++ source code for tree structures based on a simple and concise input language. The API documentation is `generated by Doxygen <doxy/index.html>`_.
tree-gen is a program that generates C++ source code for tree structures based on a simple and concise input language.
The API documentation is `generated by Doxygen <doxy/index.html>`_.

.. toctree::
:maxdepth: 2
Expand Down
9 changes: 8 additions & 1 deletion generator/tree-gen-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <fstream>
#include <iostream>
#include <cctype>
#include <fmt/format.h>
#include <unordered_set>
#include "tree-gen-cpp.hpp"

Expand Down Expand Up @@ -166,7 +167,7 @@ void generate_base_class(
header << " }" << std::endl << std::endl;

header << "protected:" << std::endl << std::endl;
format_doc(header, "Internal helper method for visiter pattern.", " ");
format_doc(header, "Internal helper method for visitor pattern.", " ");
header << " virtual void visit_internal(VisitorBase &visitor, void *retval=nullptr) = 0;" << std::endl << std::endl;

header << "public:" << std::endl << std::endl;
Expand Down Expand Up @@ -1123,6 +1124,7 @@ void generate(
header << "#pragma once" << std::endl;
header << std::endl;
header << "#include <iostream>" << std::endl;
header << "#include <fmt/ostream.h>" << std::endl;
for (auto &include : specification.includes) {
header << "#" << include << std::endl;
}
Expand Down Expand Up @@ -1337,6 +1339,11 @@ void generate(
header << std::endl;
source << std::endl;

format_doc(header, "std::ostream support via fmt (uses operator<<).");
auto namespaces = fmt::format("{}::", fmt::join(specification.namespaces, "::"));
for (auto &node : nodes) {
header << "template <> struct fmt::formatter<" << namespaces << node->title_case_name << "> : ostream_formatter {};" << std::endl;
}
}

} // namespace cpp
Expand Down

0 comments on commit 6a1339e

Please sign in to comment.