2013-10-20 15:06:05 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-10-20 15:06:05 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-10-16 17:44:06 +02:00
|
|
|
#include "config/expression.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/configitem.hpp"
|
|
|
|
#include "config/configitembuilder.hpp"
|
|
|
|
#include "config/applyrule.hpp"
|
|
|
|
#include "config/objectrule.hpp"
|
|
|
|
#include "base/array.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/scriptfunction.hpp"
|
|
|
|
#include "base/scriptvariable.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/object.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-10-01 17:01:47 +02:00
|
|
|
#include "base/configerror.hpp"
|
2014-03-18 15:29:04 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2014-03-22 08:40:09 +01:00
|
|
|
#include <boost/exception_ptr.hpp>
|
2014-11-06 19:35:47 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-03-22 08:40:09 +01:00
|
|
|
#include <boost/exception/errinfo_nested_exception.hpp>
|
2013-10-20 15:06:05 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Expression::~Expression(void)
|
2014-03-22 10:29:45 +01:00
|
|
|
{ }
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
Value Expression::Evaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2014-03-22 10:29:45 +01:00
|
|
|
try {
|
2014-03-29 13:48:04 +01:00
|
|
|
#ifdef _DEBUG
|
2014-11-09 19:48:28 +01:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
ShowCodeFragment(msgbuf, GetDebugInfo(), false);
|
|
|
|
Log(LogDebug, "Expression")
|
|
|
|
<< "Executing:\n" << msgbuf.str();
|
2014-03-29 13:48:04 +01:00
|
|
|
#endif /* _DEBUG */
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
return DoEvaluate(context, dhint);
|
2014-03-22 10:29:45 +01:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
if (boost::get_error_info<boost::errinfo_nested_exception>(ex))
|
|
|
|
throw;
|
|
|
|
else
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Error while evaluating expression: " + String(ex.what()))
|
|
|
|
<< boost::errinfo_nested_exception(boost::current_exception())
|
|
|
|
<< errinfo_debuginfo(GetDebugInfo()));
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
const DebugInfo& Expression::GetDebugInfo(void) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
static DebugInfo debugInfo;
|
|
|
|
return debugInfo;
|
2014-03-23 19:58:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
std::vector<Expression *> icinga::MakeIndexer(const String& index1)
|
2014-03-23 19:58:24 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
std::vector<Expression *> result;
|
|
|
|
result.push_back(MakeLiteral(index1));
|
|
|
|
return result;
|
2014-03-23 19:58:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
void DictExpression::MakeInline(void)
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
m_Inline = true;
|
2013-10-20 15:06:05 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
LiteralExpression::LiteralExpression(const Value& value)
|
|
|
|
: m_Value(value)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Value LiteralExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Value;
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
const DebugInfo& DebuggableExpression::GetDebugInfo(void) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_DebugInfo;
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value VariableExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-06 19:35:47 +01:00
|
|
|
Object::Ptr scope = context;
|
2014-03-24 09:10:37 +01:00
|
|
|
|
|
|
|
while (scope) {
|
2014-11-09 19:48:28 +01:00
|
|
|
if (HasField(scope, m_Variable))
|
|
|
|
return GetField(scope, m_Variable);
|
2014-03-24 09:10:37 +01:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
scope = GetField(scope, "__parent");
|
2014-03-24 09:10:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
return ScriptVariable::Get(m_Variable);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value NegateExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return ~(long)m_Operand->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value LogicalNegateExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-28 13:25:40 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return !m_Operand->Evaluate(context).ToBool();
|
2014-03-28 13:25:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value AddExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) + m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value SubtractExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) - m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value MultiplyExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) * m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value DivideExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) / m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value BinaryAndExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) & m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value BinaryOrExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) | m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value ShiftLeftExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) << m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value ShiftRightExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) >> m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value EqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) == m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value NotEqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) != m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value LessThanExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) < m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value GreaterThanExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) > m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value LessThanOrEqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) <= m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value GreaterThanOrEqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context) >= m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value InExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2013-10-20 15:06:05 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
Value right = m_Operand2->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
|
2014-03-28 12:17:22 +01:00
|
|
|
if (right.IsEmpty())
|
|
|
|
return false;
|
|
|
|
else if (!right.IsObjectType<Array>())
|
2014-10-26 19:59:49 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Invalid right side argument for 'in' operator: " + JsonEncode(right)));
|
2014-03-22 10:29:45 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value left = m_Operand1->Evaluate(context);
|
2014-03-22 10:29:45 +01:00
|
|
|
|
|
|
|
Array::Ptr arr = right;
|
2014-11-15 08:22:09 +01:00
|
|
|
return arr->Contains(left);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value NotInExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
Value right = m_Operand2->Evaluate(context);
|
|
|
|
|
|
|
|
if (right.IsEmpty())
|
|
|
|
return false;
|
|
|
|
else if (!right.IsObjectType<Array>())
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Invalid right side argument for 'in' operator: " + JsonEncode(right)));
|
|
|
|
|
|
|
|
Value left = m_Operand1->Evaluate(context);
|
|
|
|
|
|
|
|
Array::Ptr arr = right;
|
2014-11-15 08:22:09 +01:00
|
|
|
return !arr->Contains(left);
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value LogicalAndExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context).ToBool() && m_Operand2->Evaluate(context).ToBool();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value LogicalOrExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Operand1->Evaluate(context).ToBool() || m_Operand2->Evaluate(context).ToBool();
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value FunctionCallExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
Value funcName = m_FName->Evaluate(context);
|
2014-04-02 09:04:17 +02:00
|
|
|
|
|
|
|
ScriptFunction::Ptr func;
|
|
|
|
|
|
|
|
if (funcName.IsObjectType<ScriptFunction>())
|
|
|
|
func = funcName;
|
|
|
|
else
|
|
|
|
func = ScriptFunction::GetByName(funcName);
|
2014-03-22 10:29:45 +01:00
|
|
|
|
|
|
|
if (!func)
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
|
|
|
|
|
2014-03-19 10:51:09 +01:00
|
|
|
std::vector<Value> arguments;
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_FOREACH(Expression *arg, m_Args) {
|
|
|
|
arguments.push_back(arg->Evaluate(context));
|
2014-03-22 10:29:45 +01:00
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
|
2014-03-22 10:29:45 +01:00
|
|
|
return func->Invoke(arguments);
|
|
|
|
}
|
2013-10-20 15:06:05 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value ArrayExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-22 10:29:45 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr result = new Array();
|
2014-03-18 15:29:04 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
|
|
|
|
result->Add(aexpr->Evaluate(context));
|
2013-10-20 15:06:05 +02:00
|
|
|
}
|
2014-03-22 10:29:45 +01:00
|
|
|
|
|
|
|
return result;
|
2013-11-03 10:41:30 +01:00
|
|
|
}
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value DictExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary();
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
result->Set("__parent", context);
|
2014-03-24 09:10:37 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
|
|
|
|
Object::Ptr acontext = m_Inline ? context : result;
|
|
|
|
aexpr->Evaluate(acontext, dhint);
|
2014-04-09 10:36:57 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (HasField(acontext, "__result"))
|
|
|
|
break;
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
|
|
|
|
2014-05-10 12:43:16 +02:00
|
|
|
Dictionary::Ptr xresult = result->ShallowClone();
|
|
|
|
xresult->Remove("__parent");
|
|
|
|
return xresult;
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value SetExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-23 11:27:40 +01:00
|
|
|
{
|
2014-11-04 15:19:33 +01:00
|
|
|
DebugHint *sdhint = dhint;
|
2014-08-16 22:12:40 +02:00
|
|
|
|
2014-11-04 15:19:33 +01:00
|
|
|
Value parent, object;
|
|
|
|
String index;
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
for (Array::SizeType i = 0; i < m_Indexer.size(); i++) {
|
|
|
|
Expression *indexExpr = m_Indexer[i];
|
2014-11-06 19:35:47 +01:00
|
|
|
String tempindex = indexExpr->Evaluate(context, dhint);
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (i == m_Indexer.size() - 1)
|
2014-11-04 15:19:33 +01:00
|
|
|
index = tempindex;
|
2014-03-23 17:26:31 +01:00
|
|
|
|
2014-11-04 15:19:33 +01:00
|
|
|
if (i == 0) {
|
2014-11-06 19:35:47 +01:00
|
|
|
parent = context;
|
|
|
|
object = GetField(context, tempindex);
|
2014-11-04 15:19:33 +01:00
|
|
|
} else {
|
|
|
|
parent = object;
|
2014-03-23 17:26:31 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
LiteralExpression *eparent = MakeLiteral(parent);
|
|
|
|
LiteralExpression *eindex = MakeLiteral(tempindex);
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
IndexerExpression eip(eparent, eindex, m_DebugInfo);
|
|
|
|
object = eip.Evaluate(context, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
}
|
2014-03-23 19:58:24 +01:00
|
|
|
|
2014-11-04 15:19:33 +01:00
|
|
|
if (sdhint)
|
|
|
|
sdhint = sdhint->GetChild(index);
|
2014-08-16 22:12:40 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (i != m_Indexer.size() - 1 && object.IsEmpty()) {
|
2014-11-08 21:17:16 +01:00
|
|
|
object = new Dictionary();
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
SetField(parent, tempindex, object);
|
2014-11-04 15:19:33 +01:00
|
|
|
}
|
2014-03-24 11:34:41 +01:00
|
|
|
}
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value right = m_Operand2->Evaluate(context, dhint);
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (m_Op != OpSetLiteral) {
|
|
|
|
Expression *lhs = MakeLiteral(object);
|
|
|
|
Expression *rhs = MakeLiteral(right);
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
switch (m_Op) {
|
2014-11-04 15:19:33 +01:00
|
|
|
case OpSetAdd:
|
2014-11-09 19:48:28 +01:00
|
|
|
right = AddExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetSubtract:
|
2014-11-09 19:48:28 +01:00
|
|
|
right = SubtractExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetMultiply:
|
2014-11-09 19:48:28 +01:00
|
|
|
right = MultiplyExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
case OpSetDivide:
|
2014-11-09 19:48:28 +01:00
|
|
|
right = DivideExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
|
2014-11-04 15:19:33 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid opcode.");
|
|
|
|
}
|
2014-03-24 11:34:41 +01:00
|
|
|
}
|
2014-03-24 09:44:18 +01:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
SetField(parent, index, right);
|
2014-08-16 22:12:40 +02:00
|
|
|
|
|
|
|
if (sdhint)
|
2014-11-09 19:48:28 +01:00
|
|
|
sdhint->AddMessage("=", m_DebugInfo);
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-04 15:19:33 +01:00
|
|
|
return right;
|
2014-03-23 11:27:40 +01:00
|
|
|
}
|
2014-03-23 17:26:31 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value IndexerExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-23 17:26:31 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
Value value = m_Operand1->Evaluate(context);
|
|
|
|
Value index = m_Operand2->Evaluate(context);
|
2014-03-28 12:17:22 +01:00
|
|
|
|
|
|
|
if (value.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr dict = value;
|
|
|
|
return dict->Get(index);
|
2014-05-10 10:46:49 +02:00
|
|
|
} else if (value.IsObjectType<Array>()) {
|
|
|
|
Array::Ptr arr = value;
|
|
|
|
return arr->Get(index);
|
2014-11-10 20:06:28 +01:00
|
|
|
} else if (value.IsObject()) {
|
2014-03-28 12:17:22 +01:00
|
|
|
Object::Ptr object = value;
|
2014-11-03 00:44:04 +01:00
|
|
|
Type::Ptr type = object->GetReflectionType();
|
2014-03-28 12:17:22 +01:00
|
|
|
|
|
|
|
if (!type)
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Dot operator applied to object which does not support reflection"));
|
|
|
|
|
|
|
|
int field = type->GetFieldId(index);
|
|
|
|
|
|
|
|
if (field == -1)
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Tried to access invalid property '" + index + "'"));
|
|
|
|
|
|
|
|
return object->GetField(field);
|
2014-03-28 16:32:15 +01:00
|
|
|
} else if (value.IsEmpty()) {
|
|
|
|
return Empty;
|
2014-03-28 12:17:22 +01:00
|
|
|
} else {
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Dot operator cannot be applied to type '" + value.GetTypeName() + "'"));
|
|
|
|
}
|
2014-03-23 17:26:31 +01:00
|
|
|
}
|
2014-03-27 12:30:24 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value ImportExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-27 12:30:24 +01:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
Value type = m_Type->Evaluate(context);
|
|
|
|
Value name = m_Name->Evaluate(context);
|
2014-03-27 12:30:24 +01:00
|
|
|
|
|
|
|
ConfigItem::Ptr item = ConfigItem::GetObject(type, name);
|
|
|
|
|
|
|
|
if (!item)
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Import references unknown template: '" + name + "'"));
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
item->GetExpression()->Evaluate(context, dhint);
|
2014-03-27 12:30:24 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value Expression::FunctionWrapper(const std::vector<Value>& arguments,
|
|
|
|
const std::vector<String>& funcargs, const boost::shared_ptr<Expression>& expr, const Object::Ptr& scope)
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
if (arguments.size() < funcargs.size())
|
2014-03-30 15:04:53 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Too few arguments for function"));
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr context = new Dictionary();
|
2014-11-06 19:35:47 +01:00
|
|
|
context->Set("__parent", scope);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
|
|
|
|
context->Set(funcargs[i], arguments[i]);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
expr->Evaluate(context);
|
|
|
|
return context->Get("__result");
|
2014-03-30 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value FunctionExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
ScriptFunction::Ptr func = new ScriptFunction(boost::bind(&Expression::FunctionWrapper, _1, m_Args, m_Expression, context));
|
2014-04-02 09:04:17 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (!m_Name.IsEmpty())
|
|
|
|
ScriptFunction::Register(m_Name, func);
|
2014-04-02 09:04:17 +02:00
|
|
|
|
|
|
|
return func;
|
2014-03-30 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value ApplyExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
String name = m_Name->Evaluate(context, dhint);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
ApplyRule::AddRule(m_Type, m_Target, name, m_Expression, m_Filter, m_FKVar, m_FVVar, m_FTerm, m_DebugInfo, context);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value ObjectExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-03-30 15:04:53 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
String name;
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (m_Name)
|
|
|
|
name = m_Name->Evaluate(context, dhint);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
ConfigItemBuilder::Ptr item = new ConfigItemBuilder(m_DebugInfo);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-04-05 22:53:44 +02:00
|
|
|
String checkName = name;
|
2014-03-30 15:04:53 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (!m_Abstract) {
|
|
|
|
Type::Ptr ptype = Type::GetByName(m_Type);
|
2014-11-08 21:17:16 +01:00
|
|
|
|
|
|
|
NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
|
2014-04-05 22:53:44 +02:00
|
|
|
|
2014-04-06 08:09:49 +02:00
|
|
|
if (nc)
|
|
|
|
checkName = nc->MakeName(name, Dictionary::Ptr());
|
2014-04-05 22:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkName.IsEmpty()) {
|
2014-11-09 19:48:28 +01:00
|
|
|
ConfigItem::Ptr oldItem = ConfigItem::GetObject(m_Type, checkName);
|
2014-04-05 22:53:44 +02:00
|
|
|
|
|
|
|
if (oldItem) {
|
|
|
|
std::ostringstream msgbuf;
|
2014-11-09 19:48:28 +01:00
|
|
|
msgbuf << "Object '" << name << "' of type '" << m_Type << "' re-defined: " << m_DebugInfo << "; previous definition: " << oldItem->GetDebugInfo();
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(m_DebugInfo));
|
2014-04-05 22:53:44 +02:00
|
|
|
}
|
2014-03-30 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
item->SetType(m_Type);
|
2014-03-30 15:04:53 +02:00
|
|
|
|
|
|
|
if (name.FindFirstOf("!") != String::NPos) {
|
|
|
|
std::ostringstream msgbuf;
|
2014-11-09 19:48:28 +01:00
|
|
|
msgbuf << "Name for object '" << name << "' of type '" << m_Type << "' is invalid: Object names may not contain '!'";
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(m_DebugInfo));
|
2014-03-30 15:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
item->SetName(name);
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
item->AddExpression(new OwnedExpression(m_Expression));
|
|
|
|
item->SetAbstract(m_Abstract);
|
2014-11-06 19:35:47 +01:00
|
|
|
item->SetScope(context);
|
2014-11-09 19:48:28 +01:00
|
|
|
item->SetZone(m_Zone);
|
2014-03-30 15:04:53 +02:00
|
|
|
item->Compile()->Register();
|
|
|
|
|
2014-11-10 12:05:45 +01:00
|
|
|
if (m_Filter)
|
2014-11-09 19:48:28 +01:00
|
|
|
ObjectRule::AddRule(m_Type, name, m_Filter, m_DebugInfo, context);
|
2014-04-23 12:44:36 +02:00
|
|
|
|
2014-03-30 15:04:53 +02:00
|
|
|
return Empty;
|
2014-04-02 09:04:17 +02:00
|
|
|
}
|
2014-05-10 11:26:56 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
Value ForExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
|
2014-05-10 11:26:56 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
Value value = m_Value->Evaluate(context, dhint);
|
2014-05-10 11:26:56 +02:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
if (value.IsObjectType<Array>()) {
|
2014-11-09 19:48:28 +01:00
|
|
|
if (!m_FVVar.IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Cannot use dictionary iterator for array.") << errinfo_debuginfo(m_DebugInfo));
|
2014-05-10 11:26:56 +02:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
Array::Ptr arr = value;
|
|
|
|
|
|
|
|
ObjectLock olock(arr);
|
|
|
|
BOOST_FOREACH(const Value& value, arr) {
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr xcontext = new Dictionary();
|
2014-11-06 19:35:47 +01:00
|
|
|
xcontext->Set("__parent", context);
|
2014-11-09 19:48:28 +01:00
|
|
|
xcontext->Set(m_FKVar, value);
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
m_Expression->Evaluate(xcontext, dhint);
|
2014-11-04 11:01:00 +01:00
|
|
|
}
|
|
|
|
} else if (value.IsObjectType<Dictionary>()) {
|
2014-11-09 19:48:28 +01:00
|
|
|
if (m_FVVar.IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Cannot use array iterator for dictionary.") << errinfo_debuginfo(m_DebugInfo));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr dict = value;
|
|
|
|
|
|
|
|
ObjectLock olock(dict);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr xcontext = new Dictionary();
|
2014-11-06 19:35:47 +01:00
|
|
|
xcontext->Set("__parent", context);
|
2014-11-09 19:48:28 +01:00
|
|
|
xcontext->Set(m_FKVar, kv.first);
|
|
|
|
xcontext->Set(m_FVVar, kv.second);
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
m_Expression->Evaluate(xcontext, dhint);
|
2014-11-04 11:01:00 +01:00
|
|
|
}
|
|
|
|
} else
|
2014-11-09 19:48:28 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Invalid type in __for expression: " + value.GetTypeName()) << errinfo_debuginfo(m_DebugInfo));
|
2014-05-10 11:26:56 +02:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
2014-08-12 15:31:47 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr DebugHint::ToDictionary(void) const
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary();
|
2014-08-12 15:31:47 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr messages = new Array();
|
2014-08-12 15:31:47 +02:00
|
|
|
typedef std::pair<String, DebugInfo> MessageType;
|
|
|
|
BOOST_FOREACH(const MessageType& message, Messages) {
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr amsg = new Array();
|
2014-08-12 15:31:47 +02:00
|
|
|
amsg->Add(message.first);
|
|
|
|
amsg->Add(message.second.Path);
|
|
|
|
amsg->Add(message.second.FirstLine);
|
|
|
|
amsg->Add(message.second.FirstColumn);
|
|
|
|
amsg->Add(message.second.LastLine);
|
|
|
|
amsg->Add(message.second.LastColumn);
|
|
|
|
messages->Add(amsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Set("messages", messages);
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr properties = new Dictionary();
|
2014-08-12 15:31:47 +02:00
|
|
|
|
|
|
|
typedef std::map<String, DebugHint>::value_type ChildType;
|
|
|
|
BOOST_FOREACH(const ChildType& kv, Children) {
|
|
|
|
properties->Set(kv.first, kv.second.ToDictionary());
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Set("properties", properties);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
bool Expression::HasField(const Object::Ptr& context, const String& field)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
|
|
|
|
|
|
|
|
if (dict)
|
|
|
|
return dict->Contains(field);
|
|
|
|
else {
|
|
|
|
Type::Ptr type = context->GetReflectionType();
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return type->GetFieldId(field) != -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Expression::GetField(const Object::Ptr& context, const String& field)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
|
|
|
|
|
|
|
|
if (dict)
|
|
|
|
return dict->Get(field);
|
|
|
|
else {
|
|
|
|
Type::Ptr type = context->GetReflectionType();
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
int fid = type->GetFieldId(field);
|
|
|
|
|
|
|
|
if (fid == -1)
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
return context->GetField(fid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Expression::SetField(const Object::Ptr& context, const String& field, const Value& value)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
|
|
|
|
|
|
|
|
if (dict)
|
|
|
|
dict->Set(field, value);
|
|
|
|
else {
|
|
|
|
Type::Ptr type = context->GetReflectionType();
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Cannot set field on object."));
|
|
|
|
|
|
|
|
int fid = type->GetFieldId(field);
|
|
|
|
|
|
|
|
if (fid == -1)
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' does not exist."));
|
|
|
|
|
|
|
|
try {
|
|
|
|
context->SetField(fid, value);
|
|
|
|
} catch (const boost::bad_lexical_cast&) {
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
|
|
|
|
} catch (const std::bad_cast&) {
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|