on dbe27eb help sign in
elk/src/Elm/Compiler/Module.hs
63 lines 1.5 kB history raw
1module Elm.Compiler.Module
2 ( Interface, Name(Name)
3 , nameToPath, nameToString, nameFromString
4 )
5 where
7import Control.Monad (mzero)
8import qualified Data.Aeson as Json
9import qualified Data.Char as Char
10import qualified Data.List as List
11import qualified Data.Text as Text
12import System.FilePath ((</>))
13import qualified AST.Module as Module
16type Interface = Module.Interface
18newtype Name = Name [String]
19 deriving (Eq, Ord)
22nameToPath :: Name -> FilePath
23nameToPath (Name names) =
24 List.foldl1 (</>) names
27nameToString :: Name -> String
28nameToString (Name names) =
29 List.intercalate "." names
32nameFromString :: String -> Maybe Name
33nameFromString raw =
34 Name `fmap` mapM isLegit names
35 where
36 names =
37 filter (/= ".") (List.groupBy (\a b -> a /= '.' && b /= '.') raw)
39 isLegit name =
40 case name of
41 [] -> Nothing
42 char:rest ->
43 if Char.isUpper char && all legitChar rest
44 then Just name
45 else Nothing
47 legitChar char =
48 Char.isAlphaNum char || char `elem` "_'"
51instance Json.ToJSON Name where
52 toJSON name =
53 Json.toJSON (nameToString name)
56instance Json.FromJSON Name where
57 parseJSON (Json.String text) =
58 let rawName = Text.unpack text in
59 case nameFromString rawName of
60 Nothing -> fail (rawName ++ " is not a valid module name")
61 Just name -> return name
63 parseJSON _ = mzero