Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/Language/Haskell/Extract.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE CPP #-}

module Language.Haskell.Extract (
functionExtractor,
functionExtractorMap,
Expand All @@ -14,36 +16,39 @@ extractAllFunctions pattern =
return $ nub $ filter (=~pattern) $ map fst $ concat $ map lex $ lines file

-- | Extract the names and functions from the module where this function is called.
--
--
-- > foo = "test"
-- > boo = "testing"
-- > bar = $(functionExtractor "oo$")
--
--
-- will automagically extract the functions ending with "oo" such as
--
--
-- > bar = [("foo",foo), ("boo",boo)]
functionExtractor :: String -> ExpQ
functionExtractor pattern =
do functions <- extractAllFunctions pattern
#if MIN_VERSION_template_haskell(2,16,0)
let makePair n = TupE [ Just $ LitE $ StringL n , Just $ VarE $ mkName n]
#else
let makePair n = TupE [ LitE $ StringL n , VarE $ mkName n]
#endif
return $ ListE $ map makePair functions


-- | Extract the names and functions from the module and apply a function to every pair.
--
--
-- Is very useful if the common denominator of the functions is just a type class.
--
-- > secondTypeclassTest =
-- > do let expected = ["45", "88.8", "\"hej\""]
-- > actual = $(functionExtractorMap "^tc" [|\n f -> show f|] )
-- > expected @=? actual
-- >
-- >
-- > tcInt :: Integer
-- > tcInt = 45
-- >
-- >
-- > tcDouble :: Double
-- > tcDouble = 88.8
-- >
-- >
-- > tcString :: String
-- > tcString = "hej"
functionExtractorMap :: String -> ExpQ -> ExpQ
Expand Down