on master help sign in
Properly parse out module names, defaulting to Main if no name is given.
ed6b255Evan Czaplicki
compiler/Compiler.hs+15−16
@@ -22,6 +22,7 @@ import qualified Data.ByteString.Lazy as L
22import qualified Metadata.Prelude as Prelude
22import qualified Metadata.Prelude as Prelude
23import qualified Transform.Canonicalize as Canonical
23import qualified Transform.Canonicalize as Canonical
24import SourceSyntax.Module
24import SourceSyntax.Module
25+import Parse.Module (getModuleName)
25import Initialize (buildFromSource, getSortedDependencies)
26import Initialize (buildFromSource, getSortedDependencies)
26import Generate.JavaScript (jsModule)
27import Generate.JavaScript (jsModule)
27import Generate.Html (createHtml, JSStyle(..), JSSource(..))
28import Generate.Html (createHtml, JSStyle(..), JSSource(..))
@@ -96,7 +97,7 @@ elmi :: Flags -> FilePath -> FilePath
96elmi flags filePath = cachePath flags filePath "elmi"
97elmi flags filePath = cachePath flags filePath "elmi"
97
98
98
99
99−buildFile :: Flags -> Int -> Int -> Interfaces -> FilePath -> IO ModuleInterface
100+buildFile :: Flags -> Int -> Int -> Interfaces -> FilePath -> IO (String,ModuleInterface)
100buildFile flags moduleNum numModules interfaces filePath =
101buildFile flags moduleNum numModules interfaces filePath =
101 do compiled <- alreadyCompiled
102 do compiled <- alreadyCompiled
102 case compiled of
103 case compiled of
@@ -104,13 +105,11 @@ buildFile flags moduleNum numModules interfaces filePath =
104 True -> do
105 True -> do
105 handle <- openBinaryFile (elmi flags filePath) ReadMode
106 handle <- openBinaryFile (elmi flags filePath) ReadMode
106 bits <- L.hGetContents handle
107 bits <- L.hGetContents handle
108+ let info :: (String, ModuleInterface)
107− let iface = getInterface (Binary.decode bits)
109+ info = Binary.decode bits
108 L.length bits `seq` hClose handle
110 L.length bits `seq` hClose handle
109− return iface
111+ return info
110 where
112 where
111− getInterface :: (String, ModuleInterface) -> ModuleInterface
112− getInterface = snd
113−
114 alreadyCompiled :: IO Bool
113 alreadyCompiled :: IO Bool
115 alreadyCompiled = do
114 alreadyCompiled = do
116 existsi <- doesFileExist (elmi flags filePath)
115 existsi <- doesFileExist (elmi flags filePath)
@@ -124,15 +123,16 @@ buildFile flags moduleNum numModules interfaces filePath =
124 number :: String
123 number :: String
125 number = "[" ++ show moduleNum ++ " of " ++ show numModules ++ "]"
124 number = "[" ++ show moduleNum ++ " of " ++ show numModules ++ "]"
126
125
127− name :: String
128− name = List.intercalate "." (splitDirectories (dropExtensions filePath))
129−
130− compile :: IO ModuleInterface
126+ compile :: IO (String,ModuleInterface)
131 compile = do
127 compile = do
128+ source <- readFile filePath
129+ let name = case getModuleName source of
130+ Just n -> n
131+ Nothing -> "Name"
132 putStrLn $ concat [ number, " Compiling ", name
132 putStrLn $ concat [ number, " Compiling ", name
133 , replicate (max 1 (20 - length name)) ' '
133 , replicate (max 1 (20 - length name)) ' '
134 , "( " ++ filePath ++ " )" ]
134 , "( " ++ filePath ++ " )" ]
135− source <- readFile filePath
135+
136 createDirectoryIfMissing True (cache_dir flags)
136 createDirectoryIfMissing True (cache_dir flags)
137 createDirectoryIfMissing True (build_dir flags)
137 createDirectoryIfMissing True (build_dir flags)
138 metaModule <-
138 metaModule <-
@@ -158,7 +158,7 @@ buildFile flags moduleNum numModules interfaces filePath =
158 L.hPut handle (Binary.encode (name,interface))
158 L.hPut handle (Binary.encode (name,interface))
159 hClose handle
159 hClose handle
160 writeFile (elmo flags filePath) (jsModule metaModule)
160 writeFile (elmo flags filePath) (jsModule metaModule)
161− return interface
161+ return (name,interface)
162
162
163printTypes metaModule = do
163printTypes metaModule = do
164 putStrLn ""
164 putStrLn ""
@@ -207,7 +207,6 @@ build flags rootFile = do
207buildFiles :: Flags -> Int -> Interfaces -> String -> [FilePath] -> IO (String, Interfaces)
207buildFiles :: Flags -> Int -> Interfaces -> String -> [FilePath] -> IO (String, Interfaces)
208buildFiles _ _ interfaces moduleName [] = return (moduleName, interfaces)
208buildFiles _ _ interfaces moduleName [] = return (moduleName, interfaces)
209buildFiles flags numModules interfaces _ (filePath:rest) = do
209buildFiles flags numModules interfaces _ (filePath:rest) = do
210− interface <- buildFile flags (numModules - length rest) numModules interfaces filePath
210+ (name,interface) <- buildFile flags (numModules - length rest) numModules interfaces filePath
211− let moduleName = List.intercalate "." . splitDirectories $ dropExtensions filePath
212− interfaces' = Map.insert moduleName interface interfaces
211+ let interfaces' = Map.insert name interface interfaces
213− buildFiles flags numModules interfaces' moduleName rest
212+ buildFiles flags numModules interfaces' name rest
compiler/Language/Elm.hs+2−11
@@ -17,11 +17,9 @@ import Data.Version (showVersion)
17import Generate.JavaScript (jsModule)
17import Generate.JavaScript (jsModule)
18import Generate.Html (generateHtml)
18import Generate.Html (generateHtml)
19import Initialize (buildFromSource)
19import Initialize (buildFromSource)
20−import Parse.Helpers
20+import Parse.Module (getModuleName)
21−import Parse.Module (moduleDef)
22import SourceSyntax.Module
21import SourceSyntax.Module
23import Text.Blaze.Html (Html)
22import Text.Blaze.Html (Html)
24−import Text.Parsec (option,optional)
25import qualified Text.PrettyPrint as P
23import qualified Text.PrettyPrint as P
26import qualified Metadata.Prelude as Prelude
24import qualified Metadata.Prelude as Prelude
27import Paths_Elm
25import Paths_Elm
@@ -36,14 +34,7 @@ compile source =
36
34
37-- |This function extracts the module name of a given source program.
35-- |This function extracts the module name of a given source program.
38moduleName :: String -> Maybe String
36moduleName :: String -> Maybe String
39−moduleName source = case iParse getModuleName "" source of
37+moduleName = getModuleName
40− Right name -> Just name
41− Left _ -> Nothing
42− where
43− getModuleName = do
44− optional freshLine
45− (names, _) <- moduleDef
46− return (List.intercalate "." names)
47
38
48-- |This function compiles Elm code into a full HTML page.
39-- |This function compiles Elm code into a full HTML page.
49toHtml :: String -- ^ Location of elm-min.js as expected by the browser
40toHtml :: String -- ^ Location of elm-min.js as expected by the browser
compiler/Parse/Module.hs+12−1
@@ -1,5 +1,5 @@
1
1
2−module Parse.Module (moduleDef, imports) where
2+module Parse.Module (moduleDef, getModuleName, imports) where
3
3
4import Control.Applicative ((<$>), (<*>))
4import Control.Applicative ((<$>), (<*>))
5import Data.List (intercalate)
5import Data.List (intercalate)
@@ -11,6 +11,17 @@ import SourceSyntax.Module (Module(..), ImportMethod(..), Imports)
11varList :: IParser [String]
11varList :: IParser [String]
12varList = parens $ commaSep1 (var <|> parens symOp)
12varList = parens $ commaSep1 (var <|> parens symOp)
13
13
14+getModuleName :: String -> Maybe String
15+getModuleName source =
16+ case iParse getModuleName "" source of
17+ Right name -> Just name
18+ Left _ -> Nothing
19+ where
20+ getModuleName = do
21+ optional freshLine
22+ (names, _) <- moduleDef
23+ return (intercalate "." names)
24+
14moduleDef :: IParser ([String], [String])
25moduleDef :: IParser ([String], [String])
15moduleDef = do
26moduleDef = do
16 try (reserved "module")
27 try (reserved "module")