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 ((==>))
40 | Record [(Text, Type)] (Maybe Type)
46 , _aliases :: [( Text, [Text], Type )]
47 , _unions :: [( Text, [Text], [(Text, [Type])] )]
55data Format = OneLine | MultiLine
58toString :: Format -> Type -> String
68 P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
71data Context = None | InType | InFunction
74toDoc :: Context -> Type -> P.Doc
79 map (toDoc InFunction) (collectLambdas tipe)
82 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
89 P.text (Text.unpack name)
92 P.text (Text.unpack (if name == Help.zeroTuple then "()" else name))
94 App (Type name) args ->
95 if name == Help.zeroTuple then
98 else if Help.isTuple name then
100 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
108 (P.text (Text.unpack name))
110 (P.sep $ map (toDoc InType) args)
112 case (context, args) of
121 "Somehow ended up with an unexpected type, please post a minimal example that\n"
122 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
125 case flattenRecord tipe of
131 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
137 (P.lbrace <+> P.text (Text.unpack x) <+> P.text "|")
140 [ P.sep (P.punctuate P.comma (map prettyField fields))
145 prettyField (field, fieldType) =
146 P.text (Text.unpack field) <+> P.text ":" <+> toDoc None fieldType
149collectLambdas :: Type -> [Type]
152 Lambda arg body -> arg : collectLambdas body
156flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
162 Record fields Nothing ->
165 Record fields (Just ext) ->
166 let (fields',ext') = flattenRecord ext
168 (fields' ++ fields, ext')
171 error "Trying to flatten ill-formed record."
178encode :: Type -> Encode.Value
180 Encode.text (Text.pack (toString OneLine tipe))
183decoder :: Decode.Decoder Type
185 do txt <- Decode.text
186 case Parse.run Type.expression txt of
188 Decode.fail "a type, like (String -> Int)"
190 Right (tipe, _, _) ->
191 Decode.succeed (fromRawType tipe)
194fromRawType :: Type.Raw -> Type
195fromRawType (A.A _ astType) =
197 Type.RLambda t1 t2 ->
198 Lambda (fromRawType t1) (fromRawType t2)
204 Type (Var.rawToText var)
207 App (fromRawType t) (map fromRawType ts)
209 Type.RRecord fields ext ->
210 Record (map (A.drop *** fromRawType) fields) (fmap fromRawType ext)
217encodeProgram :: Program -> Encode.Value
218encodeProgram (Program msg aliases unions) =
220 [ "message" ==> encode msg
221 , "aliases" ==> Encode.object (map toAliasField aliases)
222 , "unions" ==> Encode.object (map toUnionField unions)
226toAliasField :: ( Text, [Text], Type ) -> ( String, Encode.Value )
227toAliasField ( name, args, tipe ) =
230 [ "args" ==> Encode.list Encode.text args
231 , "type" ==> encode tipe
235toUnionField :: ( Text, [Text], [(Text, [Type])] ) -> ( String, Encode.Value )
236toUnionField ( name, args, constructors ) =
239 [ "args" ==> Encode.list Encode.text args
240 , "tags" ==> Encode.object (map toCtorObject constructors)
244toCtorObject :: (Text, [Type]) -> ( String, Encode.Value )
245toCtorObject (name, args) =
246 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