on c19ccfe help sign in
60 lines 2.3 kB history raw
1{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
2{- | This module exports the functions necessary for compiling Elm code into the
3 respective HTML, CSS, and JS code.
5 The documentation for the Elm language is available at
6 <http://elm-lang.org/Documentation.elm>, and many interactive examples are
7 available at <http://elm-lang.org/Examples.elm>
9 Example implementations using Yesod and Happstack are available
10 at <https://github.com/tazjin/Elm/tree/master/Examples>
11-}
12module Language.Elm (compile, toHtml, moduleName, runtime, docs) where
14import qualified Data.List as List
15import qualified Data.Map as Map
16import Data.Version (showVersion)
17import Generate.JavaScript (showErr, jsModule)
18import Generate.Html (generateHtml)
19import Initialize (buildFromSource)
20import Parse.Helpers
21import Parse.Module (moduleDef)
22import SourceSyntax.Module
23import Text.Blaze.Html (Html)
24import Text.Parsec (option,optional)
25import qualified Text.PrettyPrint as P
26import Paths_Elm
28-- |This function compiles Elm code to JavaScript. It will return either
29-- an error message or the compiled JS code.
30compile :: String -> Either String String
31compile source =
32 case buildFromSource Map.empty source of
33 Left docs -> Left . unlines $ map P.render docs
34 Right modul -> Right $ jsModule (modul :: MetadataModule () ())
36-- |This function extracts the module name of a given source program.
37moduleName :: String -> Maybe String
38moduleName source = case iParse getModuleName "" source of
39 Right name -> Just name
40 Left _ -> Nothing
41 where
42 getModuleName = do
43 optional freshLine
44 (names, _) <- moduleDef
45 return (List.intercalate "." names)
47-- |This function compiles Elm code into a full HTML page.
48toHtml :: String -- ^ Location of elm-min.js as expected by the browser
49 -> String -- ^ The page title
50 -> String -- ^ The elm source code
51 -> Html
52toHtml = generateHtml
54-- |The absolute path to Elm's runtime system.
55runtime :: IO FilePath
56runtime = getDataFileName "elm-runtime.js"
58-- |The absolute path to Elm's core library documentation.
59docs :: IO FilePath
60docs = getDataFileName "docs.json"