on master help sign in
Properly parse out module names, defaulting to Main if no name is given.
commited6b255ba55ad1b7d54a33839978fa5b46e2cf15 authorEvan Czaplicki <info@elm-lang.org> dateTue 6 Aug 2013, 21:20 (13 years ago) parent43b604e
3 files changed+29−28
compiler/Compiler.hs+15−16 compiler/Language/Elm.hs+2−11 compiler/Parse/Module.hs+12−1
compiler/Compiler.hs+15−16
@@ -22,6 +22,7 @@ import qualified Data.ByteString.Lazy as L
2222import qualified Metadata.Prelude as Prelude
2323import qualified Transform.Canonicalize as Canonical
2424import SourceSyntax.Module
25+import Parse.Module (getModuleName)
2526import Initialize (buildFromSource, getSortedDependencies)
2627import Generate.JavaScript (jsModule)
2728import Generate.Html (createHtml, JSStyle(..), JSSource(..))
@@ -96,7 +97,7 @@ elmi :: Flags -> FilePath -> FilePath
9697elmi flags filePath = cachePath flags filePath "elmi"
9798
9899
99−buildFile :: Flags -> Int -> Int -> Interfaces -> FilePath -> IO ModuleInterface
100+buildFile :: Flags -> Int -> Int -> Interfaces -> FilePath -> IO (String,ModuleInterface)
100101buildFile flags moduleNum numModules interfaces filePath =
101102 do compiled <- alreadyCompiled
102103 case compiled of
@@ -104,13 +105,11 @@ buildFile flags moduleNum numModules interfaces filePath =
104105 True -> do
105106 handle <- openBinaryFile (elmi flags filePath) ReadMode
106107 bits <- L.hGetContents handle
107− let iface = getInterface (Binary.decode bits)
108+ let info :: (String, ModuleInterface)
109+ info = Binary.decode bits
108110 L.length bits `seq` hClose handle
109− return iface
111+ return info
110112 where
111− getInterface :: (String, ModuleInterface) -> ModuleInterface
112− getInterface = snd
113−
114113 alreadyCompiled :: IO Bool
115114 alreadyCompiled = do
116115 existsi <- doesFileExist (elmi flags filePath)
@@ -124,15 +123,16 @@ buildFile flags moduleNum numModules interfaces filePath =
124123 number :: String
125124 number = "[" ++ show moduleNum ++ " of " ++ show numModules ++ "]"
126125
127− name :: String
128− name = List.intercalate "." (splitDirectories (dropExtensions filePath))
129−
130− compile :: IO ModuleInterface
126+ compile :: IO (String,ModuleInterface)
131127 compile = do
128+ source <- readFile filePath
129+ let name = case getModuleName source of
130+ Just n -> n
131+ Nothing -> "Name"
132132 putStrLn $ concat [ number, " Compiling ", name
133133 , replicate (max 1 (20 - length name)) ' '
134134 , "( " ++ filePath ++ " )" ]
135− source <- readFile filePath
135+
136136 createDirectoryIfMissing True (cache_dir flags)
137137 createDirectoryIfMissing True (build_dir flags)
138138 metaModule <-
@@ -158,7 +158,7 @@ buildFile flags moduleNum numModules interfaces filePath =
158158 L.hPut handle (Binary.encode (name,interface))
159159 hClose handle
160160 writeFile (elmo flags filePath) (jsModule metaModule)
161− return interface
161+ return (name,interface)
162162
163163printTypes metaModule = do
164164 putStrLn ""
@@ -207,7 +207,6 @@ build flags rootFile = do
207207buildFiles :: Flags -> Int -> Interfaces -> String -> [FilePath] -> IO (String, Interfaces)
208208buildFiles _ _ interfaces moduleName [] = return (moduleName, interfaces)
209209buildFiles flags numModules interfaces _ (filePath:rest) = do
210− interface <- buildFile flags (numModules - length rest) numModules interfaces filePath
211− let moduleName = List.intercalate "." . splitDirectories $ dropExtensions filePath
212− interfaces' = Map.insert moduleName interface interfaces
213− buildFiles flags numModules interfaces' moduleName rest
210+ (name,interface) <- buildFile flags (numModules - length rest) numModules interfaces filePath
211+ let interfaces' = Map.insert name interface interfaces
212+ buildFiles flags numModules interfaces' name rest
compiler/Language/Elm.hs+2−11
@@ -17,11 +17,9 @@ import Data.Version (showVersion)
1717import Generate.JavaScript (jsModule)
1818import Generate.Html (generateHtml)
1919import Initialize (buildFromSource)
20−import Parse.Helpers
21−import Parse.Module (moduleDef)
20+import Parse.Module (getModuleName)
2221import SourceSyntax.Module
2322import Text.Blaze.Html (Html)
24−import Text.Parsec (option,optional)
2523import qualified Text.PrettyPrint as P
2624import qualified Metadata.Prelude as Prelude
2725import Paths_Elm
@@ -36,14 +34,7 @@ compile source =
3634
3735-- |This function extracts the module name of a given source program.
3836moduleName :: String -> Maybe String
39−moduleName source = case iParse getModuleName "" source of
40− Right name -> Just name
41− Left _ -> Nothing
42− where
43− getModuleName = do
44− optional freshLine
45− (names, _) <- moduleDef
46− return (List.intercalate "." names)
37+moduleName = getModuleName
4738
4839-- |This function compiles Elm code into a full HTML page.
4940toHtml :: String -- ^ Location of elm-min.js as expected by the browser
compiler/Parse/Module.hs+12−1
@@ -1,5 +1,5 @@
11
2−module Parse.Module (moduleDef, imports) where
2+module Parse.Module (moduleDef, getModuleName, imports) where
33
44import Control.Applicative ((<$>), (<*>))
55import Data.List (intercalate)
@@ -11,6 +11,17 @@ import SourceSyntax.Module (Module(..), ImportMethod(..), Imports)
1111varList :: IParser [String]
1212varList = parens $ commaSep1 (var <|> parens symOp)
1313
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+
1425moduleDef :: IParser ([String], [String])
1526moduleDef = do
1627 try (reserved "module")