~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/syntax_tools/src/erl_syntax_lib.erl

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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]).
48
48
 
 
49
-export_type([info_pair/0]).
49
50
 
50
51
%% =====================================================================
51
52
%% @spec map(Function, Tree::syntaxTree()) -> syntaxTree()
58
59
%%
59
60
%% @see map_subtrees/2
60
61
 
 
62
-spec map(fun((erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree()),
 
63
          erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree().
 
64
 
61
65
map(F, Tree) ->
62
66
    case erl_syntax:subtrees(Tree) of
63
67
        [] ->
81
85
%%
82
86
%% @see map/2
83
87
 
 
88
-spec map_subtrees(fun((erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree()),
 
89
                   erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree().
 
90
 
84
91
map_subtrees(F, Tree) ->
85
92
    case erl_syntax:subtrees(Tree) of
86
93
        [] ->
105
112
%% @see fold_subtrees/3
106
113
%% @see foldl_listlist/3
107
114
 
 
115
-spec fold(fun((erl_syntax:syntaxTree(), term()) -> term()),
 
116
           term(), erl_syntax:syntaxTree()) -> term().
 
117
 
108
118
fold(F, S, Tree) ->
109
119
    case erl_syntax:subtrees(Tree) of
110
120
        [] ->
137
147
%%
138
148
%% @see fold/3
139
149
 
 
150
-spec fold_subtrees(fun((erl_syntax:syntaxTree(), term()) -> term()),
 
151
                    term(), erl_syntax:syntaxTree()) -> term().
 
152
 
140
153
fold_subtrees(F, S, Tree) ->
141
154
    foldl_listlist(F, S, erl_syntax:subtrees(Tree)).
142
155
 
151
164
%% @see fold/3
152
165
%% @see //stdlib/lists:foldl/3
153
166
 
 
167
-spec foldl_listlist(fun((term(), term()) -> term()),
 
168
                     term(), [[term()]]) -> term().
 
169
 
154
170
foldl_listlist(F, S, [L | Ls]) ->
155
171
    foldl_listlist(F, foldl(F, S, L), Ls);
156
172
foldl_listlist(_, S, []) ->
178
194
%% @see map/2
179
195
%% @see fold/3
180
196
 
 
197
-spec mapfold(fun((erl_syntax:syntaxTree(), term()) -> {erl_syntax:syntaxTree(), term()}),
 
198
              term(), erl_syntax:syntaxTree()) -> {erl_syntax:syntaxTree(), term()}.
 
199
 
181
200
mapfold(F, S, Tree) ->
182
201
    case erl_syntax:subtrees(Tree) of
183
202
        [] ->
216
235
%%
217
236
%% @see mapfold/3
218
237
 
 
238
-spec mapfold_subtrees(fun((erl_syntax:syntaxTree(), term()) ->
 
239
                              {erl_syntax:syntaxTree(), term()}),
 
240
                       term(), erl_syntax:syntaxTree()) ->
 
241
        {erl_syntax:syntaxTree(), term()}.
 
242
 
219
243
mapfold_subtrees(F, S, Tree) ->
220
244
    case erl_syntax:subtrees(Tree) of
221
245
        [] ->
237
261
%% The list of lists in the result has the same structure as the given
238
262
%% list of lists.
239
263
 
 
264
-spec mapfoldl_listlist(fun((term(), term()) -> {term(), term()}),
 
265
                        term(), [[term()]]) -> {[[term()]], term()}.
 
266
 
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),
263
290
%%
264
291
%% @see //stdlib/sets
265
292
 
 
293
-spec variables(erl_syntax:syntaxTree()) -> set().
 
294
 
266
295
variables(Tree) ->
267
296
    variables(Tree, sets:new()).
268
297
 
316
345
%%
317
346
%% @see new_variable_name/2
318
347
 
 
348
-spec new_variable_name(set()) -> atom().
 
349
 
319
350
new_variable_name(S) ->
320
351
    new_variable_name(fun default_variable_name/1, S).
321
352
 
340
371
%% @see //stdlib/sets
341
372
%% @see //stdlib/random
342
373
 
 
374
-spec new_variable_name(fun((integer()) -> atom()), set()) -> atom().
 
375
 
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'.
366
399
 
367
400
start_range(S) ->
368
 
    max(sets:size(S) * ?START_RANGE_FACTOR, ?MINIMUM_RANGE).
369
 
 
370
 
max(X, Y) when X > Y -> X;
371
 
max(_, Y) -> Y.
 
401
    erlang:max(sets:size(S) * ?START_RANGE_FACTOR, ?MINIMUM_RANGE).
372
402
 
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.
388
418
%% 
389
419
%% @see new_variable_name/1
390
420
 
 
421
-spec new_variable_names(integer(), set()) -> [atom()].
 
422
 
391
423
new_variable_names(N, S) ->
392
424
    new_variable_names(N, fun default_variable_name/1, S).
393
425
 
402
434
%% 
403
435
%% @see new_variable_name/2
404
436
 
 
437
-spec new_variable_names(integer(), fun((integer()) -> atom()), set()) ->
 
438
        [atom()].
 
439
 
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
443
478
 
 
479
-spec annotate_bindings(erl_syntax:syntaxTree(), ordsets:ordset(atom())) ->
 
480
        erl_syntax:syntaxTree().
 
481
 
444
482
annotate_bindings(Tree, Env) ->
445
483
    {Tree1, _, _} = vann(Tree, Env),
446
484
    Tree1.
457
495
%%
458
496
%% @see annotate_bindings/2
459
497
 
 
498
-spec annotate_bindings(erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree().
 
499
 
460
500
annotate_bindings(Tree) ->
461
501
    As = erl_syntax:get_ann(Tree),
462
502
    case lists:keyfind(env, 1, As) of
856
896
 
857
897
 
858
898
%% =====================================================================
859
 
%% @spec is_fail_expr(Tree::syntaxTree()) -> bool()
 
899
%% @spec is_fail_expr(Tree::syntaxTree()) -> boolean()
860
900
%%
861
901
%% @doc Returns `true' if `Tree' represents an
862
902
%% expression which never terminates normally. Note that the reverse
869
909
%% @see //erts/erlang:error/1
870
910
%% @see //erts/erlang:error/2
871
911
 
 
912
-spec is_fail_expr(erl_syntax:syntaxTree()) -> boolean().
 
913
 
872
914
is_fail_expr(E) ->          
873
915
    case erl_syntax:type(E) of
874
916
        application ->
1038
1080
%% @see erl_syntax:error_marker_info/1
1039
1081
%% @see erl_syntax:warning_marker_info/1
1040
1082
 
 
1083
-type key() :: 'attributes' | 'errors' | 'exports' | 'functions' | 'imports'
 
1084
             | 'module' | 'records' | 'rules' | 'warnings'.
 
1085
-type info_pair() :: {key(), term()}.
 
1086
 
 
1087
-spec analyze_forms(erl_syntax:forms()) -> [info_pair()].
 
1088
 
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) ->
1082
1130
 
1083
1131
%% Abstract datatype for collecting module information.
1084
1132
 
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()}]}).
 
