Skip to content

Commit

Permalink
Workaround lack of -XExplicitNamespace support
Browse files Browse the repository at this point in the history
...by simply removing all substrings " type " from the `.imports` file
before passing it to the haskell-src-exts parser.

See also haskell-suite/haskell-src-exts#93
  • Loading branch information
hvr committed Apr 26, 2014
1 parent d50bb5f commit 151e62c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packunused.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: packunused
version: 0.1.1.1
version: 0.1.1.2
synopsis: Tool for detecting redundant Cabal package dependencies
homepage: https://github.com/hvr/packunused
bug-reports: https://github.com/hvr/packunused/issues
Expand Down Expand Up @@ -73,4 +73,5 @@ executable packunused
cmdargs ==0.10.*,
directory >=1.1 && <1.3,
filepath ==1.3.*,
haskell-src-exts >=1.13 && <1.16
haskell-src-exts >=1.13 && <1.16,
split ==0.2.*
19 changes: 13 additions & 6 deletions packunused.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Main where

import Control.Monad
import Data.List
import Data.List.Split (splitOn)
import Data.Maybe
import Data.Version (Version(Version), showVersion)
import Distribution.InstalledPackageInfo (exposedModules, installedPackageId)
Expand Down Expand Up @@ -240,25 +241,31 @@ readImports outDir fn = do

let m = MN.fromString $ take (length fn - length ".imports") fn

parseRes <- H.parseFileWithExts exts (outDir </> fn)
case parseRes of
contents <- readFile (outDir </> fn)
case parseImportsFile contents of
(H.ParseOk (H.Module _ _ _ _ _ imps _)) -> do
let imps' = [ (MN.fromString mn, extractSpecs (H.importSpecs imp))
| imp <- imps, let H.ModuleName mn = H.importModule imp ]

return (m, imps')
H.ParseFailed {} -> do
print parseRes
H.ParseFailed loc msg -> do
putStrLn "*ERROR* failed to parse .imports file"
putStrLn $ H.prettyPrint loc ++ ": " ++ msg
exitFailure

where
extractSpecs (Just (False, impspecs)) = map H.prettyPrint impspecs
extractSpecs _ = error "unexpected import specs"

parseImportsFile = H.parseFileContentsWithMode (H.defaultParseMode { H.extensions = exts, H.parseFilename = outDir </> fn }) . stripExplicitNamespaces

-- hack to remove -XExplicitNamespaces until haskell-src-exts supports that
stripExplicitNamespaces = unwords . splitOn " type "

#if MIN_VERSION_haskell_src_exts(1,14,0)
exts = map H.EnableExtension [ H.MagicHash, H.PackageImports, H.CPP, H.TypeOperators, H.TypeFamilies ]
exts = map H.EnableExtension [ H.MagicHash, H.PackageImports, H.CPP, H.TypeOperators, H.TypeFamilies {- , H.ExplicitNamespaces -} ]
#else
exts = [ H.MagicHash, H.PackageImports, H.CPP, H.TypeOperators, H.TypeFamilies {- , H.ExplicitNamespaces -} ]
exts = [ H.MagicHash, H.PackageImports, H.CPP, H.TypeOperators, H.TypeFamilies ]
#endif


Expand Down

0 comments on commit 151e62c

Please sign in to comment.