| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 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 |
#include <lsp-plug.in/io/InStringSequence.h> |
| 23 |
#include <lsp-plug.in/expr/parser.h> |
| 24 |
#include <lsp-plug.in/expr/evaluator.h> |
| 25 |
#include <lsp-plug.in/expr/Expression.h> |
| 26 |
#include <lsp-plug.in/expr/Tokenizer.h> |
| 27 |
|
| 28 |
namespace lsp |
| 29 |
{ |
| 30 |
namespace expr |
| 31 |
{ |
| 32 |
Expression::Expression() |
| 33 |
{ |
| 34 |
pResolver = NULL; |
| 35 |
} |
| 36 |
|
| 37 |
Expression::Expression(Resolver *res) |
| 38 |
{ |
| 39 |
pResolver = res; |
| 40 |
} |
| 41 |
|
| 42 |
Expression::~Expression() |
| 43 |
{ |
| 44 |
destroy_all_data(); |
| 45 |
pResolver = NULL; |
| 46 |
} |
| 47 |
|
| 48 |
void Expression::destroy() |
| 49 |
{ |
| 50 |
destroy_all_data(); |
| 51 |
pResolver = NULL; |
| 52 |
} |
| 53 |
|
| 54 |
void Expression::destroy_all_data() |
| 55 |
{ |
| 56 |
for (size_t i=0, n=vDependencies.size(); i<n; ++i) |
| 57 |
{ |
| 58 |
LSPString *dep = vDependencies.uget(i); |
| 59 |
if (dep != NULL) |
| 60 |
delete dep; |
| 61 |
} |
| 62 |
vDependencies.flush(); |
| 63 |
|
| 64 |
for (size_t i=0, n=vRoots.size(); i<n; ++i) |
| 65 |
{ |
| 66 |
root_t *r = vRoots.uget(i); |
| 67 |
if (r->expr != NULL) |
| 68 |
{ |
| 69 |
parse_destroy(r->expr); |
| 70 |
r->expr = NULL; |
| 71 |
} |
| 72 |
destroy_value(&r->result); |
| 73 |
} |
| 74 |
vRoots.flush(); |
| 75 |
} |
| 76 |
|
| 77 |
status_t Expression::result(value_t *result, size_t idx) |
| 78 |
{ |
| 79 |
root_t *r = vRoots.get(idx); |
| 80 |
if (r == NULL) |
| 81 |
return STATUS_BAD_ARGUMENTS; |
| 82 |
if (result == NULL) |
| 83 |
return STATUS_OK; |
| 84 |
|
| 85 |
return copy_value(result, &r->result); |
| 86 |
} |
| 87 |
|
| 88 |
status_t Expression::evaluate(value_t *result) |
| 89 |
{ |
| 90 |
status_t res = STATUS_BAD_STATE; |
| 91 |
|
| 92 |
for (size_t i=0, n=vRoots.size(); i<n; ++i) |
| 93 |
{ |
| 94 |
root_t *r = vRoots.uget(i); |
| 95 |
if (r->expr != NULL) |
| 96 |
res = r->expr->eval(&r->result, r->expr, pResolver); |
| 97 |
else |
| 98 |
{ |
| 99 |
set_value_undef(&r->result); |
| 100 |
res = STATUS_OK; |
| 101 |
} |
| 102 |
|
| 103 |
if (res != STATUS_OK) |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
// Store the result if ALL is OK |
| 108 |
if ((res == STATUS_OK) && (result != NULL)) |
| 109 |
res = copy_value(result, &vRoots.uget(0)->result); |
| 110 |
|
| 111 |
return res; |
| 112 |
} |
| 113 |
|
| 114 |
status_t Expression::evaluate(size_t idx, value_t *result) |
| 115 |
{ |
| 116 |
root_t *r = vRoots.get(idx); |
| 117 |
if (r == NULL) |
| 118 |
return STATUS_BAD_ARGUMENTS; |
| 119 |
|
| 120 |
status_t res = STATUS_BAD_STATE; |
| 121 |
if (r->expr != NULL) |
| 122 |
res = r->expr->eval(&r->result, r->expr, pResolver); |
| 123 |
else |
| 124 |
{ |
| 125 |
set_value_undef(&r->result); |
| 126 |
res = STATUS_OK; |
| 127 |
} |
| 128 |
|
| 129 |
// Store the result if ALL is OK |
| 130 |
if ((res == STATUS_OK) && (result != NULL)) |
| 131 |
*result = r->result; |
| 132 |
|
| 133 |
return res; |
| 134 |
} |
| 135 |
|
| 136 |
status_t Expression::parse(const char *expr, size_t flags) |
| 137 |
{ |
| 138 |
io::InStringSequence is; |
| 139 |
|
| 140 |
status_t res = is.wrap(expr, "UTF-8"); |
| 141 |
if (res == STATUS_OK) |
| 142 |
res = parse(&is, flags); |
| 143 |
if (res == STATUS_OK) |
| 144 |
res = is.close(); |
| 145 |
else |
| 146 |
is.close(); |
| 147 |
|
| 148 |
return res; |
| 149 |
} |
| 150 |
|
| 151 |
status_t Expression::parse(const char *expr, const char *charset, size_t flags) |
| 152 |
{ |
| 153 |
io::InStringSequence is; |
| 154 |
|
| 155 |
status_t res = is.wrap(expr, (charset != NULL) ? charset : "UTF-8"); |
| 156 |
if (res == STATUS_OK) |
| 157 |
res = parse(&is, flags); |
| 158 |
if (res == STATUS_OK) |
| 159 |
res = is.close(); |
| 160 |
else |
| 161 |
is.close(); |
| 162 |
|
| 163 |
return res; |
| 164 |
} |
| 165 |
|
| 166 |
status_t Expression::parse(const LSPString *expr, size_t flags) |
| 167 |
{ |
| 168 |
io::InStringSequence is; |
| 169 |
|
| 170 |
status_t res = is.wrap(expr); |
| 171 |
if (res == STATUS_OK) |
| 172 |
res = parse(&is, flags); |
| 173 |
if (res == STATUS_OK) |
| 174 |
res = is.close(); |
| 175 |
else |
| 176 |
is.close(); |
| 177 |
|
| 178 |
return res; |
| 179 |
} |
| 180 |
|
| 181 |
status_t Expression::parse_regular(io::IInSequence *seq, size_t flags) |
| 182 |
{ |
| 183 |
status_t res = STATUS_OK; |
| 184 |
Tokenizer t(seq); |
| 185 |
|
| 186 |
while (true) |
| 187 |
{ |
| 188 |
// Allocate placeholder |
| 189 |
root_t *root = vRoots.add(); |
| 190 |
if (root == NULL) |
| 191 |
{ |
| 192 |
res = STATUS_NO_MEM; |
| 193 |
break; |
| 194 |
} |
| 195 |
|
| 196 |
// Parse expression |
| 197 |
root->expr = NULL; |
| 198 |
init_value(&root->result); |
| 199 |
res = parse_expression(&root->expr, &t, TF_GET); |
| 200 |
if (res != STATUS_OK) |
| 201 |
break; |
| 202 |
|
| 203 |
// Analyze next token after expression |
| 204 |
token_t tok = t.get_token(TF_NONE); |
| 205 |
if (tok != TT_SEMICOLON) |
| 206 |
{ |
| 207 |
res = t.error(); |
| 208 |
if (tok == TT_EOF) |
| 209 |
res = STATUS_OK; |
| 210 |
else if (res == STATUS_OK) |
| 211 |
res = STATUS_INVALID_VALUE; |
| 212 |
break; |
| 213 |
} |
| 214 |
else if (!(flags & FLAG_MULTIPLE)) |
| 215 |
break; |
| 216 |
} |
| 217 |
|
| 218 |
return res; |
| 219 |
} |
| 220 |
|
| 221 |
status_t Expression::prepend_string(expr_t **expr, const LSPString *str, bool force) |
| 222 |
{ |
| 223 |
// Do we really need to prepend? |
| 224 |
if ((str->length() <= 0) && (!force)) |
| 225 |
return STATUS_OK; |
| 226 |
|
| 227 |
// What to do with expression? |
| 228 |
expr_t *right = parse_create_expr(); |
| 229 |
if (right == NULL) |
| 230 |
return STATUS_NO_MEM; |
| 231 |
|
| 232 |
right->type = ET_VALUE; |
| 233 |
right->eval = eval_value; |
| 234 |
|
| 235 |
status_t res = init_value_string(&right->value, str); |
| 236 |
if (res != STATUS_OK) |
| 237 |
{ |
| 238 |
parse_destroy(right); |
| 239 |
return res; |
| 240 |
} |
| 241 |
|
| 242 |
// Do we need to immediately return? |
| 243 |
if (*expr == NULL) |
| 244 |
{ |
| 245 |
*expr = right; |
| 246 |
return STATUS_OK; |
| 247 |
} |
| 248 |
|
| 249 |
// Need to create middle expression |
| 250 |
expr_t *middle = parse_create_expr(); |
| 251 |
if (middle == NULL) |
| 252 |
{ |
| 253 |
parse_destroy(right); |
| 254 |
return STATUS_NO_MEM; |
| 255 |
} |
| 256 |
|
| 257 |
middle->type = ET_CALC; |
| 258 |
middle->eval = eval_strcat; |
| 259 |
middle->calc.left = *expr; |
| 260 |
middle->calc.right = right; |
| 261 |
middle->calc.cond = NULL; |
| 262 |
*expr = middle; |
| 263 |
|
| 264 |
return STATUS_OK; |
| 265 |
} |
| 266 |
|
| 267 |
status_t Expression::parse_substitution(expr_t **expr, Tokenizer *t) |
| 268 |
{ |
| 269 |
expr_t *right = NULL; |
| 270 |
token_t tok = t->get_token(TF_GET); |
| 271 |
|
| 272 |
// Parse expression |
| 273 |
status_t res = (tok == TT_BAREWORD) ? |
| 274 |
parse_identifier(&right, t, TF_BAREWORD) : |
| 275 |
parse_expression(&right, t, TF_NONE); |
| 276 |
|
| 277 |
if (res != STATUS_OK) |
| 278 |
return res; |
| 279 |
|
| 280 |
// Analyze next token after expression |
| 281 |
tok = t->get_token(TF_NONE); |
| 282 |
if (tok != TT_RCBRACE) |
| 283 |
res = (tok == TT_EOF) ? STATUS_EOF : STATUS_BAD_TOKEN; |
| 284 |
|
| 285 |
if (res == STATUS_OK) |
| 286 |
*expr = right; |
| 287 |
|
| 288 |
return res; |
| 289 |
} |
| 290 |
|
| 291 |
status_t Expression::parse_string(io::IInSequence *seq, size_t flags) |
| 292 |
{ |
| 293 |
// Create expression |
| 294 |
LSPString tmp; |
| 295 |
Tokenizer t(seq); |
| 296 |
status_t res = STATUS_OK; |
| 297 |
expr_t *expr = NULL; |
| 298 |
bool predicate = false; |
| 299 |
|
| 300 |
while (true) |
| 301 |
{ |
| 302 |
lsp_swchar_t wc = seq->read(); |
| 303 |
if (predicate) |
| 304 |
{ |
| 305 |
predicate = false; |
| 306 |
if (wc < 0) |
| 307 |
{ |
| 308 |
res = (wc == -STATUS_EOF) ? STATUS_OK : -wc; |
| 309 |
break; |
| 310 |
} |
| 311 |
else if (wc != '{') |
| 312 |
{ |
| 313 |
if (wc != '$') |
| 314 |
res = (tmp.append('$')) ? STATUS_OK : STATUS_NO_MEM; |
| 315 |
|
| 316 |
if (res == STATUS_OK) |
| 317 |
res = (tmp.append(wc)) ? STATUS_OK : STATUS_NO_MEM; |
| 318 |
if (res != STATUS_OK) |
| 319 |
break; |
| 320 |
} |
| 321 |
else // Sub-expression |
| 322 |
{ |
| 323 |
// Prepend string if it is defined |
| 324 |
res = prepend_string(&expr, &tmp, false); |
| 325 |
if (res != STATUS_OK) |
| 326 |
break; |
| 327 |
tmp.clear(); |
| 328 |
|
| 329 |
// Parse expression |
| 330 |
expr_t *right = NULL; |
| 331 |
res = parse_substitution(&right, &t); |
| 332 |
if (res != STATUS_OK) |
| 333 |
break; |
| 334 |
|
| 335 |
// What to do with expression? |
| 336 |
if (expr != NULL) |
| 337 |
{ |
| 338 |
expr_t *middle = parse_create_expr(); |
| 339 |
if (middle == NULL) |
| 340 |
{ |
| 341 |
parse_destroy(right); |
| 342 |
res = STATUS_NO_MEM; |
| 343 |
break; |
| 344 |
} |
| 345 |
middle->type = ET_CALC; |
| 346 |
middle->eval = eval_strcat; |
| 347 |
middle->calc.left = expr; |
| 348 |
middle->calc.right = right; |
| 349 |
middle->calc.cond = NULL; |
| 350 |
expr = middle; |
| 351 |
} |
| 352 |
else |
| 353 |
expr = right; |
| 354 |
} |
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
// Analyze result |
| 359 |
if (wc < 0) |
| 360 |
{ |
| 361 |
res = (wc == -STATUS_EOF) ? STATUS_OK : -wc; |
| 362 |
break; |
| 363 |
} |
| 364 |
else if (wc == '$') |
| 365 |
predicate = true; |
| 366 |
else if (!tmp.append(wc)) // Append character to string |
| 367 |
{ |
| 368 |
res = STATUS_NO_MEM; |
| 369 |
break; |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
// Need to prepend string? |
| 375 |
if (res == STATUS_OK) |
| 376 |
res = prepend_string(&expr, &tmp, expr == NULL); |
| 377 |
|
| 378 |
// Analyze result |
| 379 |
if (res != STATUS_OK) |
| 380 |
{ |
| 381 |
parse_destroy(expr); |
| 382 |
return res; |
| 383 |
} |
| 384 |
|
| 385 |
// Allocate placeholder |
| 386 |
root_t *root = vRoots.add(); |
| 387 |
if (root == NULL) |
| 388 |
{ |
| 389 |
parse_destroy(expr); |
| 390 |
res = STATUS_NO_MEM; |
| 391 |
} |
| 392 |
else |
| 393 |
{ |
| 394 |
root->expr = expr; |
| 395 |
init_value_undef(&root->result); |
| 396 |
} |
| 397 |
|
| 398 |
return res; |
| 399 |
} |
| 400 |
|
| 401 |
status_t Expression::parse(io::IInSequence *seq, size_t flags) |
| 402 |
{ |
| 403 |
status_t res = STATUS_OK; |
| 404 |
destroy_all_data(); |
| 405 |
|
| 406 |
if (flags & FLAG_STRING) |
| 407 |
res = parse_string(seq, flags & (~FLAG_STRING)); |
| 408 |
else |
| 409 |
res = parse_regular(seq, flags); |
| 410 |
|
| 411 |
if (res == STATUS_OK) |
| 412 |
res = post_process(); |
| 413 |
|
| 414 |
if (res != STATUS_OK) |
| 415 |
destroy_all_data(); |
| 416 |
|
| 417 |
return res; |
| 418 |
} |
| 419 |
|
| 420 |
status_t Expression::post_process() |
| 421 |
{ |
| 422 |
// Scan for dependencies |
| 423 |
for (size_t i=0, n=vRoots.size(); i<n; ++i) |
| 424 |
{ |
| 425 |
root_t *root = vRoots.uget(i); |
| 426 |
if (root == NULL) |
| 427 |
continue; |
| 428 |
|
| 429 |
status_t res = scan_dependencies(root->expr); |
| 430 |
if (res != STATUS_OK) |
| 431 |
return res; |
| 432 |
} |
| 433 |
|
| 434 |
return STATUS_OK; |
| 435 |
} |
| 436 |
|
| 437 |
status_t Expression::add_dependency(const LSPString *str) |
| 438 |
{ |
| 439 |
// Already have such dependency? |
| 440 |
for (size_t i=0, n=vDependencies.size(); i<n; ++i) |
| 441 |
{ |
| 442 |
LSPString *dep = vDependencies.uget(i); |
| 443 |
if (dep->equals(str)) |
| 444 |
return STATUS_OK; |
| 445 |
} |
| 446 |
|
| 447 |
// Add new dependency |
| 448 |
LSPString *dep = str->clone(); |
| 449 |
if (dep == NULL) |
| 450 |
return STATUS_NO_MEM; |
| 451 |
|
| 452 |
if (vDependencies.add(dep)) |
| 453 |
return STATUS_OK; |
| 454 |
|
| 455 |
delete dep; |
| 456 |
return STATUS_NO_MEM; |
| 457 |
} |
| 458 |
|
| 459 |
status_t Expression::scan_dependencies(expr_t *expr) |
| 460 |
{ |
| 461 |
status_t res; |
| 462 |
if (expr == NULL) |
| 463 |
return STATUS_OK; |
| 464 |
|
| 465 |
switch (expr->type) |
| 466 |
{ |
| 467 |
case ET_VALUE: |
| 468 |
return STATUS_OK; |
| 469 |
case ET_CALC: |
| 470 |
{ |
| 471 |
res = scan_dependencies(expr->calc.cond); |
| 472 |
if (res == STATUS_OK) |
| 473 |
res = scan_dependencies(expr->calc.left); |
| 474 |
if (res == STATUS_OK) |
| 475 |
res = scan_dependencies(expr->calc.right); |
| 476 |
return res; |
| 477 |
} |
| 478 |
case ET_RESOLVE: |
| 479 |
{ |
| 480 |
res = add_dependency(expr->resolve.name); |
| 481 |
if (res != STATUS_OK) |
| 482 |
return res; |
| 483 |
for (size_t i=0; i<expr->resolve.count; ++i) |
| 484 |
{ |
| 485 |
res = scan_dependencies(expr->resolve.items[i]); |
| 486 |
if (res != STATUS_OK) |
| 487 |
break; |
| 488 |
} |
| 489 |
return res; |
| 490 |
} |
| 491 |
case ET_CALL: |
| 492 |
{ |
| 493 |
for (size_t i=0; i<expr->call.count; ++i) |
| 494 |
{ |
| 495 |
res = scan_dependencies(expr->call.items[i]); |
| 496 |
if (res != STATUS_OK) |
| 497 |
break; |
| 498 |
} |
| 499 |
return STATUS_OK; |
| 500 |
} |
| 501 |
default: |
| 502 |
break; |
| 503 |
} |
| 504 |
return STATUS_CORRUPTED; |
| 505 |
} |
| 506 |
|
| 507 |
bool Expression::has_dependency(const LSPString *str) const |
| 508 |
{ |
| 509 |
for (size_t i=0, n=vDependencies.size(); i<n; ++i) |
| 510 |
{ |
| 511 |
const LSPString *dep = vDependencies.uget(i); |
| 512 |
if (dep->equals(str)) |
| 513 |
return true; |
| 514 |
} |
| 515 |
return false; |
| 516 |
} |
| 517 |
|
| 518 |
bool Expression::has_dependency(const char *str) const |
| 519 |
{ |
| 520 |
LSPString tmp; |
| 521 |
if (!tmp.set_utf8(str)) |
| 522 |
return false; |
| 523 |
|
| 524 |
for (size_t i=0, n=vDependencies.size(); i<n; ++i) |
| 525 |
{ |
| 526 |
const LSPString *dep = vDependencies.uget(i); |
| 527 |
if (dep->equals(&tmp)) |
| 528 |
return true; |
| 529 |
} |
| 530 |
return false; |
| 531 |
} |
| 532 |
|
| 533 |
} /* namespace calc */ |
| 534 |
} /* namespace lsp */ |