]> granicus.if.org Git - icinga2/blob - lib/config/config_parser.yy
Implement the "import" keyword.
[icinga2] / lib / config / config_parser.yy
1 %{
2  #define YYDEBUG 1
3  
4 /******************************************************************************
5  * Icinga 2                                                                   *
6  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
7  *                                                                            *
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.                     *
12  *                                                                            *
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.                               *
17  *                                                                            *
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  ******************************************************************************/
22
23 #include "i2-config.h"
24 #include "config/configitembuilder.h"
25 #include "config/configcompiler.h"
26 #include "config/configcompilercontext.h"
27 #include "config/configerror.h"
28 #include "config/typerule.h"
29 #include "config/typerulelist.h"
30 #include "config/aexpression.h"
31 #include "config/applyrule.h"
32 #include "base/value.h"
33 #include "base/utility.h"
34 #include "base/array.h"
35 #include "base/scriptvariable.h"
36 #include "base/exception.h"
37 #include "base/dynamictype.h"
38 #include <sstream>
39 #include <stack>
40 #include <boost/foreach.hpp>
41
42 #define YYLTYPE icinga::DebugInfo
43 #define YYERROR_VERBOSE
44
45 #define YYLLOC_DEFAULT(Current, Rhs, N)                                 \
46 do {                                                                    \
47         if (N) {                                                        \
48                 (Current).Path = YYRHSLOC(Rhs, 1).Path;                 \
49                 (Current).FirstLine = YYRHSLOC(Rhs, 1).FirstLine;       \
50                 (Current).FirstColumn = YYRHSLOC(Rhs, 1).FirstColumn;   \
51                 (Current).LastLine = YYRHSLOC(Rhs, N).LastLine;         \
52                 (Current).LastColumn = YYRHSLOC(Rhs, N).LastColumn;     \
53         } else {                                                        \
54                 (Current).Path = YYRHSLOC(Rhs, 0).Path;                 \
55                 (Current).FirstLine = (Current).LastLine =              \
56                 YYRHSLOC(Rhs, 0).LastLine;                              \
57                 (Current).FirstColumn = (Current).LastColumn =          \
58                 YYRHSLOC(Rhs, 0).LastColumn;                            \
59         }                                                               \
60 } while (0)
61
62 #define YY_LOCATION_PRINT(file, loc)                    \
63 do {                                                    \
64        std::ostringstream msgbuf;                       \
65        msgbuf << loc;                                   \
66        std::string str = msgbuf.str();                  \
67        fputs(str.c_str(), file);                        \
68 } while (0)
69
70 using namespace icinga;
71
72 %}
73
74 %pure-parser
75
76 %locations
77 %defines
78 %error-verbose
79
80 %parse-param { ConfigCompiler *context }
81 %lex-param { void *scanner }
82
83 %union {
84         char *text;
85         double num;
86         icinga::Value *variant;
87         icinga::AExpression::OpCallback op;
88         icinga::TypeSpecifier type;
89         std::vector<String> *slist;
90         Array *array;
91 }
92
93 %token <text> T_STRING
94 %token <text> T_STRING_ANGLE
95 %token <num> T_NUMBER
96 %token T_NULL
97 %token <text> T_IDENTIFIER
98
99 %token <op> T_SET "= (T_SET)"
100 %token <op> T_SET_PLUS "+= (T_SET_PLUS)"
101 %token <op> T_SET_MINUS "-= (T_SET_MINUS)"
102 %token <op> T_SET_MULTIPLY "*= (T_SET_MULTIPLY)"
103 %token <op> T_SET_DIVIDE "/= (T_SET_DIVIDE)"
104
105 %token <op> T_SHIFT_LEFT "<< (T_SHIFT_LEFT)"
106 %token <op> T_SHIFT_RIGHT ">> (T_SHIFT_RIGHT)"
107 %token <op> T_EQUAL "== (T_EQUAL)"
108 %token <op> T_NOT_EQUAL "!= (T_NOT_EQUAL)"
109 %token <op> T_IN "in (T_IN)"
110 %token <op> T_NOT_IN "!in (T_NOT_IN)"
111 %token <op> T_LOGICAL_AND "&& (T_LOGICAL_AND)"
112 %token <op> T_LOGICAL_OR "|| (T_LOGICAL_OR)"
113 %token <op> T_LESS_THAN_OR_EQUAL "<= (T_LESS_THAN_OR_EQUAL)"
114 %token <op> T_GREATER_THAN_OR_EQUAL ">= (T_GREATER_THAN_OR_EQUAL)"
115 %token <op> T_PLUS "+ (T_PLUS)"
116 %token <op> T_MINUS "- (T_MINUS)"
117 %token <op> T_MULTIPLY "* (T_MULTIPLY)"
118 %token <op> T_DIVIDE_OP "/ (T_DIVIDE_OP)"
119 %token <op> T_BINARY_AND "& (T_BINARY_AND)"
120 %token <op> T_BINARY_OR "| (T_BINARY_OR)"
121 %token <op> T_LESS_THAN "< (T_LESS_THAN)"
122 %token <op> T_GREATER_THAN "> (T_GREATER_THAN)"
123
124 %token T_VAR "var (T_VAR)"
125 %token T_CONST "const (T_CONST)"
126 %token <type> T_TYPE_DICTIONARY "dictionary (T_TYPE_DICTIONARY)"
127 %token <type> T_TYPE_ARRAY "array (T_TYPE_ARRAY)"
128 %token <type> T_TYPE_NUMBER "number (T_TYPE_NUMBER)"
129 %token <type> T_TYPE_STRING "string (T_TYPE_STRING)"
130 %token <type> T_TYPE_SCALAR "scalar (T_TYPE_SCALAR)"
131 %token <type> T_TYPE_ANY "any (T_TYPE_ANY)"
132 %token <type> T_TYPE_NAME "name (T_TYPE_NAME)"
133 %token T_VALIDATOR "%validator (T_VALIDATOR)"
134 %token T_REQUIRE "%require (T_REQUIRE)"
135 %token T_ATTRIBUTE "%attribute (T_ATTRIBUTE)"
136 %token T_TYPE "type (T_TYPE)"
137 %token T_OBJECT "object (T_OBJECT)"
138 %token T_TEMPLATE "template (T_TEMPLATE)"
139 %token T_INCLUDE "include (T_INCLUDE)"
140 %token T_INCLUDE_RECURSIVE "include_recursive (T_INCLUDE_RECURSIVE)"
141 %token T_LIBRARY "library (T_LIBRARY)"
142 %token T_INHERITS "inherits (T_INHERITS)"
143 %token T_PARTIAL "partial (T_PARTIAL)"
144 %token T_APPLY "apply (T_APPLY)"
145 %token T_TO "to (T_TO)"
146 %token T_WHERE "where (T_WHERE)"
147 %token T_IMPORT "import (T_IMPORT)"
148 %type <text> identifier
149 %type <array> rterm_items
150 %type <array> rterm_items_inner
151 %type <array> lterm_items
152 %type <array> lterm_items_inner
153 %type <variant> typerulelist
154 %type <op> lbinary_op
155 %type <op> rbinary_op
156 %type <type> type
157 %type <num> partial_specifier
158 %type <variant> rterm
159 %type <variant> rterm_scope
160 %type <variant> lterm
161 %type <num> variable_decl
162 %left T_LOGICAL_OR
163 %left T_LOGICAL_AND
164 %left T_IN
165 %left T_NOT_IN
166 %nonassoc T_EQUAL
167 %nonassoc T_NOT_EQUAL
168 %left '+' '-'
169 %left '*' '/'
170 %left '&'
171 %left '|'
172 %right '~'
173 %right '!'
174 %{
175
176 int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
177
178 void yyerror(YYLTYPE *locp, ConfigCompiler *, const char *err)
179 {
180         std::ostringstream message;
181         message << *locp << ": " << err;
182         ConfigCompilerContext::GetInstance()->AddMessage(true, message.str(), *locp);
183 }
184
185 int yyparse(ConfigCompiler *context);
186
187 static bool m_Abstract;
188
189 static std::stack<TypeRuleList::Ptr> m_RuleLists;
190 static ConfigType::Ptr m_Type;
191
192 static Dictionary::Ptr m_ModuleScope;
193
194 void ConfigCompiler::Compile(void)
195 {
196         m_ModuleScope = make_shared<Dictionary>();
197
198         try {
199                 yyparse(this);
200         } catch (const ConfigError& ex) {
201                 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
202                 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
203         } catch (const std::exception& ex) {
204                 ConfigCompilerContext::GetInstance()->AddMessage(true, DiagnosticInformation(ex));
205         }
206 }
207
208 #define scanner (context->GetScanner())
209
210 %}
211
212 %%
213 statements: /* empty */
214         | statements statement
215         ;
216
217 statement: object | type | include | include_recursive | library | variable | apply
218         { }
219         | lterm
220         {
221                 AExpression::Ptr aexpr = *$1;
222                 aexpr->Evaluate(m_ModuleScope);
223                 delete $1;
224         }
225         ;
226
227 include: T_INCLUDE rterm
228         {
229                 AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$2);
230                 delete $2;
231
232                 context->HandleInclude(aexpr->Evaluate(m_ModuleScope), false, DebugInfoRange(@1, @2));
233         }
234         | T_INCLUDE T_STRING_ANGLE
235         {
236                 context->HandleInclude($2, true, DebugInfoRange(@1, @2));
237                 free($2);
238         }
239         ;
240
241 include_recursive: T_INCLUDE_RECURSIVE rterm
242         {
243                 AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$2);
244                 delete $2;
245
246                 context->HandleIncludeRecursive(aexpr->Evaluate(m_ModuleScope), "*.conf", DebugInfoRange(@1, @2));
247         }
248         | T_INCLUDE_RECURSIVE rterm rterm
249         {
250                 AExpression::Ptr aexpr1 = static_cast<AExpression::Ptr>(*$2);
251                 delete $2;
252
253                 AExpression::Ptr aexpr2 = static_cast<AExpression::Ptr>(*$3);
254                 delete $3;
255
256                 context->HandleIncludeRecursive(aexpr1->Evaluate(m_ModuleScope), aexpr2->Evaluate(m_ModuleScope), DebugInfoRange(@1, @3));
257         }
258         ;
259
260 library: T_LIBRARY T_STRING
261         {
262                 context->HandleLibrary($2);
263                 free($2);
264         }
265         ;
266
267 variable: variable_decl identifier T_SET rterm
268         {
269                 AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$4);
270                 delete $4;
271
272                 ScriptVariable::Ptr sv = ScriptVariable::Set($2, aexpr->Evaluate(m_ModuleScope));
273                 sv->SetConstant(true);
274
275                 free($2);
276         }
277         ;
278
279 variable_decl: T_VAR
280         {
281                 $$ = true;
282         }
283         | T_CONST
284         {
285                 $$ = false;
286         }
287         ;
288
289 identifier: T_IDENTIFIER
290         | T_STRING
291         {
292                 $$ = $1;
293         }
294         ;
295
296 type: partial_specifier T_TYPE identifier
297         {
298                 String name = String($3);
299                 free($3);
300
301                 m_Type = ConfigType::GetByName(name);
302
303                 if (!m_Type) {
304                         if ($1)
305                                 BOOST_THROW_EXCEPTION(ConfigError("Partial type definition for unknown type '" + name + "'") << errinfo_debuginfo(DebugInfoRange(@1, @3)));
306
307                         m_Type = make_shared<ConfigType>(name, DebugInfoRange(@1, @3));
308                         m_Type->Register();
309                 }
310         }
311         type_inherits_specifier typerulelist
312         {
313                 TypeRuleList::Ptr ruleList = *$6;
314                 m_Type->GetRuleList()->AddRules(ruleList);
315                 m_Type->GetRuleList()->AddRequires(ruleList);
316
317                 String validator = ruleList->GetValidator();
318                 if (!validator.IsEmpty())
319                         m_Type->GetRuleList()->SetValidator(validator);
320
321                 delete $6;
322         }
323         ;
324
325 partial_specifier: /* Empty */
326         {
327                 $$ = 0;
328         }
329         | T_PARTIAL
330         {
331                 $$ = 1;
332         }
333         ;
334
335 typerulelist: '{'
336         {
337                 m_RuleLists.push(make_shared<TypeRuleList>());
338         }
339         typerules
340         '}'
341         {
342                 $$ = new Value(m_RuleLists.top());
343                 m_RuleLists.pop();
344         }
345         ;
346
347 typerules: typerules_inner
348         | typerules_inner ','
349
350 typerules_inner: /* empty */
351         | typerule
352         | typerules_inner ',' typerule
353         ;
354
355 typerule: T_REQUIRE T_STRING
356         {
357                 m_RuleLists.top()->AddRequire($2);
358                 free($2);
359         }
360         | T_VALIDATOR T_STRING
361         {
362                 m_RuleLists.top()->SetValidator($2);
363                 free($2);
364         }
365         | T_ATTRIBUTE type T_STRING
366         {
367                 TypeRule rule($2, String(), $3, TypeRuleList::Ptr(), DebugInfoRange(@1, @3));
368                 free($3);
369
370                 m_RuleLists.top()->AddRule(rule);
371         }
372         | T_ATTRIBUTE T_TYPE_NAME '(' identifier ')' T_STRING
373         {
374                 TypeRule rule($2, $4, $6, TypeRuleList::Ptr(), DebugInfoRange(@1, @6));
375                 free($4);
376                 free($6);
377
378                 m_RuleLists.top()->AddRule(rule);
379         }
380         | T_ATTRIBUTE type T_STRING typerulelist
381         {
382                 TypeRule rule($2, String(), $3, *$4, DebugInfoRange(@1, @4));
383                 free($3);
384                 delete $4;
385                 m_RuleLists.top()->AddRule(rule);
386         }
387         ;
388
389 type_inherits_specifier: /* empty */
390         | T_INHERITS identifier
391         {
392                 m_Type->SetParent($2);
393                 free($2);
394         }
395         ;
396
397 type: T_TYPE_DICTIONARY
398         | T_TYPE_ARRAY
399         | T_TYPE_NUMBER
400         | T_TYPE_STRING
401         | T_TYPE_SCALAR
402         | T_TYPE_ANY
403         | T_TYPE_NAME
404         {
405                 $$ = $1;
406         }
407         ;
408
409 object:
410         {
411                 m_Abstract = false;
412         }
413         object_declaration identifier rterm rterm_scope
414         {
415                 DebugInfo di = DebugInfoRange(@2, @5);
416                 ConfigItemBuilder::Ptr item = make_shared<ConfigItemBuilder>(di);
417
418                 AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$4);
419                 delete $4;
420                 
421                 String name = aexpr->Evaluate(m_ModuleScope);
422
423                 ConfigItem::Ptr oldItem = ConfigItem::GetObject($3, name);
424
425                 if (oldItem) {
426                         std::ostringstream msgbuf;
427                         msgbuf << "Object '" << name << "' of type '" << $3 << "' re-defined: " << di << "; previous definition: " << oldItem->GetDebugInfo();
428                         free($3);
429                         delete $5;
430                         BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(di));
431                 }
432
433                 item->SetType($3);
434
435                 if (name.FindFirstOf("!") != String::NPos) {
436                         std::ostringstream msgbuf;
437                         msgbuf << "Name for object '" << name << "' of type '" << $3 << "' is invalid: Object names may not contain '!'";
438                         free($3);
439                         BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(@4));
440                 }
441
442                 free($3);
443
444                 item->SetName(name);
445
446                 AExpression::Ptr exprl = static_cast<AExpression::Ptr>(*$5);
447                 delete $5;
448
449                 exprl->MakeInline();
450                 item->AddExpression(exprl);
451
452                 item->SetAbstract(m_Abstract);
453
454                 item->SetScope(m_ModuleScope);
455
456                 item->Compile()->Register();
457                 item.reset();
458         }
459         ;
460
461 object_declaration: T_OBJECT
462         | T_TEMPLATE
463         {
464                 m_Abstract = true;
465         }
466
467 lbinary_op: T_SET
468         | T_SET_PLUS
469         | T_SET_MINUS
470         | T_SET_MULTIPLY
471         | T_SET_DIVIDE
472         {
473                 $$ = $1;
474         }
475         ;
476
477 comma_or_semicolon: ',' | ';'
478         ;
479
480 lterm_items: lterm_items_inner
481         {
482                 $$ = $1;
483         }
484         | lterm_items_inner comma_or_semicolon
485         {
486                 $$ = $1;
487         }
488
489 lterm_items_inner: /* empty */
490         {
491                 $$ = new Array();
492         }
493         | lterm
494         {
495                 $$ = new Array();
496                 $$->Add(*$1);
497                 delete $1;
498         }
499         | lterm_items_inner comma_or_semicolon lterm
500         {
501                 if ($1)
502                         $$ = $1;
503                 else
504                         $$ = new Array();
505
506                 $$->Add(*$3);
507                 delete $3;
508         }
509         ;
510
511 lterm: identifier lbinary_op rterm
512         {
513                 AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$3);
514                 $$ = new Value(make_shared<AExpression>($2, $1, aexpr, DebugInfoRange(@1, @3)));
515                 free($1);
516                 delete $3;
517         }
518         | identifier '[' T_STRING ']' lbinary_op rterm
519         {
520                 AExpression::Ptr subexpr = make_shared<AExpression>($5, $3, static_cast<AExpression::Ptr>(*$6), DebugInfoRange(@1, @6));
521                 free($3);
522                 delete $6;
523
524                 Array::Ptr subexprl = make_shared<Array>();
525                 subexprl->Add(subexpr);
526                 
527                 AExpression::Ptr expr = make_shared<AExpression>(&AExpression::OpDict, subexprl, DebugInfoRange(@1, @6));
528                 $$ = new Value(make_shared<AExpression>(&AExpression::OpSetPlus, $1, expr, DebugInfoRange(@1, @6)));
529                 free($1);
530         }
531         | rterm
532         {
533                 $$ = $1;
534         }
535         ;
536
537 rbinary_op: T_PLUS
538         | T_MINUS
539         | T_MULTIPLY
540         | T_DIVIDE_OP
541         | T_BINARY_AND
542         | T_BINARY_OR
543         | T_LESS_THAN
544         | T_GREATER_THAN
545         | T_LESS_THAN_OR_EQUAL
546         | T_GREATER_THAN_OR_EQUAL
547         | T_EQUAL
548         | T_NOT_EQUAL
549         | T_IN
550         | T_NOT_IN
551         | T_LOGICAL_AND
552         | T_LOGICAL_OR
553         | T_SHIFT_LEFT
554         | T_SHIFT_RIGHT
555         {
556                 $$ = $1;
557         }
558         ;
559         
560 rterm_items: rterm_items_inner
561         {
562                 $$ = $1;
563         }
564         | rterm_items_inner ','
565         {
566                 $$ = $1;
567         }
568         ;
569
570 rterm_items_inner: /* empty */
571         {
572                 $$ = new Array();
573         }
574         | rterm
575         {
576                 $$ = new Array();
577                 $$->Add(*$1);
578                 delete $1;
579         }
580         | rterm_items_inner ',' rterm
581         {
582                 if ($1)
583                         $$ = $1;
584                 else
585                         $$ = new Array();
586
587                 $$->Add(*$3);
588                 delete $3;
589         }
590         ;
591
592 rbinary_op: '+'
593         {
594                 $$ = &AExpression::OpAdd;
595         }
596         ;
597
598 rterm_scope: '{' lterm_items '}'
599         {
600                 $$ = new Value(make_shared<AExpression>(&AExpression::OpDict, Array::Ptr($2), DebugInfoRange(@1, @3)));
601         }
602         ;
603
604 rterm: T_STRING
605         {
606                 $$ = new Value(make_shared<AExpression>(&AExpression::OpLiteral, $1, @1));
607                 free($1);
608         }
609         | T_NUMBER
610         {
611                 $$ = new Value(make_shared<AExpression>(&AExpression::OpLiteral, $1, @1));
612         }
613         | T_NULL
614         {
615                 $$ = new Value(make_shared<AExpression>(&AExpression::OpLiteral, Empty, @1));
616         }
617         | T_IDENTIFIER '(' rterm_items ')'
618         {
619                 Array::Ptr arguments = Array::Ptr($3);
620                 $$ = new Value(make_shared<AExpression>(&AExpression::OpFunctionCall, $1, make_shared<AExpression>(&AExpression::OpLiteral, arguments, @3), DebugInfoRange(@1, @4)));
621                 free($1);
622         }
623         | T_IMPORT rterm
624         {
625                 AExpression::Ptr avar = make_shared<AExpression>(&AExpression::OpVariable, "__type", DebugInfoRange(@1, @2));
626                 AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$2);
627                 delete $2;
628                 $$ = new Value(make_shared<AExpression>(&AExpression::OpImport, avar, aexpr, DebugInfoRange(@1, @2)));
629         }
630         | T_IDENTIFIER
631         {
632                 $$ = new Value(make_shared<AExpression>(&AExpression::OpVariable, $1, @1));
633                 free($1);
634         }
635         | '!' rterm
636         {
637                 $$ = new Value(make_shared<AExpression>(&AExpression::OpNegate, static_cast<AExpression::Ptr>(*$2), DebugInfoRange(@1, @2)));
638                 delete $2;
639         }
640         | '~' rterm
641         {
642                 $$ = new Value(make_shared<AExpression>(&AExpression::OpNegate, static_cast<AExpression::Ptr>(*$2), DebugInfoRange(@1, @2)));
643                 delete $2;
644         }
645         | identifier '[' T_STRING ']'
646         {
647                 $$ = new Value(make_shared<AExpression>(&AExpression::OpIndexer, $1, $3, DebugInfoRange(@1, @4)));
648                 free($1);
649                 free($3);
650         }
651         | '[' rterm_items ']'
652         {
653                 $$ = new Value(make_shared<AExpression>(&AExpression::OpArray, Array::Ptr($2), DebugInfoRange(@1, @3)));
654         }
655         | rterm_scope
656         {
657                 $$ = $1;
658         }
659         | '(' rterm ')'
660         {
661                 $$ = $2;
662         }
663         | rterm rbinary_op rterm
664         {
665                 $$ = new Value(make_shared<AExpression>($2, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), DebugInfoRange(@1, @3)));
666                 delete $1;
667                 delete $3;
668         }
669         ;
670
671 optional_template: /* empty */
672         | T_TEMPLATE
673         ;
674
675 apply: T_APPLY optional_template identifier identifier T_TO identifier T_WHERE rterm
676         {
677                 if (!ApplyRule::IsValidCombination($3, $6)) {
678                         BOOST_THROW_EXCEPTION(ConfigError("'apply' cannot be used with types '" + String($3) + "' and '" + String($6) + "'.") << errinfo_debuginfo(@1));
679                 }
680
681                 Array::Ptr arguments = make_shared<Array>();
682                 arguments->Add(*$8);
683                 delete $8;
684
685                 AExpression::Ptr aexpr = make_shared<AExpression>(&AExpression::OpFunctionCall, "bool", make_shared<AExpression>(&AExpression::OpLiteral, arguments, @8), @8);
686
687                 ApplyRule::AddRule($3, $4, $6, aexpr, DebugInfoRange(@1, @8), m_ModuleScope);
688         }
689 %%