{-# LANGUAGE CPP #-}

module UU.Parsing.Merge((<||>), pMerged, list_of) where

import UU.Parsing

-- ==== merging
-- e.g. chars_digs = cat3 `pMerged` (list_of pDig <||> list_of pL <||> list_of pU)
--      parsing "12abCD1aV" now returns "121abaCDV", so the sequence of
-- recognised elements is stored in three lists, which are then passed to cat3

(<||>) :: IsParser p s => (c,p (d -> d),e -> f -> g) -> (h,p (i -> i),g -> j -> k) -> ((c,h),p ((d,i) -> (d,i)),e -> (f,j) -> k)
(c
pe, p (d -> d)
pp, e -> f -> g
punp) <||> :: forall (p :: * -> *) s c d e f g h i j k.
IsParser p s =>
(c, p (d -> d), e -> f -> g)
-> (h, p (i -> i), g -> j -> k)
-> ((c, h), p ((d, i) -> (d, i)), e -> (f, j) -> k)
<||> (h
qe, p (i -> i)
qp, g -> j -> k
qunp)
 =( (c
pe, h
qe)
  , (\d -> d
f (d
pv, i
qv) -> (d -> d
f d
pv, i
qv)) ((d -> d) -> (d, i) -> (d, i))
-> p (d -> d) -> p ((d, i) -> (d, i))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> p (d -> d)
pp
              p ((d, i) -> (d, i))
-> p ((d, i) -> (d, i)) -> p ((d, i) -> (d, i))
forall a. p a -> p a -> p a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    (\i -> i
f (d
pv, i
qv) -> (d
pv, i -> i
f i
qv)) ((i -> i) -> (d, i) -> (d, i))
-> p (i -> i) -> p ((d, i) -> (d, i))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> p (i -> i)
qp
  , \e
f (f
x, j
y) -> g -> j -> k
qunp (e -> f -> g
punp e
f f
x) j
y
  )

pMerged :: IsParser p s => c -> (d,p (d -> d),c -> d -> e) -> p e
c
sem pMerged :: forall (p :: * -> *) s c d e.
IsParser p s =>
c -> (d, p (d -> d), c -> d -> e) -> p e
`pMerged` (d
units, p (d -> d)
alts, c -> d -> e
unp)
 = let pres :: p d
pres = p (d -> d)
alts p (d -> d) -> p d -> p d
forall a b. p (a -> b) -> p a -> p b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> p d
pres p d -> d -> p d
forall (p :: * -> *) s a. IsParser p s => p a -> a -> p a
`opt` d
units
   in c -> d -> e
unp c
sem (d -> e) -> p d -> p e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> p d
pres

list_of :: IsParser p s => p c -> ([d],p ([c] -> [c]),e -> e)
list_of :: forall (p :: * -> *) s c d e.
IsParser p s =>
p c -> ([d], p ([c] -> [c]), e -> e)
list_of p c
p = ([], (:) (c -> [c] -> [c]) -> p c -> p ([c] -> [c])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> p c
p, e -> e
forall a. a -> a
id)