@@ -18,9 +18,11 @@ import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
18
18
import Control.Monad.Trans.Reader (runReaderT )
19
19
import qualified Data.Aeson as A
20
20
import Data.Aeson ((.=) )
21
+ import Data.Bifunctor (first , second )
21
22
import qualified Data.ByteString.Lazy as BL
22
23
import Data.Function (on )
23
24
import Data.List (foldl' , nubBy )
25
+ import qualified Data.List.NonEmpty as NE
24
26
import qualified Data.Map as M
25
27
import Data.String (fromString )
26
28
import Data.Text (Text )
@@ -30,6 +32,8 @@ import qualified Data.Text.Lazy as TL
30
32
import Data.Traversable (for )
31
33
import GHC.Generics (Generic )
32
34
import qualified Language.PureScript as P
35
+ import qualified Language.PureScript.CST as CST
36
+ import qualified Language.PureScript.CST.Monad as CSTM
33
37
import qualified Language.PureScript.Bundle as Bundle
34
38
import qualified Language.PureScript.CodeGen.JS as J
35
39
import qualified Language.PureScript.CodeGen.JS.Printer as P
@@ -63,13 +67,15 @@ server bundled externs initEnv port = do
63
67
| T. length input > 20000 = return (Left (OtherError " Please limit your input to 20000 characters" ))
64
68
| otherwise = do
65
69
let printErrors = P. prettyPrintMultipleErrors (P. defaultPPEOptions { P. ppeCodeColor = Nothing })
66
- case P . parseModuleFromFile ( const " <file>" ) ( undefined , input) of
70
+ case CST . parseModuleFromFile " <file>" input >>= CST. resFull of
67
71
Left parseError ->
68
- return . Left . CompilerErrors . pure . P. toJSONError False P. Error . P. toPositionedError $ parseError
69
- Right (_, m) | P. getModuleName m == P. ModuleName [P. ProperName " Main" ] -> do
72
+ return . Left . CompilerErrors . P. toJSONErrors False P. Error $ CST. toMultipleErrors " <file> " parseError
73
+ Right m | P. getModuleName m == P. ModuleName [P. ProperName " Main" ] -> do
70
74
(resultMay, ws) <- runLogger' . runExceptT . flip runReaderT P. defaultOptions $ do
71
75
((P. Module ss coms moduleName elaborated exps, env), nextVar) <- P. runSupplyT 0 $ do
72
- [desugared] <- P. desugar externs [P. importPrim m]
76
+ desugared <- P. desugar externs [P. importPrim m] >>= \ case
77
+ [d] -> pure d
78
+ _ -> error " desugaring did not produce one module"
73
79
P. runCheck' (P. emptyCheckState initEnv) $ P. typeCheckModule desugared
74
80
regrouped <- P. createBindingGroups moduleName . P. collapseBindingGroups $ elaborated
75
81
let mod' = P. Module ss coms moduleName regrouped exps
@@ -80,7 +86,8 @@ server bundled externs initEnv port = do
80
86
case resultMay of
81
87
Left errs -> (return . Left . CompilerErrors . P. toJSONErrors False P. Error ) errs
82
88
Right js -> (return . Right ) (P. toJSONErrors False P. Error ws, js)
83
- Right _ -> (return . Left . OtherError ) " The name of the main module should be Main."
89
+ Right _ ->
90
+ (return . Left . OtherError ) " The name of the main module should be Main."
84
91
85
92
scotty port $ do
86
93
get " /" $
@@ -147,9 +154,15 @@ replaceTypeVariablesAndDesugar f ty = State.evalState (P.everywhereOnTypesM go t
147
154
other -> pure other
148
155
149
156
tryParseType :: Text -> Maybe P. SourceType
150
- tryParseType = hush ( P. lex " " ) >=> hush ( P. runTokenParser " " ( P. parsePolyType <* Parsec. eof))
157
+ tryParseType = hush . fmap ( CST. convertType " <file> " ) . runParser CST. parseTypeP
151
158
where
152
- hush f = either (const Nothing ) Just . f
159
+ hush = either (const Nothing ) Just
160
+
161
+ runParser :: CST. Parser a -> Text -> Either String a
162
+ runParser p =
163
+ first (CST. prettyPrintError . NE. head )
164
+ . CST. runTokenParser (p <* CSTM. token CST. TokEof )
165
+ . CST. lexTopLevel
153
166
154
167
bundle :: IO (Either Bundle. ErrorMessage String )
155
168
bundle = runExceptT $ do
@@ -168,7 +181,7 @@ main = do
168
181
let onError f = either (Left . f) Right
169
182
e <- runExceptT $ do
170
183
modules <- ExceptT (fmap (onError Right ) (I. loadAllModules inputFiles))
171
- (exts, env) <- ExceptT . fmap (onError Right ) . I. runMake . I. make $ modules
184
+ (exts, env) <- ExceptT . fmap (onError Right ) . I. runMake . I. make . map (second CST. pureResult) $ modules
172
185
js <- ExceptT (fmap (onError Left ) bundle)
173
186
return (fromString js, exts, env)
174
187
case e of
0 commit comments