{-# LANGUAGE RankNTypes #-}
{-|
Module      : Toml.FromValue.Matcher
Description : A type for building results while tracking scopes
Copyright   : (c) Eric Mertens, 2023
License     : ISC
Maintainer  : emertens@gmail.com

This type helps to build up computations that can validate a TOML
value and compute some application-specific representation.

It supports warning messages which can be used to deprecate old
configuration options and to detect unused table keys.

It supports tracking multiple error messages when you have more
than one decoding option and all of them have failed.

Use 'Toml.Pretty.prettyMatchMessage' for an easy way to make human
readable strings from matcher outputs.

-}
module Toml.FromValue.Matcher (
    -- * Types
    Matcher,
    Result(..),
    MatchMessage(..),

    -- * Operations
    runMatcher,
    withScope,
    getScope,
    warning,

    -- * Scope helpers
    Scope(..),
    inKey,
    inIndex,
    ) where

import Control.Applicative (Alternative(..))
import Control.Monad (MonadPlus, ap, liftM)
import Data.Monoid (Endo(..))

-- | Computations that result in a 'Result' and which track a list
-- of nested contexts to assist in generating warnings and error
-- messages.
--
-- Use 'withScope' to run a 'Matcher' in a new, nested scope.
newtype Matcher a = Matcher {
    forall a.
Matcher a
-> forall r.
   [Scope]
   -> DList MatchMessage
   -> (DList MatchMessage -> r)
   -> (DList MatchMessage -> a -> r)
   -> r
unMatcher ::
        forall r.
        [Scope] ->
        DList MatchMessage ->
        (DList MatchMessage -> r) ->
        (DList MatchMessage -> a -> r) ->
        r
    }

instance Functor Matcher where
    fmap :: forall a b. (a -> b) -> Matcher a -> Matcher b
fmap = (a -> b) -> Matcher a -> Matcher b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM

instance Applicative Matcher where
    pure :: forall a. a -> Matcher a
pure a
x = (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
_env DList MatchMessage
warn DList MatchMessage -> r
_err DList MatchMessage -> a -> r
ok -> DList MatchMessage -> a -> r
ok DList MatchMessage
warn a
x)
    <*> :: forall a b. Matcher (a -> b) -> Matcher a -> Matcher b
(<*>) = Matcher (a -> b) -> Matcher a -> Matcher b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad Matcher where
    Matcher a
m >>= :: forall a b. Matcher a -> (a -> Matcher b) -> Matcher b
>>= a -> Matcher b
f = (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> b -> r)
 -> r)
-> Matcher b
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
env DList MatchMessage
warn DList MatchMessage -> r
err DList MatchMessage -> b -> r
ok -> Matcher a
-> forall r.
   [Scope]
   -> DList MatchMessage
   -> (DList MatchMessage -> r)
   -> (DList MatchMessage -> a -> r)
   -> r
forall a.
Matcher a
-> forall r.
   [Scope]
   -> DList MatchMessage
   -> (DList MatchMessage -> r)
   -> (DList MatchMessage -> a -> r)
   -> r
