haskell-tools-ast-fromghc-0.1.2.0: Creating the Haskell-Tools AST from GHC's representations

Safe HaskellNone
LanguageHaskell2010

Language.Haskell.Tools.AST.FromGHC

Contents

Description

The FromGHC module provides a way to transform the GHC AST into our AST. This transformation is done in the Ghc monad. The conversion can be performed from the Parsed and the Renamed GHC AST. If the renamed AST is given, additional semantic information is looked up while traversing the AST.

Synopsis

Documentation

addTypeInfos :: LHsBinds Id -> Ann Module RangeWithName -> Ghc (Ann Module RangeWithType) Source #

trfModule :: Located (HsModule RdrName) -> Trf (Ann Module RangeInfo) Source #

trfModuleRename :: Module -> Ann Module RangeInfo -> (HsGroup Name, [LImportDecl Name], Maybe [LIE Name], Maybe LHsDocString) -> Located (HsModule RdrName) -> Trf (Ann Module RangeWithName) Source #

trfModuleHead :: TransformName n r => Maybe (Located ModuleName) -> Maybe (Located [LIE n]) -> Maybe (Located WarningTxt) -> Trf (AnnMaybe ModuleHead r) Source #

trfFilePragmas :: RangeAnnot a => Trf (AnnList FilePragma a) Source #

trfLanguagePragma :: RangeAnnot a => Located String -> Trf (Ann FilePragma a) Source #

trfOptionsPragma :: RangeAnnot a => Located String -> Trf (Ann FilePragma a) Source #

trfModulePragma :: RangeAnnot a => Maybe (Located WarningTxt) -> Trf (AnnMaybe ModulePragma a) Source #

trfText' :: RangeAnnot a => StringLiteral -> Trf (StringNode a) Source #

trfExportList :: TransformName n r => SrcLoc -> Maybe (Located [LIE n]) -> Trf (AnnMaybe ExportSpecList r) Source #

trfExportList' :: TransformName n r => [LIE n] -> Trf (ExportSpecList r) Source #

trfExport :: TransformName n r => LIE n -> Trf (Maybe (Ann ExportSpec r)) Source #

trfImports :: TransformName n r => [LImportDecl n] -> Trf (AnnList ImportDecl r) Source #

trfImport :: forall n r. TransformName n r => LImportDecl n -> Trf (Ann ImportDecl r) Source #

trfImportSpecs :: TransformName n r => Maybe (Bool, Located [LIE n]) -> Trf (AnnMaybe ImportSpec r) Source #

trfIESpec :: TransformName n r => LIE n -> Trf (Maybe (Ann IESpec r)) Source #

trfIESpec' :: TransformName n r => IE n -> Trf (Maybe (IESpec r)) Source #

type Trf = ReaderT TrfInput Ghc Source #

The transformation monad type

data TrfInput Source #

The (immutable) data for the transformation

Constructors

TrfInput 

Fields

define :: Trf a -> Trf a Source #

Perform the transformation taking names as defined.

defineTypeVars :: Trf a -> Trf a Source #

Perform the transformation taking type variable names as defined.

typeVarTransform :: Trf a -> Trf a Source #

Transform as type variables

transformingPossibleVar :: HsHasName n => n -> Trf a -> Trf a Source #

Transform a name as a type variable if it is one.

addToScope :: HsHasName e => e -> Trf a -> Trf a Source #

Perform the transformation putting the given definition in a new local scope.

addToCurrentScope :: HsHasName e => e -> Trf a -> Trf a Source #

Perform the transformation putting the given definitions in the current scope.

runTrf :: Map ApiAnnKey [SrcSpan] -> Map String [Located String] -> Trf a -> Ghc a Source #

Performs the transformation given the tokens of the source file

getOriginalName :: RdrName -> Trf String Source #

Get the original format of a name (before scoping).

trfOperator :: TransformName name res => Located name -> Trf (Ann Operator res) Source #

