aboutsummaryrefslogtreecommitdiffstats
path: root/lib/diameter/src/compiler/diameter_make.erl
blob: e4486973dd233a80b40fb2509ddfc615e5fa6188 (plain) (blame)
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%
%% Module alternative to diameterc for dictionary compilation.
%%
%% Eg. 1> diameter_make:codec("mydict.dia").
%%
%%     $ erl -noinput \
%%           -boot start_clean \
%%           -eval 'ok = diameter_make:codec("mydict.dia")' \
%%           -s init stop
%%

-module(diameter_make).

-export([codec/1,
         codec/2,
         dict/1,
         dict/2,
         format/1,
         reformat/1]).

-export_type([opt/0]).

-type opt() :: {include|outdir|name|prefix|inherits, string()}
             | verbose
             | debug.

%% ===========================================================================

%% codec/1-2
%%
%% Parse a dictionary file and generate a codec module.

-spec codec(Path, [opt()])
   -> ok
    | {error, Reason}
 when Path :: string(),
      Reason :: string().

codec(File, Opts) ->
    case dict(File, Opts) of
        {ok, Dict} ->
            make(File,
                 Opts,
                 Dict,
                 lists:append([[dict, forms] || lists:member(debug, Opts)])
                 ++ [erl, hrl]);
        {error, _} = E ->
            E
    end.

codec(File) ->
    codec(File, []).

%% dict/2
%%
%% Parse a dictionary file and return the orddict that a codec module
%% returns from dict/0.

-spec dict(string(), [opt()])
   -> {ok, orddict:orddict()}
    | {error, string()}.

dict(Path, Opts) ->
    case diameter_dict_util:parse({path, Path}, Opts) of
        {ok, _} = Ok ->
            Ok;
        {error = E, Reason} ->
            {E, diameter_dict_util:format_error(Reason)}
    end.

dict(File) ->
    dict(File, []).

%% format/1
%%
%% Turn an orddict returned by dict/1-2 back into a dictionary file
%% in the form of an iolist().

-spec format(orddict:orddict())
   -> iolist().

format(Dict) ->
    diameter_dict_util:format(Dict).

%% reformat/1
%%
%% Parse a dictionary file and return its formatted equivalent.

-spec reformat(File)
   -> {ok, iolist()}
    | {error, Reason}
 when File :: string(),
      Reason :: string().

reformat(File) ->
    case dict(File) of
        {ok, Dict} ->
            {ok, format(Dict)};
        {error, _} = No ->
            No
    end.

%% ===========================================================================

make(_, _, _, []) ->
    ok;
make(File, Opts, Dict, [Mode | Rest]) ->
    try
        ok = diameter_codegen:from_dict(File, Dict, Opts, Mode),
        make(File, Opts, Dict, Rest)
    catch
        error: Reason ->
            erlang:error({Reason, Mode, erlang:get_stacktrace()})
    end.