-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtoken.go
178 lines (159 loc) · 4.56 KB
/
token.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package clang
// #include <stdlib.h>
// #include "go-clang.h"
import "C"
import (
"fmt"
)
// TokenKind describes a kind of token
type TokenKind uint32
const (
/**
* \brief A token that contains some kind of punctuation.
*/
TK_Punctuation = C.CXToken_Punctuation
/**
* \brief A language keyword.
*/
TK_Keyword = C.CXToken_Keyword
/**
* \brief An identifier (that is not a keyword).
*/
TK_Identifier = C.CXToken_Identifier
/**
* \brief A numeric, string, or character literal.
*/
TK_Literal = C.CXToken_Literal
/**
* \brief A comment.
*/
TK_Comment = C.CXToken_Comment
)
func (tk TokenKind) String() string {
switch tk {
case TK_Punctuation:
return "Punctuation"
case TK_Keyword:
return "Keyword"
case TK_Identifier:
return "Identifier"
case TK_Literal:
return "Literal"
case TK_Comment:
return "Comment"
default:
panic(fmt.Errorf("clang: invalid TokenKind value (%d)", uint32(tk)))
}
}
// Token is a single preprocessing token.
type Token struct {
c C.CXToken
}
// Kind determines the kind of this token
func (t Token) Kind() TokenKind {
o := C.clang_getTokenKind(t.c)
return TokenKind(o)
}
/**
* \brief Determine the spelling of the given token.
*
* The spelling of a token is the textual representation of that token, e.g.,
* the text of an identifier or keyword.
*/
func (tu TranslationUnit) TokenSpelling(tok Token) string {
cstr := cxstring{C.clang_getTokenSpelling(tu.c, tok.c)}
defer cstr.Dispose()
return cstr.String()
}
/**
* \brief Retrieve the source location of the given token.
*/
func (tu TranslationUnit) TokenLocation(tok Token) SourceLocation {
o := C.clang_getTokenLocation(tu.c, tok.c)
return SourceLocation{o}
}
/**
* \brief Retrieve a source range that covers the given token.
*/
func (tu TranslationUnit) TokenExtent(tok Token) SourceRange {
o := C.clang_getTokenExtent(tu.c, tok.c)
return SourceRange{o}
}
/**
* \brief Tokenize the source code described by the given range into raw
* lexical tokens.
*
* \param TU the translation unit whose text is being tokenized.
*
* \param Range the source range in which text should be tokenized. All of the
* tokens produced by tokenization will fall within this source range,
*
* \param Tokens this pointer will be set to point to the array of tokens
* that occur within the given source range. The returned pointer must be
* freed with clang_disposeTokens() before the translation unit is destroyed.
*
* \param NumTokens will be set to the number of tokens in the \c *Tokens
* array.
*
*/
func Tokenize(tu TranslationUnit, src SourceRange) Tokens {
tokens := Tokens{}
tokens.tu = tu.c
C.clang_tokenize(tu.c, src.c, &tokens.c, &tokens.n)
return tokens
}
// an array of tokens
type Tokens struct {
tu C.CXTranslationUnit
c *C.CXToken
n C.uint
}
/**
* \brief Annotate 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 token-annotation routine is equivalent to invoking
* clang_getCursor() for the source locations of each of the
* 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 \c f(x),
* clang_getCursor() would provide the following cursors:
*
* * when the cursor is over the 'f', a DeclRefExpr cursor referring to 'f'.
* * when the cursor is over the '(' or the ')', a CallExpr referring to 'f'.
* * when the cursor is over the 'x', a 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.
*
* \param TU the translation unit that owns the given tokens.
*
* \param Tokens the set of tokens to annotate.
*
* \param NumTokens the number of tokens in \p Tokens.
*
* \param Cursors an array of \p NumTokens cursors, whose contents will be
* replaced with the cursors corresponding to each token.
*/
func (t Tokens) Annotate() []Cursor {
cursors := make([]Cursor, int(t.n))
if t.n <= 0 {
return cursors
}
c_cursors := make([]C.CXCursor, int(t.n))
C.clang_annotateTokens(t.tu, t.c, t.n, &c_cursors[0])
for i, _ := range cursors {
cursors[i] = Cursor{C._go_clang_ocursor_at(&c_cursors[0], C.int(i))}
}
return cursors
}
/**
* \brief Free the given set of tokens.
*/
func (t Tokens) Dispose() {
C.clang_disposeTokens(t.tu, t.c, t.n)
}
// EOF