mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Fixed User rule problem.
This commit is contained in:
parent
427bbda5a7
commit
4eab716a6a
10
README.md
10
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<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;
|
||||
})
|
||||
},
|
||||
{
|
||||
|
26
test/test.cc
26
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<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…
Reference in New Issue
Block a user