I am using haskell-src-exts
package to parse the import
declarations inside a module, here is the code I am using
importNames :: FilePath -> IO ()
importNames fname = do
let pMode = parseMode fname
parsed <- P.parseFileWithMode pMode fname
case parsed of
P.ParseOk m -> do
let names = modNames m
putStrLn $ P.prettyPrint
P.ParseFailed srcLoc message -> do
putStrLn $ unlines [P.prettyPrint srcLoc , message]
-- | Extract all imports from a Module
extractImports :: Module -> [ImportDecl]
extractImports (Module _ _ _ _ _ imports _) = imports
modNames :: Module -> S.Set ModuleName
modNames m = foldr (\i r -> S.insert (importModule i) r)
S.empty $ extractImports m
This failes as soon as the parser hits a #ifdef
directive.
Is this supposed to happen, can haskell-src-exts
ignore those and carry on?
Thanks.
You really don't want to do that. A Haskell module written using CPP generally won't parse, let alone make any sense, if you ignore the CPP. As Gurkenglas suggests, the solution is to run the preprocessor before attempting to parse the file.