46
46
new_variable_names/2, new_variable_names/3, strip_comments/1,
47
47
to_comment/1, to_comment/2, to_comment/3, variables/1]).
49
-export_type([info_pair/0]).
50
51
%% =====================================================================
51
52
%% @spec map(Function, Tree::syntaxTree()) -> syntaxTree()
105
112
%% @see fold_subtrees/3
106
113
%% @see foldl_listlist/3
115
-spec fold(fun((erl_syntax:syntaxTree(), term()) -> term()),
116
term(), erl_syntax:syntaxTree()) -> term().
108
118
fold(F, S, Tree) ->
109
119
case erl_syntax:subtrees(Tree) of
150
-spec fold_subtrees(fun((erl_syntax:syntaxTree(), term()) -> term()),
151
term(), erl_syntax:syntaxTree()) -> term().
140
153
fold_subtrees(F, S, Tree) ->
141
154
foldl_listlist(F, S, erl_syntax:subtrees(Tree)).
152
165
%% @see //stdlib/lists:foldl/3
167
-spec foldl_listlist(fun((term(), term()) -> term()),
168
term(), [[term()]]) -> term().
154
170
foldl_listlist(F, S, [L | Ls]) ->
155
171
foldl_listlist(F, foldl(F, S, L), Ls);
156
172
foldl_listlist(_, S, []) ->
197
-spec mapfold(fun((erl_syntax:syntaxTree(), term()) -> {erl_syntax:syntaxTree(), term()}),
198
term(), erl_syntax:syntaxTree()) -> {erl_syntax:syntaxTree(), term()}.
181
200
mapfold(F, S, Tree) ->
182
201
case erl_syntax:subtrees(Tree) of
217
236
%% @see mapfold/3
238
-spec mapfold_subtrees(fun((erl_syntax:syntaxTree(), term()) ->
239
{erl_syntax:syntaxTree(), term()}),
240
term(), erl_syntax:syntaxTree()) ->
241
{erl_syntax:syntaxTree(), term()}.
219
243
mapfold_subtrees(F, S, Tree) ->
220
244
case erl_syntax:subtrees(Tree) of
237
261
%% The list of lists in the result has the same structure as the given
238
262
%% list of lists.
264
-spec mapfoldl_listlist(fun((term(), term()) -> {term(), term()}),
265
term(), [[term()]]) -> {[[term()]], term()}.
240
267
mapfoldl_listlist(F, S, [L | Ls]) ->
241
268
{L1, S1} = mapfoldl(F, S, L),
242
269
{Ls1, S2} = mapfoldl_listlist(F, S1, Ls),
340
371
%% @see //stdlib/sets
341
372
%% @see //stdlib/random
374
-spec new_variable_name(fun((integer()) -> atom()), set()) -> atom().
343
376
new_variable_name(F, S) ->
344
377
R = start_range(S),
345
378
new_variable_name(R, F, S).
365
398
%% implementation of `sets'.
367
400
start_range(S) ->
368
max(sets:size(S) * ?START_RANGE_FACTOR, ?MINIMUM_RANGE).
370
max(X, Y) when X > Y -> X;
401
erlang:max(sets:size(S) * ?START_RANGE_FACTOR, ?MINIMUM_RANGE).
373
403
%% The previous number might or might not be used to compute the
374
404
%% next number to be tried. It is currently not used.
389
419
%% @see new_variable_name/1
421
-spec new_variable_names(integer(), set()) -> [atom()].
391
423
new_variable_names(N, S) ->
392
424
new_variable_names(N, fun default_variable_name/1, S).
403
435
%% @see new_variable_name/2
437
-spec new_variable_names(integer(), fun((integer()) -> atom()), set()) ->
405
440
new_variable_names(N, F, S) when is_integer(N) ->
406
441
R = start_range(S),
407
442
new_variable_names(N, [], R, F, S).
441
476
%% @see annotate_bindings/1
442
477
%% @see //stdlib/ordsets
479
-spec annotate_bindings(erl_syntax:syntaxTree(), ordsets:ordset(atom())) ->
480
erl_syntax:syntaxTree().
444
482
annotate_bindings(Tree, Env) ->
445
483
{Tree1, _, _} = vann(Tree, Env),
458
496
%% @see annotate_bindings/2
498
-spec annotate_bindings(erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree().
460
500
annotate_bindings(Tree) ->
461
501
As = erl_syntax:get_ann(Tree),
462
502
case lists:keyfind(env, 1, As) of
858
898
%% =====================================================================
859
%% @spec is_fail_expr(Tree::syntaxTree()) -> bool()
899
%% @spec is_fail_expr(Tree::syntaxTree()) -> boolean()
861
901
%% @doc Returns `true' if `Tree' represents an
862
902
%% expression which never terminates normally. Note that the reverse
1038
1080
%% @see erl_syntax:error_marker_info/1
1039
1081
%% @see erl_syntax:warning_marker_info/1
1083
-type key() :: 'attributes' | 'errors' | 'exports' | 'functions' | 'imports'
1084
| 'module' | 'records' | 'rules' | 'warnings'.
1085
-type info_pair() :: {key(), term()}.
1087
-spec analyze_forms(erl_syntax:forms()) -> [info_pair()].
1041
1089
analyze_forms(Forms) when is_list(Forms) ->
1042
1090
finfo_to_list(lists:foldl(fun collect_form/2, new_finfo(), Forms));
1043
1091
analyze_forms(Forms) ->
1083
1131
%% Abstract datatype for collecting module information.
1085
-record(forms, {module, exports, module_imports, imports, attributes,
1086
records, errors, warnings, functions, rules}).
1133
-record(forms, {module = none :: 'none' | {'value', atom()},
1134
exports = [] :: [{atom(), arity()}],
1135
module_imports = [] :: [atom()],
1136
imports = [] :: [{atom(), [{atom(), arity()}]}],
1137
attributes = [] :: [{atom(), term()}],
1138
records = [] :: [{atom(), [{atom(), field_default()}]}],
1139
errors = [] :: [term()],
1140
warnings = [] :: [term()],
1141
functions = [] :: [{atom(), arity()}],
1142
rules = [] :: [{atom(), arity()}]}).
1144
-type field_default() :: 'none' | erl_syntax:syntaxTree().
1089
#forms{module = none,
1091
module_imports = [],
1101
1149
finfo_set_module(Name, Info) ->
1102
1150
case Info#forms.module of
1204
1252
%% @see erl_syntax:error_marker_info/1
1205
1253
%% @see erl_syntax:warning_marker_info/1
1255
-spec analyze_form(erl_syntax:syntaxTree()) -> {atom(), term()} | atom().
1207
1257
analyze_form(Node) ->
1208
1258
case erl_syntax:type(Node) of
1276
1326
%% @see analyze_record_attribute/1
1277
1327
%% @see analyze_wild_attribute/1
1329
-spec analyze_attribute(erl_syntax:syntaxTree()) ->
1330
'preprocessor' | {atom(), term()}. % XXX: underspecified
1279
1332
analyze_attribute(Node) ->
1280
1333
Name = erl_syntax:attribute_name(Node),
1281
1334
case erl_syntax:type(Name) of
1326
1379
%% containing the module name and a list of the parameter variable
1329
%% The evaluation throws `syntax_error' if
1330
%% `Node' does not represent a well-formed module
1382
%% The evaluation throws `syntax_error' if `Node' does not represent a
1383
%% well-formed module attribute.
1333
1385
%% @see analyze_attribute/1
1387
-spec analyze_module_attribute(erl_syntax:syntaxTree()) ->
1388
atom() | {atom(), [atom()]}.
1335
1390
analyze_module_attribute(Node) ->
1336
1391
case erl_syntax:type(Node) of
1370
1425
%% attribute. We do not guarantee that each name occurs at most once in
1371
1426
%% the list. The order of listing is not defined.
1373
%% The evaluation throws `syntax_error' if
1374
%% `Node' does not represent a well-formed export
1428
%% The evaluation throws `syntax_error' if `Node' does not represent a
1429
%% well-formed export attribute.
1377
1431
%% @see analyze_attribute/1
1433
-type functionN() :: atom() | {atom(), arity()}.
1434
-type functionName() :: functionN() | {atom(), functionN()}.
1436
-spec analyze_export_attribute(erl_syntax:syntaxTree()) -> [functionName()].
1379
1438
analyze_export_attribute(Node) ->
1380
1439
case erl_syntax:type(Node) of
1416
1475
%% The evaluation throws `syntax_error' if
1417
1476
%% `Node' does not represent a well-formed function name.
1478
-spec analyze_function_name(erl_syntax:syntaxTree()) -> functionName().
1419
1480
analyze_function_name(Node) ->
1420
1481
case erl_syntax:type(Node) of
1470
1531
%% that each name occurs at most once in `Names'. The order
1471
1532
%% of listing is not defined.
1473
%% The evaluation throws `syntax_error' if
1474
%% `Node' does not represent a well-formed import
1534
%% The evaluation throws `syntax_error' if `Node' does not represent a
1535
%% well-formed import attribute.
1477
1537
%% @see analyze_attribute/1
1539
-spec analyze_import_attribute(erl_syntax:syntaxTree()) ->
1540
{atom(), [functionName()]} | atom().
1479
1542
analyze_import_attribute(Node) ->
1480
1543
case erl_syntax:type(Node) of
1498
1561
%% @spec analyze_wild_attribute(Node::syntaxTree()) -> {atom(), term()}
1500
1563
%% @doc Returns the name and value of a "wild" attribute. The result is
1501
%% the pair `{Name, Value}', if `Node' represents
1502
%% "`-Name(Value)'".
1564
%% the pair `{Name, Value}', if `Node' represents "`-Name(Value)'".
1504
1566
%% Note that no checking is done whether `Name' is a
1505
1567
%% reserved attribute name such as `module' or
1506
1568
%% `export': it is assumed that the attribute is "wild".
1508
%% The evaluation throws `syntax_error' if
1509
%% `Node' does not represent a well-formed wild
1570
%% The evaluation throws `syntax_error' if `Node' does not represent a
1571
%% well-formed wild attribute.
1512
1573
%% @see analyze_attribute/1
1575
-spec analyze_wild_attribute(erl_syntax:syntaxTree()) -> {atom(), term()}.
1514
1577
analyze_wild_attribute(Node) ->
1515
1578
case erl_syntax:type(Node) of
1559
1622
%% @see analyze_attribute/1
1560
1623
%% @see analyze_record_field/1
1625
-type fields() :: [{atom(), 'none' | erl_syntax:syntaxTree()}].
1627
-spec analyze_record_attribute(erl_syntax:syntaxTree()) -> {atom(), fields()}.
1562
1629
analyze_record_attribute(Node) ->
1563
1630
case erl_syntax:type(Node) of
1629
1696
%% @see analyze_record_attribute/1
1630
1697
%% @see analyze_record_field/1
1699
-type info() :: {atom(), [{atom(), 'none' | erl_syntax:syntaxTree()}]}
1700
| {atom(), atom()} | atom().
1702
-spec analyze_record_expr(erl_syntax:syntaxTree()) -> {atom(), info()} | atom().
1632
1704
analyze_record_expr(Node) ->
1633
1705
case erl_syntax:type(Node) of
1700
1772
%% @see analyze_record_attribute/1
1701
1773
%% @see analyze_record_expr/1
1775
-spec analyze_record_field(erl_syntax:syntaxTree()) ->
1776
{atom(), 'none' | erl_syntax:syntaxTree()}.
1703
1778
analyze_record_field(Node) ->
1704
1779
case erl_syntax:type(Node) of
1705
1780
record_field ->
1731
1806
%% @see analyze_attribute/1
1808
-spec analyze_file_attribute(erl_syntax:syntaxTree()) -> {string(), integer()}.
1733
1810
analyze_file_attribute(Node) ->
1734
1811
case erl_syntax:type(Node) of
1827
1908
%% @see analyze_function_name/1
1910
-spec analyze_implicit_fun(erl_syntax:syntaxTree()) -> functionName().
1829
1912
analyze_implicit_fun(Node) ->
1830
1913
case erl_syntax:type(Node) of
1831
1914
implicit_fun ->
1832
analyze_function_name(
1833
erl_syntax:implicit_fun_name(Node));
1915
analyze_function_name(erl_syntax:implicit_fun_name(Node));
1835
1917
throw(syntax_error)
1851
1933
%% function is not explicitly named (i.e., `F' is given by
1852
1934
%% some expression), only the arity `A' is returned.
1854
%% The evaluation throws `syntax_error' if
1855
%% `Node' does not represent a well-formed application
1936
%% The evaluation throws `syntax_error' if `Node' does not represent a
1937
%% well-formed application expression.
1858
1939
%% @see analyze_function_name/1
1941
-type appFunName() :: {atom(), arity()} | {atom(), {atom(), arity()}}.
1943
-spec analyze_application(erl_syntax:syntaxTree()) -> appFunName() | arity().
1860
1945
analyze_application(Node) ->
1861
1946
case erl_syntax:type(Node) of
1898
1983
%% @see analyze_function_name/1
1985
-type shortname() :: atom() | {atom(), arity()}.
1986
-type name() :: shortname() | {atom(), shortname()}.
1988
-spec function_name_expansions([name()]) -> [{shortname(), name()}].
1900
1990
function_name_expansions(Fs) ->
1901
1991
function_name_expansions(Fs, []).
1922
2012
%% Standalone comments in form lists are removed; any other standalone
1923
2013
%% comments are changed into null-comments (no text, no indentation).
2015
-spec strip_comments(erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree().
1925
2017
strip_comments(Tree) ->
1926
2018
map(fun strip_comments_1/1, Tree).
1956
2050
%% @see to_comment/3
1957
2051
%% @see erl_prettypr:format/1
2053
-spec to_comment(erl_syntax:syntaxTree(), string()) -> erl_syntax:syntaxTree().
1959
2055
to_comment(Tree, Prefix) ->
1960
2056
F = fun (T) -> erl_prettypr:format(T) end,
1961
2057
to_comment(Tree, Prefix, F).
1985
2081
%% @see to_comment/1
1986
2082
%% @see to_comment/2
2084
-spec to_comment(erl_syntax:syntaxTree(), string(),
2085
fun((erl_syntax:syntaxTree()) -> string())) ->
2086
erl_syntax:syntaxTree().
1988
2088
to_comment(Tree, Prefix, F) ->
1989
2089
erl_syntax:comment(split_lines(F(Tree), Prefix)).
1998
2098
%% @see limit/3
1999
2099
%% @see erl_syntax:text/1
2101
-spec limit(erl_syntax:syntaxTree(), integer()) -> erl_syntax:syntaxTree().
2001
2103
limit(Tree, Depth) ->
2002
2104
limit(Tree, Depth, erl_syntax:text("...")).
2027
2129
%% @see limit/2
2131
-spec limit(erl_syntax:syntaxTree(), integer(), erl_syntax:syntaxTree()) ->
2132
erl_syntax:syntaxTree().
2029
2134
limit(_Tree, Depth, Node) when Depth < 0 ->
2031
2136
limit(Tree, Depth, Node) ->