Merge pull request #114 from yhirose/combinators_with_ast

Combinators with AST action handler
This commit is contained in:
yhirose 2020-06-07 22:08:04 -04:00 committed by GitHub
commit 94c73b1e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 11 deletions

View File

@ -24,7 +24,7 @@ endif()
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weverything -Wno-c++98-compat -Wno-padded -Wno-weak-vtables -Wno-exit-time-destructors -Wno-c++2a-compat -Wno-switch-enum") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weverything -Wno-c++98-compat -Wno-padded -Wno-weak-vtables -Wno-exit-time-destructors -Wno-c++2a-compat -Wno-switch-enum -Wno-c++98-compat-pedantic")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpedantic -Wextra") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpedantic -Wextra")
elseif(MSVC) elseif(MSVC)

View File

@ -1803,7 +1803,7 @@ struct TokenChecker : public Ope::Visitor {
void visit(Capture &ope) override { ope.ope_->accept(*this); } void visit(Capture &ope) override { ope.ope_->accept(*this); }
void visit(TokenBoundary & /*ope*/) override { has_token_boundary_ = true; } void visit(TokenBoundary & /*ope*/) override { has_token_boundary_ = true; }
void visit(Ignore &ope) override { ope.ope_->accept(*this); } void visit(Ignore &ope) override { ope.ope_->accept(*this); }
void visit(WeakHolder &ope) override { ope.weak_.lock()->accept(*this); } void visit(WeakHolder &ope) override;
void visit(Reference &ope) override; void visit(Reference &ope) override;
void visit(Whitespace &ope) override { ope.ope_->accept(*this); } void visit(Whitespace &ope) override { ope.ope_->accept(*this); }
void visit(PrecedenceClimbing &ope) override { ope.atom_->accept(*this); } void visit(PrecedenceClimbing &ope) override { ope.atom_->accept(*this); }
@ -2733,6 +2733,8 @@ inline void AssignIDToDefinition::visit(Reference &ope) {
} }
} }
inline void TokenChecker::visit(WeakHolder & /*ope*/) { has_rule_ = true; }
inline void TokenChecker::visit(Reference &ope) { inline void TokenChecker::visit(Reference &ope) {
if (ope.is_macro_) { if (ope.is_macro_) {
ope.rule_->accept(*this); ope.rule_->accept(*this);
@ -3648,19 +3650,18 @@ struct EmptyType {};
typedef AstBase<EmptyType> Ast; typedef AstBase<EmptyType> Ast;
template <typename T = Ast> template <typename T = Ast>
void add_ast_action(Definition &rule, const std::string &name) { void add_ast_action(Definition &rule) {
rule.action = [&](const SemanticValues &sv) { rule.action = [&](const SemanticValues &sv) {
auto line = sv.line_info(); auto line = sv.line_info();
if (rule.is_token()) { if (rule.is_token()) {
return std::make_shared<T>(sv.path, line.first, line.second, return std::make_shared<T>(sv.path, line.first, line.second, rule.name.c_str(),
name.c_str(), sv.token(), sv.token(), std::distance(sv.ss, sv.c_str()),
std::distance(sv.ss, sv.c_str()),
sv.length(), sv.choice_count(), sv.choice()); sv.length(), sv.choice_count(), sv.choice());
} }
auto ast = std::make_shared<T>( auto ast = std::make_shared<T>(
sv.path, line.first, line.second, name.c_str(), sv.path, line.first, line.second, rule.name.c_str(),
sv.transform<std::shared_ptr<T>>(), std::distance(sv.ss, sv.c_str()), sv.transform<std::shared_ptr<T>>(), std::distance(sv.ss, sv.c_str()),
sv.length(), sv.choice_count(), sv.choice()); sv.length(), sv.choice_count(), sv.choice());
@ -3671,6 +3672,59 @@ void add_ast_action(Definition &rule, const std::string &name) {
}; };
} }
#define ADD_AST_ACTION(rule) { rule.name = #rule; add_ast_action(rule); }
#define AST_DEFINITIONS_1(r1) \
Definition r1; r1.name = #r1; add_ast_action(r1);
#define AST_DEFINITIONS_2(r1, r2) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_1(r2)
#define AST_DEFINITIONS_3(r1, r2, r3) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_2(r2, r3)
#define AST_DEFINITIONS_4(r1, r2, r3, r4) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_3(r2, r3, r4)
#define AST_DEFINITIONS_5(r1, r2, r3, r4, r5) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_4(r2, r3, r4, r5)
#define AST_DEFINITIONS_6(r1, r2, r3, r4, r5, r6) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_5(r2, r3, r4, r5, r6)
#define AST_DEFINITIONS_7(r1, r2, r3, r4, r5, r6, r7) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_6(r2, r3, r4, r5, r6, r7)
#define AST_DEFINITIONS_8(r1, r2, r3, r4, r5, r6, r7, r8) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_7(r2, r3, r4, r5, r6, r7, r8)
#define AST_DEFINITIONS_9(r1, r2, r3, r4, r5, r6, r7, r8, r9) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_8(r2, r3, r4, r5, r6, r7, r8, r9)
#define AST_DEFINITIONS_10(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10) \
AST_DEFINITIONS_1(r1) \
AST_DEFINITIONS_9(r2, r3, r4, r5, r6, r7, r8, r9, r10)
#define PEG_EXPAND(...) __VA_ARGS__
#define PEG_PICK(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, a51, a52, a53, a54, a55, a56, a57, a58, a59, a60, a61, a62, a63, a64, a65, a66, a67, a68, a69, a70, a71, a72, a73, a74, a75, a76, a77, a78, a79, a80, a81, a82, a83, a84, a85, a86, a87, a88, a89, a90, a91, a92, a93, a94, a95, a96, a97, a98, a99, a100, ...) a100
#define PEG_COUNT(...) PEG_EXPAND(PEG_PICK(__VA_ARGS__, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
#define PEG_CONCAT(a, b) a ## b
#define PEG_CONCAT2(a, b) PEG_CONCAT(a, b)
#define AST_DEFINITIONS(...) \
PEG_EXPAND(PEG_CONCAT2(AST_DEFINITIONS_, PEG_COUNT(__VA_ARGS__))(__VA_ARGS__))
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* parser * parser
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
@ -3800,11 +3854,8 @@ public:
template <typename T = Ast> parser &enable_ast() { template <typename T = Ast> parser &enable_ast() {
for (auto &x : *grammar_) { for (auto &x : *grammar_) {
const auto &name = x.first;
auto &rule = x.second; auto &rule = x.second;
if (!rule.action) { if (!rule.action) { add_ast_action(rule); }
add_ast_action(rule, name);
}
} }
return *this; return *this;
} }

View File

@ -675,6 +675,46 @@ TEST_CASE("Calculator test with AST", "[general]")
REQUIRE(val == -3); REQUIRE(val == -3);
} }
TEST_CASE("Calculator test with combinators and AST", "[general]") {
// Construct grammer
AST_DEFINITIONS(EXPRESSION, TERM, FACTOR, TERM_OPERATOR, FACTOR_OPERATOR, NUMBER);
EXPRESSION <= seq(TERM, zom(seq(TERM_OPERATOR, TERM)));
TERM <= seq(FACTOR, zom(seq(FACTOR_OPERATOR, FACTOR)));
FACTOR <= cho(NUMBER, seq(chr('('), EXPRESSION, chr(')')));
TERM_OPERATOR <= cls("+-");
FACTOR_OPERATOR <= cls("*/");
NUMBER <= oom(cls("0-9"));
std::function<long(const Ast &)> eval = [&](const Ast &ast) {
if (ast.name == "NUMBER") {
return stol(ast.token);
} else {
const auto &nodes = ast.nodes;
auto result = eval(*nodes[0]);
for (auto i = 1u; i < nodes.size(); i += 2) {
auto num = eval(*nodes[i + 1]);
auto ope = nodes[i]->token[0];
switch (ope) {
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/': result /= num; break;
}
}
return result;
}
};
std::shared_ptr<Ast> ast;
auto r = EXPRESSION.parse_and_get_value("1+2*3*(4-5+6)/7-8", ast);
ast = AstOptimizer(true).optimize(ast);
auto val = eval(*ast);
REQUIRE(r.ret == true);
REQUIRE(val == -3);
}
TEST_CASE("Ignore semantic value test", "[general]") TEST_CASE("Ignore semantic value test", "[general]")
{ {
parser parser(R"( parser parser(R"(