mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +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;
|
||||
})
|
||||
},
|
||||
{
|
||||
|
6
peglib.h
6
peglib.h
@ -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
|
||||
|
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