1143
 
 
1144
-type field_default() :: 'none' | erl_syntax:syntaxTree().
1087
1145
 
1088
1146
new_finfo() ->
1089
 
    #forms{module = none,
1090
 
           exports = [],
1091
 
           module_imports = [],
1092
 
           imports = [],
1093
 
           attributes = [],
1094
 
           records = [],
1095
 
           errors = [],
1096
 
           warnings = [],
1097
 
           functions = [],
1098
 
           rules = []
1099
 
          }.
 
1147
    #forms{}.
1100
1148
 
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
1206
1254
 
 
1255
-spec analyze_form(erl_syntax:syntaxTree()) -> {atom(), term()} | atom().
 
1256
 
1207
1257
analyze_form(Node) ->
1208
1258
    case erl_syntax:type(Node) of
1209
1259
        attribute ->
1276
1326
%% @see analyze_record_attribute/1
1277
1327
%% @see analyze_wild_attribute/1
1278
1328
 
 
1329
-spec analyze_attribute(erl_syntax:syntaxTree()) ->
 
1330
        'preprocessor' | {atom(), term()}.  % XXX: underspecified
 
1331
 
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
1327
1380
%% names.
1328
1381
%%
1329
 
%% The evaluation throws `syntax_error' if
1330
 
%% `Node' does not represent a well-formed module
1331
 
%% attribute.
 
1382
%% The evaluation throws `syntax_error' if `Node' does not represent a
 
1383
%% well-formed module attribute.
1332
1384
%%
1333
1385
%% @see analyze_attribute/1
1334
1386
 
 
1387
-spec analyze_module_attribute(erl_syntax:syntaxTree()) ->
 
1388
        atom() | {atom(), [atom()]}.
 
1389
 
1335
1390
analyze_module_attribute(Node) ->
1336
1391
    case erl_syntax:type(Node) of
1337
1392
        attribute ->
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.
1372
1427
%%
1373
 
%% The evaluation throws `syntax_error' if
1374
 
%% `Node' does not represent a well-formed export
1375
 
%% attribute.
 
