aboutsummaryrefslogtreecommitdiff
path: root/src/build.y
blob: 3e9c8fdbe48d56429e86e37768caa976e35c6f90 (plain)
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
%defines
%{  /* Prologue -- decls and stuff */
  #include "buildparser.h"
  #include "ast.h"
void yyerror( YYLTYPE *locp, yyscan_t yyscanner, BuildParser &bld, const char *msg );
%}
/* Bison declerations */

%parse-param { yyscan_t yyscanner }
%parse-param { BuildParser &bld }
%lex-param { yyscan_t yyscanner }
%lex-param { BuildParser &bld }
%pure-parser

%locations

%debug
%error-verbose
%name-prefix="build_"

%union {
	int iVal;
	char *sVal;
	float fVal;
	bool bVal;
}

%token <sVal>	STRING		"string literal"
%token <sVal>	KEYWORD		"keyword"
%token <sVal>	CONDITION	"condition term"
%token <sVal>	VARIABLE	"variable name"
%token <sVal>	FUNCTION	"function name"
%token <sVal>	UNDEF		"undefined identifier"
%token <sVal>	PROFILE		"profile execute"
%token <iVal>	INT			"integer literal"
%token <fVal>	FLOAT		"floating point literal"
%token <bVal>	BOOL		"boolean literal"

%token TOK_TARGET			"target"
%token TOK_INPUT			"input"
%token TOK_OUTPUT			"output"
%token TOK_UNSET			"unset"
%token TOK_SET				"set"
%token TOK_CONDITION		"condition"
%token TOK_REQUIRES			"requires"
%token TOK_AUTO				"auto"
%token TOK_CONFIG			"config"
%token TOK_DISPLAY			"display"
%token TOK_TYPE				"type"
%token TOK_INT				"int"
%token TOK_FLOAT			"float"
%token TOK_BOOL				"boolean"
%token TOK_VERSION			"version"
%token TOK_STRING			"string"
%token TOK_DEFAULT			"default"
%token TOK_ALLOW			"allow"
%token TOK_RULE				"rule"
%token TOK_ACTION			"action"
%token TOK_PROFILE			"profile"
%token TOK_IF				"if"
%token TOK_THEN				"then"
%token TOK_ELSE				"else"
%token TOK_INCLUDE			"include"
%token TOK_ERROR			"error"
%token TOK_WARNING			"warning"
%token TOK_NOTICE			"notice"
%token TOK_CACHE			"cache"
%token TOK_ALWAYS			"always"
%token TOK_NEVER			"never"
%token TOK_GLOBAL			"global"
%token TOK_LOCAL			"local"
%token TOK_FOR				"for"
%token TOK_IN				"in"
%token TOK_DO				"do"
%token TOK_RETURN			"return"
%token TOK_FUNCTION			"function"
%token TOK_CONTINUE			"continue"
%token TOK_BREAK			"break"
%token TOK_VALUE			"value"
%token TOK_ALL				"all"
%token TOK_EXPORT			"export"
%token TOK_TAG				"tag"
%token TOK_NULL				"null"

%token OP_ADDSETP			"+="
%token OP_ADDSETR			"<<"
%token OP_CMPEQUAL			"=="
%token OP_INEQUAL			"!="
%token OP_LTEQUAL			"<="
%token OP_GTEQUAL			">="

%token '(' ')' '{' '}' '[' ']' ',' ';' '=' '.' '<' '>' '+' '-' '*' '/'

%right '=' OP_ADDSETP OPADDSETR
%left OP_CMPEQUAL '<' '>' OP_INEQUAL OP_LTEQUAL OP_GTEQUAL '+' '-' '*' '/'
%left '(' ')' '{' '}' '[' ']'
%left IINEG IINOT

%destructor { delete[] $$; } STRING
%destructor { delete[] $$; } KEYWORD
%destructor { delete[] $$; } CONDITION
%destructor { delete[] $$; } VARIABLE
%destructor { delete[] $$; } FUNCTION
%destructor { delete[] $$; } UNDEF
%destructor { delete[] $$; } PROFILE

%%  /* Grammar rules */

/*
 *  root stuff
 */

root:
//	| root set
	| root line_expr
	| root unset
	| root target
	| root rule
	| root config
	| root root_if
	| root root_for
	| root include
	| root notify
	| root export
	| root function_def
	| root action_def
	;

