-
Notifications
You must be signed in to change notification settings - Fork 220
/
mlr.bnf
2422 lines (2097 loc) · 65.4 KB
/
mlr.bnf
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
// ================================================================
// If you edit this file, please run tools/build-dsl to autogenerate
// Go code from it, using the GOCC tool.
// ================================================================
// ================================================================
// GRAMMAR FOR THE MILLER DOMAIN-SPECIFIC LANGUAGE
//
// This is the Miller DSL's BNF grammar, using the awesome GOCC tool framework
// from https://github.com/goccmack/gocc.
//
// The first section is lexical elements and the second section is syntactical
// elements. These are the analogs of lex and yacc, respectively, using a
// classical C/lex/yacc framework -- although for lex/yacc one would have
// separate .l and .y files, whereas here there is a single .bnf file.
//
// Notes:
//
// * This grammar is used to auto-generate Go code, using bin/gocc.
//
// * Lexical items are either literals inlined within the syntactical section,
// such as "/", or snake-cased named tokens within the lexical section, such
// as field_name.
//
// * Syntactical items are all camel-cased, such as MapLiteral.
//
// * Everything is delivered to the rest of Miller in the form of an abstract
// syntax tree (AST), via <<...>>> code segments within this file's
// syntactical section, to be processed by hand-written Go code. That code,
// in turn, turns the AST into a CST (concrete syntax tree) which is what the
// DSL runtime executes.
//
// * The <<...>> code called by the gocc framework must accept interface{} at
// all parameters, to be generic, but in practice all arguments end up being
// either token.Token (regcognizable here via string-literals or snake-cased
// namees) or *dsl.AstNode (recognizable here via camel-cased names).
//
// * Another pattern worth pointing out is that in the gocc framework,
// return-types from AST methods must be a pair of (interface{}, error),
// whereas arguments going into those same methods are interface{} only.
// Hence a few methods in the Miller AST API which don't return a pair of
// interface{}/error since they are meant for nesting as arguments here
// within this file.
//
// * Please see internal/pkg/dsl/ast*.go for more about what the <<...>>
// code here is calling.
// ================================================================
// ================================================================
// LEXICAL ELEMENTS
// ================================================================
// ----------------------------------------------------------------
// CHARACTER CLASSES
// ----------------------------------------------------------------
_letter : 'a'-'z' | 'A'-'Z' | '\u00a0'-'\u00ff' | '\u0100'-'\U0010ffff';
_decdig : '0'-'9' ;
_hexdig : '0'-'9' | 'a'-'f' | 'A'-'F';
_octdig : '0'-'7' ;
_bindig : '0'-'1' ;
_leading_idchar : _letter | '_' ;
_idchar : _letter | _decdig | '_' ;
!whitespace : ' ' | '\t' | '\n' | '\r' ;
!comment : '#' {.} '\n' ;
// ----------------------------------------------------------------
// STRING/INT/FLOAT/BOOLEAN LITERALS
// ----------------------------------------------------------------
// Notes on string literals:
// * " isn't included here -- need \" handling to put that inside strings
// * GOCC seems to lack a '[^"] notation ...
// * \[ \] \n etc special cases are a bit tedious to keystroke out ...
// these are most important for put/filter print/emit/tee/etc with "|"
// to arbitrary shell commands. E.g. in
//
// mlr put 'print | "tr \[a-z\] \[A-Z\]", $something'
//
// the shell command is the 'tr ...' string and we need to spell out the
// escape sequence used by tr.
// * See https://github.com/google/re2/wiki/Syntax
_string_literal_element
: 'A'-'Z' | 'a'-'z' | '0'-'9'
| '\n'
| ' ' | '!' | '#' | '$' | '%' | '&' | '\'' | '\\'
| '(' | ')' | '*' | '+' | ',' | '-' | '.' | '/'
| ':' | ';' | '<' | '=' | '>' | '?' | '@' | '['
| ']' | '^' | '_' | '`' | '{' | '|' | '}' | '~'
| ( '\\' '\\' ) | ( '\\' '"' ) | ( '\\' '[' ) | ( '\\' ']' )
| ( '\\' '.' ) | ( '\\' '*' ) | ( '\\' '%' )
| ( '\\' '^' ) | ( '\\' '$' ) | ( '\\' '+' )
| ( '\\' '(' ) | ( '\\' ')' ) | ( '\\' '&' )
| ( '\\' 'A') | ( '\\' 'B') | ( '\\' 'C') | ( '\\' 'D') | ( '\\' 'G') | ( '\\' 'H')
| ( '\\' 'K') | ( '\\' 'L') | ( '\\' 'N') | ( '\\' 'P') | ( '\\' 'R') | ( '\\' 'S')
| ( '\\' 'U') | ( '\\' 'V') | ( '\\' 'W') | ( '\\' 'X') | ( '\\' 'Z')
| ( '\\' 'a') | ( '\\' 'b') | ( '\\' 'c') | ( '\\' 'd') | ( '\\' 'f') | ( '\\' 'g')
| ( '\\' 'h') | ( '\\' 'k') | ( '\\' 'l') | ( '\\' 'n') | ( '\\' 'p') | ( '\\' 'r')
| ( '\\' 's') | ( '\\' 't') | ( '\\' 'u') | ( '\\' 'v') | ( '\\' 'w') | ( '\\' 'x')
| ( '\\' 'z')
| ( '\\' '0' ) | ( '\\' '1' ) | ( '\\' '2' ) | ( '\\' '3' ) | ( '\\' '4' )
| ( '\\' '5' ) | ( '\\' '6' ) | ( '\\' '7' ) | ( '\\' '8' ) | ( '\\' '9' )
| '\u00a0'-'\u00ff'
| '\u0100'-'\U0010ffff'
;
string_literal : '"' {_string_literal_element} '"' ;
// Miller regexes are of the form "a.*b" for case-sensitive, or "a.*b"i for case-insensitive.
regex_case_insensitive : '"' {_string_literal_element} '"' 'i';
// Notes on int literals:
// * Leading minus sign is handled via the unary-minus operator, not here.
int_literal
: _decdig { _decdig }
| '0' 'x' _hexdig { _hexdig }
| '0' 'o' _octdig { _octdig }
| '0' 'b' _bindig { _bindig }
;
// Notes on float literals:
// * Leading minus sign is handled via the unary-minus operator, not here.
// * The various shapes are for scientific notation. Examples:
// 123
// 123.
// 123.4
// .234
// 1e2
// 1e-2
// 1.2e3 1.e3
// 1.2e-3 1.e-3
// .2e3
// .2e-3 1.e-3
_scinotE : 'e' | 'E' ;
float_literal
: { _decdig} '.' { _decdig }
| _decdig { _decdig} '.' { _decdig }
| _decdig { _decdig} _scinotE _decdig { _decdig}
| _decdig { _decdig} _scinotE '-' _decdig { _decdig}
| _decdig { _decdig} _scinotE '+' _decdig { _decdig}
| _decdig { _decdig} '.' { _decdig} _scinotE _decdig { _decdig}
| _decdig { _decdig} '.' { _decdig} _scinotE '-' _decdig { _decdig}
| _decdig { _decdig} '.' { _decdig} _scinotE '+' _decdig { _decdig}
| { _decdig} '.' _decdig { _decdig} _scinotE _decdig { _decdig}
| { _decdig} '.' _decdig { _decdig} _scinotE '-' _decdig { _decdig}
| { _decdig} '.' _decdig { _decdig} _scinotE '+' _decdig { _decdig}
;
const_M_PI : 'M' '_' 'P' 'I' ;
const_M_E : 'M' '_' 'E' ;
// Notes on boolean literals:
// * true and false should be defined here rather than as "true" / "false"
// within the grammar below -- this forces them to be keywords, not legal as
// variable names. We want them as keywords -- we don't want to allow things
// like 'true = 3'.
_literal_true : 't' 'r' 'u' 'e' ;
_literal_false : 'f' 'a' 'l' 's' 'e';
boolean_literal : ( _literal_true | _literal_false );
null_literal : 'n' 'u' 'l' 'l';
inf_literal : 'I' 'n' 'f';
nan_literal : 'N' 'a' 'N';
// ----------------------------------------------------------------
// MILLER CONTEXT VARIABLES
// ----------------------------------------------------------------
// I want to call these simply "IPS" et al. but GOCC is has leading-case (and
// leading-underscore) semantics for token names.
ctx_IPS : 'I' 'P' 'S' ;
ctx_IFS : 'I' 'F' 'S' ;
ctx_IRS : 'I' 'R' 'S' ;
ctx_OPS : 'O' 'P' 'S' ;
ctx_OFS : 'O' 'F' 'S' ;
ctx_ORS : 'O' 'R' 'S' ;
ctx_FLATSEP : 'F' 'L' 'A' 'T' 'S' 'E' 'P';
ctx_NF : 'N' 'F' ;
ctx_NR : 'N' 'R' ;
ctx_FNR : 'F' 'N' 'R' ;
ctx_FILENAME : 'F' 'I' 'L' 'E' 'N' 'A' 'M' 'E' ;
ctx_FILENUM : 'F' 'I' 'L' 'E' 'N' 'U' 'M' ;
env : 'E' 'N' 'V' ;
// ----------------------------------------------------------------
// MILLER KEYWORDS
// ----------------------------------------------------------------
// Notes on keywords:
// * Any new keywords defined here should also be documented
// in dsl/mlr_dsl_cst.c's mlr_dsl_keyword_usage() et al.
// * true and false (boolean literals) are also keywords, defined above.
begin : 'b' 'e' 'g' 'i' 'n' ;
do : 'd' 'o' ;
elif : 'e' 'l' 'i' 'f' ;
else : 'e' 'l' 's' 'e' ;
end : 'e' 'n' 'd' ;
filter : 'f' 'i' 'l' 't' 'e' 'r' ;
for : 'f' 'o' 'r' ;
if : 'i' 'f' ;
in : 'i' 'n' ;
while : 'w' 'h' 'i' 'l' 'e' ;
break : 'b' 'r' 'e' 'a' 'k' ;
continue : 'c' 'o' 'n' 't' 'i' 'n' 'u' 'e' ;
return : 'r' 'e' 't' 'u' 'r' 'n' ;
func : 'f' 'u' 'n' 'c' ;
subr : 's' 'u' 'b' 'r' ;
call : 'c' 'a' 'l' 'l' ;
arr : 'a' 'r' 'r' ;
bool : 'b' 'o' 'o' 'l' ;
float : 'f' 'l' 'o' 'a' 't' ;
int : 'i' 'n' 't' ;
map : 'm' 'a' 'p' ;
num : 'n' 'u' 'm' ;
str : 's' 't' 'r' ;
var : 'v' 'a' 'r' ;
funct : 'f' 'u' 'n' 'c' 't';
unset : 'u' 'n' 's' 'e' 't' ;
dump : 'd' 'u' 'm' 'p' ;
edump : 'e' 'd' 'u' 'm' 'p' ;
emit1 : 'e' 'm' 'i' 't' '1' ;
emit : 'e' 'm' 'i' 't' ;
emitp : 'e' 'm' 'i' 't' 'p' ;
emitf : 'e' 'm' 'i' 't' 'f' ;
eprint : 'e' 'p' 'r' 'i' 'n' 't' ;
eprintn : 'e' 'p' 'r' 'i' 'n' 't' 'n' ;
print : 'p' 'r' 'i' 'n' 't' ;
printn : 'p' 'r' 'i' 'n' 't' 'n' ;
tee : 't' 'e' 'e' ;
stdout : 's' 't' 'd' 'o' 'u' 't' ;
stderr : 's' 't' 'd' 'e' 'r' 'r' ;
// ----------------------------------------------------------------
// FIELD NAMES, OUT-OF-STREAM VARIABLES, LOCAL VARIABLES
// ----------------------------------------------------------------
// Note: the parser depends on the dollar sign being here. If this is changed,
// that needs to be changed as well.
//
// Also note: if we omit the '$' here and include it in the parser section
// below as "$", then we get an LR-1 conflict. So this must be dealt with at
// the AST level.
//
// Also note $1 is a valid field name but @1 is not a valid oosvar name; hence
// _leading_idchar vs _idchar.
field_name : '$' _idchar { _idchar } ;
// This is for literal strings but where the field name might have spaces in it
// or somesuch.
_braced_char
: 'A'-'Z' | 'a'-'z' | '0'-'9'
| ' ' | '!' | '#' | '$' | '%' | '&' | '\'' | '\\'
| '(' | ')' | '*' | '+' | ',' | '-' | '.' | '/'
| ':' | ';' | '<' | '=' | '>' | '?' | '@' | '['
| ']' | '^' | '_' | '`' | '|' | '~'
| ( '\\' '{' ) | ( '\\' '}' )
| '\u0100'-'\U0010FFFF'
;
braced_field_name: '$' '{' _braced_char { _braced_char } '}' ;
full_srec : '$' '*' ;
oosvar_name : '@' _leading_idchar { _idchar } ;
// This is for literal strings but where the oosvar name might have spaces in it
// or somesuch.
braced_oosvar_name: '@' '{' _braced_char { _braced_char } '}' ;
full_oosvar : '@' '*' ;
all : 'a' 'l' 'l' ;
// ----------------------------------------------------------------
// FUNCTIONS AND LOCAL VARIABLES
non_sigil_name : _leading_idchar { _idchar } ;
// ----------------------------------------------------------------
// PANIC TOKEN
// ----------------------------------------------------------------
// This is for testing short-circuiting of "&&", "||", etc in the CST. The
// sole job of the CST evaluator for this token is to panic the process -- so
// we'll know if we're evaluating something we should not.
panic : '%' '%' '%' 'p' 'a' 'n' 'i' 'c' '%' '%' '%' ;
// ================================================================
// SYNTAX ELEMENTS
// ================================================================
// ================================================================
// Parsing goes through three formats:
//
// (1) Source code which is a string of characters.
//
// (2) Abstract syntax tree (AST):
//
// * Parentheses, commas, semicolons, line endings, whitespace are all stripped away
// * Variable names and literal values remain as leaf nodes of the AST
// * = + - * / ** {function names} remain as non-leaf nodes of the AST
//
// (3) Concrete syntax tree (CST): a reshaping of the AST with pre-processed
// setup of function pointers to handle each type of statement on a
// per-record basis. The if/else and/or switch statements to decide what to
// do with each AST node are done at CST-build time, so they don't need to
// be re-done when the syntax tree is executed once on every data record.
//
// The job of this parser is to turn (1) into (2).
//
// Note: This parser accepts many things that are invalid, e.g.
// * begin{end{}} -- begin/end not at top level
// * begin{$x=1} -- references to stream records at begin/end (there is no $x when
// there is no input record yet)
// * break/continue outside of for/while/do-while
// * return outside of a function definition
// * $x=x -- boundvars outside of for-loop variable bindings
//
// All of the above are enforced by the CST builder's semantic-analysis logic,
// which takes this parser's output AST as input. This is done (a) to keep
// this grammar from being overly complex, and (b) so we can get more
// informative error messages.
//
// For clearer visuals on what the ASTs look like, you can do
//
// mlr -n put -v 'your expression goes here'
//
// Also see reg_test/run's filter -v and put -v outputs, e.g. in
// reg_test/expected/out.
// ================================================================
// Import the AST/ASTNode types and functions
<< import "github.com/johnkerl/miller/internal/pkg/dsl" >>
// ================================================================
// TOP-LEVEL PRODUCTION RULE FOR THE MILLER DSL
// ----------------------------------------------------------------
Root
: StatementBlock
<< dsl.NewAST($0) >>
;
// ----------------------------------------------------------------
// A StatementBlock is a sequence of statements: either the stuff in between
// (but not including) the curly braces in things like 'if (NR > 2) { $x = 1;
// $y = 2 }', or, top-level Miller DSL statements like '$x = 1; $y = 2'.
StatementBlock
// Empty statement. This allows for 'mlr put ""', as well as repeated semicolons.
: empty
<< dsl.NewASTNodeZary(nil, dsl.NodeTypeStatementBlock) >>
| NonEmptyStatementBlock
<< dsl.Wrap($0) >>
;
// ----------------------------------------------------------------
// NonEmptyStatementBlock is split out from StatementBlock to avoid LR-1
// conflicts in parsing things like 'begin {...} x=1; y=2; end{...}' wherein we
// want to avoid forcing people to type a semicolon after the first closing
// brace.
NonEmptyStatementBlock
// ---------------------- Terminal rules
// Things not ending in a curly brace, like assignments -- and also do-while.
: BracelessStatement
<< dsl.NewASTNodeUnary(nil, $0, dsl.NodeTypeStatementBlock) >>
// Things ending in a curly brace, like for/do/while, begin/end, and pattern-acction blocks
| BracefulStatement
<< dsl.NewASTNodeUnary(nil, $0, dsl.NodeTypeStatementBlock) >>
// ---------------------- Recursive rules
// So statements can start with a semicolon
| ";" StatementBlock
<< dsl.Wrap($1) >>
// Normal case for sequential statements like '$x=1; $y=2'
| BracelessStatement ";" StatementBlock
<<dsl.PrependChild($2, $0) >>
// For 'begin {...} ; $x=1'
| BracefulStatement ";" StatementBlock
<<dsl.PrependChild($2, $0) >>
// These are for things like 'begin {...} begin {...} ...' -- where people
// shouldn't have to put semicolons after the closing curly braces.
//
// We get LR-1 conflicts with the following, so we need a pair of more
// explicit lookahead-by-more production rules instead. (By using two
// Statement rules and a (recursive) StatementBlock rule, with
// PrependTwoChildren, we are effectively getting lookahead-by-two.)
//
// | BracefulStatement StatementBlock
// <<dsl.PrependChild($1, $0) >>
// E.g. 'begin {...} begin {...} $x=1'
| BracefulStatement BracefulStatement StatementBlock
<<dsl.PrependTwoChildren($2, $0, $1) >>
// E.g. 'begin {...} $x=1'
| BracefulStatement BracelessStatement
<< dsl.NewASTNodeBinary(nil, $0, $1, dsl.NodeTypeStatementBlock) >>
// E.g. 'begin {...} $x=1 ;'
| BracefulStatement BracelessStatement ";"
<< dsl.NewASTNodeBinary(nil, $0, $1, dsl.NodeTypeStatementBlock) >>
| BracefulStatement BracelessStatement ";" NonEmptyStatementBlock
<<dsl.PrependTwoChildren($3, $0, $1) >>
;
// ----------------------------------------------------------------
// Simply a keystroke-saver for all the various if/for/do/while/begin/end/etc
// which use curly-braced bodies.
StatementBlockInBraces
: "{" StatementBlock "}"
<< dsl.Wrap($1) >>
;
// ================================================================
// ASSIGNMENT STATEMENTS
BracelessStatement
: Assignment
| Unset
| BareBoolean
| FilterStatement
| PrintStatement
| PrintnStatement
| EprintStatement
| EprintnStatement
| DumpStatement
| EdumpStatement
| TeeStatement
| Emit1Statement
| EmitStatement
| EmitPStatement
| EmitFStatement
// Has braces but does not *end* in braces -- so it requires semicolon after.
| DoWhileLoop
| BreakStatement
| ContinueStatement
| ReturnStatement
| SubroutineCallsite
;
Assignment
: Lvalue "=" Rvalue
<< dsl.NewASTNodeBinary($1, $0, $2, dsl.NodeTypeAssignment) >>
;
Unset
: unset FcnArgs
<<
dsl.AdoptChildren(
dsl.NewASTNodeNestable(
$0,
dsl.NodeTypeUnset,
),
$1,
)
>>
;
// Semantically there are far fewer things which are valid lvalues than valid
// rvalues. For example, in '1+2=3+4', the right-hand side is fine while the
// left-hand side is not.
//
// We can limit the things expressible on the left-hand side here in the AST,
// via Lvalue production rules much narrower than Rvalue production rules.
// However, this results in LR-1 conflicts for bare-boolean and pattern-action
// blocks which start with something of rvalue form -- the parser needs more
// than one lookahead symbol to realize what's going on.
//
// Instead, we use the same production rule for lvalues and rvalues here in the
// grammar, deferring lvalue restrictions to the CST builder where we have more
// flexibility. As an added bonuys, we get more expressive ability in our error
// messages.
Lvalue
: Rvalue
| Typedecl LocalVariable
<< dsl.AppendChild($1, $0) >>
;
BareBoolean
: Rvalue
<< dsl.NewASTNodeUnary(nil, $0, dsl.NodeTypeBareBoolean) >>
;
FilterStatement
: filter Rvalue
<< dsl.NewASTNodeUnary($0, $1, dsl.NodeTypeFilterStatement) >>
;
// ----------------------------------------------------------------
// For dump, emit, tee, print
Redirector
: ">" RedirectTarget
<< dsl.NewASTNodeUnary($0, $1, dsl.NodeTypeRedirectWrite) >>
| ">>" RedirectTarget
<< dsl.NewASTNodeUnary($0, $1, dsl.NodeTypeRedirectAppend) >>
| "|" RedirectTarget
<< dsl.NewASTNodeUnary($0, $1, dsl.NodeTypeRedirectPipe) >>
;
RedirectTarget
: stdout
<< dsl.NewASTNodeZary($0, dsl.NodeTypeRedirectTargetStdout) >>
| stderr
<< dsl.NewASTNodeZary($0, dsl.NodeTypeRedirectTargetStderr) >>
| Rvalue
;
// ----------------------------------------------------------------
PrintStatement
: print
<<
dsl.NewASTNodeBinary(
$0, // print
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no printable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypePrintStatement,
)
>>
| print Redirector
<<
dsl.NewASTNodeBinary(
$0, // print
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no printable
$1, // redirect
dsl.NodeTypePrintStatement,
)
>>
| print FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // print
$1, // printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypePrintStatement,
)
>>
| print Redirector "," FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // print
$3, // printables
$1, // redirect
dsl.NodeTypePrintStatement,
)
>>
;
// ----------------------------------------------------------------
PrintnStatement
: printn
<<
dsl.NewASTNodeBinary(
$0, // printn
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no printable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypePrintnStatement,
)
>>
| printn Redirector
<<
dsl.NewASTNodeBinary(
$0, // printn
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no printable
$1, // redirect
dsl.NodeTypePrintnStatement,
)
>>
| printn FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // printn
$1, // printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypePrintnStatement,
)
>>
| printn Redirector "," FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // printn
$3, // printables
$1, // redirect
dsl.NodeTypePrintnStatement,
)
>>
;
// ----------------------------------------------------------------
EprintStatement
: eprint
<<
dsl.NewASTNodeBinary(
$0, // eprint
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEprintStatement,
)
>>
| eprint FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // eprint
$1, // printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEprintStatement,
)
>>
;
// ----------------------------------------------------------------
EprintnStatement
: eprintn
<<
dsl.NewASTNodeBinary(
$0, // eprint
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEprintnStatement,
)
>>
| eprintn FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // eprintn
$1, // printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEprintnStatement,
)
>>
;
// ----------------------------------------------------------------
DumpStatement
: dump
<<
dsl.NewASTNodeBinary(
$0, // dump
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no dumpable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeDumpStatement,
)
>>
| dump Redirector
<<
dsl.NewASTNodeBinary(
$0, // dump
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no dumpable
$1, // redirect
dsl.NodeTypeDumpStatement,
)
>>
| dump FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // dump
$1, // printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeDumpStatement,
)
>>
| dump Redirector "," FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // dump
$3, // printables
$1, // redirect
dsl.NodeTypeDumpStatement,
)
>>
;
// ----------------------------------------------------------------
EdumpStatement
: edump
<<
dsl.NewASTNodeBinary(
$0, // edump
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no dumpable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEdumpStatement,
)
>>
| edump FcnArgs
<<
dsl.NewASTNodeBinary(
$0, // edump
$1, // printables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEdumpStatement,
)
>>
;
// ----------------------------------------------------------------
TeeStatement
: tee Redirector "," FullSrec
<< dsl.NewASTNodeBinary($0, $3, $1, dsl.NodeTypeTeeStatement) >>
;
// ----------------------------------------------------------------
// Examples:
// emitf @a
// emitf @a, b, $c
// Each argument must be a non-indexed oosvar/localvar/fieldname, so we can use
// their names as keys in the emitted record.
EmitFStatement
: emitf EmittableList
<<
dsl.NewASTNodeBinary(
$0, // emitf
$1, // emittables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitFStatement,
)
>>
| emitf Redirector "," EmittableList
<<
dsl.NewASTNodeBinary(
$0, // emitf
$3, // emittables
$1, // redirect
dsl.NodeTypeEmitFStatement,
)
>>
;
// ----------------------------------------------------------------
// The other emit variants need to take only oosvars, etc. -- not arbitrary
// expressions which *evaluate* to map. Emit1, by contrast, takes any
// expression which evaluates to a map. So you can do 'emit1 mapsum({"id":
// $id}, $some_map_valued_field})'.
//
// The reason for this is LR1 shift-reduce conflicts. When I originally
// implemented emit/emitp, I permitted a lot of options for lashing together
// multiple oosvars, indexing, redirection, etc. When we try to let emit (not
// emit1) take arbitrary Rvalue as argument, we get LR1 conflicts since the
// parse can't disambiguate between all the possibilities for commas and
// parentheses for emit-lashing and emit-indexing, and all the possibilities
// for commas and parentheses for the Rvalue expression itself.
//
// So, we have emit/emitp which permit grammatical complexity in the
// lashing/indexing, and emit1 which permits grammatical complexity in the
// emittable.
Emit1Statement
: emit1 Rvalue
<<
dsl.NewASTNodeUnary(
$0, // emit
$1, // Emittable
dsl.NodeTypeEmit1Statement,
)
>>
;
// ----------------------------------------------------------------
// Examples for emit:
// emit @a
// emit (@a, @b)
// emit @a, "x", "y"
// emit (@a, @b), "x", "y"
//
// Examples for emitp: syntactically identical to emit.
//
// First argument (single or in parentheses) must be non-indexed
// oosvar/localvar/fieldname, so we can use their names as keys in the emitted
// record.
//
// We use the Emittable production rule to limit the things being emitted. It
// might be fine to use more generally Rvalue -- anything *evaluating* to a
// map, including function calls -- except that the legacy punctuation design
// of 'emit (#, #), #, #' means that allowing parenthesized expressions within
// the '(...)' results in shift-reduce conflicts at parser-gen time.
//
// One backward-compatible solution (used here) is to limit the types of
// expression within the parentheses. Another (backward-incompatible) solution
// would be to modify the punctuation, e.g. 'emit [#, #], # #' or
// 'emit ([#, #], # #)' perhaps.
//
// However: we shouldn't bother. The reason is that emittables need names which
// are known.
// * emit @a -- the name is "a"
// * emit (@a, @b) -- the names are ["a", "b"]
// * emit @* -- the names are the map keys
// * emit $* -- the names are the map keys
// * emit {...} -- the names are the map keys
// If we allow emit of arbitrary expressions, we open ourselves up to things
// which are unnameable such as the return value from map-valued functions such
// as mapdiff, etc. etc.
EmitStatement
: emit EmittableAsList
<<
dsl.NewASTNodeTernary(
$0, // emit
$1, // Emittable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitStatement,
)
>>
| emit Redirector "," EmittableAsList
<<
dsl.NewASTNodeTernary(
$0, // emit
$3, // Emittable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
$1, // redirect
dsl.NodeTypeEmitStatement,
)
>>
| emit "(" EmittableList ")"
<<
dsl.NewASTNodeTernary(
$0, // emit
$2, // emittables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitStatement,
)
>>
| emit Redirector "," "(" EmittableList ")"
<<
dsl.NewASTNodeTernary(
$0, // emit
$4, // emittables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
$1, // redirect
dsl.NodeTypeEmitStatement,
)
>>
| emit EmittableAsList "," EmitKeys
<<
dsl.NewASTNodeTernary(
$0, // emit
$1, // emittable
$3, // keys
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitStatement,
)
>>
| emit Redirector "," EmittableAsList "," EmitKeys
<<
dsl.NewASTNodeTernary(
$0, // emit
$3, // emittable
$5, // keys
$1, // redirect
dsl.NodeTypeEmitStatement,
)
>>
| emit "(" EmittableList ")" "," EmitKeys
<<
dsl.NewASTNodeTernary(
$0, // emit
$2, // emittable
$5, // keys
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitStatement,
)
>>
| emit Redirector "," "(" EmittableList ")" "," EmitKeys
<<
dsl.NewASTNodeTernary(
$0, // emit
$4, // emittables
$7, // keys
$1, // redirect
dsl.NodeTypeEmitStatement,
)
>>
;
// ----------------------------------------------------------------
EmitPStatement
: emitp EmittableAsList
<<
dsl.NewASTNodeTernary(
$0, // emitp
$1, // emittable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitPStatement,
)
>>
| emitp Redirector "," EmittableAsList
<<
dsl.NewASTNodeTernary(
$0, // emitp
$3, // emittable
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
$1, // redirect
dsl.NodeTypeEmitPStatement,
)
>>
| emitp "(" EmittableList ")"
<<
dsl.NewASTNodeTernary(
$0, // emitp
$2, // emittables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitPStatement,
)
>>
| emitp Redirector "," "(" EmittableList ")"
<<
dsl.NewASTNodeTernary(
$0, // emitp
$4, // emittables
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no keys
$1, // redirect
dsl.NodeTypeEmitPStatement,
)
>>
| emitp EmittableAsList "," EmitKeys
<<
dsl.NewASTNodeTernary(
$0, // emitp
$1, // emittable
$3, // keys
dsl.NewASTNodeNestable(nil, dsl.NodeTypeNoOp), // no redirect
dsl.NodeTypeEmitPStatement,
)
>>
| emitp Redirector "," EmittableAsList "," EmitKeys
<<
dsl.NewASTNodeTernary(
$0, // emitp
$3, // emittable
$5, // keys
$1, // redirect
dsl.NodeTypeEmitPStatement,
)
>>
| emitp "(" EmittableList ")" "," EmitKeys
<<
dsl.NewASTNodeTernary(