Token.h 9.57 KB
Newer Older
con's avatar
con committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef CPLUSPLUS_TOKEN_H
#define CPLUSPLUS_TOKEN_H

#include "CPlusPlusForwardDeclarations.h"
Roberto Raggi's avatar
Roberto Raggi committed
25 26

namespace CPlusPlus {
con's avatar
con committed
27 28 29 30 31

enum Kind {
    T_EOF_SYMBOL = 0,
    T_ERROR,

32 33
    T_CPP_COMMENT,
    T_CPP_DOXY_COMMENT,
con's avatar
con committed
34
    T_COMMENT,
35
    T_DOXY_COMMENT,
con's avatar
con committed
36 37 38
    T_IDENTIFIER,

    T_FIRST_LITERAL,
39
    T_NUMERIC_LITERAL = T_FIRST_LITERAL,
40 41
    T_FIRST_CHAR_LITERAL,
    T_CHAR_LITERAL = T_FIRST_CHAR_LITERAL,
con's avatar
con committed
42
    T_WIDE_CHAR_LITERAL,
43 44 45
    T_UTF16_CHAR_LITERAL,
    T_UTF32_CHAR_LITERAL,
    T_LAST_CHAR_LITERAL = T_UTF32_CHAR_LITERAL,
46 47
    T_FIRST_STRING_LITERAL,
    T_STRING_LITERAL = T_FIRST_STRING_LITERAL,
con's avatar
con committed
48
    T_WIDE_STRING_LITERAL,
49 50 51
    T_UTF8_STRING_LITERAL,
    T_UTF16_STRING_LITERAL,
    T_UTF32_STRING_LITERAL,
52 53 54 55 56
    T_RAW_STRING_LITERAL,
    T_RAW_WIDE_STRING_LITERAL,
    T_RAW_UTF8_STRING_LITERAL,
    T_RAW_UTF16_STRING_LITERAL,
    T_RAW_UTF32_STRING_LITERAL,
57
    T_AT_STRING_LITERAL,
con's avatar
con committed
58
    T_ANGLE_STRING_LITERAL,
59
    T_LAST_STRING_LITERAL = T_ANGLE_STRING_LITERAL,
con's avatar
con committed
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
    T_LAST_LITERAL = T_ANGLE_STRING_LITERAL,

    T_FIRST_OPERATOR,
    T_AMPER = T_FIRST_OPERATOR,
    T_AMPER_AMPER,
    T_AMPER_EQUAL,
    T_ARROW,
    T_ARROW_STAR,
    T_CARET,
    T_CARET_EQUAL,
    T_COLON,
    T_COLON_COLON,
    T_COMMA,
    T_SLASH,
    T_SLASH_EQUAL,
    T_DOT,
    T_DOT_DOT_DOT,
    T_DOT_STAR,
    T_EQUAL,
    T_EQUAL_EQUAL,
    T_EXCLAIM,
    T_EXCLAIM_EQUAL,
    T_GREATER,
    T_GREATER_EQUAL,
    T_GREATER_GREATER,
    T_GREATER_GREATER_EQUAL,
    T_LBRACE,
    T_LBRACKET,
    T_LESS,
    T_LESS_EQUAL,
    T_LESS_LESS,
    T_LESS_LESS_EQUAL,
    T_LPAREN,
    T_MINUS,
    T_MINUS_EQUAL,
    T_MINUS_MINUS,
    T_PERCENT,
    T_PERCENT_EQUAL,
    T_PIPE,
    T_PIPE_EQUAL,
    T_PIPE_PIPE,
    T_PLUS,
    T_PLUS_EQUAL,
    T_PLUS_PLUS,
    T_POUND,
    T_POUND_POUND,
    T_QUESTION,
    T_RBRACE,
    T_RBRACKET,
    T_RPAREN,
    T_SEMICOLON,
    T_STAR,
    T_STAR_EQUAL,
    T_TILDE,
    T_TILDE_EQUAL,
    T_LAST_OPERATOR = T_TILDE_EQUAL,

