language-haskell-extract-0.2.1/0000755000000000000000000000000011620541513014552 5ustar0000000000000000language-haskell-extract-0.2.1/BSD3.txt0000644000000000000000000000272411620541513016013 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.1/language-haskell-extract.cabal0000644000000000000000000000334011620541513022412 0ustar0000000000000000name: language-haskell-extract version: 0.2.1 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 hs-source-dirs: src exposed-modules: Language.Haskell.Extract build-depends: base >= 4 && < 5, regex-posix, haskell-src-exts, template-haskell source-repository head type: git location: https://github.com/finnsson/language-haskell-extract/ language-haskell-extract-0.2.1/Setup.lhs0000644000000000000000000000015611620541513016364 0ustar0000000000000000#!/usr/bin/runhaskell > module Main where > import Distribution.Simple > main :: IO () > main = defaultMain language-haskell-extract-0.2.1/src/0000755000000000000000000000000011620541513015341 5ustar0000000000000000language-haskell-extract-0.2.1/src/Language/0000755000000000000000000000000011620541513017064 5ustar0000000000000000language-haskell-extract-0.2.1/src/Language/Haskell/0000755000000000000000000000000011620541513020447 5ustar0000000000000000language-haskell-extract-0.2.1/src/Language/Haskell/Extract.hs0000644000000000000000000000741311620541513022422 0ustar0000000000000000module Language.Haskell.Extract ( functionExtractor, functionExtractorMap, locationModule ) where import Language.Haskell.TH import Language.Haskell.Exts.Parser import Language.Haskell.Exts (parseFileContentsWithMode) import Language.Haskell.Exts.Syntax import Text.Regex.Posix import Data.Maybe import Data.List import Language.Haskell.Exts.Extension extractAllFunctions :: String -> String-> [String] extractAllFunctions pattern file = -- allMatchingFunctions pattern . parsedModule nub $ filter (\f->f=~pattern::Bool) $ map (fst . head . lex) $ lines file -- nub $ filter ("prop_" `isPrefixOf`) $ -- map (fst . head . lex) $ lines ct parsedModule moduleCode = let pMod = parseFileContentsWithMode (defaultParseMode { extensions = knownExtensions } ) moduleCode moduleOrDefault (ParseFailed _ _) = Module (SrcLoc "unknown" 1 1) (ModuleName "unknown") [] Nothing Nothing [] [] moduleOrDefault (ParseOk m) = m in moduleOrDefault pMod allFunctions = onlyJust extractNameOfFunctionFromDecl . hsModuleDecls allMatchingFunctions pattern = filter (\f->f=~pattern::Bool) . allFunctions extractNameOfFunctionFromDecl :: Decl -> Maybe String extractNameOfFunctionFromDecl (PatBind _ (PVar (Ident n)) _ _ _ ) = Just n extractNameOfFunctionFromDecl (FunBind ms) = Just $ head $ [n | (Language.Haskell.Exts.Syntax.Match _ (Ident n) _ _ _ _) <- ms] extractNameOfFunctionFromDecl _ = Nothing onlyJust f = map fromJust . filter isJust . map f hsModuleDecls (Module _ _ _ _ _ _ d) = d -- | 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 loc <- location moduleCode <- runIO $ readFile $ loc_filename loc let functions = extractAllFunctions pattern moduleCode makePair n = TupE [ LitE $ StringL n , VarE $ mkName n] return $ ListE $ map makePair functions -- functionExtractor' :: String -> Q [String] -- functionExtractor' pattern = -- do loc <- location -- moduleCode <- runIO $ readFile $ loc_filename loc -- let functions = extractAllFunctions pattern moduleCode -- return 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 loc <- location moduleCode <- runIO $ readFile $ loc_filename loc let functions :: [String] functions = extractAllFunctions pattern moduleCode fn <- funcName let makePair n = AppE (AppE (fn) (LitE $ StringL n)) (VarE $ mkName n) return $ ListE $ map makePair functions -- functionExtractorExpMap :: String -> (Exp -> ExpQ) -> ExpQ -- functionExtractorExpMap pattern func = -- do loc <- location -- moduleCode <- runIO $ readFile $ loc_filename loc -- let functions :: [String] -- functions = extractAllFunctions pattern moduleCode -- 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