root_sub_exprs:
//		  | root set
		  | root line_expr
		  | root unset
		  | root target
		  | root rule
		  | root config
		  | root root_if
		  | root root_for
		  | root include
		  | root notify
		  | root export
		  ;

include: TOK_INCLUDE STRING ';' { bld.include( $2, yyscanner, &yylloc ); }
	   ;

/*
 *  data related
 */

string: STRING { bld.xAst.addNode( AstNode::typeString, $1 ); }
	  ;

int: INT { bld.xAst.addNode( AstNode::typeInt, $1 ); }
   ;

float: FLOAT { bld.xAst.addNode( AstNode::typeFloat, $1 ); }
	 ;

bool: BOOL { bld.xAst.addNode( AstNode::typeBool, (bool)$1 ); }
	;

null: TOK_NULL { bld.xAst.addNode( AstNode::typeNull ); }

literal: string
	   | int
	   | float
	   | bool
	   | null
	   ;

variable: UNDEF /*VARIABLE*/ { bld.xAst.addNode( AstNode::typeVariable, $1 ); }

list_core:
		 | { bld.xAst.openBranch(); } expr
		 | list_core ',' { bld.xAst.openBranch(); } expr
		 ;

list: '[' {
	  bld.xAst.addNode( AstNode::typeList );
	} list_core ']' {
	  bld.xAst.closeNode();
	}
	;

value_mods:
		  | value_mods '.' function
		  ;

value_core: variable
		  | literal
		  | function_no_input
		  | list
		  ;

value: value_core value_mods
	 ;

/*
 *  misc global things
 */

notify: TOK_ERROR STRING ';' { bld.xAst.addNode( AstNode::typeError, $2 ); }
	  | TOK_WARNING STRING ';' { bld.xAst.addNode( AstNode::typeWarning, $2 ); }
	  | TOK_NOTICE STRING ';' { bld.xAst.addNode( AstNode::typeNotice, $2 ); }
	  ;
/*
set_rhs: '=' { bld.xAst.addNode( AstNode::typeOpEq ); } value
	  | OP_ADDSETP { bld.xAst.addNode( AstNode::typeOpPlusEq ); } value
	  | OP_ADDSETR { bld.xAst.addNode( AstNode::typeOpPlusEqRaw ); } string
	  ;

set: TOK_SET {
   	bld.xAst.addNode( AstNode::typeSet );
   	bld.xAst.openBranch();
   } variable set_rhs ';' {
	bld.xAst.closeNode();
   }
   ;*/

unset: TOK_UNSET {
	 	bld.xAst.addNode( AstNode::typeUnset );
		bld.xAst.openBranch();
	 } variable ';' {
		bld.xAst.closeNode();
	 }
	 ;

export_rhs: '=' value
		  |
		  ;

export: TOK_EXPORT {
	    bld.xAst.addNode( AstNode::typeExport );
		bld.xAst.openBranch();
	  } variable export_rhs ';' {
	    bld.xAst.closeNode();
	  }
	  ;

func_params:
		   | func_param_list
		   ;

func_param_list: { bld.xAst.openBranch(); } value
			   | func_param_list ',' { bld.xAst.openBranch(); } value
			   ;

function: UNDEF '(' {
			bld.xAst.addNode( AstNode::typeFunction );
			bld.xAst.openBranch();
			bld.xAst.addNode( AstNode::typeString, $1 );
		} func_params ')' {
			bld.xAst.closeNode();
		}
		;

function_no_input: UNDEF '(' {
			bld.xAst.addNode( AstNode::typeNull );
			bld.xAst.addNode( AstNode::typeFunction );
			bld.xAst.openBranch();
			bld.xAst.addNode( AstNode::typeString, $1 );
		} func_params ')' {
			bld.xAst.closeNode();
		}
		;

requires: TOK_REQUIRES {
			bld.xAst.addNode( AstNode::typeRequires );
			bld.xAst.openBranch();
		} value ';' {
			bld.xAst.closeNode();
		}
		;

type: TOK_STRING { bld.xAst.addNode( AstNode::typeTypeString ); }
	| TOK_INT { bld.xAst.addNode( AstNode::typeTypeInt ); }
	| TOK_FLOAT { bld.xAst.addNode( AstNode::typeTypeFloat ); }
	| TOK_BOOL { bld.xAst.addNode( AstNode::typeTypeBool ); }
	| TOK_VERSION { bld.xAst.addNode( AstNode::typeTypeVersion ); }
	;

