* always get properties

* update examples

* update travis

* add doctest example

* update example runner
This commit is contained in:
Lars Melchior
2019-05-17 14:46:54 +02:00
committed by GitHub
parent c2a413300e
commit 8d39e34ff2
18 changed files with 413 additions and 188 deletions

View 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)

View 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;
}