Simplified API.

This commit is contained in:
yhirose 2015-06-15 21:39:35 -04:00
parent 0744fa75f6
commit 30971d28de
2 changed files with 11 additions and 29 deletions

View File

@ -93,7 +93,6 @@ struct SemanticValue {
// Utility method // Utility method
template <typename T> T& get(); template <typename T> T& get();
template <typename T> const T& get() const; template <typename T> const T& get() const;
std::string str() const;
}; };
struct SemanticValues : protected std::vector<SemanticValue> struct SemanticValues : protected std::vector<SemanticValue>
@ -102,16 +101,10 @@ struct SemanticValues : protected std::vector<SemanticValue>
size_t n; // Token length size_t n; // Token length
size_t choice; // Choice number (0 based index) size_t choice; // Choice number (0 based index)
using std::vector<T>::size;
using std::vector<T>::operator[];
using std::vector<T>::begin;
using std::vector<T>::end;
// NOTE: There are more std::vector methods available...
// Transform the semantice values vector to another vector // Transform the semantice values vector to another vector
template <typename F> auto map(size_t beg, size_t end, F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type>; template <typename F> auto transform(size_t beg, size_t end, F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type>;
template <typename F> auto map(F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type>; template <typename F> auto transform(F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type>;
template <typename T> auto map(size_t beg = 0, size_t end = -1) const -> vector<T>; template <typename T> auto transform(size_t beg = 0, size_t end = -1) const -> vector<T>;
} }
``` ```

View File

@ -171,10 +171,6 @@ struct SemanticValue
const T& get() const { const T& get() const {
return val.get<T>(); return val.get<T>();
} }
std::string str() const {
return std::string(s, n);
}
}; };
struct SemanticValues : protected std::vector<SemanticValue> struct SemanticValues : protected std::vector<SemanticValue>
@ -185,13 +181,6 @@ struct SemanticValues : protected std::vector<SemanticValue>
SemanticValues() : s(nullptr), n(0), choice(0) {} SemanticValues() : s(nullptr), n(0), choice(0) {}
std::string str(size_t i = 0) const {
if (i > 0) {
return (*this)[i].str();
}
return std::string(s, n);
}
typedef SemanticValue T; typedef SemanticValue T;
using std::vector<T>::iterator; using std::vector<T>::iterator;
using std::vector<T>::const_iterator; using std::vector<T>::const_iterator;
@ -217,7 +206,7 @@ struct SemanticValues : protected std::vector<SemanticValue>
using std::vector<T>::emplace_back; using std::vector<T>::emplace_back;
template <typename F> template <typename F>
auto map(F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> { auto transform(F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> {
vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> r; vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> r;
for (const auto& v: *this) { for (const auto& v: *this) {
r.push_back(f(v)); r.push_back(f(v));
@ -226,7 +215,7 @@ struct SemanticValues : protected std::vector<SemanticValue>
} }
template <typename F> template <typename F>
auto map(size_t beg, size_t end, F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> { auto transform(size_t beg, size_t end, F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> {
vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> r; vector<typename std::remove_const<decltype(f(SemanticValue()))>::type> r;
end = std::min(end, size()); end = std::min(end, size());
for (size_t i = beg; i < end; i++) { for (size_t i = beg; i < end; i++) {
@ -236,8 +225,8 @@ struct SemanticValues : protected std::vector<SemanticValue>
} }
template <typename T> template <typename T>
auto map(size_t beg = 0, size_t end = -1) const -> vector<T> { auto transform(size_t beg = 0, size_t end = -1) const -> vector<T> {
return this->map(beg, end, [](const SemanticValue& v) { return v.get<T>(); }); return this->transform(beg, end, [](const SemanticValue& v) { return v.get<T>(); });
} }
}; };
@ -469,11 +458,11 @@ struct Context
std::vector<bool> cache_register; std::vector<bool> cache_register;
std::vector<bool> cache_success; std::vector<bool> cache_success;
std::map<std::pair<size_t, size_t>, std::tuple<size_t, any>> cache_result;
std::vector<std::shared_ptr<SemanticValues>> stack; std::vector<std::shared_ptr<SemanticValues>> stack;
size_t stack_size; size_t stack_size;
std::map<std::pair<size_t, size_t>, std::tuple<size_t, any>> cache_result;
Context(const char* _s, size_t _l, size_t _def_count, bool enablePackratParsing) Context(const char* _s, size_t _l, size_t _def_count, bool enablePackratParsing)
: s(_s) : s(_s)
, l(_l) , l(_l)
@ -2252,7 +2241,7 @@ private:
std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>(); std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>();
return ast; return ast;
} }
return std::make_shared<Ast>(info.name, info.tag, sv.map<std::shared_ptr<Ast>>()); return std::make_shared<Ast>(info.name, info.tag, sv.transform<std::shared_ptr<Ast>>());
}; };
} }
@ -2271,7 +2260,7 @@ private:
std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>(); std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>();
return ast; return ast;
} }
return std::make_shared<Ast>(name.c_str(), AstDefaultTag, sv.map<std::shared_ptr<Ast>>()); return std::make_shared<Ast>(name.c_str(), AstDefaultTag, sv.transform<std::shared_ptr<Ast>>());
}; };
} }
} }