1
/****************************************************************************
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
4
** Copyright (C) 2001-2004 Roberto Raggi
6
** This file is part of the porting application of the Qt Toolkit.
8
** This file may be distributed under the terms of the Q Public License
9
** as defined by Trolltech AS of Norway and appearing in the file
10
** LICENSE.QPL included in the packaging of this file.
12
** This file may be distributed and/or modified under the terms of the
13
** GNU General Public License version 2 as published by the Free Software
14
** Foundation and appearing in the file LICENSE.GPL included in the
15
** packaging of this file.
17
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
18
** information about Qt Commercial License Agreements.
19
** See http://www.trolltech.com/qpl/ for QPL licensing information.
20
** See http://www.trolltech.com/gpl/ for GPL licensing information.
22
** Contact info@trolltech.com if any conditions of this licensing are
25
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28
****************************************************************************/
35
using namespace TokenEngine;
43
void RppLexer::setupScanTable()
45
memset(s_attr_table, 0, 256);
47
for (int i=0; i<128; ++i) {
62
s_scan_table[i] = &RppLexer::scanOperator;
67
s_scan_table[i] = &RppLexer::scanNewline;
71
s_scan_table[i] = &RppLexer::scanCharLiteral;
75
s_scan_table[i] = &RppLexer::scanStringLiteral;
78
s_scan_table[i] = &RppLexer::scanPreprocessor;
82
s_scan_table[i] = &RppLexer::scanComment;
87
s_scan_table[i] = &RppLexer::scanWhiteSpaces;
88
s_attr_table[i] |= A_Whitespace;
89
} else if (isalpha(i) || i == '_') {
90
s_scan_table[i] = &RppLexer::scanKeyword;
91
s_attr_table[i] |= A_Alpha;
92
} else if (isdigit(i)) {
93
s_scan_table[i] = &RppLexer::scanNumberLiteral;
94
s_attr_table[i] |= A_Digit;
96
s_scan_table[i] = &RppLexer::scanChar;
100
s_scan_table[128] = &RppLexer::scanUnicodeChar;
103
QVector<Type> RppLexer::lex(const TokenContainer &tokenContainer)
105
QVector<Type> tokenTypes;
106
const int numTokens = tokenContainer.count();
107
tokenTypes.reserve(numTokens);
108
QByteArray text = tokenContainer.fullText();
109
m_buffer = text.constData();
110
for(int t=0; t<numTokens; ++t) {
111
TokenEngine::Token token = tokenContainer.token(t);
112
tokenTypes.append(indentify(token.start, token.length));
117
Type RppLexer::indentify(int pos, int length)
119
Q_ASSERT(length > 0);
123
const unsigned char ch = m_buffer[pos];
124
(this->*s_scan_table[ch < 128 ? ch : 128])(&kind);
128
void RppLexer::scanChar(int *kind)
130
*kind = m_buffer[m_ptr];
133
void RppLexer::scanWhiteSpaces(int *kind)
135
*kind = Token_whitespaces;
137
while (unsigned char ch = m_buffer[m_ptr]) {
138
if (s_attr_table[ch] & A_Whitespace)
145
void RppLexer::scanNewline(int *kind)
150
void RppLexer::scanUnicodeChar(int *kind)
152
*kind = m_buffer[m_ptr];
155
void RppLexer::scanCharLiteral(int *kind)
157
*kind = Token_char_literal;
160
void RppLexer::scanStringLiteral(int *kind)
162
*kind = Token_string_literal;
165
void RppLexer::scanIdentifier(int *kind)
167
*kind = Token_identifier;
170
void RppLexer::scanNumberLiteral(int *kind)
172
*kind = Token_number_literal;
175
void RppLexer::scanPreprocessor(int *kind)
177
*kind = Token_preproc;
180
void RppLexer::scanComment(int *kind)
182
switch(m_buffer[m_ptr + 1]) {
184
*kind = Token_line_comment;
187
*kind = Token_multiline_comment;
194
void RppLexer::scanOperator(int *kind)
196
switch (m_buffer[m_ptr]) {
198
if (m_buffer[m_ptr+1] == ':') {
208
if (m_buffer[m_ptr+1] == '=') {
209
*kind = Token_assign;
215
if (m_buffer[m_ptr+1] == '=') {
221
if (m_buffer[m_ptr+1] == '=') {
222
*kind = Token_not_eq;
228
if (m_buffer[m_ptr+1] == '&') {
231
} else if (m_buffer[m_ptr+1] == '=') {
232
*kind = Token_assign;
238
if (m_buffer[m_ptr+1] == '|' ) {
241
} else if (m_buffer[m_ptr+1] == '=') {
242
*kind = Token_assign;
248
if (m_buffer[m_ptr+1] == '+' ) {
251
} else if (m_buffer[m_ptr+1] == '=') {
252
*kind = Token_assign;
258
if (m_buffer[m_ptr+1] == '<') {
259
if (m_buffer[m_ptr+2] == '=') {
260
*kind = Token_assign;
263
*kind = Token_left_shift;
265
} else if (m_buffer[m_ptr+1] == '=') {
272
if (m_buffer[m_ptr+1] == '>') {
273
if (m_buffer[m_ptr+2] == '=') {
274
*kind = Token_assign;
277
*kind = Token_right_shift;
279
} else if (m_buffer[m_ptr+1] == '=') {
286
if (m_buffer[m_ptr+1] == '>') {
287
if (m_buffer[m_ptr+2] == '*') {
288
*kind = Token_ptrmem;
293
} else if (m_buffer[m_ptr+1] == '-') {
296
} else if (m_buffer[m_ptr+1] == '=') {
297
*kind = Token_assign;
303
if (m_buffer[m_ptr+1] == '.' && m_buffer[m_ptr+2] == '.') {
304
*kind = Token_ellipsis;
306
} else if (m_buffer[m_ptr+1] == '*') {
307
*kind = Token_ptrmem;
314
*kind = m_buffer[m_ptr++];
317
bool RppLexer::match(char *buf, int len)
321
for (int i = 0; i < len; ++i) {
322
if(m_buffer[m_ptr + i] != buf[i])
328
void RppLexer::scanKeyword(int *kind)
331
*kind = Token_directive_if;
332
else if(match("elif", 4))
333
*kind = Token_directive_elif;
334
else if(match("else", 4))
335
*kind = Token_directive_else;
336
else if(match("line", 4))
337
*kind = Token_directive_line;
338
else if(match("else", 4))
339
*kind = Token_directive_else;
340
else if(match("line", 4))
341
*kind = Token_directive_line;
342
else if(match("endif", 5))
343
*kind = Token_directive_endif;
344
else if(match("ifdef", 5))
345
*kind = Token_directive_ifdef;
346
else if(match("error", 5))
347
*kind = Token_directive_error;
348
else if(match("undef", 5))
349
*kind = Token_directive_undef;
350
else if(match("pragma", 6))
351
*kind = Token_directive_pragma;
352
else if(match("ifndef", 6))
353
*kind = Token_directive_ifndef;
354
else if(match("define", 6))
355
*kind = Token_directive_define;
356
else if(match("include", 7))
357
*kind = Token_directive_include;
358
else if(match("defined", 7))
359
*kind = Token_defined;
361
*kind = Token_identifier;