on 46bc422 help sign in
elk/src/Elm/Compiler/Module.hs
136 lines 2.6 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 , RawForJson(..)
19 , fromJson
21 -- canonical names
22 , ModuleName.Canonical(..)
23 )
24 where
26import qualified Data.Aeson as Json
27import qualified Data.Char as Char
28import qualified Data.List as List
29import qualified Data.Map as Map
30import qualified Data.Text as Text
31import Data.Text (Text)
32import System.FilePath ((</>))
34import qualified AST.Module as Module
35import qualified AST.Module.Name as ModuleName
36import qualified Elm.Compiler.Type as PublicType
37import qualified Elm.Compiler.Type.Extract as Extract
41-- INTERFACES
44interfaceAliasedTypes :: Module.Interface -> Map.Map Text PublicType.Type
45interfaceAliasedTypes interface =
46 Map.map Extract.extract (Module.iTypes interface)
49programTypes :: Module.Interfaces -> ModuleName.Canonical -> Maybe PublicType.Program
50programTypes =
51 Extract.extractProgram
55-- NAMES
58nameToPath :: ModuleName.Raw -> FilePath
59nameToPath name =
60 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
63nameToString :: ModuleName.Raw -> String
64nameToString name =
65 Text.unpack name
68nameFromText :: Text -> Maybe ModuleName.Raw
69nameFromText =
70 fromText '.'
73hyphenate :: ModuleName.Raw -> Text
74hyphenate name =
75 Text.replace "." "-" name
78dehyphenate :: Text -> Maybe ModuleName.Raw
79dehyphenate =
80 fromText '-'
83fromText :: Char -> Text -> Maybe ModuleName.Raw
84fromText sep name =
85 let
86 chunks =
87 Text.splitOn (Text.singleton sep) name
88 in
89 if all isGoodChunk chunks then Just name else Nothing
92isGoodChunk :: Text -> Bool
93isGoodChunk chunk =
94 case Text.uncons chunk of
95 Nothing ->
96 False
98 Just (first, rest) ->
99 Char.isUpper first && Text.all isGoodChar rest
102isGoodChar :: Char -> Bool
103isGoodChar char =
104 Char.isAlphaNum char || char == '_'
108-- JSON for NAME
111newtype RawForJson =
112 RawForJson ModuleName.Raw
115fromJson :: RawForJson -> ModuleName.Raw
116fromJson (RawForJson raw) =
117 raw
120instance Json.ToJSON RawForJson where
121 toJSON (RawForJson name) =
122 Json.toJSON (nameToString name)
125instance Json.FromJSON RawForJson where
126 parseJSON (Json.String text) =
127 case nameFromText text of
128 Nothing ->
129 fail (Text.unpack text ++ " is not a valid module name")
131 Just name ->
132 return (RawForJson name)
134 parseJSON _ =
135 fail "expecting the module name to be a string"