on e5093cd help sign in
elk/src/Elm/Compiler/Module.hs
137 lines 2.8 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Elm.Compiler.Module
3 ( Interface, Interfaces
4 , ModuleName.Raw
5 , nameToPath
6 , nameToString, nameFromString
7 , hyphenate, dehyphenate
8 , RawForJson(RawForJson), fromJson
9 , interfaceAliasedTypes, programTypes
10 , ModuleName.Canonical(..), qualifiedVar
11 )
12 where
14import qualified Data.Aeson as Json
15import qualified Data.Char as Char
16import qualified Data.List as List
17import qualified Data.Map as Map
18import qualified Data.Text as Text
19import System.FilePath ((</>))
21import qualified AST.Module as Module
22import qualified AST.Module.Name as ModuleName
23import qualified Elm.Compiler.Type as PublicType
24import qualified Elm.Compiler.Type.Extract as Extract
25import qualified Generate.JavaScript.Variable as Gen
29-- INTERFACES
32type Interface = Module.Interface
35type Interfaces = Module.Interfaces
38interfaceAliasedTypes :: Interface -> Map.Map String PublicType.Type
39interfaceAliasedTypes interface =
40 Map.map Extract.extract (Module.iTypes interface)
43programTypes :: Interfaces -> ModuleName.Canonical -> Maybe PublicType.Program
44programTypes =
45 Extract.extractProgram
49-- NAMES
52newtype RawForJson = RawForJson ModuleName.Raw
55fromJson :: RawForJson -> ModuleName.Raw
56fromJson (RawForJson raw) =
57 raw
60qualifiedVar :: ModuleName.Canonical -> String -> String
61qualifiedVar =
62 Gen.qualified
66-- STRING CONVERSIONS for RAW NAMES
69nameToPath :: ModuleName.Raw -> FilePath
70nameToPath names =
71 List.foldl1 (</>) names
74nameToString :: ModuleName.Raw -> String
75nameToString names =
76 List.intercalate "." names
79nameFromString :: String -> Maybe ModuleName.Raw
80nameFromString =
81 fromString '.'
84hyphenate :: ModuleName.Raw -> String
85hyphenate names =
86 List.intercalate "-" names
89dehyphenate :: String -> Maybe ModuleName.Raw
90dehyphenate =
91 fromString '-'
94fromString :: Char -> String -> Maybe ModuleName.Raw
95fromString sep raw =
96 mapM isLegit names
97 where
98 names =
99 filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
101 isLegit name =
102 case name of
103 [] -> Nothing
104 char:rest ->
105 if Char.isUpper char && all legitChar rest
106 then Just name
107 else Nothing
109 legitChar char =
110 Char.isAlphaNum char || char `elem` "_'"
114-- JSON for NAME
117instance Json.ToJSON RawForJson where
118 toJSON (RawForJson name) =
119 Json.toJSON (nameToString name)
122instance Json.FromJSON RawForJson where
123 parseJSON (Json.String text) =
124 let
125 rawString =
126 Text.unpack text
127 in
128 case nameFromString rawString of
129 Nothing ->
130 fail (rawString ++ " is not a valid module name")
132 Just name ->
133 return (RawForJson name)
135 parseJSON _ =
136 fail "expecting the module name to be a string"