{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE CPP #-}
module Retrie.ExactPrint.Annotated
(
Annotated
, astA
, seedA
, AnnotatedHsDecl
, AnnotatedHsExpr
, AnnotatedHsType
, AnnotatedImport
, AnnotatedImports
, AnnotatedModule
, AnnotatedPat
, AnnotatedStmt
, pruneA
, graftA
, transformA
, trimA
, setEntryDPA
, printA
, printA'
, showAstA
, unsafeMkA
) where
import Control.Monad.State.Lazy hiding (fix)
import Data.Default as D
import Data.Functor.Identity
import Language.Haskell.GHC.ExactPrint hiding
(
transferEntryDP
)
import Language.Haskell.GHC.ExactPrint.Utils
import Retrie.GHC
import Retrie.SYB
type AnnotatedHsDecl = Annotated (LHsDecl GhcPs)
type AnnotatedHsExpr = Annotated (LHsExpr GhcPs)
type AnnotatedHsType = Annotated (LHsType GhcPs)
type AnnotatedImport = Annotated (LImportDecl GhcPs)
type AnnotatedImports = Annotated [LImportDecl GhcPs]
#if __GLASGOW_HASKELL__ >= 906
type AnnotatedModule = Annotated (Located (HsModule GhcPs))
#else
type AnnotatedModule = Annotated (Located HsModule)
#endif
type AnnotatedPat = Annotated (LPat GhcPs)
type AnnotatedStmt = Annotated (LStmt GhcPs (LHsExpr GhcPs))
data Annotated ast = Annotated
{ forall ast. Annotated ast -> ast
astA :: ast
, forall ast. Annotated ast -> Int
seedA :: Int
}
deriving instance (Data ast) => Data (Annotated ast)
instance Functor Annotated where
fmap :: forall a b. (a -> b) -> Annotated a -> Annotated b
fmap a -> b
f Annotated{a
Int
seedA :: Int
astA :: a
seedA :: forall ast. Annotated ast -> Int
astA :: forall ast. Annotated ast -> ast
..} = Annotated{astA :: b
astA = a -> b
f a
astA, Int
seedA :: Int
seedA :: Int
..}
instance Foldable Annotated where
foldMap :: forall m a. Monoid m => (a -> m) -> Annotated a -> m
foldMap a -> m
f = a -> m
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ast. Annotated ast -> ast
astA
instance Traversable Annotated where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Annotated a -> f (Annotated b)
traverse a -> f b
f Annotated{a
Int
seedA :: Int
astA :: a
seedA :: forall ast. Annotated ast -> Int
astA :: forall ast. Annotated ast -> ast
..} =
(\b
ast -> Annotated{astA :: b
astA = b
ast, Int
seedA :: Int
seedA :: Int
..}) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
astA
instance Default ast => Default (Annotated ast) where
def :: Annotated ast
def = forall ast. ast -> Int -> Annotated ast
Annotated forall a. Default a => a
D.def Int
0
instance (Data ast, Monoid ast) => Semigroup (Annotated ast) where
Annotated ast
a1 <> :: Annotated ast -> Annotated ast -> Annotated ast
<> (Annotated ast
ast2 Int
_) =
forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) ast1 ast2.
Monad m =>
Annotated ast1 -> (ast1 -> TransformT m ast2) -> m (Annotated ast2)
transformA Annotated ast
a1 forall a b. (a -> b) -> a -> b
$ \ ast
ast1 ->
forall a. Monoid a => a -> a -> a
mappend ast
ast1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. Monad m => a -> m a
return ast
ast2
instance (Data ast, Monoid ast) => Monoid (Annotated ast) where
mempty :: Annotated ast
mempty = forall ast. ast -> Int -> Annotated ast
Annotated forall a. Monoid a => a
mempty Int
0
unsafeMkA :: ast -> Int -> Annotated ast
unsafeMkA :: forall ast. ast -> Int -> Annotated ast
unsafeMkA = forall ast. ast -> Int -> Annotated ast
Annotated
transformA
:: Monad m => Annotated ast1 -> (ast1 -> TransformT m ast2) -> m (Annotated ast2)
transformA :: forall (m :: * -> *) ast1 ast2.
Monad m =>
Annotated ast1 -> (ast1 -> TransformT m ast2) -> m (Annotated ast2)
transformA (Annotated ast1
ast Int
seed) ast1 -> TransformT m ast2
f = do
(ast2
ast',Int
seed',[String]
_) <- forall (m :: * -> *) a.
Int -> TransformT m a -> m (a, Int, [String])
runTransformFromT Int
seed (ast1 -> TransformT m ast2
f ast1
ast)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ast. ast -> Int -> Annotated ast
Annotated ast2
ast' Int
seed'
graftA :: (Data ast, Monad m) => Annotated ast -> TransformT m ast
graftA :: forall ast (m :: * -> *).
(Data ast, Monad m) =>
Annotated ast -> TransformT m ast
graftA (Annotated ast
x Int
_) = forall (m :: * -> *) a. Monad m => a -> m a
return ast
x
pruneA :: (Data ast, Monad m) => ast -> TransformT m (Annotated ast)
pruneA :: forall ast (m :: * -> *).
(Data ast, Monad m) =>
ast -> TransformT m (Annotated ast)
pruneA ast
ast = forall ast. ast -> Int -> Annotated ast
Annotated ast
ast forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a. a -> a
id
trimA :: Data ast => Annotated ast -> Annotated ast
trimA :: forall ast. Data ast => Annotated ast -> Annotated ast
trimA = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) ast1 ast2.
Monad m =>
Annotated ast1 -> (ast1 -> TransformT m ast2) -> m (Annotated ast2)
transformA Annotated ()
nil forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ast (m :: * -> *).
(Data ast, Monad m) =>
Annotated ast -> TransformT m ast
graftA
where
nil :: Annotated ()
nil :: Annotated ()
nil = forall a. Monoid a => a
mempty
setEntryDPA :: (Default an)
=> Annotated (LocatedAn an ast) -> DeltaPos -> Annotated (LocatedAn an ast)
setEntryDPA :: forall an ast.
Default an =>
Annotated (LocatedAn an ast)
-> DeltaPos -> Annotated (LocatedAn an ast)
setEntryDPA (Annotated LocatedAn an ast
ast Int
s) DeltaPos
dp = forall ast. ast -> Int -> Annotated ast
Annotated (forall t a. Default t => LocatedAn t a -> DeltaPos -> LocatedAn t a
setEntryDP LocatedAn an ast
ast DeltaPos
dp) Int
s
printA :: (Data ast, ExactPrint ast) => Annotated ast -> String
printA :: forall ast. (Data ast, ExactPrint ast) => Annotated ast -> String
printA (Annotated ast
ast Int
_) = forall ast. ExactPrint ast => ast -> String
exactPrint ast
ast
forall c. c -> String -> c
`debug` (String
"printA:" forall a. [a] -> [a] -> [a]
++ forall a. Data a => a -> String
showAst ast
ast)
printA' :: (Data ast, ExactPrint ast) => Annotated ast -> String
printA' :: forall ast. (Data ast, ExactPrint ast) => Annotated ast -> String
printA' (Annotated ast
ast Int
_) = String
"[" forall a. [a] -> [a] -> [a]
++ forall ast. ExactPrint ast => ast -> String
exactPrint ast
ast forall a. [a] -> [a] -> [a]
++ String
"]\n" forall a. [a] -> [a] -> [a]
++ forall a. Data a => a -> String
showAst ast
ast
showAstA :: (Data ast, ExactPrint ast) => Annotated ast -> String
showAstA :: forall ast. (Data ast, ExactPrint ast) => Annotated ast -> String
showAstA (Annotated ast
ast Int
_) = forall a. Data a => a -> String
showAst ast
ast