mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Allow literal operators in infix expression
This commit is contained in:
parent
a248d8689e
commit
e26a9e88f5
10
peglib.h
10
peglib.h
@ -3184,8 +3184,14 @@ private:
|
||||
seq(g["PrecedenceAssoc"],
|
||||
oom(seq(ign(g["SpacesOom"]), g["PrecedenceOpe"])));
|
||||
g["PrecedenceOpe"] <=
|
||||
tok(oom(
|
||||
seq(npd(cho(g["PrecedenceAssoc"], g["Space"], chr('}'))), dot())));
|
||||
cho(seq(cls("'"),
|
||||
tok(zom(seq(npd(cho(g["Space"], cls("'"))), g["Char"]))),
|
||||
cls("'")),
|
||||
seq(cls("\""),
|
||||
tok(zom(seq(npd(cho(g["Space"], cls("\""))), g["Char"]))),
|
||||
cls("\"")),
|
||||
tok(oom(seq(npd(cho(g["PrecedenceAssoc"], g["Space"], chr('}'))),
|
||||
dot()))));
|
||||
g["PrecedenceAssoc"] <= cls("LR");
|
||||
|
||||
// Error message instruction
|
||||
|
@ -300,6 +300,68 @@ TEST_CASE("Precedence climbing", "[precedence]") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Precedence climbing with literal operator", "[precedence]") {
|
||||
parser parser(R"(
|
||||
START <- _ EXPRESSION
|
||||
EXPRESSION <- ATOM (OPERATOR ATOM)* {
|
||||
precedence
|
||||
L '#plus#' - # weaker
|
||||
L '#multiply#' / # stronger
|
||||
}
|
||||
ATOM <- NUMBER / T('(') EXPRESSION T(')')
|
||||
OPERATOR <- T('#plus#' / '#multiply#' / [-/])
|
||||
NUMBER <- T('-'? [0-9]+)
|
||||
~_ <- [ \t]*
|
||||
T(S) <- < S > _
|
||||
)");
|
||||
|
||||
REQUIRE(!!parser); // OK
|
||||
|
||||
parser.enable_packrat_parsing();
|
||||
|
||||
// Setup actions
|
||||
parser["EXPRESSION"] = [](const SemanticValues &vs) -> long {
|
||||
auto result = std::any_cast<long>(vs[0]);
|
||||
if (vs.size() > 1) {
|
||||
auto ope = std::any_cast<std::string>(vs[1]);
|
||||
auto num = std::any_cast<long>(vs[2]);
|
||||
if (ope == "#plus#") {
|
||||
result += num;
|
||||
} else if (ope == "-") {
|
||||
result -= num;
|
||||
} else if (ope == "#multiply#") {
|
||||
result *= num;
|
||||
} else if (ope == "/") {
|
||||
result /= num;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
parser["OPERATOR"] = [](const SemanticValues &vs) { return vs.token_to_string(); };
|
||||
parser["NUMBER"] = [](const SemanticValues &vs) { return vs.token_to_number<long>(); };
|
||||
|
||||
bool ret = parser;
|
||||
REQUIRE(ret == true);
|
||||
|
||||
{
|
||||
auto expr = " 1 #plus# 2 #multiply# 3 #multiply# (4 - 5 #plus# 6) / 7 - 8 ";
|
||||
long val = 0;
|
||||
ret = parser.parse(expr, val);
|
||||
|
||||
REQUIRE(ret == true);
|
||||
REQUIRE(val == -3);
|
||||
}
|
||||
|
||||
{
|
||||
auto expr = "-1#plus#-2--3"; // -1 + -2 - -3 = 0
|
||||
long val = 0;
|
||||
ret = parser.parse(expr, val);
|
||||
|
||||
REQUIRE(ret == true);
|
||||
REQUIRE(val == 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Precedence climbing with macro", "[precedence]") {
|
||||
// Create a PEG parser
|
||||
parser parser(R"(
|
||||
|
Loading…
Reference in New Issue
Block a user