@@ -1,28 +1,124 @@
1−{-# OPTIONS_GHC -Wall #-}
21{-# LANGUAGE OverloadedStrings #-}
3−module AST.Module.Name
4− ( Canonical(..)
2+module Elm.ModuleName
3+ ( Raw
4+ , nameToString
5+ , nameToSlashPath
6+ , nameToHyphenPath
7+ , fromHyphenPath
8+ , encode
9+ , decoder
10+ , Canonical(..)
11+ , canonicalIsKernel
512 , basics, char, string
613 , maybe, result, list, array, dict, tuple
714 , platform, cmd, sub
8− , virtualDom, debug, bitwise
15+ , debug, bitwise
16+ , virtualDom
917 , jsonDecode, jsonEncode
1018 , webgl, texture, vector2, vector3, vector4, matrix4
11− , canonicalIsKernel
1219 )
1320 where
1421
1522
16−import Prelude hiding (maybe)
1723import Control.Monad (liftM2)
18−import Data.Binary
24+import Data.Binary (Binary(..))
25+import qualified Data.Char as Char
1926import qualified Data.Name as Name
27+import qualified Data.Text as Text
28+import Prelude hiding (maybe)
29+import qualified System.FilePath as FP
2030
2131import qualified Elm.Package as Pkg
32+import qualified Json.Decode as Decode
33+import qualified Json.Encode as Encode
34+
35+
36+
37+-- RAW
38+
39+
40+type Raw = Name.Name
41+
42+
43+nameToString :: Raw -> String
44+nameToString =
45+ Name.toString
46+
47+
48+nameToSlashPath :: Raw -> FilePath
49+nameToSlashPath name =
50+ map (\c -> if c == '.' then FP.pathSeparator else c) (Name.toString name)
51+
52+
53+nameToHyphenPath :: Raw -> FilePath
54+nameToHyphenPath name =
55+ map (\c -> if c == '.' then '-' else c) (Name.toString name)
56+
57+
58+fromHyphenPath :: Text.Text -> Maybe Raw
59+fromHyphenPath txt =
60+ let str = Text.unpack txt in
61+ if all isGoodChunk (splitOn '-' str)
62+ then Just (Name.fromString (map (\c -> if c == '-' then '.' else c) str))
63+ else Nothing
64+
65+
66+
67+-- JSON
2268
2369
70+encode :: Raw -> Encode.Value
71+encode =
72+ Encode.name
2473
25−-- NAMES
74+
75+decoder :: Decode.Decoder Text.Text Raw
76+decoder =
77+ do txt <- Decode.text
78+ let str = Text.unpack txt
79+ if all isGoodChunk (splitOn '.' str)
80+ then Decode.succeed (Name.fromString str)
81+ else Decode.fail txt
82+
83+
84+isGoodChunk :: String -> Bool
85+isGoodChunk chunk =
86+ case chunk of
87+ [] ->
88+ False
89+
90+ first : rest ->
91+ Char.isUpper first && all isGoodChar rest
92+
93+
94+isGoodChar :: Char -> Bool
95+isGoodChar c =
96+ Char.isAlphaNum c || c == '_'
97+
98+
99+splitOn :: Char -> String -> [String]
100+splitOn sep str =
101+ uncurry (:) (splitOnHelp sep str)
102+
103+
104+splitOnHelp :: Char -> String -> (String, [String])
105+splitOnHelp sep str =
106+ case str of
107+ [] ->
108+ ("",[])
109+
110+ c : rest ->
111+ let
112+ (chunk,chunks) = splitOnHelp sep rest
113+ in
114+ if c == sep then
115+ ("", chunk:chunks)
116+ else
117+ (c:chunk, chunks)
118+
119+
120+
121+-- CANONICAL
26122
27123
28124data Canonical =
@@ -33,18 +129,32 @@ data Canonical =
33129 deriving (Ord)
34130
35131
132+canonicalIsKernel :: Canonical -> Bool
133+canonicalIsKernel (Canonical _ name) =
134+ Name.isKernel name
135+
136+
137+
138+-- INSTANCES
139+
140+
36141instance Eq Canonical where
37− (==) (Canonical pkg home) (Canonical pkg' home') =
38− home == home' && pkg == pkg'
142+ (==) (Canonical pkg1 name1) (Canonical pkg2 name2) =
143+ name1 == name2 && pkg1 == pkg2
144+
145+
146+instance Binary Canonical where
147+ put (Canonical a b) = put a >> put b
148+ get = liftM2 Canonical get get
39149
40150
41151
42−-- PRIMITIVES
152+-- CORE
43153
44154
45155{-# NOINLINE basics #-}
46156basics :: Canonical
47−basics = Canonical Pkg.core "Basics"
157+basics = Canonical Pkg.core Name.basics
48158
49159
50160{-# NOINLINE char #-}
@@ -57,10 +167,6 @@ string :: Canonical
57167string = Canonical Pkg.core Name.string
58168
59169
60−
61−-- CONTAINERS
62−
63−
64170{-# NOINLINE maybe #-}
65171maybe :: Canonical
66172maybe = Canonical Pkg.core Name.maybe
@@ -91,10 +197,6 @@ tuple :: Canonical
91197tuple = Canonical Pkg.core Name.tuple
92198
93199
94−
95−-- EFFECTS
96−
97−
98200{-# NOINLINE platform #-}
99201platform :: Canonical
100202platform = Canonical Pkg.core Name.platform
@@ -110,15 +212,6 @@ sub :: Canonical
110212sub = Canonical Pkg.core "Platform.Sub"
111213
112214
113−
114−-- MISC
115−
116−
117−{-# NOINLINE virtualDom #-}
118−virtualDom :: Canonical
119−virtualDom = Canonical Pkg.virtualDom Name.virtualDom
120−
121−
122215{-# NOINLINE debug #-}
123216debug :: Canonical
124217debug = Canonical Pkg.core Name.debug
@@ -130,6 +223,15 @@ bitwise = Canonical Pkg.core Name.bitwise
130223
131224
132225
226+-- HTML
227+
228+
229+{-# NOINLINE virtualDom #-}
230+virtualDom :: Canonical
231+virtualDom = Canonical Pkg.virtualDom Name.virtualDom
232+
233+
234+
133235-- JSON
134236
135237
@@ -175,24 +277,3 @@ vector4 = Canonical Pkg.linearAlgebra "Math.Vector4"
175277{-# NOINLINE matrix4 #-}
176278matrix4 :: Canonical
177279matrix4 = Canonical Pkg.linearAlgebra "Math.Matrix4"
178−
179−
180−
181−-- IS KERNEL
182−
183−
184−canonicalIsKernel :: Canonical -> Bool
185−canonicalIsKernel (Canonical _ name) =
186− Name.isKernel name
187−
188−
189−
190−-- BINARY
191−
192−
193−instance Binary Canonical where
194− put (Canonical a b) =
195− put a >> put b
196−
197− get =
198− liftM2 Canonical get get