ghc-exactprint-0.6.2: ExactPrint for GHC

Safe Haskell | None
Language | Haskell2010

Language.Haskell.GHC.ExactPrint

Contents

- Relativising
- Parsing
- Transformation
- Adding default annotations
- Printing

Description

`ghc-exactprint` is a library to manage manipulating Haskell source files. There are four components.

Synopsis

- relativiseApiAnns :: (Data (SrcSpanLess ast), Annotate ast, HasSrcSpan ast) => ast -> ApiAnns -> Anns
- relativiseApiAnnsWithComments :: (Data (SrcSpanLess ast), Annotate ast, HasSrcSpan ast) => [Comment] -> ast -> ApiAnns -> Anns
- type Anns = Map AnnKey Annotation
- data Comment
- data Annotation = Ann {
  - annEntryDelta :: !DeltaPos
  - annPriorComments :: ![(Comment, DeltaPos)]
  - annFollowingComments :: ![(Comment, DeltaPos)]
  - annsDP :: ![(KeywordId, DeltaPos)]
  - annSortKey :: !(Maybe [SrcSpan])
  - annCapturedSpan :: !(Maybe AnnKey)
  }
- data AnnKey = AnnKey SrcSpan AnnConName
- parseModule :: FilePath -> IO (Either (SrcSpan, String) (Anns, ParsedSource))
- module Language.Haskell.GHC.ExactPrint.Transform
- addAnnotationsForPretty :: Annotate a => [Comment] -> Located a -> Anns -> Anns
- exactPrint :: Annotate ast => Located ast -> Anns -> String

# Relativising

relativiseApiAnns :: (Data (SrcSpanLess ast), Annotate ast, HasSrcSpan ast) => ast -> ApiAnns -> Anns Source #

Transform concrete annotations into relative annotations which are more useful when transforming an AST.

relativiseApiAnnsWithComments :: (Data (SrcSpanLess ast), Annotate ast, HasSrcSpan ast) => [Comment] -> ast -> ApiAnns -> Anns Source #

Exactly the same as `relativiseApiAnns` but with the possibilty to inject comments. This is typically used if the source has been preprocessed by e.g. CPP, and the parts stripped out of the original source are re-added as comments so they are not lost for round tripping.

type Anns = Map AnnKey Annotation Source #

This structure holds a complete set of annotations for an AST

data Comment Source #

A Haskell comment. The `AnnKeywordId` is present if it has been converted from an `AnnKeywordId` because the annotation must be interleaved into the stream and does not have a well-defined position

#### Instances

Instances details

Eq Comment Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods (==) :: Comment -> Comment -> Bool Source # (/=) :: Comment -> Comment -> Bool Source #
Data Comment Source # |

Instance details

Defined in Language.Haskell.GHC.ExactPrint.Types

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Comment -> c Comment Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Comment Source #

toConstr :: Comment -> Constr Source #

dataTypeOf :: Comment -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Comment) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Comment) Source #

gmapT :: (forall b. Data b => b -> b) -> Comment -> Comment Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Comment -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Comment -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> Comment -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Comment -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Comment -> m Comment Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Comment -> m Comment Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Comment -> m Comment Source #

Ord Comment Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods compare :: Comment -> Comment -> Ordering Source # (<) :: Comment -> Comment -> Bool Source # (<=) :: Comment -> Comment -> Bool Source # (>) :: Comment -> Comment -> Bool Source # (>=) :: Comment -> Comment -> Bool Source # max :: Comment -> Comment -> Comment Source # min :: Comment -> Comment -> Comment Source #
Show Comment Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods showsPrec :: Int -> Comment -> ShowS Source # show :: Comment -> String Source # showList :: [Comment] -> ShowS Source #
Outputable Comment Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods ppr :: Comment -> SDoc Source # pprPrec :: Rational -> Comment -> SDoc Source #

data Annotation Source #

Constructors

Ann |

Fields

- annEntryDelta :: !DeltaPos

  Offset used to get to the start of the SrcSpan, from whatever the prior output was, including all annPriorComments (field below).

- annPriorComments :: ![(Comment, DeltaPos)]

  Comments coming after the last non-comment output of the preceding element but before the SrcSpan being annotated by this Annotation. If these are changed then annEntryDelta (field above) must also change to match.

- annFollowingComments :: ![(Comment, DeltaPos)]

  Comments coming after the last output for the element subject to this Annotation. These will only be added by AST transformations, and care must be taken not to disturb layout of following elements.

- annsDP :: ![(KeywordId, DeltaPos)]

  Annotations associated with this element.

