on fe7bbbd help sign in
elk/src/Elm/Compiler/Module.hs
150 lines 2.9 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
25 -- variables
26 , Var.Global(..)
27 )
28 where
30import qualified Data.Aeson as Json
31import qualified Data.Char as Char
32import qualified Data.List as List
33import qualified Data.Map as Map
34import qualified Data.Text as Text
35import Data.Text (Text)
36import System.FilePath ((</>))
38import qualified AST.Module as Module
39import qualified AST.Module.Name as ModuleName
40import qualified AST.Variable as Var
41import qualified Elm.Compiler.Type as PublicType
42import qualified Elm.Compiler.Type.Extract as Extract
43import qualified Generate.JavaScript.Variable as Gen
47-- INTERFACES
50interfaceAliasedTypes :: Module.Interface -> Map.Map Text PublicType.Type
51interfaceAliasedTypes interface =
52 Map.map Extract.extract (Module.iTypes interface)
55programTypes :: Module.Interfaces -> ModuleName.Canonical -> Maybe PublicType.Program
56programTypes =
57 Extract.extractProgram
61-- NAMES
64newtype RawForJson = RawForJson ModuleName.Raw
67fromJson :: RawForJson -> ModuleName.Raw
68fromJson (RawForJson raw) =
69 raw
72qualifiedVar :: ModuleName.Canonical -> Text -> Text
73qualifiedVar =
74 Gen.qualified
78-- STRING CONVERSIONS for RAW NAMES
81nameToPath :: ModuleName.Raw -> FilePath
82nameToPath name =
83 List.foldl1 (</>) (map Text.unpack (Text.splitOn "." name))
86nameToString :: ModuleName.Raw -> String
87nameToString name =
88 Text.unpack name
91nameFromText :: Text -> Maybe ModuleName.Raw
92nameFromText =
93 fromText '.'
96hyphenate :: ModuleName.Raw -> Text
97hyphenate name =
98 Text.replace "." "-" name
101dehyphenate :: Text -> Maybe ModuleName.Raw
102dehyphenate =
103 fromText '-'
106fromText :: Char -> Text -> Maybe ModuleName.Raw
107fromText sep name =
108 let
109 chunks =
110 Text.splitOn (Text.singleton sep) name
111 in
112 if all isGoodChunk chunks then Just name else Nothing
115isGoodChunk :: Text -> Bool
116isGoodChunk chunk =
117 case Text.uncons chunk of
118 Nothing ->
119 False
121 Just (first, rest) ->
122 Char.isUpper first && Text.all isGoodChar rest
125isGoodChar :: Char -> Bool
126isGoodChar char =
127 Char.isAlphaNum char || char == '_'
131-- JSON for NAME
134instance Json.ToJSON RawForJson where
135 toJSON (RawForJson name) =
136 Json.toJSON (nameToString name)
139instance Json.FromJSON RawForJson where
140 parseJSON (Json.String text) =
141 case nameFromText text of
142 Nothing ->
143 fail (Text.unpack text ++ " is not a valid module name")
145 Just name ->
146 return (RawForJson name)
148 parseJSON _ =
149 fail "expecting the module name to be a string"