From ad1a6d27da8b21d3446e4995a66f807ea93a4ca6 Mon Sep 17 00:00:00 2001 From: peoro Date: Thu, 18 Jun 2020 16:17:27 +0200 Subject: [PATCH] Fix support to custom AST node types Fixes #116 --- peglib.h | 2 +- test/test1.cc | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/peglib.h b/peglib.h index c2ba439..62f143a 100644 --- a/peglib.h +++ b/peglib.h @@ -3937,7 +3937,7 @@ public: template parser &enable_ast() { for (auto &x : *grammar_) { auto &rule = x.second; - if (!rule.action) { add_ast_action(rule); } + if (!rule.action) { add_ast_action(rule); } } return *this; } diff --git a/test/test1.cc b/test/test1.cc index 918d07a..4f8f586 100644 --- a/test/test1.cc +++ b/test/test1.cc @@ -387,6 +387,24 @@ TEST_CASE("Skip token test2", "[general]") REQUIRE(ret == true); } +TEST_CASE("Custom AST test", "[general]") +{ + struct CustomType {}; + using CustomAst = AstBase; + + parser parser(R"( + ROOT <- _ TEXT* + TEXT <- [a-zA-Z]+ _ + _ <- [ \t\r\n]* + )"); + + parser.enable_ast(); + std::shared_ptr ast; + bool ret = parser.parse("a b c", ast); + REQUIRE(ret == true); + REQUIRE(ast->nodes.size() == 4); +} + TEST_CASE("Backtracking test", "[general]") { parser parser(R"(