Fix the implementation of Lexer::tokenText().
[profile/ivi/qtdeclarative.git] / src / declarative / qml / parser / qdeclarativejslexer_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEJSLEXER_P_H
43 #define QDECLARATIVEJSLEXER_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qdeclarativejsglobal_p.h"
57 #include "qdeclarativejsgrammar_p.h"
58 #include <QtCore/QString>
59
60 QT_QML_BEGIN_NAMESPACE
61
62 namespace QDeclarativeJS {
63
64 class Engine;
65
66 class QML_PARSER_EXPORT Lexer: public QDeclarativeJSGrammar
67 {
68 public:
69     enum {
70         T_ABSTRACT = T_RESERVED_WORD,
71         T_BOOLEAN = T_RESERVED_WORD,
72         T_BYTE = T_RESERVED_WORD,
73         T_CHAR = T_RESERVED_WORD,
74         T_CLASS = T_RESERVED_WORD,
75         T_DOUBLE = T_RESERVED_WORD,
76         T_ENUM = T_RESERVED_WORD,
77         T_EXPORT = T_RESERVED_WORD,
78         T_EXTENDS = T_RESERVED_WORD,
79         T_FINAL = T_RESERVED_WORD,
80         T_FLOAT = T_RESERVED_WORD,
81         T_GOTO = T_RESERVED_WORD,
82         T_IMPLEMENTS = T_RESERVED_WORD,
83         T_INT = T_RESERVED_WORD,
84         T_INTERFACE = T_RESERVED_WORD,
85         T_LET = T_RESERVED_WORD,
86         T_LONG = T_RESERVED_WORD,
87         T_NATIVE = T_RESERVED_WORD,
88         T_PACKAGE = T_RESERVED_WORD,
89         T_PRIVATE = T_RESERVED_WORD,
90         T_PROTECTED = T_RESERVED_WORD,
91         T_SHORT = T_RESERVED_WORD,
92         T_STATIC = T_RESERVED_WORD,
93         T_SUPER = T_RESERVED_WORD,
94         T_SYNCHRONIZED = T_RESERVED_WORD,
95         T_THROWS = T_RESERVED_WORD,
96         T_TRANSIENT = T_RESERVED_WORD,
97         T_VOLATILE = T_RESERVED_WORD,
98         T_YIELD = T_RESERVED_WORD
99     };
100
101     enum Error {
102         NoError,
103         IllegalCharacter,
104         UnclosedStringLiteral,
105         IllegalEscapeSequence,
106         IllegalUnicodeEscapeSequence,
107         UnclosedComment,
108         IllegalExponentIndicator,
109         IllegalIdentifier
110     };
111
112     enum RegExpBodyPrefix {
113         NoPrefix,
114         EqualPrefix
115     };
116
117 public:
118     Lexer(Engine *engine);
119
120     QString code() const;
121     void setCode(const QString &code, int lineno, bool qmlMode = true);
122
123     int lex();
124
125     bool scanRegExp(RegExpBodyPrefix prefix = NoPrefix);
126
127     int regExpFlags() const { return _patternFlags; }
128     QString regExpPattern() const { return _tokenText; }
129
130     int tokenOffset() const;
131     int tokenLength() const;
132
133     int tokenStartLine() const;
134     int tokenStartColumn() const;
135
136     int tokenEndLine() const;
137     int tokenEndColumn() const;
138
139     QStringRef tokenSpell() const;
140     double tokenValue() const;
141     QString tokenText() const;
142
143     Error errorCode() const;
144     QString errorMessage() const;
145
146     bool prevTerminator() const;
147
148     enum ParenthesesState {
149         IgnoreParentheses,
150         CountParentheses,
151         BalancedParentheses
152     };
153
154 private:
155     inline void scanChar();
156     int scanToken();
157
158     int classify(const QChar *s, int n, bool qmlMode);
159
160     bool isLineTerminator() const;
161     static bool isIdentLetter(QChar c);
162     static bool isDecimalDigit(ushort c);
163     static bool isHexDigit(QChar c);
164     static bool isOctalDigit(ushort c);
165     static bool isUnicodeEscapeSequence(const QChar *chars);
166
167     void syncProhibitAutomaticSemicolon();
168     QChar decodeUnicodeEscapeCharacter(bool *ok);
169
170 private:
171     Engine *_engine;
172
173     QString _code;
174     QString _tokenText;
175     QString _errorMessage;
176     QStringRef _tokenSpell;
177
178     const QChar *_codePtr;
179     const QChar *_lastLinePtr;
180     const QChar *_tokenLinePtr;
181     const QChar *_tokenStartPtr;
182
183     QChar _char;
184     Error _errorCode;
185
186     int _currentLineNumber;
187     double _tokenValue;
188
189     // parentheses state
190     ParenthesesState _parenthesesState;
191     int _parenthesesCount;
192
193     int _stackToken;
194
195     int _patternFlags;
196     int _tokenKind;
197     int _tokenLength;
198     int _tokenLine;
199
200     bool _validTokenText;
201     bool _prohibitAutomaticSemicolon;
202     bool _restrictedKeyword;
203     bool _terminator;
204     bool _delimited;
205     bool _qmlMode;
206 };
207
208 } // end of namespace QDeclarativeJS
209
210 QT_QML_END_NAMESPACE
211
212 #endif // LEXER_H