trfOperator' :: TransformName name res => name -> Trf (Operator res) Source #

trfName :: TransformName name res => Located name -> Trf (Ann Name res) Source #

trfName' :: TransformName name res => name -> Trf (Name res) Source #

trfAmbiguousFieldName' :: forall n res. TransformName n res => SrcSpan -> AmbiguousFieldOcc n -> Trf (Ann Name res) Source #

class (RangeAnnot res, SemanticAnnot res name, SemanticAnnot res Name, TransformableName name, HsHasName name) => TransformName name res Source #

This class allows us to use the same transformation code for multiple variants of the GHC AST. GHC Name annotated with name can be transformed to our representation with semantic annotations of res.

addNameInfo :: TransformName n r => n -> Ann SimpleName r -> Trf (Ann SimpleName r) Source #

trfSimpleName :: TransformName name res => Located name -> Trf (Ann SimpleName res) Source #

trfSimpleName' :: TransformName name res => name -> Trf (SimpleName res) Source #

trfNameStr :: RangeAnnot a => String -> Trf (AnnList UnqualName a) Source #

Creates a qualified name from a name string

trfNameStr' :: RangeAnnot a => String -> SrcLoc -> [Ann UnqualName a] Source #

trfModuleName :: RangeAnnot a => Located ModuleName -> Trf (Ann SimpleName a) Source #

trfModuleName' :: RangeAnnot a => ModuleName -> Trf (SimpleName a) Source #

trfFastString :: RangeAnnot a => Located FastString -> Trf (Ann StringNode a) Source #

trfDataKeyword :: RangeAnnot a => NewOrData -> Trf (Ann DataOrNewtypeKeyword a) Source #

trfCallConv :: RangeAnnot a => Located CCallConv -> Trf (Ann CallConv a) Source #

trfCallConv' :: RangeAnnot a => CCallConv -> Trf (CallConv a) Source #

trfSafety :: RangeAnnot a => SrcSpan -> Located Safety -> Trf (AnnMaybe Safety a) Source #

trfOverlap :: RangeAnnot a => Located OverlapMode -> Trf (Ann OverlapPragma a) Source #

trfRole :: RangeAnnot a => Located (Maybe Role) -> Trf (Ann Role a) Source #

trfPhase :: RangeAnnot a => Trf SrcLoc -> Activation -> Trf (AnnMaybe PhaseControl a) Source #

trfPhaseNum :: RangeAnnot a => PhaseNum -> Trf (Ann PhaseNumber a) Source #

class HasRange annot => RangeAnnot annot where Source #

Annotations that is made up from ranges

Instances

RangeAnnot (NodeInfo (SemanticInfo n) SpanInfo) Source # 

Methods

toNodeAnnot :: SrcSpan -> NodeInfo (SemanticInfo n) SpanInfo Source #

toListAnnot :: String -> String -> String -> SrcLoc -> NodeInfo (SemanticInfo n) SpanInfo Source #

toIndentedListAnnot :: String -> String -> String -> SrcLoc -> NodeInfo (SemanticInfo n) SpanInfo Source #

toOptAnnot :: String -> String -> SrcLoc -> NodeInfo (SemanticInfo n) SpanInfo Source #

class SemanticAnnot annot n where Source #

Annotations that carry semantic information

Minimal complete definition

addSemanticInfo, addScopeData, addImportData

Methods

addSemanticInfo :: SemanticInfo n -> annot -> annot Source #

addScopeData :: annot -> Trf annot Source #

addImportData :: Ann ImportDecl annot -> Trf (Ann ImportDecl annot) Source #

Instances

SemanticAnnot RangeWithName Name Source # 

Methods

addSemanticInfo :: SemanticInfo Name -> RangeWithName -> RangeWithName Source #

addScopeData :: RangeWithName -> Trf RangeWithName Source #

addImportData :: Ann ImportDecl RangeWithName -> Trf (Ann ImportDecl RangeWithName) Source #

SemanticAnnot RangeInfo n Source # 

