OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljs / parser / qmljslexer_p.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 ** GNU Lesser General Public License Usage
10 **
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 file.
14 ** Please review the following information to ensure the GNU Lesser General
15 ** 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 ** Other Usage
23 **
24 ** Alternatively, this file may be used in accordance with the terms and
25 ** conditions contained in a signed written agreement between you and Nokia.
26 **
27 ** If you have questions regarding the use of this file, please contact
28 ** Nokia at qt-info@nokia.com.
29 **
30 **************************************************************************/
31
32 #ifndef QMLJSLEXER_P_H
33 #define QMLJSLEXER_P_H
34
35 //
36 //  W A R N I N G
37 //  -------------
38 //
39 // This file is not part of the Qt API.  It exists purely as an
40 // implementation detail.  This header file may change from version to
41 // version without notice, or even be removed.
42 //
43 // We mean it.
44 //
45
46 #include "qmljsglobal_p.h"
47
48 #include <QtCore/QString>
49
50 QT_QML_BEGIN_NAMESPACE
51
52 namespace QmlJS {
53
54 class Engine;
55 class NameId;
56
57 class QML_PARSER_EXPORT Lexer
58 {
59 public:
60     Lexer(Engine *eng, bool tokenizeComments = false);
61     ~Lexer();
62
63     void setCode(const QString &c, int lineno);
64     int lex();
65
66     int currentLineNo() const { return yylineno; }
67     int currentColumnNo() const { return yycolumn; }
68
69     int tokenOffset() const { return startpos; }
70     int tokenLength() const { return pos - startpos; }
71
72     int startLineNo() const { return startlineno; }
73     int startColumnNo() const { return startcolumn; }
74
75     int endLineNo() const { return currentLineNo(); }
76     int endColumnNo() const
77     { int col = currentColumnNo(); return (col > 0) ? col - 1 : col; }
78
79     bool prevTerminator() const { return terminator; }
80
81     enum State { Start,
82                  Identifier,
83                  InIdentifier,
84                  InSingleLineComment,
85                  InMultiLineComment,
86                  InNum,
87                  InNum0,
88                  InHex,
89                  InOctal,
90                  InDecimal,
91                  InExponentIndicator,
92                  InExponent,
93                  Hex,
94                  Octal,
95                  Number,
96                  String,
97                  Eof,
98                  InString,
99                  InEscapeSequence,
100                  InHexEscape,
101                  InUnicodeEscape,
102                  Other,
103                  Bad };
104
105     enum Error {
106         NoError,
107         IllegalCharacter,
108         UnclosedStringLiteral,
109         IllegalEscapeSequence,
110         IllegalUnicodeEscapeSequence,
111         UnclosedComment,
112         IllegalExponentIndicator,
113         IllegalIdentifier
114     };
115
116     enum ParenthesesState {
117         IgnoreParentheses,
118         CountParentheses,
119         BalancedParentheses
120     };
121
122     enum RegExpBodyPrefix {
123         NoPrefix,
124         EqualPrefix
125     };
126
127     bool scanRegExp(RegExpBodyPrefix prefix = NoPrefix);
128
129     NameId *pattern;
130     int flags;
131
132     State lexerState() const
133         { return state; }
134
135     QString errorMessage() const
136         { return errmsg; }
137     void setErrorMessage(const QString &err)
138         { errmsg = err; }
139     void setErrorMessage(const char *err)
140         { setErrorMessage(QString::fromLatin1(err)); }
141
142     Error error() const
143         { return err; }
144     void clearError()
145         { err = NoError; }
146
147 private:
148     Engine *driver;
149     int yylineno;
150     bool done;
151     char *buffer8;
152     QChar *buffer16;
153     uint size8, size16;
154     uint pos8, pos16;
155     bool terminator;
156     bool restrKeyword;
157     // encountered delimiter like "'" and "}" on last run
158     bool delimited;
159     int stackToken;
160
161     State state;
162     void setDone(State s);
163     uint pos;
164     void shift(uint p);
165     int lookupKeyword(const char *);
166
167     bool isWhiteSpace() const;
168     bool isLineTerminator() const;
169     bool isHexDigit(ushort c) const;
170     bool isOctalDigit(ushort c) const;
171
172     int matchPunctuator(ushort c1, ushort c2,
173                          ushort c3, ushort c4);
174     ushort singleEscape(ushort c) const;
175     ushort convertOctal(ushort c1, ushort c2,
176                          ushort c3) const;
177 public:
178     static unsigned char convertHex(ushort c1);
179     static unsigned char convertHex(ushort c1, ushort c2);
180     static QChar convertUnicode(ushort c1, ushort c2,
181                                  ushort c3, ushort c4);
182     static bool isIdentLetter(ushort c);
183     static bool isDecimalDigit(ushort c);
184
185     inline int ival() const { return qsyylval.ival; }
186     inline double dval() const { return qsyylval.dval; }
187     inline NameId *ustr() const { return qsyylval.ustr; }
188
189     const QChar *characterBuffer() const { return buffer16; }
190     int characterCount() const { return pos16; }
191
192 private:
193     void record8(ushort c);
194     void record16(QChar c);
195     void recordStartPos();
196
197     int findReservedWord(const QChar *buffer, int size) const;
198
199     void syncProhibitAutomaticSemicolon();
200
201     const QChar *code;
202     uint length;
203     int yycolumn;
204     int startpos;
205     int startlineno;
206     int startcolumn;
207     int bol;     // begin of line
208
209     union {
210         int ival;
211         double dval;
212         NameId *ustr;
213     } qsyylval;
214
215     // current and following unicode characters
216     ushort current, next1, next2, next3;
217
218     struct keyword {
219         const char *name;
220         int token;
221     };
222
223     QString errmsg;
224     Error err;
225
226     bool wantRx;
227     bool check_reserved;
228
229     ParenthesesState parenthesesState;
230     int parenthesesCount;
231     bool prohibitAutomaticSemicolon;
232     bool tokenizeComments;
233 };
234
235 } // namespace QmlJS
236
237 QT_QML_END_NAMESPACE
238
239 #endif