11#if !NO_SWIFTPM
22import cclang
33#endif
4+
45import Foundation
56
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+ }
811
9- /// Retrieves the kind of the receiver.
10- public var kind : TokenKind {
11- return TokenKind ( clang: clang_getTokenKind ( clang) )
12- }
12+ extension Token {
1313
1414 /// Determine the spelling of the given token.
1515 /// The spelling of a token is the textual representation of that token,
@@ -18,8 +18,57 @@ public struct Token {
1818 return clang_getTokenSpelling ( translationUnit. clang, clang) . asSwift ( )
1919 }
2020
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) " )
2372 }
2473}
2574
@@ -78,42 +127,3 @@ public struct SourceRange {
78127 return SourceLocation ( clang: clang_getRangeEnd ( clang) )
79128 }
80129}
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