forked from bleibig/rust-grammar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-lalr.y
1995 lines (1756 loc) · 81 KB
/
parser-lalr.y
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
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
%{
#define YYERROR_VERBOSE
#define YYSTYPE struct node *
struct node;
extern int yylex();
extern void yyerror(char const *s);
extern struct node *mk_node(char const *name, int n, ...);
extern struct node *mk_atom(char *text);
extern struct node *mk_none();
extern struct node *ext_node(struct node *nd, int n, ...);
extern void push_back(char c);
extern char *yytext;
%}
%debug
%token SHL
%token SHR
%token LE
%token EQEQ
%token NE
%token GE
%token ANDAND
%token OROR
%token SHLEQ
%token SHREQ
%token MINUSEQ
%token ANDEQ
%token OREQ
%token PLUSEQ
%token STAREQ
%token SLASHEQ
%token CARETEQ
%token PERCENTEQ
%token DOTDOT
%token DOTDOTDOT
%token MOD_SEP
%token RARROW
%token LARROW
%token FAT_ARROW
%token LIT_BYTE
%token LIT_CHAR
%token LIT_INTEGER
%token LIT_FLOAT
%token LIT_STR
%token LIT_STR_RAW
%token LIT_BYTE_STR
%token LIT_BYTE_STR_RAW
%token IDENT
%token UNDERSCORE
%token LIFETIME
// keywords
%token SELF
%token STATIC
%token ABSTRACT
%token ALIGNOF
%token AS
%token BECOME
%token BREAK
%token CATCH
%token CRATE
%token DO
%token ELSE
%token ENUM
%token EXTERN
%token FALSE
%token FINAL
%token FN
%token FOR
%token IF
%token IMPL
%token IN
%token LET
%token LOOP
%token MACRO
%token MATCH
%token MOD
%token MOVE
%token MUT
%token OFFSETOF
%token OVERRIDE
%token PRIV
%token PUB
%token PURE
%token REF
%token RETURN
%token SIZEOF
%token STRUCT
%token SUPER
%token UNION
%token UNSIZED
%token TRUE
%token TRAIT
%token TYPE
%token UNSAFE
%token VIRTUAL
%token YIELD
%token DEFAULT
%token USE
%token WHILE
%token CONTINUE
%token PROC
%token BOX
%token CONST
%token WHERE
%token TYPEOF
%token INNER_DOC_COMMENT
%token OUTER_DOC_COMMENT
%token SHEBANG
%token SHEBANG_LINE
%token STATIC_LIFETIME
/*
Quoting from the Bison manual:
"Finally, the resolution of conflicts works by comparing the precedence
of the rule being considered with that of the lookahead token. If the
token's precedence is higher, the choice is to shift. If the rule's
precedence is higher, the choice is to reduce. If they have equal
precedence, the choice is made based on the associativity of that
precedence level. The verbose output file made by ‘-v’ (see Invoking
Bison) says how each conflict was resolved"
*/
// We expect no shift/reduce or reduce/reduce conflicts in this grammar;
// all potential ambiguities are scrutinized and eliminated manually.
%expect 0
// fake-precedence symbol to cause '|' bars in lambda context to parse
// at low precedence, permit things like |x| foo = bar, where '=' is
// otherwise lower-precedence than '|'. Also used for proc() to cause
// things like proc() a + b to parse as proc() { a + b }.
%precedence LAMBDA
%precedence SELF
// MUT should be lower precedence than IDENT so that in the pat rule,
// "& MUT pat" has higher precedence than "binding_mode ident [@ pat]"
%precedence MUT
// IDENT needs to be lower than '{' so that 'foo {' is shifted when
// trying to decide if we've got a struct-construction expr (esp. in
// contexts like 'if foo { .')
//
// IDENT also needs to be lower precedence than '<' so that '<' in
// 'foo:bar . <' is shifted (in a trait reference occurring in a
// bounds list), parsing as foo:(bar<baz>) rather than (foo:bar)<baz>.
%precedence IDENT
// Put the weak keywords that can be used as idents here as well
%precedence CATCH
%precedence DEFAULT
%precedence UNION
// A couple fake-precedence symbols to use in rules associated with +
// and < in trailing type contexts. These come up when you have a type
// in the RHS of operator-AS, such as "foo as bar<baz>". The "<" there
// has to be shifted so the parser keeps trying to parse a type, even
// though it might well consider reducing the type "bar" and then
// going on to "<" as a subsequent binop. The "+" case is with
// trailing type-bounds ("foo as bar:A+B"), for the same reason.
%precedence SHIFTPLUS
%precedence MOD_SEP
%precedence RARROW ':'
// In where clauses, "for" should have greater precedence when used as
// a higher ranked constraint than when used as the beginning of a
// for_in_type (which is a ty)
%precedence FORTYPE
%precedence FOR
// Binops & unops, and their precedences
%precedence '?'
%precedence BOX
%nonassoc DOTDOT
// RETURN needs to be lower-precedence than tokens that start
// prefix_exprs
%precedence RETURN YIELD
%right '=' SHLEQ SHREQ MINUSEQ ANDEQ OREQ PLUSEQ STAREQ SLASHEQ CARETEQ PERCENTEQ
%right LARROW
%left OROR
%left ANDAND
%left EQEQ NE
%left '<' '>' LE GE
%left '|'
%left '^'
%left '&'
%left SHL SHR
%left '+' '-'
%precedence AS
%left '*' '/' '%'
%precedence '!'
%precedence '{' '[' '(' '.'
%precedence RANGE
%start crate
%%
////////////////////////////////////////////////////////////////////////
// Part 1: Items and attributes
////////////////////////////////////////////////////////////////////////
crate
: maybe_shebang inner_attrs maybe_mod_items { mk_node("crate", 2, $2, $3); }
| maybe_shebang maybe_mod_items { mk_node("crate", 1, $2); }
;
maybe_shebang
: SHEBANG_LINE
| %empty
;
maybe_inner_attrs
: inner_attrs
| %empty { $$ = mk_none(); }
;
inner_attrs
: inner_attr { $$ = mk_node("InnerAttrs", 1, $1); }
| inner_attrs inner_attr { $$ = ext_node($1, 1, $2); }
;
inner_attr
: SHEBANG '[' meta_item ']' { $$ = mk_node("InnerAttr", 1, $3); }
| INNER_DOC_COMMENT { $$ = mk_node("InnerAttr", 1, mk_node("doc-comment", 1, mk_atom(yytext))); }
;
maybe_outer_attrs
: outer_attrs
| %empty { $$ = mk_none(); }
;
outer_attrs
: outer_attr { $$ = mk_node("OuterAttrs", 1, $1); }
| outer_attrs outer_attr { $$ = ext_node($1, 1, $2); }
;
outer_attr
: '#' '[' meta_item ']' { $$ = $3; }
| OUTER_DOC_COMMENT { $$ = mk_node("doc-comment", 1, mk_atom(yytext)); }
;
meta_item
: ident { $$ = mk_node("MetaWord", 1, $1); }
| ident '=' lit { $$ = mk_node("MetaNameValue", 2, $1, $3); }
| ident '(' meta_seq ')' { $$ = mk_node("MetaList", 2, $1, $3); }
| ident '(' meta_seq ',' ')' { $$ = mk_node("MetaList", 2, $1, $3); }
;
meta_seq
: %empty { $$ = mk_none(); }
| meta_item { $$ = mk_node("MetaItems", 1, $1); }
| meta_seq ',' meta_item { $$ = ext_node($1, 1, $3); }
;
maybe_mod_items
: mod_items
| %empty { $$ = mk_none(); }
;
mod_items
: mod_item { $$ = mk_node("Items", 1, $1); }
| mod_items mod_item { $$ = ext_node($1, 1, $2); }
;
attrs_and_vis
: maybe_outer_attrs visibility { $$ = mk_node("AttrsAndVis", 2, $1, $2); }
;
mod_item
: attrs_and_vis item { $$ = mk_node("Item", 2, $1, $2); }
;
// items that can appear outside of a fn block
item
: stmt_item
| item_macro
;
// items that can appear in "stmts"
stmt_item
: item_static
| item_const
| item_type
| block_item
| view_item
;
item_static
: STATIC ident ':' ty '=' expr ';' { $$ = mk_node("ItemStatic", 3, $2, $4, $6); }
| STATIC MUT ident ':' ty '=' expr ';' { $$ = mk_node("ItemStatic", 3, $3, $5, $7); }
;
item_const
: CONST ident ':' ty '=' expr ';' { $$ = mk_node("ItemConst", 3, $2, $4, $6); }
;
item_macro
: path_expr '!' maybe_ident parens_delimited_token_trees ';' { $$ = mk_node("ItemMacro", 3, $1, $3, $4); }
| path_expr '!' maybe_ident braces_delimited_token_trees { $$ = mk_node("ItemMacro", 3, $1, $3, $4); }
| path_expr '!' maybe_ident brackets_delimited_token_trees ';'{ $$ = mk_node("ItemMacro", 3, $1, $3, $4); }
;
view_item
: use_item
| extern_fn_item
| EXTERN CRATE ident ';' { $$ = mk_node("ViewItemExternCrate", 1, $3); }
| EXTERN CRATE ident AS ident ';' { $$ = mk_node("ViewItemExternCrate", 2, $3, $5); }
;
extern_fn_item
: EXTERN maybe_abi item_fn { $$ = mk_node("ViewItemExternFn", 2, $2, $3); }
;
use_item
: USE view_path ';' { $$ = mk_node("ViewItemUse", 1, $2); }
;
view_path
: path_no_types_allowed { $$ = mk_node("ViewPathSimple", 1, $1); }
| path_no_types_allowed MOD_SEP '{' '}' { $$ = mk_node("ViewPathList", 2, $1, mk_atom("ViewPathListEmpty")); }
| MOD_SEP '{' '}' { $$ = mk_node("ViewPathList", 1, mk_atom("ViewPathListEmpty")); }
| path_no_types_allowed MOD_SEP '{' idents_or_self '}' { $$ = mk_node("ViewPathList", 2, $1, $4); }
| MOD_SEP '{' idents_or_self '}' { $$ = mk_node("ViewPathList", 1, $3); }
| path_no_types_allowed MOD_SEP '{' idents_or_self ',' '}' { $$ = mk_node("ViewPathList", 2, $1, $4); }
| MOD_SEP '{' idents_or_self ',' '}' { $$ = mk_node("ViewPathList", 1, $3); }
| path_no_types_allowed MOD_SEP '*' { $$ = mk_node("ViewPathGlob", 1, $1); }
| MOD_SEP '*' { $$ = mk_atom("ViewPathGlob"); }
| '*' { $$ = mk_atom("ViewPathGlob"); }
| '{' '}' { $$ = mk_atom("ViewPathListEmpty"); }
| '{' idents_or_self '}' { $$ = mk_node("ViewPathList", 1, $2); }
| '{' idents_or_self ',' '}' { $$ = mk_node("ViewPathList", 1, $2); }
| path_no_types_allowed AS ident { $$ = mk_node("ViewPathSimple", 2, $1, $3); }
;
block_item
: item_fn
| item_unsafe_fn
| item_mod
| item_foreign_mod { $$ = mk_node("ItemForeignMod", 1, $1); }
| item_struct
| item_enum
| item_union
| item_trait
| item_impl
;
maybe_ty_ascription
: ':' ty_sum { $$ = $2; }
| %empty { $$ = mk_none(); }
;
maybe_init_expr
: '=' expr { $$ = $2; }
| %empty { $$ = mk_none(); }
;
// structs
item_struct
: STRUCT ident generic_params maybe_where_clause struct_decl_args
{
$$ = mk_node("ItemStruct", 4, $2, $3, $4, $5);
}
| STRUCT ident generic_params struct_tuple_args maybe_where_clause ';'
{
$$ = mk_node("ItemStruct", 4, $2, $3, $4, $5);
}
| STRUCT ident generic_params maybe_where_clause ';'
{
$$ = mk_node("ItemStruct", 3, $2, $3, $4);
}
;
struct_decl_args
: '{' struct_decl_fields '}' { $$ = $2; }
| '{' struct_decl_fields ',' '}' { $$ = $2; }
;
struct_tuple_args
: '(' struct_tuple_fields ')' { $$ = $2; }
| '(' struct_tuple_fields ',' ')' { $$ = $2; }
;
struct_decl_fields
: struct_decl_field { $$ = mk_node("StructFields", 1, $1); }
| struct_decl_fields ',' struct_decl_field { $$ = ext_node($1, 1, $3); }
| %empty { $$ = mk_none(); }
;
struct_decl_field
: attrs_and_vis ident ':' ty_sum { $$ = mk_node("StructField", 3, $1, $2, $4); }
;
struct_tuple_fields
: struct_tuple_field { $$ = mk_node("StructFields", 1, $1); }
| struct_tuple_fields ',' struct_tuple_field { $$ = ext_node($1, 1, $3); }
| %empty { $$ = mk_none(); }
;
struct_tuple_field
: attrs_and_vis ty_sum { $$ = mk_node("StructField", 2, $1, $2); }
;
// enums
item_enum
: ENUM ident generic_params maybe_where_clause '{' enum_defs '}' { $$ = mk_node("ItemEnum", 0); }
| ENUM ident generic_params maybe_where_clause '{' enum_defs ',' '}' { $$ = mk_node("ItemEnum", 0); }
;
enum_defs
: enum_def { $$ = mk_node("EnumDefs", 1, $1); }
| enum_defs ',' enum_def { $$ = ext_node($1, 1, $3); }
| %empty { $$ = mk_none(); }
;
enum_def
: attrs_and_vis ident enum_args { $$ = mk_node("EnumDef", 3, $1, $2, $3); }
;
enum_args
: '{' struct_decl_fields '}' { $$ = mk_node("EnumArgs", 1, $2); }
| '{' struct_decl_fields ',' '}' { $$ = mk_node("EnumArgs", 1, $2); }
| '(' maybe_ty_sums ')' { $$ = mk_node("EnumArgs", 1, $2); }
| '=' expr { $$ = mk_node("EnumArgs", 1, $2); }
| %empty { $$ = mk_none(); }
;
// unions
item_union
: UNION ident generic_params maybe_where_clause '{' struct_decl_fields '}' { $$ = mk_node("ItemUnion", 0); }
| UNION ident generic_params maybe_where_clause '{' struct_decl_fields ',' '}' { $$ = mk_node("ItemUnion", 0); }
item_mod
: MOD ident ';' { $$ = mk_node("ItemMod", 1, $2); }
| MOD ident '{' maybe_mod_items '}' { $$ = mk_node("ItemMod", 2, $2, $4); }
| MOD ident '{' inner_attrs maybe_mod_items '}' { $$ = mk_node("ItemMod", 3, $2, $4, $5); }
;
item_foreign_mod
: EXTERN maybe_abi '{' maybe_foreign_items '}' { $$ = mk_node("ItemForeignMod", 1, $4); }
| EXTERN maybe_abi '{' inner_attrs maybe_foreign_items '}' { $$ = mk_node("ItemForeignMod", 2, $4, $5); }
;
maybe_abi
: str
| %empty { $$ = mk_none(); }
;
maybe_foreign_items
: foreign_items
| %empty { $$ = mk_none(); }
;
foreign_items
: foreign_item { $$ = mk_node("ForeignItems", 1, $1); }
| foreign_items foreign_item { $$ = ext_node($1, 1, $2); }
;
foreign_item
: attrs_and_vis STATIC item_foreign_static { $$ = mk_node("ForeignItem", 2, $1, $3); }
| attrs_and_vis item_foreign_fn { $$ = mk_node("ForeignItem", 2, $1, $2); }
| attrs_and_vis UNSAFE item_foreign_fn { $$ = mk_node("ForeignItem", 2, $1, $3); }
;
item_foreign_static
: maybe_mut ident ':' ty ';' { $$ = mk_node("StaticItem", 3, $1, $2, $4); }
;
item_foreign_fn
: FN ident generic_params fn_decl_allow_variadic maybe_where_clause ';' { $$ = mk_node("ForeignFn", 4, $2, $3, $4, $5); }
;
fn_decl_allow_variadic
: fn_params_allow_variadic ret_ty { $$ = mk_node("FnDecl", 2, $1, $2); }
;
fn_params_allow_variadic
: '(' ')' { $$ = mk_none(); }
| '(' params ')' { $$ = $2; }
| '(' params ',' ')' { $$ = $2; }
| '(' params ',' DOTDOTDOT ')' { $$ = $2; }
;
visibility
: PUB { $$ = mk_atom("Public"); }
| %empty { $$ = mk_atom("Inherited"); }
;
idents_or_self
: ident_or_self { $$ = mk_node("IdentsOrSelf", 1, $1); }
| idents_or_self AS ident { $$ = mk_node("IdentsOrSelf", 2, $1, $3); }
| idents_or_self ',' ident_or_self { $$ = ext_node($1, 1, $3); }
;
ident_or_self
: ident
| SELF { $$ = mk_atom(yytext); }
;
item_type
: TYPE ident generic_params maybe_where_clause '=' ty_sum ';' { $$ = mk_node("ItemTy", 4, $2, $3, $4, $6); }
;
for_sized
: FOR '?' ident { $$ = mk_node("ForSized", 1, $3); }
| FOR ident '?' { $$ = mk_node("ForSized", 1, $2); }
| %empty { $$ = mk_none(); }
;
item_trait
: maybe_unsafe TRAIT ident generic_params for_sized maybe_ty_param_bounds maybe_where_clause '{' maybe_trait_items '}'
{
$$ = mk_node("ItemTrait", 7, $1, $3, $4, $5, $6, $7, $9);
}
;
maybe_trait_items
: trait_items
| %empty { $$ = mk_none(); }
;
trait_items
: trait_item { $$ = mk_node("TraitItems", 1, $1); }
| trait_items trait_item { $$ = ext_node($1, 1, $2); }
;
trait_item
: trait_const
| trait_type
| trait_method
| maybe_outer_attrs item_macro { $$ = mk_node("TraitMacroItem", 2, $1, $2); }
;
trait_const
: maybe_outer_attrs CONST ident maybe_ty_ascription maybe_const_default ';' { $$ = mk_node("ConstTraitItem", 4, $1, $3, $4, $5); }
;
maybe_const_default
: '=' expr { $$ = mk_node("ConstDefault", 1, $2); }
| %empty { $$ = mk_none(); }
;
trait_type
: maybe_outer_attrs TYPE ty_param ';' { $$ = mk_node("TypeTraitItem", 2, $1, $3); }
;
maybe_unsafe
: UNSAFE { $$ = mk_atom("Unsafe"); }
| %empty { $$ = mk_none(); }
;
maybe_default_maybe_unsafe
: DEFAULT UNSAFE { $$ = mk_atom("DefaultUnsafe"); }
| DEFAULT { $$ = mk_atom("Default"); }
| UNSAFE { $$ = mk_atom("Unsafe"); }
| %empty { $$ = mk_none(); }
trait_method
: type_method { $$ = mk_node("Required", 1, $1); }
| method { $$ = mk_node("Provided", 1, $1); }
;
type_method
: maybe_outer_attrs maybe_unsafe FN ident generic_params fn_decl_with_self_allow_anon_params maybe_where_clause ';'
{
$$ = mk_node("TypeMethod", 6, $1, $2, $4, $5, $6, $7);
}
| maybe_outer_attrs CONST maybe_unsafe FN ident generic_params fn_decl_with_self_allow_anon_params maybe_where_clause ';'
{
$$ = mk_node("TypeMethod", 6, $1, $3, $5, $6, $7, $8);
}
| maybe_outer_attrs maybe_unsafe EXTERN maybe_abi FN ident generic_params fn_decl_with_self_allow_anon_params maybe_where_clause ';'
{
$$ = mk_node("TypeMethod", 7, $1, $2, $4, $6, $7, $8, $9);
}
;
method
: maybe_outer_attrs maybe_unsafe FN ident generic_params fn_decl_with_self_allow_anon_params maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("Method", 7, $1, $2, $4, $5, $6, $7, $8);
}
| maybe_outer_attrs CONST maybe_unsafe FN ident generic_params fn_decl_with_self_allow_anon_params maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("Method", 7, $1, $3, $5, $6, $7, $8, $9);
}
| maybe_outer_attrs maybe_unsafe EXTERN maybe_abi FN ident generic_params fn_decl_with_self_allow_anon_params maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("Method", 8, $1, $2, $4, $6, $7, $8, $9, $10);
}
;
impl_method
: attrs_and_vis maybe_default maybe_unsafe FN ident generic_params fn_decl_with_self maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("Method", 8, $1, $2, $3, $5, $6, $7, $8, $9);
}
| attrs_and_vis maybe_default CONST maybe_unsafe FN ident generic_params fn_decl_with_self maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("Method", 8, $1, $2, $4, $6, $7, $8, $9, $10);
}
| attrs_and_vis maybe_default maybe_unsafe EXTERN maybe_abi FN ident generic_params fn_decl_with_self maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("Method", 9, $1, $2, $3, $5, $7, $8, $9, $10, $11);
}
;
// There are two forms of impl:
//
// impl (<...>)? TY { ... }
// impl (<...>)? TRAIT for TY { ... }
//
// Unfortunately since TY can begin with '<' itself -- as part of a
// TyQualifiedPath type -- there's an s/r conflict when we see '<' after IMPL:
// should we reduce one of the early rules of TY (such as maybe_once)
// or shall we continue shifting into the generic_params list for the
// impl?
//
// The production parser disambiguates a different case here by
// permitting / requiring the user to provide parens around types when
// they are ambiguous with traits. We do the same here, regrettably,
// by splitting ty into ty and ty_prim.
item_impl
: maybe_default_maybe_unsafe IMPL generic_params ty_prim_sum maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
{
$$ = mk_node("ItemImpl", 6, $1, $3, $4, $5, $7, $8);
}
| maybe_default_maybe_unsafe IMPL generic_params '(' ty ')' maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
{
$$ = mk_node("ItemImpl", 6, $1, $3, 5, $6, $9, $10);
}
| maybe_default_maybe_unsafe IMPL generic_params trait_ref FOR ty_sum maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
{
$$ = mk_node("ItemImpl", 6, $3, $4, $6, $7, $9, $10);
}
| maybe_default_maybe_unsafe IMPL generic_params '!' trait_ref FOR ty_sum maybe_where_clause '{' maybe_inner_attrs maybe_impl_items '}'
{
$$ = mk_node("ItemImplNeg", 7, $1, $3, $5, $7, $8, $10, $11);
}
| maybe_default_maybe_unsafe IMPL generic_params trait_ref FOR DOTDOT '{' '}'
{
$$ = mk_node("ItemImplDefault", 3, $1, $3, $4);
}
| maybe_default_maybe_unsafe IMPL generic_params '!' trait_ref FOR DOTDOT '{' '}'
{
$$ = mk_node("ItemImplDefaultNeg", 3, $1, $3, $4);
}
;
maybe_impl_items
: impl_items
| %empty { $$ = mk_none(); }
;
impl_items
: impl_item { $$ = mk_node("ImplItems", 1, $1); }
| impl_item impl_items { $$ = ext_node($1, 1, $2); }
;
impl_item
: impl_method
| attrs_and_vis item_macro { $$ = mk_node("ImplMacroItem", 2, $1, $2); }
| impl_const
| impl_type
;
maybe_default
: DEFAULT { $$ = mk_atom("Default"); }
| %empty { $$ = mk_none(); }
;
impl_const
: attrs_and_vis maybe_default item_const { $$ = mk_node("ImplConst", 3, $1, $2, $3); }
;
impl_type
: attrs_and_vis maybe_default TYPE ident generic_params '=' ty_sum ';' { $$ = mk_node("ImplType", 5, $1, $2, $4, $5, $7); }
;
item_fn
: FN ident generic_params fn_decl maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("ItemFn", 5, $2, $3, $4, $5, $6);
}
| CONST FN ident generic_params fn_decl maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("ItemFn", 5, $3, $4, $5, $6, $7);
}
;
item_unsafe_fn
: UNSAFE FN ident generic_params fn_decl maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("ItemUnsafeFn", 5, $3, $4, $5, $6, $7);
}
| CONST UNSAFE FN ident generic_params fn_decl maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("ItemUnsafeFn", 5, $4, $5, $6, $7, $8);
}
| UNSAFE EXTERN maybe_abi FN ident generic_params fn_decl maybe_where_clause inner_attrs_and_block
{
$$ = mk_node("ItemUnsafeFn", 6, $3, $5, $6, $7, $8, $9);
}
;
fn_decl
: fn_params ret_ty { $$ = mk_node("FnDecl", 2, $1, $2); }
;
fn_decl_with_self
: fn_params_with_self ret_ty { $$ = mk_node("FnDecl", 2, $1, $2); }
;
fn_decl_with_self_allow_anon_params
: fn_anon_params_with_self ret_ty { $$ = mk_node("FnDecl", 2, $1, $2); }
;
fn_params
: '(' maybe_params ')' { $$ = $2; }
;
fn_anon_params
: '(' anon_param anon_params_allow_variadic_tail ')' { $$ = ext_node($2, 1, $3); }
| '(' ')' { $$ = mk_none(); }
;
fn_params_with_self
: '(' maybe_mut SELF maybe_ty_ascription maybe_comma_params ')' { $$ = mk_node("SelfValue", 3, $2, $4, $5); }
| '(' '&' maybe_mut SELF maybe_ty_ascription maybe_comma_params ')' { $$ = mk_node("SelfRegion", 3, $3, $5, $6); }
| '(' '&' lifetime maybe_mut SELF maybe_ty_ascription maybe_comma_params ')' { $$ = mk_node("SelfRegion", 4, $3, $4, $6, $7); }
| '(' maybe_params ')' { $$ = mk_node("SelfStatic", 1, $2); }
;
fn_anon_params_with_self
: '(' maybe_mut SELF maybe_ty_ascription maybe_comma_anon_params ')' { $$ = mk_node("SelfValue", 3, $2, $4, $5); }
| '(' '&' maybe_mut SELF maybe_ty_ascription maybe_comma_anon_params ')' { $$ = mk_node("SelfRegion", 3, $3, $5, $6); }
| '(' '&' lifetime maybe_mut SELF maybe_ty_ascription maybe_comma_anon_params ')' { $$ = mk_node("SelfRegion", 4, $3, $4, $6, $7); }
| '(' maybe_anon_params ')' { $$ = mk_node("SelfStatic", 1, $2); }
;
maybe_params
: params
| params ','
| %empty { $$ = mk_none(); }
;
params
: param { $$ = mk_node("Args", 1, $1); }
| params ',' param { $$ = ext_node($1, 1, $3); }
;
param
: pat ':' ty_sum { $$ = mk_node("Arg", 2, $1, $3); }
;
inferrable_params
: inferrable_param { $$ = mk_node("InferrableParams", 1, $1); }
| inferrable_params ',' inferrable_param { $$ = ext_node($1, 1, $3); }
;
inferrable_param
: pat maybe_ty_ascription { $$ = mk_node("InferrableParam", 2, $1, $2); }
;
maybe_comma_params
: ',' { $$ = mk_none(); }
| ',' params { $$ = $2; }
| ',' params ',' { $$ = $2; }
| %empty { $$ = mk_none(); }
;
maybe_comma_anon_params
: ',' { $$ = mk_none(); }
| ',' anon_params { $$ = $2; }
| ',' anon_params ',' { $$ = $2; }
| %empty { $$ = mk_none(); }
;
maybe_anon_params
: anon_params
| anon_params ','
| %empty { $$ = mk_none(); }
;
anon_params
: anon_param { $$ = mk_node("Args", 1, $1); }
| anon_params ',' anon_param { $$ = ext_node($1, 1, $3); }
;
// anon means it's allowed to be anonymous (type-only), but it can
// still have a name
anon_param
: named_arg ':' ty { $$ = mk_node("Arg", 2, $1, $3); }
| ty
;
anon_params_allow_variadic_tail
: ',' DOTDOTDOT { $$ = mk_none(); }
| ',' anon_param anon_params_allow_variadic_tail { $$ = mk_node("Args", 2, $2, $3); }
| %empty { $$ = mk_none(); }
;
named_arg
: ident
| UNDERSCORE { $$ = mk_atom("PatWild"); }
| '&' ident { $$ = $2; }
| '&' UNDERSCORE { $$ = mk_atom("PatWild"); }
| ANDAND ident { $$ = $2; }
| ANDAND UNDERSCORE { $$ = mk_atom("PatWild"); }
| MUT ident { $$ = $2; }
;
ret_ty
: RARROW '!' { $$ = mk_none(); }
| RARROW ty { $$ = mk_node("ret-ty", 1, $2); }
| %prec IDENT %empty { $$ = mk_none(); }
;
generic_params
: '<' '>' { $$ = mk_node("Generics", 2, mk_none(), mk_none()); }
| '<' lifetimes '>' { $$ = mk_node("Generics", 2, $2, mk_none()); }
| '<' lifetimes ',' '>' { $$ = mk_node("Generics", 2, $2, mk_none()); }
| '<' lifetimes SHR { push_back('>'); $$ = mk_node("Generics", 2, $2, mk_none()); }
| '<' lifetimes ',' SHR { push_back('>'); $$ = mk_node("Generics", 2, $2, mk_none()); }
| '<' lifetimes ',' ty_params '>' { $$ = mk_node("Generics", 2, $2, $4); }
| '<' lifetimes ',' ty_params ',' '>' { $$ = mk_node("Generics", 2, $2, $4); }
| '<' lifetimes ',' ty_params SHR { push_back('>'); $$ = mk_node("Generics", 2, $2, $4); }
| '<' lifetimes ',' ty_params ',' SHR { push_back('>'); $$ = mk_node("Generics", 2, $2, $4); }
| '<' ty_params '>' { $$ = mk_node("Generics", 2, mk_none(), $2); }
| '<' ty_params ',' '>' { $$ = mk_node("Generics", 2, mk_none(), $2); }
| '<' ty_params SHR { push_back('>'); $$ = mk_node("Generics", 2, mk_none(), $2); }
| '<' ty_params ',' SHR { push_back('>'); $$ = mk_node("Generics", 2, mk_none(), $2); }
| %empty { $$ = mk_none(); }
;
maybe_where_clause
: %empty { $$ = mk_none(); }
| where_clause
;
where_clause
: WHERE where_predicates { $$ = mk_node("WhereClause", 1, $2); }
| WHERE where_predicates ',' { $$ = mk_node("WhereClause", 1, $2); }
;
where_predicates
: where_predicate { $$ = mk_node("WherePredicates", 1, $1); }
| where_predicates ',' where_predicate { $$ = ext_node($1, 1, $3); }
;
where_predicate
: maybe_for_lifetimes lifetime ':' bounds { $$ = mk_node("WherePredicate", 3, $1, $2, $4); }
| maybe_for_lifetimes ty ':' ty_param_bounds { $$ = mk_node("WherePredicate", 3, $1, $2, $4); }
;
maybe_for_lifetimes
: FOR '<' lifetimes '>' { $$ = mk_none(); }
| %prec FORTYPE %empty { $$ = mk_none(); }
ty_params
: ty_param { $$ = mk_node("TyParams", 1, $1); }
| ty_params ',' ty_param { $$ = ext_node($1, 1, $3); }
;
// A path with no type parameters; e.g. `foo::bar::Baz`
//
// These show up in 'use' view-items, because these are processed
// without respect to types.
path_no_types_allowed
: ident { $$ = mk_node("ViewPath", 1, $1); }
| MOD_SEP ident { $$ = mk_node("ViewPath", 1, $2); }
| SELF { $$ = mk_node("ViewPath", 1, mk_atom("Self")); }
| MOD_SEP SELF { $$ = mk_node("ViewPath", 1, mk_atom("Self")); }
| SUPER { $$ = mk_node("ViewPath", 1, mk_atom("Super")); }
| MOD_SEP SUPER { $$ = mk_node("ViewPath", 1, mk_atom("Super")); }
| path_no_types_allowed MOD_SEP ident { $$ = ext_node($1, 1, $3); }
;
// A path with a lifetime and type parameters, with no double colons
// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`
//
// These show up in "trait references", the components of
// type-parameter bounds lists, as well as in the prefix of the
// path_generic_args_and_bounds rule, which is the full form of a
// named typed expression.
//
// They do not have (nor need) an extra '::' before '<' because
// unlike in expr context, there are no "less-than" type exprs to
// be ambiguous with.
path_generic_args_without_colons
: %prec IDENT
ident { $$ = mk_node("components", 1, $1); }
| %prec IDENT
ident generic_args { $$ = mk_node("components", 2, $1, $2); }
| %prec IDENT
ident '(' maybe_ty_sums ')' ret_ty { $$ = mk_node("components", 2, $1, $3); }
| %prec IDENT
path_generic_args_without_colons MOD_SEP ident { $$ = ext_node($1, 1, $3); }
| %prec IDENT
path_generic_args_without_colons MOD_SEP ident generic_args { $$ = ext_node($1, 2, $3, $4); }
| %prec IDENT
path_generic_args_without_colons MOD_SEP ident '(' maybe_ty_sums ')' ret_ty { $$ = ext_node($1, 2, $3, $5); }
;
generic_args
: '<' generic_values '>' { $$ = $2; }
| '<' generic_values SHR { push_back('>'); $$ = $2; }
| '<' generic_values GE { push_back('='); $$ = $2; }
| '<' generic_values SHREQ { push_back('>'); push_back('='); $$ = $2; }
// If generic_args starts with "<<", the first arg must be a
// TyQualifiedPath because that's the only type that can start with a
// '<'. This rule parses that as the first ty_sum and then continues
// with the rest of generic_values.
| SHL ty_qualified_path_and_generic_values '>' { $$ = $2; }
| SHL ty_qualified_path_and_generic_values SHR { push_back('>'); $$ = $2; }
| SHL ty_qualified_path_and_generic_values GE { push_back('='); $$ = $2; }
| SHL ty_qualified_path_and_generic_values SHREQ { push_back('>'); push_back('='); $$ = $2; }
;
generic_values
: maybe_ty_sums_and_or_bindings { $$ = mk_node("GenericValues", 1, $1); }
;
maybe_ty_sums_and_or_bindings
: ty_sums
| ty_sums ','
| ty_sums ',' bindings { $$ = mk_node("TySumsAndBindings", 2, $1, $3); }
| bindings
| bindings ','
| %empty { $$ = mk_none(); }
;
maybe_bindings
: ',' bindings { $$ = $2; }
| %empty { $$ = mk_none(); }
;
////////////////////////////////////////////////////////////////////////
// Part 2: Patterns
////////////////////////////////////////////////////////////////////////
pat
: UNDERSCORE { $$ = mk_atom("PatWild"); }
| '&' pat { $$ = mk_node("PatRegion", 1, $2); }
| '&' MUT pat { $$ = mk_node("PatRegion", 1, $3); }
| ANDAND pat { $$ = mk_node("PatRegion", 1, mk_node("PatRegion", 1, $2)); }
| '(' ')' { $$ = mk_atom("PatUnit"); }
| '(' pat_tup ')' { $$ = mk_node("PatTup", 1, $2); }
| '[' pat_vec ']' { $$ = mk_node("PatVec", 1, $2); }
| lit_or_path
| lit_or_path DOTDOTDOT lit_or_path { $$ = mk_node("PatRange", 2, $1, $3); }
| path_expr '{' pat_struct '}' { $$ = mk_node("PatStruct", 2, $1, $3); }
| path_expr '(' ')' { $$ = mk_node("PatEnum", 2, $1, mk_none()); }
| path_expr '(' pat_tup ')' { $$ = mk_node("PatEnum", 2, $1, $3); }
| path_expr '!' maybe_ident delimited_token_trees { $$ = mk_node("PatMac", 3, $1, $3, $4); }
| binding_mode ident { $$ = mk_node("PatIdent", 2, $1, $2); }
| ident '@' pat { $$ = mk_node("PatIdent", 3, mk_node("BindByValue", 1, mk_atom("MutImmutable")), $1, $3); }
| binding_mode ident '@' pat { $$ = mk_node("PatIdent", 3, $1, $2, $4); }
| BOX pat { $$ = mk_node("PatUniq", 1, $2); }
| '<' ty_sum maybe_as_trait_ref '>' MOD_SEP ident { $$ = mk_node("PatQualifiedPath", 3, $2, $3, $6); }
| SHL ty_sum maybe_as_trait_ref '>' MOD_SEP ident maybe_as_trait_ref '>' MOD_SEP ident
{
$$ = mk_node("PatQualifiedPath", 3, mk_node("PatQualifiedPath", 3, $2, $3, $6), $7, $10);
}
;
pats_or
: pat { $$ = mk_node("Pats", 1, $1); }
| pats_or '|' pat { $$ = ext_node($1, 1, $3); }
;
binding_mode
: REF { $$ = mk_node("BindByRef", 1, mk_atom("MutImmutable")); }
| REF MUT { $$ = mk_node("BindByRef", 1, mk_atom("MutMutable")); }
| MUT { $$ = mk_node("BindByValue", 1, mk_atom("MutMutable")); }
;
lit_or_path
: path_expr { $$ = mk_node("PatLit", 1, $1); }
| lit { $$ = mk_node("PatLit", 1, $1); }
| '-' lit { $$ = mk_node("PatLit", 1, $2); }
;
pat_field