packages feed

BirdPP-1.0: Main.lhs

| This file is for making Haddock cooperate with Bird-style literate
comments.  The part for gluing the simple preprocessor into the
ghc pipeline is from "Syzygies"

http://hackage.haskell.org/trac/ghc/attachment/ticket/2776/dedent.lhs

Usage: ghc -pgmL BirdPP ...

This file contains some gratuitous Haddock markup so you can see it
in action.  

> module Main 
>   ( -- * The preprocessor
>     literize 
>     -- * The glue
>   , main )
> where

> import qualified System
> import qualified System.IO as IO
> import qualified System.Environment as Env
> import qualified Monad

| This comment will show up in the Haddock docs.

Replace literate comment lines by "-- " so Haddock can
understand that lines beginning with | are haddock comments.

> literize :: String -> String
> literize ('>' : ' ' : s) = s
> literize s = "-- " ++ s

| Main runs the preprocessor.  

> main :: IO ()
> main = do 
>   args <- System.getArgs

If we are calling BirdPP in the GHC pipeline, it passes a number
of arguments (see above URL).

>   if length args >= 3 then 
>     do let [orig, src, out] = drop (length args - 3) args
>        fi <- IO.openFile src IO.ReadMode
>        fo <- IO.openFile out IO.WriteMode
>        IO.hPutStrLn fo $ "{-# LINE 1 \"" ++ orig ++ "\" #-}"
>        text <- IO.hGetContents fi
>        mapM_ (IO.hPutStrLn fo . literize) (lines text)
>        IO.hClose fi
>        IO.hClose fo

To see the result of the preprocessor (for debugging) you can
just call BirdPP with a file to process.

>     else case args of 
>            [f] -> do text <- IO.readFile f
>                      mapM_ (putStrLn . literize) (lines text)
>                      return ()
>            _ -> putStrLn "usage: BirdPP file"