| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 16 сент. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_EXPR_EXPRESSION_H_ |
| 23 |
#define LSP_PLUG_IN_EXPR_EXPRESSION_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/status.h> |
| 27 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 28 |
#include <lsp-plug.in/io/IInSequence.h> |
| 29 |
#include <lsp-plug.in/expr/types.h> |
| 30 |
#include <lsp-plug.in/expr/Resolver.h> |
| 31 |
|
| 32 |
#include <lsp-plug.in/lltl/darray.h> |
| 33 |
#include <lsp-plug.in/lltl/parray.h> |
| 34 |
|
| 35 |
namespace lsp |
| 36 |
{ |
| 37 |
namespace expr |
| 38 |
{ |
| 39 |
struct expr_t; |
| 40 |
class Tokenizer; |
| 41 |
|
| 42 |
class Expression |
| 43 |
{ |
| 44 |
public: |
| 45 |
enum expr_flags |
| 46 |
{ |
| 47 |
FLAG_NONE = 0, |
| 48 |
FLAG_MULTIPLE = 1 << 0, |
| 49 |
FLAG_STRING = 1 << 1 |
| 50 |
}; |
| 51 |
|
| 52 |
protected: |
| 53 |
typedef struct root_t |
| 54 |
{ |
| 55 |
expr_t *expr; |
| 56 |
value_t result; |
| 57 |
} root_t; |
| 58 |
|
| 59 |
protected: |
| 60 |
Resolver *pResolver; |
| 61 |
lltl::darray<root_t> vRoots; |
| 62 |
lltl::parray<LSPString> vDependencies; |
| 63 |
|
| 64 |
protected: |
| 65 |
void destroy_all_data(); |
| 66 |
status_t prepend_string(expr_t **expr, const LSPString *str, bool force); |
| 67 |
status_t parse_substitution(expr_t **expr, Tokenizer *t); |
| 68 |
status_t parse_regular(io::IInSequence *seq, size_t flags); |
| 69 |
status_t parse_string(io::IInSequence *seq, size_t flags); |
| 70 |
status_t post_process(); |
| 71 |
status_t scan_dependencies(expr_t *expr); |
| 72 |
status_t add_dependency(const LSPString *str); |
| 73 |
|
| 74 |
public: |
| 75 |
explicit Expression(); |
| 76 |
explicit Expression(Resolver *res); |
| 77 |
Expression(const Expression &) = delete; |
| 78 |
Expression(Expression &&) = delete; |
| 79 |
virtual ~Expression(); |
| 80 |
|
| 81 |
Expression & operator = (const Expression &) = delete; |
| 82 |
Expression & operator = (Expression &&) = delete; |
| 83 |
|
| 84 |
void destroy(); |
| 85 |
|
| 86 |
public: |
| 87 |
/** |
| 88 |
* Parse the expression |
| 89 |
* @param expr string containing expression in UTF-8 |
| 90 |
* @param flags additional flags |
| 91 |
* @return status of operation |
| 92 |
*/ |
| 93 |
status_t parse(const char *expr, size_t flags = FLAG_NONE); |
| 94 |
|
| 95 |
/** |
| 96 |
* Parse the expression |
| 97 |
* @param expr string containing expression |
| 98 |
* @param charset character set, UTF-8 if NULL |
| 99 |
* @param flags additional flags |
| 100 |
* @return status of operation |
| 101 |
*/ |
| 102 |
status_t parse(const char *expr, const char *charset = NULL, size_t flags = FLAG_NONE); |
| 103 |
|
| 104 |
/** |
| 105 |
* Parse the expression |
| 106 |
* @param expr string containing expression |
| 107 |
* @param flags additional flags |
| 108 |
* @return status of operation |
| 109 |
*/ |
| 110 |
status_t parse(const LSPString *expr, size_t flags = FLAG_NONE); |
| 111 |
|
| 112 |
/** |
| 113 |
* Parse the expression |
| 114 |
* @param seq character input sequence |
| 115 |
* @param flags additional flags |
| 116 |
* @return status of operation |
| 117 |
*/ |
| 118 |
status_t parse(io::IInSequence *seq, size_t flags = FLAG_NONE); |
| 119 |
|
| 120 |
/** |
| 121 |
* Check that expression is valid |
| 122 |
* @return true if the expression is valid |
| 123 |
*/ |
| 124 |
inline bool valid() const { return vRoots.size() > 0; }; |
| 125 |
|
| 126 |
/** |
| 127 |
* Evaluate all the expressions |
| 128 |
* @param result pointer to return value of the zero-indexed expression |
| 129 |
* @return status of operation |
| 130 |
*/ |
| 131 |
status_t evaluate(value_t *result = NULL); |
| 132 |
|
| 133 |
/** |
| 134 |
* Evaluate the specific expression |
| 135 |
* @param idx expression index |
| 136 |
* @param result pointer to return value of the specified expression |
| 137 |
* @return status of operation |
| 138 |
*/ |
| 139 |
status_t evaluate(size_t idx, value_t *result = NULL); |
| 140 |
|
| 141 |
/** |
| 142 |
* Get number of results |
| 143 |
* @return number of results |
| 144 |
*/ |
| 145 |
inline size_t results() const { return vRoots.size(); }; |
| 146 |
|
| 147 |
/** |
| 148 |
* Get result of the specific expression |
| 149 |
* @param result the pointer to store the result |
| 150 |
* @param idx the result indes |
| 151 |
* @return status of operation |
| 152 |
*/ |
| 153 |
status_t result(value_t *result, size_t idx); |
| 154 |
|
| 155 |
/** |
| 156 |
* Get variable resolver |
| 157 |
* @return variable resolver |
| 158 |
*/ |
| 159 |
inline Resolver *resolver() { return pResolver; } |
| 160 |
|
| 161 |
/** |
| 162 |
* Sett variable resolver |
| 163 |
* @param resolver variable resolver |
| 164 |
*/ |
| 165 |
inline void set_resolver(Resolver *resolver) { pResolver = resolver; } |
| 166 |
|
| 167 |
/** |
| 168 |
* Get number of dependencies |
| 169 |
* @return number of dependencies |
| 170 |
*/ |
| 171 |
inline size_t dependencies() const { return vDependencies.size(); } |
| 172 |
|
| 173 |
/** |
| 174 |
* Get dependency |
| 175 |
* @param idx the index of dependency |
| 176 |
* @return dependency name or NULL |
| 177 |
*/ |
| 178 |
inline const LSPString *dependency(size_t idx) const { return vDependencies.get(idx); } |
| 179 |
|
| 180 |
/** |
| 181 |
* Check that expression has dependency |
| 182 |
* @param str dependency name |
| 183 |
* @return true if expression has dependency |
| 184 |
*/ |
| 185 |
bool has_dependency(const LSPString *str) const; |
| 186 |
|
| 187 |
/** |
| 188 |
* Check that expression has dependency |
| 189 |
* @param str dependency name |
| 190 |
* @return true if expression has dependency |
| 191 |
*/ |
| 192 |
bool has_dependency(const char *str) const; |
| 193 |
|
| 194 |
}; |
| 195 |
|
| 196 |
} /* namespace calc */ |
| 197 |
} /* namespace lsp */ |
| 198 |
|
| 199 |
#endif /* LSP_PLUG_IN_EXPR_EXPRESSION_H_ */ |