1- -----------------------------------------------------------------------------
2- --
3- -- Module : Main
4- -- Copyright : (c) Phil Freeman 2013-2015
5- -- License : MIT
6- --
7- 8- -- Stability :
9- -- Portability :
10- --
11- -- |
12- --
13- -----------------------------------------------------------------------------
14-
151{-# LANGUAGE DataKinds #-}
2+ {-# LANGUAGE DeriveAnyClass #-}
3+ {-# LANGUAGE DeriveGeneric #-}
164{-# LANGUAGE OverloadedStrings #-}
175{-# LANGUAGE TupleSections #-}
186
19- module Main (
20- main
21- ) where
7+ module Main (main ) where
228
239import Control.Monad (unless )
2410import Control.Monad.IO.Class (liftIO )
@@ -37,10 +23,12 @@ import qualified Data.Text as T
3723import qualified Data.Text.Encoding as T
3824import qualified Data.Text.Lazy as TL
3925import Data.Traversable (for )
26+ import GHC.Generics (Generic )
4027import qualified Language.PureScript as P
4128import qualified Language.PureScript.Bundle as Bundle
4229import qualified Language.PureScript.CodeGen.JS as J
4330import qualified Language.PureScript.CoreFn as CF
31+ import qualified Language.PureScript.Errors.JSON as P
4432import qualified Language.PureScript.Interactive as I
4533import System.Environment (getArgs )
4634import System.Exit (exitFailure )
@@ -51,18 +39,25 @@ import System.IO.UTF8 (readUTF8File)
5139import Web.Scotty
5240import qualified Web.Scotty as Scotty
5341
54- type JS = String
42+ type JS = Text
43+
44+ data Error
45+ = CompilerErrors [P. JSONError ]
46+ | OtherError Text
47+ deriving Generic
48+
49+ instance A. ToJSON Error
5550
5651server :: TL. Text -> [P. ExternsFile ] -> P. Environment -> Int -> IO ()
5752server bundled externs initEnv port = do
58- let compile :: Text -> IO (Either String JS )
53+ let compile :: Text -> IO (Either Error JS )
5954 compile input
60- | T. length input > 20000 = return $ Left " Please limit your input to 20000 characters"
55+ | T. length input > 20000 = return ( Left ( OtherError " Please limit your input to 20000 characters" ))
6156 | otherwise = do
6257 let printErrors = P. prettyPrintMultipleErrors (P. defaultPPEOptions { P. ppeCodeColor = Nothing })
6358 case P. parseModuleFromFile (const " <file>" ) (undefined , input) of
6459 Left parseError ->
65- return . Left . printErrors . P. MultipleErrors . return . P. toPositionedError $ parseError
60+ return . Left . CompilerErrors . pure . P. toJSONError False P. Error . P. toPositionedError $ parseError
6661 Right (_, m) | P. getModuleName m == P. ModuleName [P. ProperName " Main" ] -> do
6762 (resultMay, _) <- runLogger' . runExceptT . flip runReaderT P. defaultOptions $ do
6863 ((P. Module ss coms moduleName elaborated exps, env), nextVar) <- P. runSupplyT 0 $ do
@@ -75,9 +70,9 @@ server bundled externs initEnv port = do
7570 unless (null . CF. moduleForeign $ renamed) . throwError . P. errorMessage $ P. MissingFFIModule moduleName
7671 P. evalSupplyT nextVar $ P. prettyPrintJS <$> J. moduleToJs renamed Nothing
7772 case resultMay of
78- Left errs -> return . Left . printErrors $ errs
79- Right js -> return ( Right js)
80- Right _ -> return $ Left " The name of the main module should be Main."
73+ Left errs -> ( return . Left . CompilerErrors . P. toJSONErrors False P. Error ) errs
74+ Right js -> ( return . Right ) js
75+ Right _ -> ( return . Left . OtherError ) " The name of the main module should be Main."
8176
8277 scotty port $ do
8378 get " /" $
0 commit comments