    T_FIRST_KEYWORD,
118 119 120
    T_ALIGNAS = T_FIRST_KEYWORD,
    T_ALIGNOF,
    T_ASM,
con's avatar
con committed
121 122 123 124 125 126
    T_AUTO,
    T_BOOL,
    T_BREAK,
    T_CASE,
    T_CATCH,
    T_CHAR,
127 128
    T_CHAR16_T,
    T_CHAR32_T,
con's avatar
con committed
129 130 131
    T_CLASS,
    T_CONST,
    T_CONST_CAST,
Leandro Melo's avatar
Leandro Melo committed
132
    T_CONSTEXPR,
con's avatar
con committed
133
    T_CONTINUE,
134
    T_DECLTYPE,
con's avatar
con committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    T_DEFAULT,
    T_DELETE,
    T_DO,
    T_DOUBLE,
    T_DYNAMIC_CAST,
    T_ELSE,
    T_ENUM,
    T_EXPLICIT,
    T_EXPORT,
    T_EXTERN,
    T_FALSE,
    T_FLOAT,
    T_FOR,
    T_FRIEND,
    T_GOTO,
    T_IF,
    T_INLINE,
    T_INT,
    T_LONG,
    T_MUTABLE,
    T_NAMESPACE,
    T_NEW,
157
    T_NOEXCEPT,
Leandro Melo's avatar
Leandro Melo committed
158
    T_NULLPTR,
con's avatar
con committed
159 160 161 162 163 164 165 166 167 168 169
    T_OPERATOR,
    T_PRIVATE,
    T_PROTECTED,
    T_PUBLIC,
    T_REGISTER,
    T_REINTERPRET_CAST,
    T_RETURN,
    T_SHORT,
    T_SIGNED,
    T_SIZEOF,
    T_STATIC,
170
    T_STATIC_ASSERT,
con's avatar
con committed
171 172 173 174 175
    T_STATIC_CAST,
    T_STRUCT,
    T_SWITCH,
    T_TEMPLATE,
    T_THIS,
176
    T_THREAD_LOCAL,
con's avatar
con committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    T_THROW,
    T_TRUE,
    T_TRY,
    T_TYPEDEF,
    T_TYPEID,
    T_TYPENAME,
    T_UNION,
    T_UNSIGNED,
    T_USING,
    T_VIRTUAL,
    T_VOID,
    T_VOLATILE,
    T_WCHAR_T,
    T_WHILE,

    T___ATTRIBUTE__,
193
    T___THREAD,
con's avatar
con committed
194 195
    T___TYPEOF__,

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    // obj c++ @ keywords
    T_FIRST_OBJC_AT_KEYWORD,

    T_AT_CATCH = T_FIRST_OBJC_AT_KEYWORD,
    T_AT_CLASS,
    T_AT_COMPATIBILITY_ALIAS,
    T_AT_DEFS,
    T_AT_DYNAMIC,
    T_AT_ENCODE,
    T_AT_END,
    T_AT_FINALLY,
    T_AT_IMPLEMENTATION,
    T_AT_INTERFACE,
    T_AT_NOT_KEYWORD,
    T_AT_OPTIONAL,
    T_AT_PACKAGE,
    T_AT_PRIVATE,
    T_AT_PROPERTY,
    T_AT_PROTECTED,
    T_AT_PROTOCOL,
    T_AT_PUBLIC,
    T_AT_REQUIRED,
    T_AT_SELECTOR,
    T_AT_SYNCHRONIZED,
    T_AT_SYNTHESIZE,
    T_AT_THROW,
    T_AT_TRY,

    T_LAST_OBJC_AT_KEYWORD = T_AT_TRY,
Roberto Raggi's avatar
Roberto Raggi committed
225

226
    T_FIRST_QT_KEYWORD,
con's avatar
con committed
227 228

