rename tests -> examples (#18)

This commit is contained in:
Lars Melchior
2019-04-15 17:54:53 +02:00
committed by GitHub
parent 923265e7ae
commit d3d8a6ab4f
5 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
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
)
# 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
# uses git tag instead of version identifier
# 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 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.8
OPTIONS
"LARS_PARSER_BUILD_GLUE_EXTENSION ON"
)
# add executable
set (CMAKE_CXX_STANDARD 17)
add_executable(cpm-test test.cpp)
target_link_libraries(cpm-test LHC LarsParser Glue)
# tests
enable_testing()
add_test(cpm-test cpm-test)

52
examples/complex/test.cpp Normal file
View File

@@ -0,0 +1,52 @@
#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;
}