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