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.ANSI.Leijen as P
20import Text.PrettyPrint.ANSI.Leijen ((<+>))
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 Context = None | InType | InFunction
64toDoc :: Context -> Type -> P.Doc
69 map (toDoc InFunction) (collectLambdas tipe)
72 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
79 P.text (N.toString name)
87 [ "(" <+> toDoc None a
88 , "," <+> toDoc None b
90 ++ map ("," <+>) (map (toDoc None) cs)
97 P.text (N.toString name)
102 P.text (N.toString name)
105 P.hang 2 $ P.sep (docName : map (toDoc InType) args)
121 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map entryToDoc fields))
128 [ "{" <+> P.text (N.toString x) <+> "|"
129 , P.sep (P.punctuate "," (map entryToDoc fields))
134entryToDoc :: (N.Name, Type) -> P.Doc
135entryToDoc (field, fieldType) =
136 P.text (N.toString field) <+> ":" <+> toDoc None fieldType
139collectLambdas :: Type -> [Type]
142 Lambda arg body -> arg : collectLambdas body
150encode :: Type -> Encode.Value
152 Encode.text $ Text.pack $
153 P.displayS (P.renderPretty 1.0 (maxBound `div` 2) (toDoc None tipe)) ""
156decoder :: Decode.Decoder () Type
158 do txt <- Decode.text
159 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
163 Right (tipe, _, _) ->
164 Decode.succeed (fromRawType tipe)
167fromRawType :: Src.Type -> Type
168fromRawType (A.At _ astType) =
171 Lambda (fromRawType t1) (fromRawType t2)
185 Src.TType _ name args ->
186 Type name (map fromRawType args)
188 Src.TTypeQual _ _ name args ->
189 Type name (map fromRawType args)
191 Src.TRecord fields ext ->
192 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
194 (map fromField fields)
202encodeMetadata :: DebugMetadata -> Encode.Value
203encodeMetadata (DebugMetadata msg aliases unions) =
205 [ "message" ==> encode msg
206 , "aliases" ==> Encode.object (map toAliasField aliases)
207 , "unions" ==> Encode.object (map toUnionField unions)
211toAliasField :: Alias -> ( Text.Text, Encode.Value )
212toAliasField (Alias name args tipe) =
215 [ "args" ==> Encode.list Encode.name args
216 , "type" ==> encode tipe
220toUnionField :: Union -> ( Text.Text, Encode.Value )
221toUnionField (Union name args constructors) =
224 [ "args" ==> Encode.list Encode.name args
225 , "tags" ==> Encode.object (map toCtorObject constructors)
229toCtorObject :: (N.Name, [Type]) -> ( Text.Text, Encode.Value )
230toCtorObject (name, args) =
231 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