unMatcher Matcher a
m [Scope]
env DList MatchMessage
warn DList MatchMessage -> r
err (\DList MatchMessage
warn' a
x -> Matcher b
-> forall r.
   [Scope]
   -> DList MatchMessage
   -> (DList MatchMessage -> r)
   -> (DList MatchMessage -> b -> r)
   -> r
forall a.
Matcher a
-> forall r.
   [Scope]
   -> DList MatchMessage
   -> (DList MatchMessage -> r)
   -> (DList MatchMessage -> a -> r)
   -> r
unMatcher (a -> Matcher b
f a
x) [Scope]
env DList MatchMessage
warn' DList MatchMessage -> r
err DList MatchMessage -> b -> r
ok))
    {-# INLINE (>>=) #-}

instance Alternative Matcher where
    empty :: forall a. Matcher a
empty = (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
_env DList MatchMessage
_warn DList MatchMessage -> r
err DList MatchMessage -> a -> r
_ok -> DList MatchMessage -> r
err DList MatchMessage
forall a. Monoid a => a
mempty)
    Matcher forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
x <|> :: forall a. Matcher a -> Matcher a -> Matcher a
<|> Matcher forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
y = (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
env DList MatchMessage
warn DList MatchMessage -> r
err DList MatchMessage -> a -> r
ok -> [Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
x [Scope]
env DList MatchMessage
warn (\DList MatchMessage
errs1 -> [Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
y [Scope]
env DList MatchMessage
warn (\DList MatchMessage
errs2 -> DList MatchMessage -> r
err (DList MatchMessage
errs1 DList MatchMessage -> DList MatchMessage -> DList MatchMessage
forall a. Semigroup a => a -> a -> a
<> DList MatchMessage
errs2)) DList MatchMessage -> a -> r
ok) DList MatchMessage -> a -> r
ok)

instance MonadPlus Matcher

-- | Scopes for TOML message.
--
-- @since 1.3.0.0
data Scope
    = ScopeIndex Int -- ^ zero-based array index
    | ScopeKey String -- ^ key in a table
    deriving (
        ReadPrec [Scope]
ReadPrec Scope
Int -> ReadS Scope
ReadS [Scope]
(Int -> ReadS Scope)
-> ReadS [Scope]
-> ReadPrec Scope
-> ReadPrec [Scope]
-> Read Scope
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Scope
readsPrec :: Int -> ReadS Scope
$creadList :: ReadS [Scope]
readList :: ReadS [Scope]
$creadPrec :: ReadPrec Scope
readPrec :: ReadPrec Scope
$creadListPrec :: ReadPrec [Scope]
readListPrec :: ReadPrec [Scope]
Read {- ^ Default instance -},
        Int -> Scope -> ShowS
[Scope] -> ShowS
Scope -> String
(Int -> Scope -> ShowS)
-> (Scope -> String) -> ([Scope] -> ShowS) -> Show Scope
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Scope -> ShowS
showsPrec :: Int -> Scope -> ShowS
$cshow :: Scope -> String
show :: Scope -> String
$cshowList :: [Scope] -> ShowS
showList :: [Scope] -> ShowS
Show {- ^ Default instance -},
        Scope -> Scope -> Bool
(Scope -> Scope -> Bool) -> (Scope -> Scope -> Bool) -> Eq Scope
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Scope -> Scope -> Bool
== :: Scope -> Scope -> Bool
$c/= :: Scope -> Scope -> Bool
/= :: Scope -> Scope -> Bool
Eq   {- ^ Default instance -},
        Eq Scope
Eq Scope =>
(Scope -> Scope -> Ordering)
-> (Scope -> Scope -> Bool)
-> (Scope -> Scope -> Bool)
-> (Scope -> Scope -> Bool)
-> (Scope -> Scope -> Bool)
-> (Scope -> Scope -> Scope)
-> (Scope -> Scope -> Scope)
-> Ord Scope
Scope -> Scope -> Bool
Scope -> Scope -> Ordering
Scope -> Scope -> Scope
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Scope -> Scope -> Ordering
compare :: Scope -> Scope -> Ordering
$c< :: Scope -> Scope -> Bool
< :: Scope -> Scope -> Bool
$c<= :: Scope -> Scope -> Bool
<= :: Scope -> Scope -> Bool
$c> :: Scope -> Scope -> Bool
> :: Scope -> Scope -> Bool
$c>= :: Scope -> Scope -> Bool
>= :: Scope -> Scope -> Bool
$cmax :: Scope -> Scope -> Scope
max :: Scope -> Scope -> Scope
$cmin :: Scope -> Scope -> Scope
min :: Scope -> Scope -> Scope
Ord  {- ^ Default instance -})

-- | A message emitted while matching a TOML value. The message is paired
-- with the path to the value that was in focus when the message was
-- generated. These message get used for both warnings and errors.
--
-- @since 1.3.0.0
data MatchMessage = MatchMessage {
    MatchMessage -> [Scope]
matchPath :: [Scope], -- ^ path to message location
    MatchMessage -> String
matchMessage :: String -- ^ error and warning message body
    } deriving (
        ReadPrec [MatchMessage]
ReadPrec MatchMessage
Int -> ReadS MatchMessage
ReadS [MatchMessage]
(Int -> ReadS MatchMessage)
-> ReadS [MatchMessage]
-> ReadPrec MatchMessage
-> ReadPrec [MatchMessage]
-> Read MatchMessage
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS MatchMessage
readsPrec :: Int -> ReadS MatchMessage
$creadList :: ReadS [MatchMessage]
readList :: ReadS [MatchMessage]
$creadPrec :: ReadPrec MatchMessage
readPrec :: ReadPrec MatchMessage
$creadListPrec :: ReadPrec [MatchMessage]
readListPrec :: ReadPrec [MatchMessage]
Read {- ^ Default instance -},
        Int -> MatchMessage -> ShowS
[MatchMessage] -> ShowS
MatchMessage -> String
(Int -> MatchMessage -> ShowS)
-> (MatchMessage -> String)
-> ([MatchMessage] -> ShowS)
-> Show MatchMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MatchMessage -> ShowS
showsPrec :: Int -> MatchMessage -> ShowS
$cshow :: MatchMessage -> String
show :: MatchMessage -> String
$cshowList :: [MatchMessage] -> ShowS
showList :: [MatchMessage] -> ShowS
Show {- ^ Default instance -},
        MatchMessage -> MatchMessage -> Bool
(MatchMessage -> MatchMessage -> Bool)
-> (MatchMessage -> MatchMessage -> Bool) -> Eq MatchMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MatchMessage -> MatchMessage -> Bool
== :: MatchMessage -> MatchMessage -> Bool
$c/= :: MatchMessage -> MatchMessage -> Bool
/= :: MatchMessage -> MatchMessage -> Bool
Eq   {- ^ Default instance -},
        Eq MatchMessage
Eq MatchMessage =>
(MatchMessage -> MatchMessage -> Ordering)
-> (MatchMessage -> MatchMessage -> Bool)
-> (MatchMessage -> MatchMessage -> Bool)
-> (MatchMessage -> MatchMessage -> Bool)
-> (MatchMessage -> MatchMessage -> Bool)
-> (MatchMessage -> MatchMessage -> MatchMessage)
-> (MatchMessage -> MatchMessage -> MatchMessage)
-> Ord MatchMessage
MatchMessage -> MatchMessage -> Bool
MatchMessage -> MatchMessage -> Ordering
MatchMessage -> MatchMessage -> MatchMessage
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MatchMessage -> MatchMessage -> Ordering
compare :: MatchMessage -> MatchMessage -> Ordering
$c< :: MatchMessage -> MatchMessage -> Bool
< :: MatchMessage -> MatchMessage -> Bool
$c<= :: MatchMessage -> MatchMessage -> Bool
<= :: MatchMessage -> MatchMessage -> Bool
$c> :: MatchMessage -> MatchMessage -> Bool
> :: MatchMessage -> MatchMessage -> Bool
$c>= :: MatchMessage -> MatchMessage -> Bool
>= :: MatchMessage -> MatchMessage -> Bool
$cmax :: MatchMessage -> MatchMessage -> MatchMessage
max :: MatchMessage -> MatchMessage -> MatchMessage
$cmin :: MatchMessage -> MatchMessage -> MatchMessage
min :: MatchMessage -> MatchMessage -> MatchMessage
Ord  {- ^ Default instance -})

-- | List of strings that supports efficient left- and right-biased append
newtype DList a = DList (Endo [a])
    deriving (NonEmpty (DList a) -> DList a
DList a -> DList a -> DList a
(DList a -> DList a -> DList a)
-> (NonEmpty (DList a) -> DList a)
-> (forall b. Integral b => b -> DList a -> DList a)
-> Semigroup (DList a)
forall b. Integral b => b -> DList a -> DList a
forall a. NonEmpty (DList a) -> DList a
forall a. DList a -> DList a -> DList a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall a b. Integral b => b -> DList a -> DList a
$c<> :: forall a. DList a -> DList a -> DList a
<> :: DList a -> DList a -> DList a
$csconcat :: forall a. NonEmpty (DList a) -> DList a
sconcat :: NonEmpty (DList a) -> DList a
$cstimes :: forall a b. Integral b => b -> DList a -> DList a
stimes :: forall b. Integral b => b -> DList a -> DList a
Semigroup, Semigroup (DList a)
DList a
Semigroup (DList a) =>
DList a
-> (DList a -> DList a -> DList a)
-> ([DList a] -> DList a)
-> Monoid (DList a)
[DList a] -> DList a
DList a -> DList a -> DList a
forall a. Semigroup (DList a)
forall a. DList a
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall a. [DList a] -> DList a
forall a. DList a -> DList a -> DList a
$cmempty :: forall a. DList a
mempty :: DList a
$cmappend :: forall a. DList a -> DList a -> DList a
mappend :: DList a -> DList a -> DList a
$cmconcat :: forall a. [DList a] -> DList a
mconcat :: [DList a] -> DList a
Monoid)

-- | Create a singleton list of strings
one :: a -> DList a
one :: forall a. a -> DList a
one a
x = Endo [a] -> DList a
forall a. Endo [a] -> DList a
DList (([a] -> [a]) -> Endo [a]
forall a. (a -> a) -> Endo a
Endo (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:))

-- | Extract the list of strings
runDList :: DList a -> [a]
runDList :: forall a. DList a -> [a]
runDList (DList Endo [a]
x) = Endo [a]
x Endo [a] -> [a] -> [a]
forall a. Endo a -> a -> a
`appEndo` []

-- | Computation outcome with error and warning messages. Multiple error
-- messages can occur when multiple alternatives all fail. Resolving any
-- one of the error messages could allow the computation to succeed.
--
-- @since 1.3.0.0
data Result e a
    = Failure [e]   -- ^ error messages
    | Success [e] a -- ^ warning messages and result
    deriving (
        ReadPrec [Result e a]
ReadPrec (Result e a)
Int -> ReadS (Result e a)
ReadS [Result e a]
(Int -> ReadS (Result e a))
-> ReadS [Result e a]
-> ReadPrec (Result e a)
-> ReadPrec [Result e a]
-> Read (Result e a)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall e a. (Read e, Read a) => ReadPrec [Result e a]
forall e a. (Read e, Read a) => ReadPrec (Result e a)
forall e a. (Read e, Read a) => Int -> ReadS (Result e a)
forall e a. (Read e, Read a) => ReadS [Result e a]
$creadsPrec :: forall e a. (Read e, Read a) => Int -> ReadS (Result e a)
readsPrec :: Int -> ReadS (Result e a)
$creadList :: forall e a. (Read e, Read a) => ReadS [Result e a]
readList :: ReadS [Result e a]
$creadPrec :: forall e a. (Read e, Read a) => ReadPrec (Result e a)
readPrec :: ReadPrec (Result e a)
$creadListPrec :: forall e a. (Read e, Read a) => ReadPrec [Result e a]
readListPrec :: ReadPrec [Result e a]
Read {- ^ Default instance -},
        Int -> Result e a -> ShowS
[Result e a] -> ShowS
Result e a -> String
(Int -> Result e a -> ShowS)
-> (Result e a -> String)
-> ([Result e a] -> ShowS)
-> Show (Result e a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e a. (Show e, Show a) => Int -> Result e a -> ShowS
forall e a. (Show e, Show a) => [Result e a] -> ShowS
forall e a. (Show e, Show a) => Result e a -> String
$cshowsPrec :: forall e a. (Show e, Show a) => Int -> Result e a -> ShowS
showsPrec :: Int -> Result e a -> ShowS
$cshow :: forall e a. (Show e, Show a) => Result e a -> String
show :: Result e a -> String
$cshowList :: forall e a. (Show e, Show a) => [Result e a] -> ShowS
showList :: [Result e a] -> ShowS
Show {- ^ Default instance -},
        Result e a -> Result e a -> Bool
(Result e a -> Result e a -> Bool)
-> (Result e a -> Result e a -> Bool) -> Eq (Result e a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall e a. (Eq e, Eq a) => Result e a -> Result e a -> Bool
$c== :: forall e a. (Eq e, Eq a) => Result e a -> Result e a -> Bool
== :: Result e a -> Result e a -> Bool
$c/= :: forall e a. (Eq e, Eq a) => Result e a -> Result e a -> Bool
/= :: Result e a -> Result e a -> Bool
Eq   {- ^ Default instance -},
        Eq (Result e a)
Eq (Result e a) =>
(Result e a -> Result e a -> Ordering)
-> (Result e a -> Result e a -> Bool)
-> (Result e a -> Result e a -> Bool)
-> (Result e a -> Result e a -> Bool)
-> (Result e a -> Result e a -> Bool)
-> (Result e a -> Result e a -> Result e a)
-> (Result e a -> Result e a -> Result e a)
-> Ord (Result e a)
Result e a -> Result e a -> Bool
Result e a -> Result e a -> Ordering
Result e a -> Result e a -> Result e a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall e a. (Ord e, Ord a) => Eq (Result e a)
forall e a. (Ord e, Ord a) => Result e a -> Result e a -> Bool
forall e a. (Ord e, Ord a) => Result e a -> Result e a -> Ordering
forall e a.
(Ord e, Ord a) =>
Result e a -> Result e a -> Result e a
$ccompare :: forall e a. (Ord e, Ord a) => Result e a -> Result e a -> Ordering
compare :: Result e a -> Result e a -> Ordering
$c< :: forall e a. (Ord e, Ord a) => Result e a -> Result e a -> Bool
< :: Result e a -> Result e a -> Bool
$c<= :: forall e a. (Ord e, Ord a) => Result e a -> Result e a -> Bool
<= :: Result e a -> Result e a -> Bool
$c> :: forall e a. (Ord e, Ord a) => Result e a -> Result e a -> Bool
> :: Result e a -> Result e a -> Bool
$c>= :: forall e a. (Ord e, Ord a) => Result e a -> Result e a -> Bool
>= :: Result e a -> Result e a -> Bool
$cmax :: forall e a.
(Ord e, Ord a) =>
Result e a -> Result e a -> Result e a
max :: Result e a -> Result e a -> Result e a
$cmin :: forall e a.
(Ord e, Ord a) =>
Result e a -> Result e a -> Result e a
min :: Result e a -> Result e a -> Result e a
Ord  {- ^ Default instance -})

-- | Run a 'Matcher' with an empty scope.
--
-- @since 1.3.0.0
runMatcher :: Matcher a -> Result MatchMessage a
runMatcher :: forall a. Matcher a -> Result MatchMessage a
runMatcher (Matcher forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
m) = [Scope]
-> DList MatchMessage
-> (DList MatchMessage -> Result MatchMessage a)
-> (DList MatchMessage -> a -> Result MatchMessage a)
-> Result MatchMessage a
forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
m [] DList MatchMessage
forall a. Monoid a => a
mempty ([MatchMessage] -> Result MatchMessage a
forall e a. [e] -> Result e a
Failure ([MatchMessage] -> Result MatchMessage a)
-> (DList MatchMessage -> [MatchMessage])
-> DList MatchMessage
-> Result MatchMessage a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DList MatchMessage -> [MatchMessage]
forall a. DList a -> [a]
runDList) ([MatchMessage] -> a -> Result MatchMessage a
forall e a. [e] -> a -> Result e a
Success ([MatchMessage] -> a -> Result MatchMessage a)
-> (DList MatchMessage -> [MatchMessage])
-> DList MatchMessage
-> a
-> Result MatchMessage a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DList MatchMessage -> [MatchMessage]
forall a. DList a -> [a]
runDList)

-- | Run a 'Matcher' with a locally extended scope.
--
-- @since 1.3.0.0
withScope :: Scope -> Matcher a -> Matcher a
withScope :: forall a. Scope -> Matcher a -> Matcher a
withScope Scope
ctx (Matcher forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
m) = (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
env -> [Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
forall r.
[Scope]
-> DList MatchMessage
-> (DList MatchMessage -> r)
-> (DList MatchMessage -> a -> r)
-> r
m (Scope
ctx Scope -> [Scope] -> [Scope]
forall a. a -> [a] -> [a]
: [Scope]
env))

-- | Get the current list of scopes.
--
-- @since 1.3.0.0
getScope :: Matcher [Scope]
getScope :: Matcher [Scope]
getScope = (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> [Scope] -> r)
 -> r)
-> Matcher [Scope]
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
env DList MatchMessage
warn DList MatchMessage -> r
_err DList MatchMessage -> [Scope] -> r
ok -> DList MatchMessage -> [Scope] -> r
ok DList MatchMessage
warn ([Scope] -> [Scope]
forall a. [a] -> [a]
reverse [Scope]
env))

-- | Emit a warning mentioning the current scope.
warning :: String -> Matcher ()
warning :: String -> Matcher ()
warning String
w =
 do [Scope]
loc <- Matcher [Scope]
getScope
    (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> () -> r)
 -> r)
-> Matcher ()
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
_env DList MatchMessage
warn DList MatchMessage -> r
_err DList MatchMessage -> () -> r
ok -> DList MatchMessage -> () -> r
ok (DList MatchMessage
warn DList MatchMessage -> DList MatchMessage -> DList MatchMessage
forall a. Semigroup a => a -> a -> a
<> MatchMessage -> DList MatchMessage
forall a. a -> DList a
one ([Scope] -> String -> MatchMessage
MatchMessage [Scope]
loc String
w)) ())

-- | Fail with an error message annotated to the current location.
instance MonadFail Matcher where
    fail :: forall a. String -> Matcher a
fail String
e =
     do [Scope]
loc <- Matcher [Scope]
getScope
        (forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
forall a.
(forall r.
 [Scope]
 -> DList MatchMessage
 -> (DList MatchMessage -> r)
 -> (DList MatchMessage -> a -> r)
 -> r)
-> Matcher a
Matcher (\[Scope]
_env DList MatchMessage
_warn DList MatchMessage -> r
err DList MatchMessage -> a -> r
_ok -> DList MatchMessage -> r
err (MatchMessage -> DList MatchMessage
forall a. a -> DList a
one ([Scope] -> String -> MatchMessage
MatchMessage [Scope]
loc String
e)))

-- | Update the scope with the message corresponding to a table key
--
-- @since 1.3.0.0
inKey :: String -> Matcher a -> Matcher a
inKey :: forall a. String -> Matcher a -> Matcher a
inKey = Scope -> Matcher a -> Matcher a
forall a. Scope -> Matcher a -> Matcher a
withScope (Scope -> Matcher a -> Matcher a)
-> (String -> Scope) -> String -> Matcher a -> Matcher a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Scope
ScopeKey

-- | Update the scope with the message corresponding to an array index
--
-- @since 1.3.0.0
inIndex :: Int -> Matcher a -> Matcher a
inIndex :: forall a. Int -> Matcher a -> Matcher a
inIndex = Scope -> Matcher a -> Matcher a
forall a. Scope -> Matcher a -> Matcher a
withScope (Scope -> Matcher a -> Matcher a)
-> (Int -> Scope) -> Int -> Matcher a -> Matcher a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Scope
ScopeIndex