    // Qt keywords
Leandro Melo's avatar
Leandro Melo committed
229 230
    T_EMIT = T_FIRST_QT_KEYWORD,
    T_SIGNAL,
con's avatar
con committed
231
    T_SLOT,
232 233
    T_Q_SIGNAL,
    T_Q_SLOT,
234 235 236
    T_Q_SIGNALS,
    T_Q_SLOTS,
    T_Q_FOREACH,
237 238
    T_Q_D,
    T_Q_Q,
239 240
    T_Q_INVOKABLE,
    T_Q_PROPERTY,
241
    T_Q_PRIVATE_PROPERTY,
242
    T_Q_INTERFACES,
Leandro Melo's avatar
Leandro Melo committed
243
    T_Q_EMIT,
244 245
    T_Q_ENUMS,
    T_Q_FLAGS,
246 247 248 249 250
    T_Q_PRIVATE_SLOT,
    T_Q_DECLARE_INTERFACE,
    T_Q_OBJECT,
    T_Q_GADGET,
    T_LAST_KEYWORD = T_Q_GADGET,
251

252 253 254 255 256 257 258 259 260 261 262 263 264
    // aliases
    T_OR = T_PIPE_PIPE,
    T_AND = T_AMPER_AMPER,
    T_NOT = T_EXCLAIM,
    T_XOR = T_CARET,
    T_BITOR = T_PIPE,
    T_COMPL = T_TILDE,
    T_OR_EQ = T_PIPE_EQUAL,
    T_AND_EQ = T_AMPER_EQUAL,
    T_BITAND = T_AMPER,
    T_NOT_EQ = T_EXCLAIM_EQUAL,
    T_XOR_EQ = T_CARET_EQUAL,

con's avatar
con committed
265 266 267 268 269 270
    T___ASM = T_ASM,
    T___ASM__ = T_ASM,

    T_TYPEOF = T___TYPEOF__,
    T___TYPEOF = T___TYPEOF__,

271 272
    T___DECLTYPE = T_DECLTYPE,

con's avatar
con committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    T___INLINE = T_INLINE,
    T___INLINE__ = T_INLINE,

    T___CONST = T_CONST,
    T___CONST__ = T_CONST,

    T___VOLATILE = T_VOLATILE,
    T___VOLATILE__ = T_VOLATILE,

    T___ATTRIBUTE = T___ATTRIBUTE__
};

class CPLUSPLUS_EXPORT Token
{
public:
288
    Token() : flags(0), byteOffset(0), utf16charOffset(0), ptr(0) {}
con's avatar
con committed
289

290 291
    inline bool is(unsigned k) const    { return f.kind == k; }
    inline bool isNot(unsigned k) const { return f.kind != k; }
con's avatar
con committed
292 293 294
    const char *spell() const;
    void reset();

Roberto Raggi's avatar
Roberto Raggi committed
295 296 297 298 299 300 301
    inline unsigned kind() const { return f.kind; }
    inline bool newline() const { return f.newline; }
    inline bool whitespace() const { return f.whitespace; }
    inline bool joined() const { return f.joined; }
    inline bool expanded() const { return f.expanded; }
    inline bool generated() const { return f.generated; }

302 303 304
    inline unsigned bytes() const { return f.bytes; }
    inline unsigned bytesBegin() const { return byteOffset; }
    inline unsigned bytesEnd() const { return byteOffset + f.bytes; }
con's avatar
con committed
305

306 307 308
    inline unsigned utf16chars() const { return f.utf16chars; }
    inline unsigned utf16charsBegin() const { return utf16charOffset; }
    inline unsigned utf16charsEnd() const { return utf16charOffset + f.utf16chars; }
con's avatar
con committed
309 310

    inline bool isLiteral() const
311
    { return f.kind >= T_FIRST_LITERAL && f.kind <= T_LAST_LITERAL; }
con's avatar
con committed
312

313 314 315 316 317 318
    inline bool isCharLiteral() const
    { return f.kind >= T_FIRST_CHAR_LITERAL && f.kind <= T_LAST_CHAR_LITERAL; }

    inline bool isStringLiteral() const
    { return f.kind >= T_FIRST_STRING_LITERAL && f.kind <= T_LAST_STRING_LITERAL; }

con's avatar
con committed
319
    inline bool isOperator() const
320
    { return f.kind >= T_FIRST_OPERATOR && f.kind <= T_LAST_OPERATOR; }
con's avatar
con committed
321 322

    inline bool isKeyword() const
323
    { return f.kind >= T_FIRST_KEYWORD && f.kind < T_FIRST_QT_KEYWORD; }
con's avatar
con committed
324

325
    inline bool isComment() const
326 327
    { return f.kind == T_COMMENT || f.kind == T_DOXY_COMMENT ||
      f.kind == T_CPP_COMMENT || f.kind == T_CPP_DOXY_COMMENT; }
328

329 330 331
    inline bool isObjCAtKeyword() const
    { return f.kind >= T_FIRST_OBJC_AT_KEYWORD && f.kind <= T_LAST_OBJC_AT_KEYWORD; }

con's avatar
con committed
332 333 334
    static const char *name(int kind);

public:
335
    struct Flags {
336
        // The token kind.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
337
        unsigned kind          : 8;
338
        // The token starts a new line.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
339 340 341
        unsigned newline       : 1;
        // The token is preceeded by whitespace(s).
        unsigned whitespace    : 1;
342
        // The token is joined with the previous one.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
343
        unsigned joined        : 1;
344
        // The token originates from a macro expansion.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
345
        unsigned expanded      : 1;
346 347 348 349 350 351 352 353 354
        // The token originates from a macro expansion and does not correspond to an
        // argument that went through substitution. Notice the example:
        //
        // #define FOO(a, b) a + b;
        // FOO(1, 2)
        //
        // After preprocessing we would expect the following tokens: 1 + 2;
        // Tokens '1', '+', '2', and ';' are all expanded. However only tokens '+' and ';'
        // are generated.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
355
        unsigned generated     : 1;
356
        // Unused...
Nikolai Kosjar's avatar
Nikolai Kosjar committed
357
        unsigned pad           : 3;
358
        // The token length in bytes and UTF16 chars.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
359
        unsigned bytes         : 16;
360
        unsigned utf16chars    : 16;
361
    };
con's avatar
con committed
362
    union {
363
        unsigned long flags;
364
        Flags f;
con's avatar
con committed
365 366
    };

Nikolai Kosjar's avatar
Nikolai Kosjar committed
367
    unsigned byteOffset;
368
    unsigned utf16charOffset;
con's avatar
con committed
369 370 371

    union {
        void *ptr;
Roberto Raggi's avatar
Roberto Raggi committed
372 373 374 375
        const Literal *literal;
        const NumericLiteral *number;
        const StringLiteral *string;
        const Identifier *identifier;
con's avatar
con committed
376 377 378 379 380
        unsigned close_brace;
        unsigned lineno;
    };
};

381 382 383 384 385 386 387 388 389 390 391 392
struct LanguageFeatures
{
    LanguageFeatures() : flags(0) {}

    union {
        unsigned int flags;
        struct {
            unsigned int qtEnabled : 1; // If Qt is used.
            unsigned int qtMocRunEnabled : 1;
            unsigned int qtKeywordsEnabled : 1; // If Qt is used but QT_NO_KEYWORDS defined
            unsigned int cxx11Enabled : 1;
            unsigned int objCEnabled : 1;
393
            unsigned int c99Enabled : 1;
394 395 396 397
        };
    };
};

398
} // namespace CPlusPlus
Roberto Raggi's avatar
Roberto Raggi committed
399

con's avatar
con committed
400
#endif // CPLUSPLUS_TOKEN_H