Compare commits

...

22 Commits
v0.8 ... v0.9

Author SHA1 Message Date
Lars Melchior
f57476ee43 Update README.md 2019-05-17 14:50:27 +02:00
Lars Melchior
db4f2bf7bb Update README.md 2019-05-17 14:47:35 +02:00
Lars Melchior
8d39e34ff2 v0.9 (#46)
* always get properties

* update examples

* update travis

* add doctest example

* update example runner
2019-05-17 14:46:54 +02:00
Lars Melchior
c2a413300e Update README.md 2019-05-11 15:44:22 +02:00
Lars Melchior
5177860756 Update README.md 2019-05-11 15:12:06 +02:00
Lars Melchior
5dad9d4e9d update examples (#45) 2019-05-10 16:20:53 +02:00
Lars Melchior
539974785e Update README.md 2019-05-10 16:03:46 +02:00
Lars Melchior
e4a0cb4980 Update README.md 2019-05-10 13:10:48 +02:00
Lars Melchior
a8ccc42f43 Update README.md 2019-05-10 12:03:09 +02:00
Lars Melchior
f92ed18514 Update README.md 2019-05-10 09:56:57 +02:00
Lars Melchior
153c196c18 Update README.md 2019-05-10 09:56:34 +02:00
Lars Melchior
d91befd7ce Update README.md 2019-05-10 09:55:48 +02:00
Lars Melchior
978c101c7e Update README.md 2019-05-09 13:50:10 +02:00
Lars Melchior
8da680df9b Update README.md 2019-05-09 13:45:44 +02:00
Lars Melchior
6943b38afb Update README.md 2019-05-09 13:36:23 +02:00
Lars Melchior
cf042cf1ac Update README.md 2019-05-09 13:31:18 +02:00
Lars Melchior
bebea37c4a Update CPM.cmake 2019-05-09 13:29:12 +02:00
Lars Melchior
137bda299d Update CPM.cmake (#44)
* Update CPM.cmake

* Update CPM.cmake
2019-05-08 21:36:51 +02:00
Lars Melchior
4d557d128f Update README.md 2019-05-06 10:21:58 +02:00
Lars Melchior
9021499c52 Update CPM.cmake 2019-05-05 11:47:28 +02:00
Lars Melchior
9b97b64da2 Correct version output 2019-05-04 10:39:39 +02:00
Lars Melchior
df752ed5d3 Update CPM.cmake 2019-05-04 10:32:20 +02:00
18 changed files with 474 additions and 181 deletions

View File

@@ -1,11 +1,14 @@
language: cpp
sudo: require
dist: xenial
common_sources: &all_sources
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty
- llvm-toolchain-trusty-6.0
python:
- 3.7
matrix:
include:
@@ -41,10 +44,5 @@ before_install:
- cmake --version
script:
- cmake -Hexamples/simple -Bbuild/simple
- cmake --build build/simple
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/simple --target test
- cmake -Hexamples/complex -Bbuild/complex
- cmake --build build/complex
- CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/complex --target test
- python3 examples/run_all.py

105
README.md
View File

@@ -2,7 +2,7 @@
# CPM
CPM is a simple GIT dependency manager written in CMake. The main use-case is abstracting CMake's `FetchContent` and managing dependencies in small to medium sized projects.
CPM is a simple dependency manager written in CMake built on top of CMake's built-in [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module.
## Supported projects
@@ -10,7 +10,26 @@ Any project that you can add via `add_subdirectory` should already work with CPM
## Usage
To add a new dependency to your project simply add the Projects target name, the git URL and the version. If the git tag for this version does not match the pattern `v$VERSION`, then the exact branch or tag can be specified with the `GIT_TAG` argument. CMake options can also be supplied with the package.
After `CPM.cmake` has been added to your project, you can call `CPMAddPackage` for every dependency of the project with the following named parameters.
```cmake
CPMAddPackage(
NAME # The dependency name (usually chosen to match the target name)
VERSION # The minimum version of the dependency (optional, defaults to 0)
OPTIONS # Configuration options passed to the dependency (optional)
DOWNLOAD_ONLY # If set, the project is downloaded, but not configured (optional)
[...] # Source options, see below
)
```
The command downloads the project defined by the source options if a newer version hasn't been included before.
The source is usually a git repository, but svn and direct urls are als supported.
See the [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) documentation for all available options.
If a `GIT_TAG` hasn't been explicitly specified it defaults to `v$VERSION` which is a common convention for github projects.
After calling `CPMAddPackage`, the variables `(DEPENDENCY)_SOURCE_DIR` and `(DEPENDENCY)_BINARY_DIR` are set, where `(DEPENDENCY)` is the name of the dependency.
## Full Example
```cmake
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
@@ -19,24 +38,23 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(MyProject)
# add dependencies
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake)
include(cmake/CPM.cmake)
CPMAddPackage(
NAME LarsParser
VERSION 1.8
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
GIT_TAG v1.8 # optional here, as indirectly defined by VERSION
OPTIONS # optional CMake arguments passed to the dependency
OPTIONS
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
)
# add executable
add_executable(my-project my-project.cpp)
set_target_properties(my-project PROPERTIES CXX_STANDARD 17)
target_link_libraries(my-project LarsParser)
add_executable(myProject myProject.cpp)
set_target_properties(myProject PROPERTIES CXX_STANDARD 17)
target_link_libraries(myProject LarsParser)
```
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples.
See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples with source code.
## Adding CPM
@@ -50,9 +68,65 @@ wget -O cmake/CPM.cmake https://raw.githubusercontent.com/TheLartians/CPM/master
To update CPM to the newest version, simply update the script in the project's cmake directory, for example by running the command above. Dependencies using CPM will automatically use the updated script of the outermost project.
## Options
## Snipplets
Setting the CMake option `CPM_USE_LOCAL_PACKAGES` will activate finding packages via `find_package`. If the option `CPM_LOCAL_PACKAGES_ONLY` is set, CPM will only use local packages.
These are some small snipplets demonstrating how to include some projects used with CPM.
### Catch2
Has a CMakeLists.txt that supports `add_subdirectory`.
```cmake
CPMAddPackage(
NAME Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
VERSION 2.5.0
)
```
### google/benchmark
Has a CMakeLists.txt that supports `add_subdirectory`, but needs some configuring to work without external dependencies.
```cmake
CPMAddPackage(
NAME benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
VERSION 1.4.1
OPTIONS
"BENCHMARK_ENABLE_TESTING Off"
)
# needed to compile with C++17
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
```
### Lua
Has no CMakeLists.txt, so a target must be created manually.
```cmake
CPMAddPackage(
NAME lua
GIT_REPOSITORY https://github.com/lua/lua.git
VERSION 5-3-4
GIT_SHALLOW YES
DOWNLOAD_ONLY YES
)
FILE(GLOB lua_sources ${lua_SOURCE_DIR}/*.c)
add_library(lua STATIC ${lua_sources})
target_include_directories(lua
PUBLIC
$<BUILD_INTERFACE:${lua_SOURCE_DIR}>
)
```
## Local packages
CPM can be configured to use `find_package` to search for locally installed dependencies first.
If `CPM_LOCAL_PACKAGES_ONLY` is set, CPM will error when dependency is not found locally.
## Advantages
@@ -61,9 +135,12 @@ Setting the CMake option `CPM_USE_LOCAL_PACKAGES` will activate finding packages
- **Reproducable builds** By using versioning via git tags it is ensured that a project will always be in the same state everywhere.
- **No installation required** No need to install anything. Just add the script to your project and you're good to go.
- **No Setup required** There is a good chance your existing projects already work as CPM dependencies.
- **Simple source distribution** CPM makes including projects with source files easy, reducing the need for monolithic header files.
## Limitations
- **First version used** In diamond-shaped dependency graphs (e.g. `A` depends on `C`(v1.1) and `A` depends on `B` depends on `C`(v1.2)) the first added dependency will be used (in this case `C`@1.1). If the current version is older than the version beeing added, or if provided options are incompatible, a CMake warning will be emitted. To resolve, add the new version of the common dependency to the outer project.
- **No auto-update** To update a dependency, version numbers or git tags in the cmake scripts must be adapted manually.
- **No pre-built binaries** Unless they are installed or included in the linked repository.
- **First version used** In diamond-shaped dependency graphs (e.g. `A` depends on `C`@1.1 and `B`, which itself depends on `C`@1.2 the first added dependency will be used (in this case `C`@1.1). In this case, B requires a newer version of `C` than `A`, so CPM will emit an error. This can be resolved by updating the outermost dependency version.
- **No auto-update** To update a dependency, version must be adapted manually and there is no way for CPM to figure out the most recent version.
- **No pre-built binaries** Unless they are installed or included in the linked repository.
For projects with more complex needs and an extra setup step doesn't matter, it is worth to check out fully featured C++ package managers such as [conan](https://conan.io) or [hunter](https://github.com/ruslo/hunter) instead.

View File

@@ -28,14 +28,15 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
set(CURRENT_CPM_VERSION 0.8)
set(CURRENT_CPM_VERSION 0.9)
if(CPM_DIRECTORY)
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
if (${CPM_VERSION} VERSION_LESS ${CURRENT_CPM_VERSION})
message(AUTHOR_WARNING "${CPM_INDENT}\
A dependency is using a newer CPM version (${CPM_VERSION}) than the current project (${CURRENT_CPM_VERSION}). \
It is recommended to upgrade CPM to the most recent version. See https://github.com/TheLartians/CPM for more information.\
message(AUTHOR_WARNING "${CPM_INDENT} \
A dependency is using a more recent CPM (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \
It is recommended to upgrade CPM to the most recent version. \
See https://github.com/TheLartians/CPM for more information.\
")
endif()
return()
@@ -124,7 +125,7 @@ function(CPMAddPackage)
if (${CPM_ARGS_NAME} IN_LIST CPM_PACKAGES)
CPM_GET_PACKAGE_VERSION(${CPM_ARGS_NAME})
if(${CPM_PACKAGE_VERSION} VERSION_LESS ${CPM_ARGS_VERSION})
message(WARNING "${CPM_INDENT} newer package ${CPM_ARGS_NAME} requested (${CPM_ARGS_VERSION}, currently using ${CPM_PACKAGE_VERSION})")
message(WARNING "${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION}).")
endif()
if (CPM_ARGS_OPTIONS)
foreach(OPTION ${CPM_ARGS_OPTIONS})
@@ -135,6 +136,9 @@ function(CPMAddPackage)
endforeach()
endif()
CPM_FETCH_PACKAGE(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
CPMGetProperties(${CPM_ARGS_NAME})
SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}" PARENT_SCOPE)
SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" PARENT_SCOPE)
return()
endif()
@@ -149,6 +153,9 @@ function(CPMAddPackage)
CPM_DECLARE_PACKAGE(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION} ${CPM_ARGS_GIT_TAG} "${CPM_ARGS_UNPARSED_ARGUMENTS}")
CPM_FETCH_PACKAGE(${CPM_ARGS_NAME} ${DOWNLOAD_ONLY})
CPMGetProperties(${CPM_ARGS_NAME})
SET(${CPM_ARGS_NAME}_SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}" PARENT_SCOPE)
SET(${CPM_ARGS_NAME}_BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" PARENT_SCOPE)
endfunction()
function (CPM_DECLARE_PACKAGE PACKAGE VERSION GIT_TAG)
@@ -173,3 +180,10 @@ function (CPM_FETCH_PACKAGE PACKAGE DOWNLOAD_ONLY)
endif()
set(CPM_INDENT "${CPM_OLD_INDENT}")
endfunction()
function (CPMGetProperties PACKAGE)
FetchContent_GetProperties(${PACKAGE})
string(TOLOWER ${PACKAGE} lpackage)
SET(${PACKAGE}_SOURCE_DIR "${${lpackage}_SOURCE_DIR}" PARENT_SCOPE)
SET(${PACKAGE}_BINARY_DIR "${${lpackage}_BINARY_DIR}" PARENT_SCOPE)
endfunction()

View File

@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# ---- Dependencies ----
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME fibonacci
GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git
VERSION 1.0
)
CPMAddPackage(
NAME benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
VERSION 1.4.1
OPTIONS
"BENCHMARK_ENABLE_TESTING Off"
)
# patch google benchmark target
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
# ---- Executable ----
add_executable(CPMExampleBenchmark "main.cpp")
set_target_properties(CPMExampleBenchmark PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMExampleBenchmark fibonacci benchmark)

View File

@@ -0,0 +1,36 @@
#include <benchmark/benchmark.h>
#include <vector>
#include <algorithm>
#include <random>
#include <fibonacci.h>
std::vector<unsigned> createTestNumbers(){
std::vector<unsigned> v;
for (int i=0;i<25;++i) v.emplace_back(i);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(v.begin(), v.end(), g);
return v;
}
void fibonnacci(benchmark::State& state) {
auto numbers = createTestNumbers();
for (auto _ : state) {
for (auto v: numbers) benchmark::DoNotOptimize(fibonnacci(v));
}
}
BENCHMARK(fibonnacci);
void fastFibonacci(benchmark::State& state) {
auto numbers = createTestNumbers();
for (auto _ : state) {
for (auto v: numbers) benchmark::DoNotOptimize(fastFibonacci(v));
}
}
BENCHMARK(fastFibonacci);
BENCHMARK_MAIN();

View File

@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ----
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME fibonacci
GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git
VERSION 1.0
)
CPMAddPackage(
NAME Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
VERSION 2.5.0
)
# ---- Create binary ----
add_executable(CPMExampleCatch2 main.cpp)
target_link_libraries(CPMExampleCatch2 fibonacci Catch2)
set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
# ---- Enable testing ----
ENABLE_TESTING()
ADD_TEST(CPMExampleCatch2 CPMExampleCatch2)
# ---- Add code coverage ----
if (${ENABLE_TEST_COVERAGE})
set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
target_link_options(CPMExampleCatch2 PUBLIC "--coverage")
endif()

20
examples/catch2/main.cpp Normal file
View File

@@ -0,0 +1,20 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <fibonacci.h>
TEST_CASE("fibonnacci"){
REQUIRE(fibonnacci(0) == 0);
REQUIRE(fibonnacci(1) == 1);
REQUIRE(fibonnacci(2) == 1);
REQUIRE(fibonnacci(3) == 2);
REQUIRE(fibonnacci(4) == 3);
REQUIRE(fibonnacci(5) == 5);
REQUIRE(fibonnacci(13) == 233);
}
TEST_CASE("fastFibonnacci"){
for (unsigned i=0; i<25; ++i){
REQUIRE(fibonnacci(i) == fastFibonacci(i));
}
}

View File

@@ -1,55 +0,0 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(CPMTest)
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
# ignore locally installed projects for reproducable builds
set(CPM_REMOTE_PACKAGES_ONLY ON CACHE INTERNAL "")
# util library
CPMAddPackage(
NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
VERSION 0.7
)
# will be ignored as newer version already added
CPMAddPackage(
NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
VERSION 0.2
)
# language bindings
# uses git tag instead of version identifier
# depends on visitor library that depends on Event library and LHC
# CMake configuration arguments passed via OPTIONS
CPMAddPackage(
NAME Glue
GIT_TAG 78af65625751ad15a42ca52b842863e85b5d2adc
GIT_REPOSITORY https://github.com/TheLartians/Glue.git
VERSION 0.5.1
OPTIONS
"GLUE_ENABLE_LUA ON"
"GLUE_BUILD_LUA ON"
)
# parser library
# depends on LHC and Glue
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.8
OPTIONS
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
)
# add executable
add_executable(cpm-test-complex main.cpp)
set_target_properties(cpm-test-complex PROPERTIES CXX_STANDARD 17)
target_link_libraries(cpm-test-complex LHC LarsParser Glue)
# tests
enable_testing()
add_test(cpm-test-complex cpm-test-complex)

View File

@@ -1,52 +0,0 @@
#include <lars/parser/extension.h>
#include <lars/lua_glue.h>
#include <stdexcept>
int main() {
// create lua state
auto lua = lars::LuaState();
lua.open_libs();
// create extensions
lars::Extension extensions;
// add parser library to extension
extensions.add_extension("parser", lars::extensions::parser());
// connect parser extension to lua
extensions.connect(lua.get_glue());
// create a parser
lua.run(R"(
NumberMap = parser.Program.create()
NumberMap:setRule("Whitespace", "[ \t]")
NumberMap:setSeparatorRule("Whitespace")
NumberMap:setRuleWithCallback("Object", "'{' KeyValue (',' KeyValue)* '}'",function(e)
local N = e:size()-1
local res = {}
for i=0,N do
local a = e:get(i)
res[a:get(0):evaluate()] = a:get(1):evaluate()
end
return res
end)
NumberMap:setRule("KeyValue", "Number ':' Number")
NumberMap:setRuleWithCallback("Number", "'-'? [0-9]+", function(e) return tonumber(e:string()); end)
NumberMap:setStartRule("Object")
)");
// parse a string
lua.run("m = NumberMap:run('{1:3, 2:-1, 3:42}')");
// check result
if (lua.get_numeric("m[1]") != 3) throw std::runtime_error("unexpected result");
if (lua.get_numeric("m[2]") != -1) throw std::runtime_error("unexpected result");
if (lua.get_numeric("m[3]") != 42) throw std::runtime_error("unexpected result");
return 0;
}

View File

@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ----
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME fibonacci
GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git
VERSION 1.0
)
CPMAddPackage(
NAME doctest
GIT_REPOSITORY https://github.com/onqtam/doctest.git
VERSION 2.3.2
GIT_TAG 2.3.2
GIT_SHALLOW True
)
# ---- Create binary ----
add_executable(CPMExampleDoctest main.cpp)
target_link_libraries(CPMExampleDoctest fibonacci doctest)
set_target_properties(CPMExampleDoctest PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
# ---- Enable testing ----
ENABLE_TESTING()
ADD_TEST(CPMExampleDoctest CPMExampleDoctest)
# ---- Add code coverage ----
if (${ENABLE_TEST_COVERAGE})
set_target_properties(CPMExampleDoctest PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
target_link_options(CPMExampleDoctest PUBLIC "--coverage")
endif()

20
examples/doctest/main.cpp Normal file
View File

@@ -0,0 +1,20 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
#include <fibonacci.h>
TEST_CASE("fibonnacci"){
CHECK(fibonnacci(0) == 0);
CHECK(fibonnacci(1) == 1);
CHECK(fibonnacci(2) == 1);
CHECK(fibonnacci(3) == 2);
CHECK(fibonnacci(4) == 3);
CHECK(fibonnacci(5) == 5);
CHECK(fibonnacci(13) == 233);
}
TEST_CASE("fastFibonnacci"){
for (unsigned i=0; i<25; ++i){
CHECK(fibonnacci(i) == fastFibonacci(i));
}
}

View File

@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
include(../../cmake/CPM.cmake)
# ---- Dependencies ----
CPMAddPackage(
NAME Glue
GIT_REPOSITORY https://github.com/TheLartians/Glue.git
VERSION 0.8.1
OPTIONS
"GLUE_ENABLE_LUA ON"
"GLUE_BUILD_LUA ON"
)
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.9
OPTIONS
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
)
# ---- Create binary ----
add_executable(parser-lua main.cpp)
set_target_properties(parser-lua PROPERTIES CXX_STANDARD 17)
target_link_libraries(parser-lua LHC LarsParser Glue)

View File

@@ -0,0 +1,46 @@
#include <lars/parser/extension.h>
#include <glue/lua.h>
#include <stdexcept>
#include <iostream>
int main() {
auto lua = glue::LuaState();
lua.openStandardLibs();
lua["parser"] = lars::glue::parser();
lua.run(R"(
wordParser = parser.Program.create()
wordParser:setRule("Whitespace", "[ \t]")
wordParser:setSeparatorRule("Whitespace")
wordParser:setRule("Word", "[a-zA-Z]+")
wordParser:setRuleWithCallback("Words", "Word*", function(e)
local N = e:size()
local res = {}
for i=0,N-1 do res[#res+1] = e:get(i):string() end
return res
end)
wordParser:setStartRule("Words")
)");
lua.run(R"(
while true do
print("please enter some words or 'quit' to exit");
local input = io.read();
if input == "quit" then os.exit() end
local result
ok, err = pcall(function() result = wordParser:run(input) end)
if ok then
print("you entered " .. #result .. " words!")
else
print("error: " .. tostring(err))
end
end
)");
return 0;
}

View File

@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# add dependencies
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.7
)
# add executable
add_executable(calculator main.cpp)
set_target_properties(calculator PROPERTIES CXX_STANDARD 17)
target_link_libraries(calculator LarsParser)

57
examples/parser/main.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include <iostream>
#include <unordered_map>
#include <cmath>
#include <lars/parser/generator.h>
int main() {
using namespace std;
using VariableMap = unordered_map<string, float>;
lars::ParserGenerator<float, VariableMap &> calculator;
auto &g = calculator;
g.setSeparator(g["Whitespace"] << "[\t ]");
g["Expression"] << "Set | Sum";
g["Set" ] << "Name '=' Sum" >> [](auto e, auto &v){ return v[e[0].string()] = e[1].evaluate(v); };
g["Sum" ] << "Add | Subtract | Product";
g["Product" ] << "Multiply | Divide | Exponent";
g["Exponent" ] << "Power | Atomic";
g["Atomic" ] << "Number | Brackets | Variable";
g["Brackets" ] << "'(' Sum ')'";
g["Add" ] << "Sum '+' Product" >> [](auto e, auto &v){ return e[0].evaluate(v) + e[1].evaluate(v); };
g["Subtract" ] << "Sum '-' Product" >> [](auto e, auto &v){ return e[0].evaluate(v) - e[1].evaluate(v); };
g["Multiply" ] << "Product '*' Exponent" >> [](auto e, auto &v){ return e[0].evaluate(v) * e[1].evaluate(v); };
g["Divide" ] << "Product '/' Exponent" >> [](auto e, auto &v){ return e[0].evaluate(v) / e[1].evaluate(v); };
g["Power" ] << "Atomic ('^' Exponent)" >> [](auto e, auto &v){ return pow(e[0].evaluate(v), e[1].evaluate(v)); };
g["Variable" ] << "Name" >> [](auto e, auto &v){ return v[e[0].string()]; };
g["Name" ] << "[a-zA-Z] [a-zA-Z0-9]*";
g["Number" ] << "'-'? [0-9]+ ('.' [0-9]+)?" >> [](auto e, auto &){ return stod(e.string()); };
g.setStart(g["Expression"]);
cout << "Enter an expression to be evaluated or 'quit' to exit.\n";
VariableMap variables;
while (true) {
string str;
cout << "> ";
getline(cin,str);
if(str == "q" || str == "quit"){ break; }
try {
auto result = calculator.run(str, variables);
cout << str << " = " << result << endl;
} catch (lars::SyntaxError &error) {
auto syntax = error.syntax;
cout << " ";
cout << string(syntax->begin, ' ');
cout << string(syntax->length(), '~');
cout << "^\n";
cout << " " << "Syntax error while parsing " << syntax->rule->name << endl;
}
}
return 0;
}

29
examples/run_all.py Normal file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/python3
from pathlib import Path
from subprocess import PIPE, run
examples = [
x for x in Path(__file__).parent.iterdir() if x.is_dir() and (x / 'CMakeLists.txt').exists()
]
assert(len(examples) > 0)
def runCommand(command):
print('- %s' % command)
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
if result.returncode != 0:
print("error while running '%s':\n" % command, ' ' + str(result.stderr).replace('\n','\n '))
exit(result.returncode)
return result.stdout
print('')
for example in examples:
print("running example %s" % example.name)
print("================" + ('=' * len(example.name)))
project = Path(".") / 'build' / example.name
configure = runCommand('cmake -H%s -B%s -DCMAKE_BUILD_TYPE=RelWithDebInfo' % (example, project))
print(' ' + '\n '.join([line for line in configure.split('\n') if 'CPM:' in line]))
build = runCommand('cmake --build %s -j4' % (project))
print(' ' + '\n '.join([line for line in build.split('\n') if 'Built target' in line]))
print('')

View File

@@ -1,21 +0,0 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(CPMTest)
# add dependencies
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.7
)
# add executable
add_executable(cpm-test-simple main.cpp)
set_target_properties(cpm-test-simple PROPERTIES CXX_STANDARD 17)
target_link_libraries(cpm-test-simple LarsParser)
# tests
enable_testing()
add_test(cpm-test-simple cpm-test-simple)

View File

@@ -1,27 +0,0 @@
#include <lars/parser/generator.h>
int main() {
lars::ParserGenerator<float> g;
// Define grammar and evaluation rules
g.setSeparator(g["Whitespace"] << "[\t ]");
g["Sum" ] << "Add | Subtract | Product";
g["Product" ] << "Multiply | Divide | Atomic";
g["Atomic" ] << "Number | '(' Sum ')'";
g["Add" ] << "Sum '+' Product" >> [](auto e){ return e[0].evaluate() + e[1].evaluate(); };
g["Subtract"] << "Sum '-' Product" >> [](auto e){ return e[0].evaluate() - e[1].evaluate(); };
g["Multiply"] << "Product '*' Atomic" >> [](auto e){ return e[0].evaluate() * e[1].evaluate(); };
g["Divide" ] << "Product '/' Atomic" >> [](auto e){ return e[0].evaluate() / e[1].evaluate(); };
g["Number" ] << "'-'? [0-9]+ ('.' [0-9]+)?" >> [](auto e){ return stof(e.string()); };
g.setStart(g["Sum"]);
// Execute a string
float result = g.run("1 + 2 * (3+4)/2 - 3");
// validate result
if (result == 5) {
return 0;
} else {
return 1;
}
}