1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qbs.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
41 //
42 // Permission is hereby granted, free of charge, to any person obtaining a copy
43 // of this software and associated documentation files (the "Software"), to deal
44 // in the Software without restriction, including without limitation the rights
45 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
46 // copies of the Software, and to permit persons to whom the Software is
47 // furnished to do so, subject to the following conditions:
48 //
49 // The above copyright notice and this permission notice shall be included in
50 // all copies or substantial portions of the Software.
51 //
52 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
53 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
54 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
55 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
56 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
57 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
58 // THE SOFTWARE.
59 
60 #ifndef CPLUSPLUS_TOKEN_H
61 #define CPLUSPLUS_TOKEN_H
62 
63 #include "CPlusPlusForwardDeclarations.h"
64 
65 namespace CPlusPlus {
66 
67 enum Kind {
68     T_EOF_SYMBOL = 0,
69     T_ERROR,
70 
71     T_CPP_COMMENT,
72     T_CPP_DOXY_COMMENT,
73     T_COMMENT,
74     T_DOXY_COMMENT,
75     T_IDENTIFIER,
76 
77     T_FIRST_LITERAL,
78     T_NUMERIC_LITERAL = T_FIRST_LITERAL,
79     T_CHAR_LITERAL,
80     T_WIDE_CHAR_LITERAL,
81     T_STRING_LITERAL,
82     T_WIDE_STRING_LITERAL,
83     T_AT_STRING_LITERAL,
84     T_ANGLE_STRING_LITERAL,
85     T_LAST_LITERAL = T_ANGLE_STRING_LITERAL,
86 
87     T_FIRST_OPERATOR,
88     T_AMPER = T_FIRST_OPERATOR,
89     T_AMPER_AMPER,
90     T_AMPER_EQUAL,
91     T_ARROW,
92     T_ARROW_STAR,
93     T_CARET,
94     T_CARET_EQUAL,
95     T_COLON,
96     T_COLON_COLON,
97     T_COMMA,
98     T_SLASH,
99     T_SLASH_EQUAL,
100     T_DOT,
101     T_DOT_DOT_DOT,
102     T_DOT_STAR,
103     T_EQUAL,
104     T_EQUAL_EQUAL,
105     T_EXCLAIM,
106     T_EXCLAIM_EQUAL,
107     T_GREATER,
108     T_GREATER_EQUAL,
109     T_GREATER_GREATER,
110     T_GREATER_GREATER_EQUAL,
111     T_LBRACE,
112     T_LBRACKET,
113     T_LESS,
114     T_LESS_EQUAL,
115     T_LESS_LESS,
116     T_LESS_LESS_EQUAL,
117     T_LPAREN,
118     T_MINUS,
119     T_MINUS_EQUAL,
120     T_MINUS_MINUS,
121     T_PERCENT,
122     T_PERCENT_EQUAL,
123     T_PIPE,
124     T_PIPE_EQUAL,
125     T_PIPE_PIPE,
126     T_PLUS,
127     T_PLUS_EQUAL,
128     T_PLUS_PLUS,
129     T_POUND,
130     T_POUND_POUND,
131     T_QUESTION,
132     T_RBRACE,
133     T_RBRACKET,
134     T_RPAREN,
135     T_SEMICOLON,
136     T_STAR,
137     T_STAR_EQUAL,
138     T_TILDE,
139     T_TILDE_EQUAL,
140     T_LAST_OPERATOR = T_TILDE_EQUAL,
141 
142     T_FIRST_KEYWORD,
143     T_ASM = T_FIRST_KEYWORD,
144     T_AUTO,
145     T_BOOL,
146     T_BREAK,
147     T_CASE,
148     T_CATCH,
149     T_CHAR,
150     T_CLASS,
151     T_CONST,
152     T_CONST_CAST,
153     T_CONTINUE,
154     T_DEFAULT,
155     T_DELETE,
156     T_DO,
157     T_DOUBLE,
158     T_DYNAMIC_CAST,
159     T_ELSE,
160     T_ENUM,
161     T_EXPLICIT,
162     T_EXPORT,
163     T_EXTERN,
164     T_FALSE,
165     T_FLOAT,
166     T_FOR,
167     T_FRIEND,
168     T_GOTO,
169     T_IF,
170     T_INLINE,
171     T_INT,
172     T_LONG,
173     T_MUTABLE,
174     T_NAMESPACE,
175     T_NEW,
176     T_OPERATOR,
177     T_PRIVATE,
178     T_PROTECTED,
179     T_PUBLIC,
180     T_REGISTER,
181     T_REINTERPRET_CAST,
182     T_RETURN,
183     T_SHORT,
184     T_SIGNED,
185     T_SIZEOF,
186     T_STATIC,
187     T_STATIC_CAST,
188     T_STRUCT,
189     T_SWITCH,
190     T_TEMPLATE,
191     T_THIS,
192     T_THROW,
193     T_TRUE,
194     T_TRY,
195     T_TYPEDEF,
196     T_TYPEID,
197     T_TYPENAME,
198     T_UNION,
199     T_UNSIGNED,
200     T_USING,
201     T_VIRTUAL,
202     T_VOID,
203     T_VOLATILE,
204     T_WCHAR_T,
205     T_WHILE,
206 
207     T___ATTRIBUTE__,
208     T___TYPEOF__,
209 
210     // obj c++ @ keywords
211     T_FIRST_OBJC_AT_KEYWORD,
212 
213     T_AT_CATCH = T_FIRST_OBJC_AT_KEYWORD,
214     T_AT_CLASS,
215     T_AT_COMPATIBILITY_ALIAS,
216     T_AT_DEFS,
217     T_AT_DYNAMIC,
218     T_AT_ENCODE,
219     T_AT_END,
220     T_AT_FINALLY,
221     T_AT_IMPLEMENTATION,
222     T_AT_INTERFACE,
223     T_AT_NOT_KEYWORD,
224     T_AT_OPTIONAL,
225     T_AT_PACKAGE,
226     T_AT_PRIVATE,
227     T_AT_PROPERTY,
228     T_AT_PROTECTED,
229     T_AT_PROTOCOL,
230     T_AT_PUBLIC,
231     T_AT_REQUIRED,
232     T_AT_SELECTOR,
233     T_AT_SYNCHRONIZED,
234     T_AT_SYNTHESIZE,
235     T_AT_THROW,
236     T_AT_TRY,
237 
238     T_LAST_OBJC_AT_KEYWORD = T_AT_TRY,
239 
240     T_FIRST_QT_KEYWORD,
241 
242     // Qt keywords
243     T_SIGNAL = T_FIRST_QT_KEYWORD,
244     T_SLOT,
245     T_Q_SIGNAL,
246     T_Q_SLOT,
247     T_Q_SIGNALS,
248     T_Q_SLOTS,
249     T_Q_FOREACH,
250     T_Q_D,
251     T_Q_Q,
252     T_Q_INVOKABLE,
253     T_Q_PROPERTY,
254     T_Q_INTERFACES,
255     T_Q_ENUMS,
256     T_Q_FLAGS,
257     T_Q_PRIVATE_SLOT,
258     T_Q_DECLARE_INTERFACE,
259     T_Q_OBJECT,
260     T_Q_GADGET,
261     T_Q_NAMESPACE,
262     T_LAST_KEYWORD = T_Q_NAMESPACE,
263 
264     // aliases
265     T_OR = T_PIPE_PIPE,
266     T_AND = T_AMPER_AMPER,
267     T_NOT = T_EXCLAIM,
268     T_XOR = T_CARET,
269     T_BITOR = T_PIPE,
270     T_COMPL = T_TILDE,
271     T_OR_EQ = T_PIPE_EQUAL,
272     T_AND_EQ = T_AMPER_EQUAL,
273     T_BITAND = T_AMPER,
274     T_NOT_EQ = T_EXCLAIM_EQUAL,
275     T_XOR_EQ = T_CARET_EQUAL,
276 
277     T___ASM = T_ASM,
278     T___ASM__ = T_ASM,
279 
280     T_TYPEOF = T___TYPEOF__,
281     T___TYPEOF = T___TYPEOF__,
282 
283     T___INLINE = T_INLINE,
284     T___INLINE__ = T_INLINE,
285 
286     T___CONST = T_CONST,
287     T___CONST__ = T_CONST,
288 
289     T___VOLATILE = T_VOLATILE,
290     T___VOLATILE__ = T_VOLATILE,
291 
292     T___ATTRIBUTE = T___ATTRIBUTE__
293 };
294 
295 class CPLUSPLUS_EXPORT Token
296 {
297 public:
is(unsigned k)298     inline bool is(unsigned k) const    { return f.kind == k; }
isNot(unsigned k)299     inline bool isNot(unsigned k) const { return f.kind != k; }
300 #ifndef CPLUSPLUS_NO_PARSER
301     const char *spell() const;
302 #endif
303     void reset();
304 
kind()305     inline unsigned kind() const { return f.kind; }
newline()306     inline bool newline() const { return f.newline; }
whitespace()307     inline bool whitespace() const { return f.whitespace; }
joined()308     inline bool joined() const { return f.joined; }
expanded()309     inline bool expanded() const { return f.expanded; }
generated()310     inline bool generated() const { return f.generated; }
length()311     inline unsigned length() const { return f.length; }
312 
begin()313     inline unsigned begin() const
314     { return offset; }
315 
end()316     inline unsigned end() const
317     { return offset + f.length; }
318 
isLiteral()319     inline bool isLiteral() const
320     { return f.kind >= T_FIRST_LITERAL && f.kind <= T_LAST_LITERAL; }
321 
isOperator()322     inline bool isOperator() const
323     { return f.kind >= T_FIRST_OPERATOR && f.kind <= T_LAST_OPERATOR; }
324 
isKeyword()325     inline bool isKeyword() const
326     { return f.kind >= T_FIRST_KEYWORD && f.kind < T_FIRST_QT_KEYWORD; }
327 
isComment()328     inline bool isComment() const
329     { return f.kind == T_COMMENT || f.kind == T_DOXY_COMMENT ||
330       f.kind == T_CPP_COMMENT || f.kind == T_CPP_DOXY_COMMENT; }
331 
isObjCAtKeyword()332     inline bool isObjCAtKeyword() const
333     { return f.kind >= T_FIRST_OBJC_AT_KEYWORD && f.kind <= T_LAST_OBJC_AT_KEYWORD; }
334 
335     static const char *name(int kind);
336 
337 public:
338     struct Flags {
339         unsigned kind       : 8;
340         unsigned newline    : 1;
341         unsigned whitespace : 1;
342         unsigned joined     : 1;
343         unsigned expanded   : 1;
344         unsigned generated  : 1;
345         unsigned pad        : 3;
346         unsigned length     : 16;
347     };
348     Flags f{};
349 
350     unsigned offset = 0;
351 
352     union {
353         void *ptr = nullptr;
354 #ifndef CPLUSPLUS_NO_PARSER
355         const Literal *literal;
356         const NumericLiteral *number;
357         const StringLiteral *string;
358         const Identifier *identifier;
359 #endif
360         unsigned close_brace;
361         unsigned lineno;
362     };
363 };
364 
365 } // end of namespace CPlusPlus
366 
367 
368 #endif // CPLUSPLUS_TOKEN_H
369