-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for the Clang.Token module. #23
- Loading branch information
1 parent
80e0a5b
commit afa445c
Showing
3 changed files
with
73 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,90 @@ | ||
{-# LANGUAGE ConstraintKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
|
||
-- | Functions for manipulating 'Clang.Token's. | ||
-- | ||
-- This module is intended to be imported qualified. | ||
module Clang.Token | ||
( | ||
-- * Getting information about tokens | ||
getKind | ||
-- * Token lists | ||
tokenize | ||
, List | ||
|
||
-- * Token kinds | ||
, getKind | ||
|
||
-- * Mapping between tokens and source code | ||
, getSpelling | ||
, getLocation | ||
, getExtent | ||
|
||
-- * Converting to and from lists of tokens | ||
, tokenize | ||
, annotateTokens | ||
) where | ||
|
||
import Control.Monad.IO.Class | ||
import qualified Data.Vector.Storable as DVS | ||
|
||
import qualified Clang.Internal.FFI as FFI | ||
import Clang.Internal.Monad | ||
|
||
-- | Tokenizes the source code described by the given range into raw | ||
-- lexical tokens. | ||
tokenize :: ClangBase m | ||
=> FFI.TranslationUnit s' -- ^ The translation list | ||
-- containing the source code. | ||
-> FFI.SourceRange s'' -- ^ The source range in which text | ||
-- should be tokenized. | ||
-> ClangT s m (List s) | ||
tokenize = FFI.tokenize | ||
|
||
-- | A list of tokens, stored as a 'DVS.Vector' for efficiency. | ||
type List s = DVS.Vector (FFI.Token s) | ||
|
||
-- | Determines the kind of the given token, expressed as a | ||
-- 'FFI.TokenKind' value. | ||
getKind :: ClangBase m => FFI.Token s' -> ClangT s m FFI.TokenKind | ||
getKind t = liftIO $ FFI.getTokenKind t | ||
|
||
-- | Retrieves the \'spelling\', or textual representation, of the | ||
-- given token. | ||
getSpelling :: ClangBase m => FFI.TranslationUnit s' -> FFI.Token s'' -> ClangT s m (FFI.ClangString s) | ||
getSpelling = FFI.getTokenSpelling | ||
|
||
-- | Returns the source location of the given token. | ||
getLocation :: ClangBase m => FFI.TranslationUnit s' -> FFI.Token s'' | ||
-> ClangT s m (FFI.SourceLocation s) | ||
getLocation tu tk = liftIO $ FFI.getTokenLocation mkProxy tu tk | ||
|
||
-- | Retrieves the source range that covers the given token. | ||
getExtent :: ClangBase m => FFI.TranslationUnit s' -> FFI.Token s'' | ||
-> ClangT s m (FFI.SourceRange s) | ||
getExtent tu tk = liftIO $ FFI.getTokenExtent mkProxy tu tk | ||
|
||
tokenize :: ClangBase m => FFI.TranslationUnit s' -> FFI.SourceRange s'' | ||
-> ClangT s m (FFI.TokenList s) | ||
tokenize = FFI.tokenize | ||
|
||
annotateTokens :: | ||
ClangBase m => | ||
FFI.TranslationUnit s' -- ^ The translation unit related to the tokens | ||
-> FFI.TokenList s'' -- ^ Token list that you want cursors for | ||
-> ClangT s m (FFI.CursorList s) -- ^ Cursors corresponding to the tokens | ||
-- | Annotates the given set of tokens by providing cursors for each token | ||
-- that can be mapped to a specific entity within the abstract syntax tree. | ||
-- | ||
-- This is equivalent to invoking 'Clang.Cursor.getCursor' on the | ||
-- source locations of each of these tokens. The cursors provided are | ||
-- filtered, so that only those cursors that have a direct | ||
-- correspondence to the token are accepted. For example, given a | ||
-- function call \'f(x)\', 'Clang.Cursor.getCursor' would provide the | ||
-- following cursors: | ||
-- | ||
-- * When the cursor is over the \'f\', a 'Clang.DeclRefExpr' cursor | ||
-- referring to \'f\'. | ||
-- | ||
-- * When the cursor is over the \'(\' or the \')\', a | ||
-- 'Clang.CallExpr' referring to \'f\'. | ||
-- | ||
-- * When the cursor is over the \'x\', a 'Clang.DeclRefExpr' cursor | ||
-- referring to \'x\'. | ||
-- | ||
-- Only the first and last of these cursors will occur within the | ||
-- annotate, since the tokens \'f\' and \'x\' directly refer to a function | ||
-- and a variable, respectively, but the parentheses are just a small | ||
-- part of the full syntax of the function call expression, which is | ||
-- not provided as an annotation. | ||
annotateTokens :: ClangBase m | ||
=> FFI.TranslationUnit s' -- ^ The translation unit that owns the tokens. | ||
-> List s'' -- ^ The tokens to annotate. | ||
-> ClangT s m (FFI.CursorList s) | ||
annotateTokens = FFI.annotateTokens |