Commit 59c62bc7 authored by Razzi Abuissa's avatar Razzi Abuissa

Introduce PragmaType, and import errors

parent 10991eb1
...@@ -13,7 +13,7 @@ import ClassyPrelude hiding (init, last, liftIO, head, hGetContents, tail, try) ...@@ -13,7 +13,7 @@ import ClassyPrelude hiding (init, last, liftIO, head, hGetContents, tail, try)
import Control.Concurrent (forkIO, threadDelay) import Control.Concurrent (forkIO, threadDelay)
import Prelude (putChar, head, tail, last, init, (!!)) import Prelude (putChar, head, tail, last, init, (!!))
import Data.List.Utils import Data.List.Utils
import Data.List(findIndex, and) import Data.List (findIndex, and)
import Data.String.Utils import Data.String.Utils
import Text.Printf import Text.Printf
import Data.Char as Char import Data.Char as Char
...@@ -79,7 +79,7 @@ data ErrorOccurred = Success | Failure deriving (Show, Eq) ...@@ -79,7 +79,7 @@ data ErrorOccurred = Success | Failure deriving (Show, Eq)
-- | Enable debugging output -- | Enable debugging output
debug :: Bool debug :: Bool
debug = False debug = True
-- | Set GHC's verbosity for debugging -- | Set GHC's verbosity for debugging
ghcVerbosity :: Maybe Int ghcVerbosity :: Maybe Int
...@@ -882,8 +882,12 @@ evalCommand _ (ParseError loc err) state = do ...@@ -882,8 +882,12 @@ evalCommand _ (ParseError loc err) state = do
evalComms = [] evalComms = []
} }
evalCommand output (Pragma pragmas) state = do evalCommand _ (Pragma (PragmaUnsupported pragmaType) pragmas) state = wrapExecution state $
write $ "Got pragmas " ++ show pragmas return $ displayError $ "Pragmas of type " ++ pragmaType ++
"\nare not supported."
evalCommand output (Pragma PragmaLanguage pragmas) state = do
write $ "Got LANGUAGE pragma " ++ show pragmas
evalCommand output (Directive SetExtension $ unwords pragmas) state evalCommand output (Directive SetExtension $ unwords pragmas) state
hoogleResults :: KernelState -> [Hoogle.HoogleResult] -> EvalOut hoogleResults :: KernelState -> [Hoogle.HoogleResult] -> EvalOut
......
...@@ -11,6 +11,7 @@ module IHaskell.Eval.Parser ( ...@@ -11,6 +11,7 @@ module IHaskell.Eval.Parser (
parseDirective, parseDirective,
getModuleName, getModuleName,
Located(..), Located(..),
PragmaType(..),
) where ) where
-- Hide 'unlines' to use our own 'joinLines' instead. -- Hide 'unlines' to use our own 'joinLines' instead.
...@@ -20,6 +21,7 @@ import Data.List (findIndex, maximumBy, maximum, inits) ...@@ -20,6 +21,7 @@ import Data.List (findIndex, maximumBy, maximum, inits)
import Data.String.Utils (startswith, strip, split) import Data.String.Utils (startswith, strip, split)
import Data.List.Utils (subIndex) import Data.List.Utils (subIndex)
import Prelude (init, last, head, tail) import Prelude (init, last, head, tail)
import Control.Monad (msum)
import Bag import Bag
import ErrUtils hiding (ErrMsg) import ErrUtils hiding (ErrMsg)
...@@ -31,6 +33,7 @@ import OrdList ...@@ -31,6 +33,7 @@ import OrdList
import Outputable hiding ((<>)) import Outputable hiding ((<>))
import SrcLoc hiding (Located) import SrcLoc hiding (Located)
import StringBuffer import StringBuffer
import Debug.Trace
import Language.Haskell.GHC.Parser import Language.Haskell.GHC.Parser
import IHaskell.Eval.Util import IHaskell.Eval.Util
...@@ -48,7 +51,7 @@ data CodeBlock ...@@ -48,7 +51,7 @@ data CodeBlock
| Directive DirectiveType String -- ^ An IHaskell directive. | Directive DirectiveType String -- ^ An IHaskell directive.
| Module String -- ^ A full Haskell module, to be compiled and loaded. | Module String -- ^ A full Haskell module, to be compiled and loaded.
| ParseError StringLoc ErrMsg -- ^ An error indicating that parsing the code block failed. | ParseError StringLoc ErrMsg -- ^ An error indicating that parsing the code block failed.
| Pragma [String] -- ^ A list of GHC pragmas (from a {-# LANGUAGE ... #-} block) | Pragma PragmaType [String] -- ^ A list of GHC pragmas (from a {-# LANGUAGE ... #-} block)
deriving (Show, Eq) deriving (Show, Eq)
-- | Directive types. Each directive is associated with a string in the -- | Directive types. Each directive is associated with a string in the
...@@ -67,6 +70,13 @@ data DirectiveType ...@@ -67,6 +70,13 @@ data DirectiveType
| GetKind -- ^ Get the kind of a type via ':kind'. | GetKind -- ^ Get the kind of a type via ':kind'.
deriving (Show, Eq) deriving (Show, Eq)
-- | Pragma types. Only LANGUAGE pragmas are currently supported.
-- Other pragma types are kept around as a string for error reporting.
data PragmaType
= PragmaLanguage
| PragmaUnsupported String
deriving (Show, Eq)
-- | Parse a string into code blocks. -- | Parse a string into code blocks.
parseString :: String -> Ghc [Located CodeBlock] parseString :: String -> Ghc [Located CodeBlock]
parseString codeString = do parseString codeString = do
...@@ -87,11 +97,12 @@ parseString codeString = do ...@@ -87,11 +97,12 @@ parseString codeString = do
return result return result
where where
parseChunk :: GhcMonad m => String -> LineNumber -> m (Located CodeBlock) parseChunk :: GhcMonad m => String -> LineNumber -> m (Located CodeBlock)
parseChunk chunk line parseChunk chunk line = Located line <$> handleChunk chunk line
| isDirective chunk = return $ Located line $ parseDirective chunk line where
| isPragma chunk = return $ Located line $ parsePragma chunk line handleChunk chunk line
| otherwise = Located line <$> parseCodeChunk chunk line | isDirective chunk = return $ parseDirective chunk line
| isPragma chunk = trace ("HERE " ++ (show chunk)) $ return $ parsePragma chunk line
| otherwise = parseCodeChunk chunk line
processChunks :: GhcMonad m => [Located CodeBlock] -> [Located String] -> m [Located CodeBlock] processChunks :: GhcMonad m => [Located CodeBlock] -> [Located String] -> m [Located CodeBlock]
processChunks accum remaining = processChunks accum remaining =
...@@ -123,20 +134,16 @@ activateExtensions (Directive SetDynFlag flags) = ...@@ -123,20 +134,16 @@ activateExtensions (Directive SetDynFlag flags) =
case stripPrefix "-X" flags of case stripPrefix "-X" flags of
Just ext -> void $ setExtension ext Just ext -> void $ setExtension ext
Nothing -> return () Nothing -> return ()
activateExtensions (Pragma PragmaLanguage extensions) = void $ setAll extensions
activateExtensions (Pragma extensions) = void $ setAll extensions
where where
setAll :: GhcMonad m => [String] -> m (Maybe String) setAll :: GhcMonad m => [String] -> m (Maybe String)
setAll (ext:extensions) = do setAll exts = do
err <- setExtension ext errs <- mapM setExtension exts
case err of return $ msum errs
Nothing -> setAll extensions
Just err -> return $ Just err
setAll [] = return Nothing
activateExtensions _ = return () activateExtensions _ = return ()
-- | Parse a single chunk of code, as indicated by the layout of the code. -- | Parse a single chunk of code, as indicated by the layout of the code.
parseCodeChunk :: GhcMonad m => String -> LineNumber -> m CodeBlock parseCodeChunk :: GhcMonad m => String -> LineNumber -> m CodeBlock
parseCodeChunk code startLine = do parseCodeChunk code startLine = do
flags <- getSessionDynFlags flags <- getSessionDynFlags
let let
...@@ -232,14 +239,15 @@ joinFunctions blocks = ...@@ -232,14 +239,15 @@ joinFunctions blocks =
parsePragma :: String -- ^ Pragma string. parsePragma :: String -- ^ Pragma string.
-> Int -- ^ Line number at which the directive appears. -> Int -- ^ Line number at which the directive appears.
-> CodeBlock -- ^ Pragma code block or a parse error. -> CodeBlock -- ^ Pragma code block or a parse error.
parsePragma ('{':'-':'#':pragma) line = Pragma $ extractPragma pragma parsePragma ('{':'-':'#':pragma) line =
where let commaToSpace :: Char -> Char
extractPragma :: String -> [String] commaToSpace ',' = ' '
-- | After removing commas, extract words until a # is reached commaToSpace x = x
extractPragma pragmas = case (words $ takeWhile (/= '#') $ filter (/= ',' ) pragmas) of pragmas = words $ takeWhile (/= '#') $ map commaToSpace pragma in
[] -> [] case pragmas of
x:xs -> xs -- remove the first word (such as LANGUAGE) [] -> Pragma (PragmaUnsupported "") [] --empty string pragmas are unsupported
"LANGUAGE":xs -> trace ("here we get " ++ (show pragmas)) $ Pragma PragmaLanguage xs
x:xs -> Pragma (PragmaUnsupported x) xs
-- | Parse a directive of the form :directiveName. -- | Parse a directive of the form :directiveName.
parseDirective :: String -- ^ Directive string. parseDirective :: String -- ^ Directive string.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment