summaryrefslogtreecommitdiff
path: root/src/rebar_dialyzer_format.erl
blob: 56a30aa749e4135b5cb47143074dc0ea9f90ec95 (plain)
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
-module(rebar_dialyzer_format).

-export([format/1]).


-define(NR, "\033[0;31m").
-define(NG, "\033[0;32m").
-define(NY, "\033[0;33m").
-define(NB, "\033[0;34m").
-define(NM, "\033[0;35m").
-define(NC, "\033[0;36m").
-define(NW, "\033[0;37m").
-define(BR, "\033[1;31m").
-define(BG, "\033[1;32m").
-define(BY, "\033[1;33m").
-define(BB, "\033[1;34m").
-define(BM, "\033[1;35m").
-define(BC, "\033[1;36m").
-define(BW, "\033[1;37m").
-define(R,  "\033[0m").

format(Warning) ->
    format_warning(Warning, fullpath),
    Str = try
              format_warning(Warning, fullpath)
          catch
              _:_ ->
                  dialyzer:format_warning(Warning, fullpath)
          end,
    case strip(Str) of
        ":0: " ++ Unknown ->
            Unknown;
        Warning1 ->
            Warning1
    end.

strip(Warning) ->
    string:strip(Warning, right, $\n).

%%fmt(Fmt, Args) ->
%%    Args2 = [fmt("~s\033[1;37m", [A]) || A <- Args],
%%    fmt(Fmt, Args2).

fmt(Fmt) ->
    fmt(Fmt, []).
fmt(Fmt, Args) ->
    io_lib:format(cfmt(Fmt), Args).


format_warning({Tag, {File, Line, _MFA}, Msg}, FOpt) ->
    format_warning({Tag, {File, Line}, Msg}, FOpt);
format_warning({_Tag, {File, Line}, Msg}, FOpt) when is_list(File),
                                                     is_integer(Line) ->
    F = case FOpt of
            fullpath -> re:replace(File, "^.*/_build/", "_build/");
            basename -> filename:basename(File)
        end,
    String = lists:flatten(message_to_string(Msg)),
    lists:flatten(fmt("~s:~w~n~s", [F, Line, String])).


%% FROM https://github.com/erlware/erlware_commons/blob/49bc69e35a282bde4a0a6a8f211b5f77d8585256/src/ec_cmd_log.erl
%% @doc Query the term enviroment
%% For reasons of simplicity, we don't parse terminal capabilities yet, although
%% a later version could do so. Rather, we provide a simple match-list of terminal
%% capabilities.
%% @end
-spec query_term_env() -> full | dumb.
query_term_env() ->
    term_capabilities(os:getenv("TERM")).

-spec term_capabilities(string()) -> full | dumb.
term_capabilities("xterm") -> full;
term_capabilities("dumb") -> dumb;
term_capabilities(_) -> full. %% Default to the backwards compatible version.


cfmt(S) ->
    cfmt(S, query_term_env() =:= full).
cfmt(S, true) ->
    lists:flatten(cfmt_(S, true)) ++ ?R;
cfmt(S, false) ->
    lists:flatten(cfmt_(S, false)).

cfmt_([$~,$!,_C | S], false) ->
    cfmt_(S, false);

cfmt_([$~,$!,$! | S], Enabled) ->
    [?R | cfmt_(S, Enabled)];

cfmt_([$~,$!,$r | S], Enabled) ->
    [?NR | cfmt_(S, Enabled)];
cfmt_([$~,$!,$R | S], Enabled) ->
    [?BR | cfmt_(S, Enabled)];

cfmt_([$~,$!,$g | S], Enabled) ->
    [?NG | cfmt_(S, Enabled)];
cfmt_([$~,$!,$G | S], Enabled) ->
    [?BG | cfmt_(S, Enabled)];

cfmt_([$~,$!,$y | S], Enabled) ->
    [?NY | cfmt_(S, Enabled)];
cfmt_([$~,$!,$Y | S], Enabled) ->
    [?BY | cfmt_(S, Enabled)];

cfmt_([$~,$!,$b | S], Enabled) ->
    [?NB | cfmt_(S, Enabled)];
cfmt_([$~,$!,$B | S], Enabled) ->
    [?BB | cfmt_(S, Enabled)];

