module Language.Haskell.Extract (
functionExtractor,
functionExtractorMap,
locationModule
) where
import Language.Haskell.TH
import Text.Regex.Posix
import Data.List
extractAllFunctions :: String -> Q [String]
extractAllFunctions pattern =
do loc <- location
file <- runIO $ readFile $ loc_filename loc
return $ nub $ filter (=~pattern) $ map fst $ concat $ map lex $ lines file
functionExtractor :: String -> ExpQ
functionExtractor pattern =
do functions <- extractAllFunctions pattern
let makePair n = TupE [ LitE $ StringL n , VarE $ mkName n]
return $ ListE $ map makePair functions
functionExtractorMap :: String -> ExpQ -> ExpQ
functionExtractorMap pattern funcName =
do functions <- extractAllFunctions pattern
fn <- funcName
let makePair n = AppE (AppE (fn) (LitE $ StringL n)) (VarE $ mkName n)
return $ ListE $ map makePair functions
locationModule :: ExpQ
locationModule =
do loc <- location
return $ LitE $ StringL $ loc_module loc