]> granicus.if.org Git - icinga2/blob - lib/config/config_parser.yy
Implement support for arrays in custom variables
[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 "config/i2-config.hpp"
24 #include "config/configitembuilder.hpp"
25 #include "config/configtype.hpp"
26 #include "config/configcompiler.hpp"
27 #include "config/configcompilercontext.hpp"
28 #include "config/typerule.hpp"
29 #include "config/typerulelist.hpp"
30 #include "config/expression.hpp"
31 #include "config/applyrule.hpp"
32 #include "config/objectrule.hpp"
33 #include "base/value.hpp"
34 #include "base/utility.hpp"
35 #include "base/array.hpp"
36 #include "base/scriptvariable.hpp"
37 #include "base/exception.hpp"
38 #include "base/dynamictype.hpp"
39 #include "base/configerror.hpp"
40 #include <sstream>
41 #include <stack>
42 #include <boost/foreach.hpp>
43
44 #define YYLTYPE icinga::DebugInfo
45 #define YYERROR_VERBOSE
46
47 #define YYLLOC_DEFAULT(Current, Rhs, N)                                 \
48 do {                                                                    \
49         if (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;     \
55         } else {                                                        \
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;                            \
61         }                                                               \
62 } while (0)
63
64 #define YY_LOCATION_PRINT(file, loc)                    \
65 do {                                                    \
66         std::ostringstream msgbuf;                      \
67         msgbuf << loc;                                  \
68         std::string str = msgbuf.str();                 \
69         fputs(str.c_str(), file);                       \
70 } while (0)
71
72 using namespace icinga;
73
74 int ignore_newlines = 0;
75
76 static void MakeRBinaryOp(Value** result, Expression::OpCallback& op, Value *left, Value *right, DebugInfo& diLeft, DebugInfo& diRight)
77 {
78         *result = new Value(make_shared<Expression>(op, *left, *right, DebugInfoRange(diLeft, diRight)));
79         delete left;
80         delete right;
81 }
82
83 %}
84
85 %pure-parser
86
87 %locations
88 %defines
89 %error-verbose
90
91 %parse-param { ConfigCompiler *context }
92 %lex-param { void *scanner }
93
94 %union {
95         char *text;
96         double num;
97         icinga::Value *variant;
98         icinga::Expression::OpCallback op;
99         icinga::TypeSpecifier type;
100         std::vector<String> *slist;
101         Array *array;
102 }
103
104 %token T_NEWLINE "new-line"
105 %token <text> T_STRING
106 %token <text> T_STRING_ANGLE
107 %token <num> T_NUMBER
108 %token T_NULL
109 %token <text> T_IDENTIFIER
110
111 %token <op> T_SET "= (T_SET)"
112 %token <op> T_SET_PLUS "+= (T_SET_PLUS)"
113 %token <op> T_SET_MINUS "-= (T_SET_MINUS)"
114 %token <op> T_SET_MULTIPLY "*= (T_SET_MULTIPLY)"
115 %token <op> T_SET_DIVIDE "/= (T_SET_DIVIDE)"
116
117 %token <op> T_SHIFT_LEFT "<< (T_SHIFT_LEFT)"
118 %token <op> T_SHIFT_RIGHT ">> (T_SHIFT_RIGHT)"
119 %token <op> T_EQUAL "== (T_EQUAL)"
120 %token <op> T_NOT_EQUAL "!= (T_NOT_EQUAL)"
121 %token <op> T_IN "in (T_IN)"
122 %token <op> T_NOT_IN "!in (T_NOT_IN)"
123 %token <op> T_LOGICAL_AND "&& (T_LOGICAL_AND)"
124 %token <op> T_LOGICAL_OR "|| (T_LOGICAL_OR)"
125 %token <op> T_LESS_THAN_OR_EQUAL "<= (T_LESS_THAN_OR_EQUAL)"
126 %token <op> T_GREATER_THAN_OR_EQUAL ">= (T_GREATER_THAN_OR_EQUAL)"
127 %token <op> T_PLUS "+ (T_PLUS)"
128 %token <op> T_MINUS "- (T_MINUS)"
129 %token <op> T_MULTIPLY "* (T_MULTIPLY)"
130 %token <op> T_DIVIDE_OP "/ (T_DIVIDE_OP)"
131 %token <op> T_BINARY_AND "& (T_BINARY_AND)"
132 %token <op> T_BINARY_OR "| (T_BINARY_OR)"
133 %token <op> T_LESS_THAN "< (T_LESS_THAN)"
134 %token <op> T_GREATER_THAN "> (T_GREATER_THAN)"
135
136 %token T_CONST "const (T_CONST)"
137 %token <type> T_TYPE_DICTIONARY "dictionary (T_TYPE_DICTIONARY)"
138 %token <type> T_TYPE_ARRAY "array (T_TYPE_ARRAY)"
139 %token <type> T_TYPE_NUMBER "number (T_TYPE_NUMBER)"
140 %token <type> T_TYPE_STRING "string (T_TYPE_STRING)"
141 %token <type> T_TYPE_SCALAR "scalar (T_TYPE_SCALAR)"
142 %token <type> T_TYPE_ANY "any (T_TYPE_ANY)"
143 %token <type> T_TYPE_NAME "name (T_TYPE_NAME)"
144 %token T_VALIDATOR "%validator (T_VALIDATOR)"
145 %token T_REQUIRE "%require (T_REQUIRE)"
146 %token T_ATTRIBUTE "%attribute (T_ATTRIBUTE)"
147 %token T_TYPE "type (T_TYPE)"
148 %token T_OBJECT "object (T_OBJECT)"
149 %token T_TEMPLATE "template (T_TEMPLATE)"
150 %token T_INCLUDE "include (T_INCLUDE)"
151 %token T_INCLUDE_RECURSIVE "include_recursive (T_INCLUDE_RECURSIVE)"
152 %token T_LIBRARY "library (T_LIBRARY)"
153 %token T_INHERITS "inherits (T_INHERITS)"
154 %token T_APPLY "apply (T_APPLY)"
155 %token T_TO "to (T_TO)"
156 %token T_WHERE "where (T_WHERE)"
157 %token T_IMPORT "import (T_IMPORT)"
158 %token T_ASSIGN "assign (T_ASSIGN)"
159 %token T_IGNORE "ignore (T_IGNORE)"
160 %token T_FUNCTION "function (T_FUNCTION)"
161 %token T_RETURN "return (T_RETURN)"
162 %token T_FOR "for (T_FOR)"
163
164 %type <text> identifier
165 %type <array> rterm_items
166 %type <array> rterm_items_inner
167 %type <array> identifier_items
168 %type <array> identifier_items_inner
169 %type <array> lterm_items
170 %type <array> lterm_items_inner
171 %type <variant> typerulelist
172 %type <op> lbinary_op
173 %type <type> type
174 %type <variant> rterm
175 %type <variant> rterm_array
176 %type <variant> rterm_scope
177 %type <variant> lterm
178 %type <variant> object
179 %type <variant> apply
180 %type <text> target_type_specifier
181
182 %left T_LOGICAL_OR
183 %left T_LOGICAL_AND
184 %left T_BINARY_OR
185 %left T_BINARY_AND
186 %left T_IN
187 %left T_NOT_IN
188 %left T_EQUAL T_NOT_EQUAL
189 %left T_LESS_THAN T_LESS_THAN_OR_EQUAL T_GREATER_THAN T_GREATER_THAN_OR_EQUAL
190 %left T_SHIFT_LEFT T_SHIFT_RIGHT
191 %left T_PLUS T_MINUS
192 %left T_MULTIPLY T_DIVIDE_OP
193 %right '!' '~'
194 %left '.' '(' '['
195 %right ':'
196 %{
197
198 int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
199
200 void yyerror(YYLTYPE *locp, ConfigCompiler *, const char *err)
201 {
202         std::ostringstream message;
203         message << *locp << ": " << err;
204         ConfigCompilerContext::GetInstance()->AddMessage(true, message.str(), *locp);
205 }
206
207 int yyparse(ConfigCompiler *context);
208
209 static std::stack<bool> m_Abstract;
210
211 static std::stack<TypeRuleList::Ptr> m_RuleLists;
212 static ConfigType::Ptr m_Type;
213
214 static Dictionary::Ptr m_ModuleScope;
215
216 static std::stack<bool> m_Apply;
217 static std::stack<bool> m_ObjectAssign;
218 static std::stack<bool> m_SeenAssign;
219 static std::stack<Expression::Ptr> m_Assign;
220 static std::stack<Expression::Ptr> m_Ignore;
221 static std::stack<String> m_FVar;
222 static std::stack<Expression::Ptr> m_FTerm;
223
224 void ConfigCompiler::Compile(void)
225 {
226         m_ModuleScope = make_shared<Dictionary>();
227
228         m_Abstract = std::stack<bool>();
229         m_RuleLists = std::stack<TypeRuleList::Ptr>();
230         m_Type.reset();
231         m_Apply = std::stack<bool>();
232         m_ObjectAssign = std::stack<bool>();
233         m_SeenAssign = std::stack<bool>();
234         m_Assign = std::stack<Expression::Ptr>();
235         m_Ignore = std::stack<Expression::Ptr>();
236         m_FVar = std::stack<String>();
237         m_FTerm = std::stack<Expression::Ptr>();
238
239         try {
240                 yyparse(this);
241         } catch (const ConfigError& ex) {
242                 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
243                 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
244         } catch (const std::exception& ex) {
245                 ConfigCompilerContext::GetInstance()->AddMessage(true, DiagnosticInformation(ex));
246         }
247 }
248
249 #define scanner (context->GetScanner())
250
251 %}
252
253 %%
254 statements: /* empty */
255         | statements statement
256         ;
257
258 statement: type | include | include_recursive | library | constant
259         { }
260         | newlines
261         { }
262         | lterm
263         {
264                 Expression::Ptr aexpr = *$1;
265                 aexpr->Evaluate(m_ModuleScope);
266                 delete $1;
267         }
268         ;
269
270 include: T_INCLUDE rterm sep
271         {
272                 Expression::Ptr aexpr = *$2;
273                 delete $2;
274
275                 context->HandleInclude(aexpr->Evaluate(m_ModuleScope), false, DebugInfoRange(@1, @2));
276         }
277         | T_INCLUDE T_STRING_ANGLE
278         {
279                 context->HandleInclude($2, true, DebugInfoRange(@1, @2));
280                 free($2);
281         }
282         ;
283
284 include_recursive: T_INCLUDE_RECURSIVE rterm
285         {
286                 Expression::Ptr aexpr = *$2;
287                 delete $2;
288
289                 context->HandleIncludeRecursive(aexpr->Evaluate(m_ModuleScope), "*.conf", DebugInfoRange(@1, @2));
290         }
291         | T_INCLUDE_RECURSIVE rterm ',' rterm
292         {
293                 Expression::Ptr aexpr1 = *$2;
294                 delete $2;
295
296                 Expression::Ptr aexpr2 = *$4;
297                 delete $4;
298
299                 context->HandleIncludeRecursive(aexpr1->Evaluate(m_ModuleScope), aexpr2->Evaluate(m_ModuleScope), DebugInfoRange(@1, @4));
300         }
301         ;
302
303 library: T_LIBRARY T_STRING sep
304         {
305                 context->HandleLibrary($2);
306                 free($2);
307         }
308         ;
309
310 constant: T_CONST identifier T_SET rterm sep
311         {
312                 Expression::Ptr aexpr = *$4;
313                 delete $4;
314
315                 ScriptVariable::Ptr sv = ScriptVariable::Set($2, aexpr->Evaluate(m_ModuleScope));
316                 sv->SetConstant(true);
317
318                 free($2);
319         }
320         ;
321
322 identifier: T_IDENTIFIER
323         | T_STRING
324         {
325                 $$ = $1;
326         }
327         ;
328
329 type: T_TYPE identifier
330         {
331                 String name = String($2);
332                 free($2);
333
334                 m_Type = ConfigType::GetByName(name);
335
336                 if (!m_Type) {
337                         m_Type = make_shared<ConfigType>(name, DebugInfoRange(@1, @2));
338                         m_Type->Register();
339                 }
340         }
341         type_inherits_specifier typerulelist sep
342         {
343                 TypeRuleList::Ptr ruleList = *$5;
344                 delete $5;
345
346                 m_Type->GetRuleList()->AddRules(ruleList);
347                 m_Type->GetRuleList()->AddRequires(ruleList);
348
349                 String validator = ruleList->GetValidator();
350                 if (!validator.IsEmpty())
351                         m_Type->GetRuleList()->SetValidator(validator);
352         }
353         ;
354
355 typerulelist: '{'
356         {
357                 m_RuleLists.push(make_shared<TypeRuleList>());
358         }
359         typerules
360         '}'
361         {
362                 $$ = new Value(m_RuleLists.top());
363                 m_RuleLists.pop();
364         }
365         ;
366
367 typerules: typerules_inner
368         | typerules_inner sep
369
370 typerules_inner: /* empty */
371         | typerule
372         | typerules_inner sep typerule
373         ;
374
375 typerule: T_REQUIRE T_STRING
376         {
377                 m_RuleLists.top()->AddRequire($2);
378                 free($2);
379         }
380         | T_VALIDATOR T_STRING
381         {
382                 m_RuleLists.top()->SetValidator($2);
383                 free($2);
384         }
385         | T_ATTRIBUTE type T_STRING
386         {
387                 TypeRule rule($2, String(), $3, TypeRuleList::Ptr(), DebugInfoRange(@1, @3));
388                 free($3);
389
390                 m_RuleLists.top()->AddRule(rule);
391         }
392         | T_ATTRIBUTE T_TYPE_NAME '(' identifier ')' T_STRING
393         {
394                 TypeRule rule($2, $4, $6, TypeRuleList::Ptr(), DebugInfoRange(@1, @6));
395                 free($4);
396                 free($6);
397
398                 m_RuleLists.top()->AddRule(rule);
399         }
400         | T_ATTRIBUTE type T_STRING typerulelist
401         {
402                 TypeRule rule($2, String(), $3, *$4, DebugInfoRange(@1, @4));
403                 free($3);
404                 delete $4;
405                 m_RuleLists.top()->AddRule(rule);
406         }
407         ;
408
409 type_inherits_specifier: /* empty */
410         | T_INHERITS identifier
411         {
412                 m_Type->SetParent($2);
413                 free($2);
414         }
415         ;
416
417 type: T_TYPE_DICTIONARY
418         | T_TYPE_ARRAY
419         | T_TYPE_NUMBER
420         | T_TYPE_STRING
421         | T_TYPE_SCALAR
422         | T_TYPE_ANY
423         | T_TYPE_NAME
424         {
425                 $$ = $1;
426         }
427         ;
428
429 object:
430         {
431                 m_Abstract.push(false);
432                 m_ObjectAssign.push(true);
433                 m_SeenAssign.push(false);
434                 m_Assign.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
435                 m_Ignore.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
436         }
437         object_declaration identifier rterm rterm_scope
438         {
439                 m_ObjectAssign.pop();
440
441                 Array::Ptr args = make_shared<Array>();
442                 
443                 args->Add(m_Abstract.top());
444                 m_Abstract.pop();
445
446                 String type = $3;
447                 args->Add(type);
448                 free($3);
449
450                 args->Add(*$4);
451                 delete $4;
452
453                 Expression::Ptr exprl = *$5;
454                 delete $5;
455                 exprl->MakeInline();
456
457                 if (m_SeenAssign.top() && !ObjectRule::IsValidSourceType(type))
458                         BOOST_THROW_EXCEPTION(ConfigError("object rule 'assign' cannot be used for type '" + type + "'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
459
460                 m_SeenAssign.pop();
461
462                 Expression::Ptr rex = make_shared<Expression>(&Expression::OpLogicalNegate, m_Ignore.top(), DebugInfoRange(@2, @5));
463                 m_Ignore.pop();
464
465                 Expression::Ptr filter = make_shared<Expression>(&Expression::OpLogicalAnd, m_Assign.top(), rex, DebugInfoRange(@2, @5));
466                 m_Assign.pop();
467
468                 args->Add(filter);
469
470                 args->Add(context->GetZone());
471
472                 $$ = new Value(make_shared<Expression>(&Expression::OpObject, args, exprl, DebugInfoRange(@2, @5)));
473         }
474         ;
475
476 object_declaration: T_OBJECT
477         | T_TEMPLATE
478         {
479                 m_Abstract.top() = true;
480         }
481
482 identifier_items: identifier_items_inner
483         {
484                 $$ = $1;
485         }
486         | identifier_items_inner ','
487         {
488                 $$ = $1;
489         }
490         ;
491
492 identifier_items_inner: /* empty */
493         {
494                 $$ = new Array();
495         }
496         | identifier
497         {
498                 $$ = new Array();
499                 $$->Add($1);
500                 free($1);
501         }
502         | identifier_items_inner ',' identifier
503         {
504                 if ($1)
505                         $$ = $1;
506                 else
507                         $$ = new Array();
508
509                 $$->Add($3);
510                 free($3);
511         }
512         ;
513
514 lbinary_op: T_SET
515         | T_SET_PLUS
516         | T_SET_MINUS
517         | T_SET_MULTIPLY
518         | T_SET_DIVIDE
519         {
520                 $$ = $1;
521         }
522         ;
523
524 lterm_items: /* empty */
525         {
526                 $$ = new Array();
527         }
528         | lterm_items_inner
529         {
530                 $$ = $1;
531         }
532         | lterm_items_inner sep
533         {
534                 $$ = $1;
535         }
536
537 lterm_items_inner: lterm
538         {
539                 $$ = new Array();
540                 $$->Add(*$1);
541                 delete $1;
542         }
543         | lterm_items_inner sep lterm
544         {
545                 if ($1)
546                         $$ = $1;
547                 else
548                         $$ = new Array();
549
550                 $$->Add(*$3);
551                 delete $3;
552         }
553         ;
554
555 lterm: identifier lbinary_op rterm
556         {
557                 Expression::Ptr aindex = make_shared<Expression>(&Expression::OpLiteral, $1, @1);
558                 free($1);
559
560                 $$ = new Value(make_shared<Expression>($2, aindex, *$3, DebugInfoRange(@1, @3)));
561                 delete $3;
562         }
563         | identifier '[' rterm ']' lbinary_op rterm
564         {
565                 Expression::Ptr subexpr = make_shared<Expression>($5, *$3, *$6, DebugInfoRange(@1, @6));
566                 delete $3;
567                 delete $6;
568
569                 Array::Ptr subexprl = make_shared<Array>();
570                 subexprl->Add(subexpr);
571                 
572                 Expression::Ptr aindex = make_shared<Expression>(&Expression::OpLiteral, $1, @1);
573                 free($1);
574
575                 Expression::Ptr expr = make_shared<Expression>(&Expression::OpDict, subexprl, DebugInfoRange(@1, @6));
576                 $$ = new Value(make_shared<Expression>(&Expression::OpSetPlus, aindex, expr, DebugInfoRange(@1, @6)));
577         }
578         | identifier '.' T_IDENTIFIER lbinary_op rterm
579         {
580                 Expression::Ptr aindex = make_shared<Expression>(&Expression::OpLiteral, $3, @3);
581                 Expression::Ptr subexpr = make_shared<Expression>($4, aindex, *$5, DebugInfoRange(@1, @5));
582                 free($3);
583                 delete $5;
584
585                 Array::Ptr subexprl = make_shared<Array>();
586                 subexprl->Add(subexpr);
587
588                 Expression::Ptr aindexl = make_shared<Expression>(&Expression::OpLiteral, $1, @1);
589                 free($1);
590
591                 Expression::Ptr expr = make_shared<Expression>(&Expression::OpDict, subexprl, DebugInfoRange(@1, @5));
592                 $$ = new Value(make_shared<Expression>(&Expression::OpSetPlus, aindexl, expr, DebugInfoRange(@1, @5)));
593         }
594         | T_IMPORT rterm
595         {
596                 Expression::Ptr avar = make_shared<Expression>(&Expression::OpVariable, "type", DebugInfoRange(@1, @2));
597                 $$ = new Value(make_shared<Expression>(&Expression::OpImport, avar, *$2, DebugInfoRange(@1, @2)));
598                 delete $2;
599         }
600         | T_ASSIGN T_WHERE rterm
601         {
602                 if ((m_Apply.empty() || !m_Apply.top()) && (m_ObjectAssign.empty() || !m_ObjectAssign.top()))
603                         BOOST_THROW_EXCEPTION(ConfigError("'assign' keyword not valid in this context."));
604
605                 m_SeenAssign.top() = true;
606
607                 m_Assign.top() = make_shared<Expression>(&Expression::OpLogicalOr, m_Assign.top(), *$3, DebugInfoRange(@1, @3));
608                 delete $3;
609
610                 $$ = new Value(make_shared<Expression>(&Expression::OpLiteral, Empty, DebugInfoRange(@1, @3)));
611         }
612         | T_IGNORE T_WHERE rterm
613         {
614                 if ((m_Apply.empty() || !m_Apply.top()) && (m_ObjectAssign.empty() || !m_ObjectAssign.top()))
615                         BOOST_THROW_EXCEPTION(ConfigError("'ignore' keyword not valid in this context."));
616
617                 m_Ignore.top() = make_shared<Expression>(&Expression::OpLogicalOr, m_Ignore.top(), *$3, DebugInfoRange(@1, @3));
618                 delete $3;
619
620                 $$ = new Value(make_shared<Expression>(&Expression::OpLiteral, Empty, DebugInfoRange(@1, @3)));
621         }
622         | T_RETURN rterm
623         {
624                 Expression::Ptr aname = make_shared<Expression>(&Expression::OpLiteral, "__result", @1);
625                 $$ = new Value(make_shared<Expression>(&Expression::OpSet, aname, *$2, DebugInfoRange(@1, @2)));
626                 delete $2;
627
628         }
629         | apply
630         {
631                 $$ = $1;
632         }
633         | object
634         {
635                 $$ = $1;
636         }
637         | rterm
638         {
639                 $$ = $1;
640         }
641         ;
642         
643 rterm_items: /* empty */
644         {
645                 $$ = new Array();
646         }
647         | rterm_items_inner
648         {
649                 $$ = $1;
650         }
651         | rterm_items_inner arraysep
652         {
653                 $$ = $1;
654         }
655         ;
656
657 rterm_items_inner: rterm
658         {
659                 $$ = new Array();
660                 $$->Add(*$1);
661                 delete $1;
662         }
663         | rterm_items_inner arraysep rterm
664         {
665                 $$ = $1;
666                 $$->Add(*$3);
667                 delete $3;
668         }
669         ;
670
671 rterm_array: '[' newlines rterm_items newlines ']'
672         {
673                 $$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($3), DebugInfoRange(@1, @5)));
674         }
675         | '[' newlines rterm_items ']'
676         {
677                 $$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($3), DebugInfoRange(@1, @4)));
678         }
679         | '[' rterm_items newlines ']'
680         {
681                 $$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($2), DebugInfoRange(@1, @4)));
682         }
683         | '[' rterm_items ']'
684         {
685                 $$ = new Value(make_shared<Expression>(&Expression::OpArray, Array::Ptr($2), DebugInfoRange(@1, @3)));
686         }
687         ;
688
689 rterm_scope: '{' newlines lterm_items newlines '}'
690         {
691                 $$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($3), DebugInfoRange(@1, @5)));
692         }
693         | '{' newlines lterm_items '}'
694         {
695                 $$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($3), DebugInfoRange(@1, @4)));
696         }
697         | '{' lterm_items newlines '}'
698         {
699                 $$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($2), DebugInfoRange(@1, @4)));
700         }
701         | '{' lterm_items '}'
702         {
703                 $$ = new Value(make_shared<Expression>(&Expression::OpDict, Array::Ptr($2), DebugInfoRange(@1, @3)));
704         }
705         ;
706
707 rterm: T_STRING
708         {
709                 $$ = new Value(make_shared<Expression>(&Expression::OpLiteral, $1, @1));
710                 free($1);
711         }
712         | T_NUMBER
713         {
714                 $$ = new Value(make_shared<Expression>(&Expression::OpLiteral, $1, @1));
715         }
716         | T_NULL
717         {
718                 $$ = new Value(make_shared<Expression>(&Expression::OpLiteral, Empty, @1));
719         }
720         | rterm '.' T_IDENTIFIER
721         {
722                 $$ = new Value(make_shared<Expression>(&Expression::OpIndexer, *$1, make_shared<Expression>(&Expression::OpLiteral, $3, @3), DebugInfoRange(@1, @3)));
723                 delete $1;
724                 free($3);
725         }
726         | rterm '(' rterm_items ')'
727         {
728                 Array::Ptr arguments = Array::Ptr($3);
729                 $$ = new Value(make_shared<Expression>(&Expression::OpFunctionCall, *$1, make_shared<Expression>(&Expression::OpLiteral, arguments, @3), DebugInfoRange(@1, @4)));
730                 delete $1;
731         }
732         | T_IDENTIFIER
733         {
734                 $$ = new Value(make_shared<Expression>(&Expression::OpVariable, $1, @1));
735                 free($1);
736         }
737         | '!' rterm
738         {
739                 $$ = new Value(make_shared<Expression>(&Expression::OpLogicalNegate, *$2, DebugInfoRange(@1, @2)));
740                 delete $2;
741         }
742         | '~' rterm
743         {
744                 $$ = new Value(make_shared<Expression>(&Expression::OpNegate, *$2, DebugInfoRange(@1, @2)));
745                 delete $2;
746         }
747         | rterm '[' rterm ']'
748         {
749                 $$ = new Value(make_shared<Expression>(&Expression::OpIndexer, *$1, *$3, DebugInfoRange(@1, @4)));
750                 delete $1;
751                 delete $3;
752         }
753         | rterm_array
754         {
755                 $$ = $1;
756         }
757         | rterm_scope
758         {
759                 $$ = $1;
760         }
761         | '('
762         {
763                 ignore_newlines++;
764         }
765         rterm ')'
766         {
767                 ignore_newlines--;
768                 $$ = $3;
769         }
770         | rterm T_LOGICAL_OR rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
771         | rterm T_LOGICAL_AND rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
772         | rterm T_BINARY_OR rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
773         | rterm T_BINARY_AND rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
774         | rterm T_IN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
775         | rterm T_NOT_IN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
776         | rterm T_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
777         | rterm T_NOT_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
778         | rterm T_LESS_THAN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
779         | rterm T_LESS_THAN_OR_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
780         | rterm T_GREATER_THAN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
781         | rterm T_GREATER_THAN_OR_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
782         | rterm T_SHIFT_LEFT rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
783         | rterm T_SHIFT_RIGHT rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
784         | rterm T_PLUS rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
785         | rterm T_MINUS rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
786         | rterm T_MULTIPLY rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
787         | rterm T_DIVIDE_OP rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
788         | T_FUNCTION identifier '(' identifier_items ')' rterm_scope
789         {
790                 Array::Ptr arr = make_shared<Array>();
791
792                 arr->Add($2);
793                 free($2);
794
795                 Expression::Ptr aexpr = *$6;
796                 delete $6;
797                 aexpr->MakeInline();
798                 arr->Add(aexpr);
799
800                 $$ = new Value(make_shared<Expression>(&Expression::OpFunction, arr, Array::Ptr($4), DebugInfoRange(@1, @6)));
801         }
802         | T_FUNCTION '(' identifier_items ')' rterm_scope
803         {
804                 Array::Ptr arr = make_shared<Array>();
805
806                 arr->Add(Empty);
807
808                 Expression::Ptr aexpr = *$5;
809                 delete $5;
810                 aexpr->MakeInline();
811                 arr->Add(aexpr);
812
813                 $$ = new Value(make_shared<Expression>(&Expression::OpFunction, arr, Array::Ptr($3), DebugInfoRange(@1, @5)));
814         }
815         | T_FOR '(' identifier T_IN rterm ')' rterm_scope
816         {
817                 Array::Ptr arr = make_shared<Array>();
818
819                 arr->Add($3);
820                 free($3);
821
822                 Expression::Ptr aexpr = *$5;
823                 delete $5;
824                 arr->Add(aexpr);
825
826                 Expression::Ptr ascope = *$7;
827                 delete $7;
828
829                 $$ = new Value(make_shared<Expression>(&Expression::OpFor, arr, ascope, DebugInfoRange(@1, @7)));
830         }
831         ;
832
833 target_type_specifier: /* empty */
834         {
835                 $$ = strdup("");
836         }
837         | T_TO identifier
838         {
839                 $$ = $2;
840         }
841         ;
842
843 apply_for_specifier: /* empty */
844         | T_FOR '(' identifier T_IN rterm ')'
845         {
846                 m_FVar.top() = $3;
847                 free($3);
848
849                 m_FTerm.top() = *$5;
850                 delete $5;
851         }
852         ;
853
854 apply:
855         {
856                 m_Apply.push(true);
857                 m_SeenAssign.push(false);
858                 m_Assign.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
859                 m_Ignore.push(make_shared<Expression>(&Expression::OpLiteral, false, DebugInfo()));
860                 m_FVar.push("");
861                 m_FTerm.push(Expression::Ptr());
862         }
863         T_APPLY identifier rterm apply_for_specifier target_type_specifier rterm
864         {
865                 m_Apply.pop();
866
867                 String type = $3;
868                 free($3);
869                 Expression::Ptr aname = *$4;
870                 delete $4;
871                 String target = $6;
872                 free($6);
873
874                 if (!ApplyRule::IsValidSourceType(type))
875                         BOOST_THROW_EXCEPTION(ConfigError("'apply' cannot be used with type '" + type + "'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
876
877                 if (!ApplyRule::IsValidTargetType(type, target)) {
878                         if (target == "") {
879                                 std::vector<String> types = ApplyRule::GetTargetTypes(type);
880                                 String typeNames;
881
882                                 for (std::vector<String>::size_type i = 0; i < types.size(); i++) {
883                                         if (typeNames != "") {
884                                                 if (i == types.size() - 1)
885                                                         typeNames += " or ";
886                                                 else
887                                                         typeNames += ", ";
888                                         }
889
890                                         typeNames += "'" + types[i] + "'";
891                                 }
892
893                                 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)));
894                         } else
895                                 BOOST_THROW_EXCEPTION(ConfigError("'apply' target type '" + target + "' is invalid") << errinfo_debuginfo(DebugInfoRange(@2, @5)));
896                 }
897
898                 Expression::Ptr exprl = *$7;
899                 delete $7;
900
901                 exprl->MakeInline();
902
903                 // assign && !ignore
904                 if (!m_SeenAssign.top())
905                         BOOST_THROW_EXCEPTION(ConfigError("'apply' is missing 'assign'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
906
907                 m_SeenAssign.pop();
908
909                 Expression::Ptr rex = make_shared<Expression>(&Expression::OpLogicalNegate, m_Ignore.top(), DebugInfoRange(@2, @5));
910                 m_Ignore.pop();
911
912                 Expression::Ptr filter = make_shared<Expression>(&Expression::OpLogicalAnd, m_Assign.top(), rex, DebugInfoRange(@2, @5));
913                 m_Assign.pop();
914
915                 String fvar = m_FVar.top();
916                 m_FVar.pop();
917
918                 Expression::Ptr fterm = m_FTerm.top();
919                 m_FTerm.pop();
920
921                 Array::Ptr args = make_shared<Array>();
922                 args->Add(type);
923                 args->Add(target);
924                 args->Add(aname);
925                 args->Add(filter);
926                 args->Add(fvar);
927                 args->Add(fterm);
928
929                 $$ = new Value(make_shared<Expression>(&Expression::OpApply, args, exprl, DebugInfoRange(@2, @5)));
930         }
931         ;
932
933 newlines: T_NEWLINE
934         | newlines T_NEWLINE
935         ;
936
937 /* required separator */
938 sep: ',' newlines
939         | ','
940         | ';' newlines
941         | ';'
942         | newlines
943         ;
944
945 arraysep: ',' newlines
946         | ','
947         ;
948
949 %%