Skip to content

Commit

Permalink
v1.5 (#21)
Browse files Browse the repository at this point in the history
* rename files

* add extension

* add extension test

* refactor peg grammar program

* update parser extension test

* update deps

* update travis

* update Glue

* v1.5
  • Loading branch information
TheLartians authored Apr 10, 2019
1 parent 3e90a90 commit 6589ec5
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ before_install:
- sudo sh cmake.sh --skip-license --exclude-subdir --prefix=/usr/local

install:
- cmake -H. -Bbuild
- cmake -H. -Bbuild -DBUILD_LARS_PARSER_GLUE_EXTENSION=On
- cmake --build build
- sudo cmake --build build --target install

Expand Down
40 changes: 35 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ endif()
# ---- Project ----

project(LarsParser
VERSION 1.4
VERSION 1.5
LANGUAGES CXX
)

Expand All @@ -19,6 +19,7 @@ endif()

option(BUILD_LARS_PARSER_EXAMPLES "Enable examples" OFF)
option(ENABLE_LARS_PARSER_TESTS "Enable tests" OFF)
option(BUILD_LARS_PARSER_GLUE_EXTENSION "Build LarsParser Glue extension" OFF)

# ---- Dependencies ----

Expand All @@ -27,9 +28,17 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake)
CPMAddPackage(
NAME LHC
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
VERSION 0.3
VERSION 0.4
)

if(BUILD_LARS_PARSER_GLUE_EXTENSION)
CPMAddPackage(
NAME Glue
GIT_REPOSITORY https://github.com/TheLartians/Glue.git
VERSION 0.3
)
endif()

# ---- Minimum CXX ----

if(NOT CMAKE_CXX_STANDARD GREATER 17)
Expand All @@ -38,12 +47,34 @@ endif()

# ---- Create library ----

