@@ -1,3 +1,4 @@
1+{-# OPTIONS_GHC -Wall #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2module Elm.Compiler.Type
3module Elm.Compiler.Type
3 ( Type(..), toString
4 ( Type(..), toString
@@ -11,6 +12,7 @@ import qualified Data.Aeson as Json
11import qualified Data.Aeson.Types as Json
12import qualified Data.Aeson.Types as Json
12import qualified Data.ByteString.Lazy.Char8 as BS
13import qualified Data.ByteString.Lazy.Char8 as BS
13import qualified Data.Text as Text
14import qualified Data.Text as Text
15+import Data.Text (Text)
14import Text.PrettyPrint as P
16import Text.PrettyPrint as P
16import qualified AST.Helpers as Help
18import qualified AST.Helpers as Help
@@ -24,17 +26,17 @@ import qualified Reporting.Annotation as A
26 = Lambda Type Type
28 = Lambda Type Type
27− | Var String
29+ | Var Text
28− | Type String
30+ | Type Text
29 | App Type [Type]
31 | App Type [Type]
30− | Record [(String, Type)] (Maybe Type)
32+ | Record [(Text, Type)] (Maybe Type)
33data Program =
35data Program =
35 { _message :: Type
37 { _message :: Type
36− , _aliases :: [( String, [String], Type )]
38+ , _aliases :: [( Text, [Text], Type )]
37− , _unions :: [( String, [String], [(String, [Type])] )]
39+ , _unions :: [( Text, [Text], [(Text, [Type])] )]
@@ -65,27 +67,32 @@ toDoc context tipe =
65 _ -> P.parens lambda
67 _ -> P.parens lambda
67 Var name ->
69 Var name ->
68− P.text name
70+ P.text (Text.unpack name)
70 Type name ->
72 Type name ->
71− P.text (if name == "_Tuple0" then "()" else name)
73+ P.text (Text.unpack (if name == "_Tuple0" then "()" else name))
73− App (Type name) args
75+ App (Type name) args ->
74− | name == "_Tuple0" ->
76+ if name == "_Tuple0" then
75− P.text "()"
77+ P.text "()"
77− | Help.isTuple name ->
79+ else if Help.isTuple name then
79− [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
81+ [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
80− , P.rparen
82+ , P.rparen
83− | otherwise ->
85+ else
89+ (P.text (Text.unpack name))
84− let adt = P.hang (P.text name) 2 (P.sep $ map (toDoc ADT) args)
91+ (P.sep $ map (toDoc ADT) args)
86− case (context, args) of
93+ case (context, args) of
87− (ADT, _ : _) -> P.parens adt
94+ (ADT, _ : _) -> P.parens application
88− _ -> adt
95+ _ -> application
90 App _ _ ->
97 App _ _ ->
@@ -105,7 +112,7 @@ toDoc context tipe =
106 (fields, Just x) ->
113 (fields, Just x) ->
108− (P.lbrace <+> P.text x <+> P.text "|")
115+ (P.lbrace <+> P.text (Text.unpack x) <+> P.text "|")
111 [ P.sep (P.punctuate P.comma (map prettyField fields))
118 [ P.sep (P.punctuate P.comma (map prettyField fields))
@@ -113,8 +120,8 @@ toDoc context tipe =
116− prettyField (field, tipe) =
123+ prettyField (field, fieldType) =
117− P.text field <+> P.text ":" <+> toDoc None tipe
124+ P.text (Text.unpack field) <+> P.text ":" <+> toDoc None fieldType
120collectLambdas :: Type -> [Type]
127collectLambdas :: Type -> [Type]
@@ -124,19 +131,22 @@ collectLambdas tipe =
124 _ -> [tipe]
131 _ -> [tipe]
127−flattenRecord :: Type -> ( [(String, Type)], Maybe String )
134+flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
128flattenRecord tipe =
135flattenRecord tipe =
129− case tipe of
136+ case tipe of
130− Var x -> ([], Just x)
138+ ([], Just x)
132− Record fields Nothing -> (fields, Nothing)
140+ Record fields Nothing ->
134− Record fields (Just ext) ->
143+ Record fields (Just ext) ->
135− let (fields',ext') = flattenRecord ext
144+ let (fields',ext') = flattenRecord ext
137− (fields' ++ fields, ext')
146+ (fields' ++ fields, ext')
139− _ -> error "Trying to flatten ill-formed record."
149+ error "Trying to flatten ill-formed record."
@@ -157,9 +167,13 @@ instance Json.FromJSON Type where
157 ++ BS.unpack (Json.encode value)
167 ++ BS.unpack (Json.encode value)
159 case value of
169 case value of
160− Json.String text ->
170+ Json.String txt ->
171+ case Parse.run Type.expression (Text.replace "'" "_" txt) of
175+ Right (tipe, _, _) ->
161− either failure (return . fromRawType) $
176+ return (fromRawType tipe)
162− Parse.parse Type.expr (replacePrimes (Text.unpack text))
164 Json.Object obj ->
178 Json.Object obj ->
165 fromObject obj
179 fromObject obj
@@ -168,11 +182,6 @@ instance Json.FromJSON Type where
168 failure ()
182 failure ()
171−replacePrimes :: String -> String
172−replacePrimes string =
173− map (\c -> if c == '\'' then '_' else c) string
176fromObject :: Json.Object -> Json.Parser Type
185fromObject :: Json.Object -> Json.Parser Type
177fromObject obj =
186fromObject obj =
178 do tag <- obj .: "tag"
187 do tag <- obj .: "tag"
@@ -206,7 +215,7 @@ fromRawType (A.A _ astType) =
208 Type.RType var ->
217 Type.RType var ->
209− Type (Var.toString var)
218+ Type (Var.toText var)
211 Type.RApp t ts ->
220 Type.RApp t ts ->
212 App (fromRawType t) (map fromRawType ts)
221 App (fromRawType t) (map fromRawType ts)
@@ -228,22 +237,22 @@ instance Json.ToJSON Program where
231−toAliasField :: ( String, [String], Type ) -> Json.Pair
240+toAliasField :: ( Text, [Text], Type ) -> Json.Pair
232toAliasField ( name, args, tipe ) =
241toAliasField ( name, args, tipe ) =
233− Text.pack name .= Json.object
242+ name .= Json.object
234 [ "args" .= args
243 [ "args" .= args
235 , "type" .= tipe
244 , "type" .= tipe
239−toUnionField :: ( String, [String], [(String, [Type])] ) -> Json.Pair
248+toUnionField :: ( Text, [Text], [(Text, [Type])] ) -> Json.Pair
240toUnionField ( name, args, constructors ) =
249toUnionField ( name, args, constructors ) =
241− Text.pack name .= Json.object
250+ name .= Json.object
242 [ "args" .= args
251 [ "args" .= args
243 , "tags" .= Json.object (map toCtorObject constructors)
252 , "tags" .= Json.object (map toCtorObject constructors)
247−toCtorObject :: (String, [Type]) -> Json.Pair
256+toCtorObject :: (Text, [Type]) -> Json.Pair
248toCtorObject (name, args) =
257toCtorObject (name, args) =
249− Text.pack name .= args
258+ name .= args