web-routes-th-0.22.1/0000755000000000000000000000000012010220622012452 5ustar0000000000000000web-routes-th-0.22.1/LICENSE0000644000000000000000000000275712010220622013472 0ustar0000000000000000Copyright (c)2010, Jeremy Shaw 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 Jeremy Shaw nor the names of other 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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. web-routes-th-0.22.1/Setup.hs0000644000000000000000000000005612010220622014107 0ustar0000000000000000import Distribution.Simple main = defaultMain web-routes-th-0.22.1/web-routes-th.cabal0000644000000000000000000000141012010220622016137 0ustar0000000000000000Name: web-routes-th Version: 0.22.1 License: BSD3 License-File: LICENSE Author: jeremy@seereason.com Maintainer: partners@seereason.com Bug-Reports: http://bugzilla.seereason.com/ Category: Web, Language Synopsis: Support for deriving PathInfo using Template Haskell Cabal-Version: >= 1.6 Build-type: Simple Library Build-Depends: base >= 4 && < 5, parsec >= 2 && < 4, template-haskell, text, split, web-routes >= 0.26 Exposed-Modules: Web.Routes.TH source-repository head type: darcs location: http://src.seereason.com/web-routes/ web-routes-th-0.22.1/Web/0000755000000000000000000000000012010220622013167 5ustar0000000000000000web-routes-th-0.22.1/Web/Routes/0000755000000000000000000000000012010220622014450 5ustar0000000000000000web-routes-th-0.22.1/Web/Routes/TH.hs0000644000000000000000000001457712010220622015335 0ustar0000000000000000{-# LANGUAGE CPP, TemplateHaskell #-} module Web.Routes.TH ( derivePathInfo , derivePathInfo' , standard , mkRoute ) where import Control.Applicative ((<$>)) import Control.Monad (ap, replicateM) import Data.Char (isUpper, toLower, toUpper) import Data.List (intercalate, foldl') import Data.List.Split (split, dropInitBlank, keepDelimsL, whenElt) import Data.Text (pack, unpack) import Language.Haskell.TH import Language.Haskell.TH.Syntax (nameBase) import Text.ParserCombinators.Parsec ((<|>),many1) import Web.Routes.PathInfo -- | use Template Haskell to create 'PathInfo' instances for a type. -- -- > $(derivePathInfo ''SiteURL) -- -- Uses the 'standard' formatter by default. derivePathInfo :: Name -> Q [Dec] derivePathInfo = derivePathInfo' standard -- FIXME: handle when called with a type (not data, newtype) -- | use Template Haskell to create 'PathInfo' instances for a type. -- -- This variant allows the user to supply a function that transforms -- the constructor name to a prettier rendering. It is important that -- the transformation function generates a unique output for each -- input. For example, simply converting the string to all lower case -- is not acceptable, because then 'FooBar' and 'Foobar' would be -- indistinguishable. -- -- > $(derivePathInfo' standard ''SiteURL) -- -- see also: 'standard' derivePathInfo' :: (String -> String) -> Name -> Q [Dec] derivePathInfo' formatter name = do c <- parseInfo name case c of Tagged cons cx keys -> do let context = [ mkCtx ''PathInfo [varT key] | key <- keys ] ++ map return cx i <- instanceD (sequence context) (mkType ''PathInfo [mkType name (map varT keys)]) [ toPathSegmentsFn cons , fromPathSegmentsFn cons ] return [i] where #if MIN_VERSION_template_haskell(2,4,0) mkCtx = classP #else mkCtx = mkType #endif toPathSegmentsFn :: [(Name, Int)] -> DecQ toPathSegmentsFn cons = do inp <- newName "inp" let body = caseE (varE inp) $ [ do args <- replicateM nArgs (newName "arg") let matchCon = conP conName (map varP args) conStr = formatter (nameBase conName) match matchCon (normalB (toURLWork conStr args)) [] | (conName, nArgs) <- cons ] toURLWork :: String -> [Name] -> ExpQ toURLWork conStr args = foldr1 (\a b -> appE (appE [| (++) |] a) b) ([| [pack conStr] |] : [ [| toPathSegments $(varE arg) |] | arg <- args ]) funD 'toPathSegments [clause [varP inp] (normalB body) []] fromPathSegmentsFn :: [(Name,Int)] -> DecQ fromPathSegmentsFn cons = do let body = (foldl1 (\a b -> appE (appE [| (<|>) |] a) b) [ parseCon conName nArgs | (conName, nArgs) <- cons]) parseCon :: Name -> Int -> ExpQ parseCon conName nArgs = foldl1 (\a b -> appE (appE [| ap |] a) b) ([| segment (pack $(stringE (formatter $ nameBase conName))) >> return $(conE conName) |] : (replicate nArgs [| fromPathSegments |])) funD 'fromPathSegments [clause [] (normalB body) []] mkType :: Name -> [TypeQ] -> TypeQ mkType con = foldl appT (conT con) data Class = Tagged [(Name, Int)] Cxt [Name] parseInfo :: Name -> Q Class parseInfo name = do info <- reify name case info of TyConI (DataD cx _ keys cs _) -> return $ Tagged (map conInfo cs) cx $ map conv keys TyConI (NewtypeD cx _ keys con _)-> return $ Tagged [conInfo con] cx $ map conv keys _ -> error "Invalid input" where conInfo (NormalC n args) = (n, length args) conInfo (RecC n args) = (n, length args) conInfo (InfixC _ n _) = (n, 2) conInfo (ForallC _ _ con) = conInfo con #if MIN_VERSION_template_haskell(2,4,0) conv (PlainTV nm) = nm conv (KindedTV nm _) = nm #else conv = id #endif -- | the standard formatter -- -- Converts @CamelCase@ to @camel-case@. -- -- see also: 'derivePathInfo' and 'derivePathInfo'' standard :: String -> String standard = intercalate "-" . map (map toLower) . split splitter where splitter = dropInitBlank . keepDelimsL . whenElt $ isUpper mkRoute :: Name -> Q [Dec] mkRoute url = do (Tagged cons _ _) <- parseInfo url fn <- funD (mkName "route") $ map (\(con, numArgs) -> do -- methods <- parseMethods con -- runIO $ print methods args <- replicateM numArgs (newName "arg") clause [conP con $ map varP args] (normalB $ foldl' appE (varE (mkName (headLower (nameBase con)))) (map varE args)) [] ) cons return [fn] where headLower :: String -> String headLower (c:cs) = toLower c : cs -- work in progress parseMethods :: Name -> Q [Name] parseMethods con = do info <- reify con case info of (DataConI _ ty _ _) -> do runIO $ print ty runIO $ print $ lastTerm ty return $ extractMethods (lastTerm ty) extractMethods :: Type -> [Name] extractMethods ty = case ty of (AppT (ConT con) (ConT method)) -> [method] (AppT (ConT con) methods) -> extractMethods' methods where extractMethods' :: Type -> [Name] extractMethods' t = map (\(ConT n) -> n) (leafs t) -- | return the 'Type' after the right-most @->@. Or the original 'Type' if there are no @->@. lastTerm :: Type -> Type lastTerm t@(AppT l r) | hasArrowT l = lastTerm r | otherwise = t lastTerm t = t -- | tests if a 'Type' contains an 'ArrowT' somewhere hasArrowT :: Type -> Bool hasArrowT ArrowT = True hasArrowT (AppT l r) = hasArrowT l || hasArrowT r hasArrowT _ = False leafs :: Type -> [Type] leafs (AppT l@(AppT _ _) r) = leafs l ++ leafs r leafs (AppT _ r) = leafs r leafs t = [t]