1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 23:11:50 +03:00

Use MonadError in runParser.

This commit is contained in:
Rob Rix 2017-08-29 18:20:03 -04:00
parent 04593870e7
commit 568fce81d3

View File

@ -26,7 +26,6 @@ module Semantic.Task
import Control.Concurrent.STM.TMQueue import Control.Concurrent.STM.TMQueue
import Control.Exception import Control.Exception
import Control.Monad (join)
import Control.Monad.Error.Class import Control.Monad.Error.Class
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Parallel.Strategies import Control.Parallel.Strategies
@ -181,7 +180,7 @@ runTaskWithOptions options task = do
end <- Time.getCurrentTime end <- Time.getCurrentTime
queueLogMessage Info message (pairs <> [("duration", show (Time.diffUTCTime end start))]) queueLogMessage Info message (pairs <> [("duration", show (Time.diffUTCTime end start))])
either (pure . Left) yield res either (pure . Left) yield res
Parse parser blob -> go (runParser options blob parser) >>= either (pure . Left) yield . join Parse parser blob -> go (runParser options blob parser) >>= either (pure . Left) yield
Decorate algebra term -> pure (decoratorWithAlgebra algebra term) >>= yield Decorate algebra term -> pure (decoratorWithAlgebra algebra term) >>= yield
Diff differ terms -> pure (differ terms) >>= yield Diff differ terms -> pure (differ terms) >>= yield
Render renderer input -> pure (renderer input) >>= yield Render renderer input -> pure (renderer input) >>= yield
@ -198,29 +197,27 @@ runTaskWithOptions options task = do
| otherwise = pure () | otherwise = pure ()
runParser :: Options -> Blob -> Parser term -> Task (Either String term) runParser :: Options -> Blob -> Parser term -> Task term
runParser Options{..} blob@Blob{..} = go runParser Options{..} blob@Blob{..} = go
where go :: Parser term -> Task (Either String term) where go :: Parser term -> Task term
go parser = case parser of go parser = case parser of
ASTParser language -> ASTParser language ->
logTiming "ts ast parse" $ logTiming "ts ast parse" $
liftIO $ (Right <$> parseToAST language blob) `catchError` (pure . Left. displayException) liftIO ((Right <$> parseToAST language blob) `catchError` (pure . Left . displayException)) >>= either throwError pure
AssignmentParser parser assignment -> do AssignmentParser parser assignment -> do
res <- go parser ast <- go parser `catchError` \ err -> writeLog Error "failed parsing" blobFields >> throwError err
case res of logTiming "assign" $ case Assignment.assign blobSource assignment ast of
Left err -> writeLog Error "failed parsing" blobFields >> pure (Left err) Left err -> do
Right ast -> logTiming "assign" $ case Assignment.assign blobSource assignment ast of let formatted = Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err
Left err -> do writeLog Error formatted blobFields
let formatted = Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err throwError formatted
writeLog Error formatted blobFields Right term -> do
pure $ Left formatted for_ (errors term) $ \ err ->
Right term -> do writeLog Warning (Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err) blobFields
for_ (errors term) $ \ err -> pure term
writeLog Warning (Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err) blobFields TreeSitterParser tslanguage -> logTiming "ts parse" $ liftIO (treeSitterParser tslanguage blob)
pure $ Right term MarkdownParser -> logTiming "cmark parse" $ pure (cmarkParser blobSource)
TreeSitterParser tslanguage -> logTiming "ts parse" $ liftIO (Right <$> treeSitterParser tslanguage blob) LineByLineParser -> logTiming "line-by-line parse" $ pure (lineByLineParser blobSource)
MarkdownParser -> logTiming "cmark parse" $ pure (Right (cmarkParser blobSource))
LineByLineParser -> logTiming "line-by-line parse" $ pure (Right (lineByLineParser blobSource))
blobFields = [ ("path", blobPath), ("language", maybe "" show blobLanguage) ] blobFields = [ ("path", blobPath), ("language", maybe "" show blobLanguage) ]
errors :: (Syntax.Error :< fs, Apply1 Foldable fs, Apply1 Functor fs) => Term (Union fs) (Record Assignment.Location) -> [Error.Error String] errors :: (Syntax.Error :< fs, Apply1 Foldable fs, Apply1 Functor fs) => Term (Union fs) (Record Assignment.Location) -> [Error.Error String]
errors = cata $ \ (a :< syntax) -> case syntax of errors = cata $ \ (a :< syntax) -> case syntax of