cfmt_([$~,$!,$m | S], Enabled) ->
    [?NM | cfmt_(S, Enabled)];
cfmt_([$~,$!,$M | S], Enabled) ->
    [?BM | cfmt_(S, Enabled)];

cfmt_([$~,$!,$c | S], Enabled) ->
    [?NC | cfmt_(S, Enabled)];
cfmt_([$~,$!,$C | S], Enabled) ->
    [?BC | cfmt_(S, Enabled)];

cfmt_([$~,$!,$w | S], Enabled) ->
    [?NW | cfmt_(S, Enabled)];
cfmt_([$~,$!,$W | S], Enabled) ->
    [?BW | cfmt_(S, Enabled)];

cfmt_([$~,$~ | S], Enabled) ->
    [$~,$~ | cfmt_(S, Enabled)];

cfmt_([C | S], Enabled) ->
    [C | cfmt_(S, Enabled)];

cfmt_([], _Enabled) ->
    [].

%%-----------------------------------------------------------------------------
%% Message classification and pretty-printing below. Messages appear in
%% categories and in more or less alphabetical ordering within each category.
%%-----------------------------------------------------------------------------

%%----- Warnings for general discrepancies ----------------
message_to_string({apply, [Args, ArgNs, FailReason,
                           SigArgs, SigRet, Contract]}) ->
    fmt("~!WFun application with arguments ~!!~s ",
           [bad_arg(ArgNs, Args)]) ++
        call_or_apply_to_string(ArgNs, FailReason, SigArgs, SigRet, Contract);
message_to_string({app_call, [M, F, Args, Culprit, ExpectedType, FoundType]}) ->
    fmt("~!WThe call~!! ~s:~s~s ~!Wrequires that"
           "~!! ~s ~!Wis of type ~!g~s~!W not ~!r~s"
           "~!!\n",
           [M, F, Args, Culprit, ExpectedType, FoundType]);
message_to_string({bin_construction, [Culprit, Size, Seg, Type]}) ->
    fmt("~!WBinary construction will fail since the ~!b~s~!W field~!!"
           " ~s~!W in segment~!! ~s~!W has type~!! ~s\n",
           [Culprit, Size, Seg, Type]);
message_to_string({call, [M, F, Args, ArgNs, FailReason,
                          SigArgs, SigRet, Contract]}) ->
    fmt("~!WThe call~!! ~w:~w~s ", [M, F, bad_arg(ArgNs, Args)]) ++
        call_or_apply_to_string(ArgNs, FailReason, SigArgs, SigRet, Contract);
message_to_string({call_to_missing, [M, F, A]}) ->
    fmt("~!WCall to missing or unexported function ~!!~w:~w/~w\n",
           [M, F, A]);
message_to_string({exact_eq, [Type1, Op, Type2]}) ->
    fmt("~!WThe test ~!!~s ~s ~s~!W can never evaluate to 'true'\n",
           [Type1, Op, Type2]);
message_to_string({fun_app_args, [Args, Type]}) ->
    fmt("~!WFun application with arguments ~!!~s~!W will fail"
           " since the function has type ~!!~s\n", [Args, Type]);
message_to_string({fun_app_no_fun, [Op, Type, Arity]}) ->
    fmt("~!WFun application will fail since ~!!~s ~!W::~!! ~s"
           " is not a function of arity ~!!~w\n", [Op, Type, Arity]);
message_to_string({guard_fail, []}) ->
    "~!WClause guard cannot succeed.\n~!!";
message_to_string({guard_fail, [Arg1, Infix, Arg2]}) ->
    fmt("~!WGuard test ~!!~s ~s ~s~!W can never succeed\n",
           [Arg1, Infix, Arg2]);
message_to_string({neg_guard_fail, [Arg1, Infix, Arg2]}) ->
    fmt("~!WGuard test not(~!!~s ~s ~s~!W) can never succeed\n",
           [Arg1, Infix, Arg2]);
message_to_string({guard_fail, [Guard, Args]}) ->
    fmt("~!WGuard test ~!!~w~s~!W can never succeed\n",
           [Guard, Args]);
message_to_string({neg_guard_fail, [Guard, Args]}) ->
    fmt("~!WGuard test not(~!!~w~s~!W) can never succeed\n",
           [Guard, Args]);
