-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPackage.hs
More file actions
432 lines (305 loc) · 8.32 KB
/
Package.hs
File metadata and controls
432 lines (305 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE BangPatterns, EmptyDataDecls, FlexibleInstances, UnboxedTuples #-}
module Elm.Package
( Name(..)
, Author
, Project
, Canonical(..)
, isKernel
, toChars
, toUrl
, toFilePath
, toJsonString
--
, dummyName, kernel, core
, browser, virtualDom, html
, json, http, url
, webgl, linearAlgebra
--
, suggestions
, nearbyNames
--
, decoder
, encode
, keyDecoder
--
, parser
-- @LAMDERA
, authorLamdera
, lamderaCore
, lamderaCodecs
, lamderaContainers
, time
, bytes
, toName
)
where
import Control.Monad (liftM2)
import Data.Binary (Binary, get, put)
import qualified Data.Coerce as Coerce
import qualified Data.List as List
import qualified Data.Map as Map
import qualified Data.Name as Name
import Data.Monoid ((<>))
import qualified Data.Utf8 as Utf8
import Data.Word (Word8)
import Foreign.Ptr (Ptr, plusPtr, minusPtr)
import System.FilePath ((</>))
import qualified Elm.Version as V
import qualified Json.Decode as D
import qualified Json.Encode as E
import qualified Json.String as Json
import qualified Parse.Primitives as P
import Parse.Primitives (Row, Col)
import qualified Reporting.Suggest as Suggest
-- PACKGE NAMES
data Name =
Name
{ _author :: !Author
, _project :: !Project
}
deriving (Ord)
instance Show Name where
-- show = Elm.Package.toChars
show (Elm.Package.Name author project) =
"Name " ++ (quoted . Utf8.toChars) author <> " " <> (quoted . Utf8.toChars) project
quoted :: String -> String
quoted s = "\"" ++ s ++ "\""
type Author = Utf8.Utf8 AUTHOR
type Project = Utf8.Utf8 PROJECT
data AUTHOR
data PROJECT
data Canonical =
Canonical
{ _name :: !Name
, _version :: !V.Version
}
deriving (Ord)
-- HELPERS
isKernel :: Name -> Bool
isKernel (Name author _) =
author == elm || author == elm_explorations
|| author == authorLamdera
toChars :: Name -> String
toChars (Name author project) =
Utf8.toChars author <> "/" <> Utf8.toChars project
toUrl :: Name -> String
toUrl (Name author project) =
Utf8.toChars author ++ "/" ++ Utf8.toChars project
toFilePath :: Name -> FilePath
toFilePath (Name author project) =
Utf8.toChars author </> Utf8.toChars project
toJsonString :: Name -> Json.String
toJsonString (Name author project) =
Utf8.join 0x2F {-/-} [ Coerce.coerce author, Coerce.coerce project ]
-- COMMON PACKAGE NAMES
toName :: Author -> [Char] -> Name
toName author project =
Name author (Utf8.fromChars project)
{-# NOINLINE dummyName #-}
dummyName :: Name
dummyName =
toName (Utf8.fromChars "author") "project"
{-# NOINLINE kernel #-}
kernel :: Name
kernel =
toName elm "kernel"
{-# NOINLINE core #-}
core :: Name
core =
toName elm "core"
{-# NOINLINE browser #-}
browser :: Name
browser =
toName elm "browser"
{-# NOINLINE virtualDom #-}
virtualDom :: Name
virtualDom =
toName elm "virtual-dom"
{-# NOINLINE html #-}
html :: Name
html =
toName elm "html"
{-# NOINLINE json #-}
json :: Name
json =
toName elm "json"
{-# NOINLINE http #-}
http :: Name
http =
toName elm "http"
{-# NOINLINE url #-}
url :: Name
url =
toName elm "url"
{-# NOINLINE webgl #-}
webgl :: Name
webgl =
toName elm_explorations "webgl"
{-# NOINLINE linearAlgebra #-}
linearAlgebra :: Name
linearAlgebra =
toName elm_explorations "linear-algebra"
{-# NOINLINE elm #-}
elm :: Author
elm =
Utf8.fromChars "elm"
{-# NOINLINE elm_explorations #-}
elm_explorations :: Author
elm_explorations =
Utf8.fromChars "elm-explorations"
-- PACKAGE SUGGESTIONS
suggestions :: Map.Map Name.Name Name
suggestions =
let
random = toName elm "random"
time = toName elm "time"
file = toName elm "file"
in
Map.fromList
[ "Browser" ==> browser
, "File" ==> file
, "File.Download" ==> file
, "File.Select" ==> file
, "Html" ==> html
, "Html.Attributes" ==> html
, "Html.Events" ==> html
, "Http" ==> http
, "Json.Decode" ==> json
, "Json.Encode" ==> json
, "Random" ==> random
, "Time" ==> time
, "Url.Parser" ==> url
, "Url" ==> url
, "Lamdera" ==> lamderaCore
, "Lamdera.Debug" ==> lamderaCore
, "Lamdera.Wire3" ==> lamderaCodecs
]
(==>) :: [Char] -> Name -> (Name.Name, Name)
(==>) moduleName package =
( Utf8.fromChars moduleName, package )
-- NEARBY NAMES
nearbyNames :: Name -> [Name] -> [Name]
nearbyNames (Name author1 project1) possibleNames =
let
authorDist = authorDistance (Utf8.toChars author1)
projectDist = projectDistance (Utf8.toChars project1)
nameDistance (Name author2 project2) =
authorDist author2 + projectDist project2
in
take 4 $ List.sortOn nameDistance possibleNames
authorDistance :: [Char] -> Author -> Int
authorDistance given possibility =
if possibility == elm || possibility == elm_explorations
then 0
else abs (Suggest.distance given (Utf8.toChars possibility))
projectDistance :: [Char] -> Project -> Int
projectDistance given possibility =
abs (Suggest.distance given (Utf8.toChars possibility))
-- INSTANCES
instance Eq Name where
(==) (Name author1 project1) (Name author2 project2) =
project1 == project2 && author1 == author2
instance Eq Canonical where
(==) (Canonical package1 version1) (Canonical package2 version2) =
version1 == version2 && package1 == package2
-- BINARY
instance Binary Name where -- PERF try storing as a Word16
get = liftM2 Name Utf8.getUnder256 Utf8.getUnder256
put (Name a b) = Utf8.putUnder256 a >> Utf8.putUnder256 b
instance Binary Canonical where
get = liftM2 Canonical get get
put (Canonical a b) = put a >> put b
-- JSON
decoder :: D.Decoder (Row, Col) Name
decoder =
D.customString parser (,)
encode :: Name -> E.Value
encode name =
E.chars (toChars name)
keyDecoder :: (Row -> Col -> x) -> D.KeyDecoder x Name
keyDecoder toError =
let
keyParser =
P.specialize (\(r,c) _ _ -> toError r c) parser
in
D.KeyDecoder keyParser toError
-- PARSER
parser :: P.Parser (Row, Col) Name
parser =
do author <- parseName isAlphaOrDigit isAlphaOrDigit
P.word1 0x2F {-/-} (,)
project <- parseName isLower isLowerOrDigit
return (Name author project)
parseName :: (Word8 -> Bool) -> (Word8 -> Bool) -> P.Parser (Row, Col) (Utf8.Utf8 t)
parseName isGoodStart isGoodInner =
P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
if pos >= end then
eerr row col (,)
else
let !word = P.unsafeIndex pos in
if not (isGoodStart word) then
eerr row col (,)
else
let
(# isGood, newPos #) = chompName isGoodInner (plusPtr pos 1) end False
!len = fromIntegral (minusPtr newPos pos)
!newCol = col + len
in
if isGood && len < 256 then
let !newState = P.State src newPos end indent row newCol in
cok (Utf8.fromPtr pos newPos) newState
else
cerr row newCol (,)
isLower :: Word8 -> Bool
isLower word =
0x61 {-a-} <= word && word <= 0x7A {-z-}
isLowerOrDigit :: Word8 -> Bool
isLowerOrDigit word =
0x61 {-a-} <= word && word <= 0x7A {-z-}
|| 0x30 {-0-} <= word && word <= 0x39 {-9-}
isAlphaOrDigit :: Word8 -> Bool
isAlphaOrDigit word =
0x61 {-a-} <= word && word <= 0x7A {-z-}
|| 0x41 {-A-} <= word && word <= 0x5A {-Z-}
|| 0x30 {-0-} <= word && word <= 0x39 {-9-}
chompName :: (Word8 -> Bool) -> Ptr Word8 -> Ptr Word8 -> Bool -> (# Bool, Ptr Word8 #)
chompName isGoodChar pos end prevWasDash =
if pos >= end then
(# not prevWasDash, pos #)
else
let !word = P.unsafeIndex pos in
if isGoodChar word then
chompName isGoodChar (plusPtr pos 1) end False
else if word == 0x2D {---} then
if prevWasDash then
(# False, pos #)
else
chompName isGoodChar (plusPtr pos 1) end True
else
(# True, pos #)
-- @LAMDERA
{-# NOINLINE authorLamdera #-}
authorLamdera :: Author
authorLamdera =
Utf8.fromChars "lamdera"
{-# NOINLINE lamderaCore #-}
lamderaCore :: Name
lamderaCore =
toName (Utf8.fromChars "lamdera") "core"
{-# NOINLINE lamderaCodecs #-}
lamderaCodecs :: Name
lamderaCodecs =
toName (Utf8.fromChars "lamdera") "codecs"
{-# NOINLINE lamderaContainers #-}
lamderaContainers :: Name
lamderaContainers =
toName (Utf8.fromChars "lamdera") "containers"
{-# NOINLINE time #-}
time :: Name
time =
toName elm "time"
{-# NOINLINE bytes #-}
bytes :: Name
bytes =
toName elm "bytes"