on master help sign in
Convert Strings to Text in all the AST files
commit5e065de73acda3eb88fa7ec9f251660666910021 authorEvan Czaplicki <evancz@users.noreply.github.com> dateSun 4 Dec 2016, 19:54 (10 years ago) parent1f0970f
12 files changed+179−104
src/AST/Declaration.hs+3−2 src/AST/Effects.hs+7−5 src/AST/Expression/Canonical.hs+2−2 src/AST/Expression/Optimized.hs+1−1 src/AST/Expression/Source.hs+1−1 src/AST/Helpers.hs+2−2 src/AST/Literal.hs+35−23 src/AST/Module.hs+24−11 src/AST/Module/Name.hs+20−11 src/AST/Variable.hs+39−17 src/Elm/Docs.hs+16−15 src/Elm/Package.hs+29−14
src/AST/Declaration.hs+3−2
@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module AST.Declaration where
34
45import Data.Binary
@@ -108,8 +109,8 @@ data Assoc = L | N | R
108109 deriving (Eq)
109110
110111
111−assocToString :: Assoc -> String
112−assocToString assoc =
112+assocToText :: Assoc -> Text
113+assocToText assoc =
113114 case assoc of
114115 L ->
115116 "left"
src/AST/Effects.hs+7−5
@@ -1,5 +1,7 @@
11module AST.Effects where
22
3+import Data.Text (Text)
4+
35import qualified AST.Type as Type
46import qualified Elm.Package as Pkg
57import qualified Reporting.Annotation as A
@@ -39,9 +41,9 @@ data Info =
3941
4042
4143data ManagerType
42− = CmdManager (A.Located String)
43− | SubManager (A.Located String)
44− | FxManager (A.Located String) (A.Located String)
44+ = CmdManager (A.Located Text)
45+ | SubManager (A.Located Text)
46+ | FxManager (A.Located Text) (A.Located Text)
4547
4648
4749
@@ -50,14 +52,14 @@ data ManagerType
5052
5153data PortRaw =
5254 PortRaw
53− { _rawName :: String
55+ { _rawName :: Text
5456 , _rawType :: Type.Raw
5557 }
5658
5759
5860data PortCanonical =
5961 PortCanonical
60− { _name :: String
62+ { _name :: Text
6163 , _kind :: Kind
6264 , _type :: Type.Canonical
6365 }
src/AST/Expression/Canonical.hs+2−2
@@ -42,7 +42,7 @@ data Expr'
4242 | If [(Expr, Expr)] Expr
4343 | Let [Def] Expr
4444 | Case Expr [(Ptrn.Canonical, Expr)]
45− | Ctor Text [Expr]
45+ | Ctor Var.Canonical [Expr]
4646 | Access Expr Text
4747 | Update Expr [(Text, Expr)]
4848 | Record [(Text, Expr)]
@@ -53,7 +53,7 @@ data Expr'
5353 | IncomingPort Text Type.Canonical
5454 | Program Main Expr
5555 | SaveEnv ModuleName.Canonical Effects.Canonical
56− | GLShader Text Text Literal.GLShaderTipe
56+ | GLShader Text Text Literal.Shader
5757
5858
5959data Main
src/AST/Expression/Optimized.hs+1−1
@@ -57,7 +57,7 @@ data Expr
5757 | OutgoingPort Text Type.Canonical
5858 | IncomingPort Text Type.Canonical
5959 | Program Can.Main Expr
60− | GLShader Text Text Literal.GLShaderTipe
60+ | GLShader Text Text Literal.Shader
6161 | Crash ModuleName.Canonical R.Region (Maybe Expr)
6262
6363
src/AST/Expression/Source.hs+1−1
@@ -40,7 +40,7 @@ data Expr' def
4040 | Access (Expr def) Text
4141 | Update (Expr def) [(A.Located Text, Expr def)]
4242 | Record [(A.Located Text, Expr def)]
43− | GLShader Text Text Literal.GLShaderTipe
43+ | GLShader Text Text Literal.Shader
4444
4545
4646type RawExpr = Expr RawDef
src/AST/Helpers.hs+2−2
@@ -22,9 +22,9 @@ isTuple name =
2222-- INFIX OPERATORS
2323
2424
25−isOp :: Text -> Bool
25+isOp :: String -> Bool
2626isOp name =
27− Text.all isSymbol name
27+ all isSymbol name
2828
2929
3030isSymbol :: Char -> Bool
src/AST/Literal.hs+35−23
@@ -1,9 +1,16 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module AST.Literal where
34
45import qualified Data.Map as Map
56import Data.Text (Text)
67
8+import qualified AST.Variable as Var
9+
10+
11+
12+-- LITERALS
13+
714
815data Literal
916 = IntNum Int
@@ -24,32 +31,37 @@ toString literal =
2431 Boolean bool -> show bool
2532
2633
27−data GLTipe
28− = Int
29− | Float
30− | V2
31− | V3
32− | V4
33− | M4
34− | Texture
35− deriving (Eq)
34+
35+-- WebGL TYPES
36+
37+
38+data GLType
39+ = Int
40+ | Float
41+ | V2
42+ | V3
43+ | V4
44+ | M4
45+ | Texture
46+ deriving (Eq)
3647
3748
38−glTipeName :: GLTipe -> String
39−glTipeName glTipe =
40− case glTipe of
41− Int -> "Int"
42− Float -> "Float"
43− V2 -> "Math.Vector2.Vec2"
44− V3 -> "Math.Vector3.Vec3"
45− V4 -> "Math.Vector4.Vec4"
46− M4 -> "Math.Matrix4.Mat4"
47− Texture -> "WebGL.Texture"
49+glTypeToVar :: GLType -> Var.Canonical
50+glTypeToVar glTipe =
51+ case glTipe of
52+ V2 -> Var.inLinearAlgebra "Math.Vector2" "Vec2"
53+ V3 -> Var.inLinearAlgebra "Math.Vector3" "Vec3"
54+ V4 -> Var.inLinearAlgebra "Math.Vector4" "Vec4"
55+ M4 -> Var.inLinearAlgebra "Math.Matrix4" "Mat4"
56+ Int -> Var.builtin "Int"
57+ Float -> Var.builtin "Float"
58+ Texture -> Var.inWebGL "WebGL" "Texture"
4859
4960
50−data GLShaderTipe = GLShaderTipe
51− { attribute :: Map.Map String GLTipe
52− , uniform :: Map.Map String GLTipe
53− , varying :: Map.Map String GLTipe
61+data Shader =
62+ Shader
63+ { attribute :: Map.Map Text GLType
64+ , uniform :: Map.Map Text GLType
65+ , varying :: Map.Map Text GLType
5466 }
5567 deriving (Eq)
src/AST/Module.hs+24−11
@@ -1,5 +1,7 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
13module AST.Module
2− ( Header(..), Module(..)
4+ ( Header(..), defaultHeader, Module(..)
35
46 , Source, SourceInfo(..), SourceTag(..), SourceSettings, emptySettings
57 , Valid, ValidInfo(..)
@@ -17,6 +19,7 @@ module AST.Module
1719
1820import Data.Binary
1921import qualified Data.Map as Map
22+import Data.Text (Text)
2023
2124import qualified AST.Declaration as Decl
2225import qualified AST.Effects as Effects
@@ -43,11 +46,21 @@ data Header imports =
4346 , _name :: Name.Raw
4447 , _exports :: Var.Listing (A.Located Var.Value)
4548 , _settings :: SourceSettings
46− , _docs :: A.Located (Maybe String)
49+ , _docs :: Maybe (A.Located Text)
4750 , _imports :: imports
4851 }
4952
5053
54+instance Show (Header a) where
55+ show header =
56+ show (_name header)
57+
58+
59+defaultHeader :: imports -> Header imports
60+defaultHeader imports_ =
61+ Header Normal "Main" Var.openListing emptySettings Nothing imports_
62+
63+
5164
5265-- MODULES
5366
@@ -68,7 +81,7 @@ data SourceInfo =
6881 Source
6982 { srcTag :: SourceTag
7083 , srcSettings :: SourceSettings
71− , srcDocs :: A.Located (Maybe String)
84+ , srcDocs :: Maybe (A.Located Text)
7285 , srcExports :: Var.Listing (A.Located Var.Value)
7386 , srcImports :: [UserImport]
7487 , srcDecls :: [Decl.Source]
@@ -82,7 +95,7 @@ data SourceTag
8295
8396
8497type SourceSettings =
85− A.Located [(A.Located String, A.Located String)]
98+ A.Located [(A.Located Text, A.Located Text)]
8699
87100
88101emptySettings :: SourceSettings
@@ -96,7 +109,7 @@ type Valid =
96109
97110data ValidInfo =
98111 Valid
99− { validDocs :: A.Located (Maybe String)
112+ { validDocs :: Maybe (A.Located Text)
100113 , validExports :: Var.Listing (A.Located Var.Value)
101114 , validImports :: ([DefaultImport], [UserImport])
102115 , validDecls :: Decl.Valid
@@ -124,7 +137,7 @@ type DefaultImport = (Name.Raw, ImportMethod)
124137
125138data ImportMethod =
126139 ImportMethod
127− { alias :: Maybe String
140+ { alias :: Maybe Text
128141 , exposedVars :: !(Var.Listing Var.Value)
129142 }
130143
@@ -135,7 +148,7 @@ data ImportMethod =
135148
136149data Info program =
137150 Info
138− { docs :: A.Located (Maybe Docs.Centralized)
151+ { docs :: Maybe (A.Located Docs.Centralized)
139152 , exports :: [Var.Value]
140153 , imports :: [Name.Raw]
141154 , program :: program
@@ -148,19 +161,19 @@ data Info program =
148161
149162
150163type Types =
151− Map.Map String Type.Canonical
164+ Map.Map Text Type.Canonical
152165
153166
154167type Aliases =
155− Map.Map String ([String], Type.Canonical)
168+ Map.Map Text ([Text], Type.Canonical)
156169
157170
158171type Unions =
159− Map.Map String (UnionInfo String)
172+ Map.Map Text (UnionInfo Text)
160173
161174
162175type UnionInfo v =
163− ( [String], [(v, [Type.Canonical])] )
176+ ( [Text], [(v, [Type.Canonical])] )
164177
165178
166179type CanonicalUnion =
src/AST/Module/Name.hs+20−11
@@ -3,14 +3,13 @@
33module AST.Module.Name where
44
55import Data.Binary
6−import qualified Data.List as List
76import qualified Data.Text as Text
87import Data.Text (Text)
98
109import qualified Elm.Package as Package
1110
1211
13−type Raw = [Text] -- must be non-empty
12+type Raw = Text -- must be non-empty
1413
1514
1615data Canonical = Canonical
@@ -35,24 +34,34 @@ inHtml raw =
3534 Canonical Package.html raw
3635
3736
37+inWebGL :: Raw -> Canonical
38+inWebGL raw =
39+ Canonical Package.webgl raw
40+
41+
42+inLinearAlgebra :: Raw -> Canonical
43+inLinearAlgebra raw =
44+ Canonical Package.linearAlgebra raw
45+
46+
3847toString :: Raw -> String
39−toString rawName =
40− List.intercalate "." (map Text.unpack rawName)
48+toString name =
49+ Text.unpack name
4150
4251
4352canonicalToString :: Canonical -> String
44−canonicalToString (Canonical _ rawName) =
45− toString rawName
53+canonicalToString (Canonical _ name) =
54+ toString name
4655
4756
4857isNative :: Raw -> Bool
4958isNative name =
50− case name of
51− "Native" : _ ->
52− True
59+ Text.isPrefixOf "Native." name
60+
5361
54− _ ->
55− False
62+canonicalIsNative :: Canonical -> Bool
63+canonicalIsNative (Canonical _ name) =
64+ isNative name
5665
5766
5867instance Binary Canonical where
src/AST/Variable.hs+39−17
@@ -68,14 +68,24 @@ inHtml home name =
6868 Canonical (Module (ModuleName.inHtml home)) name
6969
7070
71+inWebGL :: ModuleName.Raw -> Text -> Canonical
72+inWebGL home name =
73+ Canonical (Module (ModuleName.inWebGL home)) name
74+
75+
76+inLinearAlgebra :: ModuleName.Raw -> Text -> Canonical
77+inLinearAlgebra home name =
78+ Canonical (Module (ModuleName.inLinearAlgebra home)) name
79+
80+
7181cmd :: Canonical
7282cmd =
73− inCore ["Platform","Cmd"] "Cmd"
83+ inCore "Platform.Cmd" "Cmd"
7484
7585
7686sub :: Canonical
7787sub =
78− inCore ["Platform","Sub"] "Sub"
88+ inCore "Platform.Sub" "Sub"
7989
8090
8191
@@ -105,21 +115,21 @@ is home name var =
105115
106116isJson :: Canonical -> Bool
107117isJson =
108− is ["Json", "Encode"] "Value"
118+ is "Json.Encode" "Value"
109119
110120
111121isMaybe :: Canonical -> Bool
112122isMaybe =
113− is ["Maybe"] "Maybe"
123+ is "Maybe" "Maybe"
114124
115125isArray :: Canonical -> Bool
116126isArray =
117− is ["Array"] "Array"
127+ is "Array" "Array"
118128
119129
120130isTask :: Canonical -> Bool
121131isTask =
122− is ["Task"] "Task"
132+ is "Task" "Task"
123133
124134
125135isList :: Canonical -> Bool
@@ -127,6 +137,16 @@ isList v =
127137 v == Canonical BuiltIn "List"
128138
129139
140+isNative :: Canonical -> Bool
141+isNative v =
142+ case v of
143+ Canonical (Module name) _ ->
144+ ModuleName.canonicalIsNative name
145+
146+ _ ->
147+ False
148+
149+
130150isTuple :: Canonical -> Bool
131151isTuple v =
132152 case v of
@@ -141,7 +161,8 @@ isPrimitive :: Canonical -> Bool
141161isPrimitive v =
142162 case v of
143163 Canonical BuiltIn name ->
144− name `elem` ["Int","Float","String","Bool"]
164+ elem name ["Int","Float","String","Bool"]
165+
145166 _ ->
146167 False
147168
@@ -200,7 +221,8 @@ instance ToString Canonical where
200221
201222
202223-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
203−data Listing a = Listing
224+data Listing a =
225+ Listing
204226 { _explicits :: [a]
205227 , _open :: Bool
206228 }
@@ -224,9 +246,9 @@ listing xs =
224246
225247-- | A value that can be imported or exported
226248data Value
227− = Value !String
228− | Alias !String
229− | Union !String !(Listing String)
249+ = Value !Text
250+ | Alias !Text
251+ | Union !Text !(Listing Text)
230252 deriving (Eq, Ord)
231253
232254
@@ -234,12 +256,12 @@ data Value
234256-- CATEGORIZING VALUES
235257
236258
237−getValues :: [Value] -> [String]
259+getValues :: [Value] -> [Text]
238260getValues values =
239261 Maybe.mapMaybe getValue values
240262
241263
242−getValue :: Value -> Maybe String
264+getValue :: Value -> Maybe Text
243265getValue value =
244266 case value of
245267 Value name -> Just name
@@ -247,12 +269,12 @@ getValue value =
247269 Union _ _ -> Nothing
248270
249271
250−getAliases :: [Value] -> [String]
272+getAliases :: [Value] -> [Text]
251273getAliases values =
252274 Maybe.mapMaybe getAlias values
253275
254276
255−getAlias :: Value -> Maybe String
277+getAlias :: Value -> Maybe Text
256278getAlias value =
257279 case value of
258280 Value _-> Nothing
@@ -260,12 +282,12 @@ getAlias value =
260282 Union _ _ -> Nothing
261283
262284
263−getUnions :: [Value] -> [(String, Listing String)]
285+getUnions :: [Value] -> [(Text, Listing Text)]
264286getUnions values =
265287 Maybe.mapMaybe getUnion values
266288
267289
268−getUnion :: Value -> Maybe (String, Listing String)
290+getUnion :: Value -> Maybe (Text, Listing Text)
269291getUnion value =
270292 case value of
271293 Value _ -> Nothing
src/Elm/Docs.hs+16−15
@@ -9,6 +9,7 @@ import qualified Data.Aeson.Encode.Pretty as Json
99import qualified Data.ByteString.Lazy.Char8 as BS
1010import qualified Data.Map as Map
1111import qualified Data.Text as Text
12+import Data.Text (Text)
1213
1314import qualified Docs.AST as Docs
1415import qualified Elm.Compiler.Module as Module
@@ -24,7 +25,7 @@ import qualified Reporting.Annotation as A
2425
2526data Documentation = Documentation
2627 { moduleName :: Module.Raw
27− , comment :: String
28+ , comment :: Text
2829 , aliases :: [Alias]
2930 , types :: [Union]
3031 , values :: [Value]
@@ -33,30 +34,30 @@ data Documentation = Documentation
3334
3435
3536data Alias = Alias
36− { aliasName :: String
37− , aliasComment :: String
38− , aliasArgs :: [String]
37+ { aliasName :: Text
38+ , aliasComment :: Text
39+ , aliasArgs :: [Text]
3940 , aliasType :: Type.Type
4041 }
4142
4243
4344data Union = Union
44− { unionName :: String
45− , unionComment :: String
46− , unionArgs :: [String]
47− , unionCases :: [(String, [Type.Type])]
45+ { unionName :: Text
46+ , unionComment :: Text
47+ , unionArgs :: [Text]
48+ , unionCases :: [(Text, [Type.Type])]
4849 }
4950
5051
5152data Value = Value
52− { valueName :: String
53− , valueComment :: String
53+ { valueName :: Text
54+ , valueComment :: Text
5455 , valueType :: Type.Type
55− , valueFix :: Maybe (String,Int)
56+ , valueFix :: Maybe (Text,Int)
5657 }
5758
5859
59−data Version = NonCanonicalTypes | Version String
60+data Version = NonCanonicalTypes | Version Text
6061
6162
6263
@@ -84,7 +85,7 @@ fromCheckedDocs name (Docs.Docs comment aliases unions values) =
8485 (map toAlias (Map.toList aliases))
8586 (map toUnion (Map.toList unions))
8687 (map toValue (Map.toList values))
87− (Version $ (Pkg.versionToString Version.version))
88+ (Version (Text.pack (Pkg.versionToString Version.version)))
8889
8990
9091
@@ -151,12 +152,12 @@ instance Json.ToJSON Version where
151152 Json.String "old"
152153
153154 Version vsn ->
154− Json.String (Text.pack vsn)
155+ Json.String vsn
155156
156157
157158instance Json.FromJSON Version where
158159 parseJSON (Json.String text) =
159− case Text.unpack text of
160+ case text of
160161 "old" -> return NonCanonicalTypes
161162 vrsn -> return (Version vrsn)
162163
src/Elm/Package.hs+29−14
@@ -1,3 +1,5 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
13module Elm.Package where
24
35import Data.Aeson
@@ -5,15 +7,18 @@ import Data.Binary
57import qualified Data.Char as Char
68import Data.Function (on)
79import qualified Data.List as List
8−import qualified Data.Text as T
10+import qualified Data.Text as Text
11+import Data.Text (Text)
912import System.FilePath ((</>))
1013
1114
15+
1216-- PACKGE NAMES
1317
18+
1419data Name = Name
15− { user :: String
16− , project :: String
20+ { _user :: Text
21+ , _project :: Text
1722 }
1823 deriving (Eq, Ord, Show)
1924
@@ -41,19 +46,29 @@ html =
4146 Name "elm-lang" "html"
4247
4348
49+webgl :: Name
50+webgl =
51+ Name "elm-community" "webgl"
52+
53+
54+linearAlgebra :: Name
55+linearAlgebra =
56+ Name "elm-community" "linear-algebra"
57+
58+
4459toString :: Name -> String
45−toString name =
46− user name ++ "/" ++ project name
60+toString (Name user project) =
61+ Text.unpack user ++ "/" ++ Text.unpack project
4762
4863
4964toUrl :: Name -> String
50−toUrl name =
51− user name ++ "/" ++ project name
65+toUrl (Name user project) =
66+ Text.unpack user ++ "/" ++ Text.unpack project
5267
5368
5469toFilePath :: Name -> FilePath
55−toFilePath name =
56− user name </> project name
70+toFilePath (Name user project) =
71+ Text.unpack user </> Text.unpack project
5772
5873
5974fromString :: String -> Either String Name
@@ -67,7 +82,7 @@ fromString string =
6782 Left "You did not provide a project name (user/project)"
6883
6984 else if all (/='/') project then
70− Name user <$> validateProjectName project
85+ Name (Text.pack user) <$> validateProjectName project
7186
7287 else
7388 Left "Expecting only one slash, separating the user and project name (user/project)"
@@ -102,7 +117,7 @@ whitelistedUppercaseName name =
102117 ]
103118
104119
105−validateProjectName :: String -> Either String String
120+validateProjectName :: String -> Either String Text
106121validateProjectName str =
107122 if elem ('-','-') (zip str (tail str)) then
108123 Left "There is a double dash -- in your package name. It must be a single dash."
@@ -117,7 +132,7 @@ validateProjectName str =
117132 Left "Package names must start with a letter."
118133
119134 else
120− Right str
135+ Right (Text.pack str)
121136
122137
123138instance Binary Name where
@@ -130,7 +145,7 @@ instance Binary Name where
130145instance FromJSON Name where
131146 parseJSON (String text) =
132147 let
133− string = T.unpack text
148+ string = Text.unpack text
134149 in
135150 case fromString string of
136151 Left msg ->
@@ -233,7 +248,7 @@ instance Binary Version where
233248
234249instance FromJSON Version where
235250 parseJSON (String text) =
236− let string = T.unpack text in
251+ let string = Text.unpack text in
237252 case versionFromString string of
238253 Right v ->
239254 return v