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

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