-------------------------------------------------------------------------------- -- $Id: RepMonad.hs,v 1.1 2004/03/26 12:18:05 graham Exp $ -- -- Copyright (c) 2004, G. KLYNE. All rights reserved. -- See end of this file for licence information. -------------------------------------------------------------------------------- -- | -- Module : RepMonad -- Copyright : (c) 2004, Graham Klyne -- License : GPL V2 -- -- Maintainer : Graham Klyne -- Stability : provisional -- Portability : H98 -- -- RepMonad: Composed state and IO monad for RepToRDF -- -------------------------------------------------------------------------------- module RepMonad ( RepStateIO, RepState(..) , setFormat, setGraph , setInfo, resetInfo, setError, resetError, setExitcode , emptyState , RepFormat(..) , repError , reportLines, reportLine ) where import RDFGraph ( RDFGraph, emptyRDFGraph ) import Control.Monad.Trans ( MonadTrans(..) ) import Control.Monad.State ( MonadState(..), modify, StateT(..), execStateT ) import System ( ExitCode(ExitSuccess,ExitFailure) ) import IO ( hPutStrLn, stderr ) ------------------------------------------------------------ -- State and state monad for RepToRDF program ------------------------------------------------------------ -- -- Uses StateT Monad transformer: -- See example by Mark Carroll at http://www.haskell.org/hawiki/MonadState data RepFormat = N3 | NT | RDF deriving (Eq, Show) data RepState = RepState { format :: RepFormat , graph :: RDFGraph -- current graph , infomsg :: Maybe String -- information message, or Nothing , errormsg :: Maybe String -- error message, or Nothing , exitcode :: ExitCode } type RepStateIO a = StateT RepState IO a emptyState = RepState { format = N3 , graph = emptyRDFGraph , infomsg = Nothing , errormsg = Nothing , exitcode = ExitSuccess } setFormat :: RepFormat -> RepState -> RepState setFormat fm state = state { format = fm } setGraph :: RDFGraph -> RepState -> RepState setGraph gr state = state { graph = gr } setInfo :: String -> RepState -> RepState setInfo msg state = state { infomsg = Just msg } resetInfo :: RepState -> RepState resetInfo state = state { infomsg = Nothing } setError :: String -> RepState -> RepState setError msg state = state { errormsg = Just msg } resetError :: RepState -> RepState resetError state = state { errormsg = Nothing } setExitcode :: ExitCode -> RepState -> RepState setExitcode ec state = state { exitcode = ec } ------------------------------------------------------------ -- Report error and set exit status code ------------------------------------------------------------ repError :: String -> Int -> RepStateIO () repError msg sts = do { reportLine $ msg ; if sts == 4 then reportLine $ "Use 'RepToRDF -?' for help" else return () ; modify $ setExitcode (ExitFailure sts) } ------------------------------------------------------------ -- Output text to the standard error stream ------------------------------------------------------------ -- -- Each string in the supplied list is a line of text to -- be displayed. reportLines :: [String] -> RepStateIO () reportLines text = sequence_ (map reportLine text) reportLine :: String -> RepStateIO () reportLine line = -- lift putStrLn line lift $ hPutStrLn stderr line -------------------------------------------------------------------------------- -- -- Copyright (c) 2004, G. KLYNE. All rights reserved. -- -- This is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This software is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with RepToRDF; if not, write to: -- The Free Software Foundation, Inc., -- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -- or view the web page at: -- http://www.gnu.org/copyleft/gpl.html -- -------------------------------------------------------------------------------- -- $Source: /file/cvsdev/CompileRDF/RepMonad.hs,v $ -- $Author: graham $ -- $Revision: 1.1 $ -- $Log: RepMonad.hs,v $ -- Revision 1.1 2004/03/26 12:18:05 graham -- Created report description compiler --