1428
%% The evaluation throws `syntax_error' if `Node' does not represent a
 
1429
%% well-formed export attribute.
1376
1430
%%
1377
1431
%% @see analyze_attribute/1
1378
1432
 
 
1433
-type functionN()    :: atom() | {atom(), arity()}.
 
1434
-type functionName() :: functionN() | {atom(), functionN()}.
 
1435
 
 
1436
-spec analyze_export_attribute(erl_syntax:syntaxTree()) -> [functionName()].
 
1437
 
1379
1438
analyze_export_attribute(Node) ->
1380
1439
    case erl_syntax:type(Node) of
1381
1440
        attribute ->
1416
1475
%% The evaluation throws `syntax_error' if
1417
1476
%% `Node' does not represent a well-formed function name.
1418
1477
 
 
1478
-spec analyze_function_name(erl_syntax:syntaxTree()) -> functionName().
 
1479
 
1419
1480
analyze_function_name(Node) ->
1420
1481
    case erl_syntax:type(Node) of
1421
1482
        atom ->
1470
1531
%% that each name occurs at most once in `Names'. The order
1471
1532
%% of listing is not defined.
1472
1533
%%
1473
 
%% The evaluation throws `syntax_error' if
1474
 
%% `Node' does not represent a well-formed import
1475
 
%% attribute.
 
1534
%% The evaluation throws `syntax_error' if `Node' does not represent a
 
1535
%% well-formed import attribute.
1476
1536
%%
1477
1537
%% @see analyze_attribute/1
1478
1538
 
 
1539
-spec analyze_import_attribute(erl_syntax:syntaxTree()) ->
 
1540
        {atom(), [functionName()]} | atom().
 
1541
 
1479
1542
analyze_import_attribute(Node) ->
1480
1543
    case erl_syntax:type(Node) of
1481
1544
        attribute ->
1498
1561
%% @spec analyze_wild_attribute(Node::syntaxTree()) -> {atom(), term()}
1499
1562
%%
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)'".
1503
1565
%%
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".
1507
1569
%%
1508
 
%% The evaluation throws `syntax_error' if
1509
 
%% `Node' does not represent a well-formed wild
1510
 
%% attribute.
 
1570
%% The evaluation throws `syntax_error' if `Node' does not represent a
 
1571
%% well-formed wild attribute.
1511
1572
%%
1512
1573
%% @see analyze_attribute/1
1513
1574
 
 
1575
-spec analyze_wild_attribute(erl_syntax:syntaxTree()) -> {atom(), term()}.
 
1576
 
1514
1577
analyze_wild_attribute(Node) ->
1515
1578
    case erl_syntax:type(Node) of
1516
1579
        attribute ->
1559
1622
%% @see analyze_attribute/1
1560
1623
%% @see analyze_record_field/1
1561
1624
 
 
1625
-type fields() :: [{atom(), 'none' | erl_syntax:syntaxTree()}].
 
1626
 
 
1627
-spec analyze_record_attribute(erl_syntax:syntaxTree()) -> {atom(), fields()}.
 
1628
 
1562
1629
analyze_record_attribute(Node) ->
1563
1630
    case erl_syntax:type(Node) of
1564
1631
        attribute ->
1629
1696
%% @see analyze_record_attribute/1
1630
1697
%% @see analyze_record_field/1
1631
1698
 
 
1699
-type info() :: {atom(), [{atom(), 'none' | erl_syntax:syntaxTree()}]}
 
1700
              | {atom(), atom()} | atom().
 
1701
 
 
1702
-spec analyze_record_expr(erl_syntax:syntaxTree()) -> {atom(), info()} | atom().
 
1703
 
1632
1704
analyze_record_expr(Node) ->
1633
1705
    case erl_syntax:type(Node) of
1634
1706
        record_expr ->
1700
1772
%% @see analyze_record_attribute/1
1701
1773
%% @see analyze_record_expr/1
1702
1774
 
 
1775
-spec analyze_record_field(erl_syntax:syntaxTree()) ->
 
1776
        {atom(), 'none' | erl_syntax:syntaxTree()}.
 
1777
 
1703
1778
analyze_record_field(Node) ->
1704
1779
    case erl_syntax:type(Node) of
1705
1780
        record_field ->
1730
1805
%%
1731
1806
%% @see analyze_attribute/1
1732
1807
 
 
1808
-spec analyze_file_attribute(erl_syntax:syntaxTree()) -> {string(), integer()}.
 
1809
 
1733
1810
analyze_file_attribute(Node) ->
1734
1811
    case erl_syntax:type(Node) of
1735
1812
        attribute ->
