on master help sign in
Upgrade Elm/ from String to Text
commit2203dd6d031a4bd9c1ac1549063357db20c1183a authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 7 Dec 2016, 15:16 (10 years ago) parent2feb357
6 files changed+125−121
src/Elm/Compiler.hs+11−28 src/Elm/Compiler/Imports.hs+16−12 src/Elm/Compiler/Module.hs+17−14 src/Elm/Compiler/Type.hs+60−51 src/Elm/Compiler/Type/Extract.hs+13−10 src/Elm/Utils.hs+8−6
src/Elm/Compiler.hs+11−28
@@ -12,8 +12,8 @@ module Elm.Compiler
1212import qualified Data.Aeson as Json
1313import qualified Data.Map as Map
1414import qualified Data.Text.Lazy as LazyText
15+import Data.Text (Text)
1516import System.IO (Handle)
16−import qualified Text.Parsec.Error as Parsec
1717
1818import qualified AST.Module as Module
1919import qualified AST.Module.Name as ModuleName
@@ -25,13 +25,11 @@ import qualified Elm.Compiler.Version
2525import qualified Elm.Docs as Docs
2626import qualified Elm.Package as Package
2727import qualified Generate.JavaScript as JS
28−import qualified Parse.Helpers as Parse (parse)
28+import qualified Parse.Helpers as Parse (run)
2929import qualified Parse.Module as Parse (header)
3030import qualified Reporting.Annotation as A
3131import qualified Reporting.Bag as Bag
3232import qualified Reporting.Error as Error
33−import qualified Reporting.Error.Syntax as SyntaxError
34−import qualified Reporting.Region as Region
3533import qualified Reporting.Render.Type as RenderType
3634import qualified Reporting.Report as Report
3735import qualified Reporting.Result as Result
@@ -57,28 +55,17 @@ data Tag
5755 | Port
5856
5957
60−parseDependencies
61− :: Package.Name
62− -> String
63− -> Either Error (Tag, PublicModule.Raw, [PublicModule.Raw])
58+parseDependencies :: Package.Name -> Text -> Either Error (Tag, PublicModule.Raw, [PublicModule.Raw])
6459parseDependencies pkgName sourceCode =
65− case Parse.parse Parse.header sourceCode of
60+ case Parse.run Parse.header sourceCode of
6661 Right header ->
6762 Right $ getDeps pkgName header
6863
69− Left parseError ->
70− let
71− pos = Region.fromSourcePos (Parsec.errorPos parseError)
72− msgs = Parsec.errorMessages parseError
73− err = Error.Syntax (SyntaxError.Parse msgs)
74− in
75− Left (Error (A.at pos pos err))
64+ Left err ->
65+ Left (error "TODO parse deps" err)
7666
7767
78−getDeps
79− :: Package.Name
80− -> Module.Header [Module.UserImport]
81− -> (Tag, PublicModule.Raw, [PublicModule.Raw])
68+getDeps :: Package.Name -> Module.Header [Module.UserImport] -> (Tag, PublicModule.Raw, [PublicModule.Raw])
8269getDeps pkgName (Module.Header sourceTag name _ _ _ imports) =
8370 let
8471 tag =
@@ -101,12 +88,7 @@ getDeps pkgName (Module.Header sourceTag name _ _ _ imports) =
10188
10289
10390{-| Compiles Elm source code to JavaScript. -}
104−compile
105− :: Context
106− -> String
107− -> PublicModule.Interfaces
108− -> (Localizer, [Warning], Either [Error] Result)
109−
91+compile :: Context -> Text -> PublicModule.Interfaces -> (Localizer, [Warning], Either [Error] Result)
11092compile context source interfaces =
11193 let
11294 (Context packageName isExposed dependencies) =
@@ -117,7 +99,7 @@ compile context source interfaces =
11799 docs <- Result.format Error.Docs (docsGen isExposed modul)
118100
119101 let interface = Module.toInterface packageName modul
120− let javascript = JS.generate modul
102+ let javascript = {-# SCC elm_compiler_generate #-} JS.generate modul
121103
122104 return (Result docs interface javascript)
123105 in
@@ -127,7 +109,8 @@ compile context source interfaces =
127109 )
128110
129111
130−data Context = Context
112+data Context =
113+ Context
131114 { _packageName :: Package.Name
132115 , _isExposed :: Bool
133116 , _dependencies :: [PublicModule.Canonical]
src/Elm/Compiler/Imports.hs+16−12
@@ -1,5 +1,9 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
13module Elm.Compiler.Imports (defaults) where
24
5+import Data.Text (Text)
6+
37import qualified AST.Module as Module
48import qualified AST.Variable as Var
59
@@ -12,16 +16,16 @@ import qualified AST.Variable as Var
1216
1317defaults :: [Module.DefaultImport]
1418defaults =
15− [ ["Basics"] ==> Module.ImportMethod Nothing Var.openListing
16− , ["Debug"] ==> Module.ImportMethod Nothing Var.closedListing
17− , ["List"] ==> exposing [Var.Value "::"]
18− , ["Maybe"] ==> exposing [Var.Union "Maybe" Var.openListing]
19− , ["Result"] ==> exposing [Var.Union "Result" Var.openListing]
20− , ["String"] ==> Module.ImportMethod Nothing Var.closedListing
21− , ["Tuple"] ==> Module.ImportMethod Nothing Var.closedListing
22− , ["Platform"] ==> exposing [closedType "Program"]
23− , ["Platform","Cmd"] ==> named "Cmd" [closedType "Cmd", Var.Value "!"]
24− , ["Platform","Sub"] ==> named "Sub" [closedType "Sub"]
19+ [ "Basics" ==> Module.ImportMethod Nothing Var.openListing
20+ , "Debug" ==> Module.ImportMethod Nothing Var.closedListing
21+ , "List" ==> exposing [Var.Value "::"]
22+ , "Maybe" ==> exposing [Var.Union "Maybe" Var.openListing]
23+ , "Result" ==> exposing [Var.Union "Result" Var.openListing]
24+ , "String" ==> Module.ImportMethod Nothing Var.closedListing
25+ , "Tuple" ==> Module.ImportMethod Nothing Var.closedListing
26+ , "Platform" ==> exposing [closedType "Program"]
27+ , "Platform.Cmd" ==> named "Cmd" [closedType "Cmd", Var.Value "!"]
28+ , "Platform.Sub" ==> named "Sub" [closedType "Sub"]
2529 ]
2630
2731
@@ -30,12 +34,12 @@ exposing vars =
3034 Module.ImportMethod Nothing (Var.listing vars)
3135
3236
33−closedType :: String -> Var.Value
37+closedType :: Text -> Var.Value
3438closedType name =
3539 Var.Union name Var.closedListing
3640
3741
38−named :: String -> [Var.Value] -> Module.ImportMethod
42+named :: Text -> [Var.Value] -> Module.ImportMethod
3943named name vars =
4044 Module.ImportMethod (Just name) (Var.listing vars)
4145
src/Elm/Compiler/Module.hs+17−14
@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Elm.Compiler.Module
34 ( Interface, Interfaces
45 , ModuleName.Raw
@@ -16,6 +17,7 @@ import qualified Data.Char as Char
1617import qualified Data.List as List
1718import qualified Data.Map as Map
1819import qualified Data.Text as Text
20+import Data.Text (Text)
1921import System.FilePath ((</>))
2022
2123import qualified AST.Module as Module
@@ -35,7 +37,7 @@ type Interface = Module.Interface
3537type Interfaces = Module.Interfaces
3638
3739
38−interfaceAliasedTypes :: Interface -> Map.Map String PublicType.Type
40+interfaceAliasedTypes :: Interface -> Map.Map Text PublicType.Type
3941interfaceAliasedTypes interface =
4042 Map.map Extract.extract (Module.iTypes interface)
4143
@@ -57,7 +59,7 @@ fromJson (RawForJson raw) =
5759 raw
5860
5961
60−qualifiedVar :: ModuleName.Canonical -> String -> String
62+qualifiedVar :: ModuleName.Canonical -> Text -> Text
6163qualifiedVar =
6264 Gen.qualified
6365
@@ -67,13 +69,13 @@ qualifiedVar =
6769
6870
6971nameToPath :: ModuleName.Raw -> FilePath
70−nameToPath names =
71− List.foldl1 (</>) names
72+nameToPath name =
73+ List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
7274
7375
7476nameToString :: ModuleName.Raw -> String
75−nameToString names =
76− List.intercalate "." names
77+nameToString name =
78+ Text.unpack name
7779
7880
7981nameFromString :: String -> Maybe ModuleName.Raw
@@ -83,7 +85,7 @@ nameFromString =
8385
8486hyphenate :: ModuleName.Raw -> String
8587hyphenate names =
86− List.intercalate "-" names
88+ Text.unpack (Text.replace "." "-" names)
8789
8890
8991dehyphenate :: String -> Maybe ModuleName.Raw
@@ -93,21 +95,22 @@ dehyphenate =
9395
9496fromString :: Char -> String -> Maybe ModuleName.Raw
9597fromString sep raw =
96− mapM isLegit names
98+ do chunks <- mapM isLegit names
99+ return (Text.pack (List.intercalate "." chunks))
97100 where
98101 names =
99102 filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
100103
101104 isLegit name =
102105 case name of
103− [] -> Nothing
104− char:rest ->
105− if Char.isUpper char && all legitChar rest
106− then Just name
107− else Nothing
106+ [] ->
107+ Nothing
108+
109+ char:rest ->
110+ if Char.isUpper char && all legitChar rest then Just name else Nothing
108111
109112 legitChar char =
110− Char.isAlphaNum char || char `elem` "_'"
113+ Char.isAlphaNum char || char == '_'
111114
112115
113116
src/Elm/Compiler/Type.hs+60−51
@@ -1,3 +1,4 @@
1+{-# OPTIONS_GHC -Wall #-}
12{-# LANGUAGE OverloadedStrings #-}
23module Elm.Compiler.Type
34 ( Type(..), toString
@@ -11,6 +12,7 @@ import qualified Data.Aeson as Json
1112import qualified Data.Aeson.Types as Json
1213import qualified Data.ByteString.Lazy.Char8 as BS
1314import qualified Data.Text as Text
15+import Data.Text (Text)
1416import Text.PrettyPrint as P
1517
1618import qualified AST.Helpers as Help
@@ -24,17 +26,17 @@ import qualified Reporting.Annotation as A
2426
2527data Type
2628 = Lambda Type Type
27− | Var String
28− | Type String
29+ | Var Text
30+ | Type Text
2931 | App Type [Type]
30− | Record [(String, Type)] (Maybe Type)
32+ | Record [(Text, Type)] (Maybe Type)
3133
3234
3335data Program =
3436 Program
3537 { _message :: Type
36− , _aliases :: [( String, [String], Type )]
37− , _unions :: [( String, [String], [(String, [Type])] )]
38+ , _aliases :: [( Text, [Text], Type )]
39+ , _unions :: [( Text, [Text], [(Text, [Type])] )]
3840 }
3941
4042
@@ -65,27 +67,32 @@ toDoc context tipe =
6567 _ -> P.parens lambda
6668
6769 Var name ->
68− P.text name
70+ P.text (Text.unpack name)
6971
7072 Type name ->
71− P.text (if name == "_Tuple0" then "()" else name)
72−
73− App (Type name) args
74− | name == "_Tuple0" ->
75− P.text "()"
76−
77− | Help.isTuple name ->
78− P.sep
79− [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
80− , P.rparen
81− ]
82−
83− | otherwise ->
84− let adt = P.hang (P.text name) 2 (P.sep $ map (toDoc ADT) args)
85− in
86− case (context, args) of
87− (ADT, _ : _) -> P.parens adt
88− _ -> adt
73+ P.text (Text.unpack (if name == "_Tuple0" then "()" else name))
74+
75+ App (Type name) args ->
76+ if name == "_Tuple0" then
77+ P.text "()"
78+
79+ else if Help.isTuple name then
80+ P.sep
81+ [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
82+ , P.rparen
83+ ]
84+
85+ else
86+ let
87+ application =
88+ P.hang
89+ (P.text (Text.unpack name))
90+ 2
91+ (P.sep $ map (toDoc ADT) args)
92+ in
93+ case (context, args) of
94+ (ADT, _ : _) -> P.parens application
95+ _ -> application
8996
9097 App _ _ ->
9198 error $
@@ -105,7 +112,7 @@ toDoc context tipe =
105112
106113 (fields, Just x) ->
107114 P.hang
108− (P.lbrace <+> P.text x <+> P.text "|")
115+ (P.lbrace <+> P.text (Text.unpack x) <+> P.text "|")
109116 4
110117 (P.sep
111118 [ P.sep (P.punctuate P.comma (map prettyField fields))
@@ -113,8 +120,8 @@ toDoc context tipe =
113120 ]
114121 )
115122 where
116− prettyField (field, tipe) =
117− P.text field <+> P.text ":" <+> toDoc None tipe
123+ prettyField (field, fieldType) =
124+ P.text (Text.unpack field) <+> P.text ":" <+> toDoc None fieldType
118125
119126
120127collectLambdas :: Type -> [Type]
@@ -124,19 +131,22 @@ collectLambdas tipe =
124131 _ -> [tipe]
125132
126133
127−flattenRecord :: Type -> ( [(String, Type)], Maybe String )
134+flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
128135flattenRecord tipe =
129− case tipe of
130− Var x -> ([], Just x)
136+ case tipe of
137+ Var x ->
138+ ([], Just x)
131139
132− Record fields Nothing -> (fields, Nothing)
140+ Record fields Nothing ->
141+ (fields, Nothing)
133142
134− Record fields (Just ext) ->
135− let (fields',ext') = flattenRecord ext
136− in
137− (fields' ++ fields, ext')
143+ Record fields (Just ext) ->
144+ let (fields',ext') = flattenRecord ext
145+ in
146+ (fields' ++ fields, ext')
138147
139− _ -> error "Trying to flatten ill-formed record."
148+ _ ->
149+ error "Trying to flatten ill-formed record."
140150
141151
142152
@@ -157,9 +167,13 @@ instance Json.FromJSON Type where
157167 ++ BS.unpack (Json.encode value)
158168 in
159169 case value of
160− Json.String text ->
161− either failure (return . fromRawType) $
162− Parse.parse Type.expr (replacePrimes (Text.unpack text))
170+ Json.String txt ->
171+ case Parse.run Type.expression (Text.replace "'" "_" txt) of
172+ Left err ->
173+ failure err
174+
175+ Right (tipe, _, _) ->
176+ return (fromRawType tipe)
163177
164178 Json.Object obj ->
165179 fromObject obj
@@ -168,11 +182,6 @@ instance Json.FromJSON Type where
168182 failure ()
169183
170184
171−replacePrimes :: String -> String
172−replacePrimes string =
173− map (\c -> if c == '\'' then '_' else c) string
174−
175−
176185fromObject :: Json.Object -> Json.Parser Type
177186fromObject obj =
178187 do tag <- obj .: "tag"
@@ -206,7 +215,7 @@ fromRawType (A.A _ astType) =
206215 Var x
207216
208217 Type.RType var ->
209− Type (Var.toString var)
218+ Type (Var.toText var)
210219
211220 Type.RApp t ts ->
212221 App (fromRawType t) (map fromRawType ts)
@@ -228,22 +237,22 @@ instance Json.ToJSON Program where
228237 ]
229238
230239
231−toAliasField :: ( String, [String], Type ) -> Json.Pair
240+toAliasField :: ( Text, [Text], Type ) -> Json.Pair
232241toAliasField ( name, args, tipe ) =
233− Text.pack name .= Json.object
242+ name .= Json.object
234243 [ "args" .= args
235244 , "type" .= tipe
236245 ]
237246
238247
239−toUnionField :: ( String, [String], [(String, [Type])] ) -> Json.Pair
248+toUnionField :: ( Text, [Text], [(Text, [Type])] ) -> Json.Pair
240249toUnionField ( name, args, constructors ) =
241− Text.pack name .= Json.object
250+ name .= Json.object
242251 [ "args" .= args
243252 , "tags" .= Json.object (map toCtorObject constructors)
244253 ]
245254
246255
247−toCtorObject :: (String, [Type]) -> Json.Pair
256+toCtorObject :: (Text, [Type]) -> Json.Pair
248257toCtorObject (name, args) =
249− Text.pack name .= args
258+ name .= args
src/Elm/Compiler/Type/Extract.hs+13−10
@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Elm.Compiler.Type.Extract
34 ( extract
45 , extractProgram
@@ -9,6 +10,8 @@ import qualified Control.Monad.Writer as Writer
910import qualified Data.Map as Map
1011import qualified Data.Maybe as Maybe
1112import qualified Data.Set as Set
13+import Data.Text (Text)
14+
1215import qualified AST.Module as Module
1316import qualified AST.Module.Name as ModuleName
1417import qualified AST.Type as T
@@ -45,11 +48,11 @@ extractHelp astType =
4548 Lambda <$> extractHelp arg <*> extractHelp result
4649
4750 T.Var x ->
48− return $ Var x
51+ return (Var x)
4952
5053 T.Type var ->
5154 do Writer.tell ( Set.empty, Set.singleton var )
52− return $ Type $ Var.toString var
55+ return (Type (Var.toText var))
5356
5457 T.App constructor args ->
5558 App <$> extractHelp constructor <*> traverse extractHelp args
@@ -62,7 +65,7 @@ extractHelp astType =
6265 T.Aliased name args aliasedType ->
6366 do Writer.tell ( Set.singleton name, Set.empty )
6467 _ <- extractHelp (T.dealias args aliasedType)
65− App (Type (Var.toString name)) <$> traverse (extractHelp . snd) args
68+ App (Type (Var.toText name)) <$> traverse (extractHelp . snd) args
6669
6770
6871
@@ -82,15 +85,15 @@ extractProgram interfaces name =
8285
8386 let (msgType, msgDeps) = Writer.runWriter (extractHelp message)
8487 let (aliases, unions) = extractTransitive interfaces mempty msgDeps
85− Just $ Program msgType aliases unions
88+ Just (Program msgType aliases unions)
8689
8790
8891type Alias =
89− ( String, [String], Type )
92+ ( Text, [Text], Type )
9093
9194
9295type Union =
93− ( String, [String], [(String, [Type])] )
96+ ( Text, [Text], [(Text, [Type])] )
9497
9598
9699extractTransitive :: Module.Interfaces -> Deps -> Deps -> ( [Alias], [Union] )
@@ -120,7 +123,7 @@ extractAlias :: Module.Interfaces -> Var.Canonical -> Writer.Writer Deps (Maybe
120123extractAlias interfaces var =
121124 let
122125 toAlias (args, tipe) =
123− (,,) (Var.toString var) args <$> extractHelp tipe
126+ (,,) (Var.toText var) args <$> extractHelp tipe
124127 in
125128 traverse toAlias (get Module.iAliases interfaces var)
126129
@@ -129,20 +132,20 @@ extractUnion :: Module.Interfaces -> Var.Canonical -> Writer.Writer Deps (Maybe
129132extractUnion interfaces var =
130133 let
131134 toUnion (args, constructors) =
132− (,,) (Var.toString var) args
135+ (,,) (Var.toText var) args
133136 <$> traverse (traverse (traverse extractHelp)) constructors
134137 in
135138 traverse toUnion (get Module.iUnions interfaces var)
136139
137140
138−get :: (Module.Interface -> Map.Map String a) -> Module.Interfaces -> Var.Canonical -> Maybe a
141+get :: (Module.Interface -> Map.Map Text a) -> Module.Interfaces -> Var.Canonical -> Maybe a
139142get getInfo interfaces var =
140143 do (home, name) <- getHome var
141144 iface <- Map.lookup home interfaces
142145 Map.lookup name (getInfo iface)
143146
144147
145−getHome :: Var.Canonical -> Maybe (ModuleName.Canonical, String)
148+getHome :: Var.Canonical -> Maybe (ModuleName.Canonical, Text)
146149getHome (Var.Canonical home name) =
147150 case home of
148151 Var.BuiltIn ->
src/Elm/Utils.hs+8−6
@@ -1,5 +1,6 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE OverloadedStrings #-}
34module Elm.Utils
45 ( (|>), (<|)
56 , run, unwrappedRun
@@ -8,7 +9,7 @@ module Elm.Utils
89 ) where
910
1011import Control.Monad.Except (MonadError, MonadIO, liftIO, throwError)
11−import qualified Data.List as List
12+import Data.Text (Text)
1213import System.Exit (ExitCode(ExitSuccess, ExitFailure))
1314import System.Process (readProcessWithExitCode)
1415
@@ -98,11 +99,12 @@ missingExe command =
9899
99100-- DECL CHECKER
100101
101−isDeclaration :: String -> Maybe String
102−isDeclaration str =
103− case Parse.parse Parse.def str of
104− Right (A.A _ (Source.Definition pattern _)) ->
105− Just (List.intercalate "$" (Pattern.boundVarList pattern))
102+
103+isDeclaration :: Text -> Maybe [Text]
104+isDeclaration source =
105+ case Parse.run Parse.definition source of
106+ Right (A.A _ (Source.Definition pattern _), _, _) ->
107+ Just (Pattern.boundVarList pattern)
106108
107109 _ ->
108110 Nothing