1
1
#if !NO_SWIFTPM
2
2
import cclang
3
3
#endif
4
+
4
5
import Foundation
5
6
6
- public struct Token {
7
- internal let clang : CXToken
7
+ /// Represents a C, C++, or Objective-C token.
8
+ public protocol Token {
9
+ var clang : CXToken { get }
10
+ }
8
11
9
- /// Retrieves the kind of the receiver.
10
- public var kind : TokenKind {
11
- return TokenKind ( clang: clang_getTokenKind ( clang) )
12
- }
12
+ extension Token {
13
13
14
14
/// Determine the spelling of the given token.
15
15
/// The spelling of a token is the textual representation of that token,
@@ -18,8 +18,57 @@ public struct Token {
18
18
return clang_getTokenSpelling ( translationUnit. clang, clang) . asSwift ( )
19
19
}
20
20
21
- public func asClang( ) -> CXToken {
22
- return clang
21
+ /// Retrieve the source location of the given token.
22
+ /// - param translationUnit: The translation unit in which you're looking
23
+ /// for this token.
24
+ func location( in translationUnit: TranslationUnit ) -> SourceLocation {
25
+ return SourceLocation ( clang: clang_getTokenLocation ( translationUnit. clang,
26
+ clang) )
27
+ }
28
+
29
+ /// Retrieve a source range that covers the given token.
30
+ /// - param translationUnit: The translation unit in which you're looking
31
+ /// for this token.
32
+ func range( in translationUnit: TranslationUnit ) -> SourceRange {
33
+ return SourceRange ( clang: clang_getTokenExtent ( translationUnit. clang,
34
+ clang) )
35
+ }
36
+ }
37
+
38
+ /// A token that contains some kind of punctuation.
39
+ public struct PunctuationToken : Token {
40
+ public let clang : CXToken
41
+ }
42
+
43
+ /// A language keyword.
44
+ public struct KeywordToken : Token {
45
+ public let clang : CXToken
46
+ }
47
+
48
+ /// An identifier (that is not a keyword).
49
+ public struct IdentifierToken : Token {
50
+ public let clang : CXToken
51
+ }
52
+
53
+ /// A numeric, string, or character literal.
54
+ public struct LiteralToken : Token {
55
+ public let clang : CXToken
56
+ }
57
+
58
+ /// A comment.
59
+ public struct CommentToken : Token {
60
+ public let clang : CXToken
61
+ }
62
+
63
+ /// Converts a CXToken to a Token, returning `nil` if it was unsuccessful
64
+ func convertToken( _ clang: CXToken ) -> Token {
65
+ switch clang_getTokenKind ( clang) {
66
+ case CXToken_Punctuation: return PunctuationToken ( clang: clang)
67
+ case CXToken_Keyword: return KeywordToken ( clang: clang)
68
+ case CXToken_Identifier: return IdentifierToken ( clang: clang)
69
+ case CXToken_Literal: return LiteralToken ( clang: clang)
70
+ case CXToken_Comment: return CommentToken ( clang: clang)
71
+ default : fatalError ( " invalid CXTokenKind \( clang) " )
23
72
}
24
73
}
25
74
@@ -78,42 +127,3 @@ public struct SourceRange {
78
127
return SourceLocation ( clang: clang_getRangeEnd ( clang) )
79
128
}
80
129
}
81
-
82
- /// Represents the different kinds of tokens in C/C++/Objective-C
83
- public enum TokenKind {
84
- /// A piece of punctuation, like `{`, `;`, and `:`
85
- case punctuation
86
-
87
- /// A keyword, like `if`, `else`, and `case`
88
- case keyword
89
-
90
- /// An identifier, like a variable's name or type name
91
- case identifier
92
-
93
- /// A literal, either character, string, or number
94
- case literal
95
-
96
- /// A C comment
97
- case comment
98
-
99
- init ( clang: CXTokenKind ) {
100
- switch clang {
101
- case CXToken_Comment: self = . comment
102
- case CXToken_Literal: self = . literal
103
- case CXToken_Identifier: self = . identifier
104
- case CXToken_Keyword: self = . keyword
105
- case CXToken_Punctuation: self = . punctuation
106
- default : fatalError ( " unknown CXTokenKind \( clang) " )
107
- }
108
- }
109
-
110
- func asClang( ) -> CXTokenKind {
111
- switch self {
112
- case . comment: return CXToken_Comment
113
- case . literal: return CXToken_Literal
114
- case . identifier: return CXToken_Identifier
115
- case . keyword: return CXToken_Keyword
116
- case . punctuation: return CXToken_Punctuation
117
- }
118
- }
119
- }
0 commit comments