Fixed User rule problem.

This commit is contained in:
yhirose 2015-06-15 13:47:59 -04:00
parent 427bbda5a7
commit 4eab716a6a
3 changed files with 34 additions and 8 deletions

View File

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

View File

@ -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

View File

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