Methods

addSemanticInfo :: SemanticInfo n -> RangeInfo -> RangeInfo Source #

addScopeData :: RangeInfo -> Trf RangeInfo Source #

addImportData :: Ann ImportDecl RangeInfo -> Trf (Ann ImportDecl RangeInfo) Source #

SemanticAnnot RangeInfo RdrName Source # 

Methods

addSemanticInfo :: SemanticInfo RdrName -> RangeInfo -> RangeInfo Source #

addScopeData :: RangeInfo -> Trf RangeInfo Source #

addImportData :: Ann ImportDecl RangeInfo -> Trf (Ann ImportDecl RangeInfo) Source #

addImportData' :: Ann ImportDecl RangeWithName -> Trf (Ann ImportDecl RangeWithName) Source #

Adds semantic information to an impord declaration. See ImportInfo.

getImportedNames :: String -> Maybe String -> Trf (Module, [Name]) Source #

Get names that are imported from a given import

checkImportVisible :: GhcMonad m => ImportDecl RangeWithName -> Name -> m Bool Source #

Check is a given name is imported from an import with given import specification.

ieSpecMatches :: GhcMonad m => IESpec RangeWithName -> Name -> m Bool Source #

nothing :: RangeAnnot a => String -> String -> Trf SrcLoc -> Trf (AnnMaybe e a) Source #

Creates a place for a missing node with a default location

emptyList :: RangeAnnot a => String -> Trf SrcLoc -> Trf (AnnList e a) Source #

makeList :: RangeAnnot a => String -> Trf SrcLoc -> Trf [Ann e a] -> Trf (AnnList e a) Source #

Creates a place for a list of nodes with a default place if the list is empty.

makeListBefore :: RangeAnnot a => String -> String -> Trf SrcLoc -> Trf [Ann e a] -> Trf (AnnList e a) Source #

makeListAfter :: RangeAnnot a => String -> String -> Trf SrcLoc -> Trf [Ann e a] -> Trf (AnnList e a) Source #

makeNonemptyList :: RangeAnnot a => String -> Trf [Ann e a] -> Trf (AnnList e a) Source #

makeIndentedList :: RangeAnnot a => Trf SrcLoc -> Trf [Ann e a] -> Trf (AnnList e a) Source #

Creates a place for an indented list of nodes with a default place if the list is empty.

makeIndentedListNewlineBefore :: RangeAnnot a => Trf SrcLoc -> Trf [Ann e a] -> Trf (AnnList e a) Source #

makeIndentedListBefore :: RangeAnnot a => String -> Trf SrcLoc -> Trf [Ann e a] -> Trf (AnnList e a) Source #

makeNonemptyIndentedList :: RangeAnnot a => Trf [Ann e a] -> Trf (AnnList e a) Source #

trfLoc :: RangeAnnot i => (a -> Trf (b i)) -> Located a -> Trf (Ann b i) Source #

Transform a located part of the AST by automatically transforming the location. Sets the source range for transforming children.

trfMaybe :: RangeAnnot i => String -> String -> (Located a -> Trf (Ann e i)) -> Maybe (Located a) -> Trf (AnnMaybe e i) Source #

Transforms a possibly-missing node with the default location of the end of the focus.

trfMaybeDefault :: RangeAnnot i => String -> String -> (Located a -> Trf (Ann e i)) -> Trf SrcLoc -> Maybe (Located a) -> Trf (AnnMaybe e i) Source #

Transforms a possibly-missing node with a default location

trfLocCorrect :: RangeAnnot i => (SrcSpan -> Trf SrcSpan) -> (a -> Trf (b i)) -> Located a -> Trf (Ann b i) Source #

Transform a located part of the AST by automatically transforming the location with correction by applying the given function. Sets the source range for transforming children.

trfMaybeLoc :: RangeAnnot i => (a -> Trf (Maybe (b i))) -> Located a -> Trf (Maybe (Ann b i)) Source #

