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 Text.PrettyPrint as P
19import qualified AST.Helpers as Help
20import qualified AST.Type as Type
21import qualified AST.Variable as Var
22import qualified Parse.Helpers as Parse
23import qualified Parse.Type as Type
24import qualified Reporting.Annotation as A
25import qualified Json.Decode as Decode
26import qualified Json.Encode as Encode
27import 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 (Text.unpack (if name == "_Tuple0" then "()" else name))
93 App (Type name) args ->
94 if name == "_Tuple0" then
97 else if Help.isTuple name then
99 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
107 (P.text (Text.unpack name))
109 (P.sep $ map (toDoc InType) args)
111 case (context, args) of
120 "Somehow ended up with an unexpected type, please post a minimal example that\n"
121 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
124 case flattenRecord tipe of
130 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
136 (P.lbrace <+> P.text (Text.unpack x) <+> P.text "|")
139 [ P.sep (P.punctuate P.comma (map prettyField fields))
144 prettyField (field, fieldType) =
145 P.text (Text.unpack field) <+> P.text ":" <+> toDoc None fieldType
148collectLambdas :: Type -> [Type]
151 Lambda arg body -> arg : collectLambdas body
155flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
161 Record fields Nothing ->
164 Record fields (Just ext) ->
165 let (fields',ext') = flattenRecord ext
167 (fields' ++ fields, ext')
170 error "Trying to flatten ill-formed record."
177encode :: Type -> Encode.Value
179 Encode.text (Text.pack (toString OneLine tipe))
182decoder :: Decode.Decoder Type
184 do txt <- Decode.text
185 case Parse.run Type.expression txt of
187 Decode.fail "a type, like (String -> Int)"
189 Right (tipe, _, _) ->
190 Decode.succeed (fromRawType tipe)
193fromRawType :: Type.Raw -> Type
194fromRawType (A.A _ astType) =
196 Type.RLambda t1 t2 ->
197 Lambda (fromRawType t1) (fromRawType t2)
203 Type (Var.rawToText var)
206 App (fromRawType t) (map fromRawType ts)
208 Type.RRecord fields ext ->
209 Record (map (A.drop *** fromRawType) fields) (fmap fromRawType ext)
216encodeProgram :: Program -> Encode.Value
217encodeProgram (Program msg aliases unions) =
219 [ "message" ==> encode msg
220 , "aliases" ==> Encode.object (map toAliasField aliases)
221 , "unions" ==> Encode.object (map toUnionField unions)
225toAliasField :: ( Text, [Text], Type ) -> ( String, Encode.Value )
226toAliasField ( name, args, tipe ) =
229 [ "args" ==> Encode.list Encode.text args
230 , "type" ==> encode tipe
234toUnionField :: ( Text, [Text], [(Text, [Type])] ) -> ( String, Encode.Value )
235toUnionField ( name, args, constructors ) =
238 [ "args" ==> Encode.list Encode.text args
239 , "tags" ==> Encode.object (map toCtorObject constructors)
243toCtorObject :: (Text, [Type]) -> ( String, Encode.Value )
244toCtorObject (name, args) =
245 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