ghc-exactprint-0.3.1: ExactPrint for GHC

Safe HaskellNone
LanguageHaskell2010

Language.Haskell.GHC.ExactPrint

Contents

Description

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

Synopsis

Relativising

relativiseApiAnns :: Annotate ast => Located ast -> ApiAnns -> Anns Source

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

relativiseApiAnnsWithComments :: Annotate ast => [Comment] -> Located 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

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.

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 

Parsing

parseModule :: FilePath -> IO (Either (SrcSpan, String) (Anns, Located (HsModule RdrName))) 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 RdrName)

Transformation

Printing

exactPrintWithAnns :: 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 calling relativiseApiAnns.

exactPrint :: Annotate ast => Located ast -> ApiAnns -> String Source

Print an AST exactly as specified by the annotations on the nodes in the tree. The output of this function should exactly match the source file.