/*
 *  comparisons
 */

expr: value
	| '(' expr ')'
	| UNDEF '=' {
		bld.xAst.addNode( AstNode::typeVariableRef, $1 );
	} expr {
		bld.xAst.addNode( AstNode::typeOpEq );
	}
	| UNDEF OP_ADDSETP {
		bld.xAst.addNode( AstNode::typeVariableRef, $1 );
	} expr {
		bld.xAst.addNode( AstNode::typeOpPlusEq );
	}
	| expr OP_CMPEQUAL expr
	{
		bld.xAst.addNode( AstNode::typeCmpEq );
	}
	| expr '<' expr
	{
		bld.xAst.addNode( AstNode::typeCmpLt );
	}
	| expr '>' expr
	{
		bld.xAst.addNode( AstNode::typeCmpGt );
	}
	| expr OP_INEQUAL expr
	{
		bld.xAst.addNode( AstNode::typeCmpNe );
	}
	| expr OP_LTEQUAL expr
	{
		bld.xAst.addNode( AstNode::typeCmpLtEq );
	}
	| expr OP_GTEQUAL expr
	{
		bld.xAst.addNode( AstNode::typeCmpGtEq );
	}
	| expr '+' expr
	{
		bld.xAst.addNode( AstNode::typeOpPlus );
	}
	| expr '-' expr
	{
		bld.xAst.addNode( AstNode::typeOpMinus );
	}
	| expr '*' expr
	{
		bld.xAst.addNode( AstNode::typeOpMultiply );
	}
	| expr '/' expr
	{
		bld.xAst.addNode( AstNode::typeOpDivide );
	}
	| '-' expr %prec IINEG
	{
		bld.xAst.addNode( AstNode::typeOpNegate );
	}
	| '!' expr %prec IINOT
	{
		bld.xAst.addNode( AstNode::typeOpNot );
	}
	;

line_expr: {
		 	bld.xAst.addNode( AstNode::typeExpr );
			bld.xAst.openBranch();
		 } expr ';'
		 {
		 	bld.xAst.closeNode();
		 }
		 ;

if_core: TOK_IF {
	   	bld.xAst.addNode( AstNode::typeIf );
		bld.xAst.openBranch();
//		bld.xAst.addNode( AstNode::typeExpr );
//		bld.xAst.openBranch();
	   } expr TOK_THEN {
//	    bld.xAst.closeNode();
	   	bld.xAst.openBranch();
	   }
	   ;

else: TOK_ELSE { bld.xAst.openBranch(); }
	;

root_if: if_core '{' root_sub_exprs '}' root_else { bld.xAst.closeNode(); }
	  ;

root_else:
		 | else '{' root_sub_exprs '}'
		 | else root_if
		 ;

target_if: if_core '{' target_exprs '}' target_else { bld.xAst.closeNode(); }
		 ;

target_else:
		   | else '{' target_exprs '}'
		   | else target_if
		   ;

rule_if: if_core '{' rule_exprs '}' rule_else { bld.xAst.closeNode(); }
	   ;

rule_else:
		 | else '{' rule_exprs '}'
		 | else rule_if
		 ;

function_if: if_core '{' function_exprs '}' function_else
		   { bld.xAst.closeNode(); }
		   ;

function_else:
			 | else '{' function_exprs '}'
			 | else function_if
			 ;

/*
 *  loops
 */

for_base: TOK_FOR {
			bld.xAst.addNode( AstNode::typeFor );
			bld.xAst.openBranch();
		} variable TOK_IN {
			bld.xAst.openBranch();
		} value TOK_DO {
			bld.xAst.openBranch();
		}
		;

root_for: for_base '{' root_sub_exprs '}' { bld.xAst.closeNode(); }
		;

target_for: for_base '{' target_exprs '}' { bld.xAst.closeNode(); }
		  ;

rule_for: for_base '{' rule_exprs '}' { bld.xAst.closeNode(); }
		;

function_for: for_base '{' function_exprs '}' { bld.xAst.closeNode(); }
			;

/*
 *  functions
 */

function_def: TOK_FUNCTION UNDEF {
				bld.xAst.addNode( AstNode::typeFunctionDef );
				bld.xAst.openBranch();
				bld.xAst.addNode( AstNode::typeString, $2 );
				bld.xAst.openBranch();
			} '(' param_defs ')' {
				bld.xAst.openBranch();
			} '{' function_exprs '}' {
				bld.xAst.closeNode();
			}
			;

param_defs:
		  | param_def_list
		  ;

param_def_list: variable
			  | param_def_list ',' variable
			  ;

function_exprs:
//			  | function_exprs function ';'
//			  | function_exprs set
			  | function_exprs unset
			  | function_exprs line_expr
			  | function_exprs export
			  | function_exprs notify
			  | function_exprs function_if
			  | function_exprs function_for
			  | function_exprs return
			  | function_exprs process_target
			  ;

return: TOK_RETURN {
	  	bld.xAst.addNode( AstNode::typeReturn );
		bld.xAst.openBranch();
	  } expr {
	  	bld.xAst.closeNode();
	  } ';'
	  ;

/*
 * Actions, they're basically functions, no parameters
 */

action_def: TOK_ACTION STRING {
		    bld.xAst.addNode( AstNode::typeActionDef );
			bld.xAst.openBranch();
			bld.xAst.addNode( AstNode::typeString, $2 );
			bld.xAst.openBranch();
		  } '{' function_exprs '}' {
		    bld.xAst.closeNode();
		  }
		  ;

/*
 *  profiles
 */

profile: TOK_PROFILE {
	   	bld.xAst.addNode( AstNode::typeProfile );
		bld.xAst.openBranch();
	   } string {
	   	bld.xAst.openBranch();
	   } '{' profile_exprs '}' {
	   	bld.xAst.closeNode();
	   } /* in-line function */
	   ;

profile_exprs:
//			 | profile_exprs function ';'
//			 | profile_exprs set
			 | profile_exprs unset
			 | profile_exprs line_expr
			 | profile_exprs export
			 | profile_exprs notify
			 | profile_exprs function_if
			 | profile_exprs function_for
			 | profile_exprs return
			 | profile_exprs process_target
			 | profile_exprs condition
			 ;
/*
 *  targets
 */

target: TOK_TARGET {
	  	bld.xAst.addNode( AstNode::typeTarget );
		bld.xAst.openBranch();
	  } expr {
	  	bld.xAst.openBranch();
	  } '{' target_exprs '}' {
	  	bld.xAst.closeNode();
	  }
	  ;

target_exprs:
//			| target_exprs set
			| target_exprs unset
			| target_exprs line_expr
			| target_exprs export
			| target_exprs target_input
			| target_exprs requires
			| target_exprs profile
			| target_exprs target_if
			| target_exprs target_for
			| target_exprs notify
			| target_exprs target_rule
			| target_exprs tag
			| target_exprs display
			;

target_input: TOK_INPUT {
				bld.xAst.addNode( AstNode::typeInput );
				bld.xAst.openBranch();
			} expr ';' {
				bld.xAst.closeNode();
			}
			;

target_rule: TOK_RULE {
		   	bld.xAst.addNode( AstNode::typeRule );
			bld.xAst.openBranch();
		   } string ';' {
		   	bld.xAst.closeNode();
		   }
		   ;

condition: TOK_CONDITION CONDITION ';' {
		 	bld.xAst.addNode( AstNode::typeCondition, $2 );
		 }
		 | TOK_CONDITION TOK_ALWAYS ';'{
		 	bld.xAst.addNode( AstNode::typeCondition, "always" );
		 }
		 | TOK_CONDITION TOK_NEVER ';'{
		 	bld.xAst.addNode( AstNode::typeCondition, "never" );
		 }
		 ;

/*
 *  rules
 */

rule: TOK_RULE {
		bld.xAst.addNode( AstNode::typeRuleDef );
		bld.xAst.openBranch();
	} string {
		bld.xAst.openBranch();
	} '{' rule_exprs '}' {
		bld.xAst.closeNode();
	}
	;

rule_exprs:
		  | rule_exprs rule_input
		  | rule_exprs output
		  | rule_exprs requires
		  | rule_exprs profile
		  | rule_exprs rule_if
		  | rule_exprs rule_for
		  | rule_exprs notify
		  | rule_exprs display
		  | rule_exprs tag
//		  | rule_exprs set
		  ;

