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