1765
1842
%%
1766
1843
%% @see analyze_rule/1
1767
1844
 
 
1845
-spec analyze_function(erl_syntax:syntaxTree()) -> {atom(), arity()}.
 
1846
 
1768
1847
analyze_function(Node) ->
1769
1848
    case erl_syntax:type(Node) of
1770
1849
        function ->
1794
1873
%%
1795
1874
%% @see analyze_function/1
1796
1875
 
 
1876
-spec analyze_rule(erl_syntax:syntaxTree()) -> {atom(), arity()}.
 
1877
 
1797
1878
analyze_rule(Node) ->
1798
1879
    case erl_syntax:type(Node) of
1799
1880
        rule ->
1826
1907
%%
1827
1908
%% @see analyze_function_name/1
1828
1909
 
 
1910
-spec analyze_implicit_fun(erl_syntax:syntaxTree()) -> functionName().
 
1911
 
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));
1834
1916
        _ ->
1835
1917
            throw(syntax_error)
1836
1918
    end.
1851
1933
%% function is not explicitly named (i.e., `F' is given by
1852
1934
%% some expression), only the arity `A' is returned.
1853
1935
%%
1854
 
%% The evaluation throws `syntax_error' if
1855
 
%% `Node' does not represent a well-formed application
1856
 
%% expression.
 
1936
%% The evaluation throws `syntax_error' if `Node' does not represent a
 
1937
%% well-formed application expression.
1857
1938
%%
1858
1939
%% @see analyze_function_name/1
1859
1940
 
 
1941
-type appFunName() :: {atom(), arity()} | {atom(), {atom(), arity()}}.
 
1942
 
 
1943
-spec analyze_application(erl_syntax:syntaxTree()) -> appFunName() | arity().
 
1944
 
1860
1945
analyze_application(Node) ->
1861
1946
    case erl_syntax:type(Node) of
1862
1947
        application ->
1897
1982
%%
1898
1983
%% @see analyze_function_name/1
1899
1984
 
 
1985
-type shortname() :: atom() | {atom(), arity()}.
 
1986
-type name()      :: shortname() | {atom(), shortname()}.
 
1987
 
 
1988
-spec function_name_expansions([name()]) -> [{shortname(), name()}].
 
1989
 
1900
1990
function_name_expansions(Fs) ->
1901
1991
    function_name_expansions(Fs, []).
1902
1992
 
1922
2012
%% Standalone comments in form lists are removed; any other standalone
1923
2013
%% comments are changed into null-comments (no text, no indentation).
1924
2014
 
 
2015
-spec strip_comments(erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree().
 
2016
 
1925
2017
strip_comments(Tree) ->
1926
2018
    map(fun strip_comments_1/1, Tree).
1927
2019
 
1942
2034
%% @spec to_comment(Tree) -> syntaxTree()
1943
2035
%% @equiv to_comment(Tree, "% ")
1944
2036
 
 
2037
-spec to_comment(erl_syntax:syntaxTree()) -> erl_syntax:syntaxTree().
 
2038
 
1945
2039
to_comment(Tree) ->
1946
2040
    to_comment(Tree, "% ").
1947
2041
 
1956
2050
%% @see to_comment/3
1957
2051
%% @see erl_prettypr:format/1
1958
2052
 
 
2053
-spec to_comment(erl_syntax:syntaxTree(), string()) -> erl_syntax:syntaxTree().
 
2054
 
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
1987
2083
 
 
2084
-spec to_comment(erl_syntax:syntaxTree(), string(),
 
2085
                 fun((erl_syntax:syntaxTree()) -> string())) ->
 
2086
        erl_syntax:syntaxTree().
 
2087
 
1988
2088
to_comment(Tree, Prefix, F) ->
1989
2089
    erl_syntax:comment(split_lines(F(Tree), Prefix)).
1990
2090
 
1998
2098
%% @see limit/3
1999
2099
%% @see erl_syntax:text/1
2000
2100
 
 
2101
-spec limit(erl_syntax:syntaxTree(), integer()) -> erl_syntax:syntaxTree().
 
2102
 
2001
2103
limit(Tree, Depth) ->
2002
2104
    limit(Tree, Depth, erl_syntax:text("...")).
2003
2105
 
2026
2128
%%
2027
2129
%% @see limit/2
2028
2130
 
 
2131
-spec limit(erl_syntax:syntaxTree(), integer(), erl_syntax:syntaxTree()) ->
 
2132
        erl_syntax:syntaxTree().
 
2133
 
2029
2134
limit(_Tree, Depth, Node) when Depth < 0 ->
2030
2135
    Node;
2031
2136
limit(Tree, Depth, Node) ->