Fix dying on broken code and handle errors in Main.hs

This commit is contained in:
Kirill Andreev 2020-07-13 15:26:32 +04:00
parent 508b111374
commit 58e5128142
No known key found for this signature in database
GPG Key ID: CF7DA79DE4785A47
5 changed files with 59 additions and 35 deletions

View File

@ -0,0 +1,6 @@
function hello (const x : int) : return is
block {
const boom : int =
if boom > x
else x;
} with boom

View File

@ -0,0 +1,4 @@
function hello (const x : int) : return
block {
const boom : int = x
} with boom

View File

@ -9,6 +9,7 @@ import Data.Default
-- import Data.Foldable -- import Data.Foldable
import qualified Data.Text as Text import qualified Data.Text as Text
import Data.Text (Text) import Data.Text (Text)
import Data.String (fromString)
import Data.String.Interpolate (i) import Data.String.Interpolate (i)
import qualified Language.Haskell.LSP.Control as CTRL import qualified Language.Haskell.LSP.Control as CTRL
@ -157,34 +158,43 @@ eventLoop funs chan = do
(Just 0) (Just 0)
ReqDefinition req -> do ReqDefinition req -> do
stopDyingAlready funs req do
let uri = req^.J.params.J.textDocument.J.uri let uri = req^.J.params.J.textDocument.J.uri
let pos = posToRange $ req^.J.params.J.position let pos = posToRange $ req^.J.params.J.position
tree <- loadByURI uri tree <- loadByURI uri
case Find.definitionOf pos tree of case Find.definitionOf pos tree of
Just defPos -> do Just defPos -> do
respondWith req RspDefinition $ J.MultiLoc [J.Location uri $ rangeToLoc defPos] respondWith funs req RspDefinition $ J.MultiLoc [J.Location uri $ rangeToLoc defPos]
Nothing -> do Nothing -> do
respondWith req RspDefinition $ J.MultiLoc [] respondWith funs req RspDefinition $ J.MultiLoc []
ReqFindReferences req -> do ReqFindReferences req -> do
stopDyingAlready funs req do
let uri = req^.J.params.J.textDocument.J.uri let uri = req^.J.params.J.textDocument.J.uri
let pos = posToRange $ req^.J.params.J.position let pos = posToRange $ req^.J.params.J.position
tree <- loadFromVFS funs uri tree <- loadFromVFS funs uri
case Find.referencesOf pos tree of case Find.referencesOf pos tree of
Just refs -> do Just refs -> do
let locations = J.Location uri . rangeToLoc <$> refs let locations = J.Location uri . rangeToLoc <$> refs
respondWith req RspFindReferences $ J.List locations respondWith funs req RspFindReferences $ J.List locations
Nothing -> do Nothing -> do
respondWith req RspFindReferences $ J.List [] respondWith funs req RspFindReferences $ J.List []
_ -> U.logs "unknown msg" _ -> U.logs "unknown msg"
where
respondWith respondWith
:: J.RequestMessage J.ClientMethod req rsp :: Core.LspFuncs ()
-> J.RequestMessage J.ClientMethod req rsp
-> (J.ResponseMessage rsp -> FromServerMessage) -> (J.ResponseMessage rsp -> FromServerMessage)
-> rsp -> rsp
-> IO () -> IO ()
respondWith req wrap rsp = Core.sendFunc funs $ wrap $ Core.makeResponseMessage req rsp respondWith funs req wrap rsp = Core.sendFunc funs $ wrap $ Core.makeResponseMessage req rsp
stopDyingAlready :: Core.LspFuncs () -> J.RequestMessage m a b -> IO () -> IO ()
stopDyingAlready funs req = flip catch \(e :: SomeException) -> do
Core.sendErrorResponseS (Core.sendFunc funs) (req^.J.id.to J.responseId) J.InternalError
$ fromString
$ "this happened: " ++ show e
posToRange :: J.Position -> Range posToRange :: J.Position -> Range
posToRange (J.Position l c) = Range (l + 1, c + 1, 0) (l + 1, c + 1, 0) "" posToRange (J.Position l c) = Range (l + 1, c + 1, 0) (l + 1, c + 1, 0) ""

View File

@ -915,12 +915,12 @@ example :: FilePath
-- example = "../../../src/test/contracts/bytes_arithmetic.ligo" -- example = "../../../src/test/contracts/bytes_arithmetic.ligo"
-- example = "../../../src/test/contracts/bytes_unpack.ligo" -- example = "../../../src/test/contracts/bytes_unpack.ligo"
-- example = "../../../src/test/contracts/chain_id.ligo" -- example = "../../../src/test/contracts/chain_id.ligo"
example = "../../../src/test/contracts/coase.ligo" -- example = "../../../src/test/contracts/coase.ligo"
-- example = "../../../src/test/contracts/failwith.ligo" -- example = "../../../src/test/contracts/failwith.ligo"
-- example = "../../../src/test/contracts/loop.ligo" -- example = "../../../src/test/contracts/loop.ligo"
-- example = "../../../src/test/contracts/redeclaration.ligo" -- example = "../../../src/test/contracts/redeclaration.ligo"
-- example = "../../../src/test/contracts/includer.ligo" -- example = "../../../src/test/contracts/includer.ligo"
-- example = "../../../src/test/contracts/namespaces.ligo" -- example = "../../../src/test/contracts/namespaces.ligo"
-- example = "../../../src/test/contracts/blocks.ligo" -- example = "../../../src/test/contracts/blocks.ligo"
-- example = "../../../src/test/contracts/application.ligo" example = "../../../src/test/contracts/negative/fail1.ligo"
-- example = "../../../src/test/contracts/application.ligo" -- example = "../../../src/test/contracts/application.ligo"

View File

@ -103,7 +103,8 @@ type PList = [ParseForest, [Text], FilePath, Set.Set FilePath]
type ASTInfo = Product [Range, [Text]] type ASTInfo = Product [Range, [Text]]
runParser runParser
:: Parser a :: Stubbed a ASTInfo
=> Parser a
-> Source -> Source
-> IO (a, [Error ASTInfo]) -> IO (a, [Error ASTInfo])
runParser parser fin = do runParser parser fin = do
@ -117,9 +118,12 @@ runParser parser fin = do
(Cons dir (Cons dir
(Cons Set.empty (Cons Set.empty
Nil))) Nil)))
`catch` \(e :: Error ASTInfo) -> do
return $ (stub e, [])
runParser' runParser'
:: Parser a :: Stubbed a ASTInfo
=> Parser a
-> Source -> Source
-> IO a -> IO a
runParser' parser fin = fst <$> runParser parser fin runParser' parser fin = fst <$> runParser parser fin
@ -326,7 +330,7 @@ some p = some'
-- | Run parser on given file and pretty-print stuff. -- | Run parser on given file and pretty-print stuff.
-- --
debugParser :: Show a => Parser a -> Source -> IO () debugParser :: (Show a, Stubbed a ASTInfo) => Parser a -> Source -> IO ()
debugParser parser fin = do debugParser parser fin = do
(res, errs) <- runParser parser fin (res, errs) <- runParser parser fin
putStrLn "Result:" putStrLn "Result:"