on 3e2f44c help sign in
elk/src/Elm/Compiler.hs
68 lines 2.0 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE FlexibleContexts #-}
3module Elm.Compiler (version, parseDependencies, compile, runtimePath) where
5import Control.Monad.Error (MonadError, throwError)
6import qualified Data.List as List
7import qualified Data.Map as Map
8import qualified Text.PrettyPrint as P
10import qualified AST.Module as Module (HeaderAndImports(HeaderAndImports), toInterface)
11import qualified Build.Source as Source
12import qualified Elm.Compiler.Module as PublicModule
13import qualified Elm.Compiler.Version as Version
14import qualified Elm.Utils as Utils
15import qualified Generate.JavaScript as JS
16import qualified Parse.Helpers as Help
17import qualified Parse.Module as Parse
18import qualified Paths_elm_compiler as Compiler
21-- VERSION
23version :: String
24version =
25 Version.version
28-- DEPENDENCIES
30parseDependencies
31 :: (MonadError String m)
32 => String
33 -> m (PublicModule.Name, [PublicModule.Name])
34parseDependencies src =
35 case Help.iParse Parse.headerAndImports src of
36 Left msg ->
37 throwError (show msg)
39 Right (Module.HeaderAndImports names _exports imports) ->
40 return
41 ( PublicModule.Name names
42 , map (PublicModule.Name . fst) imports
43 )
46-- COMPILATION
48{-| Compiles Elm source code to JavaScript. -}
49compile
50 :: String
51 -> Map.Map PublicModule.Name PublicModule.Interface
52 -> Either String (PublicModule.Interface, String)
53compile source interfaces =
54 case Source.build False unwrappedInterfaces source of
55 Left docs -> Left . unlines . List.intersperse "" $ map P.render docs
56 Right modul -> Right (Module.toInterface modul, JS.generate modul)
57 where
58 unwrappedInterfaces =
59 Map.mapKeysMonotonic (\(PublicModule.Name name) -> name) interfaces
62-- DATA FILES
64{-| Path to the runtime.
65-}
66runtimePath :: IO FilePath
67runtimePath =
68 Utils.getAsset "compiler" Compiler.getDataFileName "runtime/core.js"