on 0bdfd73 help sign in
elk/src/Elm/Compiler/Type.hs
248 lines 5.3 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
4 ( Type(..)
5 , Format(..)
6 , toString
7 , DebugMetadata(..)
8 , Alias(..)
9 , Union(..)
10 , encode
11 , decoder
12 , encodeMetadata
13 )
14 where
17import qualified Data.Text as Text
18import qualified Data.Text.Encoding as Text
19import qualified Text.PrettyPrint as P
20import Text.PrettyPrint ((<+>))
22import qualified AST.Source as Src
23import qualified Elm.Name as N
24import qualified Parse.Primitives as Parse
25import qualified Parse.Type as Type
26import qualified Reporting.Annotation as A
27import qualified Json.Decode as Decode
28import qualified Json.Encode as Encode
29import Json.Encode ((==>))
33-- TYPES
36data Type
37 = Lambda Type Type
38 | Var N.Name
39 | Type N.Name [Type]
40 | Record [(N.Name, Type)] (Maybe N.Name)
41 | Unit
42 | Tuple Type Type [Type]
45data DebugMetadata =
46 DebugMetadata
47 { _message :: Type
48 , _aliases :: [Alias]
49 , _unions :: [Union]
50 }
53data Alias = Alias N.Name [N.Name] Type
54data Union = Union N.Name [N.Name] [(N.Name, [Type])]
58-- TO STRING
61data Format = OneLine | MultiLine
64toString :: Format -> Type -> String
65toString format tipe =
66 let
67 mode =
68 case format of
69 OneLine ->
70 P.OneLineMode
71 MultiLine ->
72 P.PageMode
73 in
74 P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
77data Context = None | InType | InFunction
80toDoc :: Context -> Type -> P.Doc
81toDoc context tipe =
82 case tipe of
83 Lambda _ _ ->
84 let t:ts =
85 map (toDoc InFunction) (collectLambdas tipe)
87 lambda =
88 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
89 in
90 case context of
91 None -> lambda
92 _ -> P.parens lambda
94 Var name ->
95 P.text (N.toString name)
97 Unit ->
98 "()"
100 Tuple a b cs ->
101 P.sep
102 [ P.cat $
103 [ "(" <+> toDoc None a
104 , "," <+> toDoc None b
105 ]
106 ++ map ("," <+>) (map (toDoc None) cs)
107 , ")"
108 ]
110 Type name args ->
111 case args of
112 [] ->
113 P.text (N.toString name)
115 _ ->
116 let
117 application =
118 P.hang
119 (P.text (N.toString name))
120 2
121 (P.sep $ map (toDoc InType) args)
122 in
123 case context of
124 InType ->
125 P.parens application
127 _ ->
128 application
130 Record [] Nothing ->
131 "{}"
133 Record fields ext ->
134 case ext of
135 Nothing ->
136 P.sep
137 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map entryToDoc fields))
138 , "}"
139 ]
141 Just x ->
142 P.hang
143 ("{" <+> P.text (N.toString x) <+> "|")
144 4
145 (P.sep
146 [ P.sep (P.punctuate "," (map entryToDoc fields))
147 , "}"
148 ]
149 )
152entryToDoc :: (N.Name, Type) -> P.Doc
153entryToDoc (field, fieldType) =
154 P.text (N.toString field) <+> ":" <+> toDoc None fieldType
157collectLambdas :: Type -> [Type]
158collectLambdas tipe =
159 case tipe of
160 Lambda arg body -> arg : collectLambdas body
161 _ -> [tipe]
165-- JSON for TYPE
168encode :: Type -> Encode.Value
169encode tipe =
170 Encode.text (Text.pack (toString OneLine tipe))
173decoder :: Decode.Decoder () Type
174decoder =
175 do txt <- Decode.text
176 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
177 Left _ ->
178 Decode.fail ()
180 Right (tipe, _, _) ->
181 Decode.succeed (fromRawType tipe)
184fromRawType :: Src.Type -> Type
185fromRawType (A.At _ astType) =
186 case astType of
187 Src.TLambda t1 t2 ->
188 Lambda (fromRawType t1) (fromRawType t2)
190 Src.TVar x ->
191 Var x
193 Src.TUnit ->
194 Unit
196 Src.TTuple a b cs ->
197 Tuple
198 (fromRawType a)
199 (fromRawType b)
200 (map fromRawType cs)
202 Src.TType _ name args ->
203 Type name (map fromRawType args)
205 Src.TTypeQual _ _ name args ->
206 Type name (map fromRawType args)
208 Src.TRecord fields ext ->
209 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
210 Record
211 (map fromField fields)
212 (fmap A.toValue ext)
216-- JSON for PROGRAM
219encodeMetadata :: DebugMetadata -> Encode.Value
220encodeMetadata (DebugMetadata msg aliases unions) =
221 Encode.object
222 [ "message" ==> encode msg
223 , "aliases" ==> Encode.object (map toAliasField aliases)
224 , "unions" ==> Encode.object (map toUnionField unions)
225 ]
228toAliasField :: Alias -> ( Text.Text, Encode.Value )
229toAliasField (Alias name args tipe) =
230 N.toText name ==>
231 Encode.object
232 [ "args" ==> Encode.list Encode.name args
233 , "type" ==> encode tipe
234 ]
237toUnionField :: Union -> ( Text.Text, Encode.Value )
238toUnionField (Union name args constructors) =
239 N.toText name ==>
240 Encode.object
241 [ "args" ==> Encode.list Encode.name args
242 , "tags" ==> Encode.object (map toCtorObject constructors)
243 ]
246toCtorObject :: (N.Name, [Type]) -> ( Text.Text, Encode.Value )
247toCtorObject (name, args) =
248 N.toText name ==> Encode.list encode args