1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
17import qualified Data.Name as Name
19import qualified AST.Source as Src
20import qualified Json.Decode as D
21import qualified Json.Encode as E
22import Json.Encode ((==>))
23import qualified Json.String as Json
24import qualified Parse.Primitives as P
25import qualified Parse.Type as Type
26import qualified Reporting.Annotation as A
27import qualified Reporting.Doc as D
28import qualified Reporting.Render.Type as RT
29import qualified Reporting.Render.Type.Localizer as L
39 | Type Name.Name [Type]
40 | Record [(Name.Name, Type)] (Maybe Name.Name)
42 | Tuple Type Type [Type]
53data Alias = Alias Name.Name [Name.Name] Type
54data Union = Union Name.Name [Name.Name] [(Name.Name, [Type])]
61toDoc :: L.Localizer -> RT.Context -> Type -> D.Doc
62toDoc localizer context tipe =
67 map (toDoc localizer RT.Func) (collectLambdas tipe)
69 RT.lambda context a b cs
79 (toDoc localizer RT.None a)
80 (toDoc localizer RT.None b)
81 (map (toDoc localizer RT.None) cs)
87 (map (toDoc localizer RT.App) args)
91 (map (entryToDoc localizer) fields)
95entryToDoc :: L.Localizer -> (Name.Name, Type) -> (D.Doc, D.Doc)
96entryToDoc localizer (field, fieldType) =
97 ( D.fromName field, toDoc localizer RT.None fieldType )
100collectLambdas :: Type -> [Type]
104 arg : collectLambdas body
114encode :: Type -> E.Value
116 E.chars $ D.toLine (toDoc L.empty RT.None tipe)
119decoder :: D.Decoder () Type
123 P.specialize (\_ _ _ -> ()) (fromRawType . fst <$> Type.expression)
125 D.customString parser (\_ _ -> ())
128fromRawType :: Src.Type -> Type
129fromRawType (A.At _ astType) =
132 Lambda (fromRawType t1) (fromRawType t2)
146 Src.TType _ name args ->
147 Type name (map fromRawType args)
149 Src.TTypeQual _ _ name args ->
150 Type name (map fromRawType args)
152 Src.TRecord fields ext ->
153 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
155 (map fromField fields)
163encodeMetadata :: DebugMetadata -> E.Value
164encodeMetadata (DebugMetadata msg aliases unions) =
166 [ "message" ==> encode msg
167 , "aliases" ==> E.object (map toTypeAliasField aliases)
168 , "unions" ==> E.object (map toCustomTypeField unions)
172toTypeAliasField :: Alias -> ( Json.String, E.Value )
173toTypeAliasField (Alias name args tipe) =
176 [ "args" ==> E.list E.name args
177 , "type" ==> encode tipe
182toCustomTypeField :: Union -> ( Json.String, E.Value )
183toCustomTypeField (Union name args constructors) =
186 [ "args" ==> E.list E.name args
187 , "tags" ==> E.object (map toVariantObject constructors)
192toVariantObject :: (Name.Name, [Type]) -> ( Json.String, E.Value )
193toVariantObject (name, args) =
194 ( Json.fromName 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