diff --git a/peglib.h b/peglib.h index 75a3b56..3fa9c97 100644 --- a/peglib.h +++ b/peglib.h @@ -1762,6 +1762,7 @@ struct AssignIDToDefinition : public Ope::Visitor { void visit(Holder &ope) override; void visit(Reference &ope) override; void visit(Whitespace &ope) override { ope.ope_->accept(*this); } + void visit(PrecedenceClimbing &ope) override; std::unordered_map ids; }; @@ -2733,6 +2734,11 @@ inline void AssignIDToDefinition::visit(Reference &ope) { } } +inline void AssignIDToDefinition::visit(PrecedenceClimbing &ope) { + ope.atom_->accept(*this); + ope.binop_->accept(*this); +} + inline void TokenChecker::visit(WeakHolder & /*ope*/) { has_rule_ = true; } inline void TokenChecker::visit(Reference &ope) { diff --git a/test/test2.cc b/test/test2.cc index a054975..9a91aa8 100644 --- a/test/test2.cc +++ b/test/test2.cc @@ -325,6 +325,24 @@ TEST_CASE("Packrat parser test with macro", "[packrat]") REQUIRE(ret == true); } +TEST_CASE("Packrat parser test with precedence expression parser", "[packrat]") { + peg::parser parser(R"( + Expression <- Atom (Operator Atom)* { precedence L + - L * / } + Atom <- _? Number _? + Number <- [0-9]+ + Operator <- '+' / '-' / '*' / '/' + _ <- ' '+ + )"); + + bool ret = parser; + REQUIRE(ret == true); + + parser.enable_packrat_parsing(); + + ret = parser.parse(" 1 + 2 * 3 "); + REQUIRE(ret == true); +} + TEST_CASE("Backreference test", "[backreference]") { parser parser(R"(