Initial import from the monolithic Qt.
[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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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 "private/qdeclarativejsglobal_p.h"
57
58 #include <QtCore/QString>
59
60 QT_QML_BEGIN_NAMESPACE
61
62 namespace QDeclarativeJS {
63
64 class Engine;
65 class NameId;
66
67 class QML_PARSER_EXPORT Lexer
68 {
69 public:
70     Lexer(Engine *eng, bool tokenizeComments = false);
71     ~Lexer();
72
73     void setCode(const QString &c, int lineno);
74     int lex();
75
76     int currentLineNo() const { return yylineno; }
77     int currentColumnNo() const { return yycolumn; }
78
79     int tokenOffset() const { return startpos; }
80     int tokenLength() const { return pos - startpos; }
81
82     int startLineNo() const { return startlineno; }
83     int startColumnNo() const { return startcolumn; }
84
85     int endLineNo() const { return currentLineNo(); }
86     int endColumnNo() const
87     { int col = currentColumnNo(); return (col > 0) ? col - 1 : col; }
88
89     bool prevTerminator() const { return terminator; }
90
91     enum State { Start,
92                  Identifier,
93                  InIdentifier,
94                  InSingleLineComment,
95                  InMultiLineComment,
96                  InNum,
97                  InNum0,
98                  InHex,
99                  InOctal,
100                  InDecimal,
101                  InExponentIndicator,
102                  InExponent,
103                  Hex,
104                  Octal,
105                  Number,
106                  String,
107                  Eof,
108                  InString,
109                  InEscapeSequence,
110                  InHexEscape,
111                  InUnicodeEscape,
112                  Other,
113                  Bad };
114
115     enum Error {
116         NoError,
117         IllegalCharacter,
118         UnclosedStringLiteral,
119         IllegalEscapeSequence,
120         IllegalUnicodeEscapeSequence,
121         UnclosedComment,
122         IllegalExponentIndicator,
123         IllegalIdentifier
124     };
125
126     enum ParenthesesState {
127         IgnoreParentheses,
128         CountParentheses,
129         BalancedParentheses
130     };
131
132     enum RegExpBodyPrefix {
133         NoPrefix,
134         EqualPrefix
135     };
136
137     bool scanRegExp(RegExpBodyPrefix prefix = NoPrefix);
138
139     NameId *pattern;
140     int flags;
141
142     State lexerState() const
143         { return state; }
144
145     QString errorMessage() const
146         { return errmsg; }
147     void setErrorMessage(const QString &err)
148         { errmsg = err; }
149     void setErrorMessage(const char *err)
150         { setErrorMessage(QString::fromLatin1(err)); }
151
152     Error error() const
153         { return err; }
154     void clearError()
155         { err = NoError; }
156
157 private:
158     Engine *driver;
159     int yylineno;
160     bool done;
161     char *buffer8;
162     QChar *buffer16;
163     uint size8, size16;
164     uint pos8, pos16;
165     bool terminator;
166     bool restrKeyword;
167     // encountered delimiter like "'" and "}" on last run
168     bool delimited;
169     int stackToken;
170
171     State state;
172     void setDone(State s);
173     uint pos;
174     void shift(uint p);
175     int lookupKeyword(const char *);
176
177     bool isWhiteSpace() const;
178     bool isLineTerminator() const;
179     bool isHexDigit(ushort c) const;
180     bool isOctalDigit(ushort c) const;
181
182     int matchPunctuator(ushort c1, ushort c2,
183                          ushort c3, ushort c4);
184     ushort singleEscape(ushort c) const;
185     ushort convertOctal(ushort c1, ushort c2,
186                          ushort c3) const;
187 public:
188     static unsigned char convertHex(ushort c1);
189     static unsigned char convertHex(ushort c1, ushort c2);
190     static QChar convertUnicode(ushort c1, ushort c2,
191                                  ushort c3, ushort c4);
192     static bool isIdentLetter(ushort c);
193     static bool isDecimalDigit(ushort c);
194
195     inline int ival() const { return qsyylval.ival; }
196     inline double dval() const { return qsyylval.dval; }
197     inline NameId *ustr() const { return qsyylval.ustr; }
198
199     const QChar *characterBuffer() const { return buffer16; }
200     int characterCount() const { return pos16; }
201
202 private:
203     void record8(ushort c);
204     void record16(QChar c);
205     void recordStartPos();
206
207     int findReservedWord(const QChar *buffer, int size) const;
208
209     void syncProhibitAutomaticSemicolon();
210
211     const QChar *code;
212     uint length;
213     int yycolumn;
214     int startpos;
215     int startlineno;
216     int startcolumn;
217     int bol;     // begin of line
218
219     union {
220         int ival;
221         double dval;
222         NameId *ustr;
223     } qsyylval;
224
225     // current and following unicode characters
226     ushort current, next1, next2, next3;
227
228     struct keyword {
229         const char *name;
230         int token;
231     };
232
233     QString errmsg;
234     Error err;
235
236     bool wantRx;
237     bool check_reserved;
238
239     ParenthesesState parenthesesState;
240     int parenthesesCount;
241     bool prohibitAutomaticSemicolon;
242     bool tokenizeComments;
243 };
244
245 } // namespace QDeclarativeJS
246
247 QT_QML_END_NAMESPACE
248
249 #endif