4 /******************************************************************************
6 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License *
10 * as published by the Free Software Foundation; either version 2 *
11 * of the License, or (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software Foundation *
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
21 ******************************************************************************/
23 #include "i2-config.h"
24 #include "config/expression.h"
25 #include "config/expressionlist.h"
26 #include "config/configitembuilder.h"
27 #include "config/configcompiler.h"
28 #include "config/configcompilercontext.h"
29 #include "config/configerror.h"
30 #include "config/typerule.h"
31 #include "config/typerulelist.h"
32 #include "config/aexpression.h"
33 #include "config/applyrule.h"
34 #include "base/value.h"
35 #include "base/utility.h"
36 #include "base/array.h"
37 #include "base/scriptvariable.h"
38 #include "base/exception.h"
39 #include "base/dynamictype.h"
42 #include <boost/foreach.hpp>
44 #define YYLTYPE icinga::DebugInfo
45 #define YYERROR_VERBOSE
47 #define YYLLOC_DEFAULT(Current, Rhs, N) \
50 (Current).Path = YYRHSLOC(Rhs, 1).Path; \
51 (Current).FirstLine = YYRHSLOC(Rhs, 1).FirstLine; \
52 (Current).FirstColumn = YYRHSLOC(Rhs, 1).FirstColumn; \
53 (Current).LastLine = YYRHSLOC(Rhs, N).LastLine; \
54 (Current).LastColumn = YYRHSLOC(Rhs, N).LastColumn; \
56 (Current).Path = YYRHSLOC(Rhs, 0).Path; \
57 (Current).FirstLine = (Current).LastLine = \
58 YYRHSLOC(Rhs, 0).LastLine; \
59 (Current).FirstColumn = (Current).LastColumn = \
60 YYRHSLOC(Rhs, 0).LastColumn; \
64 #define YY_LOCATION_PRINT(file, loc) \
66 std::ostringstream msgbuf; \
68 std::string str = msgbuf.str(); \
69 fputs(str.c_str(), file); \
72 using namespace icinga;
82 %parse-param { ConfigCompiler *context }
83 %lex-param { void *scanner }
88 icinga::Value *variant;
89 icinga::ExpressionOperator op;
90 icinga::TypeSpecifier type;
91 std::vector<String> *slist;
93 ExpressionList *exprl;
98 %token <text> T_STRING
99 %token <text> T_STRING_ANGLE
100 %token <num> T_NUMBER
102 %token <text> T_IDENTIFIER
103 %token <op> T_SET "= (T_SET)"
104 %token <op> T_PLUS_EQUAL "+= (T_PLUS_EQUAL)"
105 %token <op> T_MINUS_EQUAL "-= (T_MINUS_EQUAL)"
106 %token <op> T_MULTIPLY_EQUAL "*= (T_MULTIPLY_EQUAL)"
107 %token <op> T_DIVIDE_EQUAL "/= (T_DIVIDE_EQUAL)"
108 %token T_VAR "var (T_VAR)"
109 %token T_CONST "const (T_CONST)"
110 %token T_SHIFT_LEFT "<< (T_SHIFT_LEFT)"
111 %token T_SHIFT_RIGHT ">> (T_SHIFT_RIGHT)"
112 %token T_EQUAL "== (T_EQUAL)"
113 %token T_NOT_EQUAL "!= (T_NOT_EQUAL)"
114 %token T_IN "in (T_IN)"
115 %token T_NOT_IN "!in (T_NOT_IN)"
116 %token T_LOGICAL_AND "&& (T_LOGICAL_AND)"
117 %token T_LOGICAL_OR "|| (T_LOGICAL_OR)"
118 %token T_LESS_THAN_OR_EQUAL "<= (T_LESS_THAN_OR_EQUAL)"
119 %token T_GREATER_THAN_OR_EQUAL ">= (T_GREATER_THAN_OR_EQUAL)"
120 %token <type> T_TYPE_DICTIONARY "dictionary (T_TYPE_DICTIONARY)"
121 %token <type> T_TYPE_ARRAY "array (T_TYPE_ARRAY)"
122 %token <type> T_TYPE_NUMBER "number (T_TYPE_NUMBER)"
123 %token <type> T_TYPE_STRING "string (T_TYPE_STRING)"
124 %token <type> T_TYPE_SCALAR "scalar (T_TYPE_SCALAR)"
125 %token <type> T_TYPE_ANY "any (T_TYPE_ANY)"
126 %token <type> T_TYPE_NAME "name (T_TYPE_NAME)"
127 %token T_VALIDATOR "%validator (T_VALIDATOR)"
128 %token T_REQUIRE "%require (T_REQUIRE)"
129 %token T_ATTRIBUTE "%attribute (T_ATTRIBUTE)"
130 %token T_TYPE "type (T_TYPE)"
131 %token T_OBJECT "object (T_OBJECT)"
132 %token T_TEMPLATE "template (T_TEMPLATE)"
133 %token T_INCLUDE "include (T_INCLUDE)"
134 %token T_INCLUDE_RECURSIVE "include_recursive (T_INCLUDE_RECURSIVE)"
135 %token T_LIBRARY "library (T_LIBRARY)"
136 %token T_INHERITS "inherits (T_INHERITS)"
137 %token T_PARTIAL "partial (T_PARTIAL)"
138 %token T_APPLY "apply (T_APPLY)"
139 %token T_TO "to (T_TO)"
140 %token T_WHERE "where (T_WHERE)"
141 %type <text> identifier
142 %type <array> array_items
143 %type <array> array_items_inner
144 %type <variant> value
145 %type <expr> expression
146 %type <exprl> expressions
147 %type <exprl> expressions_inner
148 %type <exprl> expressionlist
149 %type <variant> typerulelist
152 %type <num> partial_specifier
153 %type <slist> object_inherits_list
154 %type <slist> object_inherits_specifier
155 %type <aexpr> aexpression
156 %type <num> variable_decl
162 %nonassoc T_NOT_EQUAL
171 int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
173 void yyerror(YYLTYPE *locp, ConfigCompiler *, const char *err)
175 std::ostringstream message;
176 message << *locp << ": " << err;
177 ConfigCompilerContext::GetInstance()->AddMessage(true, message.str(), *locp);
180 int yyparse(ConfigCompiler *context);
182 static std::stack<Array::Ptr> m_Arrays;
183 static bool m_Abstract;
185 static std::stack<TypeRuleList::Ptr> m_RuleLists;
186 static ConfigType::Ptr m_Type;
188 void ConfigCompiler::Compile(void)
192 } catch (const ConfigError& ex) {
193 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), ex.GetDebugInfo());
194 } catch (const std::exception& ex) {
195 ConfigCompilerContext::GetInstance()->AddMessage(true, DiagnosticInformation(ex));
199 #define scanner (context->GetScanner())
204 statements: /* empty */
205 | statements statement
208 statement: object | type | include | include_recursive | library | variable | apply
211 include: T_INCLUDE value
213 context->HandleInclude(*$2, false, DebugInfoRange(@1, @2));
216 | T_INCLUDE T_STRING_ANGLE
218 context->HandleInclude($2, true, DebugInfoRange(@1, @2));
223 include_recursive: T_INCLUDE_RECURSIVE value
225 context->HandleIncludeRecursive(*$2, "*.conf", DebugInfoRange(@1, @2));
228 | T_INCLUDE_RECURSIVE value value
230 context->HandleIncludeRecursive(*$2, *$3, DebugInfoRange(@1, @3));
236 library: T_LIBRARY T_STRING
238 context->HandleLibrary($2);
243 variable: variable_decl identifier T_SET value
246 if (value->IsObjectType<ExpressionList>()) {
247 Dictionary::Ptr dict = make_shared<Dictionary>();
248 ExpressionList::Ptr exprl = *value;
249 exprl->Execute(dict);
251 value = new Value(dict);
254 ScriptVariable::Ptr sv = ScriptVariable::Set($2, *value);
255 sv->SetConstant(true);
272 identifier: T_IDENTIFIER
279 type: partial_specifier T_TYPE identifier
281 String name = String($3);
284 m_Type = ConfigType::GetByName(name);
288 BOOST_THROW_EXCEPTION(ConfigError("Partial type definition for unknown type '" + name + "'", DebugInfoRange(@1, @3)));
290 m_Type = make_shared<ConfigType>(name, DebugInfoRange(@1, @3));
294 type_inherits_specifier typerulelist
296 TypeRuleList::Ptr ruleList = *$6;
297 m_Type->GetRuleList()->AddRules(ruleList);
298 m_Type->GetRuleList()->AddRequires(ruleList);
300 String validator = ruleList->GetValidator();
301 if (!validator.IsEmpty())
302 m_Type->GetRuleList()->SetValidator(validator);
308 partial_specifier: /* Empty */
320 m_RuleLists.push(make_shared<TypeRuleList>());
325 $$ = new Value(m_RuleLists.top());
330 typerules: typerules_inner
331 | typerules_inner ','
333 typerules_inner: /* empty */
335 | typerules_inner ',' typerule
338 typerule: T_REQUIRE T_STRING
340 m_RuleLists.top()->AddRequire($2);
343 | T_VALIDATOR T_STRING
345 m_RuleLists.top()->SetValidator($2);
348 | T_ATTRIBUTE type T_STRING
350 TypeRule rule($2, String(), $3, TypeRuleList::Ptr(), DebugInfoRange(@1, @3));
353 m_RuleLists.top()->AddRule(rule);
355 | T_ATTRIBUTE T_TYPE_NAME '(' identifier ')' T_STRING
357 TypeRule rule($2, $4, $6, TypeRuleList::Ptr(), DebugInfoRange(@1, @6));
361 m_RuleLists.top()->AddRule(rule);
363 | T_ATTRIBUTE type T_STRING typerulelist
365 TypeRule rule($2, String(), $3, *$4, DebugInfoRange(@1, @4));
368 m_RuleLists.top()->AddRule(rule);
372 type_inherits_specifier: /* empty */
373 | T_INHERITS identifier
375 m_Type->SetParent($2);
380 type: T_TYPE_DICTIONARY
396 object_declaration identifier T_STRING object_inherits_specifier expressionlist
398 DebugInfo di = DebugInfoRange(@2, @6);
399 ConfigItemBuilder::Ptr item = make_shared<ConfigItemBuilder>(di);
401 ConfigItem::Ptr oldItem = ConfigItem::GetObject($3, $4);
404 std::ostringstream msgbuf;
405 msgbuf << "Object '" << $4 << "' of type '" << $3 << "' re-defined: " << di << "; previous definition: " << oldItem->GetDebugInfo();
409 BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str(), di));
414 if (strchr($4, '!') != NULL) {
415 std::ostringstream msgbuf;
416 msgbuf << "Name for object '" << $4 << "' of type '" << $3 << "' is invalid: Object names may not contain '!'";
418 BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str(), @4));
427 BOOST_FOREACH(const String& parent, *$5) {
428 item->AddParent(parent);
435 ExpressionList::Ptr exprl = ExpressionList::Ptr($6);
436 item->AddExpressionList(exprl);
439 item->SetAbstract(m_Abstract);
441 item->Compile()->Register();
446 object_declaration: T_OBJECT
452 object_inherits_list:
458 $$ = new std::vector<String>();
462 | object_inherits_list ',' T_STRING
467 $$ = new std::vector<String>();
474 object_inherits_specifier:
478 | T_INHERITS object_inherits_list
484 expressionlist: '{' expressions '}'
489 $$ = new ExpressionList();
493 expressions: expressions_inner
497 | expressions_inner ','
502 expressions_inner: /* empty */
508 $$ = new ExpressionList();
509 $$->AddExpression(*$1);
512 | expressions_inner ',' expression
517 $$ = new ExpressionList();
519 $$->AddExpression(*$3);
524 expression: identifier operator value
526 $$ = new Expression($1, $2, *$3, DebugInfoRange(@1, @3));
530 | identifier '[' T_STRING ']' operator value
532 Expression subexpr($3, $5, *$6, DebugInfoRange(@1, @6));
536 ExpressionList::Ptr subexprl = make_shared<ExpressionList>();
537 subexprl->AddExpression(subexpr);
539 $$ = new Expression($1, OperatorPlus, subexprl, DebugInfoRange(@1, @6));
554 array_items: array_items_inner
558 | array_items_inner ','
563 array_items_inner: /* empty */
573 | array_items_inner ',' aexpression
585 aexpression: T_STRING
587 $$ = new Value(make_shared<AExpression>(AEReturn, AValue(ATSimple, $1), @1));
592 $$ = new Value(make_shared<AExpression>(AEReturn, AValue(ATSimple, $1), @1));
596 $$ = new Value(make_shared<AExpression>(AEReturn, AValue(ATSimple, Empty), @1));
598 | T_IDENTIFIER '(' array_items ')'
600 Array::Ptr arguments = Array::Ptr($3);
601 $$ = new Value(make_shared<AExpression>(AEFunctionCall, AValue(ATSimple, $1), AValue(ATSimple, arguments), DebugInfoRange(@1, @4)));
606 $$ = new Value(make_shared<AExpression>(AEReturn, AValue(ATVariable, $1), @1));
611 $$ = new Value(make_shared<AExpression>(AENegate, static_cast<AExpression::Ptr>(*$2), DebugInfoRange(@1, @2)));
616 $$ = new Value(make_shared<AExpression>(AENegate, static_cast<AExpression::Ptr>(*$2), DebugInfoRange(@1, @2)));
619 | '[' array_items ']'
621 $$ = new Value(make_shared<AExpression>(AEArray, AValue(ATSimple, Array::Ptr($2)), DebugInfoRange(@1, @3)));
623 | '(' aexpression ')'
627 | aexpression '+' aexpression
629 $$ = new Value(make_shared<AExpression>(AEAdd, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
633 | aexpression '-' aexpression
635 $$ = new Value(make_shared<AExpression>(AESubtract, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
639 | aexpression '*' aexpression
641 $$ = new Value(make_shared<AExpression>(AEMultiply, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
645 | aexpression '/' aexpression
647 $$ = new Value(make_shared<AExpression>(AEDivide, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
651 | aexpression '&' aexpression
653 $$ = new Value(make_shared<AExpression>(AEBinaryAnd, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
657 | aexpression '|' aexpression
659 $$ = new Value(make_shared<AExpression>(AEBinaryOr, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @2)));
663 | aexpression T_IN aexpression
665 $$ = new Value(make_shared<AExpression>(AEIn, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
669 | aexpression T_NOT_IN aexpression
671 $$ = new Value(make_shared<AExpression>(AENotIn, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
675 | aexpression T_LESS_THAN_OR_EQUAL aexpression
677 $$ = new Value(make_shared<AExpression>(AELessThanOrEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
681 | aexpression T_GREATER_THAN_OR_EQUAL aexpression
683 $$ = new Value(make_shared<AExpression>(AEGreaterThanOrEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
687 | aexpression '<' aexpression
689 $$ = new Value(make_shared<AExpression>(AELessThan, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
693 | aexpression '>' aexpression
695 $$ = new Value(make_shared<AExpression>(AEGreaterThan, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
699 | aexpression T_EQUAL aexpression
701 $$ = new Value(make_shared<AExpression>(AEEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
705 | aexpression T_NOT_EQUAL aexpression
707 $$ = new Value(make_shared<AExpression>(AENotEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
711 | aexpression T_SHIFT_LEFT aexpression
713 $$ = new Value(make_shared<AExpression>(AEShiftLeft, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
717 | aexpression T_SHIFT_RIGHT aexpression
719 $$ = new Value(make_shared<AExpression>(AEShiftRight, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
723 | aexpression T_LOGICAL_AND aexpression
725 $$ = new Value(make_shared<AExpression>(AELogicalAnd, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
729 | aexpression T_LOGICAL_OR aexpression
731 $$ = new Value(make_shared<AExpression>(AELogicalOr, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
737 value: expressionlist
739 ExpressionList::Ptr exprl = ExpressionList::Ptr($1);
740 $$ = new Value(exprl);
744 AExpression::Ptr aexpr = *$1;
745 $$ = new Value(aexpr->Evaluate(Dictionary::Ptr()));
750 optional_template: /* empty */
754 apply: T_APPLY optional_template identifier identifier T_TO identifier T_WHERE aexpression
756 if (!ApplyRule::IsValidCombination($3, $6)) {
757 BOOST_THROW_EXCEPTION(ConfigError("'apply' cannot be used with types '" + String($3) + "' and '" + String($6) + "'.", @1));
760 Array::Ptr arguments = make_shared<Array>();
764 AExpression::Ptr aexpr = make_shared<AExpression>(AEFunctionCall, AValue(ATSimple, "bool"), AValue(ATSimple, arguments), @8);
766 ApplyRule::AddRule($3, $4, $6, aexpr, DebugInfoRange(@1, @8));