on master help sign in
Simplify code for `moduleName` function.
3a36946evancz
compiler/Language/Elm.hs+17−15
@@ -13,33 +13,35 @@
13 Example implementations using Yesod and Happstack are available
13 Example implementations using Yesod and Happstack are available
14 at <https://github.com/tazjin/Elm/tree/master/Examples>
14 at <https://github.com/tazjin/Elm/tree/master/Examples>
15-}
15-}
16−module Language.Elm (
16+module Language.Elm (compile, toHtml, moduleName, runtime, docs) where
17− compile, toHtml,
17+
18− moduleName,
19− runtime, docs
20− ) where
21−
22−import qualified Ast as Ast
23−import qualified Libraries as Libraries
24import Data.List (intercalate)
18import Data.List (intercalate)
25import Data.Version (showVersion)
19import Data.Version (showVersion)
26import CompileToJS (showErr, jsModule)
20import CompileToJS (showErr, jsModule)
27−import ExtractNoscript
28−import GenerateHtml
21+import GenerateHtml (generateHtml)
29−import Initialize
22+import Initialize (buildFromSource)
23+import Parse.Library
24+import Parse.Modules (moduleDef)
30import Text.Blaze.Html (Html)
25import Text.Blaze.Html (Html)
26+import Text.Parsec (option,optional)
31import Paths_Elm
27import Paths_Elm
32
28
33-- |This function compiles Elm code to JavaScript.
29-- |This function compiles Elm code to JavaScript.
34compile :: String -> String
30compile :: String -> String
35compile source = either showErr jsModule modul
31compile source = either showErr jsModule modul
32+ where
36− where modul = buildFromSource True source
33+ modul = buildFromSource True source
37
34
38-- |This function extracts the module name of a given source program.
35-- |This function extracts the module name of a given source program.
39moduleName :: String -> String
36moduleName :: String -> String
40−moduleName source = either (const "Main") getName modul
37+moduleName source = case iParse getModuleName "" source of
41− where modul = buildFromSource False source
38+ Right name -> name
39+ Left _ -> "Main"
40+ where
41+ getModuleName = do
42+ optional freshLine
43+ (names, _) <- option (["Main"],[]) moduleDef
42− getName (Ast.Module names _ _ _) = intercalate "." names
44+ return (intercalate "." names)
43
45
44-- |This function compiles Elm code into a full HTML page.
46-- |This function compiles Elm code into a full HTML page.
45toHtml :: String -- ^ Location of elm-min.js as expected by the browser
47toHtml :: String -- ^ Location of elm-min.js as expected by the browser