| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | /**************************************************************************
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							|  |  |  | ** This file is part of Qt Creator | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-06-17 00:01:27 +10:00
										 |  |  | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Commercial Usage | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Licensees holding valid Qt Commercial licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the Qt Commercial License Agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and Nokia. | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** GNU Lesser General Public License Usage | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
					
						
							|  |  |  | ** General Public License version 2.1 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
					
						
							|  |  |  | ** packaging of this file.  Please review the following information to | 
					
						
							|  |  |  | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
					
						
							|  |  |  | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** If you are unsure which license is appropriate for your use, please | 
					
						
							| 
									
										
										
										
											2009-08-14 09:30:56 +02:00
										 |  |  | ** contact the sales department at http://qt.nokia.com/contact.
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | **************************************************************************/ | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "SimpleLexer.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-08 15:45:36 +01:00
										 |  |  | #include "ObjectiveCTypeQualifiers.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | #include <Lexer.h>
 | 
					
						
							|  |  |  | #include <Token.h>
 | 
					
						
							|  |  |  | #include <QtDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace CPlusPlus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-21 15:11:18 +02:00
										 |  |  | SimpleToken::SimpleToken(const Token &token, const QStringRef &text) | 
					
						
							|  |  |  |     : _kind(token.f.kind) | 
					
						
							|  |  |  |     , _flags(0) | 
					
						
							|  |  |  |     , _position(token.begin()) | 
					
						
							|  |  |  |     , _length(token.f.length) | 
					
						
							|  |  |  |     , _text(text) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     f._whitespace = token.f.whitespace; | 
					
						
							|  |  |  |     f._newline = token.f.newline; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | bool SimpleToken::isLiteral() const | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return _kind >= T_FIRST_LITERAL && _kind <= T_LAST_LITERAL; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool SimpleToken::isOperator() const | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return _kind >= T_FIRST_OPERATOR && _kind <= T_LAST_OPERATOR; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool SimpleToken::isKeyword() const | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return _kind >= T_FIRST_KEYWORD && _kind < T_FIRST_QT_KEYWORD; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 11:52:27 +01:00
										 |  |  | bool SimpleToken::isComment() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-29 11:34:11 +02:00
										 |  |  |     return _kind == T_COMMENT || _kind == T_DOXY_COMMENT || | 
					
						
							|  |  |  |             _kind == T_CPP_COMMENT || _kind == T_CPP_DOXY_COMMENT; | 
					
						
							| 
									
										
										
										
											2009-02-20 11:52:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-13 12:05:29 +02:00
										 |  |  | bool SimpleToken::isObjCAtKeyword() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-02-14 14:41:51 +01:00
										 |  |  |     return _kind >= T_FIRST_OBJC_KEYWORD && _kind <= T_LAST_OBJC_KEYWORD; | 
					
						
							| 
									
										
										
										
											2009-07-13 12:05:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-21 15:11:18 +02:00
										 |  |  | const char *SimpleToken::name() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Token::name(_kind); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | SimpleLexer::SimpleLexer() | 
					
						
							|  |  |  |     : _lastState(0), | 
					
						
							|  |  |  |       _skipComments(false), | 
					
						
							| 
									
										
										
										
											2009-01-08 11:35:54 +01:00
										 |  |  |       _qtMocRunEnabled(true), | 
					
						
							| 
									
										
										
										
											2009-01-09 16:55:25 +01:00
										 |  |  |       _objCEnabled(false) | 
					
						
							| 
									
										
										
										
											2009-02-04 11:36:17 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | SimpleLexer::~SimpleLexer() | 
					
						
							|  |  |  | { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool SimpleLexer::qtMocRunEnabled() const | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return _qtMocRunEnabled; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void SimpleLexer::setQtMocRunEnabled(bool enabled) | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     _qtMocRunEnabled = enabled; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-09 16:55:25 +01:00
										 |  |  | bool SimpleLexer::objCEnabled() const | 
					
						
							| 
									
										
										
										
											2009-01-08 11:35:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-09 16:55:25 +01:00
										 |  |  |     return _objCEnabled; | 
					
						
							| 
									
										
										
										
											2009-01-08 11:35:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-09 16:55:25 +01:00
										 |  |  | void SimpleLexer::setObjCEnabled(bool onoff) | 
					
						
							| 
									
										
										
										
											2009-01-08 11:35:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-09 16:55:25 +01:00
										 |  |  |     _objCEnabled = onoff; | 
					
						
							| 
									
										
										
										
											2009-01-08 11:35:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | bool SimpleLexer::skipComments() const | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return _skipComments; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void SimpleLexer::setSkipComments(bool skipComments) | 
					
						
							| 
									
										
										
										
											2008-12-02 14:09:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     _skipComments = skipComments; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | QList<SimpleToken> SimpleLexer::operator()(const QString &text, int state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QList<SimpleToken> tokens; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const QByteArray bytes = text.toLatin1(); | 
					
						
							|  |  |  |     const char *firstChar = bytes.constData(); | 
					
						
							|  |  |  |     const char *lastChar = firstChar + bytes.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Lexer lex(firstChar, lastChar); | 
					
						
							|  |  |  |     lex.setQtMocRunEnabled(_qtMocRunEnabled); | 
					
						
							| 
									
										
										
										
											2009-01-09 16:55:25 +01:00
										 |  |  |     lex.setObjCEnabled(_objCEnabled); | 
					
						
							| 
									
										
										
										
											2009-09-21 15:11:18 +02:00
										 |  |  |     lex.setStartWithNewline(true); | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (! _skipComments) | 
					
						
							|  |  |  |         lex.setScanCommentTokens(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (state != -1) | 
					
						
							|  |  |  |         lex.setState(state & 0xff); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool inPreproc = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         Token tk; | 
					
						
							|  |  |  |         lex(&tk); | 
					
						
							|  |  |  |         if (tk.is(T_EOF_SYMBOL)) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-21 15:11:18 +02:00
										 |  |  |         QStringRef spell = text.midRef(lex.tokenOffset(), lex.tokenLength()); | 
					
						
							|  |  |  |         SimpleToken simpleTk(tk, spell); | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |         lex.setScanAngleStringLiteralTokens(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-27 21:47:03 +02:00
										 |  |  |         if (tk.f.newline && tk.is(T_POUND)) | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |             inPreproc = true; | 
					
						
							|  |  |  |         else if (inPreproc && tokens.size() == 1 && simpleTk.is(T_IDENTIFIER) && | 
					
						
							|  |  |  |                  simpleTk.text() == QLatin1String("include")) | 
					
						
							|  |  |  |             lex.setScanAngleStringLiteralTokens(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Syntax highlighting for Objective-C identifiers (super, self, nil, Nil, _cmd, SEL, IMP, BOOL, YES, NO, id).
											
										 
											2009-10-09 15:23:19 +02:00
										 |  |  |         if (_objCEnabled && tk.is(T_IDENTIFIER)) | 
					
						
							| 
									
										
										
										
											2010-02-04 10:58:20 +01:00
										 |  |  |             simpleTk.f._objcTypeQualifier = (classifyObjectiveCContextKeyword(firstChar + tk.offset, tk.f.length) != Token_identifier); | 
					
						
							| 
									
										
										
											
												Syntax highlighting for Objective-C identifiers (super, self, nil, Nil, _cmd, SEL, IMP, BOOL, YES, NO, id).
											
										 
											2009-10-09 15:23:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |         tokens.append(simpleTk); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _lastState = lex.state(); | 
					
						
							|  |  |  |     return tokens; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |