update examples (#45)

This commit is contained in:
Lars Melchior
2019-05-10 16:20:53 +02:00
committed by GitHub
parent 539974785e
commit 5dad9d4e9d
3 changed files with 18 additions and 29 deletions

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(CPMTest) project(CPMTest)
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake) include(../../cmake/CPM.cmake)
# ignore locally installed projects for reproducable builds # ignore locally installed projects for reproducable builds
set(CPM_REMOTE_PACKAGES_ONLY ON CACHE INTERNAL "") set(CPM_REMOTE_PACKAGES_ONLY ON CACHE INTERNAL "")
@@ -11,10 +11,11 @@ set(CPM_REMOTE_PACKAGES_ONLY ON CACHE INTERNAL "")
CPMAddPackage( CPMAddPackage(
NAME LHC NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git GIT_REPOSITORY https://github.com/TheLartians/LHC.git
VERSION 0.7 VERSION 0.8
) )
# will be ignored as newer version already added # will be ignored as a newer version has already been added
# if a newer version is required, a warning will be emitted
CPMAddPackage( CPMAddPackage(
NAME LHC NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git GIT_REPOSITORY https://github.com/TheLartians/LHC.git
@@ -27,9 +28,8 @@ CPMAddPackage(
# CMake configuration arguments passed via OPTIONS # CMake configuration arguments passed via OPTIONS
CPMAddPackage( CPMAddPackage(
NAME Glue NAME Glue
GIT_TAG 78af65625751ad15a42ca52b842863e85b5d2adc
GIT_REPOSITORY https://github.com/TheLartians/Glue.git GIT_REPOSITORY https://github.com/TheLartians/Glue.git
VERSION 0.5.1 VERSION 0.8.1
OPTIONS OPTIONS
"GLUE_ENABLE_LUA ON" "GLUE_ENABLE_LUA ON"
"GLUE_BUILD_LUA ON" "GLUE_BUILD_LUA ON"
@@ -40,7 +40,7 @@ CPMAddPackage(
CPMAddPackage( CPMAddPackage(
NAME LarsParser NAME LarsParser
GIT_REPOSITORY https://github.com/TheLartians/Parser.git GIT_REPOSITORY https://github.com/TheLartians/Parser.git
VERSION 1.8 VERSION 1.9
OPTIONS OPTIONS
"LARS_PARSER_BUILD_GLUE_EXTENSION ON" "LARS_PARSER_BUILD_GLUE_EXTENSION ON"
) )

View File

@@ -1,28 +1,20 @@
#include <lars/parser/extension.h> #include <lars/parser/extension.h>
#include <lars/lua_glue.h> #include <glue/lua.h>
#include <stdexcept> #include <stdexcept>
#include <iostream>
int main() { int main() {
// create lua state // create lua state
auto lua = lars::LuaState(); auto lua = glue::LuaState();
lua.open_libs(); lua.openStandardLibs();
// create extensions lua["parser"] = lars::glue::parser();
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 // create a parser
lua.run(R"( lua.run(R"(
NumberMap = parser.Program.create() NumberMap = parser.Program.create()
NumberMap:setRule("Whitespace", "[ \t]") NumberMap:setRule("Whitespace", "[ \t]")
NumberMap:setSeparatorRule("Whitespace") NumberMap:setSeparatorRule("Whitespace")
NumberMap:setRuleWithCallback("Object", "'{' KeyValue (',' KeyValue)* '}'",function(e) NumberMap:setRuleWithCallback("Object", "'{' KeyValue (',' KeyValue)* '}'",function(e)
local N = e:size()-1 local N = e:size()-1
local res = {} local res = {}
@@ -32,11 +24,8 @@ int main() {
end end
return res return res
end) end)
NumberMap:setRule("KeyValue", "Number ':' Number") NumberMap:setRule("KeyValue", "Number ':' Number")
NumberMap:setRuleWithCallback("Number", "'-'? [0-9]+", function(e) return tonumber(e:string()); end) NumberMap:setRuleWithCallback("Number", "'-'? [0-9]+", function(e) return tonumber(e:string()); end)
NumberMap:setStartRule("Object") NumberMap:setStartRule("Object")
)"); )");
@@ -44,9 +33,9 @@ int main() {
lua.run("m = NumberMap:run('{1:3, 2:-1, 3:42}')"); lua.run("m = NumberMap:run('{1:3, 2:-1, 3:42}')");
// check result // check result
if (lua.get_numeric("m[1]") != 3) throw std::runtime_error("unexpected result"); if (lua.get<int>("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<int>("m[2]") != -1) throw std::runtime_error("unexpected result");
if (lua.get_numeric("m[3]") != 42) throw std::runtime_error("unexpected result"); if (lua.get<int>("m[3]") != 42) throw std::runtime_error("unexpected result");
return 0; return 0;
} }

View File

@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(CPMTest) project(CPMTest)
# add dependencies # add dependencies
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake) include(../../cmake/CPM.cmake)
CPMAddPackage( CPMAddPackage(
NAME LarsParser NAME LarsParser