Skip to content

Commit 9742a35

Browse files
committed
Refactor to have a flags in a record
1 parent ff914cf commit 9742a35

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

src/HSE/All.hs

+16-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module HSE.All(
55
module HSE.Bracket, module HSE.Match,
66
module HSE.Generics,
77
module HSE.NameMatch,
8-
parseFile, parseString, fromParseResult
8+
ParseFlags(..), parseFlags, parseFile, parseString, fromParseResult
99
) where
1010

1111
import Language.Haskell.Exts hiding (parse, parseFile, paren, fromParseResult)
@@ -23,22 +23,31 @@ import Data.List
2323
import Language.Preprocessor.Cpphs
2424

2525

26+
data ParseFlags = ParseFlags
27+
{cpphs :: Maybe CpphsOptions
28+
,implies :: Bool
29+
}
30+
31+
parseFlags :: ParseFlags
32+
parseFlags = ParseFlags Nothing False
33+
34+
2635
-- | Parse a Haskell module
27-
parseString :: Maybe CpphsOptions -> Bool -> FilePath -> String -> ParseResult Module
28-
parseString cpphs implies file = parseFileContentsWithMode mode . maybe id (`runCpphs` file) cpphs
36+
parseString :: ParseFlags -> FilePath -> String -> ParseResult Module
37+
parseString flags file = parseFileContentsWithMode mode . maybe id (`runCpphs` file) (cpphs flags)
2938
where
3039
mode = defaultParseMode
3140
{parseFilename = file
3241
,extensions = extension
33-
,fixities = concat [infix_ (-1) ["==>"] | implies] ++ baseFixities
42+
,fixities = concat [infix_ (-1) ["==>"] | implies flags] ++ baseFixities
3443
}
3544

3645

3746
-- | On failure returns an empty module and prints to the console
38-
parseFile :: Maybe CpphsOptions -> Bool -> FilePath -> IO (ParseResult Module)
39-
parseFile cpphs implies file = do
47+
parseFile :: ParseFlags -> FilePath -> IO (ParseResult Module)
48+
parseFile flags file = do
4049
src <- readFile file
41-
return $ parseString cpphs implies file src
50+
return $ parseString flags file src
4251

4352

4453
-- | TODO: Use the fromParseResult in HSE once it gives source location

src/Main.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Test
1616
import Util
1717
import Parallel
1818
import Hint.All
19+
import HSE.All
1920

2021

2122
main = do
@@ -24,7 +25,7 @@ main = do
2425
settings <- readSettings cmdHintFiles
2526
let extra = [Classify Ignore x ("","") | x <- cmdIgnore]
2627
let apply :: FilePath -> IO [Idea]
27-
apply = fmap (fmap $ classify $ settings ++ extra) . applyHint (Just cmdCpphs) (allHints settings)
28+
apply = fmap (fmap $ classify $ settings ++ extra) . applyHint parseFlags{cpphs=Just cmdCpphs} (allHints settings)
2829
ideas <- liftM concat $ parallel [listM' =<< apply x | x <- cmdFiles]
2930
showItem <- if cmdColor then showANSI else return show
3031
mapM_ putStrLn [showItem i | i <- ideas, cmdShowAll || rank i /= Ignore]

src/Settings.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ readSettings xs = do
2525
-- currently it doesn't
2626
readHints :: FilePath -> IO [Module]
2727
readHints file = do
28-
y <- fromParseResult `fmap` parseFile Nothing True file
28+
y <- fromParseResult `fmap` parseFile parseFlags{implies=True} file
2929
ys <- concatMapM (f . fromNamed . importModule) $ moduleImports y
3030
return $ y:ys
3131
where

src/Test.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ parseTestFile :: FilePath -> IO [(NameMatch, Test)]
6969
parseTestFile file = do
7070
src <- readFile file
7171
return [(nm, createTest eqn)
72-
| code <- f $ lines src, let modu = fromParseResult $ parseString Nothing False file code
72+
| code <- f $ lines src, let modu = fromParseResult $ parseString parseFlags file code
7373
, let nm = nameMatch $ moduleImports modu
7474
, eqn <- concatMap getEquations $ moduleDecls modu]
7575
where

src/Type.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Data.Char
77
import Data.List
88
import Data.Maybe
99
import Data.Ord
10-
import Language.Preprocessor.Cpphs
1110
import Language.Haskell.HsColour.TTY
1211
import Language.Haskell.HsColour.Colourise
1312

@@ -83,10 +82,10 @@ type ModuHint = NameMatch -> Module -> [Idea]
8382
data Hint = DeclHint {declHint :: DeclHint} | ModuHint {moduHint :: ModuHint}
8483

8584

86-
applyHint :: Maybe CpphsOptions -> [Hint] -> FilePath -> IO [Idea]
87-
applyHint cpphs h file = do
85+
applyHint :: ParseFlags -> [Hint] -> FilePath -> IO [Idea]
86+
applyHint flags h file = do
8887
src <- readFile file
89-
case parseString cpphs False file src of
88+
case parseString flags file src of
9089
ParseFailed sl msg -> do
9190
let ticks = [" "," ","> "," "," "]
9291
let bad = zipWith (++) ticks $ take 5 $ drop (srcLine sl - 3) $ lines src ++ [""]

0 commit comments

Comments
 (0)