on master help sign in
Combine AST.Module.Name and Elm.Compiler.Module into Elm.ModuleName
commit0fb8be01acb35f1bca63d442eb92eb8b89b8b302 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 26 Nov 2018, 10:58 (8 years ago) parenta798228
3 files changed+134−169
compiler/src/Elm/Compiler/Module.hs−116 compiler/src/AST/Module/Name.hs → compiler/src/Elm/ModuleName.hs+131−50 elm.cabal+3−3
compiler/src/Elm/Compiler/Module.hsdeleted−116
@@ -1,116 +0,0 @@
1−{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−module Elm.Compiler.Module
4− -- interfaces
5− ( I.Interface
6− , I.Interfaces
7−
8− -- module names
9− , Raw
10− , nameToString
11− , nameToSlashPath
12− , nameToHyphenPath
13− , fromHyphenPath
14− , encode
15− , decoder
16−
17− -- canonical names
18− , ModuleName.Canonical(..)
19− )
20− where
21−
22−
23−import qualified Data.Char as Char
24−import qualified Data.Name as Name
25−import qualified Data.Text as Text
26−import qualified System.FilePath as FP
27−
28−import qualified AST.Module.Name as ModuleName
29−import qualified Elm.Interface as I
30−import qualified Json.Decode as Decode
31−import qualified Json.Encode as Encode
32−
33−
34−
35−-- NAMES
36−
37−
38−type Raw = Name.Name
39−
40−
41−nameToString :: Raw -> String
42−nameToString =
43− Name.toString
44−
45−
46−nameToSlashPath :: Raw -> FilePath
47−nameToSlashPath name =
48− map (\c -> if c == '.' then FP.pathSeparator else c) (Name.toString name)
49−
50−
51−nameToHyphenPath :: Raw -> FilePath
52−nameToHyphenPath name =
53− map (\c -> if c == '.' then '-' else c) (Name.toString name)
54−
55−
56−fromHyphenPath :: Text.Text -> Maybe Raw
57−fromHyphenPath txt =
58− let str = Text.unpack txt in
59− if all isGoodChunk (splitOn '-' str)
60− then Just (Name.fromString (map (\c -> if c == '-' then '.' else c) str))
61− else Nothing
62−
63−
64−
65−-- JSON
66−
67−
68−encode :: Raw -> Encode.Value
69−encode =
70− Encode.name
71−
72−
73−decoder :: Decode.Decoder Text.Text Raw
74−decoder =
75− do txt <- Decode.text
76− let str = Text.unpack txt
77− if all isGoodChunk (splitOn '.' str)
78− then Decode.succeed (Name.fromString str)
79− else Decode.fail txt
80−
81−
82−isGoodChunk :: String -> Bool
83−isGoodChunk chunk =
84− case chunk of
85− [] ->
86− False
87−
88− first : rest ->
89− Char.isUpper first && all isGoodChar rest
90−
91−
92−isGoodChar :: Char -> Bool
93−isGoodChar char =
94− Char.isAlphaNum char || char == '_'
95−
96−
97−splitOn :: Char -> String -> [String]
98−splitOn sep string =
99− uncurry (:) (splitOnHelp sep string)
100−
101−
102−splitOnHelp :: Char -> String -> (String, [String])
103−splitOnHelp sep string =
104− case string of
105− [] ->
106− ("",[])
107−
108− char : rest ->
109− let
110− (chunk,chunks) = splitOnHelp sep rest
111− in
112− if char == sep then
113− ("", chunk:chunks)
114− else
115− (char:chunk, chunks)
116−
compiler/src/AST/Module/Name.hs → compiler/src/Elm/ModuleName.hsrenamed+131−50
@@ -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
elm.cabal+3−3
@@ -130,12 +130,12 @@ Executable elm
130130
131131 -- shared from compiler/
132132 Elm.Compiler
133− Elm.Compiler.Module
134133 Elm.Compiler.Objects
135134 Elm.Compiler.Type
136135 Elm.Docs
137− Elm.Kernel
138136 Elm.Header
137+ Elm.Kernel
138+ Elm.ModuleName
139139 Elm.Package
140140 Json.Decode
141141 Json.Encode
@@ -144,7 +144,6 @@ Executable elm
144144 AST.Canonical
145145 AST.Optimized
146146 AST.Source
147− AST.Module.Name
148147 AST.Utils.Binop
149148 AST.Utils.Shader
150149 AST.Utils.Type
@@ -240,6 +239,7 @@ Executable elm
240239 directory >= 1.2.3.0 && < 2.0,
241240 edit-distance >= 0.2 && < 0.3,
242241 file-embed,
242+ filelock,
243243 filepath >= 1 && < 2.0,
244244 ghc-prim >= 0.5.2,
245245 haskeline,