1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
14import Control.Arrow ((***))
15import qualified Data.Text as Text
16import Data.Text (Text)
17import qualified Text.PrettyPrint as P
18import Text.PrettyPrint ((<+>))
20import qualified AST.Helpers as Help
21import qualified AST.Type as Type
22import qualified AST.Variable as Var
23import qualified Parse.Helpers as Parse
24import qualified Parse.Type as Type
25import qualified Reporting.Annotation as A
26import qualified Json.Decode as Decode
27import qualified Json.Encode as Encode
28import Json.Encode ((==>))
39 | Record [(Text, Type)] (Maybe Type)
45 , _aliases :: [( Text, [Text], Type )]
46 , _unions :: [( Text, [Text], [(Text, [Type])] )]
54data Format = OneLine | MultiLine
57toString :: Format -> Type -> String
67 P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
70data Context = None | InType | InFunction
73toDoc :: Context -> Type -> P.Doc
78 map (toDoc InFunction) (collectLambdas tipe)
81 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
88 P.text (Text.unpack name)
91 P.text (if name == Help.zeroTuple then "()" else Text.unpack name)
93 Type name arguments@(arg:args) ->
94 if Help.isTuple name then
96 [ P.cat ("(" <+> toDoc None arg : map (\a -> "," <+> toDoc None a) args)
104 (P.text (Text.unpack name))
106 (P.sep $ map (toDoc InType) arguments)
116 case flattenRecord tipe of
122 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map prettyField fields))
128 ("{" <+> P.text (Text.unpack x) <+> "|")
131 [ P.sep (P.punctuate "," (map prettyField fields))
136 prettyField (field, fieldType) =
137 P.text (Text.unpack field) <+> ":" <+> toDoc None fieldType
140collectLambdas :: Type -> [Type]
143 Lambda arg body -> arg : collectLambdas body
147flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
153 Record fields Nothing ->
156 Record fields (Just ext) ->
157 let (fields',ext') = flattenRecord ext
159 (fields' ++ fields, ext')
162 error "Trying to flatten ill-formed record."
169encode :: Type -> Encode.Value
171 Encode.text (Text.pack (toString OneLine tipe))
174decoder :: Decode.Decoder Type
176 do txt <- Decode.text
177 case Parse.run Type.expression txt of
179 Decode.fail "a type, like (String -> Int)"
181 Right (tipe, _, _) ->
182 Decode.succeed (fromRawType tipe)
185fromRawType :: Type.Raw -> Type
186fromRawType (A.A _ astType) =
188 Type.RLambda t1 t2 ->
189 Lambda (fromRawType t1) (fromRawType t2)
194 Type.RType (A.A _ name) args ->
195 Type (Var.rawToText name) (map fromRawType args)
197 Type.RRecord fields ext ->
198 Record (map (A.drop *** fromRawType) fields) (fmap fromRawType ext)
205encodeProgram :: Program -> Encode.Value
206encodeProgram (Program msg aliases unions) =
208 [ "message" ==> encode msg
209 , "aliases" ==> Encode.object (map toAliasField aliases)
210 , "unions" ==> Encode.object (map toUnionField unions)
214toAliasField :: ( Text, [Text], Type ) -> ( String, Encode.Value )
215toAliasField ( name, args, tipe ) =
218 [ "args" ==> Encode.list Encode.text args
219 , "type" ==> encode tipe
223toUnionField :: ( Text, [Text], [(Text, [Type])] ) -> ( String, Encode.Value )
224toUnionField ( name, args, constructors ) =
227 [ "args" ==> Encode.list Encode.text args
228 , "tags" ==> Encode.object (map toCtorObject constructors)
232toCtorObject :: (Text, [Type]) -> ( String, Encode.Value )
233toCtorObject (name, args) =
234 Text.unpack 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