mirror of
https://github.com/hyprwm/hyprlang.git
synced 2025-12-20 03:50:02 +01:00
config: allow nesting if statements
This commit is contained in:
parent
23f0debd20
commit
3d63fb4a42
3 changed files with 25 additions and 12 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
std::vector<SIfBlockData> ifDatas;
|
||||
} currentFlags;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue