on master help sign in
Hide Elm.Name implementation in more places

This is prep work that will allow experimenting with alternate implementations.

commitdc356df99fff2fe5d3300bb47503b4715c21658c authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 29 Jan 2018, 14:17 (9 years ago) parent4953132
3 files changed+32−31
src/AST/Module/Name.hs+3−15 src/Elm/Name.hs+22−5 src/Type/Type.hs+7−11
src/AST/Module/Name.hs+3−15
@@ -8,7 +8,6 @@ module AST.Module.Name
88 , virtualDom, debug, bitwise
99 , jsonDecode, jsonEncode
1010 , webgl, vector2, vector3, vector4, matrix4
11− , canonicalToText
1211 , isKernel, getKernel, canonicalIsKernel
1312 )
1413 where
@@ -17,8 +16,6 @@ module AST.Module.Name
1716import Prelude hiding (maybe)
1817import Control.Monad (liftM2)
1918import Data.Binary
20−import qualified Data.Text as Text
21−import Data.Text (Text)
2219
2320import qualified Elm.Name as N
2421import qualified Elm.Package as Pkg
@@ -161,26 +158,17 @@ matrix4 = Canonical Pkg.linearAlgebra "Math.Matrix4"
161158
162159
163160
164−-- CONVERSIONS
165−
166−
167−canonicalToText :: Canonical -> Text
168−canonicalToText (Canonical _ name) =
169− name
170−
171−
172−
173161-- IS KERNEL
174162
175163
176164isKernel :: N.Name -> Bool
177165isKernel name =
178− Text.isPrefixOf "Elm.Kernel." name
166+ N.startsWith "Elm.Kernel." name
179167
180168
181−getKernel :: N.Name -> Text
169+getKernel :: N.Name -> N.Name
182170getKernel name =
183− Text.drop 11 name
171+ N.drop 11 name
184172
185173
186174canonicalIsKernel :: Canonical -> Bool
src/Elm/Name.hs+22−5
@@ -4,10 +4,12 @@ module Elm.Name
44 ( Name
55 , length
66 , startsWith
7+ , drop
78 , toString
89 , toBuilder
910 , toShort
1011 , addIndex
12+ , addSafeIndex
1113 , toCompositeName
1214 , fromForeignPtr
1315 , int, float, bool, char, string
@@ -20,10 +22,11 @@ module Elm.Name
2022 where
2123
2224
23−import Prelude hiding (length, maybe, negate)
25+import Prelude hiding (drop, length, maybe, negate)
2426import qualified Data.ByteString.Builder as B
2527import qualified Data.ByteString.Internal as B
2628import qualified Data.ByteString.Short as S
29+import qualified Data.Char as Char
2730import qualified Data.Set as Set
2831import qualified Data.Text as Text
2932import qualified Data.Text.Encoding as Text
@@ -48,6 +51,11 @@ startsWith =
4851 Text.isPrefixOf
4952
5053
54+drop :: Int -> Name -> Name
55+drop =
56+ Text.drop
57+
58+
5159toString :: Name -> String
5260toString =
5361 Text.unpack
@@ -59,13 +67,22 @@ toBuilder =
5967
6068
6169toShort :: Name -> S.ShortByteString
62−toShort =
63− error "TODO"
70+toShort name =
71+ error "TODO" name
6472
6573
6674addIndex :: Name -> Int -> Name
67−addIndex name i =
68− Text.append name (Text.pack (show i))
75+addIndex name index =
76+ Text.append name (Text.pack (show index))
77+
78+
79+addSafeIndex :: Name -> Int -> Name
80+addSafeIndex name index =
81+ Text.append name $ Text.pack $
82+ if Char.isDigit (Text.last name) then
83+ '_' : show index
84+ else
85+ show index
6986
7087
7188toCompositeName :: Set.Set Name -> Name
src/Type/Type.hs+7−11
@@ -32,9 +32,7 @@ import qualified Control.Monad.State.Strict as State
3232import qualified Data.Char as Char
3333import Data.Foldable (foldrM)
3434import qualified Data.Map.Strict as Map
35−import Data.Monoid ((<>))
3635import qualified Data.Text as Text
37−import Data.Text (Text)
3836import Data.Word (Word32)
3937
4038import qualified AST.Canonical as Can
@@ -93,7 +91,7 @@ data Content
9391 | RigidSuper SuperType N.Name
9492 | Structure FlatType
9593 | Alias ModuleName.Canonical N.Name [(N.Name,Variable)] Variable
96− | Error Text
94+ | Error N.Name
9795
9896
9997data SuperType
@@ -287,16 +285,16 @@ nameToRigid name =
287285
288286toSuper :: N.Name -> Maybe SuperType
289287toSuper name =
290− if Text.isPrefixOf "number" name then
288+ if N.startsWith "number" name then
291289 Just Number
292290
293− else if Text.isPrefixOf "comparable" name then
291+ else if N.startsWith "comparable" name then
294292 Just Comparable
295293
296− else if Text.isPrefixOf "appendable" name then
294+ else if N.startsWith "appendable" name then
297295 Just Appendable
298296
299− else if Text.isPrefixOf "compappend" name then
297+ else if N.startsWith "compappend" name then
300298 Just CompAppend
301299
302300 else
@@ -502,7 +500,7 @@ getFreshSuperHelp :: N.Name -> Int -> TakenNames -> (N.Name, Int)
502500getFreshSuperHelp name index taken =
503501 let
504502 newName =
505− if index <= 0 then name else name <> Text.pack (show index)
503+ if index <= 0 then name else N.addIndex name index
506504 in
507505 if Map.member newName taken then
508506 getFreshSuperHelp name (index + 1) taken
@@ -586,10 +584,8 @@ addName index givenName var makeContent takenNames =
586584 indexedName =
587585 if index <= 0 then
588586 givenName
589− else if Char.isDigit (Text.last givenName) then
590− givenName <> Text.pack ('_' : show index)
591587 else
592− givenName <> Text.pack (show index)
588+ N.addSafeIndex givenName index
593589 in
594590 case Map.lookup indexedName takenNames of
595591 Nothing ->