1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
17import qualified Data.Text as Text
18import qualified Data.Text.Encoding as Text
19import qualified Text.PrettyPrint as P
20import Text.PrettyPrint ((<+>))
22import qualified AST.Source as Src
23import qualified Elm.Name as N
24import qualified Parse.Primitives as Parse
25import qualified Parse.Type as Type
26import qualified Reporting.Annotation as A
27import qualified Json.Decode as Decode
28import qualified Json.Encode as Encode
29import Json.Encode ((==>))
40 | Record [(N.Name, Type)] (Maybe Type)
42 | Tuple Type Type [Type]
53data Alias = Alias N.Name [N.Name] Type
54data Union = Union N.Name [N.Name] [(N.Name, [Type])]
61data Format = OneLine | MultiLine
64toString :: Format -> Type -> String
74 P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
77data Context = None | InType | InFunction
80toDoc :: Context -> Type -> P.Doc
85 map (toDoc InFunction) (collectLambdas tipe)
88 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
95 P.text (N.toString name)
103 [ "(" <+> toDoc None a
104 , "," <+> toDoc None b
106 ++ map ("," <+>) (map (toDoc None) cs)
113 P.text (N.toString name)
119 (P.text (N.toString name))
121 (P.sep $ map (toDoc InType) args)
131 case flattenRecord tipe of
137 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map prettyField fields))
143 ("{" <+> P.text (N.toString x) <+> "|")
146 [ P.sep (P.punctuate "," (map prettyField fields))
151 prettyField (field, fieldType) =
152 P.text (N.toString field) <+> ":" <+> toDoc None fieldType
155collectLambdas :: Type -> [Type]
158 Lambda arg body -> arg : collectLambdas body
162flattenRecord :: Type -> ( [(N.Name, Type)], Maybe N.Name )
168 Record fields Nothing ->
171 Record fields (Just ext) ->
172 let (fields',ext') = flattenRecord ext
174 (fields' ++ fields, ext')
177 error "Trying to flatten ill-formed record."
184encode :: Type -> Encode.Value
186 Encode.text (Text.pack (toString OneLine tipe))
189decoder :: Decode.Decoder () Type
191 do txt <- Decode.text
192 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
196 Right (tipe, _, _) ->
197 Decode.succeed (fromRawType tipe)
200fromRawType :: Src.Type -> Type
201fromRawType (A.At _ astType) =
204 Lambda (fromRawType t1) (fromRawType t2)
218 Src.TType _ name args ->
219 Type name (map fromRawType args)
221 Src.TTypeQual _ _ name args ->
222 Type name (map fromRawType args)
224 Src.TRecord fields ext ->
225 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
227 (map fromField fields)
228 (fmap fromRawType ext)
235encodeMetadata :: DebugMetadata -> Encode.Value
236encodeMetadata (DebugMetadata msg aliases unions) =
238 [ "message" ==> encode msg
239 , "aliases" ==> Encode.object (map toAliasField aliases)
240 , "unions" ==> Encode.object (map toUnionField unions)
244toAliasField :: Alias -> ( Text.Text, Encode.Value )
245toAliasField (Alias name args tipe) =
248 [ "args" ==> Encode.list Encode.name args
249 , "type" ==> encode tipe
253toUnionField :: Union -> ( Text.Text, Encode.Value )
254toUnionField (Union name args constructors) =
257 [ "args" ==> Encode.list Encode.name args
258 , "tags" ==> Encode.object (map toCtorObject constructors)
262toCtorObject :: (N.Name, [Type]) -> ( Text.Text, Encode.Value )
263toCtorObject (name, args) =
264 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