This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Haoyu Bai

By: a guest on Apr 29th, 2009  |  syntax: Haskell  |  size: 1.34 KB  |  views: 59  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. module Main where
  2.  
  3. import qualified Parser
  4. import StringBuffer
  5. import Lexer
  6. import FastString
  7. import SrcLoc
  8. import DynFlags
  9. import StaticFlagParser
  10. import HscTypes
  11. import GHC
  12. import GHC.SYB.Utils
  13.  
  14. import System
  15. import IO
  16.  
  17. doParse infile dflags = do
  18.     buf <- hGetStringBuffer infile
  19.     let loc = mkSrcLoc (mkFastString infile) 1 0
  20.     return $ unP Parser.parseModule (mkPState buf loc dflags)
  21.  
  22. parseFile infile =
  23.     defaultErrorHandler defaultDynFlags $
  24.     {-defaultCleanupHandler $-}
  25.     runGhc (Just "/usr/lib/ghc-6.10.2/") $ do
  26.         hsc_env <- getSession
  27.         let dflags0 = hsc_dflags hsc_env
  28.         let dflags = dflags0{
  29.             verbosity = 99
  30.         }
  31.         setSessionDynFlags dflags
  32.         handleSourceError (\e -> do
  33.         printExceptionAndWarnings e
  34.         liftIO $ exitWith (ExitFailure 1)) $ do
  35.             liftIO $ doParse infile dflags
  36.  
  37. main :: IO ()
  38. main = do
  39.     args <- getArgs
  40.     let [infile, outfile] = args
  41.     hSetBuffering stdout NoBuffering
  42.     res <- parseFile infile
  43.     case res of
  44.         POk _ x -> bracket (openFile outfile WriteMode) hClose
  45.                              (\h -> hPutStrLn h $ showData Parser 2 x)
  46.         PFailed loc err -> do hPutStrLn stderr $ "Failure when parsing " ++ show infile ++ ": " -- ++ show err
  47.                   {-exitWith (ExitFailure (-1))-}
clone this paste RAW Paste Data