mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-19 17:02:15 -07:00
Formatter
This commit is contained in:
parent
45d329b707
commit
bdf75d1621
1 changed files with 30 additions and 26 deletions
|
|
@ -65,12 +65,14 @@ std::once_flag init;
|
|||
|
||||
static void setupParserRules()
|
||||
{
|
||||
auto passthru = [](const peg::SemanticValues &sv) -> Filter { return !sv.empty() ? std::any_cast<Filter>(sv[0]) : nullptr; };
|
||||
auto passthru = [](const peg::SemanticValues &sv) -> Filter {
|
||||
return !sv.empty() ? std::any_cast<Filter>(sv[0]) : nullptr;
|
||||
};
|
||||
|
||||
search["Start"] = passthru;
|
||||
search["QueryPartList"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
return [=](const CardData& x) {
|
||||
for (const auto & i : sv) {
|
||||
return [=](const CardData &x) {
|
||||
for (const auto &i : sv) {
|
||||
if (!std::any_cast<Filter>(i)(x))
|
||||
return false;
|
||||
}
|
||||
|
|
@ -78,8 +80,8 @@ static void setupParserRules()
|
|||
};
|
||||
};
|
||||
search["ComplexQueryPart"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
return [=](const CardData& x) {
|
||||
for (const auto & i : sv) {
|
||||
return [=](const CardData &x) {
|
||||
for (const auto &i : sv) {
|
||||
if (std::any_cast<Filter>(i)(x))
|
||||
return true;
|
||||
}
|
||||
|
|
@ -90,15 +92,15 @@ static void setupParserRules()
|
|||
search["QueryPart"] = passthru;
|
||||
search["NotQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
Filter dependent = std::any_cast<Filter>(sv[0]);
|
||||
return [=](const CardData& x) -> bool { return !dependent(x); };
|
||||
return [=](const CardData &x) -> bool { return !dependent(x); };
|
||||
};
|
||||
search["TypeQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
||||
return [=](const CardData& x) -> bool { return matcher(x->getCardType()); };
|
||||
return [=](const CardData &x) -> bool { return matcher(x->getCardType()); };
|
||||
};
|
||||
search["SetQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
||||
return [=](const CardData& x) -> bool {
|
||||
return [=](const CardData &x) -> bool {
|
||||
for (const auto &set : x->getSets().keys()) {
|
||||
if (matcher(set))
|
||||
return true;
|
||||
|
|
@ -108,7 +110,7 @@ static void setupParserRules()
|
|||
};
|
||||
search["RarityQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
||||
return [=](const CardData& x) -> bool {
|
||||
return [=](const CardData &x) -> bool {
|
||||
for (const auto &set : x->getSets().values()) {
|
||||
if (matcher(set.getProperty("rarity")))
|
||||
return true;
|
||||
|
|
@ -119,11 +121,13 @@ static void setupParserRules()
|
|||
search["FormatQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
if (sv.choice() == 0) {
|
||||
QString format = std::any_cast<QString>(sv[0]);
|
||||
return [=](const CardData& x) -> bool { return x->getProperty(QString("format-%1").arg(format)) == "legal"; };
|
||||
return
|
||||
[=](const CardData &x) -> bool { return x->getProperty(QString("format-%1").arg(format)) == "legal"; };
|
||||
} else {
|
||||
QString format = std::any_cast<QString>(sv[1]);
|
||||
QString legality = std::any_cast<QString>(sv[0]);
|
||||
return [=](const CardData& x) -> bool { return x->getProperty(QString("format-%1").arg(format)) == legality; };
|
||||
return
|
||||
[=](const CardData &x) -> bool { return x->getProperty(QString("format-%1").arg(format)) == legality; };
|
||||
}
|
||||
};
|
||||
search["Legality"] = [](const peg::SemanticValues &sv) -> QString {
|
||||
|
|
@ -203,14 +207,14 @@ static void setupParserRules()
|
|||
};
|
||||
search["CompactStringSet"] = [](const peg::SemanticValues &sv) -> QStringList {
|
||||
QStringList result;
|
||||
for (const auto & i : sv) {
|
||||
for (const auto &i : sv) {
|
||||
result.append(std::any_cast<QString>(i));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
search["StringList"] = [](const peg::SemanticValues &sv) -> QStringList {
|
||||
QStringList result;
|
||||
for (const auto & i : sv) {
|
||||
for (const auto &i : sv) {
|
||||
result.append(std::any_cast<QString>(i));
|
||||
}
|
||||
return result;
|
||||
|
|
@ -260,17 +264,17 @@ static void setupParserRules()
|
|||
|
||||
search["OracleQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
||||
return [=](const CardData& x) { return matcher(x->getText()); };
|
||||
return [=](const CardData &x) { return matcher(x->getText()); };
|
||||
};
|
||||
|
||||
search["ColorQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
QString parts;
|
||||
for (const auto & i : sv) {
|
||||
for (const auto &i : sv) {
|
||||
parts += std::any_cast<char>(i);
|
||||
}
|
||||
bool identity = sv.tokens[0][0] != 'i';
|
||||
if (sv.tokens[1][0] == ':') {
|
||||
return [=](const CardData& x) {
|
||||
return [=](const CardData &x) {
|
||||
QString match = identity ? x->getColors() : x->getProperty("coloridentity");
|
||||
if (parts.contains("m") && match.length() < 2) {
|
||||
return false;
|
||||
|
|
@ -288,7 +292,7 @@ static void setupParserRules()
|
|||
return false;
|
||||
};
|
||||
} else {
|
||||
return [=](const CardData& x) {
|
||||
return [=](const CardData &x) {
|
||||
QString match = identity ? x->getColors() : x->getProperty("colorIdentity");
|
||||
if (parts.contains("m") && match.length() < 2)
|
||||
return false;
|
||||
|
|
@ -312,15 +316,15 @@ static void setupParserRules()
|
|||
|
||||
search["CMCQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[0]);
|
||||
return [=](const CardData& x) -> bool { return matcher(x->getProperty("cmc").toInt()); };
|
||||
return [=](const CardData &x) -> bool { return matcher(x->getProperty("cmc").toInt()); };
|
||||
};
|
||||
search["PowerQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[0]);
|
||||
return [=](const CardData& x) -> bool { return matcher(x->getPowTough().split("/")[0].toInt()); };
|
||||
return [=](const CardData &x) -> bool { return matcher(x->getPowTough().split("/")[0].toInt()); };
|
||||
};
|
||||
search["ToughnessQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[0]);
|
||||
return [=](const CardData& x) -> bool {
|
||||
return [=](const CardData &x) -> bool {
|
||||
auto parts = x->getPowTough().split("/");
|
||||
return matcher(parts.length() == 2 ? parts[1].toInt() : 0);
|
||||
};
|
||||
|
|
@ -329,17 +333,17 @@ static void setupParserRules()
|
|||
QString field = std::any_cast<QString>(sv[0]);
|
||||
if (sv.choice() == 0) {
|
||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[1]);
|
||||
return [=](const CardData& x) -> bool { return x->hasProperty(field) && matcher(x->getProperty(field)); };
|
||||
return [=](const CardData &x) -> bool { return x->hasProperty(field) && matcher(x->getProperty(field)); };
|
||||
} else {
|
||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[1]);
|
||||
return [=](const CardData& x) -> bool {
|
||||
return [=](const CardData &x) -> bool {
|
||||
return x->hasProperty(field) && matcher(x->getProperty(field).toInt());
|
||||
};
|
||||
}
|
||||
};
|
||||
search["GenericQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
||||
return [=](const CardData& x) { return matcher(x->getName()); };
|
||||
return [=](const CardData &x) { return matcher(x->getName()); };
|
||||
};
|
||||
|
||||
search["Color"] = [](const peg::SemanticValues &sv) -> char { return "WUBRGU"[sv.choice()]; };
|
||||
|
|
@ -350,7 +354,7 @@ static void setupParserRules()
|
|||
|
||||
FilterString::FilterString()
|
||||
{
|
||||
result = [](const CardData&) -> bool { return false; };
|
||||
result = [](const CardData &) -> bool { return false; };
|
||||
_error = "Not initialized";
|
||||
}
|
||||
|
||||
|
|
@ -363,7 +367,7 @@ FilterString::FilterString(const QString &expr)
|
|||
_error = QString();
|
||||
|
||||
if (ba.isEmpty()) {
|
||||
result = [](const CardData&) -> bool { return true; };
|
||||
result = [](const CardData &) -> bool { return true; };
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -373,6 +377,6 @@ FilterString::FilterString(const QString &expr)
|
|||
|
||||
if (!search.parse(ba.data(), result)) {
|
||||
qDebug().nospace() << "FilterString error for " << expr << "; " << qPrintable(_error);
|
||||
result = [](const CardData&) -> bool { return false; };
|
||||
result = [](const CardData &) -> bool { return false; };
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue