| 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 |
#ifndef STYLEPARAMETERS_PARSER_H |
| 25 |
#define STYLEPARAMETERS_PARSER_H |
| 26 |
|
| 27 |
#include <memory> |
| 28 |
#include <string> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "ParameterManager.h" |
| 32 |
|
| 33 |
namespace Gui::StyleParameters |
| 34 |
{ |
| 35 |
|
| 36 |
enum class Operator : std::uint8_t |
| 37 |
{ |
| 38 |
Add, |
| 39 |
Subtract, |
| 40 |
Multiply, |
| 41 |
Divide |
| 42 |
}; |
| 43 |
|
| 44 |
struct EvaluationContext |
| 45 |
{ |
| 46 |
const ParameterManager* manager {}; |
| 47 |
ParameterManager::ResolveContext context; |
| 48 |
}; |
| 49 |
|
| 50 |
// Abstract Syntax Tree (AST) Base |
| 51 |
struct GuiExport Expr |
| 52 |
{ |
| 53 |
Expr() = default; |
| 54 |
|
| 55 |
FC_DEFAULT_MOVE(Expr); |
| 56 |
FC_DISABLE_COPY(Expr); |
| 57 |
|
| 58 |
virtual Value evaluate(const EvaluationContext& context) const = 0; |
| 59 |
virtual ~Expr() = default; |
| 60 |
}; |
| 61 |
|
| 62 |
struct GuiExport ParameterReference: public Expr |
| 63 |
{ |
| 64 |
std::string name; |
| 65 |
|
| 66 |
explicit ParameterReference(std::string name) |
| 67 |
: name(std::move(name)) |
| 68 |
{} |
| 69 |
|
| 70 |
Value evaluate(const EvaluationContext& context) const override; |
| 71 |
}; |
| 72 |
|
| 73 |
struct GuiExport Number: public Expr |
| 74 |
{ |
| 75 |
Numeric value; |
| 76 |
|
| 77 |
Number(double value, std::string unit) |
| 78 |
: value({value, std::move(unit)}) |
| 79 |
{} |
| 80 |
|
| 81 |
Value evaluate([[maybe_unused]] const EvaluationContext& context) const override; |
| 82 |
}; |
| 83 |
|
| 84 |
struct GuiExport Color: public Expr |
| 85 |
{ |
| 86 |
Base::Color color; |
| 87 |
|
| 88 |
explicit Color(Base::Color color) |
| 89 |
: color(std::move(color)) |
| 90 |
{} |
| 91 |
|
| 92 |
Value evaluate([[maybe_unused]] const EvaluationContext& context) const override; |
| 93 |
}; |
| 94 |
|
| 95 |
struct GuiExport FunctionCall: public Expr |
| 96 |
{ |
| 97 |
std::string functionName; |
| 98 |
std::vector<std::unique_ptr<Expr>> arguments; |
| 99 |
|
| 100 |
FunctionCall(std::string functionName, std::vector<std::unique_ptr<Expr>> arguments) |
| 101 |
: functionName(std::move(functionName)) |
| 102 |
, arguments(std::move(arguments)) |
| 103 |
{} |
| 104 |
|
| 105 |
Value evaluate(const EvaluationContext& context) const override; |
| 106 |
}; |
| 107 |
|
| 108 |
struct GuiExport BinaryOp: public Expr |
| 109 |
{ |
| 110 |
std::unique_ptr<Expr> left, right; |
| 111 |
Operator op; |
| 112 |
|
| 113 |
BinaryOp(std::unique_ptr<Expr> left, Operator op, std::unique_ptr<Expr> right) |
| 114 |
: left(std::move(left)) |
| 115 |
, right(std::move(right)) |
| 116 |
, op(op) |
| 117 |
{} |
| 118 |
|
| 119 |
Value evaluate(const EvaluationContext& context) const override; |
| 120 |
}; |
| 121 |
|
| 122 |
struct GuiExport UnaryOp: public Expr |
| 123 |
{ |
| 124 |
Operator op; |
| 125 |
std::unique_ptr<Expr> operand; |
| 126 |
|
| 127 |
UnaryOp(Operator op, std::unique_ptr<Expr> operand) |
| 128 |
: op(op) |
| 129 |
, operand(std::move(operand)) |
| 130 |
{} |
| 131 |
|
| 132 |
Value evaluate(const EvaluationContext& context) const override; |
| 133 |
}; |
| 134 |
|
| 135 |
class GuiExport Parser |
| 136 |
{ |
| 137 |
static constexpr auto rgbFunction = "rgb("; |
| 138 |
static constexpr auto rgbaFunction = "rgba("; |
| 139 |
|
| 140 |
std::string input; |
| 141 |
size_t pos = 0; |
| 142 |
|
| 143 |
public: |
| 144 |
explicit Parser(std::string input) |
| 145 |
: input(std::move(input)) |
| 146 |
{} |
| 147 |
|
| 148 |
std::unique_ptr<Expr> parse(); |
| 149 |
|
| 150 |
private: |
| 151 |
bool peekString(const char* function) const; |
| 152 |
std::unique_ptr<Expr> parseExpression(); |
| 153 |
std::unique_ptr<Expr> parseTerm(); |
| 154 |
std::unique_ptr<Expr> parseFactor(); |
| 155 |
bool peekColor(); |
| 156 |
std::unique_ptr<Expr> parseColor(); |
| 157 |
bool peekParameter(); |
| 158 |
std::unique_ptr<Expr> parseParameter(); |
| 159 |
bool peekFunction(); |
| 160 |
std::unique_ptr<Expr> parseFunctionCall(); |
| 161 |
int parseInt(); |
| 162 |
std::unique_ptr<Expr> parseNumber(); |
| 163 |
std::string parseUnit(); |
| 164 |
bool match(char expected); |
| 165 |
void skipWhitespace(); |
| 166 |
}; |
| 167 |
|
| 168 |
} // namespace Gui::StyleParameters |
| 169 |
|
| 170 |
#endif // STYLEPARAMETERS_PARSER_H |