on 4bec0ec help sign in
elk/src/Elm/Compiler/Module.hs
144 lines 3.0 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 , defaultImports
10 , interfaceAliasedTypes, programTypes
11 , ModuleName.Canonical(..), qualifiedVar
12 )
13 where
15import qualified Data.Aeson as Json
16import qualified Data.Char as Char
17import qualified Data.List as List
18import qualified Data.Map as Map
19import qualified Data.Text as Text
20import System.FilePath ((</>))
22import qualified AST.Module as Module
23import qualified AST.Module.Name as ModuleName
24import qualified Elm.Compiler.Imports as Imports
25import qualified Elm.Compiler.Type as PublicType
26import qualified Elm.Compiler.Type.Extract as Extract
27import qualified Generate.JavaScript.Variable as Gen
31-- INTERFACES
34type Interface = Module.Interface
37type Interfaces = Module.Interfaces
40interfaceAliasedTypes :: Interface -> Map.Map String PublicType.Type
41interfaceAliasedTypes interface =
42 Map.map Extract.extract (Module.iTypes interface)
45programTypes :: Interfaces -> ModuleName.Canonical -> Maybe PublicType.Program
46programTypes =
47 Extract.extractProgram
51-- NAMES
54newtype RawForJson = RawForJson ModuleName.Raw
57fromJson :: RawForJson -> ModuleName.Raw
58fromJson (RawForJson raw) =
59 raw
62qualifiedVar :: ModuleName.Canonical -> String -> String
63qualifiedVar =
64 Gen.qualified
67defaultImports :: [ModuleName.Raw]
68defaultImports =
69 map fst Imports.defaults
73-- STRING CONVERSIONS for RAW NAMES
76nameToPath :: ModuleName.Raw -> FilePath
77nameToPath names =
78 List.foldl1 (</>) names
81nameToString :: ModuleName.Raw -> String
82nameToString names =
83 List.intercalate "." names
86nameFromString :: String -> Maybe ModuleName.Raw
87nameFromString =
88 fromString '.'
91hyphenate :: ModuleName.Raw -> String
92hyphenate names =
93 List.intercalate "-" names
96dehyphenate :: String -> Maybe ModuleName.Raw
97dehyphenate =
98 fromString '-'
101fromString :: Char -> String -> Maybe ModuleName.Raw
102fromString sep raw =
103 mapM isLegit names
104 where
105 names =
106 filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
108 isLegit name =
109 case name of
110 [] -> Nothing
111 char:rest ->
112 if Char.isUpper char && all legitChar rest
113 then Just name
114 else Nothing
116 legitChar char =
117 Char.isAlphaNum char || char `elem` "_'"
121-- JSON for NAME
124instance Json.ToJSON RawForJson where
125 toJSON (RawForJson name) =
126 Json.toJSON (nameToString name)
129instance Json.FromJSON RawForJson where
130 parseJSON (Json.String text) =
131 let
132 rawString =
133 Text.unpack text
134 in
135 case nameFromString rawString of
136 Nothing ->
137 fail (rawString ++ " is not a valid module name")
139 Just name ->
140 return (RawForJson name)
142 parseJSON _ =
143 fail "expecting the module name to be a string"