Name refactoring.

pull/3/head
yhirose 10 years ago
parent c01c780044
commit 97641ed257
  1. 179
      peglib.h

@ -29,19 +29,16 @@ void* enabler;
class Any class Any
{ {
public: public:
Any() : content_(nullptr) { Any() : content_(nullptr) {}
}
Any(const Any& rhs) : content_(rhs.clone()) { Any(const Any& rhs) : content_(rhs.clone()) {}
}
Any(Any&& rhs) : content_(rhs.content_) { Any(Any&& rhs) : content_(rhs.content_) {
rhs.content_ = nullptr; rhs.content_ = nullptr;
} }
template <typename T> template <typename T>
Any(const T& value) : content_(new holder<T>(value)) { Any(const T& value) : content_(new holder<T>(value)) {}
}
Any& operator=(const Any& rhs) { Any& operator=(const Any& rhs) {
if (this != &rhs) { if (this != &rhs) {
@ -388,7 +385,7 @@ public:
class Sequence : public Ope class Sequence : public Ope
{ {
public: public:
Sequence(const Sequence& rhs) : rules_(rhs.rules_) {} Sequence(const Sequence& rhs) : opes_(rhs.opes_) {}
#if defined(_MSC_VER) && _MSC_VER < 1900 // Less than Visual Studio 2015 #if defined(_MSC_VER) && _MSC_VER < 1900 // Less than Visual Studio 2015
// NOTE: Compiler Error C2797 on Visual Studio 2013 // NOTE: Compiler Error C2797 on Visual Studio 2013
@ -399,20 +396,20 @@ public:
// generation. Visual Studio 2013 Update 3 reports this as an error." // generation. Visual Studio 2013 Update 3 reports this as an error."
template <typename... Args> template <typename... Args>
Sequence(const Args& ...args) { Sequence(const Args& ...args) {
rules_ = std::vector<std::shared_ptr<Ope>>{ static_cast<std::shared_ptr<Ope>>(args)... }; opes_ = std::vector<std::shared_ptr<Ope>>{ static_cast<std::shared_ptr<Ope>>(args)... };
} }
#else #else
template <typename... Args> template <typename... Args>
Sequence(const Args& ...args) : rules_{ static_cast<std::shared_ptr<Ope>>(args)... } {} Sequence(const Args& ...args) : opes_{ static_cast<std::shared_ptr<Ope>>(args)... } {}
#endif #endif
Sequence(const std::vector<std::shared_ptr<Ope>>& rules) : rules_(rules) {} Sequence(const std::vector<std::shared_ptr<Ope>>& opes) : opes_(opes) {}
Sequence(std::vector<std::shared_ptr<Ope>>&& rules) : rules_(std::move(rules)) {} Sequence(std::vector<std::shared_ptr<Ope>>&& opes) : opes_(std::move(opes)) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
size_t i = 0; size_t i = 0;
for (const auto& rule : rules_) { for (const auto& ope : opes_) {
auto m = rule->parse(s + i, l - i, v); auto m = ope->parse(s + i, l - i, v);
if (!m.ret) { if (!m.ret) {
return fail(); return fail();
} }
@ -422,7 +419,7 @@ public:
} }
private: private:
std::vector<std::shared_ptr<Ope>> rules_; std::vector<std::shared_ptr<Ope>> opes_;
}; };
class PrioritizedChoice : public Ope class PrioritizedChoice : public Ope
@ -437,21 +434,21 @@ public:
// generation. Visual Studio 2013 Update 3 reports this as an error." // generation. Visual Studio 2013 Update 3 reports this as an error."
template <typename... Args> template <typename... Args>
PrioritizedChoice(const Args& ...args) { PrioritizedChoice(const Args& ...args) {
rules_ = std::vector<std::shared_ptr<Ope>>{ static_cast<std::shared_ptr<Ope>>(args)... }; opes_ = std::vector<std::shared_ptr<Ope>>{ static_cast<std::shared_ptr<Ope>>(args)... };
} }
#else #else
template <typename... Args> template <typename... Args>
PrioritizedChoice(const Args& ...args) : rules_{ static_cast<std::shared_ptr<Ope>>(args)... } {} PrioritizedChoice(const Args& ...args) : opes_{ static_cast<std::shared_ptr<Ope>>(args)... } {}
#endif #endif
PrioritizedChoice(const std::vector<std::shared_ptr<Ope>>& rules) : rules_(rules) {} PrioritizedChoice(const std::vector<std::shared_ptr<Ope>>& opes) : opes_(opes) {}
PrioritizedChoice(std::vector<std::shared_ptr<Ope>>&& rules) : rules_(std::move(rules)) {} PrioritizedChoice(std::vector<std::shared_ptr<Ope>>&& opes) : opes_(std::move(opes)) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
size_t id = 0; size_t id = 0;
for (const auto& rule : rules_) { for (const auto& ope : opes_) {
Values chldsv; Values chldsv;
auto m = rule->parse(s, l, chldsv); auto m = ope->parse(s, l, chldsv);
if (m.ret) { if (m.ret) {
if (!chldsv.values.empty()) { if (!chldsv.values.empty()) {
for (const auto& x: chldsv.values) { for (const auto& x: chldsv.values) {
@ -468,21 +465,21 @@ public:
return fail(); return fail();
} }
size_t size() const { return rules_.size(); } size_t size() const { return opes_.size(); }
private: private:
std::vector<std::shared_ptr<Ope>> rules_; std::vector<std::shared_ptr<Ope>> opes_;
}; };
class ZeroOrMore : public Ope class ZeroOrMore : public Ope
{ {
public: public:
ZeroOrMore(const std::shared_ptr<Ope>& rule) : rule_(rule) {} ZeroOrMore(const std::shared_ptr<Ope>& ope) : ope_(ope) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
auto i = 0; auto i = 0;
while (l - i > 0) { while (l - i > 0) {
auto m = rule_->parse(s + i, l - i, v); auto m = ope_->parse(s + i, l - i, v);
if (!m.ret) { if (!m.ret) {
break; break;
} }
@ -492,22 +489,22 @@ public:
} }
private: private:
std::shared_ptr<Ope> rule_; std::shared_ptr<Ope> ope_;
}; };
class OneOrMore : public Ope class OneOrMore : public Ope
{ {
public: public:
OneOrMore(const std::shared_ptr<Ope>& rule) : rule_(rule) {} OneOrMore(const std::shared_ptr<Ope>& ope) : ope_(ope) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
auto m = rule_->parse(s, l, v); auto m = ope_->parse(s, l, v);
if (!m.ret) { if (!m.ret) {
return fail(); return fail();
} }
auto i = m.len; auto i = m.len;
while (l - i > 0) { while (l - i > 0) {
auto m = rule_->parse(s + i, l - i, v); auto m = ope_->parse(s + i, l - i, v);
if (!m.ret) { if (!m.ret) {
break; break;
} }
@ -517,30 +514,30 @@ public:
} }
private: private:
std::shared_ptr<Ope> rule_; std::shared_ptr<Ope> ope_;
}; };
class Option : public Ope class Option : public Ope
{ {
public: public:
Option(const std::shared_ptr<Ope>& rule) : rule_(rule) {} Option(const std::shared_ptr<Ope>& ope) : ope_(ope) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
auto m = rule_->parse(s, l, v); auto m = ope_->parse(s, l, v);
return success(m.ret ? m.len : 0); return success(m.ret ? m.len : 0);
} }
private: private:
std::shared_ptr<Ope> rule_; std::shared_ptr<Ope> ope_;
}; };
class AndPredicate : public Ope class AndPredicate : public Ope
{ {
public: public:
AndPredicate(const std::shared_ptr<Ope>& rule) : rule_(rule) {} AndPredicate(const std::shared_ptr<Ope>& ope) : ope_(ope) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
auto m = rule_->parse(s, l, v); auto m = ope_->parse(s, l, v);
if (m.ret) { if (m.ret) {
return success(0); return success(0);
} else { } else {
@ -549,16 +546,16 @@ public:
} }
private: private:
std::shared_ptr<Ope> rule_; std::shared_ptr<Ope> ope_;
}; };
class NotPredicate : public Ope class NotPredicate : public Ope
{ {
public: public:
NotPredicate(const std::shared_ptr<Ope>& rule) : rule_(rule) {} NotPredicate(const std::shared_ptr<Ope>& ope) : ope_(ope) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
auto m = rule_->parse(s, l, v); auto m = ope_->parse(s, l, v);
if (m.ret) { if (m.ret) {
return fail(); return fail();
} else { } else {
@ -567,7 +564,7 @@ public:
} }
private: private:
std::shared_ptr<Ope> rule_; std::shared_ptr<Ope> ope_;
}; };
class LiteralString : public Ope class LiteralString : public Ope
@ -654,12 +651,12 @@ public:
class Grouping : public Ope class Grouping : public Ope
{ {
public: public:
Grouping(const std::shared_ptr<Ope>& rule) : rule_(rule) {} Grouping(const std::shared_ptr<Ope>& ope) : ope_(ope) {}
Grouping(const std::shared_ptr<Ope>& rule, std::function<void(const char* s, size_t l)> match) : rule_(rule), match_(match) {} Grouping(const std::shared_ptr<Ope>& ope, std::function<void(const char* s, size_t l)> match) : ope_(ope), match_(match) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
assert(rule_); assert(ope_);
auto m = rule_->parse(s, l, v); auto m = ope_->parse(s, l, v);
if (m.ret && match_) { if (m.ret && match_) {
match_(s, m.len); match_(s, m.len);
} }
@ -667,19 +664,19 @@ public:
} }
private: private:
std::shared_ptr<Ope> rule_; std::shared_ptr<Ope> ope_;
std::function<void(const char* s, size_t l)> match_; std::function<void(const char* s, size_t l)> match_;
}; };
class WeakHolder : public Ope class WeakHolder : public Ope
{ {
public: public:
WeakHolder(const std::shared_ptr<Ope>& rule) : weak_(rule) {} WeakHolder(const std::shared_ptr<Ope>& ope) : weak_(ope) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
auto rule = weak_.lock(); auto ope = weak_.lock();
assert(rule); assert(ope);
return rule->parse(s, l, v); return ope->parse(s, l, v);
} }
private: private:
@ -712,24 +709,24 @@ public:
holder_->outer_ = this; holder_->outer_ = this;
} }
Definition(const std::shared_ptr<Ope>& rule) Definition(const std::shared_ptr<Ope>& ope)
: actions(1) : actions(1)
, holder_(std::make_shared<Holder>(this)) , holder_(std::make_shared<Holder>(this))
{ {
holder_->rule_ = rule; holder_->ope_ = ope;
} }
operator std::shared_ptr<Ope>() { operator std::shared_ptr<Ope>() {
return std::make_shared<WeakHolder>(holder_); return std::make_shared<WeakHolder>(holder_);
} }
Definition& operator<=(const std::shared_ptr<Ope>& rule) { Definition& operator<=(const std::shared_ptr<Ope>& ope) {
holder_->rule_ = rule; holder_->ope_ = ope;
return *this; return *this;
} }
Definition& rule(const std::shared_ptr<Ope>& rule) { Definition& ope(const std::shared_ptr<Ope>& ope) {
holder_->rule_ = rule; holder_->ope_ = ope;
return *this; return *this;
} }
@ -792,12 +789,12 @@ private:
: outer_(outer) {} : outer_(outer) {}
Match parse_core(const char* s, size_t l, Values& v) const { Match parse_core(const char* s, size_t l, Values& v) const {
if (!rule_) { if (!ope_) {
throw std::logic_error("Uninitialized definition rule was used..."); throw std::logic_error("Uninitialized definition ope was used...");
} }
Values chldsv; Values chldsv;
auto m = rule_->parse(s, l, chldsv); auto m = ope_->parse(s, l, chldsv);
if (m.ret) { if (m.ret) {
v.names.push_back(outer_->name); v.names.push_back(outer_->name);
@ -826,8 +823,8 @@ private:
} }
} }
std::shared_ptr<Ope> rule_; std::shared_ptr<Ope> ope_;
Definition* outer_; Definition* outer_;
}; };
Definition& operator=(const Definition& rhs); Definition& operator=(const Definition& rhs);
@ -848,11 +845,9 @@ public:
return grammar_.at(name_).holder_->parse(s, l, v); return grammar_.at(name_).holder_->parse(s, l, v);
} }
std::string name() const { return std::string(); };
private: private:
const std::map<std::string, Definition>& grammar_; const std::map<std::string, Definition>& grammar_;
std::string name_; const std::string name_;
}; };
typedef Definition rule; typedef Definition rule;
@ -870,24 +865,24 @@ std::shared_ptr<Ope> cho(Args&& ...args) {
return std::make_shared<PrioritizedChoice>(static_cast<std::shared_ptr<Ope>>(args)...); return std::make_shared<PrioritizedChoice>(static_cast<std::shared_ptr<Ope>>(args)...);
} }
inline std::shared_ptr<Ope> zom(const std::shared_ptr<Ope>& rule) { inline std::shared_ptr<Ope> zom(const std::shared_ptr<Ope>& ope) {
return std::make_shared<ZeroOrMore>(rule); return std::make_shared<ZeroOrMore>(ope);
} }
inline std::shared_ptr<Ope> oom(const std::shared_ptr<Ope>& rule) { inline std::shared_ptr<Ope> oom(const std::shared_ptr<Ope>& ope) {
return std::make_shared<OneOrMore>(rule); return std::make_shared<OneOrMore>(ope);
} }
inline std::shared_ptr<Ope> opt(const std::shared_ptr<Ope>& rule) { inline std::shared_ptr<Ope> opt(const std::shared_ptr<Ope>& ope) {
return std::make_shared<Option>(rule); return std::make_shared<Option>(ope);
} }
inline std::shared_ptr<Ope> apd(const std::shared_ptr<Ope>& rule) { inline std::shared_ptr<Ope> apd(const std::shared_ptr<Ope>& ope) {
return std::make_shared<AndPredicate>(rule); return std::make_shared<AndPredicate>(ope);
} }
inline std::shared_ptr<Ope> npd(const std::shared_ptr<Ope>& rule) { inline std::shared_ptr<Ope> npd(const std::shared_ptr<Ope>& ope) {
return std::make_shared<NotPredicate>(rule); return std::make_shared<NotPredicate>(ope);
} }
inline std::shared_ptr<Ope> lit(const char* lit) { inline std::shared_ptr<Ope> lit(const char* lit) {
@ -906,12 +901,12 @@ inline std::shared_ptr<Ope> any() {
return std::make_shared<AnyCharacter>(); return std::make_shared<AnyCharacter>();
} }
inline std::shared_ptr<Ope> grp(const std::shared_ptr<Ope>& rule) { inline std::shared_ptr<Ope> grp(const std::shared_ptr<Ope>& ope) {
return std::make_shared<Grouping>(rule); return std::make_shared<Grouping>(ope);
} }
inline std::shared_ptr<Ope> grp(const std::shared_ptr<Ope>& rule, std::function<void (const char* s, size_t l)> match) { inline std::shared_ptr<Ope> grp(const std::shared_ptr<Ope>& ope, std::function<void (const char* s, size_t l)> match) {
return std::make_shared<Grouping>(rule, match); return std::make_shared<Grouping>(ope, match);
} }
inline std::shared_ptr<Ope> ref(const std::map<std::string, Definition>& grammar, const std::string& name) { inline std::shared_ptr<Ope> ref(const std::map<std::string, Definition>& grammar, const std::string& name) {
@ -1006,51 +1001,51 @@ inline std::shared_ptr<Grammar> make_grammar(const char* syntax, std::string& st
}; };
peg["Expression"] = [&](const std::vector<Any>& v) { peg["Expression"] = [&](const std::vector<Any>& v) {
std::vector<std::shared_ptr<Ope>> rules; std::vector<std::shared_ptr<Ope>> opes;
for (auto i = 0u; i < v.size(); i++) { for (auto i = 0u; i < v.size(); i++) {
if (!(i % 2)) { if (!(i % 2)) {
rules.push_back(v[i].get<std::shared_ptr<Ope>>()); opes.push_back(v[i].get<std::shared_ptr<Ope>>());
} }
} }
return static_cast<std::shared_ptr<Ope>>(std::make_shared<PrioritizedChoice>(rules)); return static_cast<std::shared_ptr<Ope>>(std::make_shared<PrioritizedChoice>(opes));
}; };
peg["Sequence"] = [&](const std::vector<Any>& v) { peg["Sequence"] = [&](const std::vector<Any>& v) {
std::vector<std::shared_ptr<Ope>> rules; std::vector<std::shared_ptr<Ope>> opes;
for (const auto& x: v) { for (const auto& x: v) {
rules.push_back(x.get<std::shared_ptr<Ope>>()); opes.push_back(x.get<std::shared_ptr<Ope>>());
} }
return static_cast<std::shared_ptr<Ope>>(std::make_shared<Sequence>(rules)); return static_cast<std::shared_ptr<Ope>>(std::make_shared<Sequence>(opes));
}; };
peg["Prefix"] = [&](const std::vector<Any>& v, const std::vector<std::string>& n) { peg["Prefix"] = [&](const std::vector<Any>& v, const std::vector<std::string>& n) {
std::shared_ptr<Ope> rule; std::shared_ptr<Ope> ope;
if (v.size() == 1) { if (v.size() == 1) {
rule = v[0].get<std::shared_ptr<Ope>>(); ope = v[0].get<std::shared_ptr<Ope>>();
} else { } else {
assert(v.size() == 2); assert(v.size() == 2);
rule = v[1].get<std::shared_ptr<Ope>>(); ope = v[1].get<std::shared_ptr<Ope>>();
if (n[0] == "AND") { if (n[0] == "AND") {
rule = apd(rule); ope = apd(ope);
} else { // "NOT" } else { // "NOT"
rule = npd(rule); ope = npd(ope);
} }
} }
return rule; return ope;
}; };
peg["Suffix"] = [&](const std::vector<Any>& v, const std::vector<std::string>& n) { peg["Suffix"] = [&](const std::vector<Any>& v, const std::vector<std::string>& n) {
auto rule = v[0].get<std::shared_ptr<Ope>>(); auto ope = v[0].get<std::shared_ptr<Ope>>();
if (v.size() == 1) { if (v.size() == 1) {
return rule; return ope;
} else { } else {
assert(v.size() == 2); assert(v.size() == 2);
if (n[1] == "QUESTION") { if (n[1] == "QUESTION") {
return opt(rule); return opt(ope);
} else if (n[1] == "STAR") { } else if (n[1] == "STAR") {
return zom(rule); return zom(ope);
} else { // "PLUS" } else { // "PLUS"
return oom(rule); return oom(ope);
} }
} }
}; };

Loading…
Cancel
Save