message_to_string({guard_fail_pat, [Pat, Type]}) ->
    fmt("~!WClause guard cannot succeed. The ~!!~s~!W was matched"
           " against the type ~!!~s\n", [Pat, Type]);
message_to_string({improper_list_constr, [TlType]}) ->
    fmt("~!WCons will produce an improper list"
           " since its ~!b2~!!nd~!W argument is~!! ~s\n", [TlType]);
message_to_string({no_return, [Type|Name]}) ->
    NameString =
        case Name of
            [] -> "~!WThe created fun ";
            [F, A] -> fmt("~!WFunction ~!r~w/~w ", [F, A])
        end,
    case Type of
        no_match -> fmt("~s~!Whas no clauses that will ever match\n",[NameString]);
        only_explicit -> fmt("~s~!Wonly terminates with explicit exception\n", [NameString]);
        only_normal -> fmt("~s~!W~!Whas no local return\n", [NameString]);
        both -> fmt("~s~!W~!Whas no local return\n", [NameString])
    end;
message_to_string({record_constr, [RecConstr, FieldDiffs]}) ->
    fmt("~!WRecord construction ~!!~s~!W violates the"
           " declared type of field ~!!~s\n", [RecConstr, FieldDiffs]);
message_to_string({record_constr, [Name, Field, Type]}) ->
    fmt("~!WRecord construction violates the declared type for ~!!#~w{}~!W"
           " since ~!!~s~!W cannot be of type ~!!~s\n",
           [Name, Field, Type]);
message_to_string({record_matching, [String, Name]}) ->
    fmt("~!WThe ~!!~s~!W violates the"
           " declared type for ~!!#~w{}\n", [String, Name]);
message_to_string({record_match, [Pat, Type]}) ->
    fmt("~!WMatching of ~!!~s~!W tagged with a record name violates the"
           " declared type of ~!!~s\n", [Pat, Type]);
message_to_string({pattern_match, [Pat, Type]}) ->
    fmt("~!WThe ~s~!W can never match the type ~!g~s\n",
           [bad_pat(Pat), Type]);
message_to_string({pattern_match_cov, [Pat, Type]}) ->
    fmt("~!WThe ~s~!W can never match since previous"
           " clauses completely covered the type ~!g~s\n",
           [bad_pat(Pat), Type]);
message_to_string({unmatched_return, [Type]}) ->
    fmt("~!WExpression produces a value of type ~!!~s~!W,"
           " but this value is unmatched\n", [Type]);
message_to_string({unused_fun, [F, A]}) ->
    fmt("~!WFunction ~!r~w/~w~!W will never be called\n", [F, A]);
%%----- Warnings for specs and contracts -------------------
message_to_string({contract_diff, [M, F, _A, Contract, Sig]}) ->
    fmt("~!WType specification ~!!~w:~w~s~!W"
           " is not equal to the success typing: ~!!~w:~w~s\n",
           [M, F, Contract, M, F, Sig]);
message_to_string({contract_subtype, [M, F, _A, Contract, Sig]}) ->
    fmt("~!WType specification ~!!~w:~w~s~!W"
           " is a subtype of the success typing: ~!!~w:~w~s\n",
           [M, F, Contract, M, F, Sig]);
message_to_string({contract_supertype, [M, F, _A, Contract, Sig]}) ->
    fmt("~!WType specification ~!!~w:~w~s~!W"
           " is a supertype of the success typing: ~!!~w:~w~s\n",
           [M, F, Contract, M, F, Sig]);
message_to_string({contract_range, [Contract, M, F, ArgStrings, Line, CRet]}) ->
    fmt("~!WThe contract ~!!~w:~w~s~!W cannot be right because the"
           " inferred return for ~!!~w~s~!W on line ~!!~w~!W is ~!!~s\n",
           [M, F, Contract, F, ArgStrings, Line, CRet]);
message_to_string({invalid_contract, [M, F, A, Sig]}) ->
    fmt("~!WInvalid type specification for function~!! ~w:~w/~w."
           "~!W The success typing is~!! ~s\n", [M, F, A, Sig]);
