-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathMain.hs
257 lines (227 loc) · 10.3 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards #-}
module Main (main) where
import Control.Monad (unless, foldM)
import Control.Monad.Error.Class (throwError)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Logger (runLogger')
import qualified Control.Monad.State as State
import Control.Monad.Trans (lift)
import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
import Control.Monad.Trans.Reader (runReaderT)
import Control.Monad.Writer.Strict (runWriterT)
import qualified Data.Aeson as A
import Data.Aeson ((.=))
import Data.Bifunctor (first, second, bimap)
import qualified Data.ByteString.Lazy as BL
import Data.Default (def)
import Data.Function (on)
import qualified Data.IORef as IORef
import Data.List (nubBy)
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as M
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Time.Clock (UTCTime)
import GHC.Generics (Generic)
import qualified Language.PureScript as P
import qualified Language.PureScript.CST as CST
import qualified Language.PureScript.CST.Monad as CSTM
import qualified Language.PureScript.CodeGen.JS as J
import qualified Language.PureScript.CodeGen.JS.Printer as P
import qualified Language.PureScript.CoreFn as CF
import qualified Language.PureScript.Docs.Types as Docs
import qualified Language.PureScript.Errors.JSON as P
import qualified Language.PureScript.Interactive as I
import qualified Language.PureScript.Make as Make
import qualified Language.PureScript.Make.Cache as Cache
import qualified Language.PureScript.Names as N
import qualified Language.PureScript.TypeChecker.TypeSearch as TS
import qualified Network.Wai.Handler.Warp as Warp
import System.Environment (getArgs)
import System.Exit (exitFailure)
import System.FilePath.Glob (glob)
import qualified System.IO as IO
import Web.Scotty
import qualified Web.Scotty as Scotty
type JS = Text
data Error
= CompilerErrors [P.JSONError]
| OtherError Text
deriving Generic
instance A.ToJSON Error
toCompilerErrors :: NE.NonEmpty CST.ParserError -> Error
toCompilerErrors = CompilerErrors . toJsonErrors . CST.toMultipleErrors "<file>"
toJsonErrors :: P.MultipleErrors -> [P.JSONError]
toJsonErrors = P.toJSONErrors False P.Error []
-- As of PureScript 0.14 we only need the `codegen` part of `MakeActions` to run
-- Try PureScript, because we already know all dependencies are compiled, we're
-- only building one module, we don't allow FFI declarations, and we want to
-- avoid writing to the file system as much as possible.
buildMakeActions :: IORef.IORef (Maybe JS) -> Make.MakeActions Make.Make
buildMakeActions codegenRef =
Make.MakeActions
getInputTimestampsAndHashes
getOutputTimestamp
readExterns
codegen
ffiCodegen
progress
readCacheDb
writeCacheDb
writePackageJson
outputPrimDocs
where
getInputTimestampsAndHashes :: P.ModuleName -> Make.Make (Either Make.RebuildPolicy (M.Map FilePath (UTCTime, Make.Make Cache.ContentHash)))
getInputTimestampsAndHashes _ = pure $ Right M.empty
getOutputTimestamp :: P.ModuleName -> Make.Make (Maybe UTCTime)
getOutputTimestamp _ = pure Nothing
readExterns :: P.ModuleName -> Make.Make (FilePath, Maybe P.ExternsFile)
readExterns _ = pure ("<file>", Nothing)
codegen :: CF.Module CF.Ann -> Docs.Module -> P.ExternsFile -> P.SupplyT Make.Make ()
codegen m _ _ = do
rawJs <- J.moduleToJs m Nothing
lift $ liftIO $ IORef.writeIORef codegenRef $ Just $ P.prettyPrintJS rawJs
-- If we ever support FFI implementations in Try PureScript then we will need
-- to implement this function. However, we do not plan to support this feature.
ffiCodegen :: CF.Module CF.Ann -> Make.Make ()
ffiCodegen _ = pure ()
progress :: Make.ProgressMessage -> Make.Make ()
progress _ = pure ()
readCacheDb :: Make.Make Cache.CacheDb
readCacheDb = pure M.empty
writeCacheDb :: Cache.CacheDb -> Make.Make ()
writeCacheDb _ = pure ()
writePackageJson :: Make.Make ()
writePackageJson = pure ()
outputPrimDocs :: Make.Make ()
outputPrimDocs = pure ()
server :: [N.ModuleName] -> [P.ExternsFile] -> P.Env -> P.Environment -> Int -> IO ()
server allModuleNames externs initNamesEnv initEnv port = do
codegenRef <- IORef.newIORef Nothing
let makeActions = buildMakeActions codegenRef
let modNames = Set.fromList allModuleNames
let compile :: Text -> IO (Either Error ([P.JSONError], JS))
compile input
| T.length input > 20000 = return $ Left $ OtherError "Please limit your input to 20000 characters"
| otherwise = do
case CST.parseModuleFromFile "<file>" input of
Left parserErrors ->
return $ Left $ toCompilerErrors parserErrors
Right partialResult -> case CST.resFull partialResult of
(_, Left parserErrors) ->
return $ Left $ toCompilerErrors parserErrors
(parserWarnings, Right m)
| Set.notMember (P.getModuleName m) modNames -> do
(makeResult, warnings) <- Make.runMake P.defaultOptions $ Make.rebuildModule' makeActions initNamesEnv externs m
codegenResult <- IORef.readIORef codegenRef
return $ case makeResult of
Left errors ->
Left $ CompilerErrors $ toJsonErrors errors
Right _ | Just js <- codegenResult -> do
let ws = warnings <> CST.toMultipleWarnings "<file>" parserWarnings
Right (toJsonErrors ws, js)
Right _ ->
Left $ OtherError "Failed to read the results of codegen."
| otherwise -> do
let
modName = N.runModuleName $ P.getModuleName m
return $ Left $ OtherError $
"The name of the module you defined " <> modName <> " clashes with another module in the package set. Rename the module to something else (e.g. 'Main')."
scottyOpts (getOpts port) $ do
get "/" $
Scotty.text "POST api.purescript.org/compile"
post "/compile" $ do
code <- T.decodeUtf8 . BL.toStrict <$> body
response <- lift $ compile code
Scotty.setHeader "Access-Control-Allow-Origin" "*"
case response of
Left err ->
Scotty.json $ A.object [ "error" .= err ]
Right (warnings, comp) ->
Scotty.json $ A.object [ "js" .= comp, "warnings" .= warnings ]
get "/search" $ do
query <- param "q"
Scotty.setHeader "Access-Control-Allow-Origin" "*"
Scotty.setHeader "Content-Type" "application/json"
case tryParseType query of
Nothing -> Scotty.json $ A.object [ "error" .= ("Cannot parse type" :: Text) ]
Just ty -> do
let elabs = lookupAllConstructors initEnv ty
search = fst . TS.typeSearch (Just []) initEnv (P.emptyCheckState initEnv)
results = nubBy ((==) `on` fst) $ do
elab <- elabs
let mkSkolemType nm s = P.Skolem P.NullSourceAnn nm Nothing s (P.SkolemScope 0)
strictMatches = search (replaceTypeVariablesAndDesugar mkSkolemType elab)
flexMatches = search (replaceTypeVariablesAndDesugar (const (P.TUnknown P.NullSourceAnn)) elab)
take 50 (strictMatches ++ flexMatches)
Scotty.json $ A.object [ "results" .= [ P.showQualified id k
| (k, _) <- take 50 results
]
]
getOpts :: Int -> Scotty.Options
getOpts port = def
{ settings =
Warp.setHost "127.0.0.1"
$ Warp.setPort port
$ Warp.defaultSettings
}
lookupAllConstructors :: P.Environment -> P.SourceType -> [P.SourceType]
lookupAllConstructors env = P.everywhereOnTypesM $ \case
P.TypeConstructor ann (P.Qualified (N.BySourcePos _) tyCon) -> P.TypeConstructor ann <$> lookupConstructor env tyCon
other -> pure other
where
lookupConstructor :: P.Environment -> P.ProperName 'P.TypeName -> [P.Qualified (P.ProperName 'P.TypeName)]
lookupConstructor env nm =
[ q
| (q@(P.Qualified (N.ByModuleName _) thisNm), _) <- M.toList (P.types env)
, thisNm == nm
]
-- | (Consistently) replace unqualified type constructors and type variables with unknowns.
--
-- Also remove the @ParensInType@ Constructor (we need to deal with type operators later at some point).
replaceTypeVariablesAndDesugar :: (Text -> Int -> P.SourceType) -> P.SourceType -> P.SourceType
replaceTypeVariablesAndDesugar f ty = State.evalState (P.everywhereOnTypesM go ty) (0, M.empty) where
go = \case
P.ParensInType _ ty -> pure ty
P.TypeVar _ s -> do
(next, m) <- State.get
case M.lookup s m of
Nothing -> do
let ty = f s next
State.put (next + 1, M.insert s ty m)
pure ty
Just ty -> pure ty
other -> pure other
tryParseType :: Text -> Maybe P.SourceType
tryParseType = hush . fmap (CST.convertType "<file>") . runParser CST.parseTypeP
where
hush = either (const Nothing) Just
runParser :: CST.Parser a -> Text -> Either String a
runParser p =
bimap (CST.prettyPrintError . NE.head) snd
. CST.runTokenParser (p <* CSTM.token CST.TokEof)
. CST.lexTopLevel
main :: IO ()
main = do
-- Stop mangled "Compiling ModuleName" text
IO.hSetBuffering IO.stderr IO.LineBuffering
(portString : inputGlobs) <- getArgs
let port = read portString
inputFiles <- concat <$> traverse glob inputGlobs
e <- runExceptT $ do
modules <- ExceptT $ I.loadAllModules inputFiles
(exts, env) <- ExceptT . I.runMake . I.make $ map (second CST.pureResult) modules
namesEnv <- fmap fst . runWriterT $ foldM P.externsEnv P.primEnv exts
let
allModuleNames = fmap (P.getModuleName . snd) modules
pure (allModuleNames, exts, namesEnv, env)
case e of
Left err -> print err >> exitFailure
Right (allModuleNames, exts, namesEnv, env) -> server allModuleNames exts namesEnv env port