mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Merge pull request #117 from peoro/master
Fix support to custom AST node types
This commit is contained in:
commit
8e890ced7f
2
peglib.h
2
peglib.h
@ -3937,7 +3937,7 @@ public:
|
|||||||
template <typename T = Ast> parser &enable_ast() {
|
template <typename T = Ast> parser &enable_ast() {
|
||||||
for (auto &x : *grammar_) {
|
for (auto &x : *grammar_) {
|
||||||
auto &rule = x.second;
|
auto &rule = x.second;
|
||||||
if (!rule.action) { add_ast_action(rule); }
|
if (!rule.action) { add_ast_action<T>(rule); }
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -387,6 +387,24 @@ TEST_CASE("Skip token test2", "[general]")
|
|||||||
REQUIRE(ret == true);
|
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]")
|
TEST_CASE("Backtracking test", "[general]")
|
||||||
{
|
{
|
||||||
parser parser(R"(
|
parser parser(R"(
|
||||||
|
Loading…
Reference in New Issue
Block a user