diff --git a/README.md b/README.md index 9b83e7b..a24307d 100644 --- a/README.md +++ b/README.md @@ -304,14 +304,14 @@ auto syntax = R"( Rules rules = { { - "NAME", usr([](const char* s, size_t n, SemanticValues& sv, any& c) { + "NAME", usr([](const char* s, size_t n, SemanticValues& sv, any& c) -> size_t { static vector names = { "PEG", "BNF" }; - for (const auto& n: names) { - if (n.size() <= n && !n.compare(0, n.size(), s, n.size())) { - return success(n.size()); + for (const auto& name: names) { + if (name.size() <= n && !name.compare(0, name.size(), s, name.size())) { + return name.size(); } } - return fail(s); + return -1; }) }, { diff --git a/peglib.h b/peglib.h index cbd8a5e..a634209 100644 --- a/peglib.h +++ b/peglib.h @@ -284,7 +284,7 @@ any call(F fn, Args&&... args) { /* * Predicate */ -typedef std::function Predicate; +typedef std::function Predicate; #endif class Action @@ -902,7 +902,7 @@ public: std::shared_ptr ope_; }; -typedef std::function Parser; +typedef std::function Parser; class User : public Ope { @@ -916,7 +916,7 @@ public: void accept(Visitor& v) override; - std::function fn_; + std::function fn_; }; class WeakHolder : public Ope diff --git a/test/test.cc b/test/test.cc index 111a7b4..6374af5 100644 --- a/test/test.cc +++ b/test/test.cc @@ -613,6 +613,32 @@ TEST_CASE("Left recursive with empty string test", "[left recursive]") REQUIRE(parser == false); } +TEST_CASE("User rule test", "[user rule]") +{ + auto syntax = " ROOT <- _ 'Hello' _ NAME '!' _ "; + + Rules rules = { + { + "NAME", usr([](const char* s, size_t n, SemanticValues& sv, any& c) -> size_t { + static vector names = { "PEG", "BNF" }; + for (const auto& name: names) { + if (name.size() <= n && !name.compare(0, name.size(), s, name.size())) { + return name.size(); + } + } + return -1; + }) + }, + { + "~_", zom(cls(" \t\r\n")) + } + }; + + peg g = peg(syntax, rules); + + REQUIRE(g.parse(" Hello BNF! ") == true); +} + bool exact(Grammar& g, const char* d, const char* s) { auto n = strlen(s); auto r = g[d].parse(s, n);