1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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> {
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: raw, tu: 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()
}
}