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 qualified Reporting.Render.Type as RT
27import qualified Reporting.Render.Type.Localizer as L
29import qualified Json.Decode as Decode
30import qualified Json.Encode as Encode
31import Json.Encode ((==>))
42 | Record [(N.Name, Type)] (Maybe N.Name)
44 | Tuple Type Type [Type]
55data Alias = Alias N.Name [N.Name] Type
56data Union = Union N.Name [N.Name] [(N.Name, [Type])]
63toDoc :: L.Localizer -> RT.Context -> Type -> D.Doc
64toDoc localizer context tipe =
69 map (toDoc localizer RT.Func) (collectLambdas tipe)
71 RT.lambda context a b cs
81 (toDoc localizer RT.None a)
82 (toDoc localizer RT.None b)
83 (map (toDoc localizer RT.None) cs)
89 (map (toDoc localizer RT.App) args)
93 (map (entryToDoc localizer) fields)
97entryToDoc :: L.Localizer -> (N.Name, Type) -> (D.Doc, D.Doc)
98entryToDoc localizer (field, fieldType) =
99 ( D.fromName field, toDoc localizer RT.None fieldType )
102collectLambdas :: Type -> [Type]
106 arg : collectLambdas body
116encode :: Type -> Encode.Value
118 Encode.text $ Text.pack $ D.toLine (toDoc L.empty RT.None tipe)
121decoder :: Decode.Decoder () Type
123 do txt <- Decode.text
124 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
128 Right (tipe, _, _) ->
129 Decode.succeed (fromRawType tipe)
132fromRawType :: Src.Type -> Type
133fromRawType (A.At _ astType) =
136 Lambda (fromRawType t1) (fromRawType t2)
150 Src.TType _ name args ->
151 Type name (map fromRawType args)
153 Src.TTypeQual _ _ name args ->
154 Type name (map fromRawType args)
156 Src.TRecord fields ext ->
157 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
159 (map fromField fields)
167encodeMetadata :: DebugMetadata -> Encode.Value
168encodeMetadata (DebugMetadata msg aliases unions) =
170 [ "message" ==> encode msg
171 , "aliases" ==> Encode.object (map toAliasField aliases)
172 , "unions" ==> Encode.object (map toUnionField unions)
176toAliasField :: Alias -> ( Text.Text, Encode.Value )
177toAliasField (Alias name args tipe) =
180 [ "args" ==> Encode.list Encode.name args
181 , "type" ==> encode tipe
185toUnionField :: Union -> ( Text.Text, Encode.Value )
186toUnionField (Union name args constructors) =
189 [ "args" ==> Encode.list Encode.name args
190 , "tags" ==> Encode.object (map toCtorObject constructors)
194toCtorObject :: (N.Name, [Type]) -> ( Text.Text, Encode.Value )
195toCtorObject (name, args) =
196 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