@@ -20,6 +20,9 @@ module Parsing
2020 , fail
2121 , failWithPosition
2222 , region
23+ , liftMaybe
24+ , liftEither
25+ , liftExceptT
2326 , ParseState (..)
2427 , stateParserT
2528 , getParserT
@@ -33,6 +36,7 @@ import Control.Alt (class Alt)
3336import Control.Apply (lift2 )
3437import Control.Lazy (class Lazy )
3538import Control.Monad.Error.Class (class MonadError , class MonadThrow , catchError , throwError )
39+ import Control.Monad.Except (ExceptT , runExceptT )
3640import Control.Monad.Reader.Class (class MonadAsk , class MonadReader , ask , local )
3741import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM )
3842import Control.Monad.State.Class (class MonadState , state )
@@ -43,6 +47,7 @@ import Data.Function.Uncurried (Fn2, Fn5, mkFn2, mkFn3, mkFn5, runFn2, runFn3, r
4347import Data.Generic.Rep (class Generic )
4448import Data.Identity (Identity )
4549import Data.Lazy as Lazy
50+ import Data.Maybe (Maybe (..))
4651import Data.Newtype (unwrap )
4752import Data.Show.Generic (genericShow )
4853import Data.Tuple (Tuple (..), fst )
@@ -441,4 +446,49 @@ instance Ord Position where
441446-- |
442447-- | `{ index: 0, line: 1, column: 1 }`
443448initialPos :: Position
444- initialPos = Position { index: 0 , line: 1 , column: 1 }
449+ initialPos = Position { index: 0 , line: 1 , column: 1 }
450+
451+
452+ -- | Lift a `Maybe a` computation into a `ParserT`, with a note for
453+ -- | the `ParseError` message in case of `Nothing`.
454+ -- |
455+ -- | Consumes no parsing input, does not change the parser state at all.
456+ -- | If the `Maybe` computation is `Nothing`, then this will `fail` in the
457+ -- | `ParserT` monad with the given error message `String` at the current input
458+ -- | `Position`.
459+ -- |
460+ -- | This is a “validation” function, for when we want to produce some
461+ -- | data from the parsing input or fail at the current
462+ -- | parsing position if that’s impossible.
463+ liftMaybe :: forall s m a . Monad m => Lazy.Lazy String -> Maybe a -> ParserT s m a
464+ liftMaybe message f = case f of
465+ Nothing -> fail (Lazy .force message)
466+ Just x -> pure x
467+
468+ -- | Lift an `Either String a` computation into a `ParserT`.
469+ -- |
470+ -- | Consumes no parsing input, does not change the parser state at all.
471+ -- | If the `Either` computation is `Left String`, then this will `fail` in the
472+ -- | `ParserT` monad at the current input `Position`.
473+ -- |
474+ -- | This is a “validation” function, for when we want to produce some
475+ -- | data from the parsing input or fail at the current
476+ -- | parsing position if that’s impossible.
477+ liftEither :: forall s m a . Monad m => Either String a -> ParserT s m a
478+ liftEither f = case f of
479+ Left err -> fail err
480+ Right x -> pure x
481+
482+ -- | Lift an `ExceptT String m a` computation into a `ParserT`.
483+ -- |
484+ -- | Consumes no parsing input, does not change the parser state at all.
485+ -- | If the `ExceptT` computation is `Left String`, then this will `fail` in the
486+ -- | `ParserT` monad at the current input `Position`.
487+ -- |
488+ -- | This is a “validation” function, for when we want to produce some
489+ -- | data from the parsing input or fail at the current
490+ -- | parsing position if that’s impossible.
491+ liftExceptT :: forall s m a . (Monad m ) => ExceptT String m a -> ParserT s m a
492+ liftExceptT f = lift (runExceptT f) >>= case _ of
493+ Left err -> fail err
494+ Right x -> pure x
0 commit comments