1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
17import qualified Data.Text as Text
18import qualified Data.Text.Encoding as Text
20import qualified AST.Source as Src
21import qualified Elm.Name as N
22import qualified Parse.Primitives as Parse
23import qualified Parse.Type as Type
24import qualified Reporting.Annotation as A
25import qualified Reporting.Doc as D
26import Reporting.Doc ((<>), (<+>))
27import qualified Json.Decode as Decode
28import qualified Json.Encode as Encode
29import Json.Encode ((==>))
40 | Record [(N.Name, Type)] (Maybe N.Name)
42 | Tuple Type Type [Type]
53data Alias = Alias N.Name [N.Name] Type
54data Union = Union N.Name [N.Name] [(N.Name, [Type])]
61data Context = None | InType | InFunction
64toDoc :: Context -> Type -> D.Doc
69 map (toDoc InFunction) (collectLambdas tipe)
72 D.sep [ t, D.sep (map ("->" <+>) ts) ]
76 _ -> "(" <> lambda <> ")"
87 [ "(" <+> toDoc None a
88 , "," <+> toDoc None b
90 ++ map ("," <+>) (map (toDoc None) cs)
105 D.hang 2 $ D.sep (docName : map (toDoc InType) args)
109 "(" <> application <> ")"
121 [ D.cat (zipWith (<+>) ("{" : repeat ",") (map entryToDoc fields))
128 [ "{" <+> D.fromName x
129 , D.cat (zipWith (<+>) ("|" : repeat ",") (map entryToDoc fields))
135entryToDoc :: (N.Name, Type) -> D.Doc
136entryToDoc (field, fieldType) =
137 D.fromName field <+> ":" <+> toDoc None fieldType
140collectLambdas :: Type -> [Type]
143 Lambda arg body -> arg : collectLambdas body
151encode :: Type -> Encode.Value
153 Encode.text $ Text.pack $ D.toLine (toDoc None tipe)
156decoder :: Decode.Decoder () Type
158 do txt <- Decode.text
159 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
163 Right (tipe, _, _) ->
164 Decode.succeed (fromRawType tipe)
167fromRawType :: Src.Type -> Type
168fromRawType (A.At _ astType) =
171 Lambda (fromRawType t1) (fromRawType t2)
185 Src.TType _ name args ->
186 Type name (map fromRawType args)
188 Src.TTypeQual _ _ name args ->
189 Type name (map fromRawType args)
191 Src.TRecord fields ext ->
192 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
194 (map fromField fields)
202encodeMetadata :: DebugMetadata -> Encode.Value
203encodeMetadata (DebugMetadata msg aliases unions) =
205 [ "message" ==> encode msg
206 , "aliases" ==> Encode.object (map toAliasField aliases)
207 , "unions" ==> Encode.object (map toUnionField unions)
211toAliasField :: Alias -> ( Text.Text, Encode.Value )
212toAliasField (Alias name args tipe) =
215 [ "args" ==> Encode.list Encode.name args
216 , "type" ==> encode tipe
220toUnionField :: Union -> ( Text.Text, Encode.Value )
221toUnionField (Union name args constructors) =
224 [ "args" ==> Encode.list Encode.name args
225 , "tags" ==> Encode.object (map toCtorObject constructors)
229toCtorObject :: (N.Name, [Type]) -> ( Text.Text, Encode.Value )
230toCtorObject (name, args) =
231 N.toText 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