Skip to content

Commit

Permalink
Merge pull request #117 from peoro/master
Browse files Browse the repository at this point in the history
Fix support to custom AST node types
  • Loading branch information
yhirose authored Jun 18, 2020
2 parents c49366a + ad1a6d2 commit 8e890ce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion peglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3937,7 +3937,7 @@ class parser {
template <typename T = Ast> parser &enable_ast() {
for (auto &x : *grammar_) {
auto &rule = x.second;
if (!rule.action) { add_ast_action(rule); }
if (!rule.action) { add_ast_action<T>(rule); }
}
return *this;
}
Expand Down
18 changes: 18 additions & 0 deletions test/test1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ TEST_CASE("Skip token test2", "[general]")
REQUIRE(ret == true);
}

TEST_CASE("Custom AST test", "[general]")
{
struct CustomType {};
using CustomAst = AstBase<CustomType>;

parser parser(R"(
ROOT <- _ TEXT*
TEXT <- [a-zA-Z]+ _
_ <- [ \t\r\n]*
)");

parser.enable_ast<CustomAst>();
std::shared_ptr<CustomAst> 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"(
Expand Down

0 comments on commit 8e890ce

Please sign in to comment.