0 | module Text.WebIDL.Codegen.Enum
4 | import Text.WebIDL.Codegen.Util
8 | enum : {opts : _} -> Enum -> Doc opts
9 | enum (MkEnum _ pn vs) =
10 | let (s ::: ss) := map value vs
11 | (c ::: cs) := map toDataConstructor (s ::: ss)
12 | vals := the (List $
Doc opts) $
map (\sl => line "| \{sl}") cs
13 | sl := line "data \{pn} =" <++> sep (line "\{c}" :: vals)
16 | (line "data \{pn} =")
17 | (indent 2 $
vsep (indent 2 (line "\{c}") :: vals))
22 | , line "public export"
25 | , line "public export"
26 | , line "Show \{pn} where"
27 | , indent 2 $
vsep $
zipWith showImpl (c :: cs) (s :: ss)
29 | , line "public export"
30 | , line "Eq \{pn} where"
31 | , indent 2 $
line "(==) = (==) `on` show"
33 | , line "public export"
34 | , line "Ord \{pn} where"
35 | , indent 2 $
line "compare = compare `on` show"
37 | , line "public export"
38 | , typeDecl "read" (line "Maybe \{pn}") [ line "String" ]
40 | , vsep $
zipWith readImpl (s :: ss) (c :: cs)
41 | , line "read _ = Nothing"
44 | , line "ToFFI \{pn} String where"
45 | , indent 2 $
line "toFFI = show"
48 | , line "FromFFI \{pn} String where"
49 | , indent 2 $
line "fromFFI = read"
52 | in vsep ["", line "namespace \{pn}", indent 2 code]
55 | showImpl : String -> String -> Doc opts
56 | showImpl x y = line #"show \#{x} = "\#{y}""#
58 | readImpl : String -> String -> Doc opts
59 | readImpl x y = line #"read "\#{x}" = Just \#{y}"#
62 | enums : List Enum -> String
63 | enums = section "Enums" . map (render80 . enum)