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