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)
74 boost::any as_binding(const boost::any& value);
77 binding<T> require(const boost::any& value)
79 auto b = as_binding(value);
81 if (is<binding<T>>(b))
82 return as<binding<T>>(b);
84 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
85 L"Required binding of type " + u16(typeid(T).name())
86 + L" but got " + u16(value.type().name())));
89 boost::any parse_expression(
90 std::wstring::const_iterator& cursor,
91 const std::wstring& str,
92 const variable_repository& var_repo);
94 boost::any parse_parenthesis(
95 std::wstring::const_iterator& cursor,
96 const std::wstring& str,
97 const variable_repository& var_repo)
99 if (*cursor++ != L'(')
100 CASPAR_THROW_EXCEPTION(user_error()
101 << msg_info(L"Expected (" + at_position(cursor, str)));
103 auto expr = parse_expression(cursor, str, var_repo);
105 if (next_non_whitespace(cursor, str, L"Expected )") != L')')
106 CASPAR_THROW_EXCEPTION(user_error()
107 << msg_info(L"Expected )" + at_position(cursor, str)));
114 boost::any create_animate_function(const std::vector<boost::any>& params, const variable_repository& var_repo)
116 if (params.size() != 3)
117 CASPAR_THROW_EXCEPTION(user_error()
118 << msg_info(L"animate() function requires three parameters: to_animate, duration, tweener"));
120 auto to_animate = require<double>(params.at(0));
121 auto frame_counter = var_repo(L"frame").as<double>();
122 auto duration = require<double>(params.at(1));
123 auto tw = require<std::wstring>(params.at(2)).transformed([](const std::wstring& s) { return tweener(s); });
125 return to_animate.animated(frame_counter, duration, tw);
128 boost::any create_sin_function(const std::vector<boost::any>& params, const variable_repository& var_repo)
130 if (params.size() != 1)
131 CASPAR_THROW_EXCEPTION(user_error()
132 << msg_info(L"sin() function requires one parameters: angle"));
134 auto angle = require<double>(params.at(0));
136 return angle.transformed([](double a) { return std::sin(a); });
139 boost::any create_cos_function(const std::vector<boost::any>& params, const variable_repository& var_repo)
141 if (params.size() != 1)
142 CASPAR_THROW_EXCEPTION(user_error()
143 << msg_info(L"cos() function requires one parameters: angle"));
145 auto angle = require<double>(params.at(0));
147 return angle.transformed([](double a) { return std::cos(a); });
150 boost::any create_abs_function(const std::vector<boost::any>& params, const variable_repository& var_repo)
152 if (params.size() != 1)
153 CASPAR_THROW_EXCEPTION(user_error()
154 << msg_info(L"abs() function requires one parameters: value"));
156 auto val = require<double>(params.at(0));
158 return val.transformed([](double v) { return std::abs(v); });
161 boost::any parse_function(
162 const std::wstring& function_name,
163 std::wstring::const_iterator& cursor,
164 const std::wstring& str,
165 const variable_repository& var_repo)
167 static std::map<std::wstring, std::function<boost::any (const std::vector<boost::any>& params, const variable_repository& var_repo)>> FUNCTIONS
169 {L"animate", create_animate_function },
170 {L"sin", create_sin_function },
171 {L"cos", create_cos_function },
172 {L"abs", create_abs_function }
175 auto function = FUNCTIONS.find(function_name);
177 if (function == FUNCTIONS.end())
178 CASPAR_THROW_EXCEPTION(user_error()
179 << msg_info(function_name + L"() is an unknown function" + at_position(cursor, str)));
181 if (*cursor++ != L'(')
182 CASPAR_THROW_EXCEPTION(user_error()
183 << msg_info(L"Expected (" + at_position(cursor, str)));
185 std::vector<boost::any> params;
187 while (cursor != str.end())
189 params.push_back(parse_expression(cursor, str, var_repo));
191 auto next = next_non_whitespace(cursor, str, L"Expected , or )");
195 else if (next != L',')
196 CASPAR_THROW_EXCEPTION(user_error()
197 << msg_info(L"Expected ) or ," + at_position(cursor, str)));
202 if (next_non_whitespace(cursor, str, L"Expected , or )") != L')')
203 CASPAR_THROW_EXCEPTION(user_error()
204 << msg_info(L"Expected ) " + at_position(cursor, str)));
208 return function->second(params, var_repo);
211 double parse_constant(
212 std::wstring::const_iterator& cursor, const std::wstring& str)
214 std::wstring constant;
216 while (cursor != str.end())
218 wchar_t ch = *cursor;
220 if ((ch >= L'0' && ch <= L'9') || ch == L'.')
228 return boost::lexical_cast<double>(constant);
231 std::wstring parse_string_literal(
232 std::wstring::const_iterator& cursor, const std::wstring& str)
234 std::wstring literal;
236 if (*cursor++ != L'"')
237 CASPAR_THROW_EXCEPTION(user_error()
238 << msg_info(L"Expected (" + at_position(cursor, str)));
240 bool escaping = false;
242 while (cursor != str.end())
244 wchar_t ch = *cursor;
267 return std::move(literal);
285 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
286 L"Unexpected end of input (Expected closing \") in " + str));
289 boost::any parse_variable(
290 std::wstring::const_iterator& cursor,
291 const std::wstring& str,
292 const variable_repository& var_repo)
294 std::wstring variable_name;
296 while (cursor != str.end())
298 wchar_t ch = *cursor;
302 || (ch >= L'a' && ch <= L'z')
303 || (ch >= L'A' && ch <= L'Z')
304 || (variable_name.length() > 0 && ch >= L'0' && ch <= L'9'))
312 if (cursor != str.end() && *cursor == L'(')
313 return variable_name;
315 if (variable_name == L"true")
317 else if (variable_name == L"false")
320 variable& var = var_repo(variable_name);
322 if (var.is<double>())
323 return var.as<double>();
324 else if (var.is<int64_t>())
325 return var.as<int64_t>().as<double>();
326 else if (var.is<std::wstring>())
327 return var.as<std::wstring>();
328 else if (var.is<bool>())
329 return var.as<bool>();
331 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
332 L"Unhandled variable type of " + variable_name
333 + at_position(cursor, str)));
345 std::wstring characters;
349 op(wchar_t ch, int precedence, op_type type)
350 : characters(1, ch), precedence(precedence), type(type)
354 op(const std::wstring& chs, int precedence, op_type type)
355 : characters(chs), precedence(precedence), type(type)
360 op parse_operator(std::wstring::const_iterator& cursor, const std::wstring& str)
362 static const wchar_t NONE = L' ';
363 wchar_t first = NONE;
365 while (cursor != str.end())
367 wchar_t ch = *cursor;
373 return op(ch, 6, op::op_type::BINARY);
378 return op(ch, 5, op::op_type::BINARY);
382 return op(ch, 15, op::op_type::TERNARY);
385 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
386 L"Did not expect -" + at_position(cursor, str)));
394 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
395 L"Did not expect !" + at_position(cursor, str)));
403 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
404 L"Did not expect <" + at_position(cursor, str)));
412 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
413 L"Did not expect >" + at_position(cursor, str)));
423 return op(L"==", 9, op::op_type::BINARY);
425 else if (first == L'!')
428 return op(L"!=", 9, op::op_type::BINARY);
430 else if (first == L'>')
433 return op(L">=", 8, op::op_type::BINARY);
435 else if (first == L'<')
438 return op(L"<=", 8, op::op_type::BINARY);
440 else if (first == NONE)
446 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
447 L"Did not expect =" + at_position(cursor, str)));
454 return op(L"||", 14, op::op_type::BINARY);
456 else if (first == NONE)
462 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
463 L"Did not expect =" + at_position(cursor, str)));
470 return op(L"&&", 13, op::op_type::BINARY);
472 else if (first == NONE)
478 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
479 L"Did not expect =" + at_position(cursor, str)));
485 return op(L'-', 6, op::op_type::BINARY);
486 else if (first == L'!')
487 return op(L'!', 3, op::op_type::UNARY);
490 return op(L'<', 8, op::op_type::BINARY);
491 else if (first == L'>')
492 return op(L'>', 8, op::op_type::BINARY);
493 else if (first == L'-')
494 return op(L"unary-", 3, op::op_type::UNARY);
495 else if (first == L'!')
496 return op(L'!', 3, op::op_type::UNARY);
498 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
499 L"Expected second character of operator"
500 + at_position(cursor, str)));
504 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
505 L"Unexpected end of input (Expected operator) in " + str));
508 boost::any as_binding(const boost::any& value)
510 // Wrap supported constants as bindings
511 if (is<double>(value))
512 return binding<double>(as<double>(value));
513 else if (is<bool>(value))
514 return binding<bool>(as<bool>(value));
515 else if (is<std::wstring>(value))
516 return binding<std::wstring>(as<std::wstring>(value));
517 // Already one of the supported binding types
518 else if (is<binding<double>>(value))
519 return as<binding<double>>(value);
520 else if (is<binding<bool>>(value))
521 return as<binding<bool>>(value);
522 else if (is<binding<std::wstring>>(value))
523 return as<binding<std::wstring>>(value);
525 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
526 L"Couldn't detect type of " + u16(value.type().name())));
529 boost::any negative(const boost::any& to_create_negative_of)
531 return -require<double>(to_create_negative_of);
534 boost::any not_(const boost::any& to_create_not_of)
536 return !require<bool>(to_create_not_of);
539 boost::any multiply(const boost::any& lhs, boost::any& rhs)
541 return require<double>(lhs) * require<double>(rhs);
544 boost::any divide(const boost::any& lhs, boost::any& rhs)
546 return require<double>(lhs) / require<double>(rhs);
549 boost::any modulus(const boost::any& lhs, boost::any& rhs)
553 require<double>(lhs).as<int64_t>()
554 % require<double>(rhs).as<int64_t>()
558 binding<std::wstring> stringify(const boost::any& value)
560 auto b = as_binding(value);
562 if (is<binding<std::wstring>>(b))
563 return as<binding<std::wstring>>(b);
564 else if (is<binding<double>>(b))
565 return as<binding<double>>(b).as<std::wstring>();
566 else if (is<binding<bool>>(b))
567 return as<binding<bool>>(b).as<std::wstring>();
569 CASPAR_THROW_EXCEPTION(user_error()
570 << msg_info(L"Couldn't stringify " + u16(value.type().name())));
573 boost::any add(const boost::any& lhs, boost::any& rhs)
575 auto l = as_binding(lhs);
576 auto r = as_binding(rhs);
579 if (is<binding<double>>(l) && is<binding<double>>(r))
580 return as<binding<double>>(l) + as<binding<double>>(r);
582 else if (is<binding<std::wstring>>(l) && is<binding<std::wstring>>(r))
583 return as<binding<std::wstring>>(l) + as<binding<std::wstring>>(r);
584 // mixed types to string and concatenated
586 return stringify(lhs) + stringify(rhs);
589 boost::any subtract(const boost::any& lhs, boost::any& rhs)
591 return require<double>(lhs) - require<double>(rhs);
594 boost::any less(const boost::any& lhs, boost::any& rhs)
596 return require<double>(lhs) < require<double>(rhs);
599 boost::any less_or_equal(const boost::any& lhs, boost::any& rhs)
601 return require<double>(lhs) <= require<double>(rhs);
604 boost::any greater(const boost::any& lhs, boost::any& rhs)
606 return require<double>(lhs) > require<double>(rhs);
609 boost::any greater_or_equal(const boost::any& lhs, boost::any& rhs)
611 return require<double>(lhs) >= require<double>(rhs);
614 boost::any equal(const boost::any& lhs, boost::any& rhs)
616 auto l = as_binding(lhs);
617 auto r = as_binding(rhs);
620 if (is<binding<double>>(l) && is<binding<double>>(r))
621 return as<binding<double>>(l) == as<binding<double>>(r);
623 else if (is<binding<std::wstring>>(l) && is<binding<std::wstring>>(r))
624 return as<binding<std::wstring>>(l) == as<binding<std::wstring>>(r);
627 return require<bool>(l) == require<bool>(r);
630 boost::any and_(const boost::any& lhs, boost::any& rhs)
632 return require<bool>(lhs) && require<bool>(rhs);
635 boost::any or_(const boost::any& lhs, boost::any& rhs)
637 return require<bool>(lhs) || require<bool>(rhs);
642 const binding<bool>& condition,
643 const binding<T>& true_value,
644 const binding<T>& false_value)
646 return when(condition).then(true_value).otherwise(false_value);
650 const boost::any& condition,
651 const boost::any& true_value,
652 const boost::any& false_value)
654 auto cond = require<bool>(condition);
655 auto t = as_binding(true_value);
656 auto f = as_binding(false_value);
659 if (is<binding<double>>(t) && is<binding<double>>(f))
660 return ternary(cond, as<binding<double>>(t), as<binding<double>>(f));
662 else if (is<binding<std::wstring>>(t) && is<binding<std::wstring>>(f))
665 as<binding<std::wstring>>(t),
666 as<binding<std::wstring>>(f));
669 return ternary(cond, require<bool>(t), require<bool>(f));
672 void resolve_operators(int precedence, std::vector<boost::any>& tokens)
674 for (int i = 0; i < tokens.size(); ++i)
676 auto& token = tokens.at(i);
681 auto op_token = as<op>(token);
683 if (op_token.precedence != precedence)
686 int index_after = i + 1;
687 auto& token_after = tokens.at(index_after);
689 switch (op_token.type)
691 case op::op_type::UNARY:
692 if (op_token.characters == L"unary-")
694 tokens.at(i) = negative(token_after);
696 else if (op_token.characters == L"!")
698 tokens.at(i) = not_(token_after);
701 tokens.erase(tokens.begin() + index_after);
704 case op::op_type::BINARY:
706 auto& token_before = tokens.at(i - 1);
708 if (op_token.characters == L"*")
709 token_before = multiply(token_before, token_after);
710 else if (op_token.characters == L"/")
711 token_before = divide(token_before, token_after);
712 else if (op_token.characters == L"%")
713 token_before = modulus(token_before, token_after);
714 else if (op_token.characters == L"+")
715 token_before = add(token_before, token_after);
716 else if (op_token.characters == L"-")
717 token_before = subtract(token_before, token_after);
718 else if (op_token.characters == L"<")
719 token_before = less(token_before, token_after);
720 else if (op_token.characters == L"<=")
721 token_before = less_or_equal(token_before, token_after);
722 else if (op_token.characters == L">")
723 token_before = greater(token_before, token_after);
724 else if (op_token.characters == L">=")
725 token_before = greater_or_equal(token_before, token_after);
726 else if (op_token.characters == L"==")
727 token_before = equal(token_before, token_after);
728 else if (op_token.characters == L"!=")
729 token_before = not_(equal(token_before, token_after));
730 else if (op_token.characters == L"&&")
731 token_before = and_(token_before, token_after);
732 else if (op_token.characters == L"||")
733 token_before = or_(token_before, token_after);
736 tokens.erase(tokens.begin() + i, tokens.begin() + i + 2);
740 case op::op_type::TERNARY:
741 if (op_token.characters == L"?")
743 auto& token_before = tokens.at(i - 1);
744 auto& token_colon_operator = tokens.at(i + 2);
746 if (as<op>(token_colon_operator).characters != L":")
747 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
748 L"Expected : as part of ternary expression"));
750 auto& token_false_value = tokens.at(i + 3);
751 token_before = ternary(
752 token_before, token_after, token_false_value);
753 tokens.erase(tokens.begin() + i, tokens.begin() + i + 4);
762 boost::any parse_expression(
763 std::wstring::const_iterator& cursor,
764 const std::wstring& str,
765 const variable_repository& var_repo)
767 std::vector<boost::any> tokens;
770 while (cursor != str.end())
772 wchar_t ch = next_non_whitespace(cursor, str, L"Expected expression");
786 tokens.push_back(parse_constant(cursor, str));
801 tokens.push_back(parse_operator(cursor, str));
804 tokens.push_back(parse_string_literal(cursor, str));
807 if (!tokens.empty() && is<std::wstring>(tokens.back()))
809 auto function_name = as<std::wstring>(tokens.back());
811 tokens.push_back(parse_function(function_name, cursor, str, var_repo));
814 tokens.push_back(parse_parenthesis(cursor, str, var_repo));
821 tokens.push_back(parse_variable(cursor, str, var_repo));
830 CASPAR_THROW_EXCEPTION(user_error()
831 << msg_info(L"Expected expression" + at_position(cursor, str)));
835 while (tokens.size() > 1)
837 resolve_operators(precedence++, tokens);
840 return as_binding(tokens.at(0));