File size: 4,955 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | // SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2025 Kacper Donat <kacper@kadet.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#ifndef STYLEPARAMETERS_PARSER_H
#define STYLEPARAMETERS_PARSER_H
#include <memory>
#include <string>
#include <vector>
#include "ParameterManager.h"
namespace Gui::StyleParameters
{
enum class Operator : std::uint8_t
{
Add,
Subtract,
Multiply,
Divide
};
struct EvaluationContext
{
const ParameterManager* manager {};
ParameterManager::ResolveContext context;
};
// Abstract Syntax Tree (AST) Base
struct GuiExport Expr
{
Expr() = default;
FC_DEFAULT_MOVE(Expr);
FC_DISABLE_COPY(Expr);
virtual Value evaluate(const EvaluationContext& context) const = 0;
virtual ~Expr() = default;
};
struct GuiExport ParameterReference: public Expr
{
std::string name;
explicit ParameterReference(std::string name)
: name(std::move(name))
{}
Value evaluate(const EvaluationContext& context) const override;
};
struct GuiExport Number: public Expr
{
Numeric value;
Number(double value, std::string unit)
: value({value, std::move(unit)})
{}
Value evaluate([[maybe_unused]] const EvaluationContext& context) const override;
};
struct GuiExport Color: public Expr
{
Base::Color color;
explicit Color(Base::Color color)
: color(std::move(color))
{}
Value evaluate([[maybe_unused]] const EvaluationContext& context) const override;
};
struct GuiExport FunctionCall: public Expr
{
std::string functionName;
std::vector<std::unique_ptr<Expr>> arguments;
FunctionCall(std::string functionName, std::vector<std::unique_ptr<Expr>> arguments)
: functionName(std::move(functionName))
, arguments(std::move(arguments))
{}
Value evaluate(const EvaluationContext& context) const override;
};
struct GuiExport BinaryOp: public Expr
{
std::unique_ptr<Expr> left, right;
Operator op;
BinaryOp(std::unique_ptr<Expr> left, Operator op, std::unique_ptr<Expr> right)
: left(std::move(left))
, right(std::move(right))
, op(op)
{}
Value evaluate(const EvaluationContext& context) const override;
};
struct GuiExport UnaryOp: public Expr
{
Operator op;
std::unique_ptr<Expr> operand;
UnaryOp(Operator op, std::unique_ptr<Expr> operand)
: op(op)
, operand(std::move(operand))
{}
Value evaluate(const EvaluationContext& context) const override;
};
class GuiExport Parser
{
static constexpr auto rgbFunction = "rgb(";
static constexpr auto rgbaFunction = "rgba(";
std::string input;
size_t pos = 0;
public:
explicit Parser(std::string input)
: input(std::move(input))
{}
std::unique_ptr<Expr> parse();
private:
bool peekString(const char* function) const;
std::unique_ptr<Expr> parseExpression();
std::unique_ptr<Expr> parseTerm();
std::unique_ptr<Expr> parseFactor();
bool peekColor();
std::unique_ptr<Expr> parseColor();
bool peekParameter();
std::unique_ptr<Expr> parseParameter();
bool peekFunction();
std::unique_ptr<Expr> parseFunctionCall();
int parseInt();
std::unique_ptr<Expr> parseNumber();
std::string parseUnit();
bool match(char expected);
void skipWhitespace();
};
} // namespace Gui::StyleParameters
#endif // STYLEPARAMETERS_PARSER_H
|