From 5dad9d4e9d9ab42be1c45a5f65f0ceb04b939c29 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Fri, 10 May 2019 16:20:53 +0200 Subject: [PATCH] update examples (#45) --- examples/complex/CMakeLists.txt | 12 ++++++------ examples/complex/main.cpp | 33 +++++++++++---------------------- examples/simple/CMakeLists.txt | 2 +- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/examples/complex/CMakeLists.txt b/examples/complex/CMakeLists.txt index b59290f..8f704bf 100644 --- a/examples/complex/CMakeLists.txt +++ b/examples/complex/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(CPMTest) -include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake) +include(../../cmake/CPM.cmake) # ignore locally installed projects for reproducable builds set(CPM_REMOTE_PACKAGES_ONLY ON CACHE INTERNAL "") @@ -11,10 +11,11 @@ set(CPM_REMOTE_PACKAGES_ONLY ON CACHE INTERNAL "") CPMAddPackage( NAME LHC 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( NAME LHC GIT_REPOSITORY https://github.com/TheLartians/LHC.git @@ -27,9 +28,8 @@ CPMAddPackage( # CMake configuration arguments passed via OPTIONS CPMAddPackage( NAME Glue - GIT_TAG 78af65625751ad15a42ca52b842863e85b5d2adc GIT_REPOSITORY https://github.com/TheLartians/Glue.git - VERSION 0.5.1 + VERSION 0.8.1 OPTIONS "GLUE_ENABLE_LUA ON" "GLUE_BUILD_LUA ON" @@ -40,7 +40,7 @@ CPMAddPackage( CPMAddPackage( NAME LarsParser GIT_REPOSITORY https://github.com/TheLartians/Parser.git - VERSION 1.8 + VERSION 1.9 OPTIONS "LARS_PARSER_BUILD_GLUE_EXTENSION ON" ) diff --git a/examples/complex/main.cpp b/examples/complex/main.cpp index 959ede9..595d573 100644 --- a/examples/complex/main.cpp +++ b/examples/complex/main.cpp @@ -1,29 +1,21 @@ #include -#include +#include #include +#include 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()); + auto lua = glue::LuaState(); + lua.openStandardLibs(); + lua["parser"] = lars::glue::parser(); + // create a parser lua.run(R"( NumberMap = parser.Program.create() - NumberMap:setRule("Whitespace", "[ \t]") NumberMap:setSeparatorRule("Whitespace") - - NumberMap:setRuleWithCallback("Object", "'{' KeyValue (',' KeyValue)* '}'",function(e) + NumberMap:setRuleWithCallback("Object", "'{' KeyValue (',' KeyValue)* '}'",function(e) local N = e:size()-1 local res = {} for i=0,N do @@ -32,21 +24,18 @@ int main() { 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"); + if (lua.get("m[1]") != 3) throw std::runtime_error("unexpected result"); + if (lua.get("m[2]") != -1) throw std::runtime_error("unexpected result"); + if (lua.get("m[3]") != 42) throw std::runtime_error("unexpected result"); return 0; } diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index 7a6238a..518b76d 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(CPMTest) # add dependencies -include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake) +include(../../cmake/CPM.cmake) CPMAddPackage( NAME LarsParser