mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-21 01:42: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()
|
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["Start"] = passthru;
|
||||||
search["QueryPartList"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["QueryPartList"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
return [=](const CardData& x) {
|
return [=](const CardData &x) {
|
||||||
for (const auto & i : sv) {
|
for (const auto &i : sv) {
|
||||||
if (!std::any_cast<Filter>(i)(x))
|
if (!std::any_cast<Filter>(i)(x))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -78,8 +80,8 @@ static void setupParserRules()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
search["ComplexQueryPart"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["ComplexQueryPart"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
return [=](const CardData& x) {
|
return [=](const CardData &x) {
|
||||||
for (const auto & i : sv) {
|
for (const auto &i : sv) {
|
||||||
if (std::any_cast<Filter>(i)(x))
|
if (std::any_cast<Filter>(i)(x))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -90,15 +92,15 @@ static void setupParserRules()
|
||||||
search["QueryPart"] = passthru;
|
search["QueryPart"] = passthru;
|
||||||
search["NotQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["NotQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
Filter dependent = std::any_cast<Filter>(sv[0]);
|
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 {
|
search["TypeQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
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 {
|
search["SetQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
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()) {
|
for (const auto &set : x->getSets().keys()) {
|
||||||
if (matcher(set))
|
if (matcher(set))
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -108,7 +110,7 @@ static void setupParserRules()
|
||||||
};
|
};
|
||||||
search["RarityQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["RarityQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
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()) {
|
for (const auto &set : x->getSets().values()) {
|
||||||
if (matcher(set.getProperty("rarity")))
|
if (matcher(set.getProperty("rarity")))
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -119,11 +121,13 @@ static void setupParserRules()
|
||||||
search["FormatQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["FormatQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
if (sv.choice() == 0) {
|
if (sv.choice() == 0) {
|
||||||
QString format = std::any_cast<QString>(sv[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 {
|
} else {
|
||||||
QString format = std::any_cast<QString>(sv[1]);
|
QString format = std::any_cast<QString>(sv[1]);
|
||||||
QString legality = std::any_cast<QString>(sv[0]);
|
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 {
|
search["Legality"] = [](const peg::SemanticValues &sv) -> QString {
|
||||||
|
|
@ -203,14 +207,14 @@ static void setupParserRules()
|
||||||
};
|
};
|
||||||
search["CompactStringSet"] = [](const peg::SemanticValues &sv) -> QStringList {
|
search["CompactStringSet"] = [](const peg::SemanticValues &sv) -> QStringList {
|
||||||
QStringList result;
|
QStringList result;
|
||||||
for (const auto & i : sv) {
|
for (const auto &i : sv) {
|
||||||
result.append(std::any_cast<QString>(i));
|
result.append(std::any_cast<QString>(i));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
search["StringList"] = [](const peg::SemanticValues &sv) -> QStringList {
|
search["StringList"] = [](const peg::SemanticValues &sv) -> QStringList {
|
||||||
QStringList result;
|
QStringList result;
|
||||||
for (const auto & i : sv) {
|
for (const auto &i : sv) {
|
||||||
result.append(std::any_cast<QString>(i));
|
result.append(std::any_cast<QString>(i));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -260,17 +264,17 @@ static void setupParserRules()
|
||||||
|
|
||||||
search["OracleQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["OracleQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
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 {
|
search["ColorQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
QString parts;
|
QString parts;
|
||||||
for (const auto & i : sv) {
|
for (const auto &i : sv) {
|
||||||
parts += std::any_cast<char>(i);
|
parts += std::any_cast<char>(i);
|
||||||
}
|
}
|
||||||
bool identity = sv.tokens[0][0] != 'i';
|
bool identity = sv.tokens[0][0] != 'i';
|
||||||
if (sv.tokens[1][0] == ':') {
|
if (sv.tokens[1][0] == ':') {
|
||||||
return [=](const CardData& x) {
|
return [=](const CardData &x) {
|
||||||
QString match = identity ? x->getColors() : x->getProperty("coloridentity");
|
QString match = identity ? x->getColors() : x->getProperty("coloridentity");
|
||||||
if (parts.contains("m") && match.length() < 2) {
|
if (parts.contains("m") && match.length() < 2) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -288,7 +292,7 @@ static void setupParserRules()
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return [=](const CardData& x) {
|
return [=](const CardData &x) {
|
||||||
QString match = identity ? x->getColors() : x->getProperty("colorIdentity");
|
QString match = identity ? x->getColors() : x->getProperty("colorIdentity");
|
||||||
if (parts.contains("m") && match.length() < 2)
|
if (parts.contains("m") && match.length() < 2)
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -312,15 +316,15 @@ static void setupParserRules()
|
||||||
|
|
||||||
search["CMCQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["CMCQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[0]);
|
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 {
|
search["PowerQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[0]);
|
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 {
|
search["ToughnessQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[0]);
|
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[0]);
|
||||||
return [=](const CardData& x) -> bool {
|
return [=](const CardData &x) -> bool {
|
||||||
auto parts = x->getPowTough().split("/");
|
auto parts = x->getPowTough().split("/");
|
||||||
return matcher(parts.length() == 2 ? parts[1].toInt() : 0);
|
return matcher(parts.length() == 2 ? parts[1].toInt() : 0);
|
||||||
};
|
};
|
||||||
|
|
@ -329,17 +333,17 @@ static void setupParserRules()
|
||||||
QString field = std::any_cast<QString>(sv[0]);
|
QString field = std::any_cast<QString>(sv[0]);
|
||||||
if (sv.choice() == 0) {
|
if (sv.choice() == 0) {
|
||||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[1]);
|
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 {
|
} else {
|
||||||
NumberMatcher matcher = std::any_cast<NumberMatcher>(sv[1]);
|
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());
|
return x->hasProperty(field) && matcher(x->getProperty(field).toInt());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
search["GenericQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
search["GenericQuery"] = [](const peg::SemanticValues &sv) -> Filter {
|
||||||
StringMatcher matcher = std::any_cast<StringMatcher>(sv[0]);
|
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()]; };
|
search["Color"] = [](const peg::SemanticValues &sv) -> char { return "WUBRGU"[sv.choice()]; };
|
||||||
|
|
@ -350,7 +354,7 @@ static void setupParserRules()
|
||||||
|
|
||||||
FilterString::FilterString()
|
FilterString::FilterString()
|
||||||
{
|
{
|
||||||
result = [](const CardData&) -> bool { return false; };
|
result = [](const CardData &) -> bool { return false; };
|
||||||
_error = "Not initialized";
|
_error = "Not initialized";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -363,7 +367,7 @@ FilterString::FilterString(const QString &expr)
|
||||||
_error = QString();
|
_error = QString();
|
||||||
|
|
||||||
if (ba.isEmpty()) {
|
if (ba.isEmpty()) {
|
||||||
result = [](const CardData&) -> bool { return true; };
|
result = [](const CardData &) -> bool { return true; };
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -373,6 +377,6 @@ FilterString::FilterString(const QString &expr)
|
||||||
|
|
||||||
if (!search.parse(ba.data(), result)) {
|
if (!search.parse(ba.data(), result)) {
|
||||||
qDebug().nospace() << "FilterString error for " << expr << "; " << qPrintable(_error);
|
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