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