1 /** 2 * Copyright: Copyright (c) 2016 Wojciech Szęszoł. All rights reserved. 3 * Authors: Wojciech Szęszoł 4 * Version: Initial created: Feb 14, 2016 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module clang.Token; 8 9 import std.conv : to; 10 import std.typecons; 11 public import std.range.primitives : empty, front, back; 12 13 import clang.c.Index; 14 import clang.SourceLocation; 15 import clang.SourceRange; 16 import clang.Type; 17 import clang.Util; 18 import clang.Visitor; 19 import clang.Cursor; 20 21 enum TokenKind 22 { 23 punctuation = CXTokenKind.punctuation, 24 keyword = CXTokenKind.keyword, 25 identifier = CXTokenKind.identifier, 26 literal = CXTokenKind.literal, 27 comment = CXTokenKind.comment, 28 } 29 30 TokenKind toD(CXTokenKind kind) 31 { 32 return cast(TokenKind) kind; 33 } 34 35 struct Token 36 { 37 TokenKind kind; 38 string spelling; 39 SourceRange extent; 40 41 @property SourceLocation location() 42 { 43 return extent.start; 44 } 45 46 @property string toString() const 47 { 48 import std.format: format; 49 return format("Token(kind = %s, spelling = %s)", kind, spelling); 50 } 51 } 52 53 SourceRange extent(Token[] tokens) 54 { 55 if (!tokens.empty) 56 return SourceRange( 57 tokens.front.extent.start, 58 tokens.back.extent.end); 59 else 60 return SourceRange.empty; 61 }