- annSortKey :: !(Maybe [SrcSpan])

  Captures the sort order of sub elements. This is needed when the sub-elements have been split (as in a HsLocalBind which holds separate binds and sigs) or for infix patterns where the order has been re-arranged. It is captured explicitly so that after the Delta phase a SrcSpan is used purely as an index into the annotations, allowing transformations of the AST including the introduction of new Located items or re-arranging existing ones.

- annCapturedSpan :: !(Maybe AnnKey)

  Occasionally we must calculate a SrcSpan for an unlocated list of elements which we must remember for the Print phase. e.g. the statements in a HsLet or HsDo. These must be managed as a group because they all need eo be vertically aligned for the Haskell layout rules, and this guarantees this property in the presence of AST edits.

#### Instances

Instances details

Eq Annotation Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods (==) :: Annotation -> Annotation -> Bool Source # (/=) :: Annotation -> Annotation -> Bool Source #
Show Annotation Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods showsPrec :: Int -> Annotation -> ShowS Source # show :: Annotation -> String Source # showList :: [Annotation] -> ShowS Source #
Outputable Annotation Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods ppr :: Annotation -> SDoc Source # pprPrec :: Rational -> Annotation -> SDoc Source #
Monad m => MonadState (Anns, Int) (TransformT m) Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Transform Methods get :: TransformT m (Anns, Int) Source # put :: (Anns, Int) -> TransformT m () Source # state :: ((Anns, Int) -> (a, (Anns, Int))) -> TransformT m a Source #

data AnnKey Source #

For every `Located a`, use the `SrcSpan` and constructor name of a as the key, to store the standard annotation. These are used to maintain context in the AP and EP monads

Constructors

AnnKey SrcSpan AnnConName |

#### Instances

Instances details

Eq AnnKey Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods (==) :: AnnKey -> AnnKey -> Bool Source # (/=) :: AnnKey -> AnnKey -> Bool Source #
Data AnnKey Source # |

Instance details

Defined in Language.Haskell.GHC.ExactPrint.Types

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> AnnKey -> c AnnKey Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c AnnKey Source #

toConstr :: AnnKey -> Constr Source #

dataTypeOf :: AnnKey -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c AnnKey) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AnnKey) Source #

gmapT :: (forall b. Data b => b -> b) -> AnnKey -> AnnKey Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> AnnKey -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> AnnKey -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> AnnKey -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> AnnKey -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> AnnKey -> m AnnKey Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> AnnKey -> m AnnKey Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> AnnKey -> m AnnKey Source #

Ord AnnKey Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods compare :: AnnKey -> AnnKey -> Ordering Source # (<) :: AnnKey -> AnnKey -> Bool Source # (<=) :: AnnKey -> AnnKey -> Bool Source # (>) :: AnnKey -> AnnKey -> Bool Source # (>=) :: AnnKey -> AnnKey -> Bool Source # max :: AnnKey -> AnnKey -> AnnKey Source # min :: AnnKey -> AnnKey -> AnnKey Source #
Show AnnKey Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods showsPrec :: Int -> AnnKey -> ShowS Source # show :: AnnKey -> String Source # showList :: [AnnKey] -> ShowS Source #
Outputable AnnKey Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Types Methods ppr :: AnnKey -> SDoc Source # pprPrec :: Rational -> AnnKey -> SDoc Source #
Monad m => MonadState (Anns, Int) (TransformT m) Source # |
Instance details Defined in Language.Haskell.GHC.ExactPrint.Transform Methods get :: TransformT m (Anns, Int) Source # put :: (Anns, Int) -> TransformT m () Source # state :: ((Anns, Int) -> (a, (Anns, Int))) -> TransformT m a Source #

# Parsing

parseModule :: FilePath -> IO (Either (SrcSpan, String) (Anns, ParsedSource)) Source #

This entry point will also work out which language extensions are required and perform CPP processing if necessary.

    parseModule = parseModuleWithCpp defaultCppOptions

Note: `ParsedSource` is a synonym for `Located` (`HsModule` `GhcPs`)

# Transformation

module Language.Haskell.GHC.ExactPrint.Transform

# Adding default annotations

addAnnotationsForPretty :: Annotate a => [Comment] -> Located a -> Anns -> Anns Source #

Add any missing annotations so that the full AST element will exactprint properly when done.

# Printing

exactPrint :: Annotate ast => Located ast -> Anns -> String Source #

Print an AST with a map of potential modified `Anns`. The usual way to generate such a map is by using one of the parsers in Language.Haskell.GHC.ExactPrint.Parsers.