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