{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
module Retrie.ExactPrint
(
fix
, Parsers.LibDir
, parseContent
, parseContentNoFixity
, parseDecl
, parseExpr
, parseImports
, parsePattern
, parseStmt
, parseType
, addAllAnnsT
, swapEntryDPT
, transferAnnsT
, transferEntryAnnsT
, transferEntryDPT
, transferAnchor
, debugDump
, debugParse
, debug
, hasComments
, isComma
, module Retrie.ExactPrint.Annotated
, module Language.Haskell.GHC.ExactPrint
, module Language.Haskell.GHC.ExactPrint.Types
, module Language.Haskell.GHC.ExactPrint.Utils
, module Language.Haskell.GHC.ExactPrint.Transform
) where
import Control.Exception
import Control.Monad
import Control.Monad.State.Lazy hiding (fix)
import Data.List (transpose)
import Text.Printf
import Language.Haskell.GHC.ExactPrint hiding
(
setEntryDP
, transferEntryDP
)
import Language.Haskell.GHC.ExactPrint.Utils hiding (debug)
import qualified Language.Haskell.GHC.ExactPrint.Parsers as Parsers
import Language.Haskell.GHC.ExactPrint.Types
( showGhc
)
import Language.Haskell.GHC.ExactPrint.Transform
import Retrie.ExactPrint.Annotated
import Retrie.Fixity
import Retrie.GHC
import Retrie.SYB hiding (ext1)
import Retrie.Util
import GHC.Stack
import Debug.Trace
debug :: c -> String -> c
debug :: forall c. c -> String -> c
debug c
c String
s = forall a. String -> a -> a
trace String
s c
c
fix :: (Data ast, MonadIO m) => FixityEnv -> ast -> TransformT m ast
fix :: forall ast (m :: * -> *).
(Data ast, MonadIO m) =>
FixityEnv -> ast -> TransformT m ast
fix FixityEnv
env = ast -> TransformT m ast
fixAssociativity forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ast -> TransformT m ast
fixEntryDP
where
fixAssociativity :: ast -> TransformT m ast
fixAssociativity = forall (m :: * -> *). Monad m => GenericM m -> GenericM m
everywhereM (forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(b -> m b) -> a -> m a
mkM (forall (m :: * -> *).
MonadIO m =>
FixityEnv -> LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneExpr FixityEnv
env) forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(a -> m a) -> (b -> m b) -> a -> m a
`extM` forall (m :: * -> *).
Monad m =>
FixityEnv -> LPat GhcPs -> TransformT m (LPat GhcPs)
fixOnePat FixityEnv
env)
fixEntryDP :: ast -> TransformT m ast
fixEntryDP = forall (m :: * -> *). Monad m => GenericM m -> GenericM m
everywhereM (forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(b -> m b) -> a -> m a
mkM forall (m :: * -> *).
MonadIO m =>
LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneEntryExpr forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(a -> m a) -> (b -> m b) -> a -> m a
`extM` forall (m :: * -> *).
MonadIO m =>
LPat GhcPs -> TransformT m (LPat GhcPs)
fixOneEntryPat)
associatesRight :: Fixity -> Fixity -> Bool
associatesRight :: Fixity -> Fixity -> Bool
associatesRight (Fixity SourceText
_ Int
p1 FixityDirection
a1) (Fixity SourceText
_ Int
p2 FixityDirection
_a2) =
Int
p2 forall a. Ord a => a -> a -> Bool
> Int
p1 Bool -> Bool -> Bool
|| Int
p1 forall a. Eq a => a -> a -> Bool
== Int
p2 Bool -> Bool -> Bool
&& FixityDirection
a1 forall a. Eq a => a -> a -> Bool
== FixityDirection
InfixR
fixOneExpr
:: MonadIO m
=> FixityEnv
-> LHsExpr GhcPs
-> TransformT m (LHsExpr GhcPs)
fixOneExpr :: forall (m :: * -> *).
MonadIO m =>
FixityEnv -> LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneExpr FixityEnv
env (L SrcSpanAnnA
l2 (OpApp XOpApp GhcPs
x2 ap1 :: LHsExpr GhcPs
ap1@(L SrcSpanAnnA
l1 (OpApp XOpApp GhcPs
x1 LHsExpr GhcPs
x LHsExpr GhcPs
op1 LHsExpr GhcPs
y)) LHsExpr GhcPs
op2 LHsExpr GhcPs
z))
| Fixity -> Fixity -> Bool
associatesRight (LHsExpr GhcPs -> FixityEnv -> Fixity
lookupOp LHsExpr GhcPs
op1 FixityEnv
env) (LHsExpr GhcPs -> FixityEnv -> Fixity
lookupOp LHsExpr GhcPs
op2 FixityEnv
env) = do
let ap2' :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap2' = forall l e. l -> e -> GenLocated l e
L (forall an. SrcAnn an -> SrcAnn an
stripComments SrcSpanAnnA
l2) forall a b. (a -> b) -> a -> b
$ forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcPs
x2 LHsExpr GhcPs
y LHsExpr GhcPs
op2 LHsExpr GhcPs
z
(GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap1_0, GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap2'_0) <- forall a b (m :: * -> *) a1 a2.
(Data a, Data b, Monad m, Monoid a1, Monoid a2, Typeable a1,
Typeable a2) =>
LocatedAn a1 a
-> LocatedAn a2 b -> TransformT m (LocatedAn a1 a, LocatedAn a2 b)
swapEntryDPT LHsExpr GhcPs
ap1 GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap2'
GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap1_1 <- forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(TrailingAnn -> Bool)
-> LocatedA a -> LocatedA b -> TransformT m (LocatedA b)
transferAnnsT TrailingAnn -> Bool
isComma GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap2'_0 GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap1_0
GenLocated SrcSpanAnnA (HsExpr GhcPs)
rhs <- forall (m :: * -> *).
MonadIO m =>
FixityEnv -> LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneExpr FixityEnv
env GenLocated SrcSpanAnnA (HsExpr GhcPs)
ap2'_0
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l2 forall a b. (a -> b) -> a -> b
$ forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcPs
x1 LHsExpr GhcPs
x LHsExpr GhcPs
op1 GenLocated SrcSpanAnnA (HsExpr GhcPs)
rhs
fixOneExpr FixityEnv
_ LHsExpr GhcPs
e = forall (m :: * -> *) a. Monad m => a -> m a
return LHsExpr GhcPs
e
fixOnePat :: Monad m => FixityEnv -> LPat GhcPs -> TransformT m (LPat GhcPs)
fixOnePat :: forall (m :: * -> *).
Monad m =>
FixityEnv -> LPat GhcPs -> TransformT m (LPat GhcPs)
fixOnePat FixityEnv
env (forall (p :: Pass). LPat (GhcPass p) -> Maybe (LPat (GhcPass p))
dLPat -> Just (L SrcSpanAnnA
l2 (ConPat XConPat GhcPs
ext2 XRec GhcPs (ConLikeP GhcPs)
op2 (InfixCon (forall (p :: Pass). LPat (GhcPass p) -> Maybe (LPat (GhcPass p))
dLPat -> Just ap1 :: LPat GhcPs
ap1@(L SrcSpanAnnA
l1 (ConPat XConPat GhcPs
ext1 XRec GhcPs (ConLikeP GhcPs)
op1 (InfixCon LPat GhcPs
x LPat GhcPs
y)))) LPat GhcPs
z))))
| Fixity -> Fixity -> Bool
associatesRight (LocatedN RdrName -> FixityEnv -> Fixity
lookupOpRdrName XRec GhcPs (ConLikeP GhcPs)
op1 FixityEnv
env) (LocatedN RdrName -> FixityEnv -> Fixity
lookupOpRdrName XRec GhcPs (ConLikeP GhcPs)
op2 FixityEnv
env) = do
let ap2' :: GenLocated SrcSpanAnnA (Pat GhcPs)
ap2' = forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l2 (forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
ext2 XRec GhcPs (ConLikeP GhcPs)
op2 (forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon LPat GhcPs
y LPat GhcPs
z))
(GenLocated SrcSpanAnnA (Pat GhcPs)
ap1_0, GenLocated SrcSpanAnnA (Pat GhcPs)
ap2'_0) <- forall a b (m :: * -> *) a1 a2.
(Data a, Data b, Monad m, Monoid a1, Monoid a2, Typeable a1,
Typeable a2) =>
LocatedAn a1 a
-> LocatedAn a2 b -> TransformT m (LocatedAn a1 a, LocatedAn a2 b)
swapEntryDPT LPat GhcPs
ap1 GenLocated SrcSpanAnnA (Pat GhcPs)
ap2'
GenLocated SrcSpanAnnA (Pat GhcPs)
ap1_1 <- forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(TrailingAnn -> Bool)
-> LocatedA a -> LocatedA b -> TransformT m (LocatedA b)
transferAnnsT TrailingAnn -> Bool
isComma GenLocated SrcSpanAnnA (Pat GhcPs)
ap2' LPat GhcPs
ap1
GenLocated SrcSpanAnnA (Pat GhcPs)
rhs <- forall (m :: * -> *).
Monad m =>
FixityEnv -> LPat GhcPs -> TransformT m (LPat GhcPs)
fixOnePat FixityEnv
env (forall (p :: Pass). LPat (GhcPass p) -> LPat (GhcPass p)
cLPat GenLocated SrcSpanAnnA (Pat GhcPs)
ap2'_0)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (p :: Pass). LPat (GhcPass p) -> LPat (GhcPass p)
cLPat forall a b. (a -> b) -> a -> b
$ forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l1 (forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
ext1 XRec GhcPs (ConLikeP GhcPs)
op1 (forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon LPat GhcPs
x GenLocated SrcSpanAnnA (Pat GhcPs)
rhs))
fixOnePat FixityEnv
_ LPat GhcPs
e = forall (m :: * -> *) a. Monad m => a -> m a
return LPat GhcPs
e
stripComments :: SrcAnn an -> SrcAnn an
(SrcSpanAnn EpAnn an
EpAnnNotUsed SrcSpan
l) = forall a. a -> SrcSpan -> SrcSpanAnn' a
SrcSpanAnn forall ann. EpAnn ann
EpAnnNotUsed SrcSpan
l
stripComments (SrcSpanAnn (EpAnn Anchor
anc an
an EpAnnComments
_) SrcSpan
l) = forall a. a -> SrcSpan -> SrcSpanAnn' a
SrcSpanAnn (forall ann. Anchor -> ann -> EpAnnComments -> EpAnn ann
EpAnn Anchor
anc an
an EpAnnComments
emptyComments) SrcSpan
l
fixOneEntry
:: (MonadIO m, Data a)
=> LocatedA a
-> LocatedA a
-> TransformT m (LocatedA a, LocatedA a)
fixOneEntry :: forall (m :: * -> *) a.
(MonadIO m, Data a) =>
LocatedA a -> LocatedA a -> TransformT m (LocatedA a, LocatedA a)
fixOneEntry LocatedA a
e LocatedA a
x = do
let xdp :: DeltaPos
xdp = forall a. LocatedA a -> DeltaPos
entryDP LocatedA a
x
let xr :: Int
xr = DeltaPos -> Int
getDeltaLine DeltaPos
xdp
let xc :: Int
xc = DeltaPos -> Int
deltaColumn DeltaPos
xdp
let edp :: DeltaPos
edp = forall a. LocatedA a -> DeltaPos
entryDP LocatedA a
e
let er :: Int
er = DeltaPos -> Int
getDeltaLine DeltaPos
edp
let ec :: Int
ec = DeltaPos -> Int
deltaColumn DeltaPos
edp
case DeltaPos
xdp of
SameLine Int
n -> do
forall (m :: * -> *) a. Monad m => a -> m a
return ( forall t a. Default t => LocatedAn t a -> DeltaPos -> LocatedAn t a
setEntryDP LocatedA a
e (Int -> Int -> DeltaPos
deltaPos Int
er (Int
xc forall a. Num a => a -> a -> a
+ Int
ec))
, forall t a. Default t => LocatedAn t a -> DeltaPos -> LocatedAn t a
setEntryDP LocatedA a
x (Int -> Int -> DeltaPos
deltaPos Int
xr Int
0))
DeltaPos
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return (LocatedA a
e,LocatedA a
x)
entryDP :: LocatedA a -> DeltaPos
entryDP :: forall a. LocatedA a -> DeltaPos
entryDP (L (SrcSpanAnn EpAnn AnnListItem
EpAnnNotUsed SrcSpan
_) a
_) = Int -> DeltaPos
SameLine Int
1
entryDP (L (SrcSpanAnn (EpAnn Anchor
anc AnnListItem
_ EpAnnComments
_) SrcSpan
_) a
_)
= case Anchor -> AnchorOperation
anchor_op Anchor
anc of
AnchorOperation
UnchangedAnchor -> Int -> DeltaPos
SameLine Int
1
MovedAnchor DeltaPos
dp -> DeltaPos
dp
fixOneEntryExpr :: MonadIO m => LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneEntryExpr :: forall (m :: * -> *).
MonadIO m =>
LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneEntryExpr e :: LHsExpr GhcPs
e@(L SrcSpanAnnA
l (OpApp XOpApp GhcPs
a LHsExpr GhcPs
x LHsExpr GhcPs
b LHsExpr GhcPs
c)) = do
(GenLocated SrcSpanAnnA (HsExpr GhcPs)
e',GenLocated SrcSpanAnnA (HsExpr GhcPs)
x') <- forall (m :: * -> *) a.
(MonadIO m, Data a) =>
LocatedA a -> LocatedA a -> TransformT m (LocatedA a, LocatedA a)
fixOneEntry LHsExpr GhcPs
e LHsExpr GhcPs
x
forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L (forall l e. GenLocated l e -> l
getLoc GenLocated SrcSpanAnnA (HsExpr GhcPs)
e') (forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcPs
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
x' LHsExpr GhcPs
b LHsExpr GhcPs
c))
fixOneEntryExpr LHsExpr GhcPs
e = forall (m :: * -> *) a. Monad m => a -> m a
return LHsExpr GhcPs
e
fixOneEntryPat :: MonadIO m => LPat GhcPs -> TransformT m (LPat GhcPs)
fixOneEntryPat :: forall (m :: * -> *).
MonadIO m =>
LPat GhcPs -> TransformT m (LPat GhcPs)
fixOneEntryPat LPat GhcPs
pat
#if __GLASGOW_HASKELL__ < 900
| Just p@(L l (ConPatIn a (InfixCon x b))) <- dLPat pat = do
#else
| Just p :: LPat GhcPs
p@(L SrcSpanAnnA
l (ConPat XConPat GhcPs
a XRec GhcPs (ConLikeP GhcPs)
b (InfixCon LPat GhcPs
x LPat GhcPs
c))) <- forall (p :: Pass). LPat (GhcPass p) -> Maybe (LPat (GhcPass p))
dLPat LPat GhcPs
pat = do
#endif
(GenLocated SrcSpanAnnA (Pat GhcPs)
p', GenLocated SrcSpanAnnA (Pat GhcPs)
x') <- forall (m :: * -> *) a.
(MonadIO m, Data a) =>
LocatedA a -> LocatedA a -> TransformT m (LocatedA a, LocatedA a)
fixOneEntry LPat GhcPs
p (forall (p :: Pass). LPat (GhcPass p) -> LPat (GhcPass p)
dLPatUnsafe LPat GhcPs
x)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (p :: Pass). LPat (GhcPass p) -> LPat (GhcPass p)
cLPat forall a b. (a -> b) -> a -> b
$ (forall l e. l -> e -> GenLocated l e
L (forall l e. GenLocated l e -> l
getLoc GenLocated SrcSpanAnnA (Pat GhcPs)
p') (forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
a XRec GhcPs (ConLikeP GhcPs)
b (forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon GenLocated SrcSpanAnnA (Pat GhcPs)
x' LPat GhcPs
c))))
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return LPat GhcPs
pat
swapEntryDPT
:: (Data a, Data b, Monad m, Monoid a1, Monoid a2, Typeable a1, Typeable a2)
=> LocatedAn a1 a -> LocatedAn a2 b -> TransformT m (LocatedAn a1 a, LocatedAn a2 b)
swapEntryDPT :: forall a b (m :: * -> *) a1 a2.
(Data a, Data b, Monad m, Monoid a1, Monoid a2, Typeable a1,
Typeable a2) =>
LocatedAn a1 a
-> LocatedAn a2 b -> TransformT m (LocatedAn a1 a, LocatedAn a2 b)
swapEntryDPT LocatedAn a1 a
a LocatedAn a2 b
b = do
LocatedAn a2 b
b' <- forall (m :: * -> *) t2 t1 a b.
(Monad m, Monoid t2, Typeable t1, Typeable t2) =>
LocatedAn t1 a -> LocatedAn t2 b -> TransformT m (LocatedAn t2 b)
transferEntryDP LocatedAn a1 a
a LocatedAn a2 b
b
LocatedAn a1 a
a' <- forall (m :: * -> *) t2 t1 a b.
(Monad m, Monoid t2, Typeable t1, Typeable t2) =>
LocatedAn t1 a -> LocatedAn t2 b -> TransformT m (LocatedAn t2 b)
transferEntryDP LocatedAn a2 b
b LocatedAn a1 a
a
forall (m :: * -> *) a. Monad m => a -> m a
return (LocatedAn a1 a
a',LocatedAn a2 b
b')
parseContentNoFixity :: Parsers.LibDir -> FilePath -> String -> IO AnnotatedModule
parseContentNoFixity :: String -> String -> String -> IO AnnotatedModule
parseContentNoFixity String
libdir String
fp String
str = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall a. String -> (DynFlags -> a) -> IO a
Parsers.withDynFlags String
libdir forall a b. (a -> b) -> a -> b
$ \DynFlags
dflags -> do
ParseResult (Located HsModule)
r <- String -> String -> String -> IO (ParseResult (Located HsModule))
Parsers.parseModuleFromString String
libdir String
fp String
str
case ParseResult (Located HsModule)
r of
Left ErrorMessages
msg -> do
#if __GLASGOW_HASKELL__ < 900
fail $ show msg
#elif __GLASGOW_HASKELL__ < 904
forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. Bag a -> [a]
bagToList ErrorMessages
msg
#else
fail $ showSDoc dflags $ ppr msg
#endif
Right Located HsModule
m -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ast. ast -> Int -> Annotated ast
unsafeMkA (forall ast. ExactPrint ast => ast -> ast
makeDeltaAst Located HsModule
m) Int
0
parseContent :: Parsers.LibDir -> FixityEnv -> FilePath -> String -> IO AnnotatedModule
parseContent :: String -> FixityEnv -> String -> String -> IO AnnotatedModule
parseContent String
libdir FixityEnv
fixities String
fp =
String -> String -> String -> IO AnnotatedModule
parseContentNoFixity String
libdir String
fp forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (forall (m :: * -> *) ast1 ast2.
Monad m =>
Annotated ast1 -> (ast1 -> TransformT m ast2) -> m (Annotated ast2)
`transformA` forall ast (m :: * -> *).
(Data ast, MonadIO m) =>
FixityEnv -> ast -> TransformT m ast
fix FixityEnv
fixities)
parseImports :: Parsers.LibDir -> [String] -> IO AnnotatedImports
parseImports :: String -> [String] -> IO AnnotatedImports
parseImports String
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty
parseImports String
libdir [String]
imports = do
AnnotatedModule
am <- String -> String -> String -> IO AnnotatedModule
parseContentNoFixity String
libdir String
"parseImports" forall a b. (a -> b) -> a -> b
$ String
"\n" forall a. [a] -> [a] -> [a]
++ [String] -> String
unlines [String]
imports
Annotated [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
ais <- forall (m :: * -> *) ast1 ast2.
Monad m =>
Annotated ast1 -> (ast1 -> TransformT m ast2) -> m (Annotated ast2)
transformA AnnotatedModule
am forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsModule -> [LImportDecl GhcPs]
hsmodImports forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ast. Data ast => Annotated ast -> Annotated ast
trimA Annotated [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
ais
parseDecl :: Parsers.LibDir -> String -> IO AnnotatedHsDecl
parseDecl :: String -> String -> IO AnnotatedHsDecl
parseDecl String
libdir String
str = forall a.
ExactPrint a =>
String -> String -> Parser a -> String -> IO (Annotated a)
parseHelper String
libdir String
"parseDecl" Parser (LHsDecl GhcPs)
Parsers.parseDecl String
str
parseExpr :: Parsers.LibDir -> String -> IO AnnotatedHsExpr
parseExpr :: String -> String -> IO AnnotatedHsExpr
parseExpr String
libdir String
str = forall a.
ExactPrint a =>
String -> String -> Parser a -> String -> IO (Annotated a)
parseHelper String
libdir String
"parseExpr" Parser (LHsExpr GhcPs)
Parsers.parseExpr String
str
parsePattern :: Parsers.LibDir -> String -> IO AnnotatedPat
parsePattern :: String -> String -> IO AnnotatedPat
parsePattern String
libdir String
str = forall a.
ExactPrint a =>
String -> String -> Parser a -> String -> IO (Annotated a)
parseHelper String
libdir String
"parsePattern" Parser (LPat GhcPs)
Parsers.parsePattern String
str
parseStmt :: Parsers.LibDir -> String -> IO AnnotatedStmt
parseStmt :: String -> String -> IO AnnotatedStmt
parseStmt String
libdir String
str = do
Annotated
(LocatedAn
AnnListItem
(StmtLR GhcPs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
res <- forall a.
ExactPrint a =>
String -> String -> Parser a -> String -> IO (Annotated a)
parseHelper String
libdir String
"parseStmt" Parser (ExprLStmt GhcPs)
Parsers.parseStmt String
str
forall (m :: * -> *) a. Monad m => a -> m a
return (forall an ast.
Default an =>
Annotated (LocatedAn an ast)
-> DeltaPos -> Annotated (LocatedAn an ast)
setEntryDPA Annotated
(LocatedAn
AnnListItem
(StmtLR GhcPs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
res (Int -> Int -> DeltaPos
DifferentLine Int
1 Int
0))
parseType :: Parsers.LibDir -> String -> IO AnnotatedHsType
parseType :: String -> String -> IO AnnotatedHsType
parseType String
libdir String
str = forall a.
ExactPrint a =>
String -> String -> Parser a -> String -> IO (Annotated a)
parseHelper String
libdir String
"parseType" Parser (LHsType GhcPs)
Parsers.parseType String
str
parseHelper :: (ExactPrint a)
=> Parsers.LibDir -> FilePath -> Parsers.Parser a -> String -> IO (Annotated a)
parseHelper :: forall a.
ExactPrint a =>
String -> String -> Parser a -> String -> IO (Annotated a)
parseHelper String
libdir String
fp Parser a
parser String
str = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall a. String -> (DynFlags -> a) -> IO a
Parsers.withDynFlags String
libdir forall a b. (a -> b) -> a -> b
$ \DynFlags
dflags ->
case Parser a
parser DynFlags
dflags String
fp String
str of
#if __GLASGOW_HASKELL__ < 900
Left (_, msg) -> throwIO $ ErrorCall msg
#elif __GLASGOW_HASKELL__ < 904
Left ErrorMessages
errBag -> forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$ String -> ErrorCall
ErrorCall (forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. Bag a -> [a]
bagToList ErrorMessages
errBag)
#else
Left msg -> throwIO $ ErrorCall (showSDoc dflags $ ppr msg)
#endif
Right a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ast. ast -> Int -> Annotated ast
unsafeMkA (forall ast. ExactPrint ast => ast -> ast
makeDeltaAst a
x) Int
0
debugDump :: (Data a, ExactPrint a) => Annotated a -> IO ()
debugDump :: forall a. (Data a, ExactPrint a) => Annotated a -> IO ()
debugDump Annotated a
ax = do
let
str :: String
str = forall ast. (Data ast, ExactPrint ast) => Annotated ast -> String
printA Annotated a
ax
maxCol :: Int
maxCol = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ String -> [String]
lines String
str
(String
tens, String
ones) =
case forall a. [[a]] -> [[a]]
transpose [forall r. PrintfType r => String -> r
printf String
"%2d" Int
i | Int
i <- [Int
1 .. Int
maxCol]] of
[String
ts, String
os] -> (String
ts, String
os)
[String]
_ -> (String
"", String
"")
String -> IO ()
putStrLn String
tens
String -> IO ()
putStrLn String
ones
String -> IO ()
putStrLn String
str
String -> IO ()
putStrLn String
"------------------------------------"
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ forall ast. (Data ast, ExactPrint ast) => Annotated ast -> String
showAstA Annotated a
ax
String -> IO ()
putStrLn String
"------------------------------------"
transferEntryAnnsT
:: (HasCallStack, Data a, Data b, Monad m)
=> (TrailingAnn -> Bool)
-> LocatedA a
-> LocatedA b
-> TransformT m (LocatedA b)
transferEntryAnnsT :: forall a b (m :: * -> *).
(HasCallStack, Data a, Data b, Monad m) =>
(TrailingAnn -> Bool)
-> LocatedA a -> LocatedA b -> TransformT m (LocatedA b)
transferEntryAnnsT TrailingAnn -> Bool
p LocatedA a
a LocatedA b
b = do
LocatedA b
b' <- forall (m :: * -> *) t2 t1 a b.
(Monad m, Monoid t2, Typeable t1, Typeable t2) =>
LocatedAn t1 a -> LocatedAn t2 b -> TransformT m (LocatedAn t2 b)
transferEntryDP LocatedA a
a LocatedA b
b
forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(TrailingAnn -> Bool)
-> LocatedA a -> LocatedA b -> TransformT m (LocatedA b)
transferAnnsT TrailingAnn -> Bool
p LocatedA a
a LocatedA b
b'
transferEntryDPT
:: (HasCallStack, Data a, Data b, Monad m)
=> Located a -> Located b -> TransformT m ()
transferEntryDPT :: forall a b (m :: * -> *).
(HasCallStack, Data a, Data b, Monad m) =>
Located a -> Located b -> TransformT m ()
transferEntryDPT Located a
_a Located b
_b = forall a. HasCallStack => String -> a
error String
"transferEntryDPT"
addAllAnnsT
:: (HasCallStack, Monoid an, Data a, Data b, MonadIO m, Typeable an)
=> LocatedAn an a -> LocatedAn an b -> TransformT m (LocatedAn an b)
addAllAnnsT :: forall an a b (m :: * -> *).
(HasCallStack, Monoid an, Data a, Data b, MonadIO m,
Typeable an) =>
LocatedAn an a -> LocatedAn an b -> TransformT m (LocatedAn an b)
addAllAnnsT LocatedAn an a
a LocatedAn an b
b = do
forall (m :: * -> *) t2 t1 a b.
(Monad m, Monoid t2, Typeable t1, Typeable t2) =>
LocatedAn t1 a -> LocatedAn t2 b -> TransformT m (LocatedAn t2 b)
transferEntryDP LocatedAn an a
a LocatedAn an b
b
transferAnchor :: LocatedA a -> LocatedA b -> LocatedA b
transferAnchor :: forall a b. LocatedA a -> LocatedA b -> LocatedA b
transferAnchor (L (SrcSpanAnn EpAnn AnnListItem
EpAnnNotUsed SrcSpan
l) a
_) LocatedA b
lb = forall an a.
Default an =>
LocatedAn an a -> Anchor -> EpAnnComments -> LocatedAn an a
setAnchorAn LocatedA b
lb (SrcSpan -> Anchor
spanAsAnchor SrcSpan
l) EpAnnComments
emptyComments
transferAnchor (L (SrcSpanAnn (EpAnn Anchor
anc AnnListItem
_ EpAnnComments
_) SrcSpan
_) a
_) LocatedA b
lb = forall an a.
Default an =>
LocatedAn an a -> Anchor -> EpAnnComments -> LocatedAn an a
setAnchorAn LocatedA b
lb Anchor
anc EpAnnComments
emptyComments
isComma :: TrailingAnn -> Bool
isComma :: TrailingAnn -> Bool
isComma (AddCommaAnn EpaLocation
_) = Bool
True
isComma TrailingAnn
_ = Bool
False
isCommentKeyword :: AnnKeywordId -> Bool
AnnKeywordId
_ = Bool
False
hasComments :: LocatedAn an a -> Bool
(L (SrcSpanAnn EpAnn an
EpAnnNotUsed SrcSpan
_) a
_) = Bool
False
hasComments (L (SrcSpanAnn (EpAnn Anchor
anc an
_ EpAnnComments
cs) SrcSpan
_) a
_)
= case EpAnnComments
cs of
EpaComments [] -> Bool
False
EpaCommentsBalanced [] [] -> Bool
False
EpAnnComments
_ -> Bool
True
transferAnnsT
:: (Data a, Data b, Monad m)
=> (TrailingAnn -> Bool)
-> LocatedA a
-> LocatedA b
-> TransformT m (LocatedA b)
transferAnnsT :: forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(TrailingAnn -> Bool)
-> LocatedA a -> LocatedA b -> TransformT m (LocatedA b)
transferAnnsT TrailingAnn -> Bool
p (L (SrcSpanAnn EpAnn AnnListItem
EpAnnNotUsed SrcSpan
_) a
_) LocatedA b
b = forall (m :: * -> *) a. Monad m => a -> m a
return LocatedA b
b
transferAnnsT TrailingAnn -> Bool
p (L (SrcSpanAnn (EpAnn Anchor
anc (AnnListItem [TrailingAnn]
ts) EpAnnComments
cs) SrcSpan
l) a
a) (L (SrcSpanAnn EpAnn AnnListItem
an SrcSpan
lb) b
b) = do
let ps :: [TrailingAnn]
ps = forall a. (a -> Bool) -> [a] -> [a]
filter TrailingAnn -> Bool
p [TrailingAnn]
ts
let an' :: EpAnn AnnListItem
an' = case EpAnn AnnListItem
an of
EpAnn AnnListItem
EpAnnNotUsed -> forall ann. Anchor -> ann -> EpAnnComments -> EpAnn ann
EpAnn (SrcSpan -> Anchor
spanAsAnchor SrcSpan
lb) ([TrailingAnn] -> AnnListItem
AnnListItem [TrailingAnn]
ps) EpAnnComments
emptyComments
EpAnn Anchor
ancb (AnnListItem [TrailingAnn]
tsb) EpAnnComments
csb -> forall ann. Anchor -> ann -> EpAnnComments -> EpAnn ann
EpAnn Anchor
ancb ([TrailingAnn] -> AnnListItem
AnnListItem ([TrailingAnn]
tsbforall a. [a] -> [a] -> [a]
++[TrailingAnn]
ps)) EpAnnComments
csb
forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L (forall a. a -> SrcSpan -> SrcSpanAnn' a
SrcSpanAnn EpAnn AnnListItem
an' SrcSpan
lb) b
b)
debugParse :: Parsers.LibDir -> FixityEnv -> String -> IO ()
debugParse :: String -> FixityEnv -> String -> IO ()
debugParse String
libdir FixityEnv
fixityEnv String
s = do
String -> String -> IO ()
writeFile String
"debug.txt" String
s
ParseResult (Located HsModule)
r <- String -> String -> IO (ParseResult (Located HsModule))
parseModule String
libdir String
"debug.txt"
case ParseResult (Located HsModule)
r of
Left ErrorMessages
_ -> String -> IO ()
putStrLn String
"parse failed"
Right Located HsModule
modl -> do
let m :: AnnotatedModule
m = forall ast. ast -> Int -> Annotated ast
unsafeMkA (forall ast. ExactPrint ast => ast -> ast
makeDeltaAst Located HsModule
modl) Int
0
String -> IO ()
putStrLn String
"parseModule"
forall a. (Data a, ExactPrint a) => Annotated a -> IO ()
debugDump AnnotatedModule
m
forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ AnnotatedModule -> IO AnnotatedModule
transformDebug AnnotatedModule
m
where
transformDebug :: AnnotatedModule -> IO AnnotatedModule
transformDebug =
forall {a} {b}.
(Data a, ExactPrint a, Typeable b) =>
String -> (b -> TransformT IO b) -> Annotated a -> IO (Annotated a)
run String
"fixOneExpr D.def" (forall (m :: * -> *).
MonadIO m =>
FixityEnv -> LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneExpr FixityEnv
fixityEnv)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall {a} {b}.
(Data a, ExactPrint a, Typeable b) =>
String -> (b -> TransformT IO b) -> Annotated a -> IO (Annotated a)
run String
"fixOnePat D.def" (forall (m :: * -> *).
Monad m =>
FixityEnv -> LPat GhcPs -> TransformT m (LPat GhcPs)
fixOnePat FixityEnv
fixityEnv)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall {a} {b}.
(Data a, ExactPrint a, Typeable b) =>
String -> (b -> TransformT IO b) -> Annotated a -> IO (Annotated a)
run String
"fixOneEntryExpr" forall (m :: * -> *).
MonadIO m =>
LHsExpr GhcPs -> TransformT m (LHsExpr GhcPs)
fixOneEntryExpr
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall {a} {b}.
(Data a, ExactPrint a, Typeable b) =>
String -> (b -> TransformT IO b) -> Annotated a -> IO (Annotated a)
run String
"fixOneEntryPat" forall (m :: * -> *).
MonadIO m =>
LPat GhcPs -> TransformT m (LPat GhcPs)
fixOneEntryPat
run :: String -> (b -> TransformT IO b) -> Annotated a -> IO (Annotated a)
run String
wat b -> TransformT IO b
f Annotated a
m = do
String -> IO ()
putStrLn String
wat
Annotated a
m' <- forall (m :: * -> *) ast1 ast2.
Monad m =>
Annotated ast1 -> (ast1 -> TransformT m ast2) -> m (Annotated ast2)
transformA Annotated a
m (forall (m :: * -> *). Monad m => GenericM m -> GenericM m
everywhereM (forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
(b -> m b) -> a -> m a
mkM b -> TransformT IO b
f))
forall a. (Data a, ExactPrint a) => Annotated a -> IO ()
debugDump Annotated a
m'
forall (m :: * -> *) a. Monad m => a -> m a
return Annotated a
m'