rule_input_func: function
			   | STRING {
			     /* In this case, when the input is just a string,
				    lets actually turn it into a call to the matches function.
					*/
			     bld.xAst.addNode( AstNode::typeFunction );
			     bld.xAst.openBranch();
			     bld.xAst.addNode( AstNode::typeString, "matches" );
			     bld.xAst.openBranch();
			     bld.xAst.addNode( AstNode::typeString, $1 );
			     bld.xAst.closeNode();
			   }
/*			   | string */
			   ;

rule_input: TOK_INPUT {
		  	bld.xAst.addNode( AstNode::typeInput );
			bld.xAst.openBranch();
		  } rule_input_func ';' {
		  	bld.xAst.closeNode();
		  }
		  ;

output: TOK_OUTPUT {
	  	bld.xAst.addNode( AstNode::typeOutput );
		bld.xAst.openBranch();
	  } value ';' {
	  	bld.xAst.closeNode();
	  }
	  ;

/*
 *  config
 */
config: TOK_CONFIG {
	  	bld.xAst.addNode( AstNode::typeConfig );
		bld.xAst.openBranch();
	  } string {
	  	bld.xAst.openBranch();
	  } '{' config_exprs '}' {
	  	bld.xAst.closeNode();
	  }
	  | TOK_AUTO TOK_CONFIG {
	  	bld.xAst.addNode( AstNode::typeAutoConfig );
		bld.xAst.openBranch();
	  } string {
	  	bld.xAst.openBranch();
	  } '{' config_exprs '}' {
	  	bld.xAst.closeNode();
	  }
	  | TOK_GLOBAL TOK_CONFIG {
	  	bld.xAst.addNode( AstNode::typeGlobalConfig );
		bld.xAst.openBranch();
	  } string {
	  	bld.xAst.openBranch();
	  } '{' config_exprs '}' {
	  	bld.xAst.closeNode();
	  }
	  ;

config_exprs:
			| config_exprs display
			| config_exprs config_type
			| config_exprs default
			| config_exprs value_key
			| config_exprs allow
			| config_exprs cache
			;

display: TOK_DISPLAY STRING ';' {
	   	bld.xAst.addNode( AstNode::typeDisplay, $2 );
	   }
	   ;

config_type: TOK_TYPE {
		   	bld.xAst.addNode( AstNode::typeType );
			bld.xAst.openBranch();
		   } type ';' {
		   	bld.xAst.closeNode();
		   }
		   ;

default: TOK_DEFAULT {
	   	bld.xAst.addNode( AstNode::typeDefault );
		bld.xAst.openBranch();
	   } literal ';' {
	   	bld.xAst.closeNode();
	   }
	   ;

value_key_val: value ';'
			 | '{' function_exprs '}' /* inline function */

value_key: TOK_VALUE {
		 	bld.xAst.addNode( AstNode::typeValue );
			bld.xAst.openBranch();
		 } value_key_val {
		 	bld.xAst.closeNode();
		 }
		 ;

allow: TOK_ALLOW {
	 	bld.xAst.addNode( AstNode::typeAllow );
		bld.xAst.openBranch();
	 } value ';' {
	 	bld.xAst.closeNode();
	 }
	 ;

cache: TOK_CACHE TOK_ALWAYS ';'
	 { bld.xAst.addNode( AstNode::typeCache, true ); }
	 | TOK_CACHE TOK_NEVER ';'
	 { bld.xAst.addNode( AstNode::typeCache, false ); }
	 ;

/*
 * target/profile execute
 */
process_target: PROFILE
			  {
			  	bld.xAst.addNode( AstNode::typeProcessTarget );
				bld.xAst.openBranch();
				bld.xAst.addNode( AstNode::typeString, $1 );
				bld.xAst.openBranch();
			  } value ';' {
				bld.xAst.closeNode();
			  }
			  ;

tag: TOK_TAG
   {
   	bld.xAst.addNode( AstNode::typeTag );
	bld.xAst.openBranch();
   } value ';' {
   	bld.xAst.closeNode();
   }
   ;
	

%%

/* Epilogue -- whatever you want, functions mainly */

void build_error( YYLTYPE *locp, yyscan_t, BuildParser &bld, const char *msg )
{
	bld.error(
		locp->first_line, locp->last_line,
		locp->first_column, locp->last_column,
		msg
		);
}