-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Utilities to tie up tokens to an AST
--
-- This library is currently experimental.
--
-- The GHC part is solid, since it has been migrated from HaRe.
--
-- The haskell-src-exts one is still in progress
--
-- This package provides a set of data structures to manage the tie-up
-- between a Haskell AST and the underlying tokens, such that it
-- explicitly captures the Haskell layout rules and original formatting.
-- As a result changes can be made to the AST and the tokens will be
-- updated so that the source file can be recreated with only the updated
-- parts changed. This makes it easier to write Haskell source code
-- modification programmes.
@package haskell-token-utils
@version 0.0.0.1
module Language.Haskell.TokenUtils.Types
-- | An entry in the data structure for a particular srcspan.
data Entry a
-- | Entry has * the source span contained in this Node * how the
-- sub-elements nest * the tokens for the SrcSpan if subtree is empty
Entry :: !ForestSpan -> !Layout -> ![a] -> Entry a
-- | Deleted has * the source span has been deleted * prior gap in lines *
-- the gap between this span end and the start of the next in the fringe
-- of the tree.
Deleted :: !ForestSpan -> !RowOffset -> !SimpPos -> Entry a
data TokenCache a
TK :: !(Map TreeId (Tree (Entry a))) -> !TreeId -> TokenCache a
tkCache :: TokenCache a -> !(Map TreeId (Tree (Entry a)))
tkLastTreeId :: TokenCache a -> !TreeId
data TreeId
TId :: !Int -> TreeId
-- | Identifies the tree carrying the main tokens, not any work in progress
-- or deleted ones
mainTid :: TreeId
-- | Match a SrcSpan, using a ForestLine as the marker
type ForestSpan = (ForestPos, ForestPos)
type ForestPos = (ForestLine, Int)
data ForestLine
ForestLine :: !Bool -> !Int -> !Int -> !Int -> ForestLine
-- | The length of the span may have changed due to updated tokens.
flSpanLengthChanged :: ForestLine -> !Bool
flTreeSelector :: ForestLine -> !Int
flInsertVersion :: ForestLine -> !Int
flLine :: ForestLine -> !Int
type RowOffset = Int
type ColOffset = Int
type Row = Int
type Col = Int
type SimpPos = (Int, Int)
data Layout
-- | Initial offset from token before the stacked list of items, the (r,c)
-- of the first non-comment token, the (r,c) of the end of the last
-- non-comment token in the stacked list to be able to calculate the
-- (RowOffset,ColOffset) between the last token and the start of the next
-- item.
Above :: EndOffset -> (Row, Col) -> (Row, Col) -> EndOffset -> Layout
NoChange :: Layout
data EndOffset
None :: EndOffset
SameLine :: ColOffset -> EndOffset
FromAlignCol :: (RowOffset, ColOffset) -> EndOffset
data Located e
L :: Span -> e -> Located e
data Span
Span :: (Row, Col) -> (Row, Col) -> Span
nullSpan :: Span
data TokenLayout a
type LayoutTree a = Tree (Entry a)
forestSpanFromEntry :: Entry a -> ForestSpan
putForestSpanInEntry :: Entry a -> ForestSpan -> Entry a
-- | Strip out the version markers
forestSpanToSimpPos :: ForestSpan -> (SimpPos, SimpPos)
-- | Checks if the version is non-zero in either position
forestSpanVersionSet :: ForestSpan -> Bool
-- | Get the start and end position of a Tree treeStartEnd :: Tree Entry
-- -> (SimpPos,SimpPos) treeStartEnd (Node (Entry sspan _) _) =
-- (getGhcLoc sspan,getGhcLocEnd sspan)
treeStartEnd :: Tree (Entry a) -> ForestSpan
groupTokensByLine :: IsToken a => [a] -> [[a]]
tokenRow :: IsToken a => a -> Int
tokenCol :: IsToken a => a -> Int
tokenColEnd :: IsToken a => a -> Int
tokenPos :: IsToken a => a -> SimpPos
tokenPosEnd :: IsToken a => a -> SimpPos
-- | Shift the whole token by the given offset
increaseSrcSpan :: IsToken a => SimpPos -> a -> a
srcPosToSimpPos :: (Int, Int) -> (Int, Int)
-- | Add a constant line and column offset to a span of tokens
addOffsetToToks :: IsToken a => SimpPos -> [a] -> [a]
-- | Extract an encoded ForestLine from a GHC line
ghcLineToForestLine :: Int -> ForestLine
forestLineToGhcLine :: ForestLine -> Int
-- | The IsToken class captures the different token type in use. For GHC it
-- represents the type returned by getRichTokenStream, namely
-- [(GHC.Located GHC.Token, String)] For haskell-src-exts this is the
-- reult of lexTokenStream, namely `[HSE.Loc HSE.Token]`
class Show a => IsToken a
getSpan :: IsToken a => a -> Span
putSpan :: IsToken a => a -> Span -> a
tokenLen :: IsToken a => a -> Int
isComment :: IsToken a => a -> Bool
isEmpty :: IsToken a => a -> Bool
isDo :: IsToken a => a -> Bool
isElse :: IsToken a => a -> Bool
isIn :: IsToken a => a -> Bool
isLet :: IsToken a => a -> Bool
isOf :: IsToken a => a -> Bool
isThen :: IsToken a => a -> Bool
isWhere :: IsToken a => a -> Bool
tokenToString :: IsToken a => a -> String
showTokenStream :: IsToken a => [a] -> String
notWhiteSpace :: IsToken a => a -> Bool
isWhiteSpaceOrIgnored :: IsToken a => a -> Bool
isIgnored :: IsToken a => a -> Bool
-- | Tokens that are ignored when determining the first non-comment token
-- in a span
isIgnoredNonComment :: IsToken a => a -> Bool
isWhereOrLet :: IsToken a => a -> Bool
showFriendlyToks :: IsToken a => [a] -> String
class HasLoc a
getLoc :: HasLoc a => a -> SimpPos
getLocEnd :: HasLoc a => a -> SimpPos
class Allocatable b a
allocTokens :: Allocatable b a => b -> [a] -> LayoutTree a
instance Show EndOffset
instance Eq EndOffset
instance Show Layout
instance Eq Layout
instance Show a => Show (Entry a)
instance Eq TreeId
instance Ord TreeId
instance Show TreeId
instance Show Span
instance Eq Span
instance Ord Span
instance Show e => Show (Located e)
instance IsToken t => Ord (LayoutTree t)
instance HasLoc ForestSpan
instance Ord ForestLine
instance Show ForestLine
instance Eq ForestLine
instance HasLoc (Entry a)
instance IsToken a => Eq (Entry a)
module Language.Haskell.TokenUtils.DualTree
layoutTreeToSourceTree :: IsToken a => LayoutTree a -> SourceTree a
retrieveLinesFromLayoutTree :: IsToken a => LayoutTree a -> [Line a]
retrieveLines :: IsToken a => SourceTree a -> [Line a]
renderLines :: IsToken a => [Line a] -> String
renderSourceTree :: IsToken a => SourceTree a -> String
-- | The main data structure for this module
type SourceTree a = DUALTree Transformation (Up a) Annot (Prim a)
data Line a
Line :: Row -> Col -> RowOffset -> Source -> LineOpt -> [a] -> Line a
data Source
SOriginal :: Source
SAdded :: Source
SWasAdded :: Source
renderLinesFromLayoutTree :: IsToken a => LayoutTree a -> String
data Alignment
ANone :: Alignment
AVertical :: Alignment
data Annot
Ann :: String -> Annot
ADeleted :: ForestSpan -> RowOffset -> SimpPos -> Annot
ASubtree :: ForestSpan -> Annot
data DeletedSpan
DeletedSpan :: Span -> RowOffset -> SimpPos -> DeletedSpan
data LineOpt
ONone :: LineOpt
-- | This line needs to be grouped with the next in terms of layout, so any
-- column offsets need to be propagated
OGroup :: LineOpt
data Prim a
PToks :: [a] -> Prim a
PDeleted :: ForestSpan -> RowOffset -> SimpPos -> Prim a
data Transformation
TAbove :: ColOffset -> EndOffset -> (Row, Col) -> (Row, Col) -> EndOffset -> Transformation
-- | The value that bubbles up. This is the Span occupied by the subtree,
-- together with a string representation of the subtree. The origin of
-- the string is the start of the span.
data Up a
Up :: Span -> Alignment -> (NonEmpty (Line a)) -> [DeletedSpan] -> Up a
UDeleted :: [DeletedSpan] -> Up a
instance Show DeletedSpan
instance Eq DeletedSpan
instance Show Transformation
instance Show Alignment
instance Eq Alignment
instance Show Source
instance Eq Source
instance Show LineOpt
instance Eq LineOpt
instance IsToken a => Show (Up a)
instance Show Annot
instance Show a => Show (Prim a)
instance Action Transformation (Up a)
instance Semigroup Transformation
instance IsToken a => Semigroup (Up a)
instance Semigroup Span
instance IsToken a => Show (Line a)
module Language.Haskell.TokenUtils.Layout
retrieveTokens :: IsToken a => LayoutTree a -> [a]
module Language.Haskell.TokenUtils.Pretty
class Outputable a
ppr :: Outputable a => a -> Doc
showPpr :: Outputable a => a -> String
instance (Outputable a, Outputable b) => Outputable (a, b)
instance Outputable a => Outputable [a]
instance Outputable Row
instance Outputable Bool
instance Outputable ForestLine
instance Outputable LineOpt
instance Outputable Source
instance IsToken a => Outputable (Line a)
instance Outputable a => Outputable (NonEmpty a)
instance Outputable Span
instance Outputable DeletedSpan
instance Outputable Alignment
instance IsToken a => Outputable (Up a)
instance Outputable Annot
instance Outputable EndOffset
instance Outputable Transformation
instance IsToken a => Outputable (Prim a)
instance IsToken a => Outputable (DUALTreeNE Transformation (Up a) Annot (Prim a))
instance IsToken a => Outputable (DUALTreeU Transformation (Up a) Annot (Prim a))
instance IsToken a => Outputable (SourceTree a)
module Language.Haskell.TokenUtils.Utils
-- | Split the token stream into three parts: the tokens before the
-- startPos, the tokens between startPos and endPos, and the tokens after
-- endPos. Note: The startPos and endPos refer to the startPos of a token
-- only. So a single token will have the same startPos and endPos NO^^^^
splitToks :: IsToken a => (SimpPos, SimpPos) -> [a] -> ([a], [a], [a])
ghead :: String -> [a] -> a
glast :: String -> [a] -> a
gtail :: String -> [a] -> [a]
gfromJust :: [Char] -> Maybe a -> a
addEndOffsets :: IsToken a => LayoutTree a -> [a] -> LayoutTree a
calcLastTokenPos :: IsToken a => [a] -> (Int, Int)
makeOffset :: RowOffset -> ColOffset -> EndOffset
makeLeaf :: IsToken a => Span -> Layout -> [a] -> LayoutTree a
makeLeafFromToks :: IsToken a => [a] -> [LayoutTree a]
-- | Split the given tokens to include the comments belonging to the span.
splitToksIncComments :: IsToken a => (SimpPos, SimpPos) -> [a] -> ([a], [a], [a])
makeGroup :: IsToken a => [LayoutTree a] -> LayoutTree a
makeGroupLayout :: IsToken a => Layout -> [LayoutTree a] -> LayoutTree a
makeSpanFromTrees :: [LayoutTree a] -> ForestSpan
mkGroup :: IsToken a => Span -> Layout -> [LayoutTree a] -> LayoutTree a
subTreeOnly :: IsToken a => [LayoutTree a] -> [LayoutTree a]
-- | Split the given tokens into the ones that occur prior to the start of
-- the list and ones that occur after
splitToksForList :: (IsToken a, HasLoc b) => [b] -> [a] -> ([a], [a], [a])
placeAbove :: IsToken a => EndOffset -> (Row, Col) -> (Row, Col) -> [LayoutTree a] -> LayoutTree a
allocList :: (IsToken a, HasLoc b) => [b] -> [a] -> (b -> [a] -> [LayoutTree a]) -> [LayoutTree a]
strip :: IsToken a => [LayoutTree a] -> [LayoutTree a]
sf :: Span -> ForestSpan
srcSpanToForestSpan :: Span -> ForestSpan
fs :: ForestSpan -> Span
forestSpanToSrcSpan :: ForestSpan -> Span
treeIdFromForestSpan :: ForestSpan -> TreeId
-- | Gets the version numbers
forestSpanVersions :: ForestSpan -> (Int, Int)
-- | Gets the AST tree numbers
forestSpanAstVersions :: ForestSpan -> (Int, Int)
-- | Gets the SpanLengthChanged flags
forestSpanLenChangedFlags :: ForestSpan -> (Bool, Bool)
-- | Checks if the version is zero in both positions
forestSpanVersionNotSet :: ForestSpan -> Bool
-- | Checks if the version is non-zero
forestPosVersionSet :: ForestPos -> Bool
-- | Checks if the AST version is non-zero
forestPosAstVersionSet :: ForestPos -> Bool
-- | Checks if the version is zero
forestPosVersionNotSet :: ForestPos -> Bool
forestSpanLenChanged :: ForestSpan -> Bool
forestPosLenChanged :: ForestPos -> Bool
-- | Puts a TreeId into a forestSpan
treeIdIntoForestSpan :: TreeId -> ForestSpan -> ForestSpan
-- | Does the first span contain the second? Takes cognisance of the
-- various flags a ForestSpan can have. NOTE: This function relies on the
-- Eq instance for ForestLine
spanContains :: ForestSpan -> ForestSpan -> Bool
insertVersionsInForestSpan :: Int -> Int -> ForestSpan -> ForestSpan
insertLenChangedInForestSpan :: Bool -> ForestSpan -> ForestSpan
spanStartEnd :: Span -> (SimpPos, SimpPos)
-- | ForestSpan version of GHC combineSrcSpans
combineSpans :: ForestSpan -> ForestSpan -> ForestSpan
-- | Neat 2-dimensional drawing of a tree.
drawTreeEntry :: Tree (Entry a) -> String
-- | Neat 2-dimensional drawing of a forest.
drawForestEntry :: Forest (Entry a) -> String
showLayout :: Layout -> String
drawTreeCompact :: Tree (Entry a) -> String
drawTreeWithToks :: IsToken a => Tree (Entry a) -> String
showForestSpan :: ForestSpan -> String
module Language.Haskell.TokenUtils.TokenUtils
replaceTokenInCache :: IsToken a => TokenCache a -> Span -> a -> TokenCache a
-- | Replace a single token in a token tree, without changing the structure
-- of the tree NOTE: the GHC.SrcSpan may have been used to select the
-- appropriate forest in the first place, and is required to select the
-- correct span in the tree, due to the ForestLine annotations that may
-- be present
replaceTokenForSrcSpan :: IsToken a => Tree (Entry a) -> Span -> a -> Tree (Entry a)
invariant :: a
module Language.Haskell.TokenUtils.GHC.Layout
initTokenLayout :: ParsedSource -> [GhcPosToken] -> LayoutTree GhcPosToken
ghcAllocTokens :: ParsedSource -> [GhcPosToken] -> LayoutTree GhcPosToken
retrieveTokens :: IsToken a => LayoutTree a -> [a]
getLoc :: HasLoc a => a -> SimpPos
nullSrcSpan :: SrcSpan
addEndOffsets :: IsToken a => LayoutTree a -> [a] -> LayoutTree a
instance Show (GenLocated SrcSpan Token)
instance HasLoc (Located a)
instance IsToken (Located Token, String)
instance Allocatable ParsedSource GhcPosToken
instance Outputable EndOffset
instance Outputable Token
instance Outputable Layout
instance Outputable ForestLine
instance Outputable (Entry GhcPosToken)
instance Outputable (LayoutTree GhcPosToken)
instance Outputable LineOpt
instance Outputable Source
instance Outputable (Line GhcPosToken)
module Language.Haskell.TokenUtils.HSE.Layout
loadFile :: FilePath -> IO (ParseResult (Module SrcSpanInfo, [Loc TuToken]))
loadFileWithMode :: ParseMode -> FilePath -> IO (ParseResult (Module SrcSpanInfo, [Loc TuToken]))
templateHaskellMode :: ParseMode
data TuToken
T :: Token -> TuToken
C :: Comment -> TuToken
instance Show TuToken
instance Eq TuToken
instance Monoid Layout
instance Allocatable (Module SrcSpanInfo) (Loc TuToken)
instance IsToken (Loc TuToken)
module Language.Haskell.TokenUtils.API
-- | The IsToken class captures the different token type in use. For GHC it
-- represents the type returned by getRichTokenStream, namely
-- [(GHC.Located GHC.Token, String)] For haskell-src-exts this is the
-- reult of lexTokenStream, namely `[HSE.Loc HSE.Token]`
class Show a => IsToken a
getSpan :: IsToken a => a -> Span
putSpan :: IsToken a => a -> Span -> a
tokenLen :: IsToken a => a -> Int
isComment :: IsToken a => a -> Bool
isEmpty :: IsToken a => a -> Bool
isDo :: IsToken a => a -> Bool
isElse :: IsToken a => a -> Bool
isIn :: IsToken a => a -> Bool
isLet :: IsToken a => a -> Bool
isOf :: IsToken a => a -> Bool
isThen :: IsToken a => a -> Bool
isWhere :: IsToken a => a -> Bool
tokenToString :: IsToken a => a -> String
showTokenStream :: IsToken a => [a] -> String
class HasLoc a
getLoc :: HasLoc a => a -> SimpPos
getLocEnd :: HasLoc a => a -> SimpPos