-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- Parsing utilities for Swarm.
module Swarm.Util.Parse where

import Control.Applicative (optional)
import Text.Megaparsec (MonadParsec, eof)

-- | Run a parser "fully", consuming leading whitespace and ensuring
--   that the parser extends all the way to eof.
fully :: (MonadParsec e s f) => f () -> f a -> f a
fully :: forall e s (f :: * -> *) a. MonadParsec e s f => f () -> f a -> f a
fully f ()
sc f a
p = f ()
sc forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f a
p forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall e s (m :: * -> *). MonadParsec e s m => m ()
eof

-- | Run a parser "fully", consuming leading whitespace (including the
--   possibility that the input is nothing but whitespace) and
--   ensuring that the parser extends all the way to eof.
fullyMaybe :: (MonadParsec e s f) => f () -> f a -> f (Maybe a)
fullyMaybe :: forall e s (f :: * -> *) a.
MonadParsec e s f =>
f () -> f a -> f (Maybe a)
fullyMaybe f ()
sc = forall e s (f :: * -> *) a. MonadParsec e s f => f () -> f a -> f a
fully f ()
sc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional