2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
22 #include "../../StdAfx.h"
24 #include "expression_parser.h"
34 #include <boost/any.hpp>
36 #include <common/log.h>
37 #include <common/except.h>
38 #include <common/utf.h>
39 #include <common/tweener.h>
41 namespace caspar { namespace core { namespace scene {
43 wchar_t next_non_whitespace(
44 std::wstring::const_iterator& cursor,
45 const std::wstring& str,
46 const std::wstring& error_if_eof)
48 while (cursor != str.end())
61 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
62 L"Unexpected end of input (" + error_if_eof + L") in " + str));
65 std::wstring at_position(
66 const std::wstring::const_iterator& cursor, const std::wstring& str)
68 int index = static_cast<int>(cursor - str.begin());
70 return L" at index " + boost::lexical_cast<std::wstring>(index)
75 binding<T> require(const boost::any& value)
77 auto b = as_binding(value);
79 if (is<binding<T>>(b))
80 return as<binding<T>>(b);
82 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
83 L"Required binding of type " + u16(typeid(T).name())
84 + L" but got " + u16(value.type().name())));
87 boost::any parse_expression(
88 std::wstring::const_iterator& cursor,
89 const std::wstring& str,
90 const variable_repository& var_repo);
92 boost::any parse_parenthesis(
93 std::wstring::const_iterator& cursor,
94 const std::wstring& str,
95 const variable_repository& var_repo)
97 if (*cursor++ != L'(')
98 CASPAR_THROW_EXCEPTION(user_error()
99 << msg_info(L"Expected (" + at_position(cursor, str)));
101 auto expr = parse_expression(cursor, str, var_repo);
103 if (next_non_whitespace(cursor, str, L"Expected )") != L')')
104 CASPAR_THROW_EXCEPTION(user_error()
105 << msg_info(L"Expected )" + at_position(cursor, str)));
112 boost::any create_animate_function(const std::vector<boost::any>& params, const variable_repository& var_repo)
114 if (params.size() != 3)
115 CASPAR_THROW_EXCEPTION(user_error()
116 << msg_info(L"animate() function requires three parameters: to_animate, duration, tweener"));
118 auto to_animate = require<double>(params.at(0));
119 auto frame_counter = var_repo(L"frame").as<int64_t>().as<double>();
120 auto duration = require<double>(params.at(1));
121 auto tw = require<std::wstring>(params.at(2)).transformed([](const std::wstring& s) { return tweener(s); });
123 return to_animate.animated(frame_counter, duration, tw);
126 boost::any create_sin_function(const std::vector<boost::any>& params, const variable_repository& var_repo)
128 if (params.size() != 1)
129 CASPAR_THROW_EXCEPTION(user_error()
130 << msg_info(L"sin() function requires one parameters: angle"));
132 auto angle = require<double>(params.at(0));
134 return angle.transformed([](double a) { return std::sin(a); });
137 boost::any create_cos_function(const std::vector<boost::any>& params, const variable_repository& var_repo)
139 if (params.size() != 1)
140 CASPAR_THROW_EXCEPTION(user_error()
141 << msg_info(L"cos() function requires one parameters: angle"));
143 auto angle = require<double>(params.at(0));
145 return angle.transformed([](double a) { return std::cos(a); });
148 boost::any parse_function(
149 const std::wstring& function_name,
150 std::wstring::const_iterator& cursor,
151 const std::wstring& str,
152 const variable_repository& var_repo)
154 static std::map<std::wstring, std::function<boost::any (const std::vector<boost::any>& params, const variable_repository& var_repo)>> FUNCTIONS
156 {L"animate", create_animate_function },
157 {L"sin", create_sin_function },
158 {L"cos", create_cos_function }
161 auto function = FUNCTIONS.find(function_name);
163 if (function == FUNCTIONS.end())
164 CASPAR_THROW_EXCEPTION(user_error()
165 << msg_info(function_name + L"() is an unknown function" + at_position(cursor, str)));
167 if (*cursor++ != L'(')
168 CASPAR_THROW_EXCEPTION(user_error()
169 << msg_info(L"Expected (" + at_position(cursor, str)));
171 std::vector<boost::any> params;
173 while (cursor != str.end())
175 params.push_back(parse_expression(cursor, str, var_repo));
177 auto next = next_non_whitespace(cursor, str, L"Expected , or )");
181 else if (next != L',')
182 CASPAR_THROW_EXCEPTION(user_error()
183 << msg_info(L"Expected ) or ," + at_position(cursor, str)));
188 if (next_non_whitespace(cursor, str, L"Expected , or )") != L')')
189 CASPAR_THROW_EXCEPTION(user_error()
190 << msg_info(L"Expected ) " + at_position(cursor, str)));
194 return function->second(params, var_repo);
197 double parse_constant(
198 std::wstring::const_iterator& cursor, const std::wstring& str)
200 std::wstring constant;
202 while (cursor != str.end())
204 wchar_t ch = *cursor;
206 if ((ch >= L'0' && ch <= L'9') || ch == L'.')
214 return boost::lexical_cast<double>(constant);
217 std::wstring parse_string_literal(
218 std::wstring::const_iterator& cursor, const std::wstring& str)
220 std::wstring literal;
222 if (*cursor++ != L'"')
223 CASPAR_THROW_EXCEPTION(user_error()
224 << msg_info(L"Expected (" + at_position(cursor, str)));
226 bool escaping = false;
228 while (cursor != str.end())
230 wchar_t ch = *cursor;
253 return std::move(literal);
271 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
272 L"Unexpected end of input (Expected closing \") in " + str));
275 boost::any parse_variable(
276 std::wstring::const_iterator& cursor,
277 const std::wstring& str,
278 const variable_repository& var_repo)
280 std::wstring variable_name;
282 while (cursor != str.end())
284 wchar_t ch = *cursor;
288 || (ch >= L'a' && ch <= L'z')
289 || (ch >= L'A' && ch <= L'Z')
290 || (variable_name.length() > 0 && ch >= L'0' && ch <= L'9'))
298 if (cursor != str.end() && *cursor == L'(')
299 return variable_name;
301 if (variable_name == L"true")
303 else if (variable_name == L"false")
306 variable& var = var_repo(variable_name);
308 if (var.is<double>())
309 return var.as<double>();
310 else if (var.is<int64_t>())
311 return var.as<int64_t>().as<double>();
312 else if (var.is<std::wstring>())
313 return var.as<std::wstring>();
314 else if (var.is<bool>())
315 return var.as<bool>();
317 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
318 L"Unhandled variable type of " + variable_name
319 + at_position(cursor, str)));
331 std::wstring characters;
335 op(wchar_t ch, int precedence, op_type type)
336 : characters(1, ch), precedence(precedence), type(type)
340 op(const std::wstring& chs, int precedence, op_type type)
341 : characters(chs), precedence(precedence), type(type)
346 op parse_operator(std::wstring::const_iterator& cursor, const std::wstring& str)
348 static const wchar_t NONE = L' ';
349 wchar_t first = NONE;
351 while (cursor != str.end())
353 wchar_t ch = *cursor;
359 return op(ch, 6, op::op_type::BINARY);
364 return op(ch, 5, op::op_type::BINARY);
368 return op(ch, 15, op::op_type::TERNARY);
371 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
372 L"Did not expect -" + at_position(cursor, str)));
380 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
381 L"Did not expect !" + at_position(cursor, str)));
389 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
390 L"Did not expect <" + at_position(cursor, str)));
398 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
399 L"Did not expect >" + at_position(cursor, str)));
409 return op(L"==", 9, op::op_type::BINARY);
411 else if (first == L'!')
414 return op(L"!=", 9, op::op_type::BINARY);
416 else if (first == L'>')
419 return op(L">=", 8, op::op_type::BINARY);
421 else if (first == L'<')
424 return op(L"<=", 8, op::op_type::BINARY);
426 else if (first == NONE)
432 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
433 L"Did not expect =" + at_position(cursor, str)));
440 return op(L"||", 14, op::op_type::BINARY);
442 else if (first == NONE)
448 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
449 L"Did not expect =" + at_position(cursor, str)));
456 return op(L"&&", 13, op::op_type::BINARY);
458 else if (first == NONE)
464 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
465 L"Did not expect =" + at_position(cursor, str)));
471 return op(L'-', 6, op::op_type::BINARY);
472 else if (first == L'!')
473 return op(L'!', 3, op::op_type::UNARY);
476 return op(L'<', 8, op::op_type::BINARY);
477 else if (first == L'>')
478 return op(L'>', 8, op::op_type::BINARY);
479 else if (first == L'-')
480 return op(L"unary-", 3, op::op_type::UNARY);
481 else if (first == L'!')
482 return op(L'!', 3, op::op_type::UNARY);
484 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
485 L"Expected second character of operator"
486 + at_position(cursor, str)));
490 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
491 L"Unexpected end of input (Expected operator) in " + str));
494 boost::any as_binding(const boost::any& value)
496 // Wrap supported constants as bindings
497 if (is<double>(value))
498 return binding<double>(as<double>(value));
499 else if (is<bool>(value))
500 return binding<bool>(as<bool>(value));
501 else if (is<std::wstring>(value))
502 return binding<std::wstring>(as<std::wstring>(value));
503 // Already one of the supported binding types
504 else if (is<binding<double>>(value))
505 return as<binding<double>>(value);
506 else if (is<binding<bool>>(value))
507 return as<binding<bool>>(value);
508 else if (is<binding<std::wstring>>(value))
509 return as<binding<std::wstring>>(value);
511 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
512 L"Couldn't detect type of " + u16(value.type().name())));
515 boost::any negative(const boost::any& to_create_negative_of)
517 return -require<double>(to_create_negative_of);
520 boost::any not_(const boost::any& to_create_not_of)
522 return !require<bool>(to_create_not_of);
525 boost::any multiply(const boost::any& lhs, boost::any& rhs)
527 return require<double>(lhs) * require<double>(rhs);
530 boost::any divide(const boost::any& lhs, boost::any& rhs)
532 return require<double>(lhs) / require<double>(rhs);
535 boost::any modulus(const boost::any& lhs, boost::any& rhs)
539 require<double>(lhs).as<int64_t>()
540 % require<double>(rhs).as<int64_t>()
544 binding<std::wstring> stringify(const boost::any& value)
546 auto b = as_binding(value);
548 if (is<binding<std::wstring>>(b))
549 return as<binding<std::wstring>>(b);
550 else if (is<binding<double>>(b))
551 return as<binding<double>>(b).as<std::wstring>();
552 else if (is<binding<bool>>(b))
553 return as<binding<bool>>(b).as<std::wstring>();
555 CASPAR_THROW_EXCEPTION(user_error()
556 << msg_info(L"Couldn't stringify " + u16(value.type().name())));
559 boost::any add(const boost::any& lhs, boost::any& rhs)
561 auto l = as_binding(lhs);
562 auto r = as_binding(rhs);
565 if (is<binding<double>>(l) && is<binding<double>>(r))
566 return as<binding<double>>(l) + as<binding<double>>(r);
568 else if (is<binding<std::wstring>>(l) && is<binding<std::wstring>>(r))
569 return as<binding<std::wstring>>(l) + as<binding<std::wstring>>(r);
570 // mixed types to string and concatenated
572 return stringify(lhs) + stringify(rhs);
575 boost::any subtract(const boost::any& lhs, boost::any& rhs)
577 return require<double>(lhs) - require<double>(rhs);
580 boost::any less(const boost::any& lhs, boost::any& rhs)
582 return require<double>(lhs) < require<double>(rhs);
585 boost::any less_or_equal(const boost::any& lhs, boost::any& rhs)
587 return require<double>(lhs) <= require<double>(rhs);
590 boost::any greater(const boost::any& lhs, boost::any& rhs)
592 return require<double>(lhs) > require<double>(rhs);
595 boost::any greater_or_equal(const boost::any& lhs, boost::any& rhs)
597 return require<double>(lhs) >= require<double>(rhs);
600 boost::any equal(const boost::any& lhs, boost::any& rhs)
602 auto l = as_binding(lhs);
603 auto r = as_binding(rhs);
606 if (is<binding<double>>(l) && is<binding<double>>(r))
607 return as<binding<double>>(l) == as<binding<double>>(r);
609 else if (is<binding<std::wstring>>(l) && is<binding<std::wstring>>(r))
610 return as<binding<std::wstring>>(l) == as<binding<std::wstring>>(r);
613 return require<bool>(l) == require<bool>(r);
616 boost::any and_(const boost::any& lhs, boost::any& rhs)
618 return require<bool>(lhs) && require<bool>(rhs);
621 boost::any or_(const boost::any& lhs, boost::any& rhs)
623 return require<bool>(lhs) || require<bool>(rhs);
628 const binding<bool>& condition,
629 const binding<T>& true_value,
630 const binding<T>& false_value)
632 return when(condition).then(true_value).otherwise(false_value);
636 const boost::any& condition,
637 const boost::any& true_value,
638 const boost::any& false_value)
640 auto cond = require<bool>(condition);
641 auto t = as_binding(true_value);
642 auto f = as_binding(false_value);
645 if (is<binding<double>>(t) && is<binding<double>>(f))
646 return ternary(cond, as<binding<double>>(t), as<binding<double>>(f));
648 else if (is<binding<std::wstring>>(t) && is<binding<std::wstring>>(f))
651 as<binding<std::wstring>>(t),
652 as<binding<std::wstring>>(f));
655 return ternary(cond, require<bool>(t), require<bool>(f));
658 void resolve_operators(int precedence, std::vector<boost::any>& tokens)
660 for (int i = 0; i < tokens.size(); ++i)
662 auto& token = tokens.at(i);
667 auto op_token = as<op>(token);
669 if (op_token.precedence != precedence)
672 int index_after = i + 1;
673 auto& token_after = tokens.at(index_after);
675 switch (op_token.type)
677 case op::op_type::UNARY:
678 if (op_token.characters == L"unary-")
680 tokens.at(i) = negative(token_after);
682 else if (op_token.characters == L"!")
684 tokens.at(i) = not_(token_after);
687 tokens.erase(tokens.begin() + index_after);
690 case op::op_type::BINARY:
692 auto& token_before = tokens.at(i - 1);
694 if (op_token.characters == L"*")
695 token_before = multiply(token_before, token_after);
696 else if (op_token.characters == L"/")
697 token_before = divide(token_before, token_after);
698 else if (op_token.characters == L"%")
699 token_before = modulus(token_before, token_after);
700 else if (op_token.characters == L"+")
701 token_before = add(token_before, token_after);
702 else if (op_token.characters == L"-")
703 token_before = subtract(token_before, token_after);
704 else if (op_token.characters == L"<")
705 token_before = less(token_before, token_after);
706 else if (op_token.characters == L"<=")
707 token_before = less_or_equal(token_before, token_after);
708 else if (op_token.characters == L">")
709 token_before = greater(token_before, token_after);
710 else if (op_token.characters == L">=")
711 token_before = greater_or_equal(token_before, token_after);
712 else if (op_token.characters == L"==")
713 token_before = equal(token_before, token_after);
714 else if (op_token.characters == L"!=")
715 token_before = not_(equal(token_before, token_after));
716 else if (op_token.characters == L"&&")
717 token_before = and_(token_before, token_after);
718 else if (op_token.characters == L"||")
719 token_before = or_(token_before, token_after);
722 tokens.erase(tokens.begin() + i, tokens.begin() + i + 2);
726 case op::op_type::TERNARY:
727 if (op_token.characters == L"?")
729 auto& token_before = tokens.at(i - 1);
730 auto& token_colon_operator = tokens.at(i + 2);
732 if (as<op>(token_colon_operator).characters != L":")
733 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
734 L"Expected : as part of ternary expression"));
736 auto& token_false_value = tokens.at(i + 3);
737 token_before = ternary(
738 token_before, token_after, token_false_value);
739 tokens.erase(tokens.begin() + i, tokens.begin() + i + 4);
748 boost::any parse_expression(
749 std::wstring::const_iterator& cursor,
750 const std::wstring& str,
751 const variable_repository& var_repo)
753 std::vector<boost::any> tokens;
756 while (cursor != str.end())
758 wchar_t ch = next_non_whitespace(cursor, str, L"Expected expression");
772 tokens.push_back(parse_constant(cursor, str));
787 tokens.push_back(parse_operator(cursor, str));
790 tokens.push_back(parse_string_literal(cursor, str));
793 if (!tokens.empty() && is<std::wstring>(tokens.back()))
795 auto function_name = as<std::wstring>(tokens.back());
797 tokens.push_back(parse_function(function_name, cursor, str, var_repo));
800 tokens.push_back(parse_parenthesis(cursor, str, var_repo));
807 tokens.push_back(parse_variable(cursor, str, var_repo));
816 CASPAR_THROW_EXCEPTION(user_error()
817 << msg_info(L"Expected expression" + at_position(cursor, str)));
821 while (tokens.size() > 1)
823 resolve_operators(precedence++, tokens);
826 return as_binding(tokens.at(0));