add package options (#13)

* add package options

* update tests

* update Glue package
This commit is contained in:
Lars Melchior
2019-04-14 14:49:35 +02:00
committed by GitHub
parent 77a118d3ce
commit 2d95c66fa4
5 changed files with 103 additions and 40 deletions

View File

@@ -4,34 +4,50 @@ 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.4
VERSION 0.7
)
# adding LHC again will be ignored as the package has already been added
CPMAddPackage(
NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
VERSION 0.1
)
# language bindings
# depends on visitor library that depends on Event library and LHC (ignored as already added)
# configuration arguments passed via OPTIONS. these will all be set internally
set(GLUE_ENABLE_LUA ON)
CPMAddPackage(
NAME LarsEvent
GIT_REPOSITORY https://github.com/TheLartians/Event.git
VERSION 1.0
NAME Glue
GIT_TAG 78af65625751ad15a42ca52b842863e85b5d2adc
GIT_REPOSITORY https://github.com/TheLartians/Glue.git
OPTIONS
"GLUE_ENABLE_LUA ON"
"GLUE_BUILD_LUA ON"
)
# parser library
# depends on LHC (ignored as already added)
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.4
VERSION 1.7
OPTIONS
"BUILD_LARS_PARSER_GLUE_EXTENSION ON"
)
# add executable
set (CMAKE_CXX_STANDARD 17)
add_executable(cpm-test test.cpp)
target_link_libraries(cpm-test LHC LarsEvent LarsParser)
target_link_libraries(cpm-test LHC LarsParser Glue)
# tests
enable_testing()

View File

@@ -1,30 +1,52 @@
#include <lars/parser_generator.h>
#include <lars/event.h>
#include <lars/parser/extension.h>
#include <lars/lua_glue.h>
#include <stdexcept>
int main() {
// Define the return value
int result = 1;
// create lua state
auto lua = lars::LuaState();
lua.open_libs();
// Define grammar and evaluation rules
lars::ParserGenerator<float> g;
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"]);
// create extensions
lars::Extension extensions;
// create an event
lars::Event<float> onResult;
onResult.connect([&](float v){ result = !(int(v) == 5); });
// 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}')");
// emit the result of a parsed string
onResult.notify(g.run("1 + 2 * (3+4)/2 - 3"));
// 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 the result
return result;
return 0;
}

View File

@@ -8,7 +8,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
CPMAddPackage(
NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.4
VERSION 1.7
)
# add executable

View File

@@ -1,4 +1,4 @@
#include <lars/parser_generator.h>
#include <lars/parser/generator.h>
int main() {
lars::ParserGenerator<float> g;