Skip to content

Instantly share code, notes, and snippets.

@wz1000
Last active May 15, 2018 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wz1000/edf14747bd890b08c01c226d5bc6a1d6 to your computer and use it in GitHub Desktop.
Save wz1000/edf14747bd890b08c01c226d5bc6a1d6 to your computer and use it in GitHub Desktop.
data HieToken = HieToken
{ htkInfo :: TokenType
, htkSpan :: Span
, htkDetails :: Maybe TokenDetails
, htkType :: Maybe TypeIndex
}
data HieAST =
Leaf HieToken
| Node
{ nodeInfo :: NodeInfo -- ^ Can include the type and constructor this node corresponds to in HsSyn
, nodeSpan :: Span
, nodeChildren :: [HieAST] -- ^ in the order they appear in the source, elements should have disjoin spans and be entirely contained in the parent span.
} -- ^ Root Node will span the entire file
-- flattenAST ast should be a superset of [RichToken] that is consumed by the haddock hyperlinker
flattenAST :: HieAST -> [HieToken]
flattenAST (Leaf a) = [a]
flattenAST (Node _ _ xs) = concatMap flattenAST xs
data HieFile = HieFile
{ hieVersion :: String
, ghcVersion :: String
, hsFile :: String
, hieTypes :: Array TypeIndex GHC.Type
, hieAST :: HieAST
, hsSrc :: String -- ^ the original file source
}
type NodeInfo = [String] -- ^ For now, might add type info
type Span = GHC.RealSrcSpan
type TypeIndex = Int
-- Stolen from haddock hyperlinker
data TokenType
= TkIdentifier
| TkKeyword
| TkString
| TkChar
| TkNumber
| TkOperator
| TkGlyph
| TkSpecial
| TkSpace
| TkComment
| TkCpp
| TkPragma
| TkUnknown
deriving (Show, Eq)
data TokenDetails
= RtkVar GHC.Name
| RtkType GHC.Name
| RtkBind GHC.Name
| RtkDecl GHC.Name
| RtkModule GHC.ModuleName
deriving (Eq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment