]> granicus.if.org Git - icinga2/blob - lib/config/config_parser.yy
Replace boost::shared_ptr with boost::intrusive_ptr
[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(new 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 = new 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 = new 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(new 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 = new 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 = new Expression(&Expression::OpLogicalNegate, m_Ignore.top(), DebugInfoRange(@2, @5));
472                 m_Ignore.pop();
473
474                 Expression::Ptr filter = new 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(new 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: identifier
524         {
525                 $$ = new Array();
526                 $$->Add(MakeLiteral($1));
527                 free($1);
528         }
529         | identifier indexer_items
530         {
531                 $$ = $2;
532                 $$->Insert(0, MakeLiteral($1));
533                 free($1);
534         }
535         ;
536
537 indexer_items: indexer_item
538         {
539                 $$ = new Array();
540                 $$->Add(*$1);
541                 delete $1;
542         }
543         | indexer_items indexer_item
544         {
545                 $$ = $1;
546                 $$->Add(*$2);
547                 delete $2;
548         }
549         ;
550
551 indexer_item: '.' identifier
552         {
553                 $$ = new Value(MakeLiteral($2));
554                 free($2);
555         }
556         | '[' rterm ']'
557         {
558                 $$ = $2;
559         }
560         ;
561
562 combined_set_op: T_SET
563         | T_SET_ADD
564         | T_SET_SUBTRACT
565         | T_SET_MULTIPLY
566         | T_SET_DIVIDE
567         {
568                 $$ = $1;
569         }
570         ;
571
572 lterm_items: /* empty */
573         {
574                 $$ = new Array();
575         }
576         | lterm_items_inner
577         {
578                 $$ = $1;
579         }
580         | lterm_items_inner sep
581         {
582                 $$ = $1;
583         }
584
585 lterm_items_inner: lterm
586         {
587                 $$ = new Array();
588                 $$->Add(*$1);
589                 delete $1;
590         }
591         | lterm_items_inner sep lterm
592         {
593                 if ($1)
594                         $$ = $1;
595                 else
596                         $$ = new Array();
597
598                 $$->Add(*$3);
599                 delete $3;
600         }
601         ;
602
603 lterm: indexer combined_set_op rterm
604         {
605                 $$ = new Value(new Expression(&Expression::OpSet, MakeArray(Array::Ptr($1), $2), *$3, DebugInfoRange(@1, @3)));
606                 delete $3;
607         }
608         | T_IMPORT rterm
609         {
610                 Expression::Ptr avar = new Expression(&Expression::OpVariable, "type", DebugInfoRange(@1, @2));
611                 $$ = new Value(new Expression(&Expression::OpImport, avar, *$2, DebugInfoRange(@1, @2)));
612                 delete $2;
613         }
614         | T_ASSIGN T_WHERE rterm
615         {
616                 if ((m_Apply.empty() || !m_Apply.top()) && (m_ObjectAssign.empty() || !m_ObjectAssign.top()))
617                         BOOST_THROW_EXCEPTION(ConfigError("'assign' keyword not valid in this context."));
618
619                 m_SeenAssign.top() = true;
620
621                 m_Assign.top() = new Expression(&Expression::OpLogicalOr, m_Assign.top(), *$3, DebugInfoRange(@1, @3));
622                 delete $3;
623
624                 $$ = new Value(MakeLiteral());
625         }
626         | T_IGNORE T_WHERE rterm
627         {
628                 if ((m_Apply.empty() || !m_Apply.top()) && (m_ObjectAssign.empty() || !m_ObjectAssign.top()))
629                         BOOST_THROW_EXCEPTION(ConfigError("'ignore' keyword not valid in this context."));
630
631                 m_Ignore.top() = new Expression(&Expression::OpLogicalOr, m_Ignore.top(), *$3, DebugInfoRange(@1, @3));
632                 delete $3;
633
634                 $$ = new Value(MakeLiteral());
635         }
636         | T_RETURN rterm
637         {
638                 Expression::Ptr aname = MakeLiteral("__result");
639                 $$ = new Value(new Expression(&Expression::OpSet, MakeArray(MakeArray(MakeLiteral(aname)), OpSetLiteral), *$2, DebugInfoRange(@1, @2)));
640                 delete $2;
641
642         }
643         | apply
644         {
645                 $$ = $1;
646         }
647         | object
648         {
649                 $$ = $1;
650         }
651         | rterm
652         {
653                 $$ = $1;
654         }
655         ;
656         
657 rterm_items: /* empty */
658         {
659                 $$ = new Array();
660         }
661         | rterm_items_inner
662         {
663                 $$ = $1;
664         }
665         | rterm_items_inner arraysep
666         {
667                 $$ = $1;
668         }
669         ;
670
671 rterm_items_inner: rterm
672         {
673                 $$ = new Array();
674                 $$->Add(*$1);
675                 delete $1;
676         }
677         | rterm_items_inner arraysep rterm
678         {
679                 $$ = $1;
680                 $$->Add(*$3);
681                 delete $3;
682         }
683         ;
684
685 rterm_array: '[' newlines rterm_items newlines ']'
686         {
687                 $$ = new Value(new Expression(&Expression::OpArray, Array::Ptr($3), DebugInfoRange(@1, @5)));
688         }
689         | '[' newlines rterm_items ']'
690         {
691                 $$ = new Value(new Expression(&Expression::OpArray, Array::Ptr($3), DebugInfoRange(@1, @4)));
692         }
693         | '[' rterm_items newlines ']'
694         {
695                 $$ = new Value(new Expression(&Expression::OpArray, Array::Ptr($2), DebugInfoRange(@1, @4)));
696         }
697         | '[' rterm_items ']'
698         {
699                 $$ = new Value(new Expression(&Expression::OpArray, Array::Ptr($2), DebugInfoRange(@1, @3)));
700         }
701         ;
702
703 rterm_scope: '{' newlines lterm_items newlines '}'
704         {
705                 $$ = new Value(new Expression(&Expression::OpDict, Array::Ptr($3), DebugInfoRange(@1, @5)));
706         }
707         | '{' newlines lterm_items '}'
708         {
709                 $$ = new Value(new Expression(&Expression::OpDict, Array::Ptr($3), DebugInfoRange(@1, @4)));
710         }
711         | '{' lterm_items newlines '}'
712         {
713                 $$ = new Value(new Expression(&Expression::OpDict, Array::Ptr($2), DebugInfoRange(@1, @4)));
714         }
715         | '{' lterm_items '}'
716         {
717                 $$ = new Value(new Expression(&Expression::OpDict, Array::Ptr($2), DebugInfoRange(@1, @3)));
718         }
719         ;
720
721 rterm: T_STRING
722         {
723                 $$ = new Value(MakeLiteral($1));
724                 free($1);
725         }
726         | T_NUMBER
727         {
728                 $$ = new Value(MakeLiteral($1));
729         }
730         | T_NULL
731         {
732                 $$ = new Value(MakeLiteral());
733         }
734         | rterm '.' T_IDENTIFIER
735         {
736                 $$ = new Value(new Expression(&Expression::OpIndexer, *$1, MakeLiteral($3), DebugInfoRange(@1, @3)));
737                 delete $1;
738                 free($3);
739         }
740         | rterm '(' rterm_items ')'
741         {
742                 $$ = new Value(new Expression(&Expression::OpFunctionCall, *$1, MakeLiteral(Array::Ptr($3)), DebugInfoRange(@1, @4)));
743                 delete $1;
744         }
745         | T_IDENTIFIER
746         {
747                 $$ = new Value(new Expression(&Expression::OpVariable, $1, @1));
748                 free($1);
749         }
750         | '!' rterm
751         {
752                 $$ = new Value(new Expression(&Expression::OpLogicalNegate, *$2, DebugInfoRange(@1, @2)));
753                 delete $2;
754         }
755         | '~' rterm
756         {
757                 $$ = new Value(new Expression(&Expression::OpNegate, *$2, DebugInfoRange(@1, @2)));
758                 delete $2;
759         }
760         | rterm '[' rterm ']'
761         {
762                 $$ = new Value(new Expression(&Expression::OpIndexer, *$1, *$3, DebugInfoRange(@1, @4)));
763                 delete $1;
764                 delete $3;
765         }
766         | rterm_array
767         {
768                 $$ = $1;
769         }
770         | rterm_scope
771         {
772                 $$ = $1;
773         }
774         | '('
775         {
776                 ignore_newlines++;
777         }
778         rterm ')'
779         {
780                 ignore_newlines--;
781                 $$ = $3;
782         }
783         | rterm T_LOGICAL_OR rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
784         | rterm T_LOGICAL_AND rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
785         | rterm T_BINARY_OR rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
786         | rterm T_BINARY_AND rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
787         | rterm T_IN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
788         | rterm T_NOT_IN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
789         | rterm T_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
790         | rterm T_NOT_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
791         | rterm T_LESS_THAN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
792         | rterm T_LESS_THAN_OR_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
793         | rterm T_GREATER_THAN rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
794         | rterm T_GREATER_THAN_OR_EQUAL rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
795         | rterm T_SHIFT_LEFT rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
796         | rterm T_SHIFT_RIGHT rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
797         | rterm T_PLUS rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
798         | rterm T_MINUS rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
799         | rterm T_MULTIPLY rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
800         | rterm T_DIVIDE_OP rterm { MakeRBinaryOp(&$$, $2, $1, $3, @1, @3); }
801         | T_FUNCTION identifier '(' identifier_items ')' rterm_scope
802         {
803                 Expression::Ptr aexpr = *$6;
804                 delete $6;
805                 aexpr->MakeInline();
806
807                 $$ = new Value(new Expression(&Expression::OpFunction, MakeArray($2, aexpr), Array::Ptr($4), DebugInfoRange(@1, @6)));
808                 free($2);
809         }
810         | T_FUNCTION '(' identifier_items ')' rterm_scope
811         {
812                 Expression::Ptr aexpr = *$5;
813                 delete $5;
814                 aexpr->MakeInline();
815
816                 $$ = new Value(new Expression(&Expression::OpFunction, MakeArray(Empty, aexpr), Array::Ptr($3), DebugInfoRange(@1, @5)));
817         }
818         | T_FOR '(' identifier T_FOLLOWS identifier T_IN rterm ')' rterm_scope
819         {
820                 Expression::Ptr aexpr = *$7;
821                 delete $7;
822
823                 Expression::Ptr ascope = *$9;
824                 delete $9;
825
826                 $$ = new Value(new Expression(&Expression::OpFor, MakeArray($3, $5, aexpr), ascope, DebugInfoRange(@1, @9)));
827                 free($3);
828                 free($5);
829         }
830         | T_FOR '(' identifier T_IN rterm ')' rterm_scope
831         {
832                 Expression::Ptr aexpr = *$5;
833                 delete $5;
834
835                 Expression::Ptr ascope = *$7;
836                 delete $7;
837
838                 $$ = new Value(new Expression(&Expression::OpFor, MakeArray($3, Empty, aexpr), ascope, DebugInfoRange(@1, @7)));
839                 free($3);
840         }
841         ;
842
843 target_type_specifier: /* empty */
844         {
845                 $$ = strdup("");
846         }
847         | T_TO identifier
848         {
849                 $$ = $2;
850         }
851         ;
852
853 apply_for_specifier: /* empty */
854         | T_APPLY_FOR '(' identifier T_FOLLOWS identifier T_IN rterm ')'
855         {
856                 m_FKVar.top() = $3;
857                 free($3);
858
859                 m_FVVar.top() = $5;
860                 free($5);
861
862                 m_FTerm.top() = *$7;
863                 delete $7;
864         }
865         | T_APPLY_FOR '(' identifier T_IN rterm ')'
866         {
867                 m_FKVar.top() = $3;
868                 free($3);
869
870                 m_FVVar.top() = "";
871
872                 m_FTerm.top() = *$5;
873                 delete $5;
874         }
875         ;
876
877 optional_rterm: /* empty */
878         {
879                 $$ = new Value(MakeLiteral(Empty));
880         }
881         | rterm
882         {
883                 $$ = $1;
884         }
885         ;
886
887 apply:
888         {
889                 m_Apply.push(true);
890                 m_SeenAssign.push(false);
891                 m_Assign.push(MakeLiteral(false));
892                 m_Ignore.push(MakeLiteral(false));
893                 m_FKVar.push("");
894                 m_FVVar.push("");
895                 m_FTerm.push(Expression::Ptr());
896         }
897         T_APPLY identifier optional_rterm apply_for_specifier target_type_specifier rterm
898         {
899                 m_Apply.pop();
900
901                 String type = $3;
902                 free($3);
903                 Expression::Ptr aname = *$4;
904                 delete $4;
905                 String target = $6;
906                 free($6);
907
908                 if (!ApplyRule::IsValidSourceType(type))
909                         BOOST_THROW_EXCEPTION(ConfigError("'apply' cannot be used with type '" + type + "'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
910
911                 if (!ApplyRule::IsValidTargetType(type, target)) {
912                         if (target == "") {
913                                 std::vector<String> types = ApplyRule::GetTargetTypes(type);
914                                 String typeNames;
915
916                                 for (std::vector<String>::size_type i = 0; i < types.size(); i++) {
917                                         if (typeNames != "") {
918                                                 if (i == types.size() - 1)
919                                                         typeNames += " or ";
920                                                 else
921                                                         typeNames += ", ";
922                                         }
923
924                                         typeNames += "'" + types[i] + "'";
925                                 }
926
927                                 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)));
928                         } else
929                                 BOOST_THROW_EXCEPTION(ConfigError("'apply' target type '" + target + "' is invalid") << errinfo_debuginfo(DebugInfoRange(@2, @5)));
930                 }
931
932                 Expression::Ptr exprl = *$7;
933                 delete $7;
934
935                 exprl->MakeInline();
936
937                 // assign && !ignore
938                 if (!m_SeenAssign.top())
939                         BOOST_THROW_EXCEPTION(ConfigError("'apply' is missing 'assign'") << errinfo_debuginfo(DebugInfoRange(@2, @3)));
940
941                 m_SeenAssign.pop();
942
943                 Expression::Ptr rex = new Expression(&Expression::OpLogicalNegate, m_Ignore.top(), DebugInfoRange(@2, @5));
944                 m_Ignore.pop();
945
946                 Expression::Ptr filter = new Expression(&Expression::OpLogicalAnd, m_Assign.top(), rex, DebugInfoRange(@2, @5));
947                 m_Assign.pop();
948
949                 String fkvar = m_FKVar.top();
950                 m_FKVar.pop();
951
952                 String fvvar = m_FVVar.top();
953                 m_FVVar.pop();
954
955                 Expression::Ptr fterm = m_FTerm.top();
956                 m_FTerm.pop();
957
958                 Array::Ptr args = new Array();
959                 args->Add(type);
960                 args->Add(target);
961                 args->Add(aname);
962                 args->Add(filter);
963                 args->Add(fkvar);
964                 args->Add(fvvar);
965                 args->Add(fterm);
966
967                 $$ = new Value(new Expression(&Expression::OpApply, args, exprl, DebugInfoRange(@2, @5)));
968         }
969         ;
970
971 newlines: T_NEWLINE
972         | newlines T_NEWLINE
973         ;
974
975 /* required separator */
976 sep: ',' newlines
977         | ','
978         | ';' newlines
979         | ';'
980         | newlines
981         ;
982
983 arraysep: ',' newlines
984         | ','
985         ;
986
987 %%