-
Notifications
You must be signed in to change notification settings - Fork 3k
/
erl_syntax.erl
8398 lines (7027 loc) · 232 KB
/
erl_syntax.erl
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
%% =====================================================================
%% 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.
%%
%% Alternatively, you may use this file under the terms of the GNU Lesser
%% General Public License (the "LGPL") as published by the Free Software
%% Foundation; either version 2.1, or (at your option) any later version.
%% If you wish to allow use of your version of this file only under the
%% terms of the LGPL, you should delete the provisions above and replace
%% them with the notice and other provisions required by the LGPL; see
%% <http://www.gnu.org/licenses/>. If you do not delete the provisions
%% above, a recipient may use your version of this file under the terms of
%% either the Apache License or the LGPL.
%%
%% @copyright 1997-2006 Richard Carlsson
%% @author Richard Carlsson <[email protected]>
%% @end
%% =====================================================================
%% @doc Abstract Erlang syntax trees.
%%
%% This module defines an abstract data type for representing Erlang
%% source code as syntax trees, in a way that is backwards compatible
%% with the data structures created by the Erlang standard library
%% parser module `erl_parse' (often referred to as "parse
%% trees", which is a bit of a misnomer). This means that all
%% `erl_parse' trees are valid abstract syntax trees, but the
%% reverse is not true: abstract syntax trees can in general not be used
%% as input to functions expecting an `erl_parse' tree.
%% However, as long as an abstract syntax tree represents a correct
%% Erlang program, the function {@link revert/1} should be able to
%% transform it to the corresponding `erl_parse'
%% representation.
%%
%% A recommended starting point for the first-time user is the documentation
%% of the {@link syntaxTree()} data type, and the function {@link type/1}.
%%
%% == NOTES: ==
%%
%% This module deals with the composition and decomposition of
%% <em>syntactic</em> entities (as opposed to semantic ones); its
%% purpose is to hide all direct references to the data structures used
%% to represent these entities. With few exceptions, the functions in
%% this module perform no semantic interpretation of their inputs, and
%% in general, the user is assumed to pass type-correct arguments - if
%% this is not done, the effects are not defined.
%%
%% With the exception of the {@link erl_parse()} data structures,
%% the internal representations of abstract syntax trees are subject to
%% change without notice, and should not be documented outside this
%% module. Furthermore, we do not give any guarantees on how an abstract
%% syntax tree may or may not be represented, <em>with the following
%% exceptions</em>: no syntax tree is represented by a single atom, such
%% as `none', by a list constructor `[X | Y]', or
%% by the empty list `[]'. This can be relied on when writing
%% functions that operate on syntax trees.
%% @type syntaxTree(). An abstract syntax tree. The {@link erl_parse()}
%% "parse tree" representation is a proper subset of the `syntaxTree()'
%% representation.
%%
%% Every abstract syntax tree node has a <em>type</em>, given by the
%% function {@link type/1}. Each node also has associated
%% <em>attributes</em>; see {@link get_attrs/1} for details. The functions
%% {@link make_tree/2} and {@link subtrees/1} are generic
%% constructor/decomposition functions for abstract syntax trees. The
%% functions {@link abstract/1} and {@link concrete/1} convert between
%% constant Erlang terms and their syntactic representations. The set of
%% syntax tree nodes is extensible through the {@link tree/2} function.
%%
%% A syntax tree can be transformed to the {@link erl_parse()}
%% representation with the {@link revert/1} function.
-module(erl_syntax).
-export([type/1,
is_leaf/1,
is_form/1,
is_literal/1,
abstract/1,
concrete/1,
revert/1,
revert_forms/1,
subtrees/1,
make_tree/2,
update_tree/2,
meta/1,
get_pos/1,
set_pos/2,
copy_pos/2,
get_precomments/1,
set_precomments/2,
add_precomments/2,
get_postcomments/1,
set_postcomments/2,
add_postcomments/2,
has_comments/1,
remove_comments/1,
copy_comments/2,
join_comments/2,
get_ann/1,
set_ann/2,
add_ann/2,
copy_ann/2,
get_attrs/1,
set_attrs/2,
copy_attrs/2,
flatten_form_list/1,
cons/2,
list_head/1,
list_tail/1,
is_list_skeleton/1,
is_proper_list/1,
list_elements/1,
list_length/1,
normalize_list/1,
compact_list/1,
annotated_type/2,
annotated_type_name/1,
annotated_type_body/1,
application/2,
application/3,
application_arguments/1,
application_operator/1,
arity_qualifier/2,
arity_qualifier_argument/1,
arity_qualifier_body/1,
atom/1,
is_atom/2,
atom_value/1,
atom_literal/1,
atom_literal/2,
atom_name/1,
attribute/1,
attribute/2,
attribute_arguments/1,
attribute_name/1,
binary/1,
binary_comp/2,
binary_comp_template/1,
binary_comp_body/1,
binary_field/1,
binary_field/2,
binary_field/3,
binary_field_body/1,
binary_field_types/1,
binary_field_size/1,
binary_fields/1,
binary_generator/2,
binary_generator_body/1,
binary_generator_pattern/1,
bitstring_type/2,
bitstring_type_m/1,
bitstring_type_n/1,
block_expr/1,
block_expr_body/1,
case_expr/2,
case_expr_argument/1,
case_expr_clauses/1,
catch_expr/1,
catch_expr_body/1,
char/1,
is_char/2,
char_value/1,
char_literal/1,
char_literal/2,
clause/2,
clause/3,
clause_body/1,
clause_guard/1,
clause_patterns/1,
comment/1,
comment/2,
comment_padding/1,
comment_text/1,
conjunction/1,
conjunction_body/1,
constrained_function_type/2,
constrained_function_type_body/1,
constrained_function_type_argument/1,
constraint/2,
constraint_argument/1,
constraint_body/1,
disjunction/1,
disjunction_body/1,
eof_marker/0,
error_marker/1,
error_marker_info/1,
float/1,
float_value/1,
float_literal/1,
form_list/1,
form_list_elements/1,
fun_expr/1,
fun_expr_arity/1,
fun_expr_clauses/1,
fun_type/0,
function/2,
function_arity/1,
function_clauses/1,
function_name/1,
function_type/1,
function_type/2,
function_type_arguments/1,
function_type_return/1,
generator/2,
generator_body/1,
generator_pattern/1,
if_expr/1,
if_expr_clauses/1,
implicit_fun/1,
implicit_fun/2,
implicit_fun/3,
implicit_fun_name/1,
infix_expr/3,
infix_expr_left/1,
infix_expr_operator/1,
infix_expr_right/1,
integer/1,
is_integer/2,
integer_value/1,
integer_literal/1,
integer_range_type/2,
integer_range_type_low/1,
integer_range_type_high/1,
list/1,
list/2,
list_comp/2,
list_comp_body/1,
list_comp_template/1,
list_prefix/1,
list_suffix/1,
macro/1,
macro/2,
macro_arguments/1,
macro_name/1,
map_expr/1,
map_expr/2,
map_expr_argument/1,
map_expr_fields/1,
map_field_assoc/2,
map_field_assoc_name/1,
map_field_assoc_value/1,
map_field_exact/2,
map_field_exact_name/1,
map_field_exact_value/1,
map_type/0,
map_type/1,
map_type_fields/1,
map_type_assoc/2,
map_type_assoc_name/1,
map_type_assoc_value/1,
map_type_exact/2,
map_type_exact_name/1,
map_type_exact_value/1,
match_expr/2,
match_expr_body/1,
match_expr_pattern/1,
module_qualifier/2,
module_qualifier_argument/1,
module_qualifier_body/1,
named_fun_expr/2,
named_fun_expr_arity/1,
named_fun_expr_clauses/1,
named_fun_expr_name/1,
nil/0,
operator/1,
operator_literal/1,
operator_name/1,
parentheses/1,
parentheses_body/1,
prefix_expr/2,
prefix_expr_argument/1,
prefix_expr_operator/1,
receive_expr/1,
receive_expr/3,
receive_expr_action/1,
receive_expr_clauses/1,
receive_expr_timeout/1,
record_access/3,
record_access_argument/1,
record_access_field/1,
record_access_type/1,
record_expr/2,
record_expr/3,
record_expr_argument/1,
record_expr_fields/1,
record_expr_type/1,
record_field/1,
record_field/2,
record_field_name/1,
record_field_value/1,
record_index_expr/2,
record_index_expr_field/1,
record_index_expr_type/1,
record_type/2,
record_type_name/1,
record_type_fields/1,
record_type_field/2,
record_type_field_name/1,
record_type_field_type/1,
size_qualifier/2,
size_qualifier_argument/1,
size_qualifier_body/1,
string/1,
is_string/2,
string_value/1,
string_literal/1,
string_literal/2,
text/1,
text_string/1,
try_expr/2,
try_expr/3,
try_expr/4,
try_after_expr/2,
try_expr_body/1,
try_expr_clauses/1,
try_expr_handlers/1,
try_expr_after/1,
tuple_type/0,
tuple_type/1,
tuple_type_elements/1,
type_application/2,
type_application/3,
type_application_name/1,
type_application_arguments/1,
type_union/1,
type_union_types/1,
typed_record_field/2,
typed_record_field_body/1,
typed_record_field_type/1,
class_qualifier/2,
class_qualifier/3,
class_qualifier_argument/1,
class_qualifier_body/1,
class_qualifier_stacktrace/1,
tuple/1,
tuple_elements/1,
tuple_size/1,
underscore/0,
user_type_application/2,
user_type_application_name/1,
user_type_application_arguments/1,
variable/1,
variable_name/1,
variable_literal/1,
warning_marker/1,
warning_marker_info/1,
tree/1,
tree/2,
data/1,
is_tree/1]).
-export_type([forms/0, syntaxTree/0, syntaxTreeAttributes/0, padding/0]).
%% =====================================================================
%% IMPLEMENTATION NOTES:
%%
%% All nodes are represented by tuples of arity 2 or greater, whose
%% first element is an atom which uniquely identifies the type of the
%% node. (In the backwards-compatible representation, the interpretation
%% is also often dependent on the context; the second element generally
%% holds the position information - with a couple of exceptions; see
%% `get_pos' and `set_pos' for details). In the documentation of this
%% module, `Pos' is the source code position information associated with
%% a node; usually, this is a positive integer indicating the original
%% source code line, but no assumptions are made in this module
%% regarding the format or interpretation of position information. When
%% a syntax tree node is constructed, its associated position is by
%% default set to the integer zero.
%% =====================================================================
-define(NO_UNUSED, true).
%% =====================================================================
%% Declarations of globally used internal data structures
%% =====================================================================
%% `com' records are used to hold comment information attached to a
%% syntax tree node or a wrapper structure.
%%
%% #com{pre :: Pre, post :: Post}
%%
%% Pre = Post = [Com]
%% Com = syntaxTree()
%%
%% type(Com) = comment
-record(com, {pre = [] :: [syntaxTree()],
post = [] :: [syntaxTree()]}).
%% `attr' records store node attributes as an aggregate.
%%
%% #attr{pos :: Pos, ann :: Ann, com :: Comments}
%%
%% Pos = term()
%% Ann = [term()]
%% Comments = none | #com{}
%%
%% where `Pos' `Ann' and `Comments' are the corresponding values of a
%% `tree' or `wrapper' record.
-record(attr, {pos = erl_anno:new(0) :: term(),
ann = [] :: [term()],
com = none :: 'none' | #com{}}).
-type syntaxTreeAttributes() :: #attr{}.
%% `tree' records represent new-form syntax tree nodes.
%%
%% Tree = #tree{type :: Type, attr :: Attr, data :: Data}
%%
%% Type = atom()
%% Attr = #attr{}
%% Data = term()
%%
%% is_tree(Tree) = true
-record(tree, {type :: atom(),
attr = #attr{} :: #attr{},
data :: term()}).
-type tree() :: #tree{}.
%% `wrapper' records are used for attaching new-form node information to
%% `erl_parse' trees.
%%
%% Wrapper = #wrapper{type :: Type, attr :: Attr, tree :: ParseTree}
%%
%% Type = atom()
%% Attr = #attr{}
%% ParseTree = term()
%%
%% is_tree(Wrapper) = false
-record(wrapper, {type :: atom(),
attr = #attr{} :: #attr{},
tree :: erl_parse()}).
-type wrapper() :: #wrapper{}.
%% =====================================================================
-type syntaxTree() :: tree() | wrapper() | erl_parse().
-type erl_parse() :: erl_parse:abstract_clause()
| erl_parse:abstract_expr()
| erl_parse:abstract_form()
| erl_parse:abstract_type()
| erl_parse:form_info()
| erl_parse:af_binelement(term())
| erl_parse:af_generator()
| erl_parse:af_remote_function().
%% The representation built by the Erlang standard library parser
%% `erl_parse'. This is a subset of the {@link syntaxTree()} type.
%% =====================================================================
%%
%% Exported functions
%%
%% =====================================================================
%% =====================================================================
%% @doc Returns the type tag of `Node'. If `Node'
%% does not represent a syntax tree, evaluation fails with reason
%% `badarg'. Node types currently defined by this module are:
%%
%% <center><table border="1">
%% <tr>
%% <td>application</td>
%% <td>annotated_type</td>
%% <td>arity_qualifier</td>
%% <td>atom</td>
%% </tr><tr>
%% <td>attribute</td>
%% <td>binary</td>
%% <td>binary_field</td>
%% <td>bitstring_type</td>
%% </tr><tr>
%% <td>block_expr</td>
%% <td>case_expr</td>
%% <td>catch_expr</td>
%% <td>char</td>
%% </tr><tr>
%% <td>class_qualifier</td>
%% <td>clause</td>
%% <td>comment</td>
%% <td>conjunction</td>
%% </tr><tr>
%% <td>constrained_function_type</td>
%% <td>constraint</td>
%% <td>disjunction</td>
%% <td>eof_marker</td>
%% </tr><tr>
%% <td>error_marker</td>
%% <td>float</td>
%% <td>form_list</td>
%% <td>fun_expr</td>
%% </tr><tr>
%% <td>fun_type</td>
%% <td>function</td>
%% <td>function_type</td>
%% <td>generator</td>
%% </tr><tr>
%% <td>if_expr</td>
%% <td>implicit_fun</td>
%% <td>infix_expr</td>
%% <td>integer</td>
%% </tr><tr>
%% <td>integer_range_type</td>
%% <td>list</td>
%% <td>list_comp</td>
%% <td>macro</td>
%% </tr><tr>
%% <td>map_expr</td>
%% <td>map_field_assoc</td>
%% <td>map_field_exact</td>
%% <td>map_type</td>
%% </tr><tr>
%% <td>map_type_assoc</td>
%% <td>map_type_exact</td>
%% <td>match_expr</td>
%% <td>module_qualifier</td>
%% </tr><tr>
%% <td>named_fun_expr</td>
%% <td>nil</td>
%% <td>operator</td>
%% <td>parentheses</td>
%% </tr><tr>
%% <td>prefix_expr</td>
%% <td>receive_expr</td>
%% <td>record_access</td>
%% <td>record_expr</td>
%% </tr><tr>
%% <td>record_field</td>
%% <td>record_index_expr</td>
%% <td>record_type</td>
%% <td>record_type_field</td>
%% </tr><tr>
%% <td>size_qualifier</td>
%% <td>string</td>
%% <td>text</td>
%% <td>try_expr</td>
%% </tr><tr>
%% <td>tuple</td>
%% <td>tuple_type</td>
%% <td>typed_record_field</td>
%% <td>type_application</td>
%% </tr><tr>
%% <td>type_union</td>
%% <td>underscore</td>
%% <td>user_type_application</td>
%% <td>variable</td>
%% </tr><tr>
%% <td>warning_marker</td>
%% </tr>
%% </table></center>
%%
%% The user may (for special purposes) create additional nodes
%% with other type tags, using the {@link tree/2} function.
%%
%% Note: The primary constructor functions for a node type should
%% always have the same name as the node type itself.
%%
%% @see tree/2
%% @see annotated_type/2
%% @see application/3
%% @see arity_qualifier/2
%% @see atom/1
%% @see attribute/2
%% @see binary/1
%% @see binary_field/2
%% @see bitstring_type/2
%% @see block_expr/1
%% @see case_expr/2
%% @see catch_expr/1
%% @see char/1
%% @see class_qualifier/2
%% @see clause/3
%% @see comment/2
%% @see conjunction/1
%% @see constrained_function_type/2
%% @see constraint/2
%% @see disjunction/1
%% @see eof_marker/0
%% @see error_marker/1
%% @see float/1
%% @see form_list/1
%% @see fun_expr/1
%% @see fun_type/0
%% @see function/2
%% @see function_type/1
%% @see function_type/2
%% @see generator/2
%% @see if_expr/1
%% @see implicit_fun/2
%% @see infix_expr/3
%% @see integer/1
%% @see integer_range_type/2
%% @see list/2
%% @see list_comp/2
%% @see macro/2
%% @see map_expr/2
%% @see map_field_assoc/2
%% @see map_field_exact/2
%% @see map_type/0
%% @see map_type/1
%% @see map_type_assoc/2
%% @see map_type_exact/2
%% @see match_expr/2
%% @see module_qualifier/2
%% @see named_fun_expr/2
%% @see nil/0
%% @see operator/1
%% @see parentheses/1
%% @see prefix_expr/2
%% @see receive_expr/3
%% @see record_access/3
%% @see record_expr/2
%% @see record_field/2
%% @see record_index_expr/2
%% @see record_type/2
%% @see record_type_field/2
%% @see size_qualifier/2
%% @see string/1
%% @see text/1
%% @see try_expr/3
%% @see tuple/1
%% @see tuple_type/0
%% @see tuple_type/1
%% @see typed_record_field/2
%% @see type_application/2
%% @see type_union/1
%% @see underscore/0
%% @see user_type_application/2
%% @see variable/1
%% @see warning_marker/1
-spec type(syntaxTree()) -> atom().
type(#tree{type = T}) ->
T;
type(#wrapper{type = T}) ->
T;
type(Node) ->
%% Check for `erl_parse'-compatible nodes, and otherwise fail.
case Node of
%% Leaf types
{atom, _, _} -> atom;
{char, _, _} -> char;
{float, _, _} -> float;
{integer, _, _} -> integer;
{nil, _} -> nil;
{string, _, _} -> string;
{var, _, Name} ->
if Name =:= '_' -> underscore;
true -> variable
end;
{error, _} -> error_marker;
{warning, _} -> warning_marker;
{eof, _} -> eof_marker;
%% Composite types
{'case', _, _, _} -> case_expr;
{'catch', _, _} -> catch_expr;
{'fun', _, {clauses, _}} -> fun_expr;
{named_fun, _, _, _} -> named_fun_expr;
{'fun', _, {function, _, _}} -> implicit_fun;
{'fun', _, {function, _, _, _}} -> implicit_fun;
{'if', _, _} -> if_expr;
{'receive', _, _, _, _} -> receive_expr;
{'receive', _, _} -> receive_expr;
{attribute, _, _, _} -> attribute;
{bin, _, _} -> binary;
{bin_element, _, _, _, _} -> binary_field;
{block, _, _} -> block_expr;
{call, _, _, _} -> application;
{clause, _, _, _, _} -> clause;
{cons, _, _, _} -> list;
{function, _, _, _, _} -> function;
{b_generate, _, _, _} -> binary_generator;
{generate, _, _, _} -> generator;
{lc, _, _, _} -> list_comp;
{bc, _, _, _} -> binary_comp;
{match, _, _, _} -> match_expr;
{map, _, _, _} -> map_expr;
{map, _, _} -> map_expr;
{map_field_assoc, _, _, _} -> map_field_assoc;
{map_field_exact, _, _, _} -> map_field_exact;
{op, _, _, _, _} -> infix_expr;
{op, _, _, _} -> prefix_expr;
{record, _, _, _, _} -> record_expr;
{record, _, _, _} -> record_expr;
{record_field, _, _, _, _} -> record_access;
{record_index, _, _, _} -> record_index_expr;
{remote, _, _, _} -> module_qualifier;
{'try', _, _, _, _, _} -> try_expr;
{tuple, _, _} -> tuple;
%% Type types
{ann_type, _, _} -> annotated_type;
{remote_type, _, _} -> type_application;
{type, _, binary, [_, _]} -> bitstring_type;
{type, _, bounded_fun, [_, _]} -> constrained_function_type;
{type, _, constraint, [_, _]} -> constraint;
{type, _, 'fun', []} -> fun_type;
{type, _, 'fun', [_, _]} -> function_type;
{type, _, map, _} -> map_type;
{type, _, map_field_assoc, _} -> map_type_assoc;
{type, _, map_field_exact, _} -> map_type_exact;
{type, _, record, _} -> record_type;
{type, _, field_type, _} -> record_type_field;
{type, _, range, _} -> integer_range_type;
{type, _, tuple, _} -> tuple_type;
{type, _, union, _} -> type_union;
{type, _, _, _} -> type_application;
{user_type, _, _, _} -> user_type_application;
_ ->
erlang:error({badarg, Node})
end.
%% =====================================================================
%% @doc Returns `true' if `Node' is a leaf node,
%% otherwise `false'. The currently recognised leaf node
%% types are:
%%
%% <center><table border="1">
%% <tr>
%% <td>`atom'</td>
%% <td>`char'</td>
%% <td>`comment'</td>
%% <td>`eof_marker'</td>
%% <td>`error_marker'</td>
%% </tr><tr>
%% <td>`float'</td>
%% <td>`fun_type'</td>
%% <td>`integer'</td>
%% <td>`nil'</td>
%% <td>`operator'</td>
%% <td>`string'</td>
%% </tr><tr>
%% <td>`text'</td>
%% <td>`underscore'</td>
%% <td>`variable'</td>
%% <td>`warning_marker'</td>
%% </tr>
%% </table></center>
%%
%% A node of type `map_expr' is a leaf node if and only if it has no
%% argument and no fields.
%% A node of type `map_type' is a leaf node if and only if it has no
%% fields (`any_size').
%% A node of type `tuple' is a leaf node if and only if its arity is zero.
%% A node of type `tuple_type' is a leaf node if and only if it has no
%% elements (`any_size').
%%
%% Note: not all literals are leaf nodes, and vice versa. E.g.,
%% tuples with nonzero arity and nonempty lists may be literals, but are
%% not leaf nodes. Variables, on the other hand, are leaf nodes but not
%% literals.
%%
%% @see type/1
%% @see is_literal/1
-spec is_leaf(syntaxTree()) -> boolean().
is_leaf(Node) ->
case type(Node) of
atom -> true;
char -> true;
comment -> true; % nonstandard type
eof_marker -> true;
error_marker -> true;
float -> true;
fun_type -> true;
integer -> true;
nil -> true;
operator -> true; % nonstandard type
string -> true;
text -> true; % nonstandard type
map_expr ->
map_expr_fields(Node) =:= [] andalso
map_expr_argument(Node) =:= none;
map_type -> map_type_fields(Node) =:= any_size;
tuple -> tuple_elements(Node) =:= [];
tuple_type -> tuple_type_elements(Node) =:= any_size;
underscore -> true;
variable -> true;
warning_marker -> true;
_ -> false
end.
%% =====================================================================
%% @doc Returns `true' if `Node' is a syntax tree
%% representing a so-called "source code form", otherwise
%% `false'. Forms are the Erlang source code units which,
%% placed in sequence, constitute an Erlang program. Current form types
%% are:
%%
%% <center><table border="1">
%% <tr>
%% <td>`attribute'</td>
%% <td>`comment'</td>
%% <td>`error_marker'</td>
%% <td>`eof_marker'</td>
%% </tr><tr>
%% <td>`form_list'</td>
%% <td>`function'</td>
%% <td>`warning_marker'</td>
%% <td>`text'</td>
%% </tr>
%% </table></center>
%%
%% @see type/1
%% @see attribute/2
%% @see comment/2
%% @see eof_marker/0
%% @see error_marker/1
%% @see form_list/1
%% @see function/2
%% @see warning_marker/1
-spec is_form(syntaxTree()) -> boolean().
is_form(Node) ->
case type(Node) of
attribute -> true;
comment -> true;
function -> true;
eof_marker -> true;
error_marker -> true;
form_list -> true;
warning_marker -> true;
text -> true;
_ -> false
end.
%% =====================================================================
%% @doc Returns the position information associated with
%% `Node'. This is usually a nonnegative integer (indicating
%% the source code line number), but may be any term. By default, all
%% new tree nodes have their associated position information set to the
%% integer zero.
%%
%% @see set_pos/2
%% @see get_attrs/1
%% All `erl_parse' tree nodes are represented by tuples whose second
%% field is the position information (usually an integer), *with the
%% exceptions of* `{error, ...}' (type `error_marker') and `{warning,
%% ...}' (type `warning_marker'), which only contain the associated line
%% number *of the error descriptor*; this is all handled transparently
%% by `get_pos' and `set_pos'.
-spec get_pos(syntaxTree()) -> term().
get_pos(#tree{attr = Attr}) ->
Attr#attr.pos;
get_pos(#wrapper{attr = Attr}) ->
Attr#attr.pos;
get_pos({error, {Pos, _, _}}) ->
Pos;
get_pos({warning, {Pos, _, _}}) ->
Pos;
get_pos(Node) ->
%% Here, we assume that we have an `erl_parse' node with position
%% information in element 2.
element(2, Node).
%% =====================================================================
%% @doc Sets the position information of `Node' to `Pos'.
%%
%% @see get_pos/1
%% @see copy_pos/2
-spec set_pos(syntaxTree(), term()) -> syntaxTree().
set_pos(Node, Pos) ->
case Node of
#tree{attr = Attr} ->
Node#tree{attr = Attr#attr{pos = Pos}};
#wrapper{attr = Attr} ->
Node#wrapper{attr = Attr#attr{pos = Pos}};
_ ->
%% We then assume we have an `erl_parse' node, and create a
%% wrapper around it to make things more uniform.
set_pos(wrap(Node), Pos)
end.
%% =====================================================================
%% @doc Copies the position information from `Source' to `Target'.
%%
%% This is equivalent to `set_pos(Target,
%% get_pos(Source))', but potentially more efficient.
%%
%% @see get_pos/1
%% @see set_pos/2
-spec copy_pos(syntaxTree(), syntaxTree()) -> syntaxTree().
copy_pos(Source, Target) ->
set_pos(Target, get_pos(Source)).
%% =====================================================================
%% `get_com' and `set_com' are for internal use only.
get_com(#tree{attr = Attr}) -> Attr#attr.com;
get_com(#wrapper{attr = Attr}) -> Attr#attr.com;
get_com(_) -> none.
set_com(Node, Com) ->
case Node of
#tree{attr = Attr} ->
Node#tree{attr = Attr#attr{com = Com}};
#wrapper{attr = Attr} ->
Node#wrapper{attr = Attr#attr{com = Com}};
_ ->
set_com(wrap(Node), Com)
end.
%% =====================================================================
%% @doc Returns the associated pre-comments of a node. This is a
%% possibly empty list of abstract comments, in top-down textual order.
%% When the code is formatted, pre-comments are typically displayed
%% directly above the node. For example:
%% ```% Pre-comment of function
%% foo(X) -> {bar, X}.'''
%%
%% If possible, the comment should be moved before any preceding
%% separator characters on the same line. E.g.:
%% ```foo([X | Xs]) ->
%% % Pre-comment of 'bar(X)' node
%% [bar(X) | foo(Xs)];
%% ...'''
%% (where the comment is moved before the "`['").
%%
%% @see comment/2
%% @see set_precomments/2
%% @see get_postcomments/1
%% @see get_attrs/1
-spec get_precomments(syntaxTree()) -> [syntaxTree()].
get_precomments(#tree{attr = Attr}) -> get_precomments_1(Attr);
get_precomments(#wrapper{attr = Attr}) -> get_precomments_1(Attr);
get_precomments(_) -> [].
get_precomments_1(#attr{com = none}) -> [];
get_precomments_1(#attr{com = #com{pre = Cs}}) -> Cs.
%% =====================================================================
%% @doc Sets the pre-comments of `Node' to
%% `Comments'. `Comments' should be a possibly
%% empty list of abstract comments, in top-down textual order.
%%
%% @see comment/2
%% @see get_precomments/1
%% @see add_precomments/2
%% @see set_postcomments/2
%% @see copy_comments/2
%% @see remove_comments/1
%% @see join_comments/2
-spec set_precomments(syntaxTree(), [syntaxTree()]) -> syntaxTree().
set_precomments(Node, Cs) ->
case Node of
#tree{attr = Attr} ->
Node#tree{attr = set_precomments_1(Attr, Cs)};
#wrapper{attr = Attr} ->
Node#wrapper{attr = set_precomments_1(Attr, Cs)};
_ ->
set_precomments(wrap(Node), Cs)
end.
set_precomments_1(#attr{com = none} = Attr, Cs) ->
Attr#attr{com = #com{pre = Cs}};
set_precomments_1(#attr{com = Com} = Attr, Cs) ->
Attr#attr{com = Com#com{pre = Cs}}.
%% =====================================================================