mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Added str() in SemanticValues.
This commit is contained in:
parent
dff2db0a8e
commit
c9b5f0ac3b
13
README.md
13
README.md
@ -60,7 +60,7 @@ int main(void) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
parser["Number"] = [](const SemanticValues& sv) {
|
parser["Number"] = [](const SemanticValues& sv) {
|
||||||
return stoi(string(sv.s, sv.n), nullptr, 10);
|
return stoi(sv.str(), nullptr, 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
// (4) Parse
|
// (4) Parse
|
||||||
@ -89,7 +89,7 @@ struct SemanticValue {
|
|||||||
const char* s; // Token start for the semantic value
|
const char* s; // Token start for the semantic value
|
||||||
size_t n; // Token length for the semantic value
|
size_t n; // Token length for the semantic value
|
||||||
|
|
||||||
// Utility method
|
// Cast semantic value
|
||||||
template <typename T> T& get();
|
template <typename T> T& get();
|
||||||
template <typename T> const T& get() const;
|
template <typename T> const T& get() const;
|
||||||
};
|
};
|
||||||
@ -100,6 +100,9 @@ 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)
|
||||||
|
|
||||||
|
// Get token
|
||||||
|
std::string str() const;
|
||||||
|
|
||||||
// Transform the semantice values vector to another vector
|
// Transform the semantice values vector to another vector
|
||||||
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 transform(size_t beg, size_t end, 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 F> auto transform(F f) const -> vector<typename std::remove_const<decltype(f(SemanticValue()))>::type>;
|
||||||
@ -126,7 +129,7 @@ peg pg(syntax);
|
|||||||
|
|
||||||
pg["TOKEN"] = [](const SemanticValues& sv) {
|
pg["TOKEN"] = [](const SemanticValues& sv) {
|
||||||
// 'token' doesn't include trailing whitespaces
|
// 'token' doesn't include trailing whitespaces
|
||||||
auto token = string(sv.s, sv.n);
|
auto token = sv.str();
|
||||||
};
|
};
|
||||||
|
|
||||||
auto ret = pg.parse(" token1, token2 ");
|
auto ret = pg.parse(" token1, token2 ");
|
||||||
@ -164,7 +167,7 @@ peglib::peg parser(
|
|||||||
peglib::peg parser("NUMBER <- [0-9]+");
|
peglib::peg parser("NUMBER <- [0-9]+");
|
||||||
|
|
||||||
parser["NUMBER"] = [](const SemanticValues& sv) {
|
parser["NUMBER"] = [](const SemanticValues& sv) {
|
||||||
auto val = stol(string(sv.s, sv.n), nullptr, 10);
|
auto val = stol(sv.str(), nullptr, 10);
|
||||||
if (val != 100) {
|
if (val != 100) {
|
||||||
throw peglib::parse_error("value error!!");
|
throw peglib::parse_error("value error!!");
|
||||||
}
|
}
|
||||||
@ -279,7 +282,7 @@ vector<string> tags;
|
|||||||
Definition ROOT, TAG_NAME, _;
|
Definition ROOT, TAG_NAME, _;
|
||||||
ROOT <= seq(_, zom(seq(chr('['), TAG_NAME, chr(']'), _)));
|
ROOT <= seq(_, zom(seq(chr('['), TAG_NAME, chr(']'), _)));
|
||||||
TAG_NAME <= oom(seq(npd(chr(']')), dot())), [&](const SemanticValues& sv) {
|
TAG_NAME <= oom(seq(npd(chr(']')), dot())), [&](const SemanticValues& sv) {
|
||||||
tags.push_back(string(sv.s, sv.n));
|
tags.push_back(sv.str());
|
||||||
};
|
};
|
||||||
_ <= zom(cls(" \t"));
|
_ <= zom(cls(" \t"));
|
||||||
|
|
||||||
|
4
peglib.h
4
peglib.h
@ -205,6 +205,10 @@ struct SemanticValues : protected std::vector<SemanticValue>
|
|||||||
using std::vector<T>::emplace;
|
using std::vector<T>::emplace;
|
||||||
using std::vector<T>::emplace_back;
|
using std::vector<T>::emplace_back;
|
||||||
|
|
||||||
|
std::string str() const {
|
||||||
|
return std::string(s, n);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
auto transform(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;
|
||||||
|
16
test/test.cc
16
test/test.cc
@ -34,7 +34,7 @@ TEST_CASE("String capture test", "[general]")
|
|||||||
std::vector<std::string> tags;
|
std::vector<std::string> tags;
|
||||||
|
|
||||||
parser["TAG_NAME"] = [&](const peglib::SemanticValues& sv) {
|
parser["TAG_NAME"] = [&](const peglib::SemanticValues& sv) {
|
||||||
tags.push_back(std::string(sv.s, sv.n));
|
tags.push_back(sv.str());
|
||||||
};
|
};
|
||||||
|
|
||||||
auto ret = parser.parse(" [tag1] [tag:2] [tag-3] ");
|
auto ret = parser.parse(" [tag1] [tag:2] [tag-3] ");
|
||||||
@ -73,7 +73,7 @@ TEST_CASE("String capture test2", "[general]")
|
|||||||
Definition ROOT, TAG, TAG_NAME, WS;
|
Definition ROOT, TAG, TAG_NAME, WS;
|
||||||
ROOT <= seq(WS, zom(TAG));
|
ROOT <= seq(WS, zom(TAG));
|
||||||
TAG <= seq(chr('['), TAG_NAME, chr(']'), WS);
|
TAG <= seq(chr('['), TAG_NAME, chr(']'), WS);
|
||||||
TAG_NAME <= oom(seq(npd(chr(']')), dot())), [&](const SemanticValues& sv) { tags.push_back(string(sv.s, sv.n)); };
|
TAG_NAME <= oom(seq(npd(chr(']')), dot())), [&](const SemanticValues& sv) { tags.push_back(sv.str()); };
|
||||||
WS <= zom(cls(" \t"));
|
WS <= zom(cls(" \t"));
|
||||||
|
|
||||||
auto r = ROOT.parse(" [tag1] [tag:2] [tag-3] ");
|
auto r = ROOT.parse(" [tag1] [tag:2] [tag-3] ");
|
||||||
@ -98,7 +98,7 @@ TEST_CASE("String capture test3", "[general]")
|
|||||||
std::vector<std::string> tags;
|
std::vector<std::string> tags;
|
||||||
|
|
||||||
pg["TOKEN"] = [&](const SemanticValues& sv) {
|
pg["TOKEN"] = [&](const SemanticValues& sv) {
|
||||||
tags.push_back(std::string(sv.s, sv.n));
|
tags.push_back(sv.str());
|
||||||
};
|
};
|
||||||
|
|
||||||
auto ret = pg.parse(" [tag1] [tag:2] [tag-3] ");
|
auto ret = pg.parse(" [tag1] [tag:2] [tag-3] ");
|
||||||
@ -270,7 +270,7 @@ TEST_CASE("mutable lambda test", "[general]")
|
|||||||
|
|
||||||
// This test makes sure if the following code can be compiled.
|
// This test makes sure if the following code can be compiled.
|
||||||
pg["TOKEN"] = [=](const SemanticValues& sv) mutable {
|
pg["TOKEN"] = [=](const SemanticValues& sv) mutable {
|
||||||
vec.push_back(string(sv.s, sv.n));
|
vec.push_back(sv.str());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -343,7 +343,7 @@ TEST_CASE("Calculator test", "[general]")
|
|||||||
TERM = reduce;
|
TERM = reduce;
|
||||||
TERM_OPERATOR = [](const SemanticValues& sv) { return *sv.s; };
|
TERM_OPERATOR = [](const SemanticValues& sv) { return *sv.s; };
|
||||||
FACTOR_OPERATOR = [](const SemanticValues& sv) { return *sv.s; };
|
FACTOR_OPERATOR = [](const SemanticValues& sv) { return *sv.s; };
|
||||||
NUMBER = [](const SemanticValues& sv) { return stol(string(sv.s, sv.n), nullptr, 10); };
|
NUMBER = [](const SemanticValues& sv) { return stol(sv.str(), nullptr, 10); };
|
||||||
|
|
||||||
// Parse
|
// Parse
|
||||||
long val;
|
long val;
|
||||||
@ -389,7 +389,7 @@ TEST_CASE("Calculator test2", "[general]")
|
|||||||
g["TERM"] = reduce;
|
g["TERM"] = reduce;
|
||||||
g["TERM_OPERATOR"] = [](const SemanticValues& sv) { return *sv.s; };
|
g["TERM_OPERATOR"] = [](const SemanticValues& sv) { return *sv.s; };
|
||||||
g["FACTOR_OPERATOR"] = [](const SemanticValues& sv) { return *sv.s; };
|
g["FACTOR_OPERATOR"] = [](const SemanticValues& sv) { return *sv.s; };
|
||||||
g["NUMBER"] = [](const SemanticValues& sv) { return stol(string(sv.s, sv.n), nullptr, 10); };
|
g["NUMBER"] = [](const SemanticValues& sv) { return stol(sv.str(), nullptr, 10); };
|
||||||
|
|
||||||
// Parse
|
// Parse
|
||||||
long val;
|
long val;
|
||||||
@ -431,7 +431,7 @@ TEST_CASE("Calculator test3", "[general]")
|
|||||||
parser["TERM"] = reduce;
|
parser["TERM"] = reduce;
|
||||||
parser["TERM_OPERATOR"] = [](const SemanticValues& sv) { return (char)*sv.s; };
|
parser["TERM_OPERATOR"] = [](const SemanticValues& sv) { return (char)*sv.s; };
|
||||||
parser["FACTOR_OPERATOR"] = [](const SemanticValues& sv) { return (char)*sv.s; };
|
parser["FACTOR_OPERATOR"] = [](const SemanticValues& sv) { return (char)*sv.s; };
|
||||||
parser["NUMBER"] = [](const SemanticValues& sv) { return stol(string(sv.s, sv.n), nullptr, 10); };
|
parser["NUMBER"] = [](const SemanticValues& sv) { return stol(sv.str(), nullptr, 10); };
|
||||||
|
|
||||||
// Parse
|
// Parse
|
||||||
long val;
|
long val;
|
||||||
@ -622,7 +622,7 @@ TEST_CASE("Semantic predicate test", "[predicate]")
|
|||||||
peg parser("NUMBER <- [0-9]+");
|
peg parser("NUMBER <- [0-9]+");
|
||||||
|
|
||||||
parser["NUMBER"] = [](const SemanticValues& sv) {
|
parser["NUMBER"] = [](const SemanticValues& sv) {
|
||||||
auto val = stol(string(sv.s, sv.n), nullptr, 10);
|
auto val = stol(sv.str(), nullptr, 10);
|
||||||
if (val != 100) {
|
if (val != 100) {
|
||||||
throw parse_error("value error!!");
|
throw parse_error("value error!!");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user