Safe Haskell | None |
---|
This module contains an API to manage a token stream.
This API is used internally by MonadFunctions and the other utility modules, it should probably never be used directly in a refactoring.
- putToksInCache :: IsToken a => TokenCache a -> Span -> [a] -> (TokenCache a, Span)
- putDeclToksInCache :: Data t => TokenCache PosToken -> SrcSpan -> [PosToken] -> Located t -> (TokenCache PosToken, SrcSpan, Located t)
- replaceTokenInCache :: IsToken a => TokenCache a -> Span -> a -> TokenCache a
- getTreeFromCache :: SrcSpan -> TokenCache PosToken -> Tree (Entry PosToken)
- replaceTreeInCache :: SrcSpan -> Tree (Entry PosToken) -> TokenCache PosToken -> TokenCache PosToken
- syncAstToLatestCache :: Data t => TokenCache PosToken -> Located t -> Located t
- addToksAfterSrcSpan :: IsToken a => Tree (Entry a) -> Span -> Positioning -> [a] -> (Tree (Entry a), Span)
- addDeclToksAfterSrcSpan :: Data t => Tree (Entry PosToken) -> SrcSpan -> Positioning -> [PosToken] -> Located t -> (Tree (Entry PosToken), SrcSpan, Located t)
- syncAST :: Data t => Located t -> ForestSpan -> Located t
- indentDeclToks :: (IsToken a, HasLoc t) => (t -> ForestSpan -> t) -> t -> Tree (Entry a) -> Int -> (t, Tree (Entry a))
- data Positioning
- = PlaceAdjacent
- | PlaceAbsolute !Int !Int
- | PlaceAbsCol !Int !Int !Int
- | PlaceOffset !Int !Int !Int
- | PlaceIndent !Int !Int !Int
- retrieveTokensInterim :: IsToken a => Tree (Entry a) -> [a]
- treeIdFromForestSpan :: ForestSpan -> TreeId
- posToSrcSpan :: Tree (Entry PosToken) -> (SimpPos, SimpPos) -> SrcSpan
- posToSrcSpanTok :: PosToken -> (SimpPos, SimpPos) -> SrcSpan
- fileNameFromTok :: PosToken -> FastString
- treeStartEnd :: Tree (Entry a) -> ForestSpan
- sf :: Span -> ForestSpan
- fs :: ForestSpan -> Span
- ss2f :: SrcSpan -> ForestSpan
- f2ss :: ForestSpan -> SrcSpan
- ss2s :: SrcSpan -> Span
- s2ss :: Span -> SrcSpan
- forestSpanFromEntry :: Entry a -> ForestSpan
- nonCommentSpan :: IsToken a => [a] -> (SimpPos, SimpPos)
- showTree :: IsToken a => Tree (Entry a) -> String
- showSrcSpan :: SrcSpan -> String
- showSrcSpanF :: SrcSpan -> String
- ghcSpanStartEnd :: SrcSpan -> ((Int, Int), (Int, Int))
- forestSpanToSimpPos :: ForestSpan -> (SimpPos, SimpPos)
- forestSpanToGhcPos :: ForestSpan -> (SimpPos, SimpPos)
- ghcLineToForestLine :: Int -> ForestLine
- stripForestLineFromGhc :: SrcSpan -> SrcSpan
- forestLineToGhcLine :: ForestLine -> Int
- forestSpanToGhcSrcSpan :: ForestSpan -> SrcSpan
- forestSpanVersions :: ForestSpan -> (Int, Int)
- forestSpanVersionSet :: ForestSpan -> Bool
- insertForestLineInSrcSpan :: ForestLine -> SrcSpan -> SrcSpan
- ghcSrcSpanToForestSpan :: SrcSpan -> ForestSpan
- nullForestSpan :: ForestSpan
- nullForestPos :: ForestPos
- srcPosToSimpPos :: (Int, Int) -> (Int, Int)
- showForestSpan :: ForestSpan -> String
- deleteGapsToks :: [Entry PosToken] -> [PosToken]
- drawTreeEntry :: Tree (Entry a) -> String
- drawForestEntry :: Forest (Entry a) -> String
- drawTreeCompact :: Tree (Entry a) -> String
Operations at TokenCache
level
putToksInCache :: IsToken a => TokenCache a -> Span -> [a] -> (TokenCache a, Span)
putDeclToksInCache :: Data t => TokenCache PosToken -> SrcSpan -> [PosToken] -> Located t -> (TokenCache PosToken, SrcSpan, Located t)Source
replaceTokenInCache :: IsToken a => TokenCache a -> Span -> a -> TokenCache a
getTreeFromCache :: SrcSpan -> TokenCache PosToken -> Tree (Entry PosToken)Source
replaceTreeInCache :: SrcSpan -> Tree (Entry PosToken) -> TokenCache PosToken -> TokenCache PosTokenSource
syncAstToLatestCache :: Data t => TokenCache PosToken -> Located t -> Located tSource
Assuming most recent operation has stashed the old tokens, sync the given AST to the most recent stash entry
Operations at Tree
Entry
level
:: IsToken a | |
=> Tree (Entry a) | TokenTree to be modified |
-> Span | Preceding location for new tokens |
-> Positioning | |
-> [a] | New tokens to be added |
-> (Tree (Entry a), Span) | updated TokenTree and SrcSpan location for the new tokens in the TokenTree |
Add new tokens after the given SrcSpan, constructing a new SrcSpan in the process
:: Data t | |
=> Tree (Entry PosToken) | TokenTree to be modified |
-> SrcSpan | Preceding location for new tokens |
-> Positioning | |
-> [PosToken] | New tokens to be added |
-> Located t | Declaration the tokens belong to, to be synced |
-> (Tree (Entry PosToken), SrcSpan, Located t) | updated TokenTree ,SrcSpan location for -> (Tree (Entry PosToken), GHC.SrcSpan,t) -- ^ updated TokenTree ,SrcSpan location for the new tokens in the TokenTree, and updated AST element |
Add new tokens belonging to an AST fragment after a given SrcSpan, and re-sync the AST fragment to match the new location
:: Data t | |
=> Located t | The AST (or fragment) |
-> ForestSpan | The SrcSpan created in the Tree (Entry PosToken) |
-> Located t | Updated AST and tokens |
Synchronise a located AST fragment to use a newly created SrcSpan in the token tree. TODO: Should this indent the tokens as well?
:: (IsToken a, HasLoc t) | |
=> (t -> ForestSpan -> t) | |
-> t | The AST (or fragment) |
-> Tree (Entry a) | Existing token tree |
-> Int | (signed) number of columns to indent/dedent |
-> (t, Tree (Entry a)) | Updated AST and tokens |
indent the tree and tokens by the given offset, and sync the AST to the tree too.
data Positioning
How new SrcSpans should be inserted in the Token tree, relative to the prior span
PlaceAdjacent | Only a single space between the end of the prior span and the new one |
PlaceAbsolute !Int !Int | Start at the specified line and col |
PlaceAbsCol !Int !Int !Int | Line offset and absolute Col. Mainly for forcing start at left margin, number of lines to add at the end |
PlaceOffset !Int !Int !Int | Line and Col offset for start, num lines to add at the end relative to the indent level of the prior span |
PlaceIndent !Int !Int !Int | Line and Col offset for start, num lines to add at the end relative to the indent level of the prior line |
Retrieving tokens
retrieveTokensInterim :: IsToken a => Tree (Entry a) -> [a]
Retrieve all the tokens at the leaves of the tree, in order. No adjustments are made to address gaps or re-alignment of the tokens
Token Tree Selection
Token marking and re-alignment
Utility
posToSrcSpan :: Tree (Entry PosToken) -> (SimpPos, SimpPos) -> SrcSpanSource
Convert a simple (start,end) position to a SrcSpan belonging to the file in the tree
posToSrcSpanTok :: PosToken -> (SimpPos, SimpPos) -> SrcSpanSource
Convert a simple (start,end) position to a SrcSpan belonging to the file in the given token
treeStartEnd :: Tree (Entry a) -> ForestSpan
Get the start and end position of a Tree treeStartEnd :: Tree Entry -> (SimpPos,SimpPos) treeStartEnd (Node (Entry sspan _) _) = (getGhcLoc sspan,getGhcLocEnd sspan)
sf :: Span -> ForestSpan
fs :: ForestSpan -> Span
ss2f :: SrcSpan -> ForestSpanSource
f2ss :: ForestSpan -> SrcSpanSource
forestSpanFromEntry :: Entry a -> ForestSpan
A token stream with last tokens first, and functions to manipulate it
Internal, for testing
nonCommentSpan :: IsToken a => [a] -> (SimpPos, SimpPos)
Extract the start and end position of a span, without any leading or trailing comments
showSrcSpan :: SrcSpan -> StringSource
showSrcSpanF :: SrcSpan -> StringSource
forestSpanToSimpPos :: ForestSpan -> (SimpPos, SimpPos)
Strip out the version markers
forestSpanToGhcPos :: ForestSpan -> (SimpPos, SimpPos)
Strip out the version markers
ghcLineToForestLine :: Int -> ForestLine
Extract an encoded ForestLine from a GHC line
forestLineToGhcLine :: ForestLine -> Int
forestSpanVersions :: ForestSpan -> (Int, Int)
Gets the version numbers
forestSpanVersionSet :: ForestSpan -> Bool
Checks if the version is non-zero in either position
insertForestLineInSrcSpan :: ForestLine -> SrcSpan -> SrcSpanSource
Replace any ForestLine flags already in a SrcSpan with the given ones
srcPosToSimpPos :: (Int, Int) -> (Int, Int)
showForestSpan :: ForestSpan -> String
deleteGapsToks :: [Entry PosToken] -> [PosToken]Source
Process the leaf nodes of a tree to remove all deleted spans
Based on Data.Tree
drawTreeEntry :: Tree (Entry a) -> String
Neat 2-dimensional drawing of a tree.
drawForestEntry :: Forest (Entry a) -> String
Neat 2-dimensional drawing of a forest.
drawTreeCompact :: Tree (Entry a) -> String