1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
17import qualified Data.Name as Name
18import qualified Data.Utf8 as Utf8
20import qualified AST.Source as Src
21import qualified Parse.Primitives as P
22import qualified Parse.Type as Type
23import qualified Reporting.Annotation as A
24import qualified Reporting.Doc as D
25import qualified Reporting.Render.Type as RT
26import qualified Reporting.Render.Type.Localizer as L
28import qualified Json.Decode as D
29import qualified Json.Encode as E
30import Json.Encode ((==>))
40 | Type Name.Name [Type]
41 | Record [(Name.Name, Type)] (Maybe Name.Name)
43 | Tuple Type Type [Type]
54data Alias = Alias Name.Name [Name.Name] Type
55data Union = Union Name.Name [Name.Name] [(Name.Name, [Type])]
62toDoc :: L.Localizer -> RT.Context -> Type -> D.Doc
63toDoc localizer context tipe =
68 map (toDoc localizer RT.Func) (collectLambdas tipe)
70 RT.lambda context a b cs
80 (toDoc localizer RT.None a)
81 (toDoc localizer RT.None b)
82 (map (toDoc localizer RT.None) cs)
88 (map (toDoc localizer RT.App) args)
92 (map (entryToDoc localizer) fields)
96entryToDoc :: L.Localizer -> (Name.Name, Type) -> (D.Doc, D.Doc)
97entryToDoc localizer (field, fieldType) =
98 ( D.fromName field, toDoc localizer RT.None fieldType )
101collectLambdas :: Type -> [Type]
105 arg : collectLambdas body
115encode :: Type -> E.Value
117 E.string $ Utf8.fromChars $ D.toLine (toDoc L.empty RT.None tipe)
120decoder :: D.Decoder () Type
123 case P.fromByteString Type.expression (error "TODO turn Utf8 to something that can be parsed" str) of
124 P.Ok (tipe, _, _) _ ->
125 return (fromRawType tipe)
131fromRawType :: Src.Type -> Type
132fromRawType (A.At _ astType) =
135 Lambda (fromRawType t1) (fromRawType t2)
149 Src.TType _ name args ->
150 Type name (map fromRawType args)
152 Src.TTypeQual _ _ name args ->
153 Type name (map fromRawType args)
155 Src.TRecord fields ext ->
156 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
158 (map fromField fields)
166encodeMetadata :: DebugMetadata -> E.Value
167encodeMetadata (DebugMetadata msg aliases unions) =
169 [ "message" ==> encode msg
170 , "aliases" ==> E.object (map toAliasField aliases)
171 , "unions" ==> E.object (map toUnionField unions)
175toAliasField :: Alias -> ( Utf8.String, E.Value )
176toAliasField (Alias name args tipe) =
179 [ "args" ==> E.list E.name args
180 , "type" ==> encode tipe
184toUnionField :: Union -> ( Utf8.String, E.Value )
185toUnionField (Union name args constructors) =
188 [ "args" ==> E.list E.name args
189 , "tags" ==> E.object (map toCtorObject constructors)
193toCtorObject :: (Name.Name, [Type]) -> ( Utf8.String, E.Value )
194toCtorObject (name, args) =
195 Name.toUtf8 name ==> E.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