-
Notifications
You must be signed in to change notification settings - Fork 3k
/
erl_parse.yrl
2296 lines (1891 loc) · 78.3 KB
/
erl_parse.yrl
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
%% -*- erlang -*-
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2024. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%% Definition of the Erlang grammar.
Nonterminals
form
attribute attr_val
function function_clauses function_clause
clause_args clause_guard clause_body
expr expr_max expr_remote
pat_expr pat_expr_max map_pat_expr record_pat_expr
pat_argument_list pat_exprs
list tail
list_comprehension lc_expr lc_exprs
map_comprehension
binary_comprehension
tuple
record_expr record_tuple record_field record_fields
map_expr map_tuple map_field map_field_assoc map_field_exact map_fields map_key
if_expr if_clause if_clauses case_expr cr_clause cr_clauses receive_expr
fun_expr fun_clause fun_clauses atom_or_var integer_or_var
try_expr try_catch try_clause try_clauses try_opt_stacktrace
function_call argument_list
exprs guard
atomic strings
prefix_op mult_op add_op list_op comp_op
binary bin_elements bin_element bit_expr sigil
opt_bit_size_expr bit_size_expr opt_bit_type_list bit_type_list bit_type
top_type top_types type typed_expr typed_attr_val
type_sig type_sigs type_guard type_guards fun_type binary_type
type_spec spec_fun typed_exprs typed_record_fields field_types field_type
map_pair_types map_pair_type
bin_base_type bin_unit_type
maybe_expr maybe_match_exprs maybe_match
clause_body_exprs
ssa_check_anno
ssa_check_anno_clause
ssa_check_anno_clauses
ssa_check_args
ssa_check_binary_lit
ssa_check_binary_lit_bytes_ls
ssa_check_binary_lit_rest
ssa_check_clause_args
ssa_check_clause_args_ls
ssa_check_expr
ssa_check_exprs
ssa_check_fun_ref
ssa_check_list_lit
ssa_check_list_lit_ls
ssa_check_map_key
ssa_check_map_key_element
ssa_check_map_key_elements
ssa_check_map_key_list
ssa_check_map_key_tuple_elements
ssa_check_pat
ssa_check_pats
ssa_check_when_clause
ssa_check_when_clauses.
Terminals
char integer float atom sigil_prefix string sigil_suffix var
'(' ')' ',' '->' '{' '}' '[' ']' '|' '||' '<-' ';' ':' '#' '.'
'after' 'begin' 'case' 'try' 'catch' 'end' 'fun' 'if' 'of' 'receive' 'when'
'maybe' 'else'
'andalso' 'orelse'
'bnot' 'not'
'*' '/' 'div' 'rem' 'band' 'and'
'+' '-' 'bor' 'bxor' 'bsl' 'bsr' 'or' 'xor'
'++' '--'
'==' '/=' '=<' '<' '>=' '>' '=:=' '=/=' '<=' '=>' ':='
'<<' '>>'
'!' '=' '::' '..' '...'
'?='
'spec' 'callback' % helper
dot
'%ssa%'.
Expect 0.
Rootsymbol form.
%% Expressions
Unary 0 'catch'.
Right 100 '=' '!'.
Right 150 'orelse'.
Right 160 'andalso'.
Nonassoc 200 comp_op.
Right 300 list_op.
Left 400 add_op.
Left 500 mult_op.
Unary 600 prefix_op.
Nonassoc 700 '#'.
Nonassoc 800 ':'.
Nonassoc 900 clause_body_exprs.
%% Types
Right 150 '::'.
Left 170 '|'.
Nonassoc 200 '..'.
Nonassoc 500 '*'. % for binary expressions
form -> attribute dot : '$1'.
form -> function dot : '$1'.
attribute -> '-' atom attr_val : build_attribute('$2', '$3').
attribute -> '-' atom typed_attr_val : build_typed_attribute('$2','$3').
attribute -> '-' atom '(' typed_attr_val ')' : build_typed_attribute('$2','$4').
attribute -> '-' 'spec' type_spec : build_type_spec('$2', '$3').
attribute -> '-' 'callback' type_spec : build_type_spec('$2', '$3').
type_spec -> spec_fun type_sigs : {'$1', '$2'}.
type_spec -> '(' spec_fun type_sigs ')' : {'$2', '$3'}.
spec_fun -> atom : '$1'.
spec_fun -> atom ':' atom : {'$1', '$3'}.
typed_attr_val -> expr ',' typed_record_fields : {typed_record, '$1', '$3'}.
typed_attr_val -> expr '::' top_type : {type_def, '$1', '$3'}.
typed_record_fields -> '{' typed_exprs '}' : {tuple, ?anno('$1'), '$2'}.
typed_exprs -> typed_expr : ['$1'].
typed_exprs -> typed_expr ',' typed_exprs : ['$1'|'$3'].
typed_exprs -> expr ',' typed_exprs : ['$1'|'$3'].
typed_exprs -> typed_expr ',' exprs : ['$1'|'$3'].
typed_expr -> expr '::' top_type : {typed,'$1','$3'}.
type_sigs -> type_sig : ['$1'].
type_sigs -> type_sig ';' type_sigs : ['$1'|'$3'].
type_sig -> fun_type : '$1'.
type_sig -> fun_type 'when' type_guards : {type, ?anno('$1'), bounded_fun,
['$1','$3']}.
type_guards -> type_guard : ['$1'].
type_guards -> type_guard ',' type_guards : ['$1'|'$3'].
type_guard -> atom '(' top_types ')' : build_compat_constraint('$1', '$3').
type_guard -> var '::' top_type : build_constraint('$1', '$3').
top_types -> top_type : ['$1'].
top_types -> top_type ',' top_types : ['$1'|'$3'].
top_type -> var '::' top_type : {ann_type, ?anno('$1'), ['$1','$3']}.
top_type -> type '|' top_type : lift_unions('$1','$3').
top_type -> type : '$1'.
type -> type '..' type : {type, ?anno('$1'), range, ['$1', '$3']}.
type -> type add_op type : ?mkop2('$1', '$2', '$3').
type -> type mult_op type : ?mkop2('$1', '$2', '$3').
type -> prefix_op type : ?mkop1('$1', '$2').
type -> '(' top_type ')' : '$2'.
type -> var : '$1'.
type -> atom : '$1'.
type -> atom '(' ')' : build_gen_type('$1').
type -> atom '(' top_types ')' : build_type('$1', '$3').
type -> atom ':' atom '(' ')' : {remote_type, ?anno('$1'),
['$1', '$3', []]}.
type -> atom ':' atom '(' top_types ')' : {remote_type, ?anno('$1'),
['$1', '$3', '$5']}.
type -> '[' ']' : {type, ?anno('$1'), nil, []}.
type -> '[' top_type ']' : {type, ?anno('$1'), list, ['$2']}.
type -> '[' top_type ',' '...' ']' : {type, ?anno('$1'),
nonempty_list, ['$2']}.
type -> '#' '{' '}' : {type, ?anno('$1'), map, []}.
type -> '#' '{' map_pair_types '}' : {type, ?anno('$1'), map, '$3'}.
type -> '{' '}' : {type, ?anno('$1'), tuple, []}.
type -> '{' top_types '}' : {type, ?anno('$1'), tuple, '$2'}.
type -> '#' atom '{' '}' : {type, ?anno('$1'), record, ['$2']}.
type -> '#' atom '{' field_types '}' : {type, ?anno('$1'),
record, ['$2'|'$4']}.
type -> binary_type : '$1'.
type -> integer : '$1'.
type -> char : '$1'.
type -> 'fun' '(' ')' : {type, ?anno('$1'), 'fun', []}.
type -> 'fun' '(' fun_type ')' : '$3'.
fun_type -> '(' '...' ')' '->' top_type : {type, ?anno('$1'), 'fun',
[{type, ?anno('$1'), any}, '$5']}.
fun_type -> '(' ')' '->' top_type : {type, ?anno('$1'), 'fun',
[{type, ?anno('$1'), product, []}, '$4']}.
fun_type -> '(' top_types ')' '->' top_type
: {type, ?anno('$1'), 'fun',
[{type, ?anno('$1'), product, '$2'},'$5']}.
map_pair_types -> map_pair_type : ['$1'].
map_pair_types -> map_pair_type ',' map_pair_types : ['$1'|'$3'].
map_pair_type -> top_type '=>' top_type : {type, ?anno('$2'),
map_field_assoc,['$1','$3']}.
map_pair_type -> top_type ':=' top_type : {type, ?anno('$2'),
map_field_exact,['$1','$3']}.
field_types -> field_type : ['$1'].
field_types -> field_type ',' field_types : ['$1'|'$3'].
field_type -> atom '::' top_type : {type, ?anno('$1'), field_type,
['$1', '$3']}.
binary_type -> '<<' '>>' : {type, ?anno('$1'),binary,
[abstract2(0, ?anno('$1')),
abstract2(0, ?anno('$1'))]}.
binary_type -> '<<' bin_base_type '>>' : {type, ?anno('$1'),binary,
['$2', abstract2(0, ?anno('$1'))]}.
binary_type -> '<<' bin_unit_type '>>' : {type, ?anno('$1'),binary,
[abstract2(0, ?anno('$1')), '$2']}.
binary_type -> '<<' bin_base_type ',' bin_unit_type '>>'
: {type, ?anno('$1'), binary, ['$2', '$4']}.
bin_base_type -> var ':' type : build_bin_type(['$1'], '$3').
bin_unit_type -> var ':' var '*' type : build_bin_type(['$1', '$3'], '$5').
attr_val -> expr : ['$1'].
attr_val -> expr ',' exprs : ['$1' | '$3'].
attr_val -> '(' expr ',' exprs ')' : ['$2' | '$4'].
function -> function_clauses : build_function('$1').
function_clauses -> function_clause : ['$1'].
function_clauses -> function_clause ';' function_clauses : ['$1'|'$3'].
function_clause -> atom clause_args clause_guard clause_body :
{clause,?anno('$1'),element(3, '$1'),'$2','$3','$4'}.
clause_args -> pat_argument_list : element(1, '$1').
clause_guard -> 'when' guard : '$2'.
clause_guard -> '$empty' : [].
clause_body -> '->' clause_body_exprs: '$2'.
expr -> 'catch' expr : {'catch',?anno('$1'),'$2'}.
expr -> expr '=' expr : {match,first_anno('$1'),'$1','$3'}.
expr -> expr '!' expr : ?mkop2('$1', '$2', '$3').
expr -> expr 'orelse' expr : ?mkop2('$1', '$2', '$3').
expr -> expr 'andalso' expr : ?mkop2('$1', '$2', '$3').
expr -> expr comp_op expr : ?mkop2('$1', '$2', '$3').
expr -> expr list_op expr : ?mkop2('$1', '$2', '$3').
expr -> expr add_op expr : ?mkop2('$1', '$2', '$3').
expr -> expr mult_op expr : ?mkop2('$1', '$2', '$3').
expr -> prefix_op expr : ?mkop1('$1', '$2').
expr -> map_expr : '$1'.
expr -> function_call : '$1'.
expr -> record_expr : '$1'.
expr -> expr_remote : '$1'.
expr_remote -> expr_max ':' expr_max : {remote,?anno('$2'),'$1','$3'}.
expr_remote -> expr_max : '$1'.
expr_max -> var : '$1'.
expr_max -> atomic : '$1'.
expr_max -> list : '$1'.
expr_max -> binary : '$1'.
expr_max -> sigil : '$1'.
expr_max -> list_comprehension : '$1'.
expr_max -> map_comprehension : '$1'.
expr_max -> binary_comprehension : '$1'.
expr_max -> tuple : '$1'.
expr_max -> '(' expr ')' : '$2'.
expr_max -> 'begin' exprs 'end' : {block,?anno('$1'),'$2'}.
expr_max -> if_expr : '$1'.
expr_max -> case_expr : '$1'.
expr_max -> receive_expr : '$1'.
expr_max -> fun_expr : '$1'.
expr_max -> try_expr : '$1'.
expr_max -> maybe_expr : '$1'.
pat_expr -> pat_expr '=' pat_expr : {match,first_anno('$1'),'$1','$3'}.
pat_expr -> pat_expr comp_op pat_expr : ?mkop2('$1', '$2', '$3').
pat_expr -> pat_expr list_op pat_expr : ?mkop2('$1', '$2', '$3').
pat_expr -> pat_expr add_op pat_expr : ?mkop2('$1', '$2', '$3').
pat_expr -> pat_expr mult_op pat_expr : ?mkop2('$1', '$2', '$3').
pat_expr -> prefix_op pat_expr : ?mkop1('$1', '$2').
pat_expr -> map_pat_expr : '$1'.
pat_expr -> record_pat_expr : '$1'.
pat_expr -> pat_expr_max : '$1'.
pat_expr_max -> var : '$1'.
pat_expr_max -> atomic : '$1'.
pat_expr_max -> list : '$1'.
pat_expr_max -> binary : '$1'.
pat_expr_max -> sigil : '$1'.
pat_expr_max -> tuple : '$1'.
pat_expr_max -> '(' pat_expr ')' : '$2'.
map_pat_expr -> '#' map_tuple :
{map, ?anno('$1'),'$2'}.
record_pat_expr -> '#' atom '.' atom :
{record_index,?anno('$1'),element(3, '$2'),'$4'}.
record_pat_expr -> '#' atom record_tuple :
{record,?anno('$1'),element(3, '$2'),'$3'}.
list -> '[' ']' : {nil,?anno('$1')}.
list -> '[' expr tail : {cons,?anno('$1'),'$2','$3'}.
tail -> ']' : {nil,?anno('$1')}.
tail -> '|' expr ']' : '$2'.
tail -> ',' expr tail : {cons,first_anno('$2'),'$2','$3'}.
binary -> '<<' '>>' : {bin,?anno('$1'),[]}.
binary -> '<<' bin_elements '>>' : {bin,?anno('$1'),'$2'}.
bin_elements -> bin_element : ['$1'].
bin_elements -> bin_element ',' bin_elements : ['$1'|'$3'].
bin_element -> bit_expr opt_bit_size_expr opt_bit_type_list :
{bin_element,first_anno('$1'),'$1','$2','$3'}.
bit_expr -> prefix_op expr_max : ?mkop1('$1', '$2').
bit_expr -> expr_max : '$1'.
opt_bit_size_expr -> ':' bit_size_expr : '$2'.
opt_bit_size_expr -> '$empty' : default.
opt_bit_type_list -> '/' bit_type_list : '$2'.
opt_bit_type_list -> '$empty' : default.
bit_type_list -> bit_type '-' bit_type_list : ['$1' | '$3'].
bit_type_list -> bit_type : ['$1'].
bit_type -> atom : element(3,'$1').
bit_type -> atom ':' integer : { element(3,'$1'), element(3,'$3') }.
bit_size_expr -> expr_max : '$1'.
sigil -> sigil_prefix string sigil_suffix : build_sigil('$1', '$2', '$3').
list_comprehension -> '[' expr '||' lc_exprs ']' :
{lc,?anno('$1'),'$2','$4'}.
map_comprehension -> '#' '{' map_field_assoc '||' lc_exprs '}' :
{mc,?anno('$1'),'$3','$5'}.
binary_comprehension -> '<<' expr_max '||' lc_exprs '>>' :
{bc,?anno('$1'),'$2','$4'}.
lc_exprs -> lc_expr : ['$1'].
lc_exprs -> lc_expr ',' lc_exprs : ['$1'|'$3'].
lc_expr -> expr : '$1'.
lc_expr -> map_field_exact '<-' expr : {m_generate,?anno('$2'),'$1','$3'}.
lc_expr -> expr '<-' expr : {generate,?anno('$2'),'$1','$3'}.
lc_expr -> binary '<=' expr : {b_generate,?anno('$2'),'$1','$3'}.
tuple -> '{' '}' : {tuple,?anno('$1'),[]}.
tuple -> '{' exprs '}' : {tuple,?anno('$1'),'$2'}.
map_expr -> '#' map_tuple :
{map, ?anno('$1'),'$2'}.
map_expr -> expr_max '#' map_tuple :
{map, ?anno('$2'),'$1','$3'}.
map_expr -> map_expr '#' map_tuple :
{map, ?anno('$2'),'$1','$3'}.
map_tuple -> '{' '}' : [].
map_tuple -> '{' map_fields '}' : '$2'.
map_fields -> map_field : ['$1'].
map_fields -> map_field ',' map_fields : ['$1' | '$3'].
map_field -> map_field_assoc : '$1'.
map_field -> map_field_exact : '$1'.
map_field_assoc -> map_key '=>' expr :
{map_field_assoc,?anno('$2'),'$1','$3'}.
map_field_exact -> map_key ':=' expr :
{map_field_exact,?anno('$2'),'$1','$3'}.
map_key -> expr : '$1'.
%% N.B. This is called from expr.
%% N.B. Field names are returned as the complete object, even if they are
%% always atoms for the moment, this might change in the future.
record_expr -> '#' atom '.' atom :
{record_index,?anno('$1'),element(3, '$2'),'$4'}.
record_expr -> '#' atom record_tuple :
{record,?anno('$1'),element(3, '$2'),'$3'}.
record_expr -> expr_max '#' atom '.' atom :
{record_field,?anno('$2'),'$1',element(3, '$3'),'$5'}.
record_expr -> expr_max '#' atom record_tuple :
{record,?anno('$2'),'$1',element(3, '$3'),'$4'}.
record_expr -> record_expr '#' atom '.' atom :
{record_field,?anno('$2'),'$1',element(3, '$3'),'$5'}.
record_expr -> record_expr '#' atom record_tuple :
{record,?anno('$2'),'$1',element(3, '$3'),'$4'}.
record_tuple -> '{' '}' : [].
record_tuple -> '{' record_fields '}' : '$2'.
record_fields -> record_field : ['$1'].
record_fields -> record_field ',' record_fields : ['$1' | '$3'].
record_field -> var '=' expr : {record_field,?anno('$1'),'$1','$3'}.
record_field -> atom '=' expr : {record_field,?anno('$1'),'$1','$3'}.
%% N.B. This is called from expr.
function_call -> expr_remote argument_list :
{call,first_anno('$1'),'$1',element(1, '$2')}.
if_expr -> 'if' if_clauses 'end' : {'if',?anno('$1'),'$2'}.
if_clauses -> if_clause : ['$1'].
if_clauses -> if_clause ';' if_clauses : ['$1' | '$3'].
if_clause -> guard clause_body :
{clause,first_anno(hd(hd('$1'))),[],'$1','$2'}.
case_expr -> 'case' expr 'of' cr_clauses 'end' :
{'case',?anno('$1'),'$2','$4'}.
cr_clauses -> cr_clause : ['$1'].
cr_clauses -> cr_clause ';' cr_clauses : ['$1' | '$3'].
%% FIXME: merl in syntax_tools depends on patterns in a 'case' being
%% full expressions. Therefore, we can't use pat_expr here. There
%% should be a better way.
cr_clause -> expr clause_guard clause_body :
{clause,first_anno('$1'),['$1'],'$2','$3'}.
receive_expr -> 'receive' cr_clauses 'end' :
{'receive',?anno('$1'),'$2'}.
receive_expr -> 'receive' 'after' expr clause_body 'end' :
{'receive',?anno('$1'),[],'$3','$4'}.
receive_expr -> 'receive' cr_clauses 'after' expr clause_body 'end' :
{'receive',?anno('$1'),'$2','$4','$5'}.
fun_expr -> 'fun' atom '/' integer :
{'fun',?anno('$1'),{function,element(3, '$2'),element(3, '$4')}}.
fun_expr -> 'fun' atom_or_var ':' atom_or_var '/' integer_or_var :
{'fun',?anno('$1'),{function,'$2','$4','$6'}}.
fun_expr -> 'fun' fun_clauses 'end' :
build_fun(?anno('$1'), '$2').
atom_or_var -> atom : '$1'.
atom_or_var -> var : '$1'.
integer_or_var -> integer : '$1'.
integer_or_var -> var : '$1'.
fun_clauses -> fun_clause : ['$1'].
fun_clauses -> fun_clause ';' fun_clauses : ['$1' | '$3'].
fun_clause -> pat_argument_list clause_guard clause_body :
{Args,Anno} = '$1',
{clause,Anno,'fun',Args,'$2','$3'}.
fun_clause -> var pat_argument_list clause_guard clause_body :
{clause,?anno('$1'),element(3, '$1'),element(1, '$2'),'$3','$4'}.
try_expr -> 'try' exprs 'of' cr_clauses try_catch :
build_try(?anno('$1'),'$2','$4','$5').
try_expr -> 'try' exprs try_catch :
build_try(?anno('$1'),'$2',[],'$3').
try_catch -> 'catch' try_clauses 'end' :
{'$2',[]}.
try_catch -> 'catch' try_clauses 'after' exprs 'end' :
{'$2','$4'}.
try_catch -> 'after' exprs 'end' :
{[],'$2'}.
try_clauses -> try_clause : ['$1'].
try_clauses -> try_clause ';' try_clauses : ['$1' | '$3'].
try_clause -> pat_expr clause_guard clause_body :
A = first_anno('$1'),
Az = last_anno('$1'), % Good enough...
{clause,A,[{tuple,A,[{atom,A,throw},'$1',{var,Az,'_'}]}],'$2','$3'}.
try_clause -> atom ':' pat_expr try_opt_stacktrace clause_guard clause_body :
A = ?anno('$1'),
T = case '$4' of '_' -> {var,last_anno('$3'),'_'}; V -> V end,
{clause,A,[{tuple,A,['$1','$3',T]}],'$5','$6'}.
try_clause -> var ':' pat_expr try_opt_stacktrace clause_guard clause_body :
A = ?anno('$1'),
T = case '$4' of '_' -> {var,last_anno('$3'),'_'}; V -> V end,
{clause,A,[{tuple,A,['$1','$3',T]}],'$5','$6'}.
try_opt_stacktrace -> ':' var : '$2'.
try_opt_stacktrace -> '$empty' : '_'.
maybe_expr -> 'maybe' maybe_match_exprs 'end' :
{'maybe',?anno('$1'),'$2'}.
maybe_expr -> 'maybe' maybe_match_exprs 'else' cr_clauses 'end' :
%% `erl_lint` can produce a better warning when the position
%% of the `else` keyword is known.
{'maybe',?anno('$1'),'$2',{'else',?anno('$3'),'$4'}}.
maybe_match_exprs -> maybe_match : ['$1'].
maybe_match_exprs -> maybe_match ',' maybe_match_exprs : ['$1' | '$3'].
maybe_match_exprs -> expr : ['$1'].
maybe_match_exprs -> expr ',' maybe_match_exprs : ['$1' | '$3'].
maybe_match -> expr '?=' expr : {maybe_match,?anno('$2'),'$1','$3'}.
argument_list -> '(' ')' : {[],?anno('$1')}.
argument_list -> '(' exprs ')' : {'$2',?anno('$1')}.
pat_argument_list -> '(' ')' : {[],?anno('$1')}.
pat_argument_list -> '(' pat_exprs ')' : {'$2',?anno('$1')}.
exprs -> expr : ['$1'].
exprs -> expr ',' exprs : ['$1' | '$3'].
clause_body_exprs -> ssa_check_when_clauses exprs : '$1' ++ '$2'.
clause_body_exprs -> exprs : '$1'.
pat_exprs -> pat_expr : ['$1'].
pat_exprs -> pat_expr ',' pat_exprs : ['$1' | '$3'].
guard -> exprs : ['$1'].
guard -> exprs ';' guard : ['$1'|'$3'].
atomic -> char : '$1'.
atomic -> integer : '$1'.
atomic -> float : '$1'.
atomic -> atom : '$1'.
atomic -> strings : '$1'.
strings -> string : '$1'.
strings -> string strings :
{string,?anno('$1'),element(3, '$1') ++ element(3, '$2')}.
prefix_op -> '+' : '$1'.
prefix_op -> '-' : '$1'.
prefix_op -> 'bnot' : '$1'.
prefix_op -> 'not' : '$1'.
mult_op -> '/' : '$1'.
mult_op -> '*' : '$1'.
mult_op -> 'div' : '$1'.
mult_op -> 'rem' : '$1'.
mult_op -> 'band' : '$1'.
mult_op -> 'and' : '$1'.
add_op -> '+' : '$1'.
add_op -> '-' : '$1'.
add_op -> 'bor' : '$1'.
add_op -> 'bxor' : '$1'.
add_op -> 'bsl' : '$1'.
add_op -> 'bsr' : '$1'.
add_op -> 'or' : '$1'.
add_op -> 'xor' : '$1'.
list_op -> '++' : '$1'.
list_op -> '--' : '$1'.
comp_op -> '==' : '$1'.
comp_op -> '/=' : '$1'.
comp_op -> '=<' : '$1'.
comp_op -> '<' : '$1'.
comp_op -> '>=' : '$1'.
comp_op -> '>' : '$1'.
comp_op -> '=:=' : '$1'.
comp_op -> '=/=' : '$1'.
ssa_check_when_clauses -> ssa_check_when_clause : ['$1'].
ssa_check_when_clauses -> ssa_check_when_clause ssa_check_when_clauses :
['$1'|'$2'].
ssa_check_when_clause -> '%ssa%' atom ssa_check_clause_args_ls 'when' atom '->'
ssa_check_exprs '.' :
{ssa_check_when, ?anno('$1'), '$2', '$3', '$5', '$7'}.
ssa_check_when_clause -> '%ssa%' ssa_check_clause_args_ls 'when' atom '->'
ssa_check_exprs '.' :
{ssa_check_when, ?anno('$1'), {atom,?anno('$1'),pass}, '$2', '$4', '$6'}.
ssa_check_exprs -> ssa_check_expr : [add_anno_check('$1', [])].
ssa_check_exprs -> ssa_check_expr ssa_check_anno : [add_anno_check('$1', '$2')].
ssa_check_exprs -> ssa_check_expr ',' ssa_check_exprs :
[add_anno_check('$1', [])|'$3'].
ssa_check_exprs -> ssa_check_expr ssa_check_anno ',' ssa_check_exprs :
[add_anno_check('$1', '$2')|'$4'].
ssa_check_anno -> '{' ssa_check_anno_clauses '}' : '$2'.
ssa_check_anno_clauses -> ssa_check_anno_clause : ['$1'].
ssa_check_anno_clauses -> ssa_check_anno_clause ',' ssa_check_anno_clauses :
['$1'|'$3'].
ssa_check_anno_clause -> atom '=>' ssa_check_pat : {term, '$1', '$3'}.
ssa_check_expr -> var '=' atom ssa_check_args :
{check_expr, ?anno('$1'), [set, '$1', '$3'|'$4']}.
ssa_check_expr -> atom ssa_check_args :
{check_expr, ?anno('$1'), [none, '$1'|'$2']}.
ssa_check_expr -> var '=' atom ':' atom ssa_check_args :
{check_expr, ?anno('$1'), [set, '$1', {'$3', '$5'}|'$6']}.
ssa_check_expr -> atom integer :
{check_expr, ?anno('$1'), build_ssa_check_label('$1', '$2')}.
ssa_check_expr -> atom var :
{check_expr, ?anno('$1'), build_ssa_check_label('$1', '$2')}.
ssa_check_clause_args_ls -> '(' ')' : [].
ssa_check_clause_args_ls -> '(' ssa_check_clause_args ')' : '$2'.
ssa_check_clause_args_ls -> '(' '...' ')' : ['$2'].
ssa_check_clause_args -> var : ['$1'].
ssa_check_clause_args -> var ',' ssa_check_clause_args : ['$1'|'$3'].
ssa_check_clause_args -> var ',' '...' : ['$1', '$3'].
ssa_check_args -> '(' ')' : {[], ?anno('$1')}.
ssa_check_args -> '(' ssa_check_pats ')' : '$2'.
ssa_check_args -> '(' '...' ')' : ['$2'].
ssa_check_pats -> ssa_check_pat : ['$1'].
ssa_check_pats -> ssa_check_pat ',' ssa_check_pats : ['$1'|'$3'].
ssa_check_pats -> ssa_check_pat ',' '...' : ['$1', '$3'].
ssa_check_pat -> var : '$1'.
ssa_check_pat -> atom : '$1'.
ssa_check_pat -> integer : '$1'.
ssa_check_pat -> float : '$1'.
ssa_check_pat -> float '(' float ')': {float_epsilon, '$1', '$3'}.
ssa_check_pat -> ssa_check_fun_ref : '$1'.
ssa_check_pat -> '{' '}' : {tuple, ?anno('$1'), []}.
ssa_check_pat -> '{' ssa_check_pats '}' : {tuple, ?anno('$1'), '$2'}.
ssa_check_pat -> '{' '...' '}' : {tuple, ?anno('$1'), ['$2']}.
ssa_check_pat -> ssa_check_binary_lit : '$1'.
ssa_check_pat -> ssa_check_list_lit : '$1'.
ssa_check_pat -> '#' '{' '}' : {map, ?anno('$1'), []}.
ssa_check_pat -> '#' '{' ssa_check_map_key_elements '}' : {map, ?anno('$1'), '$3'}.
ssa_check_fun_ref -> 'fun' atom '/' integer : {local_fun, '$2', '$4'}.
ssa_check_fun_ref -> 'fun' atom ':' atom '/' integer : {external_fun, '$2', '$4', '$6'}.
ssa_check_binary_lit -> '<<' '>>' : {binary, ?anno('$1'), []}.
ssa_check_binary_lit -> '<<' ssa_check_binary_lit_bytes_ls '>>' :
{binary, ?anno('$1'), '$2'}.
ssa_check_binary_lit -> '<<' ssa_check_binary_lit_rest '>>' :
{binary, ?anno('$1'), ['$2']}.
ssa_check_binary_lit_bytes_ls -> integer : ['$1'].
ssa_check_binary_lit_bytes_ls -> integer ',' ssa_check_binary_lit_bytes_ls :
['$1'|'$3'].
ssa_check_binary_lit_bytes_ls -> integer ',' ssa_check_binary_lit_rest :
['$1', '$3'].
ssa_check_binary_lit_rest -> integer ':' integer : {'$1', '$3'}.
ssa_check_list_lit -> '[' ']' : {list, ?anno('$1'), []}.
ssa_check_list_lit -> '[' ssa_check_list_lit_ls ']' :
{list, ?anno('$1'), '$2'}.
ssa_check_list_lit_ls -> ssa_check_pat : ['$1'].
ssa_check_list_lit_ls -> ssa_check_pat ',' ssa_check_list_lit_ls : ['$1'|'$3'].
ssa_check_list_lit_ls -> ssa_check_pat ',' '...' : ['$1', '$3'].
ssa_check_list_lit_ls -> ssa_check_pat '|' ssa_check_pat : ['$1'|'$3'].
ssa_check_map_key -> atom : '$1'.
ssa_check_map_key -> integer : '$1'.
ssa_check_map_key -> float : '$1'.
ssa_check_map_key -> var : '$1'.
ssa_check_map_key -> '{' ssa_check_map_key_tuple_elements '}' :
{tuple, ?anno('$1'), '$2'}.
ssa_check_map_key -> '{' '}' : {tuple, ?anno('$1'), []}.
ssa_check_map_key -> ssa_check_binary_lit : '$1'.
ssa_check_map_key -> '[' ssa_check_map_key_list ']' :
{list, ?anno('$1'), '$2'}.
ssa_check_map_key -> '[' ']' : {list, ?anno('$1'), []}.
ssa_check_map_key -> '#' '{' '}' : {map, ?anno('$1'), []}.
ssa_check_map_key -> '#' '{' ssa_check_map_key_elements '}' : '$3'.
ssa_check_map_key_list -> ssa_check_map_key : ['$1'].
ssa_check_map_key_list -> ssa_check_map_key ',' ssa_check_map_key_list :
['$1'|'$3'].
ssa_check_map_key_list -> ssa_check_map_key '|' ssa_check_map_key :
['$1'|'$3'].
ssa_check_map_key_elements -> ssa_check_map_key_element : ['$1'].
ssa_check_map_key_elements -> ssa_check_map_key_element ',' ssa_check_map_key_elements :
['$1'|'$3'].
ssa_check_map_key_element -> ssa_check_map_key '=>' ssa_check_map_key:
{'$1', '$3'}.
%% ssa_check_map_key_element -> ssa_check_map_key '::' top_type:
%% {type, '$1', '$3'}.
ssa_check_map_key_tuple_elements -> ssa_check_map_key : ['$1'].
ssa_check_map_key_tuple_elements -> ssa_check_map_key ',' ssa_check_map_key_tuple_elements:
['$1'|'$3'].
Header
"%% This file was automatically generated from the file \"erl_parse.yrl\"."
"%%"
"%% Copyright Ericsson AB 1996-2015. All Rights Reserved."
"%%"
"%% Licensed under the Apache License, Version 2.0 (the \"License\"); you may"
"%% not use this file except in compliance with the License. You may obtain"
"%% a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>"
"%%"
"%% Unless required by applicable law or agreed to in writing, software"
"%% distributed under the License is distributed on an \"AS IS\" BASIS,"
"%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied."
"%% See the License for the specific language governing permissions and"
"%% limitations under the License."
"".
Erlang code.
-moduledoc """
This module is the basic Erlang parser that converts tokens into the abstract
form of either forms (that is, top-level constructs), expressions, or terms.
The Abstract Format is described in the ERTS User's Guide. Notice that a token
list must end with the dot token to be acceptable to the parse functions
(see the `m:erl_scan`) module.
## Error Information
ErrorInfo is the standard ErrorInfo structure that is returned from all I/O modules.
The format is as follows:
```
{ErrorLine, Module, ErrorDescriptor}
```
A string describing the error is obtained with the following call:
```
Module:format_error(ErrorDescriptor)
```
## See Also
`m:erl_anno`, `m:erl_scan`, `m:io`, section [The Abstract Format](`e:erts:absform`)
in the ERTS User's Guide.
""".
-define(YECC_PARSE_DOC, false).
-define(YECC_PARSE_AND_SCAN_DOC, false).
-define(YECC_FORMAT_ERROR_DOC, """
Uses an ErrorDescriptor and returns a string that describes the error.
This function is usually called implicitly when an ErrorInfo structure is
processed (see section [Error Information](#module-error-information)).
""").
-export([parse_form/1,parse_exprs/1,parse_term/1]).
-export([normalise/1,abstract/1,tokens/1,tokens/2]).
-export([abstract/2]).
-export([inop_prec/1,preop_prec/1,func_prec/0,max_prec/0]).
-export([type_inop_prec/1,type_preop_prec/1]).
-export([map_anno/2, fold_anno/3, mapfold_anno/3,
new_anno/1, anno_to_term/1, anno_from_term/1]).
-export([first_anno/1]). % Internal export.
-export_type([abstract_clause/0, abstract_expr/0, abstract_form/0,
abstract_type/0, form_info/0, error_info/0]).
%% The following types are exported because they are used by syntax_tools
-export_type([af_binelement/1, af_generator/0, af_remote_function/0]).
%% The following type is used by PropEr
-export_type([af_field_decl/0]).
%% Removed functions
-removed([{set_line,2,"use erl_anno:set_line/2"},
{get_attributes,1,"erl_anno:{column,line,location,text}/1 instead"},
{get_attribute,2,"erl_anno:{column,line,location,text}/1 instead"}]).
%% Start of Abstract Format
-type anno() :: erl_anno:anno().
-doc "Abstract form of an Erlang form.".
-type abstract_form() :: af_module()
| af_behavior()
| af_behaviour()
| af_export()
| af_import()
| af_export_type()
| af_compile()
| af_file()
| af_record_decl()
| af_type_decl()
| af_function_spec()
| af_wild_attribute()
| af_function_decl().
-type af_module() :: {'attribute', anno(), 'module', module()}.
-type af_behavior() :: {'attribute', anno(), 'behavior', behaviour()}.
-type af_behaviour() :: {'attribute', anno(), 'behaviour', behaviour()}.
-type behaviour() :: atom().
-type af_export() :: {'attribute', anno(), 'export', af_fa_list()}.
-type af_import() :: {'attribute', anno(), 'import', {module(), af_fa_list()}}.
-type af_fa_list() :: [{function_name(), arity()}].
-type af_export_type() :: {'attribute', anno(), 'export_type', af_ta_list()}.
-type af_ta_list() :: [{type_name(), arity()}].
-type af_compile() :: {'attribute', anno(), 'compile', any()}.
-type af_file() :: {'attribute', anno(), 'file', {string(), anno()}}.
-type af_record_decl() ::
{'attribute', anno(), 'record', {record_name(), [af_field_decl()]}}.
-doc "Abstract representation of a record field.".
-type af_field_decl() :: af_typed_field() | af_field().
-type af_typed_field() ::
{'typed_record_field', af_field(), abstract_type()}.
-type af_field() :: {'record_field', anno(), af_field_name()}
| {'record_field', anno(), af_field_name(), abstract_expr()}.
-type af_type_decl() :: {'attribute', anno(), type_attr(),
{type_name(), abstract_type(), [af_variable()]}}.
-type type_attr() :: 'opaque' | 'type'.
-type af_function_spec() :: {'attribute', anno(), spec_attr(),
{{function_name(), arity()},
af_function_type_list()}}
| {'attribute', anno(), 'spec',
{{module(), function_name(), arity()},
af_function_type_list()}}.
-type spec_attr() :: 'callback' | 'spec'.
-type af_wild_attribute() :: {'attribute', anno(), atom(), any()}.
-type af_function_decl() ::
{'function', anno(), function_name(), arity(), af_clause_seq()}.
-doc "Abstract form of an Erlang expression.".
-type abstract_expr() :: af_literal()
| af_match(abstract_expr())
| af_maybe_match()
| af_variable()
| af_tuple(abstract_expr())
| af_nil()
| af_cons(abstract_expr())
| af_bin(abstract_expr())
| af_binary_op(abstract_expr())
| af_unary_op(abstract_expr())
| af_record_creation(abstract_expr())
| af_record_update(abstract_expr())
| af_record_index()
| af_record_field_access(abstract_expr())
| af_map_creation(abstract_expr())
| af_map_update(abstract_expr())
| af_catch()
| af_local_call()
| af_remote_call()
| af_list_comprehension()
| af_map_comprehension()
| af_binary_comprehension()
| af_block()
| af_if()
| af_case()
| af_try()
| af_receive()
| af_local_fun()
| af_remote_fun()
| af_fun()
| af_named_fun()
| af_maybe()
| af_maybe_else().
-type af_record_update(T) :: {'record',
anno(),
abstract_expr(),
record_name(),
[af_record_field(T)]}.
-type af_catch() :: {'catch', anno(), abstract_expr()}.
-type af_local_call() :: {'call', anno(), af_local_function(), af_args()}.
-type af_remote_call() :: {'call', anno(), af_remote_function(), af_args()}.
-type af_args() :: [abstract_expr()].
-type af_local_function() :: abstract_expr().
-doc "Abstract representation of a remote function call.".
-type af_remote_function() ::
{'remote', anno(), abstract_expr(), abstract_expr()}.
-type af_list_comprehension() ::
{'lc', anno(), af_template(), af_qualifier_seq()}.
-type af_map_comprehension() ::
{'mc', anno(), af_assoc(abstract_expr()), af_qualifier_seq()}.
-type af_binary_comprehension() ::
{'bc', anno(), af_template(), af_qualifier_seq()}.
-type af_template() :: abstract_expr().
-type af_qualifier_seq() :: [af_qualifier(), ...].
-type af_qualifier() :: af_generator() | af_filter().
-doc "Abstract representation of a generator or a bitstring generator.".
-type af_generator() :: {'generate', anno(), af_pattern(), abstract_expr()}
| {'m_generate', anno(), af_assoc_exact(af_pattern()), abstract_expr()}
| {'b_generate', anno(), af_pattern(), abstract_expr()}.
-type af_filter() :: abstract_expr().
-type af_block() :: {'block', anno(), af_body()}.
-type af_if() :: {'if', anno(), af_clause_seq()}.
-type af_case() :: {'case', anno(), abstract_expr(), af_clause_seq()}.
-type af_try() :: {'try',
anno(),
af_body(),
af_clause_seq() | [],
af_clause_seq() | [],
af_body() | []}.
-type af_clause_seq() :: [af_clause(), ...].
-type af_receive() ::
{'receive', anno(), af_clause_seq()}
| {'receive', anno(), af_clause_seq(), abstract_expr(), af_body()}.
-type af_local_fun() ::
{'fun', anno(), {'function', function_name(), arity()}}.
-type af_remote_fun() ::
{'fun', anno(), {'function', module(), function_name(), arity()}}
| {'fun', anno(), {'function',
af_atom() | af_variable(),
af_atom() | af_variable(),
af_integer() | af_variable()}}.
-type af_fun() :: {'fun', anno(), {'clauses', af_clause_seq()}}.
-type af_named_fun() :: {'named_fun', anno(), fun_name(), af_clause_seq()}.
-type fun_name() :: atom().
-doc "Abstract form of an Erlang clause.".
-type abstract_clause() :: af_clause().
-type af_clause() ::
{'clause', anno(), [af_pattern()], af_guard_seq(), af_body()}.
-type af_body() :: [abstract_expr(), ...].
-type af_guard_seq() :: [af_guard()].
-type af_guard() :: [af_guard_test(), ...].
-type af_guard_test() :: af_literal()
| af_variable()
| af_tuple(af_guard_test())
| af_nil()
| af_cons(af_guard_test())
| af_bin(af_guard_test())
| af_binary_op(af_guard_test())
| af_unary_op(af_guard_test())
| af_record_creation(af_guard_test())
| af_record_index()
| af_record_field_access(af_guard_test())