{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
#if __GLASGOW_HASKELL__ < 800
{-# LANGUAGE FlexibleContexts #-}
#endif
module Language.Rust.Parser.Reversed (
Reversed(..),
toNonEmpty,
unsnoc,
snoc,
) where
import Language.Rust.Data.Position
import Data.Foldable ( Foldable(toList) )
import Data.Semigroup ( Semigroup(..) )
import qualified Data.List.NonEmpty as N
import qualified GHC.Exts as G
newtype Reversed f a = Reversed (f a)
instance Functor f => Functor (Reversed f) where
fmap f (Reversed xs) = Reversed (fmap f xs)
instance Foldable (Reversed []) where
foldMap f (Reversed xs) = foldMap f (reverse xs)
toList (Reversed xs) = reverse xs
instance Foldable (Reversed N.NonEmpty) where
foldMap f (Reversed xs) = foldMap f (N.reverse xs)
toList (Reversed xs) = reverse (toList xs)
instance Semigroup (f a) => Semigroup (Reversed f a) where
Reversed xs <> Reversed ys = Reversed (ys <> xs)
instance Monoid (f a) => Monoid (Reversed f a) where
mempty = Reversed mempty
mappend (Reversed xs) (Reversed ys) = Reversed (mappend ys xs)
instance G.IsList (f a) => G.IsList (Reversed f a) where
type Item (Reversed f a) = G.Item (f a)
fromList xs = Reversed (G.fromList (reverse xs))
toList (Reversed xs) = reverse (G.toList xs)
instance Located (f a) => Located (Reversed f a) where
spanOf (Reversed xs) = spanOf xs
{-# INLINE toNonEmpty #-}
toNonEmpty :: Reversed N.NonEmpty a -> N.NonEmpty a
toNonEmpty (Reversed xs) = N.reverse xs
{-# INLINE unsnoc #-}
unsnoc :: Reversed N.NonEmpty a -> (Reversed [] a, a)
unsnoc (Reversed (x N.:| xs)) = (Reversed xs, x)
{-# INLINE snoc #-}
snoc :: Reversed [] a -> a -> Reversed N.NonEmpty a
snoc (Reversed xs) x = Reversed (x N.:| xs)