on 44cc85e help sign in
elk/src/Elm/Compiler/Module.hs
146 lines 2.8 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 , qualifiedVar
24 )
25 where
27import qualified Data.Aeson as Json
28import qualified Data.Char as Char
29import qualified Data.List as List
30import qualified Data.Map as Map
31import qualified Data.Text as Text
32import Data.Text (Text)
33import System.FilePath ((</>))
35import qualified AST.Module as Module
36import qualified AST.Module.Name as ModuleName
37import qualified Elm.Compiler.Type as PublicType
38import qualified Elm.Compiler.Type.Extract as Extract
39import qualified Generate.JavaScript.Variable as Gen
43-- INTERFACES
46interfaceAliasedTypes :: Module.Interface -> Map.Map Text PublicType.Type
47interfaceAliasedTypes interface =
48 Map.map Extract.extract (Module.iTypes interface)
51programTypes :: Module.Interfaces -> ModuleName.Canonical -> Maybe PublicType.Program
52programTypes =
53 Extract.extractProgram
57-- NAMES
60nameToPath :: ModuleName.Raw -> FilePath
61nameToPath name =
62 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
65nameToString :: ModuleName.Raw -> String
66nameToString name =
67 Text.unpack name
70nameFromText :: Text -> Maybe ModuleName.Raw
71nameFromText =
72 fromText '.'
75hyphenate :: ModuleName.Raw -> Text
76hyphenate name =
77 Text.replace "." "-" name
80dehyphenate :: Text -> Maybe ModuleName.Raw
81dehyphenate =
82 fromText '-'
85fromText :: Char -> Text -> Maybe ModuleName.Raw
86fromText sep name =
87 let
88 chunks =
89 Text.splitOn (Text.singleton sep) name
90 in
91 if all isGoodChunk chunks then Just name else Nothing
94isGoodChunk :: Text -> Bool
95isGoodChunk chunk =
96 case Text.uncons chunk of
97 Nothing ->
98 False
100 Just (first, rest) ->
101 Char.isUpper first && Text.all isGoodChar rest
104isGoodChar :: Char -> Bool
105isGoodChar char =
106 Char.isAlphaNum char || char == '_'
110-- JSON for NAME
113newtype RawForJson =
114 RawForJson ModuleName.Raw
117fromJson :: RawForJson -> ModuleName.Raw
118fromJson (RawForJson raw) =
119 raw
122instance Json.ToJSON RawForJson where
123 toJSON (RawForJson name) =
124 Json.toJSON (nameToString name)
127instance Json.FromJSON RawForJson where
128 parseJSON (Json.String text) =
129 case nameFromText text of
130 Nothing ->
131 fail (Text.unpack text ++ " is not a valid module name")
133 Just name ->
134 return (RawForJson name)
136 parseJSON _ =
137 fail "expecting the module name to be a string"
141-- CANONICAL NAMES
144qualifiedVar :: ModuleName.Canonical -> Text -> Text
145qualifiedVar =
146 Gen.qualified