message_to_string({extra_range, [M, F, A, ExtraRanges, SigRange]}) ->
    fmt("~!WThe specification for ~!!~w:~w/~w~!W states that the function"
           " might also return ~!!~s~!W but the inferred return is ~!!~s\n",
           [M, F, A, ExtraRanges, SigRange]);
message_to_string({overlapping_contract, [M, F, A]}) ->
    fmt("~!WOverloaded contract for ~!!~w:~w/~w~!W has overlapping"
           " domains; such contracts are currently unsupported and are simply "
           "ignored\n", [M, F, A]);
message_to_string({spec_missing_fun, [M, F, A]}) ->
    fmt("~!WContract for function that does not exist: ~!!~w:~w/~w\n",
           [M, F, A]);
%%----- Warnings for opaque type violations -------------------
message_to_string({call_with_opaque, [M, F, Args, ArgNs, ExpArgs]}) ->
    fmt("~!WThe call ~!!~w:~w~s~!W contains ~!!~s~!W when ~!!~s\n",
           [M, F, Args, form_positions(ArgNs), form_expected(ExpArgs)]);
message_to_string({call_without_opaque, [M, F, Args, [{N,_,_}|_] = ExpectedTriples]}) ->
    fmt("~!WThe call ~!!~w:~w~s ~!Wdoes not have~!! ~s\n",
        [M, F, bad_arg(N, Args), form_expected_without_opaque(ExpectedTriples)]);
message_to_string({opaque_eq, [Type, _Op, OpaqueType]}) ->
    fmt("~!WAttempt to test for equality between a term of type ~!!~s~!W"
           " and a term of opaque type ~!!~s\n", [Type, OpaqueType]);
message_to_string({opaque_guard, [Arg1, Infix, Arg2, ArgNs]}) ->
    fmt("~!WGuard test ~!!~s ~s ~s~!W contains ~!!~s\n",
           [Arg1, Infix, Arg2, form_positions(ArgNs)]);
message_to_string({opaque_guard, [Guard, Args]}) ->
    fmt("~!WGuard test ~!!~w~s~!W breaks the opaqueness of its"
           " argument\n", [Guard, Args]);
message_to_string({opaque_match, [Pat, OpaqueType, OpaqueTerm]}) ->
    Term = if OpaqueType =:= OpaqueTerm -> "the term";
              true -> OpaqueTerm
           end,
    fmt("~!WThe attempt to match a term of type ~!!~s~!W against the"
           "~!! ~s~!W breaks the opaqueness of ~!!~s\n",
           [OpaqueType, Pat, Term]);
message_to_string({opaque_neq, [Type, _Op, OpaqueType]}) ->
    fmt("~!WAttempt to test for inequality between a term of type ~!!~s"
           "~!W and a term of opaque type ~!!~s\n", [Type, OpaqueType]);
message_to_string({opaque_type_test, [Fun, Args, Arg, ArgType]}) ->
    fmt("~!WThe type test ~!!~s~s~!W breaks the opaqueness of the term "
           "~!!~s~s\n", [Fun, Args, Arg, ArgType]);
message_to_string({opaque_size, [SizeType, Size]}) ->
    fmt("~!WThe size ~!!~s~!W breaks the opaqueness of ~!!~s\n",
           [SizeType, Size]);
message_to_string({opaque_call, [M, F, Args, Culprit, OpaqueType]}) ->
    fmt("~!WThe call ~!!~s:~s~s~!W breaks the opaqueness of the term~!!"
           " ~s :: ~s\n", [M, F, Args, Culprit, OpaqueType]);
%%----- Warnings for concurrency errors --------------------
message_to_string({race_condition, [M, F, Args, Reason]}) ->
    fmt("~!WThe call ~!!~w:~w~s ~s\n", [M, F, Args, Reason]);
%%----- Warnings for behaviour errors --------------------
message_to_string({callback_type_mismatch, [B, F, A, ST, CT]}) ->
    fmt("~!WThe inferred return type of~!! ~w/~w (~s) ~!W"
           "has nothing in common with~!! ~s, ~!Wwhich is the expected"
           " return type for the callback of~!! ~w ~!Wbehaviour\n",
           [F, A, ST, CT, B]);
