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 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 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)
137 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map entryToDoc fields))
143 ("{" <+> P.text (N.toString x) <+> "|")
146 [ P.sep (P.punctuate "," (map entryToDoc fields))
152entryToDoc :: (N.Name, Type) -> P.Doc
153entryToDoc (field, fieldType) =
154 P.text (N.toString field) <+> ":" <+> toDoc None fieldType
157collectLambdas :: Type -> [Type]
160 Lambda arg body -> arg : collectLambdas body
168encode :: Type -> Encode.Value
170 Encode.text (Text.pack (toString OneLine tipe))
173decoder :: Decode.Decoder () Type
175 do txt <- Decode.text
176 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
180 Right (tipe, _, _) ->
181 Decode.succeed (fromRawType tipe)
184fromRawType :: Src.Type -> Type
185fromRawType (A.At _ astType) =
188 Lambda (fromRawType t1) (fromRawType t2)
202 Src.TType _ name args ->
203 Type name (map fromRawType args)
205 Src.TTypeQual _ _ name args ->
206 Type name (map fromRawType args)
208 Src.TRecord fields ext ->
209 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
211 (map fromField fields)
219encodeMetadata :: DebugMetadata -> Encode.Value
220encodeMetadata (DebugMetadata msg aliases unions) =
222 [ "message" ==> encode msg
223 , "aliases" ==> Encode.object (map toAliasField aliases)
224 , "unions" ==> Encode.object (map toUnionField unions)
228toAliasField :: Alias -> ( Text.Text, Encode.Value )
229toAliasField (Alias name args tipe) =
232 [ "args" ==> Encode.list Encode.name args
233 , "type" ==> encode tipe
237toUnionField :: Union -> ( Text.Text, Encode.Value )
238toUnionField (Union name args constructors) =
241 [ "args" ==> Encode.list Encode.name args
242 , "tags" ==> Encode.object (map toCtorObject constructors)
246toCtorObject :: (N.Name, [Type]) -> ( Text.Text, Encode.Value )
247toCtorObject (name, args) =
248 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