Fixed User rule problem.

pull/3/head
yhirose 9 years ago
parent 427bbda5a7
commit 4eab716a6a
  1. 10
      README.md
  2. 6
      peglib.h
  3. 26
      test/test.cc

@ -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<string> 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;
})
},
{

@ -284,7 +284,7 @@ any call(F fn, Args&&... args) {
/*
* Predicate
*/
typedef std::function<bool(const char* s, size_t n, const any& val, const any& dt)> Predicate;
typedef std::function<bool (const char* s, size_t n, const any& val, const any& dt)> Predicate;
#endif
class Action
@ -902,7 +902,7 @@ public:
std::shared_ptr<Ope> ope_;
};
typedef std::function<size_t(const char* s, size_t n, SemanticValues& sv, any& dt)> Parser;
typedef std::function<size_t (const char* s, size_t n, SemanticValues& sv, any& dt)> Parser;
class User : public Ope
{
@ -916,7 +916,7 @@ public:
void accept(Visitor& v) override;
std::function<size_t(const char* s, size_t n, SemanticValues& sv, any& dt)> fn_;
std::function<size_t (const char* s, size_t n, SemanticValues& sv, any& dt)> fn_;
};
class WeakHolder : public Ope

@ -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<string> 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);

Loading…
Cancel
Save