1
1
mirror of https://github.com/github/semantic.git synced 2024-12-19 12:51:52 +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.Exception
import Control.Monad (join)
import Control.Monad.Error.Class
import Control.Monad.IO.Class
import Control.Parallel.Strategies
@ -181,7 +180,7 @@ runTaskWithOptions options task = do
end <- Time.getCurrentTime
queueLogMessage Info message (pairs <> [("duration", show (Time.diffUTCTime end start))])
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
Diff differ terms -> pure (differ terms) >>= yield
Render renderer input -> pure (renderer input) >>= yield
@ -198,29 +197,27 @@ runTaskWithOptions options task = do
| otherwise = pure ()
runParser :: Options -> Blob -> Parser term -> Task (Either String term)
runParser :: Options -> Blob -> Parser term -> Task term
runParser Options{..} blob@Blob{..} = go
where go :: Parser term -> Task (Either String term)
where go :: Parser term -> Task term
go parser = case parser of
ASTParser language ->
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
res <- go parser
case res of
Left err -> writeLog Error "failed parsing" blobFields >> pure (Left err)
Right ast -> logTiming "assign" $ case Assignment.assign blobSource assignment ast of
Left err -> do
let formatted = Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err
writeLog Error formatted blobFields
pure $ Left formatted
Right term -> do
for_ (errors term) $ \ err ->
writeLog Warning (Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err) blobFields
pure $ Right term
TreeSitterParser tslanguage -> logTiming "ts parse" $ liftIO (Right <$> treeSitterParser tslanguage blob)
MarkdownParser -> logTiming "cmark parse" $ pure (Right (cmarkParser blobSource))
LineByLineParser -> logTiming "line-by-line parse" $ pure (Right (lineByLineParser blobSource))
ast <- go parser `catchError` \ err -> writeLog Error "failed parsing" blobFields >> throwError err
logTiming "assign" $ case Assignment.assign blobSource assignment ast of
Left err -> do
let formatted = Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err
writeLog Error formatted blobFields
throwError formatted
Right term -> do
for_ (errors term) $ \ err ->
writeLog Warning (Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err) blobFields
pure term
TreeSitterParser tslanguage -> logTiming "ts parse" $ liftIO (treeSitterParser tslanguage blob)
MarkdownParser -> logTiming "cmark parse" $ pure (cmarkParser blobSource)
LineByLineParser -> logTiming "line-by-line parse" $ pure (lineByLineParser blobSource)
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 = cata $ \ (a :< syntax) -> case syntax of