language-haskell-extract-0.2.4/0000755000000000000000000000000012100715431014551 5ustar0000000000000000language-haskell-extract-0.2.4/BSD3.txt0000644000000000000000000000272412100715431016012 0ustar0000000000000000Copyright (c) 2010, Oscar Finnsson All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Oscar Finnsson nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Oscar Finnsson BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.language-haskell-extract-0.2.4/language-haskell-extract.cabal0000644000000000000000000000332712100715431022416 0ustar0000000000000000name: language-haskell-extract version: 0.2.4 cabal-version: >=1.6 build-type: Simple license: BSD3 license-file: "BSD3.txt" maintainer: Oscar Finnsson stability: stable homepage: http://github.com/finnsson/template-helper package-url: bug-reports: synopsis: Module to automatically extract functions from the local code. description: @language-haskell-extract@ contains some useful helper functions on top of Template Haskell. . @functionExtractor@ extracts all functions after a regexp-pattern. . > foo = "test" > boo = "testing" > bar = $(functionExtractor "oo$") . will automagically extract the functions ending with @oo@ such as . > bar = [("foo",foo), ("boo",boo)] . This can be useful if you wish to extract all functions beginning with test (for a test-framework) or all functions beginning with wc (for a web service). . @functionExtractorMap@ works like @functionsExtractor@ but applies a function over all function-pairs. . This functions is useful if the common return type of the functions is a type class. . Example: . > 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" category: Template Haskell author: Oscar Finnsson & Emil Nordling library ghc-options: -Wall hs-source-dirs: src exposed-modules: Language.Haskell.Extract build-depends: base >= 4 && < 5, regex-posix, template-haskell source-repository head type: git location: http://github.com/finnsson/template-helper language-haskell-extract-0.2.4/Setup.lhs0000644000000000000000000000015612100715431016363 0ustar0000000000000000#!/usr/bin/runhaskell > module Main where > import Distribution.Simple > main :: IO () > main = defaultMain language-haskell-extract-0.2.4/src/0000755000000000000000000000000012100715431015340 5ustar0000000000000000language-haskell-extract-0.2.4/src/Language/0000755000000000000000000000000012100715431017063 5ustar0000000000000000language-haskell-extract-0.2.4/src/Language/Haskell/0000755000000000000000000000000012100715431020446 5ustar0000000000000000language-haskell-extract-0.2.4/src/Language/Haskell/Extract.hs0000644000000000000000000000345712100715431022425 0ustar0000000000000000module 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 -- | 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 let makePair n = TupE [ LitE $ StringL n , VarE $ mkName n] 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 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 -- | Extract the name of the current module. locationModule :: ExpQ locationModule = do loc <- location return $ LitE $ StringL $ loc_module loc