message_to_string({callback_arg_type_mismatch, [B, F, A, N, ST, CT]}) ->
    fmt("~!WThe inferred type for the~!! ~s ~!Wargument of~!!"
           " ~w/~w (~s) ~!Wis not a supertype of~!! ~s~!W, which is"
           "expected type for this argument in the callback of the~!! ~w "
           "~!Wbehaviour\n",
           [ordinal(N), F, A, ST, CT, B]);
message_to_string({callback_spec_type_mismatch, [B, F, A, ST, CT]}) ->
    fmt("~!WThe return type ~!!~s~!W in the specification of ~!!"
           "~w/~w~!W is not a subtype of ~!!~s~!W, which is the expected"
           " return type for the callback of ~!!~w~!W behaviour\n",
           [ST, F, A, CT, B]);
message_to_string({callback_spec_arg_type_mismatch, [B, F, A, N, ST, CT]}) ->
    fmt("~!WThe specified type for the ~!!~s~!W argument of ~!!"
           "~w/~w (~s)~!W is not a supertype of ~!!~s~!W, which is"
           " expected type for this argument in the callback of the ~!!~w"
           "~!W behaviour\n", [ordinal(N), F, A, ST, CT, B]);
message_to_string({callback_missing, [B, F, A]}) ->
    fmt("~!WUndefined callback function ~!!~w/~w~!W (behaviour ~!!"
           "'~w'~!W)\n",[F, A, B]);
message_to_string({callback_info_missing, [B]}) ->
    fmt("~!WCallback info about the ~!r~w~!W"
           " behaviour is not available\n", [B]);
%%----- Warnings for unknown functions, types, and behaviours -------------
message_to_string({unknown_type, {M, F, A}}) ->
    fmt("~!WUnknown type ~!r~w:~w/~w", [M, F, A]);
message_to_string({unknown_function, {M, F, A}}) ->
    fmt("~!WUnknown function ~!r~w:~w/~w", [M, F, A]);
message_to_string({unknown_behaviour, B}) ->
    fmt("~!WUnknown behaviour ~!r~w", [B]).

%%-----------------------------------------------------------------------------
%% Auxiliary functions below
%%-----------------------------------------------------------------------------

call_or_apply_to_string(ArgNs, FailReason, SigArgs, SigRet,
                        {IsOverloaded, Contract}) ->
    PositionString = form_position_string(ArgNs),
    case FailReason of
        only_sig ->
            case ArgNs =:= [] of
                true ->
                    %% We do not know which argument(s) caused the failure
                    fmt("~!Wwill never return since the success typing arguments"
                        " are ~!!~s\n", [SigArgs]);
                false ->
                    fmt("~!Wwill never return since it differs in the~!!"
                           " ~s ~!Wargument from the success typing"
                           " arguments:~!! ~s\n",
                           [PositionString, good_arg(ArgNs, SigArgs)])
            end;
        only_contract ->
            case (ArgNs =:= []) orelse IsOverloaded of
                true ->
                    %% We do not know which arguments caused the failure
                    fmt("~!Wbreaks the contract~!! ~s\n", [Contract]);
                false ->
                    fmt("~!Wbreaks the contract~!! ~s ~!Win the~!!"
                           " ~s ~!Wargument\n",
                           [good_arg(ArgNs, Contract), PositionString])
            end;
        both ->
            fmt("~!Wwill never return since the success typing is "
                   "~!!~s ~!W->~!! ~s ~!Wand the contract is ~!!~s\n",
                   [good_arg(ArgNs, SigArgs), SigRet,
                    good_arg(ArgNs, Contract)])
    end.

form_positions(ArgNs) ->
    case ArgNs of
        [_] -> "an opaque term as ";
        [_,_|_] -> "opaque terms as "
    end ++ form_position_string(ArgNs) ++
        case ArgNs of
            [_] -> " argument";
            [_,_|_] -> " arguments"
        end.

%% We know which positions N are to blame;
%% the list of triples will never be empty.
form_expected_without_opaque([{N, T, TStr}]) ->
    FStr = case erl_types:t_is_opaque(T) of
               true  ->
                   "~!Wan opaque term of type~!g ~s ~!Was ";
               false ->
                   "~!Wa term of type ~!g~s ~!W(with opaque subterms) as "
           end ++ form_position_string([N]) ++ "~!W argument",
    fmt(FStr, [TStr]);

form_expected_without_opaque(ExpectedTriples) -> %% TODO: can do much better here
    {ArgNs, _Ts, _TStrs} = lists:unzip3(ExpectedTriples),
    "opaque terms as " ++ form_position_string(ArgNs) ++ " arguments".

form_expected(ExpectedArgs) ->
    case ExpectedArgs of
        [T] ->
            TS = erl_types:t_to_string(T),
            case erl_types:t_is_opaque(T) of
                true  -> fmt("~!Wan opaque term of type ~!!~s~!W is"
                                " expected", [TS]);
                false -> fmt("~!Wa structured term of type ~!!~s~!W is"
                                " expected", [TS])
            end;
        [_,_|_] -> fmt("~!Wterms of different types are expected in these"
                          " positions", [])
    end.

form_position_string(ArgNs) ->
    case ArgNs of
        [] -> "";
        [N1] -> ordinal(N1);
        [_,_|_] ->
            [Last|Prevs] = lists:reverse(ArgNs),
            ", " ++ Head = lists:flatten([fmt(", ~s",[ordinal(N)]) ||
                                             N <- lists:reverse(Prevs)]),
            Head ++ " and " ++ ordinal(Last)
    end.

ordinal(1) -> fmt("~!B1~!!st");
ordinal(2) -> fmt("~!B2~!!nd");
ordinal(3) -> fmt("~!B3~!!rd");
ordinal(N) when is_integer(N) -> fmt("~!B~w~!!th", [N]).


bad_pat("pattern " ++ P) ->
    fmt("pattern ~!r~s",[P]);
bad_pat("variable " ++ P) ->
    fmt("variable ~!r~s",[P]);
bad_pat(P) ->
    fmt("~!r~s",[P]).


bad_arg(N, Args) ->
    color_arg(N, g, Args).

good_arg(N, Args) ->
    color_arg(N, r, Args).
color_arg(N, C, Args) when is_integer(N) ->
    color_arg([N], C, Args);
color_arg(Ns, C, Args) ->
    Args1 = seperate_args(Args),
    Args2 = highlight(Ns, 1, C, Args1),
    join_args(Args2).


highlight([], _N, _C, Rest) ->
    Rest;

highlight([N | Nr], N, g, [Arg | Rest]) ->
    [fmt("~!g~s", [Arg]) | highlight(Nr, N+1, g, Rest)];

highlight([N | Nr], N, r, [Arg | Rest]) ->
    [fmt("~!r~s", [Arg]) | highlight(Nr, N+1, r, Rest)];

highlight(Ns, N, C, [Arg | Rest]) ->
    [Arg | highlight(Ns, N + 1, C, Rest)].

seperate_args([$( | S]) ->
    seperate_args([], S, "", []).



%% We strip this space since dialyzer is inconsistant in adding or not adding
%% it ....
seperate_args([], [$,, $\s | R], Arg, Args) ->
    seperate_args([], R, [], [lists:reverse(Arg) | Args]);

seperate_args([], [$, | R], Arg, Args) ->
    seperate_args([], R, [], [lists:reverse(Arg) | Args]);

seperate_args([], [$)], Arg, Args) ->
    lists:reverse([lists:reverse(Arg) | Args]);
seperate_args([C | D], [C | R], Arg, Args) ->
    seperate_args(D, R, [C | Arg], Args);
%% Brackets
seperate_args(D, [${ | R], Arg, Args) ->
    seperate_args([$}|D], R, [${ | Arg], Args);

seperate_args(D, [$( | R], Arg, Args) ->
    seperate_args([$)|D], R, [$( | Arg], Args);

seperate_args(D, [$[ | R], Arg, Args) ->
    seperate_args([$]|D], R, [$[ | Arg], Args);

seperate_args(D, [$< | R], Arg, Args) ->
    seperate_args([$>|D], R, [$< | Arg], Args);
%% 'strings'
seperate_args(D, [$' | R], Arg, Args) ->
    seperate_args([$'|D], R, [$' | Arg], Args);
seperate_args(D, [$" | R], Arg, Args) ->
    seperate_args([$"|D], R, [$" | Arg], Args);

seperate_args(D, [C | R], Arg, Args) ->
    seperate_args(D, R, [C | Arg], Args).

join_args(Args) ->
    [$(, string:join(Args, ", "), $)].