1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
18import Data.Text (Text)
19import qualified Data.Text as Text
20import qualified Data.Text.Encoding as Text
21import qualified Text.PrettyPrint as P
22import Text.PrettyPrint ((<+>))
24import qualified AST.Source as Src
25import qualified Elm.Name as N
26import qualified Parse.Primitives as Parse
27import qualified Parse.Type as Type
28import qualified Reporting.Annotation as A
29import qualified Json.Decode as Decode
30import qualified Json.Encode as Encode
31import Json.Encode ((==>))
42 | Record [(N.Name, Type)] (Maybe Type)
44 | Tuple Type Type [Type]
55data Alias = Alias N.Name [N.Name] Type
56data Union = Union N.Name [N.Name] [(N.Name, [Type])]
63data Format = OneLine | MultiLine
66toString :: Format -> Type -> String
76 P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
79data Context = None | InType | InFunction
82toDoc :: Context -> Type -> P.Doc
87 map (toDoc InFunction) (collectLambdas tipe)
90 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
97 P.text (Text.unpack name)
105 [ "(" <+> toDoc None a
106 , "," <+> toDoc None b
108 ++ map ("," <+>) (map (toDoc None) cs)
115 P.text (Text.unpack name)
121 (P.text (Text.unpack name))
123 (P.sep $ map (toDoc InType) args)
133 case flattenRecord tipe of
139 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map prettyField fields))
145 ("{" <+> P.text (Text.unpack x) <+> "|")
148 [ P.sep (P.punctuate "," (map prettyField fields))
153 prettyField (field, fieldType) =
154 P.text (Text.unpack field) <+> ":" <+> toDoc None fieldType
157collectLambdas :: Type -> [Type]
160 Lambda arg body -> arg : collectLambdas body
164flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
170 Record fields Nothing ->
173 Record fields (Just ext) ->
174 let (fields',ext') = flattenRecord ext
176 (fields' ++ fields, ext')
179 error "Trying to flatten ill-formed record."
186encode :: Type -> Encode.Value
188 Encode.text (Text.pack (toString OneLine tipe))
194decoder :: Decode.Decoder Error Type
196 do txt <- Decode.text
197 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
201 Right (tipe, _, _) ->
202 Decode.succeed (fromRawType tipe)
205fromRawType :: Src.Type -> Type
206fromRawType (A.At _ astType) =
209 Lambda (fromRawType t1) (fromRawType t2)
223 Src.TType _ name args ->
224 Type name (map fromRawType args)
226 Src.TTypeQual _ _ name args ->
227 Type name (map fromRawType args)
229 Src.TRecord fields ext ->
230 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
232 (map fromField fields)
233 (fmap fromRawType ext)
240encodeMetadata :: DebugMetadata -> Encode.Value
241encodeMetadata (DebugMetadata msg aliases unions) =
243 [ "message" ==> encode msg
244 , "aliases" ==> Encode.object (map toAliasField aliases)
245 , "unions" ==> Encode.object (map toUnionField unions)
249toAliasField :: Alias -> ( String, Encode.Value )
250toAliasField (Alias name args tipe) =
253 [ "args" ==> Encode.list Encode.text args
254 , "type" ==> encode tipe
258toUnionField :: Union -> ( String, Encode.Value )
259toUnionField (Union name args constructors) =
262 [ "args" ==> Encode.list Encode.text args
263 , "tags" ==> Encode.object (map toCtorObject constructors)
267toCtorObject :: (Text, [Type]) -> ( String, Encode.Value )
268toCtorObject (name, args) =
269 Text.unpack name ==> Encode.list encode args
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list