icinga2/lib/config/config_parser.yy

925 lines
23 KiB
Plaintext
Raw Normal View History

%{
#define YYDEBUG 1
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* 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-05-25 16:23:35 +02:00
#include "config/i2-config.hpp"
#include "config/configitembuilder.hpp"
#include "config/configtype.hpp"
#include "config/configcompiler.hpp"
#include "config/configcompilercontext.hpp"
#include "config/typerule.hpp"
#include "config/typerulelist.hpp"
2014-10-16 17:44:06 +02:00
#include "config/expression.hpp"
2014-05-25 16:23:35 +02:00
#include "config/applyrule.hpp"
#include "config/objectrule.hpp"
#include "base/value.hpp"
#include "base/utility.hpp"
#include "base/array.hpp"
#include "base/scriptvariable.hpp"
#include "base/exception.hpp"
#include "base/dynamictype.hpp"
#include "base/configerror.hpp"
2013-03-16 21:18:53 +01:00
#include <sstream>
#include <stack>
#include <boost/foreach.hpp>
2013-03-17 20:19:29 +01:00
#define YYLTYPE icinga::DebugInfo
#define YYERROR_VERBOSE
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do { \
2014-03-21 15:41:00 +01:00
if (N) { \
(Current).Path = YYRHSLOC(Rhs, 1).Path; \
(Current).FirstLine = YYRHSLOC(Rhs, 1).FirstLine; \
(Current).FirstColumn = YYRHSLOC(Rhs, 1).FirstColumn; \
(Current).LastLine = YYRHSLOC(Rhs, N).LastLine; \
(Current).LastColumn = YYRHSLOC(Rhs, N).LastColumn; \
} else { \
(Current).Path = YYRHSLOC(Rhs, 0).Path; \
(Current).FirstLine = (Current).LastLine = \
YYRHSLOC(Rhs, 0).LastLine; \
(Current).FirstColumn = (Current).LastColumn = \
YYRHSLOC(Rhs, 0).LastColumn; \
} \
} while (0)
#define YY_LOCATION_PRINT(file, loc) \
do { \
2014-10-13 09:46:58 +02:00
std::ostringstream msgbuf; \
msgbuf << loc; \
std::string str = msgbuf.str(); \
fputs(str.c_str(), file); \
} while (0)
using namespace icinga;
int ignore_newlines = 0;
2014-10-16 17:44:06 +02:00
static void MakeRBinaryOp(Value** result, Expression::OpCallback& op, Value *left, Value *right, DebugInfo& diLeft, DebugInfo& diRight)
2014-03-30 05:04:58 +02:00
{
2014-10-16 17:44:06 +02:00
*result = new Value(make_shared<Expression>(op, *left, *right, DebugInfoRange(diLeft, diRight)));
2014-03-30 05:04:58 +02:00
delete left;
delete right;
}
%}
2012-05-31 08:45:02 +02:00
%pure-parser
%locations
%defines
%error-verbose
%parse-param { ConfigCompiler *context }
2012-05-31 08:45:02 +02:00
%lex-param { void *scanner }
%union {
char *text;
double num;
icinga::Value *variant;
2014-10-16 17:44:06 +02:00
icinga::Expression::OpCallback op;
icinga::TypeSpecifier type;
std::vector<String> *slist;
Array *array;
2012-05-31 08:45:02 +02:00
}
%token T_NEWLINE "new-line"
2012-05-31 08:45:02 +02:00
%token <text> T_STRING
%token <text> T_STRING_ANGLE
2012-05-31 08:45:02 +02:00
%token <num> T_NUMBER
%token T_NULL
%token <text> T_IDENTIFIER
%token <op> T_SET "= (T_SET)"
%token <op> T_SET_PLUS "+= (T_SET_PLUS)"
%token <op> T_SET_MINUS "-= (T_SET_MINUS)"
%token <op> T_SET_MULTIPLY "*= (T_SET_MULTIPLY)"
%token <op> T_SET_DIVIDE "/= (T_SET_DIVIDE)"
%token <op> T_SHIFT_LEFT "<< (T_SHIFT_LEFT)"
%token <op> T_SHIFT_RIGHT ">> (T_SHIFT_RIGHT)"
%token <op> T_EQUAL "== (T_EQUAL)"
%token <op> T_NOT_EQUAL "!= (T_NOT_EQUAL)"
%token <op> T_IN "in (T_IN)"
%token <op> T_NOT_IN "!in (T_NOT_IN)"
%token <op> T_LOGICAL_AND "&& (T_LOGICAL_AND)"
%token <op> T_LOGICAL_OR "|| (T_LOGICAL_OR)"
%token <op> T_LESS_THAN_OR_EQUAL "<= (T_LESS_THAN_OR_EQUAL)"
%token <op> T_GREATER_THAN_OR_EQUAL ">= (T_GREATER_THAN_OR_EQUAL)"
%token <op> T_PLUS "+ (T_PLUS)"
%token <op> T_MINUS "- (T_MINUS)"
%token <op> T_MULTIPLY "* (T_MULTIPLY)"
2014-03-24 12:54:23 +01:00
%token <op> T_DIVIDE_OP "/ (T_DIVIDE_OP)"
%token <op> T_BINARY_AND "& (T_BINARY_AND)"
%token <op> T_BINARY_OR "| (T_BINARY_OR)"
%token <op> T_LESS_THAN "< (T_LESS_THAN)"
%token <op> T_GREATER_THAN "> (T_GREATER_THAN)"
%token T_CONST "const (T_CONST)"
%token <type> T_TYPE_DICTIONARY "dictionary (T_TYPE_DICTIONARY)"
%token <type> T_TYPE_ARRAY "array (T_TYPE_ARRAY)"
%token <type> T_TYPE_NUMBER "number (T_TYPE_NUMBER)"
%token <type> T_TYPE_STRING "string (T_TYPE_STRING)"
%token <type> T_TYPE_SCALAR "scalar (T_TYPE_SCALAR)"
%token <type> T_TYPE_ANY "any (T_TYPE_ANY)"
%token <type> T_TYPE_NAME "name (T_TYPE_NAME)"
%token T_VALIDATOR "%validator (T_VALIDATOR)"
%token T_REQUIRE "%require (T_REQUIRE)"
%token T_ATTRIBUTE "%attribute (T_ATTRIBUTE)"
%token T_TYPE "type (T_TYPE)"
%token T_OBJECT "object (T_OBJECT)"
%token T_TEMPLATE "template (T_TEMPLATE)"
%token T_INCLUDE "include (T_INCLUDE)"
%token T_INCLUDE_RECURSIVE "include_recursive (T_INCLUDE_RECURSIVE)"
%token T_LIBRARY "library (T_LIBRARY)"
%token T_INHERITS "inherits (T_INHERITS)"
%token T_APPLY "apply (T_APPLY)"
%token T_TO "to (T_TO)"
%token T_WHERE "where (T_WHERE)"
%token T_IMPORT "import (T_IMPORT)"
%token T_ASSIGN "assign (T_ASSIGN)"
%token T_IGNORE "ignore (T_IGNORE)"
%token T_FUNCTION "function (T_FUNCTION)"
%token T_RETURN "return (T_RETURN)"
2014-05-10 11:26:56 +02:00
%token T_FOR "for (T_FOR)"
2014-03-28 16:32:15 +01:00
%type <text> identifier
%type <array> rterm_items
%type <array> rterm_items_inner
%type <array> identifier_items
%type <array> identifier_items_inner
%type <array> lterm_items
%type <array> lterm_items_inner
%type <variant> typerulelist
%type <op> lbinary_op
%type <type> type
%type <variant> rterm
%type <variant> rterm_array
%type <variant> rterm_scope
%type <variant> lterm
%type <variant> object
%type <variant> apply
%type <text> target_type_specifier
2014-03-28 16:32:15 +01:00
%left T_LOGICAL_OR
%left T_LOGICAL_AND
2014-03-30 05:04:58 +02:00
%left T_BINARY_OR
%left T_BINARY_AND
%left T_IN
%left T_NOT_IN
%left T_EQUAL T_NOT_EQUAL
%left T_LESS_THAN T_LESS_THAN_OR_EQUAL T_GREATER_THAN T_GREATER_THAN_OR_EQUAL
%left T_SHIFT_LEFT T_SHIFT_RIGHT
2014-03-28 12:17:22 +01:00
%left T_PLUS T_MINUS
%left T_MULTIPLY T_DIVIDE_OP
2014-03-30 05:04:58 +02:00
%right '!' '~'
%left '.' '(' '['
%right ':'
2012-05-31 08:45:02 +02:00
%{
2012-05-31 08:59:40 +02:00
int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
2012-05-31 08:45:02 +02:00
2012-08-07 21:02:12 +02:00
void yyerror(YYLTYPE *locp, ConfigCompiler *, const char *err)
2012-05-31 08:45:02 +02:00
{
2013-03-16 21:18:53 +01:00
std::ostringstream message;
2012-07-09 11:39:14 +02:00
message << *locp << ": " << err;
ConfigCompilerContext::GetInstance()->AddMessage(true, message.str(), *locp);
2012-05-31 08:45:02 +02:00
}
int yyparse(ConfigCompiler *context);
static std::stack<bool> m_Abstract;
2013-03-16 21:18:53 +01:00
static std::stack<TypeRuleList::Ptr> m_RuleLists;
static ConfigType::Ptr m_Type;
2012-05-31 10:16:32 +02:00
static Dictionary::Ptr m_ModuleScope;
static std::stack<bool> m_Apply;
static std::stack<bool> m_ObjectAssign;
static std::stack<bool> m_SeenAssign;
static std::stack<Expression::Ptr> m_Assign;
static std::stack<Expression::Ptr> m_Ignore;
void ConfigCompiler::Compile(void)
2012-05-31 10:16:32 +02:00
{
m_ModuleScope = make_shared<Dictionary>();
m_Abstract = std::stack<bool>();
m_RuleLists = std::stack<TypeRuleList::Ptr>();
m_Type.reset();
m_Apply = std::stack<bool>();
m_ObjectAssign = std::stack<bool>();
m_SeenAssign = std::stack<bool>();
m_Assign = std::stack<Expression::Ptr>();
m_Ignore = std::stack<Expression::Ptr>();
try {
yyparse(this);
} catch (const ConfigError& ex) {
const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
2013-03-16 21:18:53 +01:00
} catch (const std::exception& ex) {
ConfigCompilerContext::GetInstance()->AddMessage(true, DiagnosticInformation(ex));
}
2012-05-31 10:16:32 +02:00
}
2012-05-31 09:43:46 +02:00
#define scanner (context->GetScanner())
2012-05-31 08:45:02 +02:00
%}
%%
statements: /* empty */
| statements statement
;
2014-10-17 12:45:57 +02:00
statement: type | include | include_recursive | library | constant
2014-11-01 05:23:14 +01:00
{ }
| newlines
{ }
| lterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = *$1;
aexpr->Evaluate(m_ModuleScope);
delete $1;
}
;
include: T_INCLUDE rterm sep
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = *$2;
delete $2;
context->HandleInclude(aexpr->Evaluate(m_ModuleScope), false, DebugInfoRange(@1, @2));
}
| T_INCLUDE T_STRING_ANGLE
{
context->HandleInclude($2, true, DebugInfoRange(@1, @2));
free($2);
}
;
include_recursive: T_INCLUDE_RECURSIVE rterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = *$2;
delete $2;
context->HandleIncludeRecursive(aexpr->Evaluate(m_ModuleScope), "*.conf", DebugInfoRange(@1, @2));
}
| T_INCLUDE_RECURSIVE rterm ',' rterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr1 = *$2;
delete $2;
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr2 = *$4;
delete $4;
context->HandleIncludeRecursive(aexpr1->Evaluate(m_ModuleScope), aexpr2->Evaluate(m_ModuleScope), DebugInfoRange(@1, @4));
}
;
2012-05-31 08:45:02 +02:00
library: T_LIBRARY T_STRING sep
{
context->HandleLibrary($2);
2013-02-11 10:10:17 +01:00
free($2);
}
;
constant: T_CONST identifier T_SET rterm sep
2013-06-25 09:21:25 +02:00
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = *$4;
delete $4;
2013-09-10 16:03:36 +02:00
ScriptVariable::Ptr sv = ScriptVariable::Set($2, aexpr->Evaluate(m_ModuleScope));
sv->SetConstant(true);
2013-06-25 09:21:25 +02:00
free($2);
}
;
identifier: T_IDENTIFIER
| T_STRING
{
$$ = $1;
}
;
type: T_TYPE identifier
{
String name = String($2);
free($2);
m_Type = ConfigType::GetByName(name);
if (!m_Type) {
m_Type = make_shared<ConfigType>(name, DebugInfoRange(@1, @2));
m_Type->Register();
}
}
type_inherits_specifier typerulelist sep
{
TypeRuleList::Ptr ruleList = *$5;
delete $5;
m_Type->GetRuleList()->AddRules(ruleList);
m_Type->GetRuleList()->AddRequires(ruleList);
String validator = ruleList->GetValidator();
if (!validator.IsEmpty())
m_Type->GetRuleList()->SetValidator(validator);
}
;
typerulelist: '{'
{
m_RuleLists.push(make_shared<TypeRuleList>());
}
typerules
'}'
{
$$ = new Value(m_RuleLists.top());
m_RuleLists.pop();
}
;
typerules: typerules_inner
| typerules_inner sep
typerules_inner: /* empty */
| typerule
| typerules_inner sep typerule
;
typerule: T_REQUIRE T_STRING
{
m_RuleLists.top()->AddRequire($2);
2013-02-11 10:10:17 +01:00
free($2);
}
| T_VALIDATOR T_STRING
{
m_RuleLists.top()->SetValidator($2);
2013-02-11 10:10:17 +01:00
free($2);
}
| T_ATTRIBUTE type T_STRING
{
TypeRule rule($2, String(), $3, TypeRuleList::Ptr(), DebugInfoRange(@1, @3));
2013-02-11 10:10:17 +01:00
free($3);
m_RuleLists.top()->AddRule(rule);
}
| T_ATTRIBUTE T_TYPE_NAME '(' identifier ')' T_STRING
{
TypeRule rule($2, $4, $6, TypeRuleList::Ptr(), DebugInfoRange(@1, @6));
free($4);
free($6);
m_RuleLists.top()->AddRule(rule);
}
| T_ATTRIBUTE type T_STRING typerulelist
{
TypeRule rule($2, String(), $3, *$4, DebugInfoRange(@1, @4));
2013-02-11 10:10:17 +01:00
free($3);
delete $4;
m_RuleLists.top()->AddRule(rule);
}
;
type_inherits_specifier: /* empty */
2013-06-06 11:26:00 +02:00
| T_INHERITS identifier
{
m_Type->SetParent($2);
2013-02-11 10:10:17 +01:00
free($2);
}
;
type: T_TYPE_DICTIONARY
2013-03-14 12:17:46 +01:00
| T_TYPE_ARRAY
| T_TYPE_NUMBER
| T_TYPE_STRING
| T_TYPE_SCALAR
| T_TYPE_ANY
| T_TYPE_NAME
{
$$ = $1;
}
;
object:
{
m_Abstract.push(false);
m_ObjectAssign.push(true);
m_SeenAssign.push(false);
m_Assign.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
m_Ignore.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
}
object_declaration identifier rterm rterm_scope
{
m_ObjectAssign.pop();
Array::Ptr args = make_shared<Array>();
args->Add(m_Abstract.top());
m_Abstract.pop();
String type = $3;
args->Add(type);
free($3);
args->Add(*$4);
delete $4;
2014-10-16 17:44:06 +02:00
Expression::Ptr exprl = *$5;
delete $5;
exprl->MakeInline();
if (m_SeenAssign.top() && !ObjectRule::IsValidSourceType(type))
BOOST_THROW_EXCEPTION(ConfigError("object rule 'assign' cannot be used for type '" + type + "'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
m_SeenAssign.pop();
Expression::Ptr rex = make_shared<Expression>(&Expression::OpLogicalNegate, m_Ignore.top(), DebugInfoRange(@2, @5));
m_Ignore.pop();
Expression::Ptr filter = make_shared<Expression>(&Expression::OpLogicalAnd, m_Assign.top(), rex, DebugInfoRange(@2, @5));
m_Assign.pop();
args->Add(filter);
args->Add(context->GetZone());
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpObject, args, exprl, DebugInfoRange(@2, @5)));
}
2012-05-31 08:45:02 +02:00
;
2013-09-25 14:10:26 +02:00
object_declaration: T_OBJECT
| T_TEMPLATE
{
m_Abstract.top() = true;
}
identifier_items: identifier_items_inner
{
$$ = $1;
}
| identifier_items_inner ','
{
$$ = $1;
}
;
identifier_items_inner: /* empty */
{
$$ = new Array();
}
| identifier
{
$$ = new Array();
$$->Add($1);
free($1);
}
| identifier_items_inner ',' identifier
{
if ($1)
$$ = $1;
else
$$ = new Array();
$$->Add($3);
free($3);
}
;
lbinary_op: T_SET
| T_SET_PLUS
| T_SET_MINUS
| T_SET_MULTIPLY
| T_SET_DIVIDE
{
$$ = $1;
}
2012-05-31 08:45:02 +02:00
;
lterm_items: /* empty */
{
$$ = new Array();
}
| lterm_items_inner
{
$$ = $1;
}
| lterm_items_inner sep
{
$$ = $1;
}
lterm_items_inner: lterm
{
$$ = new Array();
$$->Add(*$1);
2013-04-04 13:51:36 +02:00
delete $1;
}
| lterm_items_inner sep lterm
{
if ($1)
$$ = $1;
else
$$ = new Array();
$$->Add(*$3);
2013-04-04 13:51:36 +02:00
delete $3;
}
2012-05-31 08:45:02 +02:00
;
lterm: identifier lbinary_op rterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aindex = make_shared<Expression>(&Expression::OpLiteral, $1, @1);
free($1);
2014-03-30 10:00:11 +02:00
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>($2, aindex, *$3, DebugInfoRange(@1, @3)));
delete $3;
}
2014-03-30 10:00:11 +02:00
| identifier '[' rterm ']' lbinary_op rterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr subexpr = make_shared<Expression>($5, *$3, *$6, DebugInfoRange(@1, @6));
2014-03-30 10:00:11 +02:00
delete $3;
delete $6;
Array::Ptr subexprl = make_shared<Array>();
subexprl->Add(subexpr);
2014-10-16 17:44:06 +02:00
Expression::Ptr aindex = make_shared<Expression>(&Expression::OpLiteral, $1, @1);
free($1);
2014-03-30 10:00:11 +02:00
2014-10-16 17:44:06 +02:00
Expression::Ptr expr = make_shared<Expression>(&Expression::OpDict, subexprl, DebugInfoRange(@1, @6));
$$ = new Value(make_shared<Expression>(&Expression::OpSetPlus, aindex, expr, DebugInfoRange(@1, @6)));
}
2014-03-28 12:17:22 +01:00
| identifier '.' T_IDENTIFIER lbinary_op rterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aindex = make_shared<Expression>(&Expression::OpLiteral, $3, @3);
Expression::Ptr subexpr = make_shared<Expression>($4, aindex, *$5, DebugInfoRange(@1, @5));
2014-03-28 12:17:22 +01:00
free($3);
delete $5;
Array::Ptr subexprl = make_shared<Array>();
subexprl->Add(subexpr);
2014-10-16 17:44:06 +02:00
Expression::Ptr aindexl = make_shared<Expression>(&Expression::OpLiteral, $1, @1);
2014-03-28 12:17:22 +01:00
free($1);
2014-03-30 10:00:11 +02:00
2014-10-16 17:44:06 +02:00
Expression::Ptr expr = make_shared<Expression>(&Expression::OpDict, subexprl, DebugInfoRange(@1, @5));
$$ = new Value(make_shared<Expression>(&Expression::OpSetPlus, aindexl, expr, DebugInfoRange(@1, @5)));
2014-03-28 12:17:22 +01:00
}
| T_IMPORT rterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr avar = make_shared<Expression>(&Expression::OpVariable, "type", DebugInfoRange(@1, @2));
$$ = new Value(make_shared<Expression>(&Expression::OpImport, avar, *$2, DebugInfoRange(@1, @2)));
delete $2;
}
| T_ASSIGN T_WHERE rterm
{
if ((m_Apply.empty() || !m_Apply.top()) && (m_ObjectAssign.empty() || !m_ObjectAssign.top()))
BOOST_THROW_EXCEPTION(ConfigError("'assign' keyword not valid in this context."));
m_SeenAssign.top() = true;
m_Assign.top() = make_shared<Expression>(&Expression::OpLogicalOr, m_Assign.top(), *$3, DebugInfoRange(@1, @3));
delete $3;
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpLiteral, Empty, DebugInfoRange(@1, @3)));
}
| T_IGNORE T_WHERE rterm
{
if ((m_Apply.empty() || !m_Apply.top()) && (m_ObjectAssign.empty() || !m_ObjectAssign.top()))
BOOST_THROW_EXCEPTION(ConfigError("'ignore' keyword not valid in this context."));
m_Ignore.top() = make_shared<Expression>(&Expression::OpLogicalOr, m_Ignore.top(), *$3, DebugInfoRange(@1, @3));
delete $3;
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpLiteral, Empty, DebugInfoRange(@1, @3)));
}
| T_RETURN rterm
{
2014-10-16 17:44:06 +02:00
Expression::Ptr aname = make_shared<Expression>(&Expression::OpLiteral, "__result", @1);
$$ = new Value(make_shared<Expression>(&Expression::OpSet, aname, *$2, DebugInfoRange(@1, @2)));
delete $2;
}
| apply
{
$$ = $1;
}
| object
{
$$ = $1;
}
| rterm
{
$$ = $1;
}
;
rterm_items: /* empty */
{
$$ = new Array();
}
| rterm_items_inner
{
$$ = $1;
}
| rterm_items_inner arraysep
{
$$ = $1;
}
;
2013-03-14 12:17:46 +01:00
rterm_items_inner: rterm
2013-03-14 12:17:46 +01:00
{
$$ = new Array();
$$->Add(*$1);
2013-04-04 13:51:36 +02:00
delete $1;
2013-03-14 12:17:46 +01:00
}
| rterm_items_inner arraysep rterm
2013-03-14 12:17:46 +01:00
{
$$ = $1;
$$->Add(*$3);
2013-04-04 13:51:36 +02:00
delete $3;
2013-03-14 12:17:46 +01:00
}
;
rterm_array: '[' newlines rterm_items newlines ']'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($3), DebugInfoRange(@1, @5)));
}
| '[' newlines rterm_items ']'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($3), DebugInfoRange(@1, @4)));
}
| '[' rterm_items newlines ']'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($2), DebugInfoRange(@1, @4)));
}
| '[' rterm_items ']'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($2), DebugInfoRange(@1, @3)));
}
2013-03-14 12:17:46 +01:00
;
rterm_scope: '{' newlines lterm_items newlines '}'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($3), DebugInfoRange(@1, @5)));
}
| '{' newlines lterm_items '}'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($3), DebugInfoRange(@1, @4)));
}
| '{' lterm_items newlines '}'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($2), DebugInfoRange(@1, @4)));
}
| '{' lterm_items '}'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($2), DebugInfoRange(@1, @3)));
}
;
rterm: T_STRING
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpLiteral, $1, @1));
free($1);
}
| T_NUMBER
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpLiteral, $1, @1));
}
| T_NULL
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpLiteral, Empty, @1));
}
2014-03-28 12:17:22 +01:00
| rterm '.' T_IDENTIFIER
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpIndexer, *$1, make_shared<Expression>(&Expression::OpLiteral, $3, @3), DebugInfoRange(@1, @3)));
2014-03-28 12:17:22 +01:00
delete $1;
free($3);
}
| rterm '(' rterm_items ')'
2013-03-14 12:17:46 +01:00
{
Array::Ptr arguments = Array::Ptr($3);
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpFunctionCall, *$1, make_shared<Expression>(&Expression::OpLiteral, arguments, @3), DebugInfoRange(@1, @4)));
delete $1;
}
| T_IDENTIFIER
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpVariable, $1, @1));
2013-06-25 09:21:25 +02:00
free($1);
}
| '!' rterm
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpLogicalNegate, *$2, DebugInfoRange(@1, @2)));
delete $2;
}
| '~' rterm
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpNegate, *$2, DebugInfoRange(@1, @2)));
delete $2;
}
2014-03-28 12:17:22 +01:00
| rterm '[' rterm ']'
{
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpIndexer, *$1, *$3, DebugInfoRange(@1, @4)));
2014-03-28 12:17:22 +01:00
delete $1;
delete $3;
}
| rterm_array
{
$$ = $1;
}
| rterm_scope
2013-06-25 09:21:25 +02:00
{
$$ = $1;
2013-06-25 09:21:25 +02:00
}
| '('
2013-06-25 09:21:25 +02:00
{
ignore_newlines++;
}
rterm ')'
{
ignore_newlines--;
$$ = $3;
}
2014-03-30 05:04:58 +02:00
| rterm T_LOGICAL_OR rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_LOGICAL_AND rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_BINARY_OR rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_BINARY_AND rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_IN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_NOT_IN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_NOT_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_LESS_THAN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_LESS_THAN_OR_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_GREATER_THAN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_GREATER_THAN_OR_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_SHIFT_LEFT rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_SHIFT_RIGHT rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_PLUS rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_MINUS rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_MULTIPLY rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| rterm T_DIVIDE_OP rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
| T_FUNCTION identifier '(' identifier_items ')' rterm_scope
{
Array::Ptr arr = make_shared<Array>();
arr->Add($2);
free($2);
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = *$6;
delete $6;
aexpr->MakeInline();
arr->Add(aexpr);
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpFunction, arr, Array::Ptr($4), DebugInfoRange(@1, @6)));
}
| T_FUNCTION '(' identifier_items ')' rterm_scope
{
Array::Ptr arr = make_shared<Array>();
arr->Add(Empty);
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = *$5;
delete $5;
aexpr->MakeInline();
arr->Add(aexpr);
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpFunction, arr, Array::Ptr($3), DebugInfoRange(@1, @5)));
}
2014-05-10 11:26:56 +02:00
| T_FOR '(' identifier T_IN rterm ')' rterm_scope
{
Array::Ptr arr = make_shared<Array>();
arr->Add($3);
free($3);
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = *$5;
2014-05-10 11:26:56 +02:00
delete $5;
arr->Add(aexpr);
2014-10-16 17:44:06 +02:00
Expression::Ptr ascope = *$7;
2014-05-10 11:26:56 +02:00
delete $7;
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpFor, arr, ascope, DebugInfoRange(@1, @7)));
2014-05-10 11:26:56 +02:00
}
2013-06-25 09:21:25 +02:00
;
target_type_specifier: /* empty */
{
$$ = strdup("");
}
| T_TO identifier
{
$$ = $2;
}
;
apply:
{
m_Apply.push(true);
m_SeenAssign.push(false);
m_Assign.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
m_Ignore.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
}
T_APPLY identifier rterm target_type_specifier rterm
{
m_Apply.pop();
String type = $3;
free($3);
2014-10-16 17:44:06 +02:00
Expression::Ptr aname = *$4;
delete $4;
String target = $5;
free($5);
if (!ApplyRule::IsValidSourceType(type))
2014-03-28 16:32:15 +01:00
BOOST_THROW_EXCEPTION(ConfigError("'apply' cannot be used with type '" + type + "'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
if (!ApplyRule::IsValidTargetType(type, target)) {
if (target == "") {
std::vector<String> types = ApplyRule::GetTargetTypes(type);
String typeNames;
2014-05-11 06:00:34 +02:00
for (std::vector<String>::size_type i = 0; i < types.size(); i++) {
if (typeNames != "") {
if (i == types.size() - 1)
typeNames += " or ";
else
typeNames += ", ";
}
typeNames += "'" + types[i] + "'";
}
BOOST_THROW_EXCEPTION(ConfigError("'apply' target type is ambiguous (can be one of " + typeNames + "): use 'to' to specify a type") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
} else
BOOST_THROW_EXCEPTION(ConfigError("'apply' target type '" + target + "' is invalid") << errinfo_debuginfo(DebugInfoRange(@2, @5)));
}
2014-10-16 17:44:06 +02:00
Expression::Ptr exprl = *$6;
delete $6;
exprl->MakeInline();
// assign && !ignore
if (!m_SeenAssign.top())
BOOST_THROW_EXCEPTION(ConfigError("'apply' is missing 'assign'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
m_SeenAssign.pop();
Expression::Ptr rex = make_shared<Expression>(&Expression::OpLogicalNegate, m_Ignore.top(), DebugInfoRange(@2, @5));
m_Ignore.pop();
Expression::Ptr filter = make_shared<Expression>(&Expression::OpLogicalAnd, m_Assign.top(), rex, DebugInfoRange(@2, @5));
m_Assign.pop();
Array::Ptr args = make_shared<Array>();
args->Add(type);
args->Add(target);
args->Add(aname);
args->Add(filter);
2014-10-16 17:44:06 +02:00
$$ = new Value(make_shared<Expression>(&Expression::OpApply, args, exprl, DebugInfoRange(@2, @5)));
}
;
newlines: T_NEWLINE
| newlines T_NEWLINE
;
/* required separator */
sep: ',' newlines
| ','
| ';' newlines
| ';'
| newlines
;
arraysep: ',' newlines
| ','
;
2012-05-31 08:45:02 +02:00
%%