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<int64_t>().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 parse_function(
151 const std::wstring& function_name,
152 std::wstring::const_iterator& cursor,
153 const std::wstring& str,
154 const variable_repository& var_repo)
156 static std::map<std::wstring, std::function<boost::any (const std::vector<boost::any>& params, const variable_repository& var_repo)>> FUNCTIONS
158 {L"animate", create_animate_function },
159 {L"sin", create_sin_function },
160 {L"cos", create_cos_function }
163 auto function = FUNCTIONS.find(function_name);
165 if (function == FUNCTIONS.end())
166 CASPAR_THROW_EXCEPTION(user_error()
167 << msg_info(function_name + L"() is an unknown function" + at_position(cursor, str)));
169 if (*cursor++ != L'(')
170 CASPAR_THROW_EXCEPTION(user_error()
171 << msg_info(L"Expected (" + at_position(cursor, str)));
173 std::vector<boost::any> params;
175 while (cursor != str.end())
177 params.push_back(parse_expression(cursor, str, var_repo));
179 auto next = next_non_whitespace(cursor, str, L"Expected , or )");
183 else if (next != L',')
184 CASPAR_THROW_EXCEPTION(user_error()
185 << msg_info(L"Expected ) or ," + at_position(cursor, str)));
190 if (next_non_whitespace(cursor, str, L"Expected , or )") != L')')
191 CASPAR_THROW_EXCEPTION(user_error()
192 << msg_info(L"Expected ) " + at_position(cursor, str)));
196 return function->second(params, var_repo);
199 double parse_constant(
200 std::wstring::const_iterator& cursor, const std::wstring& str)
202 std::wstring constant;
204 while (cursor != str.end())
206 wchar_t ch = *cursor;
208 if ((ch >= L'0' && ch <= L'9') || ch == L'.')
216 return boost::lexical_cast<double>(constant);
219 std::wstring parse_string_literal(
220 std::wstring::const_iterator& cursor, const std::wstring& str)
222 std::wstring literal;
224 if (*cursor++ != L'"')
225 CASPAR_THROW_EXCEPTION(user_error()
226 << msg_info(L"Expected (" + at_position(cursor, str)));
228 bool escaping = false;
230 while (cursor != str.end())
232 wchar_t ch = *cursor;
255 return std::move(literal);
273 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
274 L"Unexpected end of input (Expected closing \") in " + str));
277 boost::any parse_variable(
278 std::wstring::const_iterator& cursor,
279 const std::wstring& str,
280 const variable_repository& var_repo)
282 std::wstring variable_name;
284 while (cursor != str.end())
286 wchar_t ch = *cursor;
290 || (ch >= L'a' && ch <= L'z')
291 || (ch >= L'A' && ch <= L'Z')
292 || (variable_name.length() > 0 && ch >= L'0' && ch <= L'9'))
300 if (cursor != str.end() && *cursor == L'(')
301 return variable_name;
303 if (variable_name == L"true")
305 else if (variable_name == L"false")
308 variable& var = var_repo(variable_name);
310 if (var.is<double>())
311 return var.as<double>();
312 else if (var.is<int64_t>())
313 return var.as<int64_t>().as<double>();
314 else if (var.is<std::wstring>())
315 return var.as<std::wstring>();
316 else if (var.is<bool>())
317 return var.as<bool>();
319 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
320 L"Unhandled variable type of " + variable_name
321 + at_position(cursor, str)));
333 std::wstring characters;
337 op(wchar_t ch, int precedence, op_type type)
338 : characters(1, ch), precedence(precedence), type(type)
342 op(const std::wstring& chs, int precedence, op_type type)
343 : characters(chs), precedence(precedence), type(type)
348 op parse_operator(std::wstring::const_iterator& cursor, const std::wstring& str)
350 static const wchar_t NONE = L' ';
351 wchar_t first = NONE;
353 while (cursor != str.end())
355 wchar_t ch = *cursor;
361 return op(ch, 6, op::op_type::BINARY);
366 return op(ch, 5, op::op_type::BINARY);
370 return op(ch, 15, op::op_type::TERNARY);
373 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
374 L"Did not expect -" + at_position(cursor, str)));
382 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
383 L"Did not expect !" + at_position(cursor, str)));
391 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
392 L"Did not expect <" + at_position(cursor, str)));
400 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
401 L"Did not expect >" + at_position(cursor, str)));
411 return op(L"==", 9, op::op_type::BINARY);
413 else if (first == L'!')
416 return op(L"!=", 9, op::op_type::BINARY);
418 else if (first == L'>')
421 return op(L">=", 8, op::op_type::BINARY);
423 else if (first == L'<')
426 return op(L"<=", 8, op::op_type::BINARY);
428 else if (first == NONE)
434 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
435 L"Did not expect =" + at_position(cursor, str)));
442 return op(L"||", 14, op::op_type::BINARY);
444 else if (first == NONE)
450 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
451 L"Did not expect =" + at_position(cursor, str)));
458 return op(L"&&", 13, op::op_type::BINARY);
460 else if (first == NONE)
466 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
467 L"Did not expect =" + at_position(cursor, str)));
473 return op(L'-', 6, op::op_type::BINARY);
474 else if (first == L'!')
475 return op(L'!', 3, op::op_type::UNARY);
478 return op(L'<', 8, op::op_type::BINARY);
479 else if (first == L'>')
480 return op(L'>', 8, op::op_type::BINARY);
481 else if (first == L'-')
482 return op(L"unary-", 3, op::op_type::UNARY);
483 else if (first == L'!')
484 return op(L'!', 3, op::op_type::UNARY);
486 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
487 L"Expected second character of operator"
488 + at_position(cursor, str)));
492 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
493 L"Unexpected end of input (Expected operator) in " + str));
496 boost::any as_binding(const boost::any& value)
498 // Wrap supported constants as bindings
499 if (is<double>(value))
500 return binding<double>(as<double>(value));
501 else if (is<bool>(value))
502 return binding<bool>(as<bool>(value));
503 else if (is<std::wstring>(value))
504 return binding<std::wstring>(as<std::wstring>(value));
505 // Already one of the supported binding types
506 else if (is<binding<double>>(value))
507 return as<binding<double>>(value);
508 else if (is<binding<bool>>(value))
509 return as<binding<bool>>(value);
510 else if (is<binding<std::wstring>>(value))
511 return as<binding<std::wstring>>(value);
513 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
514 L"Couldn't detect type of " + u16(value.type().name())));
517 boost::any negative(const boost::any& to_create_negative_of)
519 return -require<double>(to_create_negative_of);
522 boost::any not_(const boost::any& to_create_not_of)
524 return !require<bool>(to_create_not_of);
527 boost::any multiply(const boost::any& lhs, boost::any& rhs)
529 return require<double>(lhs) * require<double>(rhs);
532 boost::any divide(const boost::any& lhs, boost::any& rhs)
534 return require<double>(lhs) / require<double>(rhs);
537 boost::any modulus(const boost::any& lhs, boost::any& rhs)
541 require<double>(lhs).as<int64_t>()
542 % require<double>(rhs).as<int64_t>()
546 binding<std::wstring> stringify(const boost::any& value)
548 auto b = as_binding(value);
550 if (is<binding<std::wstring>>(b))
551 return as<binding<std::wstring>>(b);
552 else if (is<binding<double>>(b))
553 return as<binding<double>>(b).as<std::wstring>();
554 else if (is<binding<bool>>(b))
555 return as<binding<bool>>(b).as<std::wstring>();
557 CASPAR_THROW_EXCEPTION(user_error()
558 << msg_info(L"Couldn't stringify " + u16(value.type().name())));
561 boost::any add(const boost::any& lhs, boost::any& rhs)
563 auto l = as_binding(lhs);
564 auto r = as_binding(rhs);
567 if (is<binding<double>>(l) && is<binding<double>>(r))
568 return as<binding<double>>(l) + as<binding<double>>(r);
570 else if (is<binding<std::wstring>>(l) && is<binding<std::wstring>>(r))
571 return as<binding<std::wstring>>(l) + as<binding<std::wstring>>(r);
572 // mixed types to string and concatenated
574 return stringify(lhs) + stringify(rhs);
577 boost::any subtract(const boost::any& lhs, boost::any& rhs)
579 return require<double>(lhs) - require<double>(rhs);
582 boost::any less(const boost::any& lhs, boost::any& rhs)
584 return require<double>(lhs) < require<double>(rhs);
587 boost::any less_or_equal(const boost::any& lhs, boost::any& rhs)
589 return require<double>(lhs) <= require<double>(rhs);
592 boost::any greater(const boost::any& lhs, boost::any& rhs)
594 return require<double>(lhs) > require<double>(rhs);
597 boost::any greater_or_equal(const boost::any& lhs, boost::any& rhs)
599 return require<double>(lhs) >= require<double>(rhs);
602 boost::any equal(const boost::any& lhs, boost::any& rhs)
604 auto l = as_binding(lhs);
605 auto r = as_binding(rhs);
608 if (is<binding<double>>(l) && is<binding<double>>(r))
609 return as<binding<double>>(l) == as<binding<double>>(r);
611 else if (is<binding<std::wstring>>(l) && is<binding<std::wstring>>(r))
612 return as<binding<std::wstring>>(l) == as<binding<std::wstring>>(r);
615 return require<bool>(l) == require<bool>(r);
618 boost::any and_(const boost::any& lhs, boost::any& rhs)
620 return require<bool>(lhs) && require<bool>(rhs);
623 boost::any or_(const boost::any& lhs, boost::any& rhs)
625 return require<bool>(lhs) || require<bool>(rhs);
630 const binding<bool>& condition,
631 const binding<T>& true_value,
632 const binding<T>& false_value)
634 return when(condition).then(true_value).otherwise(false_value);
638 const boost::any& condition,
639 const boost::any& true_value,
640 const boost::any& false_value)
642 auto cond = require<bool>(condition);
643 auto t = as_binding(true_value);
644 auto f = as_binding(false_value);
647 if (is<binding<double>>(t) && is<binding<double>>(f))
648 return ternary(cond, as<binding<double>>(t), as<binding<double>>(f));
650 else if (is<binding<std::wstring>>(t) && is<binding<std::wstring>>(f))
653 as<binding<std::wstring>>(t),
654 as<binding<std::wstring>>(f));
657 return ternary(cond, require<bool>(t), require<bool>(f));
660 void resolve_operators(int precedence, std::vector<boost::any>& tokens)
662 for (int i = 0; i < tokens.size(); ++i)
664 auto& token = tokens.at(i);
669 auto op_token = as<op>(token);
671 if (op_token.precedence != precedence)
674 int index_after = i + 1;
675 auto& token_after = tokens.at(index_after);
677 switch (op_token.type)
679 case op::op_type::UNARY:
680 if (op_token.characters == L"unary-")
682 tokens.at(i) = negative(token_after);
684 else if (op_token.characters == L"!")
686 tokens.at(i) = not_(token_after);
689 tokens.erase(tokens.begin() + index_after);
692 case op::op_type::BINARY:
694 auto& token_before = tokens.at(i - 1);
696 if (op_token.characters == L"*")
697 token_before = multiply(token_before, token_after);
698 else if (op_token.characters == L"/")
699 token_before = divide(token_before, token_after);
700 else if (op_token.characters == L"%")
701 token_before = modulus(token_before, token_after);
702 else if (op_token.characters == L"+")
703 token_before = add(token_before, token_after);
704 else if (op_token.characters == L"-")
705 token_before = subtract(token_before, token_after);
706 else if (op_token.characters == L"<")
707 token_before = less(token_before, token_after);
708 else if (op_token.characters == L"<=")
709 token_before = less_or_equal(token_before, token_after);
710 else if (op_token.characters == L">")
711 token_before = greater(token_before, token_after);
712 else if (op_token.characters == L">=")
713 token_before = greater_or_equal(token_before, token_after);
714 else if (op_token.characters == L"==")
715 token_before = equal(token_before, token_after);
716 else if (op_token.characters == L"!=")
717 token_before = not_(equal(token_before, token_after));
718 else if (op_token.characters == L"&&")
719 token_before = and_(token_before, token_after);
720 else if (op_token.characters == L"||")
721 token_before = or_(token_before, token_after);
724 tokens.erase(tokens.begin() + i, tokens.begin() + i + 2);
728 case op::op_type::TERNARY:
729 if (op_token.characters == L"?")
731 auto& token_before = tokens.at(i - 1);
732 auto& token_colon_operator = tokens.at(i + 2);
734 if (as<op>(token_colon_operator).characters != L":")
735 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
736 L"Expected : as part of ternary expression"));
738 auto& token_false_value = tokens.at(i + 3);
739 token_before = ternary(
740 token_before, token_after, token_false_value);
741 tokens.erase(tokens.begin() + i, tokens.begin() + i + 4);
750 boost::any parse_expression(
751 std::wstring::const_iterator& cursor,
752 const std::wstring& str,
753 const variable_repository& var_repo)
755 std::vector<boost::any> tokens;
758 while (cursor != str.end())
760 wchar_t ch = next_non_whitespace(cursor, str, L"Expected expression");
774 tokens.push_back(parse_constant(cursor, str));
789 tokens.push_back(parse_operator(cursor, str));
792 tokens.push_back(parse_string_literal(cursor, str));
795 if (!tokens.empty() && is<std::wstring>(tokens.back()))
797 auto function_name = as<std::wstring>(tokens.back());
799 tokens.push_back(parse_function(function_name, cursor, str, var_repo));
802 tokens.push_back(parse_parenthesis(cursor, str, var_repo));
809 tokens.push_back(parse_variable(cursor, str, var_repo));
818 CASPAR_THROW_EXCEPTION(user_error()
819 << msg_info(L"Expected expression" + at_position(cursor, str)));
823 while (tokens.size() > 1)
825 resolve_operators(precedence++, tokens);
828 return as_binding(tokens.at(0));