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 Format = OneLine | MultiLine
64toString :: Format -> Type -> String
68 P.displayS (P.renderPretty 1.0 maxBound (toDoc None tipe)) ""
71 P.displayS (P.renderPretty 1.0 80 (toDoc None tipe)) ""
74data Context = None | InType | InFunction
77toDoc :: Context -> Type -> P.Doc
82 map (toDoc InFunction) (collectLambdas tipe)
85 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
92 P.text (N.toString name)
100 [ "(" <+> toDoc None a
101 , "," <+> toDoc None b
103 ++ map ("," <+>) (map (toDoc None) cs)
110 P.text (N.toString name)
115 P.text (N.toString name)
118 P.hang 2 $ P.sep (docName : map (toDoc InType) args)
134 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map entryToDoc fields))
141 [ "{" <+> P.text (N.toString x) <+> "|"
142 , P.sep (P.punctuate "," (map entryToDoc fields))
147entryToDoc :: (N.Name, Type) -> P.Doc
148entryToDoc (field, fieldType) =
149 P.text (N.toString field) <+> ":" <+> toDoc None fieldType
152collectLambdas :: Type -> [Type]
155 Lambda arg body -> arg : collectLambdas body
163encode :: Type -> Encode.Value
165 Encode.text (Text.pack (toString OneLine tipe))
168decoder :: Decode.Decoder () Type
170 do txt <- Decode.text
171 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
175 Right (tipe, _, _) ->
176 Decode.succeed (fromRawType tipe)
179fromRawType :: Src.Type -> Type
180fromRawType (A.At _ astType) =
183 Lambda (fromRawType t1) (fromRawType t2)
197 Src.TType _ name args ->
198 Type name (map fromRawType args)
200 Src.TTypeQual _ _ name args ->
201 Type name (map fromRawType args)
203 Src.TRecord fields ext ->
204 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
206 (map fromField fields)
214encodeMetadata :: DebugMetadata -> Encode.Value
215encodeMetadata (DebugMetadata msg aliases unions) =
217 [ "message" ==> encode msg
218 , "aliases" ==> Encode.object (map toAliasField aliases)
219 , "unions" ==> Encode.object (map toUnionField unions)
223toAliasField :: Alias -> ( Text.Text, Encode.Value )
224toAliasField (Alias name args tipe) =
227 [ "args" ==> Encode.list Encode.name args
228 , "type" ==> encode tipe
232toUnionField :: Union -> ( Text.Text, Encode.Value )
233toUnionField (Union name args constructors) =
236 [ "args" ==> Encode.list Encode.name args
237 , "tags" ==> Encode.object (map toCtorObject constructors)
241toCtorObject :: (N.Name, [Type]) -> ( Text.Text, Encode.Value )
242toCtorObject (name, args) =
243 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