kdl-hs-1.1.1: KDL language parser and API
Safe HaskellNone
LanguageGHC2021

KDL.Decoder.Internal.Decoder

Synopsis

Decoder

type Decoder o a = DecodeArrow o () a Source #

DecodeArrow

data DecodeArrow o a b Source #

DecodeArrow o a b represents an arrow with input a and output b, within the context of decoding a KDL object of type o. It also knows the expected schema of o. Most of the time, a is (); it would only be different if you're using Arrows notation.

We're using arrows here so that we can:

  1. Get the schema without running the decoder, and also
  2. Use previously decoded values to inform decoding other values

Using monads alone would lose (1), but applicatives can't do (2).

Constructors

DecodeArrow 

Fields

Instances

Instances details
Category (DecodeArrow o :: Type -> Type -> Type) Source # 
Instance details

Defined in KDL.Decoder.Internal.Decoder

Methods

id :: DecodeArrow o a a #

(.) :: DecodeArrow o b c -> DecodeArrow o a b -> DecodeArrow o a c #

Arrow (DecodeArrow o) Source # 
Instance details

Defined in KDL.Decoder.Internal.Decoder

Methods

arr :: (b -> c) -> DecodeArrow o b c #

first :: DecodeArrow o b c -> DecodeArrow o (b, d) (c, d) #

second :: DecodeArrow o b c -> DecodeArrow o (d, b) (d, c) #

(***) :: DecodeArrow o b c -> DecodeArrow o b' c' -> DecodeArrow o (b, b') (c, c') #

(&&&) :: DecodeArrow o b c -> DecodeArrow o b c' -> DecodeArrow o b (c, c') #

ArrowChoice (DecodeArrow o) Source # 
Instance details

Defined in KDL.Decoder.Internal.Decoder

Methods

left :: DecodeArrow o b c -> DecodeArrow o (Either b d) (Either c d) #

right :: DecodeArrow o b c -> DecodeArrow o (Either d b) (Either d c) #

(+++) :: DecodeArrow o b c -> DecodeArrow o b' c' -> DecodeArrow o (Either b b') (Either c c') #

(|||) :: DecodeArrow o b d -> DecodeArrow o c d -> DecodeArrow o (Either b c) d #

Alternative (DecodeArrow o a) Source # 
Instance details

Defined in KDL.Decoder.Internal.Decoder

Methods

empty :: DecodeArrow o a a0 #

(<|>) :: DecodeArrow o a a0 -> DecodeArrow o a a0 -> DecodeArrow o a a0 #

some :: DecodeArrow o a a0 -> DecodeArrow o a [a0] #

many :: DecodeArrow o a a0 -> DecodeArrow o a [a0] #

Applicative (DecodeArrow o a) Source # 
Instance details

Defined in KDL.Decoder.Internal.Decoder

Methods

pure :: a0 -> DecodeArrow o a a0 #

(<*>) :: DecodeArrow o a (a0 -> b) -> DecodeArrow o a a0 -> DecodeArrow o a b #

liftA2 :: (a0 -> b -> c) -> DecodeArrow o a a0 -> DecodeArrow o a b -> DecodeArrow o a c #

(*>) :: DecodeArrow o a a0 -> DecodeArrow o a b -> DecodeArrow o a b #

(<*) :: DecodeArrow o a a0 -> DecodeArrow o a b -> DecodeArrow o a a0 #

Functor (DecodeArrow o a) Source # 
Instance details

Defined in KDL.Decoder.Internal.Decoder

Methods

fmap :: (a0 -> b) -> DecodeArrow o a a0 -> DecodeArrow o a b #

(<$) :: a0 -> DecodeArrow o a b -> DecodeArrow o a a0 #

Monad (DecodeArrow o a) Source #

Eliminates all schema information; avoid whenever possible.

Instance details

Defined in KDL.Decoder.Internal.Decoder

Methods

(>>=) :: DecodeArrow o a a0 -> (a0 -> DecodeArrow o a b) -> DecodeArrow o a b #

(>>) :: DecodeArrow o a a0 -> DecodeArrow o a b -> DecodeArrow o a b #

return :: a0 -> DecodeArrow o a a0 #

liftDecodeM :: (a -> DecodeM b) -> DecodeArrow o a b Source #

withDecoder :: DecodeArrow o a b -> (b -> DecodeM c) -> DecodeArrow o a c Source #

Run actions within a DecodeArrow. Useful for adding post-processing logic.

Example

Expand
decoder = KDL.withDecoder KDL.number $ \x -> do
  when (x > 100)
    KDL.failM $ "argument is too large: " <> (Text.pack . show) x
  pure $ MyVal x

fail :: forall b o. DecodeArrow o Text b Source #

Unconditionally fail the decoder.

Example

Expand
decoder = proc () -> do
  x <- KDL.arg -< ()
  if x > 100
    then KDL.fail -< "argument is too large: " <> (Text.pack . show) x
    else returnA -< ()
  returnA -< x

debug :: Show o => DecodeArrow o a () Source #

Debug the current state of the object being decoded.

Example

Expand
decoder = proc () -> do
  KDL.debug -< ()    -- Node{entries = [Entry{}, Entry{}]}
  x <- KDL.arg -< ()
  KDL.debug -< ()    -- Node{entries = [Entry{}]}
  y <- KDL.arg -< ()
  KDL.debug -< ()    -- Node{entries = []}
  returnA -< (x, y)

DecodeStateM

DecodeState

data DecodeState o Source #

The state to track when decoding an object of type o.

At each decode step, some value within o is consumed and the action is recorded in the history.

Constructors

DecodeState 

Fields