| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Kacper Donat <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include "Parser.h" |
| 25 |
#include "ParameterManager.h" |
| 26 |
|
| 27 |
#include <Utilities.h> |
| 28 |
#include <Base/Tools.h> |
| 29 |
|
| 30 |
#include <QColor> |
| 31 |
#include <QRegularExpression> |
| 32 |
#include <QString> |
| 33 |
#include <ranges> |
| 34 |
#include <variant> |
| 35 |
|
| 36 |
namespace Gui::StyleParameters |
| 37 |
{ |
| 38 |
|
| 39 |
Value ParameterReference::evaluate(const EvaluationContext& context) const |
| 40 |
{ |
| 41 |
return context.manager->resolve(name, context.context).value_or("@" + name); |
| 42 |
} |
| 43 |
|
| 44 |
Value Number::evaluate([[maybe_unused]] const EvaluationContext& context) const |
| 45 |
{ |
| 46 |
return value; |
| 47 |
} |
| 48 |
|
| 49 |
Value Color::evaluate([[maybe_unused]] const EvaluationContext& context) const |
| 50 |
{ |
| 51 |
return color; |
| 52 |
} |
| 53 |
|
| 54 |
Value FunctionCall::evaluate(const EvaluationContext& context) const |
| 55 |
{ |
| 56 |
const auto lightenOrDarken = [this](const EvaluationContext& context) -> Value { |
| 57 |
if (arguments.size() != 2) { |
| 58 |
THROWM( |
| 59 |
Base::ExpressionError, |
| 60 |
fmt::format("Function '{}' expects 2 arguments, got {}", functionName, arguments.size()) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
auto colorArg = arguments[0]->evaluate(context); |
| 65 |
auto amountArg = arguments[1]->evaluate(context); |
| 66 |
|
| 67 |
if (!std::holds_alternative<Base::Color>(colorArg)) { |
| 68 |
THROWM(Base::ExpressionError, fmt::format("'{}' is not supported for colors", functionName)); |
| 69 |
} |
| 70 |
|
| 71 |
auto color = std::get<Base::Color>(colorArg).asValue<QColor>(); |
| 72 |
|
| 73 |
// In Qt if you want to make color 20% darker or lighter, you need to pass 120 as the value |
| 74 |
// we, however, want users to pass only the relative difference, hence we need to add the |
| 75 |
// 100 required by Qt. |
| 76 |
// |
| 77 |
// NOLINTNEXTLINE(*-magic-numbers) |
| 78 |
auto amount = 100 + static_cast<int>(std::get<Numeric>(amountArg).value); |
| 79 |
|
| 80 |
if (functionName == "lighten") { |
| 81 |
return Base::Color::fromValue(color.lighter(amount)); |
| 82 |
} |
| 83 |
|
| 84 |
if (functionName == "darken") { |
| 85 |
return Base::Color::fromValue(color.darker(amount)); |
| 86 |
} |
| 87 |
|
| 88 |
return {}; |
| 89 |
}; |
| 90 |
|
| 91 |
const auto blend = [this](const EvaluationContext& context) -> Value { |
| 92 |
if (arguments.size() != 3) { |
| 93 |
THROWM( |
| 94 |
Base::ExpressionError, |
| 95 |
fmt::format("Function '{}' expects 3 arguments, got {}", functionName, arguments.size()) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
auto firstColorArg = arguments[0]->evaluate(context); |
| 100 |
auto secondColorArg = arguments[1]->evaluate(context); |
| 101 |
auto amountArg = arguments[2]->evaluate(context); |
| 102 |
|
| 103 |
if (!std::holds_alternative<Base::Color>(firstColorArg)) { |
| 104 |
THROWM( |
| 105 |
Base::ExpressionError, |
| 106 |
fmt::format("first argument of '{}' must be color", functionName) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
if (!std::holds_alternative<Base::Color>(secondColorArg)) { |
| 111 |
THROWM( |
| 112 |
Base::ExpressionError, |
| 113 |
fmt::format("second argument of '{}' must be color", functionName) |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
auto firstColor = std::get<Base::Color>(firstColorArg); |
| 118 |
auto secondColor = std::get<Base::Color>(secondColorArg); |
| 119 |
|
| 120 |
auto amount = Base::fromPercent(static_cast<long>(std::get<Numeric>(amountArg).value)); |
| 121 |
|
| 122 |
return Base::Color( |
| 123 |
(1 - amount) * firstColor.r + amount * secondColor.r, |
| 124 |
(1 - amount) * firstColor.g + amount * secondColor.g, |
| 125 |
(1 - amount) * firstColor.b + amount * secondColor.b |
| 126 |
); |
| 127 |
}; |
| 128 |
|
| 129 |
std::map<std::string, std::function<Value(const EvaluationContext&)>> functions = { |
| 130 |
{"lighten", lightenOrDarken}, |
| 131 |
{"darken", lightenOrDarken}, |
| 132 |
{"blend", blend}, |
| 133 |
}; |
| 134 |
|
| 135 |
if (functions.contains(functionName)) { |
| 136 |
auto function = functions.at(functionName); |
| 137 |
return function(context); |
| 138 |
} |
| 139 |
|
| 140 |
THROWM(Base::ExpressionError, fmt::format("Unknown function '{}'", functionName)); |
| 141 |
} |
| 142 |
|
| 143 |
Value BinaryOp::evaluate(const EvaluationContext& context) const |
| 144 |
{ |
| 145 |
Value lval = left->evaluate(context); |
| 146 |
Value rval = right->evaluate(context); |
| 147 |
|
| 148 |
if (!std::holds_alternative<Numeric>(lval) || !std::holds_alternative<Numeric>(rval)) { |
| 149 |
THROWM(Base::ExpressionError, "Math operations are supported only on lengths"); |
| 150 |
} |
| 151 |
|
| 152 |
auto lhs = std::get<Numeric>(lval); |
| 153 |
auto rhs = std::get<Numeric>(rval); |
| 154 |
|
| 155 |
switch (op) { |
| 156 |
case Operator::Add: |
| 157 |
return lhs + rhs; |
| 158 |
case Operator::Subtract: |
| 159 |
return lhs - rhs; |
| 160 |
case Operator::Multiply: |
| 161 |
return lhs * rhs; |
| 162 |
case Operator::Divide: |
| 163 |
return lhs / rhs; |
| 164 |
default: |
| 165 |
THROWM(Base::ExpressionError, "Unknown operator"); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
Value UnaryOp::evaluate(const EvaluationContext& context) const |
| 170 |
{ |
| 171 |
Value val = operand->evaluate(context); |
| 172 |
if (std::holds_alternative<Base::Color>(val)) { |
| 173 |
THROWM(Base::ExpressionError, "Unary operations on colors are not supported"); |
| 174 |
} |
| 175 |
|
| 176 |
auto v = std::get<Numeric>(val); |
| 177 |
switch (op) { |
| 178 |
case Operator::Add: |
| 179 |
return v; |
| 180 |
case Operator::Subtract: |
| 181 |
return -v; |
| 182 |
default: |
| 183 |
THROWM(Base::ExpressionError, "Unknown unary operator"); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
std::unique_ptr<Expr> Parser::parse() |
| 188 |
{ |
| 189 |
auto expr = parseExpression(); |
| 190 |
skipWhitespace(); |
| 191 |
if (pos != input.size()) { |
| 192 |
THROWM( |
| 193 |
Base::ParserError, |
| 194 |
fmt::format("Unexpected characters at end of input: {}", input.substr(pos)) |
| 195 |
); |
| 196 |
} |
| 197 |
return expr; |
| 198 |
} |
| 199 |
|
| 200 |
bool Parser::peekString(const char* function) const |
| 201 |
{ |
| 202 |
return input.compare(pos, strlen(function), function) == 0; |
| 203 |
} |
| 204 |
|
| 205 |
std::unique_ptr<Expr> Parser::parseExpression() |
| 206 |
{ |
| 207 |
auto expr = parseTerm(); |
| 208 |
while (true) { |
| 209 |
skipWhitespace(); |
| 210 |
if (match('+')) { |
| 211 |
expr = std::make_unique<BinaryOp>(std::move(expr), Operator::Add, parseTerm()); |
| 212 |
} |
| 213 |
else if (match('-')) { |
| 214 |
expr = std::make_unique<BinaryOp>(std::move(expr), Operator::Subtract, parseTerm()); |
| 215 |
} |
| 216 |
else { |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
return expr; |
| 221 |
} |
| 222 |
|
| 223 |
std::unique_ptr<Expr> Parser::parseTerm() |
| 224 |
{ |
| 225 |
auto expr = parseFactor(); |
| 226 |
while (true) { |
| 227 |
skipWhitespace(); |
| 228 |
if (match('*')) { |
| 229 |
expr = std::make_unique<BinaryOp>(std::move(expr), Operator::Multiply, parseFactor()); |
| 230 |
} |
| 231 |
else if (match('/')) { |
| 232 |
expr = std::make_unique<BinaryOp>(std::move(expr), Operator::Divide, parseFactor()); |
| 233 |
} |
| 234 |
else { |
| 235 |
break; |
| 236 |
} |
| 237 |
} |
| 238 |
return expr; |
| 239 |
} |
| 240 |
|
| 241 |
std::unique_ptr<Expr> Parser::parseFactor() |
| 242 |
{ |
| 243 |
skipWhitespace(); |
| 244 |
if (match('+') || match('-')) { |
| 245 |
Operator op = (input[pos - 1] == '+') ? Operator::Add : Operator::Subtract; |
| 246 |
return std::make_unique<UnaryOp>(op, parseFactor()); |
| 247 |
} |
| 248 |
if (match('(')) { |
| 249 |
auto expr = parseExpression(); |
| 250 |
if (!match(')')) { |
| 251 |
THROWM(Base::ParserError, fmt::format("Expected ')', got '{}'", input[pos])); |
| 252 |
} |
| 253 |
return expr; |
| 254 |
} |
| 255 |
if (peekColor()) { |
| 256 |
return parseColor(); |
| 257 |
} |
| 258 |
if (peekParameter()) { |
| 259 |
return parseParameter(); |
| 260 |
} |
| 261 |
if (peekFunction()) { |
| 262 |
return parseFunctionCall(); |
| 263 |
} |
| 264 |
return parseNumber(); |
| 265 |
} |
| 266 |
|
| 267 |
bool Parser::peekColor() |
| 268 |
{ |
| 269 |
skipWhitespace(); |
| 270 |
// clang-format off |
| 271 |
return input[pos] == '#' |
| 272 |
|| peekString(rgbFunction) |
| 273 |
|| peekString(rgbaFunction); |
| 274 |
// clang-format on |
| 275 |
} |
| 276 |
|
| 277 |
std::unique_ptr<Expr> Parser::parseColor() |
| 278 |
{ |
| 279 |
const auto parseHexadecimalColor = [&]() { |
| 280 |
constexpr int hexadecimalBase = 16; |
| 281 |
|
| 282 |
// Format is #RRGGBB |
| 283 |
pos++; |
| 284 |
int r = std::stoi(input.substr(pos, 2), nullptr, hexadecimalBase); |
| 285 |
pos += 2; |
| 286 |
int g = std::stoi(input.substr(pos, 2), nullptr, hexadecimalBase); |
| 287 |
pos += 2; |
| 288 |
int b = std::stoi(input.substr(pos, 2), nullptr, hexadecimalBase); |
| 289 |
pos += 2; |
| 290 |
|
| 291 |
return std::make_unique<Color>(Base::Color(r / 255.0, g / 255.0, b / 255.0)); |
| 292 |
}; |
| 293 |
|
| 294 |
const auto parseFunctionStyleColor = [&]() { |
| 295 |
bool hasAlpha = peekString(rgbaFunction); |
| 296 |
|
| 297 |
pos += hasAlpha ? strlen(rgbaFunction) : strlen(rgbFunction); |
| 298 |
|
| 299 |
int r = parseInt(); |
| 300 |
if (!match(',')) { |
| 301 |
THROWM(Base::ParserError, fmt::format("Expected ',' after red, got '{}'", input[pos])); |
| 302 |
} |
| 303 |
int g = parseInt(); |
| 304 |
if (!match(',')) { |
| 305 |
THROWM(Base::ParserError, fmt::format("Expected ',' after green, got '{}'", input[pos])); |
| 306 |
} |
| 307 |
int b = parseInt(); |
| 308 |
int a = 255; // NOLINT(*-magic-numbers) |
| 309 |
if (hasAlpha) { |
| 310 |
if (!match(',')) { |
| 311 |
THROWM(Base::ParserError, fmt::format("Expected ',' after blue, got '{}'", input[pos])); |
| 312 |
} |
| 313 |
a = parseInt(); |
| 314 |
} |
| 315 |
if (!match(')')) { |
| 316 |
THROWM( |
| 317 |
Base::ParserError, |
| 318 |
fmt::format("Expected ')' after color arguments, got '{}'", input[pos]) |
| 319 |
); |
| 320 |
} |
| 321 |
return std::make_unique<Color>(Base::Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0)); |
| 322 |
}; |
| 323 |
|
| 324 |
skipWhitespace(); |
| 325 |
|
| 326 |
try { |
| 327 |
if (input[pos] == '#') { |
| 328 |
return parseHexadecimalColor(); |
| 329 |
} |
| 330 |
|
| 331 |
if (peekString(rgbFunction) || peekString(rgbaFunction)) { |
| 332 |
return parseFunctionStyleColor(); |
| 333 |
} |
| 334 |
} |
| 335 |
catch (std::invalid_argument&) { |
| 336 |
THROWM( |
| 337 |
Base::ParserError, |
| 338 |
"Invalid color format, expected #RRGGBB or rgb(r,g,b) or rgba(r,g,b,a)" |
| 339 |
); |
| 340 |
} |
| 341 |
|
| 342 |
THROWM(Base::ParserError, "Unknown color format"); |
| 343 |
} |
| 344 |
|
| 345 |
bool Parser::peekParameter() |
| 346 |
{ |
| 347 |
skipWhitespace(); |
| 348 |
return pos < input.size() && input[pos] == '@'; |
| 349 |
} |
| 350 |
|
| 351 |
std::unique_ptr<Expr> Parser::parseParameter() |
| 352 |
{ |
| 353 |
skipWhitespace(); |
| 354 |
if (!match('@')) { |
| 355 |
THROWM(Base::ParserError, fmt::format("Expected '@' for parameter, got '{}'", input[pos])); |
| 356 |
} |
| 357 |
size_t start = pos; |
| 358 |
while (pos < input.size() && (isalnum(input[pos]) || input[pos] == '_')) { |
| 359 |
++pos; |
| 360 |
} |
| 361 |
if (start == pos) { |
| 362 |
THROWM( |
| 363 |
Base::ParserError, |
| 364 |
fmt::format("Expected parameter name after '@', got '{}'", input[pos]) |
| 365 |
); |
| 366 |
} |
| 367 |
return std::make_unique<ParameterReference>(input.substr(start, pos - start)); |
| 368 |
} |
| 369 |
|
| 370 |
bool Parser::peekFunction() |
| 371 |
{ |
| 372 |
skipWhitespace(); |
| 373 |
return pos < input.size() && isalpha(input[pos]); |
| 374 |
} |
| 375 |
|
| 376 |
std::unique_ptr<Expr> Parser::parseFunctionCall() |
| 377 |
{ |
| 378 |
skipWhitespace(); |
| 379 |
size_t start = pos; |
| 380 |
while (pos < input.size() && isalnum(input[pos])) { |
| 381 |
++pos; |
| 382 |
} |
| 383 |
std::string functionName = input.substr(start, pos - start); |
| 384 |
|
| 385 |
if (!match('(')) { |
| 386 |
THROWM(Base::ParserError, fmt::format("Expected '(' after function name, got '{}'", input[pos])); |
| 387 |
} |
| 388 |
|
| 389 |
std::vector<std::unique_ptr<Expr>> arguments; |
| 390 |
if (!match(')')) { |
| 391 |
do { // NOLINT(*-avoid-do-while) |
| 392 |
arguments.push_back(parseExpression()); |
| 393 |
} while (match(',')); |
| 394 |
|
| 395 |
if (!match(')')) { |
| 396 |
THROWM( |
| 397 |
Base::ParserError, |
| 398 |
fmt::format("Expected ')' after function arguments, got '{}'", input[pos]) |
| 399 |
); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
return std::make_unique<FunctionCall>(functionName, std::move(arguments)); |
| 404 |
} |
| 405 |
|
| 406 |
int Parser::parseInt() |
| 407 |
{ |
| 408 |
skipWhitespace(); |
| 409 |
size_t start = pos; |
| 410 |
while (pos < input.size() && (isdigit(input[pos]) || input[pos] == '.')) { |
| 411 |
++pos; |
| 412 |
} |
| 413 |
return std::stoi(input.substr(start, pos - start)); |
| 414 |
} |
| 415 |
|
| 416 |
std::unique_ptr<Expr> Parser::parseNumber() |
| 417 |
{ |
| 418 |
skipWhitespace(); |
| 419 |
size_t start = pos; |
| 420 |
while (pos < input.size() && (isdigit(input[pos]) || input[pos] == '.')) { |
| 421 |
++pos; |
| 422 |
} |
| 423 |
|
| 424 |
std::string number = input.substr(start, pos - start); |
| 425 |
|
| 426 |
try { |
| 427 |
double value = std::stod(number); |
| 428 |
std::string unit = parseUnit(); |
| 429 |
return std::make_unique<Number>(value, unit); |
| 430 |
} |
| 431 |
catch (std::invalid_argument&) { |
| 432 |
THROWM(Base::ParserError, fmt::format("Invalid number: {}", number)); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
std::string Parser::parseUnit() |
| 437 |
{ |
| 438 |
skipWhitespace(); |
| 439 |
size_t start = pos; |
| 440 |
while (pos < input.size() && (isalpha(input[pos]) || input[pos] == '%')) { |
| 441 |
++pos; |
| 442 |
} |
| 443 |
if (start == pos) { |
| 444 |
return ""; |
| 445 |
} |
| 446 |
return input.substr(start, pos - start); |
| 447 |
} |
| 448 |
|
| 449 |
bool Parser::match(char expected) |
| 450 |
{ |
| 451 |
skipWhitespace(); |
| 452 |
if (pos < input.size() && input[pos] == expected) { |
| 453 |
++pos; |
| 454 |
return true; |
| 455 |
} |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
void Parser::skipWhitespace() |
| 460 |
{ |
| 461 |
while (pos < input.size() && isspace(input[pos])) { |
| 462 |
++pos; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
} // namespace Gui::StyleParameters |