on 26bf33f help sign in
elk/src/Elm/Compiler/Module.hs
125 lines 2.4 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Module
4 -- interfaces
5 ( Module.Interface
6 , Module.Interfaces
7 , Module.privatize
8 , interfaceAliasedTypes
9 , programTypes
11 -- module names
12 , ModuleName.Raw
13 , nameToPath
14 , nameToString
15 , nameFromText
16 , hyphenate
17 , dehyphenate
18 , encode
19 , decoder
21 -- canonical names
22 , ModuleName.Canonical(..)
23 )
24 where
26import qualified Data.Char as Char
27import qualified Data.List as List
28import qualified Data.Map as Map
29import qualified Data.Text as Text
30import Data.Text (Text)
31import System.FilePath ((</>))
33import qualified AST.Module as Module
34import qualified AST.Module.Name as ModuleName
35import qualified Elm.Compiler.Type as PublicType
36import qualified Elm.Compiler.Type.Extract as Extract
37import qualified Json.Decode as Decode
38import qualified Json.Encode as Encode
42-- INTERFACES
45interfaceAliasedTypes :: Module.Interface -> Map.Map Text PublicType.Type
46interfaceAliasedTypes interface =
47 Map.map Extract.extract (Module.iTypes interface)
50programTypes :: Module.Interfaces -> ModuleName.Canonical -> Maybe PublicType.Program
51programTypes =
52 Extract.extractProgram
56-- NAMES
59nameToPath :: ModuleName.Raw -> FilePath
60nameToPath name =
61 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
64nameToString :: ModuleName.Raw -> String
65nameToString name =
66 Text.unpack name
69nameFromText :: Text -> Maybe ModuleName.Raw
70nameFromText =
71 fromText '.'
74hyphenate :: ModuleName.Raw -> Text
75hyphenate name =
76 Text.replace "." "-" name
79dehyphenate :: Text -> Maybe ModuleName.Raw
80dehyphenate =
81 fromText '-'
84fromText :: Char -> Text -> Maybe ModuleName.Raw
85fromText sep name =
86 let
87 chunks =
88 Text.splitOn (Text.singleton sep) name
89 in
90 if all isGoodChunk chunks then Just name else Nothing
93isGoodChunk :: Text -> Bool
94isGoodChunk chunk =
95 case Text.uncons chunk of
96 Nothing ->
97 False
99 Just (first, rest) ->
100 Char.isUpper first && Text.all isGoodChar rest
103isGoodChar :: Char -> Bool
104isGoodChar char =
105 Char.isAlphaNum char || char == '_'
109-- JSON
112encode :: ModuleName.Raw -> Encode.Value
113encode =
114 Encode.text
117decoder :: Decode.Decoder ModuleName.Raw
118decoder =
119 do txt <- Decode.text
120 case nameFromText txt of
121 Nothing ->
122 Decode.fail "a module name like \"Html.Events\""
124 Just name ->
125 Decode.succeed name