FILE(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/include/lars/*.h")
FILE(GLOB sources "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
SET(headers
"${CMAKE_CURRENT_SOURCE_DIR}/include/lars/parser/grammar.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/lars/parser/parser.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/lars/parser/interpreter.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/lars/parser/generator.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/lars/parser/peg.h"
)

SET(sources
"${CMAKE_CURRENT_SOURCE_DIR}/source/grammar.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/source/parser.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/source/interpreter.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/source/peg.cpp"
)

if(BUILD_LARS_PARSER_GLUE_EXTENSION)
LIST(APPEND headers "${CMAKE_CURRENT_SOURCE_DIR}/include/lars/parser/extension.h")
LIST(APPEND sources "${CMAKE_CURRENT_SOURCE_DIR}/source/extension.cpp")
endif()

add_library(LarsParser ${sources} ${headers})

target_link_libraries(LarsParser PRIVATE LHC)

if(BUILD_LARS_PARSER_GLUE_EXTENSION)
target_link_libraries(LarsParser PUBLIC Glue)
endif()

target_include_directories(LarsParser
PUBLIC
$<INSTALL_INTERFACE:include>
Expand All @@ -58,7 +89,6 @@ write_basic_package_version_file(
COMPATIBILITY AnyNewerVersion
)


# ---- Install ----

install(
Expand Down
9 changes: 6 additions & 3 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ set(_CPM_Dir "${CMAKE_CURRENT_LIST_DIR}")

include(CMakeParseArguments)
include(${_CPM_Dir}/DownloadProject.cmake)

function(CPMHasPackage)

endfunction()
option(CPM_OFFLINE "CPM offline mode" OFF)

if(NOT ${CPM_OFFLINE})
set(CPM_PACKAGES "" CACHE INTERNAL "CPM Packages")
endif()

function(CPMAddPackage)
set(options QUIET)
Expand All @@ -16,6 +18,7 @@ function(CPMAddPackage)
VERSION
GIT_TAG
BINARY_DIR
UPDATE_DISCONNECTED
)

set(multiValueArgs "")
Expand Down
2 changes: 1 addition & 1 deletion cmake/CPMProject.CMakeLists.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ else()
PROJ @CPM_ARGS_NAME@
GIT_REPOSITORY @CPM_ARGS_GIT_REPOSITORY@
GIT_TAG @CPM_ARGS_GIT_TAG@
UPDATE_DISCONNECTED 1
UPDATE_DISCONNECTED @CPM_OFFLINE@
GIT_SHALLOW 1
PREFIX @CPM_ARGS_BINARY_DIR@/dl
QUIET
Expand Down
5 changes: 5 additions & 0 deletions cmake/LarsParserConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

find_dependency(LHC)

if(@BUILD_LARS_PARSER_GLUE_EXTENSION@)
find_dependency(Glue)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/LarsParserTargets.cmake")
check_required_components("@PROJECT_NAME@")

2 changes: 1 addition & 1 deletion examples/calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <unordered_map>
#include <cmath>

#include <lars/parser_generator.h>
#include <lars/parser/generator.h>

int main() {
using namespace std;
Expand Down
2 changes: 1 addition & 1 deletion examples/calculator_sequental.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <cmath>
#include <numeric>

#include <lars/parser_generator.h>
#include <lars/parser/generator.h>

int main() {
using namespace std;
Expand Down
2 changes: 1 addition & 1 deletion examples/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <iostream>
#include <unordered_set>

#include <lars/parser_generator.h>
#include <lars/parser/generator.h>

int main() {
using namespace std;
Expand Down
9 changes: 9 additions & 0 deletions include/lars/parser/extension.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <lars/glue.h>

namespace lars{
namespace extensions{
std::shared_ptr<Extension> parser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ namespace lars {

template <class R = void, typename ... Args> class ParserGenerator: public Program<R, Args ...> {
private:
Program<peg::GrammarNode::Shared> grammarProgram;
peg::GrammarProgram grammarProgram;
std::unordered_map<std::string, std::shared_ptr<peg::Rule>> rules;
peg::GrammarNode::Shared separatorRule;

public:

ParserGenerator(){
grammarProgram = peg::createGrammarProgram([this](const std::string_view &name){
return getRuleNode(std::string(name));
});
grammarProgram = peg::createGrammarProgram();
}

std::shared_ptr<peg::Rule> getRule(const std::string &name) {
Expand Down Expand Up @@ -45,8 +42,13 @@ namespace lars {
return rule;
}

peg::GrammarNode::Shared parseRule(const std::string_view &grammar){
peg::RuleGetter rg = [this](const auto &name){ return getRuleNode(std::string(name)); };
return grammarProgram.run(grammar, rg);
}

std::shared_ptr<peg::Rule> setRule(const std::string &name, const std::string_view &grammar, const typename Interpreter<R, Args ...>::Callback &callback = typename Interpreter<R, Args ...>::Callback()){
return setRule(name, grammarProgram.run(grammar), callback);
return setRule(name, parseRule(grammar), callback);
}

template <class R2, typename ... Args2> std::shared_ptr<peg::Rule> setProgramRule(const std::string &name, Program<R2, Args2 ...> subprogram, std::function<R(typename Interpreter<R2, Args2 ...>::Expression,Args...)> callback = [](auto e, Args...){ return e.evaluate(); }){
Expand All @@ -59,7 +61,7 @@ namespace lars {
}

std::shared_ptr<peg::Rule> setFilteredRule(const std::string &name, const std::string_view &grammar, const peg::GrammarNode::FilterCallback &filter, const typename Interpreter<R, Args ...>::Callback &callback = typename Interpreter<R, Args ...>::Callback()){
return setRule(name, peg::GrammarNode::Sequence({grammarProgram.run(grammar), peg::GrammarNode::Filter(filter)}), callback);
return setRule(name, peg::GrammarNode::Sequence({parseRule(grammar), peg::GrammarNode::Filter(filter)}), callback);
}

void setSeparator(const std::shared_ptr<peg::Rule> &rule){
Expand All @@ -74,7 +76,7 @@ namespace lars {
}

std::shared_ptr<peg::Rule> setSeparatorRule(const std::string &name, const std::string_view &grammar){
return setSeparatorRule(name, grammarProgram.run(grammar));
return setSeparatorRule(name, parseRule(grammar));
}

void setStart(const std::shared_ptr<peg::Rule> &rule){
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ namespace lars {
const char * what()const noexcept override;
};


template <class R, typename ... Args> struct Program {
using Expression = typename Interpreter<R, Args...>::Expression;

Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion include/lars/peg.h → include/lars/parser/peg.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ namespace lars {
std::function<char(char)> defaultEscapeCodeCallback();
Program<char> createCharacterProgram(const std::function<char(char)> escapeCodeCallback = defaultEscapeCodeCallback());
Program<std::string> createStringProgram(const std::string &open, const std::string &close);
Program<peg::GrammarNode::Shared> createGrammarProgram(const std::function<GrammarNode::Shared(const std::string_view &)> &getRule);

using RuleGetter = const std::function<GrammarNode::Shared(const std::string_view &)> &;
using GrammarProgram = Program<peg::GrammarNode::Shared, RuleGetter &>;
GrammarProgram createGrammarProgram();
}

}
74 changes: 74 additions & 0 deletions source/extension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <lars/parser/extension.h>
#include <lars/parser/generator.h>
#include <stdexcept>
#include <lars/log.h>

std::shared_ptr<lars::Extension> lars::extensions::parser(){
using namespace lars;

using ParserGenerator = lars::ParserGenerator<Any,Any&>;
using Expression = ParserGenerator::Expression;

auto expressionExtension = std::make_shared<Extension>();
expressionExtension->set_class<ParserGenerator::Expression>();

expressionExtension->add_function("evaluate", [](Expression &e,Any &d){
return e.evaluate(d);
});

expressionExtension->add_function("size", [](Expression &e)->unsigned{
return e.size();
});

expressionExtension->add_function("get", [](Expression &e, unsigned i){
if (i < e.size()) {
return e[i];
} else {
throw std::runtime_error("invalid expression index");
}
});

expressionExtension->add_function("string", [](Expression &e){
return e.string();
});

expressionExtension->add_function("position", [](Expression &e){
return e.position();
});

expressionExtension->add_function("length", [](Expression &e){
return e.length();
});

auto parserGeneratorExtension = std::make_shared<Extension>();
parserGeneratorExtension->set_class<ParserGenerator>();
parserGeneratorExtension->add_function("create", [](){ return ParserGenerator(); });

parserGeneratorExtension->add_function("run", [](ParserGenerator &g, const std::string &str, Any& arg){
return g.run(str, arg);
});

parserGeneratorExtension->add_function("setRule",[](ParserGenerator &g, const std::string &name, const std::string &grammar){
return g.setRule(name, grammar);
});

parserGeneratorExtension->add_function("setRuleWithCallback",[](ParserGenerator &g, const std::string &name, const std::string &grammar, AnyFunction callback){
return g.setRule(name, grammar, [callback](auto e, Any v){
return callback(e, v);
});
});

parserGeneratorExtension->add_function("setStartRule", [](ParserGenerator &g, const std::string &name){
g.setStart(g.getRule(name));
});

parserGeneratorExtension->add_function("setSeparatorRule", [](ParserGenerator &g, const std::string &name){
g.setSeparator(g.getRule(name));
});

auto extension = std::make_shared<Extension>();
extension->add_extension("Program", parserGeneratorExtension);
extension->add_extension("Expression", expressionExtension);

return extension;
}
4 changes: 2 additions & 2 deletions source/grammar.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <lars/grammar.h>
#include <lars/parser/grammar.h>
#include <lars/parser/interpreter.h>
#include <lars/iterators.h>
#include <lars/interpreter.h>

using namespace lars::peg;

Expand Down
2 changes: 1 addition & 1 deletion source/interpreter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <lars/interpreter.h>
#include <lars/parser/interpreter.h>
#include <lars/to_string.h>
#include <lars/iterators.h>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion source/parser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include <lars/parser.h>
#include <lars/parser/parser.h>
#include <lars/to_string.h>
#include <lars/iterators.h>
#include <lars/hashers.h>
Expand Down
Loading

0 comments on commit 6589ec5

Please sign in to comment.