QuickAnnotate (empty) → 0.1
raw patch · 6 files changed
+156/−0 lines, 6 filesdep +basedep +haskell-src-extssetup-changed
Dependencies added: base, haskell-src-exts
Files
- Examples/TestPP.hs +20/−0
- LICENSE +30/−0
- QuickAnnotate.cabal +22/−0
- QuickAnnotate.hs +17/−0
- QuickAnnotate/Preprocessor.hs +65/−0
- Setup.hs +2/−0
+ Examples/TestPP.hs view
@@ -0,0 +1,20 @@+{-# OPTIONS_GHC -F -pgmF qapp #-}+{-# LANGUAGE FlexibleInstances #-}++module Examples.TestPP where+import QuickAnnotate+ +data D a =+ C1 + |C2 a + |Ann Loc (D a) + deriving Show+ +instance Annotatable (D a) where annotate = Ann+ +instance Annotatable ([Char]) where annotate loc d = d ++ (show loc) ++t = " hello "++test = C2 1+main = print test
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2012, Shayan Najd++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 Shayan Najd 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.
+ QuickAnnotate.cabal view
@@ -0,0 +1,22 @@+Name: QuickAnnotate+Version: 0.1+Synopsis: Annotation Framework+Homepage: darcs repository http://code.haskell.org/QuickAnnotate/+License: BSD3 +License-file: LICENSE+Author: Shayan Najd+Maintainer: sh.najd@gmail.com+Category: Development+Build-type: Simple+Cabal-version: >=1.2+description: A framework introducing annotation by preprocessing ++Executable qapp+ Main-is: QuickAnnotate/Preprocessor.hs + Build-depends: base >= 4 && < 4.5, + haskell-src-exts >= 1.11.1 && < 1.12 + Other-modules: Examples.TestPP + +Library + Exposed-modules: QuickAnnotate + Build-depends: base >= 4 && < 4.5
+ QuickAnnotate.hs view
@@ -0,0 +1,17 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE IncoherentInstances #-}+{-# LANGUAGE OverlappingInstances #-}++module QuickAnnotate where ++type Loc = String ++class Annotatable a where + annotate :: Loc -> a -> a ++instance Annotatable a where+ annotate _ = id++instance Annotatable b => Annotatable (a -> b) where+ annotate l f = \x -> annotate l (f x) +
+ QuickAnnotate/Preprocessor.hs view
@@ -0,0 +1,65 @@+--module QuickAnnotate.Preprocessor where++import Language.Haskell.Exts+import System.Environment (getArgs)+++annotationFunction :: String+annotationFunction = "annotate"++-- | Borrowed from HRX project +main :: IO ()+main = do args <- getArgs+ case args of+ [origfile, infile, outfile] -> transformFile origfile infile outfile+ [infile, outfile] -> transformFile infile infile outfile+ [infile] -> testFile infile+ _ -> putStrLn usageString++usageString :: String+usageString = "Usage: pp <infile> [<outfile>]"+++transformFile :: String -> String -> String -> IO ()+transformFile origfile infile outfile = do+ f <- readFile infile+ let fm = process origfile f+ writeFile outfile fm++testFile :: String -> IO ()+testFile file = do+ f <- readFile file+ putStrLn $ process file f++process :: FilePath -> String -> String+process fp fc = let mode = ParseMode {+ parseFilename = fp,+ extensions = [],+ ignoreLanguagePragmas = False,+ ignoreLinePragmas = True,+ fixities = Just preludeFixities+ }+ ParseOk s = parseFileContentsWithMode mode fc+ in prettyPrint $ transModule $ s+ +transModule :: Module -> Module+transModule (Module srcLoc moduleName modulePragmas mWarningText mExportSpecs importDecls decs) + = Module srcLoc moduleName modulePragmas mWarningText mExportSpecs importDecls (transDecl `map` decs)++transDecl :: Decl -> Decl +transDecl (FunBind mtchs) = FunBind (transMatch`map`mtchs)+transDecl (PatBind srcLoc pat mtype rhs binds) = PatBind srcLoc pat mtype (transRhs srcLoc rhs) binds+transDecl x = x ++transMatch :: Match -> Match+transMatch (Match srcLoc n ps mts rhs binds) =Match srcLoc n ps mts (transRhs srcLoc rhs) binds++transRhs :: SrcLoc -> Rhs -> Rhs+transRhs srcLoc (UnGuardedRhs exp) = UnGuardedRhs (transExp srcLoc exp)+transRhs _ (GuardedRhss guardedRhss)= GuardedRhss (transG `map` guardedRhss)++transG :: GuardedRhs -> GuardedRhs+transG (GuardedRhs gSrcLoc stmts exp) = GuardedRhs gSrcLoc stmts (transExp gSrcLoc exp)++transExp :: SrcLoc -> Exp -> Exp+transExp loc exp = (App (App (Var (UnQual (Ident annotationFunction))) (Lit (String $ show loc))) (Paren exp))
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain