174ce0a8c5c8d70da8260fda71a819a5fda18fcb
[profile/ivi/qtdeclarative.git] / src / declarative / qml / parser / qdeclarativejslexer_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 Directives {
67 public:
68     virtual ~Directives() {}
69
70     virtual void pragmaLibrary()
71     {
72     }
73
74     virtual void importFile(const QString &jsfile, const QString &module)
75     {
76         Q_UNUSED(jsfile);
77         Q_UNUSED(module);
78     }
79
80     virtual void importModule(const QString &uri, const QString &version, const QString &module)
81     {
82         Q_UNUSED(uri);
83         Q_UNUSED(version);
84         Q_UNUSED(module);
85     }
86 };
87
88 class QML_PARSER_EXPORT Lexer: public QDeclarativeJSGrammar
89 {
90 public:
91     enum {
92         T_ABSTRACT = T_RESERVED_WORD,
93         T_BOOLEAN = T_RESERVED_WORD,
94         T_BYTE = T_RESERVED_WORD,
95         T_CHAR = T_RESERVED_WORD,
96         T_CLASS = T_RESERVED_WORD,
97         T_DOUBLE = T_RESERVED_WORD,
98         T_ENUM = T_RESERVED_WORD,
99         T_EXPORT = T_RESERVED_WORD,
100         T_EXTENDS = T_RESERVED_WORD,
101         T_FINAL = T_RESERVED_WORD,
102         T_FLOAT = T_RESERVED_WORD,
103         T_GOTO = T_RESERVED_WORD,
104         T_IMPLEMENTS = T_RESERVED_WORD,
105         T_INT = T_RESERVED_WORD,
106         T_INTERFACE = T_RESERVED_WORD,
107         T_LET = T_RESERVED_WORD,
108         T_LONG = T_RESERVED_WORD,
109         T_NATIVE = T_RESERVED_WORD,
110         T_PACKAGE = T_RESERVED_WORD,
111         T_PRIVATE = T_RESERVED_WORD,
112         T_PROTECTED = T_RESERVED_WORD,
113         T_SHORT = T_RESERVED_WORD,
114         T_STATIC = T_RESERVED_WORD,
115         T_SUPER = T_RESERVED_WORD,
116         T_SYNCHRONIZED = T_RESERVED_WORD,
117         T_THROWS = T_RESERVED_WORD,
118         T_TRANSIENT = T_RESERVED_WORD,
119         T_VOLATILE = T_RESERVED_WORD,
120         T_YIELD = T_RESERVED_WORD
121     };
122
123     enum Error {
124         NoError,
125         IllegalCharacter,
126         UnclosedStringLiteral,
127         IllegalEscapeSequence,
128         IllegalUnicodeEscapeSequence,
129         UnclosedComment,
130         IllegalExponentIndicator,
131         IllegalIdentifier
132     };
133
134     enum RegExpBodyPrefix {
135         NoPrefix,
136         EqualPrefix
137     };
138
139     enum RegExpFlag {
140         RegExp_Global     = 0x01,
141         RegExp_IgnoreCase = 0x02,
142         RegExp_Multiline  = 0x04
143     };
144
145 public:
146     Lexer(Engine *engine);
147
148     bool qmlMode() const;
149
150     QString code() const;
151     void setCode(const QString &code, int lineno, bool qmlMode = true);
152
153     int lex();
154
155     bool scanRegExp(RegExpBodyPrefix prefix = NoPrefix);
156     bool scanDirectives(Directives *directives);
157
158     int regExpFlags() const { return _patternFlags; }
159     QString regExpPattern() const { return _tokenText; }
160
161     int tokenKind() const;
162     int tokenOffset() const;
163     int tokenLength() const;
164
165     int tokenStartLine() const;
166     int tokenStartColumn() const;
167
168     int tokenEndLine() const;
169     int tokenEndColumn() const;
170
171     QStringRef tokenSpell() const;
172     double tokenValue() const;
173     QString tokenText() const;
174
175     Error errorCode() const;
176     QString errorMessage() const;
177
178     bool prevTerminator() const;
179     bool followsClosingBrace() const;
180     bool canInsertAutomaticSemicolon(int token) const;
181
182     enum ParenthesesState {
183         IgnoreParentheses,
184         CountParentheses,
185         BalancedParentheses
186     };
187
188 protected:
189     int classify(const QChar *s, int n, bool qmlMode);
190
191 private:
192     inline void scanChar();
193     int scanToken();
194
195     bool isLineTerminator() const;
196     static bool isIdentLetter(QChar c);
197     static bool isDecimalDigit(ushort c);
198     static bool isHexDigit(QChar c);
199     static bool isOctalDigit(ushort c);
200     static bool isUnicodeEscapeSequence(const QChar *chars);
201
202     void syncProhibitAutomaticSemicolon();
203     QChar decodeUnicodeEscapeCharacter(bool *ok);
204
205 private:
206     Engine *_engine;
207
208     QString _code;
209     QString _tokenText;
210     QString _errorMessage;
211     QStringRef _tokenSpell;
212
213     const QChar *_codePtr;
214     const QChar *_lastLinePtr;
215     const QChar *_tokenLinePtr;
216     const QChar *_tokenStartPtr;
217
218     QChar _char;
219     Error _errorCode;
220
221     int _currentLineNumber;
222     double _tokenValue;
223
224     // parentheses state
225     ParenthesesState _parenthesesState;
226     int _parenthesesCount;
227
228     int _stackToken;
229
230     int _patternFlags;
231     int _tokenKind;
232     int _tokenLength;
233     int _tokenLine;
234
235     bool _validTokenText;
236     bool _prohibitAutomaticSemicolon;
237     bool _restrictedKeyword;
238     bool _terminator;
239     bool _followsClosingBrace;
240     bool _delimited;
241     bool _qmlMode;
242 };
243
244 } // end of namespace QDeclarativeJS
245
246 QT_QML_END_NAMESPACE
247
248 #endif // LEXER_H