elm/compiler/Language/Elm.hs

62 lines
2.3 KiB
Haskell
Raw Normal View History

{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
{- | This module exports the functions necessary for compiling Elm code into the
respective HTML, CSS, and JS code.
The documentation for the Elm language is available at
<http://elm-lang.org/Documentation.elm>, and many interactive examples are
available at <http://elm-lang.org/Examples.elm>
2012-05-30 20:24:33 +00:00
Example implementations using Yesod and Happstack are available
at <https://github.com/tazjin/Elm/tree/master/Examples>
2012-05-28 16:02:02 +00:00
-}
module Language.Elm (compile, toHtml, moduleName, runtime, docs) where
import qualified Data.List as List
import qualified Data.Map as Map
2012-10-03 07:17:09 +00:00
import Data.Version (showVersion)
2013-06-14 05:59:14 +00:00
import Generate.JavaScript (showErr, jsModule)
import Generate.Html (generateHtml)
import Initialize (buildFromSource)
2013-06-14 05:59:14 +00:00
import Parse.Helpers
import Parse.Module (moduleDef)
import SourceSyntax.Module
import Text.Blaze.Html (Html)
import Text.Parsec (option,optional)
import qualified Text.PrettyPrint as P
2013-07-31 16:31:48 +00:00
import qualified Metadata.Prelude as Prelude
2012-10-03 06:47:46 +00:00
import Paths_Elm
-- |This function compiles Elm code to JavaScript. It will return either
-- an error message or the compiled JS code.
compile :: String -> Either String String
compile source =
2013-07-31 16:31:48 +00:00
case buildFromSource False Prelude.interfaces source of
Left docs -> Left . unlines $ map P.render docs
Right modul -> Right $ jsModule (modul :: MetadataModule () ())
-- |This function extracts the module name of a given source program.
moduleName :: String -> Maybe String
moduleName source = case iParse getModuleName "" source of
Right name -> Just name
Left _ -> Nothing
where
getModuleName = do
optional freshLine
(names, _) <- moduleDef
return (List.intercalate "." names)
-- |This function compiles Elm code into a full HTML page.
toHtml :: String -- ^ Location of elm-min.js as expected by the browser
-> String -- ^ The page title
-> String -- ^ The elm source code
-> Html
toHtml = generateHtml
-- |The absolute path to Elm's runtime system.
runtime :: IO FilePath
runtime = getDataFileName "elm-runtime.js"
-- |The absolute path to Elm's core library documentation.
docs :: IO FilePath
docs = getDataFileName "docs.json"