@@ -20,6 +20,9 @@ module Parsing
20
20
, fail
21
21
, failWithPosition
22
22
, region
23
+ , liftMaybe
24
+ , liftEither
25
+ , liftExceptT
23
26
, ParseState (..)
24
27
, stateParserT
25
28
, getParserT
@@ -33,6 +36,7 @@ import Control.Alt (class Alt)
33
36
import Control.Apply (lift2 )
34
37
import Control.Lazy (class Lazy )
35
38
import Control.Monad.Error.Class (class MonadError , class MonadThrow , catchError , throwError )
39
+ import Control.Monad.Except (ExceptT , runExceptT )
36
40
import Control.Monad.Reader.Class (class MonadAsk , class MonadReader , ask , local )
37
41
import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM )
38
42
import Control.Monad.State.Class (class MonadState , state )
@@ -43,6 +47,7 @@ import Data.Function.Uncurried (Fn2, Fn5, mkFn2, mkFn3, mkFn5, runFn2, runFn3, r
43
47
import Data.Generic.Rep (class Generic )
44
48
import Data.Identity (Identity )
45
49
import Data.Lazy as Lazy
50
+ import Data.Maybe (Maybe (..))
46
51
import Data.Newtype (unwrap )
47
52
import Data.Show.Generic (genericShow )
48
53
import Data.Tuple (Tuple (..), fst )
@@ -467,4 +472,57 @@ instance Ord Position where
467
472
-- |
468
473
-- | `{ index: 0, line: 1, column: 1 }`
469
474
initialPos :: Position
470
- initialPos = Position { index: 0 , line: 1 , column: 1 }
475
+ initialPos = Position { index: 0 , line: 1 , column: 1 }
476
+
477
+ -- | Lift a `Maybe a` computation into a `ParserT`, with a note for
478
+ -- | the `ParseError` message in case of `Nothing`.
479
+ -- |
480
+ -- | Consumes no parsing input, does not change the parser state at all.
481
+ -- | If the `Maybe` computation is `Nothing`, then this will `fail` in the
482
+ -- | `ParserT` monad with the given error message `String` at the current input
483
+ -- | `Position`.
484
+ -- |
485
+ -- | This is a “validation” function, for when we want to produce some
486
+ -- | data from the parsing input or fail at the current
487
+ -- | parsing position if that’s impossible.
488
+ -- |
489
+ -- | For example:
490
+ -- |
491
+ -- | ```
492
+ -- | runParser "3" do
493
+ -- | myenum :: MyEnum <- tryRethrow do
494
+ -- | x <- intDecimal
495
+ -- | liftMaybe (\_ -> "Bad MyEnum " <> show x) $ toEnum x
496
+ -- | ```
497
+ liftMaybe :: forall s m a . Monad m => (Unit -> String ) -> Maybe a -> ParserT s m a
498
+ liftMaybe message f = case f of
499
+ Nothing -> fail (message unit)
500
+ Just x -> pure x
501
+
502
+ -- | Lift an `Either String a` computation into a `ParserT`.
503
+ -- |
504
+ -- | Consumes no parsing input, does not change the parser state at all.
505
+ -- | If the `Either` computation is `Left String`, then this will `fail` in the
506
+ -- | `ParserT` monad at the current input `Position`.
507
+ -- |
508
+ -- | This is a “validation” function, for when we want to produce some
509
+ -- | data from the parsing input or fail at the current
510
+ -- | parsing position if that’s impossible.
511
+ liftEither :: forall s m a . Monad m => Either String a -> ParserT s m a
512
+ liftEither f = case f of
513
+ Left err -> fail err
514
+ Right x -> pure x
515
+
516
+ -- | Lift an `ExceptT String m a` computation into a `ParserT`.
517
+ -- |
518
+ -- | Consumes no parsing input, does not change the parser state at all.
519
+ -- | If the `ExceptT` computation is `Left String`, then this will `fail` in the
520
+ -- | `ParserT` monad at the current input `Position`.
521
+ -- |
522
+ -- | This is a “validation” function, for when we want to produce some
523
+ -- | data from the parsing input or fail at the current
524
+ -- | parsing position if that’s impossible.
525
+ liftExceptT :: forall s m a . (Monad m ) => ExceptT String m a -> ParserT s m a
526
+ liftExceptT f = lift (runExceptT f) >>= case _ of
527
+ Left err -> fail err
528
+ Right x -> pure x
0 commit comments