config: allow nesting if statements

This commit is contained in:
Vaxry 2025-09-02 12:51:41 +02:00
parent 23f0debd20
commit 3d63fb4a42
Signed by: vaxry
GPG key ID: 665806380871D640
3 changed files with 25 additions and 12 deletions

View file

@ -536,9 +536,9 @@ std::optional<std::string> CConfigImpl::parseComment(const std::string& comment)
}
if (args[i] == "endif") {
if (!currentFlags.inAnIfBlock)
if (currentFlags.ifDatas.empty())
return "stray endif";
currentFlags.inAnIfBlock = false;
currentFlags.ifDatas.pop_back();
break;
}
@ -549,20 +549,19 @@ std::optional<std::string> CConfigImpl::parseComment(const std::string& comment)
}
if (!ifBlockVariable.empty()) {
if (currentFlags.inAnIfBlock)
return "nested if statements are not allowed";
if (ifBlockVariable.starts_with("!")) {
negated = true;
ifBlockVariable = ifBlockVariable.substr(1);
}
currentFlags.inAnIfBlock = true;
CConfigImpl::SIfBlockData newIfData;
if (const auto VAR = getVariable(ifBlockVariable); VAR)
currentFlags.ifBlockFailed = negated ? VAR->truthy() : !VAR->truthy();
newIfData.failed = negated ? VAR->truthy() : !VAR->truthy();
else
currentFlags.ifBlockFailed = !negated;
newIfData.failed = !negated;
currentFlags.ifDatas.emplace_back(newIfData);
}
return std::nullopt;
@ -632,7 +631,7 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) {
return result;
}
if (impl->currentFlags.inAnIfBlock && impl->currentFlags.ifBlockFailed)
if (!impl->currentFlags.ifDatas.empty() && impl->currentFlags.ifDatas.back().failed)
return result;
size_t lastHashPos = 0;

View file

@ -103,9 +103,13 @@ class CConfigImpl {
std::expected<float, std::string> parseExpression(const std::string& s);
SVariable* getVariable(const std::string& name);
struct SIfBlockData {
bool failed = false;
};
struct {
bool noError = false;
bool inAnIfBlock = false;
bool ifBlockFailed = false;
bool noError = false;
std::vector<SIfBlockData> ifDatas;
} currentFlags;
};

View file

@ -39,10 +39,20 @@ customType = abc
# hyprlang if !NONEXISTENT_VAR
# hyprlang if !NONEXISTENT_VAR_2
testStringColon = ee:ee:ee
# hyprlang endif
# hyprlang if NONEXISTENT_VAR
testStringColon = ee:ee:ee:22
# hyprlang endif
# hyprlang endif
# hyprlang noerror true
errorVariable = true