summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2020-03-16 15:00:25 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-04-21 06:39:32 -0400
commit6655f93324b7f1d30a6baaedfecae455d5e08e39 (patch)
treee0a260f8b69d3cfcdf6d890849b88933fe6f3f6b
parentbca02fca0119354a6201fd5d019a553015ba2dd8 (diff)
downloadhaskell-6655f93324b7f1d30a6baaedfecae455d5e08e39.tar.gz
Use ParserFlags in GHC.Runtime.Eval (#17957)
Instead of passing `DynFlags` to functions such as `isStmt` and `hasImport` in `GHC.Runtime.Eval` we pass `ParserFlags`. It's a much simpler structure that can be created purely with `mkParserFlags'`.
-rw-r--r--compiler/GHC/Runtime/Eval.hs33
-rw-r--r--ghc/GHCi/UI.hs8
-rw-r--r--testsuite/tests/ghc-api/T9015.hs6
3 files changed, 25 insertions, 22 deletions
diff --git a/compiler/GHC/Runtime/Eval.hs b/compiler/GHC/Runtime/Eval.hs
index 655e0ea5bc..8e6d5e3ed5 100644
--- a/compiler/GHC/Runtime/Eval.hs
+++ b/compiler/GHC/Runtime/Eval.hs
@@ -95,7 +95,8 @@ import Outputable
import FastString
import Bag
import Util
-import qualified GHC.Parser.Lexer as Lexer (P (..), ParseResult(..), unP, mkPState)
+import qualified GHC.Parser.Lexer as Lexer (P (..), ParseResult(..), unP, mkPStatePure)
+import GHC.Parser.Lexer (ParserFlags)
import qualified GHC.Parser as Parser (parseStmt, parseModule, parseDeclaration, parseImport)
import System.Directory
@@ -879,44 +880,44 @@ parseName str = withSession $ \hsc_env -> liftIO $
; hscTcRnLookupRdrName hsc_env lrdr_name }
-- | Returns @True@ if passed string is a statement.
-isStmt :: DynFlags -> String -> Bool
-isStmt dflags stmt =
- case parseThing Parser.parseStmt dflags stmt of
+isStmt :: ParserFlags -> String -> Bool
+isStmt pflags stmt =
+ case parseThing Parser.parseStmt pflags stmt of
Lexer.POk _ _ -> True
Lexer.PFailed _ -> False
-- | Returns @True@ if passed string has an import declaration.
-hasImport :: DynFlags -> String -> Bool
-hasImport dflags stmt =
- case parseThing Parser.parseModule dflags stmt of
+hasImport :: ParserFlags -> String -> Bool
+hasImport pflags stmt =
+ case parseThing Parser.parseModule pflags stmt of
Lexer.POk _ thing -> hasImports thing
Lexer.PFailed _ -> False
where
hasImports = not . null . hsmodImports . unLoc
-- | Returns @True@ if passed string is an import declaration.
-isImport :: DynFlags -> String -> Bool
-isImport dflags stmt =
- case parseThing Parser.parseImport dflags stmt of
+isImport :: ParserFlags -> String -> Bool
+isImport pflags stmt =
+ case parseThing Parser.parseImport pflags stmt of
Lexer.POk _ _ -> True
Lexer.PFailed _ -> False
-- | Returns @True@ if passed string is a declaration but __/not a splice/__.
-isDecl :: DynFlags -> String -> Bool
-isDecl dflags stmt = do
- case parseThing Parser.parseDeclaration dflags stmt of
+isDecl :: ParserFlags -> String -> Bool
+isDecl pflags stmt = do
+ case parseThing Parser.parseDeclaration pflags stmt of
Lexer.POk _ thing ->
case unLoc thing of
SpliceD _ _ -> False
_ -> True
Lexer.PFailed _ -> False
-parseThing :: Lexer.P thing -> DynFlags -> String -> Lexer.ParseResult thing
-parseThing parser dflags stmt = do
+parseThing :: Lexer.P thing -> ParserFlags -> String -> Lexer.ParseResult thing
+parseThing parser pflags stmt = do
let buf = stringToStringBuffer stmt
loc = mkRealSrcLoc (fsLit "<interactive>") 1 1
- Lexer.unP parser (Lexer.mkPState dflags buf loc)
+ Lexer.unP parser (Lexer.mkPStatePure pflags buf loc)
getDocs :: GhcMonad m
=> Name
diff --git a/ghc/GHCi/UI.hs b/ghc/GHCi/UI.hs
index be3c75f556..2416fd9d9d 100644
--- a/ghc/GHCi/UI.hs
+++ b/ghc/GHCi/UI.hs
@@ -1168,7 +1168,7 @@ enqueueCommands cmds = do
-- The return value True indicates success, as in `runOneCommand`.
runStmt :: GhciMonad m => String -> SingleStep -> m (Maybe GHC.ExecResult)
runStmt input step = do
- dflags <- GHC.getInteractiveDynFlags
+ pflags <- Lexer.mkParserFlags <$> GHC.getInteractiveDynFlags
-- In GHCi, we disable `-fdefer-type-errors`, as well as `-fdefer-type-holes`
-- and `-fdefer-out-of-scope-variables` for **naked expressions**. The
-- declarations and statements are not affected.
@@ -1177,7 +1177,7 @@ runStmt input step = do
let source = progname st
let line = line_number st
- if | GHC.isStmt dflags input -> do
+ if | GHC.isStmt pflags input -> do
hsc_env <- GHC.getSession
mb_stmt <- liftIO (runInteractiveHsc hsc_env (hscParseStmtWithLocation source line input))
case mb_stmt of
@@ -1187,13 +1187,13 @@ runStmt input step = do
Just stmt ->
run_stmt stmt
- | GHC.isImport dflags input -> run_import
+ | GHC.isImport pflags input -> run_import
-- Every import declaration should be handled by `run_import`. As GHCi
-- in general only accepts one command at a time, we simply throw an
-- exception when the input contains multiple commands of which at least
-- one is an import command (see #10663).
- | GHC.hasImport dflags input -> throwGhcException
+ | GHC.hasImport pflags input -> throwGhcException
(CmdLineError "error: expecting a single import declaration")
-- Otherwise assume a declaration (or a list of declarations)
diff --git a/testsuite/tests/ghc-api/T9015.hs b/testsuite/tests/ghc-api/T9015.hs
index 228449041d..3388ee0566 100644
--- a/testsuite/tests/ghc-api/T9015.hs
+++ b/testsuite/tests/ghc-api/T9015.hs
@@ -2,8 +2,9 @@ module Main where
import GHC
import GHC.Driver.Session
-import System.Environment
import GHC.Driver.Monad
+import GHC.Parser.Lexer (mkParserFlags)
+import System.Environment
testStrings = [
"import Data.Maybe"
@@ -52,7 +53,8 @@ main = do
where
testWithParser parser = do
dflags <- getSessionDynFlags
- liftIO . putStrLn . unlines $ map (testExpr (parser dflags)) testStrings
+ let pflags = mkParserFlags dflags
+ liftIO . putStrLn . unlines $ map (testExpr (parser pflags)) testStrings
testExpr parser expr = do
expr ++ ": " ++ show (parser expr)