This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtu_pp.erl
76 lines (57 loc) · 1.98 KB
/
dtu_pp.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
-module(dtu_pp).
-export([abovel/2, besidel/2, pp_unk_if_not_empty/2, pp_unk/3, atext/2, ntext/2, nestc/2,
new_ctx/0, quote_string/1, quote_string/2, quote_string_raw/1, quote_string_raw/2, join/4,
join_no_sep/4]).
-record(ctx, {sub_indent = 2 :: non_neg_integer()}).
-import(prettypr, [beside/2, empty/0, text/1, par/2, nest/2, above/2]).
new_ctx() ->
#ctx{}.
ast_as_comment(Label, Ast) ->
text(io_lib:format("/* UNK ~s: ~p */", [Label, Ast])).
nestc(Layout, Ctx) ->
nest(Ctx#ctx.sub_indent, Layout).
atext(Atom, _Ctx) ->
text(atom_to_list(Atom)).
ntext(V, _Ctx) ->
text(io_lib:format("~p", [V])).
pp_unk_if_not_empty([], _Ctx) ->
empty();
pp_unk_if_not_empty(Ast, Ctx) ->
pp_unk("not empty", Ast, Ctx).
pp_unk(Label, Ast, _Ctx) ->
ast_as_comment(Label, Ast).
abovel([], _Ctx) ->
empty();
abovel([H], _Ctx) ->
H;
% maybe skip empty() here?
abovel([H | T], Ctx) ->
above(H, abovel(T, Ctx)).
besidel([], _Ctx) ->
empty();
besidel([H], _Ctx) ->
H;
besidel([H | T], Ctx) ->
beside(H, besidel(T, Ctx)).
quote_string(V) ->
quote_string(V, $").
quote_string(V, QuoteChar) ->
text(quote_string_raw(V, QuoteChar)).
quote_string_raw(V) ->
quote_string_raw(V, $").
quote_string_raw(V, QuoteChar) ->
io_lib:write_string(V, QuoteChar).
join(Items, Ctx, PPFun, Sep) ->
join(Items, Ctx, PPFun, Sep, fun(V, _Ctx) -> par(V, 2) end, []).
join_no_sep(Items, Ctx, PPFun, Sep) ->
join(Items, Ctx, PPFun, Sep, fun(V, Ctx1) -> besidel(V, Ctx1) end, []).
join([], _Ctx, _PPFun, _Sep, _FFun, []) ->
empty();
join([Item], Ctx, PPFun, _Sep, _FFun, []) ->
PPFun(Item, Ctx);
join([Item], Ctx, PPFun, _Sep, FFun, Accum) ->
FFun(lists:reverse([PPFun(Item, Ctx) | Accum]), Ctx);
join([H | T = [_ | _]], Ctx, PPFun, Sep, FFun, Accum) ->
join(T, Ctx, PPFun, Sep, FFun, [beside(PPFun(H, Ctx), Sep) | Accum]);
join([H | T], Ctx, PPFun, Sep, FFun, Accum) ->
join([T], Ctx, PPFun, Sep, FFun, [beside(PPFun(H, Ctx), Sep) | Accum]).