HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-9.10.3GHC2021

GHC.Parser.Annotation

  • 45 types
  • 3 classes
  • 60 values
  • Packageghc-9.10.3
  • Exports108
  • LanguageGHC2021
  • LicenceBSD-3-Clause
  • SourceAnnotation.hs

Core Exact Print Annotation types

10 declarations
datadata AnnKeywordId
#

Exact print annotations exist so that tools can perform source to source conversions of Haskell code. They are used to keep track of the various syntactic keywords that are not otherwise captured in the AST.

The wiki page describing this feature is https://gitlab.haskell.org/ghc/ghc/wikis/api-annotations https://gitlab.haskell.org/ghc/ghc/-/wikis/implementing-trees-that-grow/in-tree-api-annotations

Note: in general the names of these are taken from the corresponding token, unless otherwise noted See Note [exact print annotations] above for details of the usage

Constructors

Instances6Eq, Data, Ord, Show, NoAnn, Outputable
datadata EpToken (tok :: Symbol)
#

A token stored in the syntax tree. For example, when parsing a let-expression, we store EpToken "let" and EpToken "in". The locations of those tokens can be used to faithfully reproduce (exactprint) the original program text.

Instances3Eq, Data, NoAnn
datadata EpUniToken (tok :: Symbol) (utok :: Symbol)
#

With UnicodeSyntax, there might be multiple ways to write the same token. For example an arrow could be either -> or →. This choice must be recorded in order to exactprint such tokens, so instead of EpToken "->" we introduce EpUniToken "->" "→".

Instances2Data, NoAnn
datadata EpLayout
#

Layout information for declarations.

Constructors

  • EpExplicitBraces !(EpToken "{") !(EpToken "}")

    Explicit braces written by the user.

    class C a where { foo :: a; bar :: a }
    
  • EpVirtualBraces !Int

    Virtual braces inserted by the layout algorithm.

    class C a where
      foo :: a
      bar :: a
    
  • EpNoLayout

    Empty or compiler-generated blocks do not have layout information associated with them.

Instances1Data
datadata EpaComment
#

Constructors

Instances10Eq, Data, Show, Semigroup, HasAnnotation, HasLoc, …
datadata IsUnicodeSyntax
#

Certain tokens can have alternate representations when unicode syntax is enabled. This flag is attached to those tokens in the lexer so that the original source representation can be reproduced in the corresponding EpAnnotation

Instances5Eq, Data, Ord, Show, Outputable
datadata HasE
#

Some template haskell tokens have two variants, one with an e the other not:

 [| or [e|
 [|| or [e||

This type indicates whether the e is present or not.

Instances4Eq, Data, Ord, Show
  • Eq HasEDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Data HasEDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Ord HasEDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Show HasEDefined in ghc-9.10.3 · GHC.Parser.Annotation

In-tree Exact Print Annotations

15 declarations
datadata AddEpAnn
#

Captures an annotation, storing the AnnKeywordId and its location. The parser only ever inserts EpaLocation fields with a RealSrcSpan being the original location of the annotation in the source file. The EpaLocation can also store a delta position if the AST has been modified and needs to be pretty printed again. The usual way an AddEpAnn is created is using the mj ("make jump") function, and then it can be inserted into the appropriate annotation.

Instances4Eq, Data, NoAnn, Outputable
datadata EpaLocation' a
#

The anchor for an AnnKeywordId. The Parser inserts the EpaSpan variant, giving the exact location of the original item in the parsed source. This can be replaced by the EpaDelta version, to provide a position for the item relative to the end of the previous item in the source. This is useful when editing an AST prior to exact printing the changed one. The list of comments in the EpaDelta variant captures any comments between the prior output and the thing being marked here, since we cannot otherwise sort the relative order.

Instances10Semigroup, HasAnnotation, HasLoc, NoAnn, Eq, Data, …
datadata DeltaPos
#

Spacing between output items when exact printing. It captures the spacing from the current print position on the page to the position required for the thing about to be printed. This is either on the same line in which case is is simply the number of spaces to emit, or it is some number of lines down, with a given column offset. The exact printing algorithm keeps track of the column offset pertaining to the current anchor position, so the deltaColumn is the additional spaces to add in this case. See https://gitlab.haskell.org/ghc/ghc/wikis/api-annotations for details.

Constructors

Instances5Eq, Data, Ord, Show, Outputable
datadata EpAnn ann
#

The exact print annotations (EPAs) are kept in the HsSyn AST for the GhcPs phase. We do not always have EPAs though, only for code that has been parsed as they do not exist for generated code. This type captures that they may be missing.

A goal of the annotations is that an AST can be edited, including moving subtrees from one place to another, duplicating them, and so on. This means that each fragment must be self-contained. To this end, each annotated fragment keeps track of the anchor position it was originally captured at, being simply the start span of the topmost element of the ast fragment. This gives us a way to later re-calculate all Located items in this layer of the AST, as well as any annotations captured. The comments associated with the AST fragment are also captured here.

The ann type parameter allows this general structure to be specialised to the specific set of locations of original exact print annotation elements. So for HsLet we have

type instance XLet GhcPs = EpAnn AnnsLet data AnnsLet = AnnsLet { alLet :: EpaLocation, alIn :: EpaLocation } deriving Data

The spacing between the items under the scope of a given EpAnn is normally derived from the original Anchor. But if a sub-element is not in its original position, the required spacing can be directly captured in the anchor_op field of the entry Anchor. This allows us to freely move elements around, and stitch together new AST fragments out of old ones, and have them still printed out in a precise way.

Constructors

  • EpAnn
    • entry :: !Anchor

      Base location for the start of the syntactic element holding the annotations.

    • anns :: !ann

      Annotations added by the Parser

    • comments :: !EpAnnComments

      Comments enclosed in the SrcSpan of the element this EpAnn is attached to

Instances146Functor, Eq, Semigroup, HasAnnotation, HasLoc, NoAnn, …
typetype Anchor = EpaLocation
#

An Anchor records the base location for the start of the syntactic element holding the annotations, and is used as the point of reference for calculating delta positions for contained annotations. It is also normally used as the reference point for the spacing of the element relative to its container. If the AST element is moved, that relationship is tracked in the anchor_op instead.

classclass NoAnn a where
#

Methods

  • noAnn :: a

    equivalent of mempty, but does not need Semigroup

Instances29NoAnn, …

Comments in Annotations

datadata EpAnnComments
#

When we are parsing we add comments that belong a particular AST element, and print them together with the element, interleaving them into the output stream. But when editing the AST to move fragments around it is useful to be able to first separate the comments into those occurring before the AST element and those following it. The EpaCommentsBalanced constructor is used to do this. The GHC parser will only insert the EpaComments form.

Instances4Eq, Data, Semigroup, Outputable
datadata NoComments
#
Instances11Eq, Data, Ord, Show, Semigroup, HasAnnotation, …

Annotations in GenLocated

Annotation data types used in GenLocated

datadata AnnListItem
#

Annotation for items appearing in a list. They can have one or more trailing punctuations items, such as commas or semicolons.

Instances125Eq, Semigroup, NoAnn, Outputable, HasType, HasHaddock, …
datadata AnnList
#

Annotation for the "container" of a list. This captures surrounding items such as braces if present, and introductory keywords such as where.

Constructors

Instances7Eq, Data, NoAnn, Outputable, ToHie, HasHaddock, …
  • Eq AnnListDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Data AnnListDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • NoAnn AnnListDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Outputable AnnListDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • ToHie (LBooleanFormula (LocatedN Name))Defined in ghc-9.10.3 · GHC.Iface.Ext.Ast
  • ToHie (LocatedL [LocatedA (ConDeclField GhcRn)])Defined in ghc-9.10.3 · GHC.Iface.Ext.Ast
  • HasHaddock (LocatedL [LocatedA (IE GhcPs)])Defined in ghc-9.10.3 · GHC.Parser.PostProcess.Haddock

    Only for module exports, not module imports.

    module M (a, b, c) where -- use on this [LIE GhcPs] import I (a, b, c) -- do not use here!

    Imports cannot have documentation comments anyway.

datadata ParenType
#

Detail of the "brackets" used in an AnnParen exact print annotation.

Constructors

Instances5Eq, Data, Ord, Show, Outputable
datadata AnnPragma
#

exact print annotation used for capturing the locations of annotations in pragmas.

Instances5Eq, Data, NoAnn, Outputable, ToHie
datadata AnnContext
#

Exact print annotation for the Context data type.

Constructors

Instances6Data, NoAnn, Outputable, ToHie, HasHaddock
datadata NameAnn
#

exact print annotations for a RdrName. There are many kinds of adornment that can be attached to a given RdrName. This type captures them, as detailed on the individual constructors.

Instances6Eq, Data, NoAnn, Outputable, ToHie
datadata NameAdornment
#

A NameAnn can capture the locations of surrounding adornments, such as parens or backquotes. This data type identifies what particular pair are being used.

Constructors

Instances4Eq, Data, Ord, Outputable
datadata NoEpAnns
#
Instances16Eq, Data, Ord, NoAnn, Outputable, ToHie, …
datadata AnnSortKey tag
#

Captures the sort order of sub elements for ValBinds, ClassDecl, ClsInstDecl

Constructors

Instances5Eq, Data, Semigroup, Monoid, Outputable
datadata DeclTag
#

Used to track interleaving of class methods, class signatures, associated types and associate type defaults in ClassDecl and ClsInstDecl.

Instances5Eq, Data, Ord, Show, Outputable
  • Eq DeclTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Data DeclTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Ord DeclTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Show DeclTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Outputable DeclTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
datadata BindTag
#

Used to track of interleaving of binds and signatures for ValBind

Instances5Eq, Data, Ord, Show, Outputable
  • Eq BindTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Data BindTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Ord BindTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Show BindTagDefined in ghc-9.10.3 · GHC.Parser.Annotation
  • Outputable BindTagDefined in ghc-9.10.3 · GHC.Parser.Annotation

Trailing annotations in lists

datadata TrailingAnn
#

Captures the location of punctuation occurring between items, normally in a list. It is captured as a trailing annotation.

Instances3Eq, Data, Outputable

Utilities for converting between different GenLocated when

we do not care about the annotations.

valuel2l :: (HasLoc a, HasAnnotation b) => a -> b
#

Helper function for converting annotation types. Discards any annotations

classclass HasLoc a where
#

Methods

  • getHasLoc :: a -> SrcSpan

    conveniently calculate locations for things without locations attached

Instances10HasLoc, …

Building up annotations

Querying annotations

Working with locations of annotations

Constructing GenLocated annotation types when we do not care

Working with comments in annotations