mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-17 14:47:30 -05:00
v0.9 (#46)
* always get properties * update examples * update travis * add doctest example * update example runner
This commit is contained in:
28
examples/parser-lua/CMakeLists.txt
Normal file
28
examples/parser-lua/CMakeLists.txt
Normal 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)
|
||||
46
examples/parser-lua/main.cpp
Normal file
46
examples/parser-lua/main.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user