------------------------------------------------------------------------------
-- The ADP Compiler 
-- Copyright (C) 2001-2007 Peter Steffen, Marco Ruether, Christian Lang,
-- Georg Sauthoff
--
-- Send comments/bug reports to: P.Steffen <psteffen@techfak.uni-bielefeld.de>.
-- Updates: http://www.techfak.uni-bielefeld.de/~psteffen/
------------------------------------------------------------------------------


> module ParseMonad where

> import ParseTree

> -- data ParseResult a = Ok { getResult :: a } | Failed (String, Int)
> data ParseResult a = Ok a
>                   | Failed (String, Int)
> type P a = String -> LineNumber -> ParseResult a


> thenP :: P a -> (a -> P b) -> P b
> m `thenP` k = \s li ->
>     case m s li of 
>         Ok a -> k a s li
>         Failed e -> Failed e

> returnP :: a -> P a
> returnP a = \s li -> Ok a

> failP :: (String,Int) -> P a
> failP err = \s li -> Failed err

> catchP :: P a -> ((String,Int) -> P a) -> P a
> catchP m k = \s li ->
>    case m s li of
>       Ok a -> Ok a
>       Failed e -> k e s li


> failed (Failed _) = True
> failed (Ok _    ) = False

> getFailed (Failed s) = s
> getOk     (Ok s)     = s


