aboutsummaryrefslogtreecommitdiffstats
path: root/style-generator/src/MyElm/Stringify.elm
blob: da2793ca3b732b7e1b4ca19ce903214881fe1a69 (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
module MyElm.Stringify exposing (arg2string, declaration2string, expose2string, expression2string, module2string, needsBrackets, qualifiedName2string, type2str, type2string)

import MyElm.Types exposing (..)



-- indentation


indented : String -> String
indented s =
    s
        |> String.split "\n"
        |> String.join "\n    "
        |> String.append "    "


listLike : String -> String -> String -> List String -> String
listLike before sep after l =
    let
        shouldBeMultiline =
            List.any (\ln -> List.length (String.split "\n" ln) > 1) l || List.foldl (\ln s -> s + String.length ln) 0 l > 100
    in
    if shouldBeMultiline then
        "\n" ++ indented (before ++ " " ++ String.join ("\n" ++ sep) l ++ "\n" ++ after)

    else if after == "" && before == "" then
        String.join sep l

    else
        before ++ " " ++ String.join sep l ++ " " ++ after


bodyIndent : String -> String
bodyIndent str =
    if List.length (String.split "\n" str) > 1 then
        str

    else
        "\n    " ++ str


expose2string : Exposing -> String
expose2string expose =
    case expose of
        ValueExposed val ->
            val

        TypeExposed tp ->
            tp

        TypeAndConstructors tp ->
            tp ++ "(..)"


module2string : Module -> String
module2string (Module { name, exposes, doc, imports, declarations }) =
    let
        header =
            "module " ++ name ++ " exposing (" ++ String.join ", " (List.map expose2string exposes) ++ ")\n\n"

        docstr =
            case doc of
                Just d ->
                    "{-|" ++ d ++ "-}\n\n"

                Nothing ->
                    ""

        imps =
            String.join "\n" imports
                ++ (if List.length imports > 0 then
                        "\n\n\n"

                    else
                        ""
                   )

        decs =
            String.join "" <| List.map declaration2string declarations
    in
    header ++ docstr ++ imps ++ decs


type2str : Bool -> Type -> String
type2str needsBr tp =
    case tp of
        NamedType qualifiedName typeList ->
            if List.length typeList > 0 then
                if needsBr then
                    "(" ++ qualifiedName2string qualifiedName ++ " " ++ String.join " " (List.map (type2str True) typeList) ++ ")"

                else
                    qualifiedName2string qualifiedName ++ " " ++ String.join " " (List.map (type2str True) typeList)

            else
                qualifiedName2string qualifiedName

        RecordType branches ->
            "{ " ++ String.join ", " (List.map (\( name, typ ) -> name ++ " = " ++ type2str False typ) branches) ++ " }"

        FunctionType typeList ->
            let
                a =
                    String.join " -> " (List.map (type2str False) typeList)
            in
            if needsBr then
                "(" ++ a ++ ")"

            else
                a

        TupleType typeList ->
            "( " ++ String.join ",  " (List.map (type2str False) typeList) ++ " )"

        TypeVariable name ->
            name


type2string =
    type2str False


declaration2string : Declaration -> String
declaration2string declaration =
    case declaration of
        CustomType name variables variants ->
            "type " ++ String.join " " (name :: variables) ++ "\n    = " ++ String.join "\n    | " (List.map (\( nm, args ) -> String.join " " (nm :: List.map (type2str True) args)) variants) ++ "\n\n\n"

        TypeAlias name variables aliased ->
            "type alias " ++ String.join " " (name :: variables) ++ "\n    =" ++ type2string aliased ++ "\n\n\n"

        Comment str ->
            "{-|" ++ str ++ "}"

        ValueDeclaration name anno argList expression ->
            let
                decl =
                    name ++ " " ++ String.join " " (List.map arg2string argList) ++ " =" ++ bodyIndent (expression2string expression) ++ "\n\n\n"
            in
            case anno of
                [] ->
                    decl

                signature ->
                    name ++ " : " ++ String.join " -> " (List.map type2string signature) ++ "\n" ++ decl


arg2string : Argument -> String
arg2string argument =
    case argument of
        Argument a ->
            a


qualifiedName2string : QualifiedName -> String
qualifiedName2string qualifiedName =
    let
        identifierToStr id =
            case id of
                Constructor _ s ->
                    s

                ValueOrType s ->
                    s
    in
    case qualifiedName of
        Local ident ->
            identifierToStr ident

        FullyQualified modPath ident ->
            String.join "." modPath ++ "." ++ identifierToStr ident

        Aliased _ alias_ ident ->
            alias_ ++ "." ++ identifierToStr ident

        Bare _ ident ->
            identifierToStr ident


bracketify : Expression -> String
bracketify arg =
    if needsBrackets arg then
        "(" ++ expression2string arg ++ ")"

    else
        expression2string arg


isOperator : String -> Bool
isOperator op =
    case op of
        "++" ->
            True

        "-" ->
            True

        "+" ->
            True

        "*" ->
            True

        "/" ->
            True

        "//" ->
            True

        "^" ->
            True

        "|>" ->
            True

        "<|" ->
            True

        _ ->
            False


expression2string : Expression -> String
expression2string expression =
    case expression of
        Call name args ->
            let
                nameStr =
                    qualifiedName2string name
            in
            if isOperator nameStr then
                case args of
                    a :: b :: rest ->
                        case nameStr of
                            "|>" ->
                                listLike "" " |> " "" [ expression2string a, String.join " " (List.map expression2string (b :: rest)) ]

                            _ ->
                                expression2string a ++ " " ++ nameStr ++ " " ++ String.join " " (List.map expression2string (b :: rest))

                    _ ->
                        "(" ++ nameStr ++ ") " ++ String.join " " (List.map bracketify args)

            else
                String.join " "
                    (nameStr
                        :: List.map
                            (\arg ->
                                if needsBrackets arg then
                                    "(" ++ expression2string arg ++ ")"

                                else
                                    expression2string arg
                            )
                            args
                    )

        Literal lit ->
            lit

        ListExpr expressions ->
            listLike "[" ", " "]" (List.map expression2string expressions)

        Tuple expressions ->
            listLike "(" ", " ")" (List.map expression2string expressions)

        Record branches ->
            listLike "{" ", " "}" (List.map (\( name, branch ) -> name ++ " = " ++ expression2string branch) branches)


needsBrackets : Expression -> Bool
needsBrackets expression =
    case expression of
        Call _ [] ->
            False

        Call _ _ ->
            True

        _ ->
            False