Skip to content

Commit

Permalink
Build system with tests. (#18)
Browse files Browse the repository at this point in the history
* created a build cmake system
* added tests
* fixed push_back list helper, which was doing emplace_back
* minor fixes
  • Loading branch information
peter-winter authored Dec 21, 2021
1 parent 9be547e commit 2013210
Show file tree
Hide file tree
Showing 31 changed files with 1,179 additions and 72 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
bin/*
.vscode
build
75 changes: 75 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
cmake_minimum_required(VERSION 3.12)

project("ctpg" VERSION 1.3.4
DESCRIPTION "Compile Time Parser Generator. A single header library turning c++ code into LR(1) parser with finite state machine lexer in compile time."
HOMEPAGE_URL "https://github.com/peter-winter/ctpg")

include(GNUInstallDirs)

add_library(${PROJECT_NAME} INTERFACE)

target_include_directories(
${PROJECT_NAME}
INTERFACE
$<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)


# Test
find_package(Catch2 3 REQUIRED)
add_executable(testbin
tests/regex_tests.cpp
tests/simple_cases.cpp
tests/simple_grammar.cpp
tests/empty_symbols.cpp
tests/recurrence.cpp
tests/precedence.cpp
tests/compile_time.cpp
tests/list_helpers.cpp
tests/skip_whitespace.cpp
tests/typed_terms.cpp
tests/source_tracking.cpp
tests/error_recovery.cpp
tests/buffers.cpp
)
target_link_libraries(testbin PRIVATE Catch2::Catch2WithMain)
target_include_directories(testbin PRIVATE ${PROJECT_SOURCE_DIR}/include)
include(CTest)
include(Catch)
catch_discover_tests(testbin)
enable_testing()

# Install
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

include(CMakePackageConfigHelpers)

write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
if(NOT INCLUDE_INSTALL_DIR)
set(INCLUDE_INSTALL_DIR include/${PROJECT_NAME})
endif()

configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake
PATH_VARS INCLUDE_INSTALL_DIR)

install(EXPORT ${PROJECT_NAME}_Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)

install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)

install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
1 change: 1 addition & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
5 changes: 5 additions & 0 deletions cmake/ctpgConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
set_and_check(ctpg_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
check_required_components("@PROJECT_NAME@")
2 changes: 1 addition & 1 deletion examples/ctjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Compile Time JSON Parser example
*/

#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>

#include <iostream>
#include <variant>
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_lexer.cpp → examples/custom-lexer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>
#include <iostream>

using namespace ctpg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>
#include <iostream>

using namespace ctpg;
Expand Down
2 changes: 1 addition & 1 deletion examples/html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Texts and attributes are a trivial task to add
This example shows how to take advantage of the fact that the parser is defined in c++.
Here both terms and rules are generated from tag names
*/
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>

#include <iostream>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion examples/json-parser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>

#include <iostream>
#include <variant>
Expand Down
6 changes: 6 additions & 0 deletions examples/language/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.12)
project(language LANGUAGES CXX)
find_package(ctpg CONFIG REQUIRED)
include_directories(${ctpg_INCLUDE_DIR})
add_executable(${PROJECT_NAME} language.cpp)
target_link_libraries(${PROJECT_NAME} ctpg::ctpg)
22 changes: 15 additions & 7 deletions examples/language/language.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
/*
A simple language parser, demonstrates how to set up a language parser with a
abstract syntax tree builder.
Use sample file examples/language/samples/1 to parser and see the results
Compile:
g++ ./examples/language/language.cpp -std=c++17 -o ./bin/a.out
Look at the CMakeLists.txt to see how to integrate CTPG into your CMake projects
Provide a text file as an argument:
./bin/a.out "$(cat ./examples/language/samples/1)"
Build in a separate build direcotry:
mkdir build
cd build
cmake ..
make
When no input string as an argument is provided program prints a parser diagnostic messsage.
Run (in this directory):
./build/language "$(cat ./samples/1)"
When no input string as an argument is provided program prints a parser diagnostic message.
*/

#include "../../ctpg.hpp"
// The header file normally sits in its own directory, somewhere like: /usr/local/include/ctpg/ctpg.hpp
// but this time CMake dealt with it so there is no need for #include <ctpg/ctpg.hpp>
#include <ctpg.hpp>

#include "ast_builder.hpp"

using namespace ctpg;
Expand Down
2 changes: 1 addition & 1 deletion examples/readme-example.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>
#include <iostream>

using namespace ctpg;
Expand Down
2 changes: 1 addition & 1 deletion examples/regex-test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <iostream>
#include <sstream>

#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>

using namespace ctpg;

Expand Down
2 changes: 1 addition & 1 deletion examples/simple-expr-parser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>
#include <iostream>

using namespace ctpg;
Expand Down
2 changes: 1 addition & 1 deletion examples/source-tracking.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>

#include <iostream>

Expand Down
2 changes: 1 addition & 1 deletion examples/typed_terms.cpp → examples/typed-terms.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../ctpg.hpp"
#include <ctpg/ctpg.hpp>
#include <iostream>

using namespace ctpg;
Expand Down
12 changes: 5 additions & 7 deletions ctpg.hpp → include/ctpg.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef CTPG_H
#define CTPG_H

constexpr const char* version_str = "1.3.4";

#include <utility>
#include <type_traits>
#include <cstdint>
Expand Down Expand Up @@ -639,9 +637,9 @@ namespace ftors
true>
{
template<typename Container, typename Arg, typename... Rest>
constexpr decltype(auto) operator()(ignore<Skip1>..., Container &&container, ignore<Skip2>..., Arg&& arg, Rest&&...) const
constexpr decltype(auto) operator()(ignore<Skip1>..., Container &&container, ignore<Skip2>..., const Arg& arg, Rest&&...) const
{
container.emplace_back(std::move(arg));
container.push_back(arg);
return std::move(container);
}
};
Expand All @@ -660,9 +658,9 @@ namespace ftors
false>
{
template<typename Container, typename Arg, typename... Rest>
constexpr decltype(auto) operator()(ignore<Skip1>..., Arg&& arg, ignore<Skip2>..., Container &&container, Rest&&...) const
constexpr decltype(auto) operator()(ignore<Skip1>..., const Arg& arg, ignore<Skip2>..., Container &&container, Rest&&...) const
{
container.emplace_back(std::move(arg));
container.push_back(arg);
return std::move(container);
}
};
Expand Down Expand Up @@ -780,7 +778,7 @@ struct source_point
friend std::ostream& operator << (std::ostream& o, const source_point& sp);
};

std::ostream& operator << (std::ostream& o, const source_point& sp)
inline std::ostream& operator << (std::ostream& o, const source_point& sp)
{
o << "[" << sp.line << ":" << sp.column << "]";
return o;
Expand Down
Loading

0 comments on commit 2013210

Please sign in to comment.