on 96dfd88 help sign in
elk/src/Elm/Compiler.hs
81 lines 2.1 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE FlexibleContexts #-}
3module Elm.Compiler
4 ( version
5 , parseDependencies, compile
6 , runtimeDebugPath
7 ) where
9import Control.Monad.Error (MonadError, throwError)
10import qualified Data.List as List
11import qualified Data.Map as Map
12import qualified Text.PrettyPrint as P
14import qualified AST.Module as Module (HeaderAndImports(HeaderAndImports), toInterface)
15import qualified Compile
16import qualified Elm.Compiler.Module as PublicModule
17import qualified Elm.Compiler.Version as Version
18import Elm.Utils ((|>))
19import qualified Elm.Utils as Utils
20import qualified Generate.JavaScript as JS
21import qualified Parse.Helpers as Help
22import qualified Parse.Module as Parse
23import qualified Paths_elm_compiler as Paths
26-- VERSION
28version :: String
29version =
30 Version.version
33-- DEPENDENCIES
35parseDependencies
36 :: (MonadError String m)
37 => String
38 -> m (PublicModule.Name, [PublicModule.Name])
39parseDependencies src =
40 case Help.iParse Parse.headerAndImports src of
41 Left msg ->
42 throwError (show msg)
44 Right (Module.HeaderAndImports names _exports imports) ->
45 return
46 ( PublicModule.Name names
47 , map (PublicModule.Name . fst) imports
48 )
51-- COMPILATION
53{-| Compiles Elm source code to JavaScript. -}
54compile
55 :: String
56 -> String
57 -> String
58 -> Map.Map PublicModule.Name PublicModule.Interface
59 -> Either String (PublicModule.Interface, String)
60compile user packageName source interfaces =
61 let unwrappedInterfaces =
62 Map.mapKeysMonotonic (\(PublicModule.Name name) -> name) interfaces
63 in
64 case Compile.compile user packageName unwrappedInterfaces source of
65 Right modul ->
66 Right (Module.toInterface modul, JS.generate modul)
68 Left docs ->
69 map P.render docs
70 |> List.intersperse ""
71 |> unlines
72 |> Left
75-- DATA FILES
77{-| Path to the debugger runtime.
78-}
79runtimeDebugPath :: IO FilePath
80runtimeDebugPath =
81 Utils.getAsset "compiler" Paths.getDataFileName "runtime/debug.js"