use std::fmt;
use std::mem;
use clang_sys::*;
use utility;
use super::{TranslationUnit};
use super::source::{SourceLocation, SourceRange};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[repr(C)]
pub enum TokenKind {
Comment = 4,
Identifier = 2,
Keyword = 1,
Literal = 3,
Punctuation = 0,
}
#[derive(Copy, Clone)]
pub struct Token<'tu> {
pub(crate) raw: CXToken,
tu: &'tu TranslationUnit<'tu>,
}
impl<'tu> Token<'tu> {
#[doc(hidden)]
pub fn from_raw(raw: CXToken, tu: &'tu TranslationUnit<'tu>) -> Token<'tu> {
Token{ raw, tu }
}
pub fn get_kind(&self) -> TokenKind {
unsafe { mem::transmute(clang_getTokenKind(self.raw)) }
}
pub fn get_spelling(&self) -> String {
unsafe { utility::to_string(clang_getTokenSpelling(self.tu.ptr, self.raw)) }
}
pub fn get_location(&self) -> SourceLocation<'tu> {
unsafe { SourceLocation::from_raw(clang_getTokenLocation(self.tu.ptr, self.raw), self.tu) }
}
pub fn get_range(&self) -> SourceRange<'tu> {
unsafe { SourceRange::from_raw(clang_getTokenExtent(self.tu.ptr, self.raw), self.tu) }
}
}
impl<'tu> fmt::Debug for Token<'tu> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.debug_struct("Token")
.field("kind", &self.get_kind())
.field("spelling", &self.get_spelling())
.field("range", &self.get_range())
.finish()
}
}