Transform a located part of the AST by automatically transforming the location. Sets the source range for transforming children.

trfAnnList :: RangeAnnot i => String -> (a -> Trf (b i)) -> [Located a] -> Trf (AnnList b i) Source #

Creates a place for a list of nodes with the default place at the end of the focus if the list is empty.

trfAnnList' :: RangeAnnot i => String -> (Located a -> Trf (Ann b i)) -> [Located a] -> Trf (AnnList b i) Source #

nonemptyAnnList :: RangeAnnot i => [Ann e i] -> AnnList e i Source #

Creates a place for a list of nodes that cannot be empty.

makeJust :: RangeAnnot a => Ann e a -> AnnMaybe e a Source #

Creates an optional node from an existing element

annLoc :: RangeAnnot a => Trf SrcSpan -> Trf (b a) -> Trf (Ann b a) Source #

Annotates a node with the given location and focuses on the given source span.

Focus manipulation

focusOn :: SrcSpan -> Trf a -> Trf a Source #

between :: AnnKeywordId -> AnnKeywordId -> Trf a -> Trf a Source #

Focuses the transformation to go between tokens. The tokens must be found inside the current range.

betweenIfPresent :: AnnKeywordId -> AnnKeywordId -> Trf a -> Trf a Source #

Focuses the transformation to go between tokens if they are present

focusAfter :: AnnKeywordId -> Trf a -> Trf a Source #

Focuses the transformation to be performed after the given token. The token must be found inside the current range.

focusBefore :: AnnKeywordId -> Trf a -> Trf a Source #

Focuses the transformation to be performed after the given token. The token must be found inside the current range.

before :: AnnKeywordId -> Trf SrcLoc Source #

Gets the position before the given token

after :: AnnKeywordId -> Trf SrcLoc Source #

Gets the position after the given token

annFrom :: RangeAnnot a => AnnKeywordId -> Trf (e a) -> Trf (Ann e a) Source #

The element should span from the given token to the end of focus

atTheStart :: Trf SrcLoc Source #

Gets the position at the beginning of the focus

atTheEnd :: Trf SrcLoc Source #

Gets the position at the end of the focus

tokenLoc :: AnnKeywordId -> Trf SrcSpan Source #

Searches for a token inside the focus and retrieves its location

tokenLocBack :: AnnKeywordId -> Trf SrcSpan Source #

Searches for a token backward inside the focus and retrieves its location

tokensLoc :: [AnnKeywordId] -> Trf SrcSpan Source #

Searches for tokens in the given order inside the parent element and returns their combined location

uniqueTokenAnywhere :: AnnKeywordId -> Trf SrcSpan Source #

Searches for a token and retrieves its location anywhere

annCont :: RangeAnnot a => Trf (e a) -> Trf (Ann e a) Source #

Annotates the given element with the current focus as a location.

copyAnnot :: (Ann a i -> b i) -> Trf (Ann a i) -> Trf (Ann b i) Source #

Annotates the element with the same annotation that is on the other element

foldLocs :: [SrcSpan] -> SrcSpan Source #

Combine source spans into one that contains them all

advanceStr :: String -> SrcLoc -> SrcLoc Source #

The location after the given string

updateCol :: (Int -> Int) -> SrcLoc -> SrcLoc Source #

Update column information in a source location

collectLocs :: [Located e] -> SrcSpan Source #

Combine source spans of elements into one that contains them all

orderDefs :: RangeAnnot i => [Ann e i] -> [Ann e i] Source #

Rearrange definitions to appear in the order they are defined in the source file.

orderAnnList :: RangeAnnot i => AnnList e i -> AnnList e i Source #

Orders a list of elements to the order they are defined in the source file.

trfScopedSequence :: HsHasName d => (d -> Trf e) -> [d] -> Trf [e] Source #

Transform a list of definitions where the defined names are in scope for subsequent definitions

splitLocated :: Located String -> [Located String] Source #

Splits a given string at whitespaces while calculating the source location of the fragments