1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2015 The Qt Company Ltd. 4 ** Contact: http://www.qt.io/licensing/ 5 ** 6 ** This file is part of the QtScript module of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:LGPL$ 9 ** Commercial License Usage 10 ** Licensees holding valid commercial Qt licenses may use this file in 11 ** accordance with the commercial license agreement provided with the 12 ** Software or, alternatively, in accordance with the terms contained in 13 ** a written agreement between you and The Qt Company. For licensing terms 14 ** and conditions see https://www.qt.io/terms-conditions. For further 15 ** information use the contact form at https://www.qt.io/contact-us. 16 ** 17 ** GNU Lesser General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU Lesser 19 ** General Public License version 3 as published by the Free Software 20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 21 ** packaging of this file. Please review the following information to 22 ** ensure the GNU Lesser General Public License version 3 requirements 23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 24 ** 25 ** GNU General Public License Usage 26 ** Alternatively, this file may be used under the terms of the GNU 27 ** General Public License version 2.0 or (at your option) the GNU General 28 ** Public license version 3 or any later version approved by the KDE Free 29 ** Qt Foundation. The licenses are as published by the Free Software 30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 31 ** included in the packaging of this file. Please review the following 32 ** information to ensure the GNU General Public License requirements will 33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and 34 ** https://www.gnu.org/licenses/gpl-3.0.html. 35 ** 36 ** $QT_END_LICENSE$ 37 ** 38 ****************************************************************************/ 39 40 #ifndef QSCRIPTLEXER_P_H 41 #define QSCRIPTLEXER_P_H 42 43 // 44 // W A R N I N G 45 // ------------- 46 // 47 // This file is not part of the Qt API. It exists purely as an 48 // implementation detail. This header file may change from version to 49 // version without notice, or even be removed. 50 // 51 // We mean it. 52 // 53 54 #include <QtCore/qstring.h> 55 56 QT_BEGIN_NAMESPACE 57 58 class QScriptEnginePrivate; 59 class QScriptNameIdImpl; 60 61 namespace QScript { 62 63 class Lexer 64 { 65 public: 66 Lexer(QScriptEnginePrivate *eng); 67 ~Lexer(); 68 69 void setCode(const QString &c, int lineno); 70 int lex(); 71 currentLineNo()72 int currentLineNo() const { return yylineno; } currentColumnNo()73 int currentColumnNo() const { return yycolumn; } 74 startLineNo()75 int startLineNo() const { return startlineno; } startColumnNo()76 int startColumnNo() const { return startcolumn; } 77 endLineNo()78 int endLineNo() const { return currentLineNo(); } endColumnNo()79 int endColumnNo() const 80 { int col = currentColumnNo(); return (col > 0) ? col - 1 : col; } 81 prevTerminator()82 bool prevTerminator() const { return terminator; } 83 84 enum State { Start, 85 Identifier, 86 InIdentifier, 87 InSingleLineComment, 88 InMultiLineComment, 89 InNum, 90 InNum0, 91 InHex, 92 InOctal, 93 InDecimal, 94 InExponentIndicator, 95 InExponent, 96 Hex, 97 Octal, 98 Number, 99 String, 100 Eof, 101 InString, 102 InEscapeSequence, 103 InHexEscape, 104 InUnicodeEscape, 105 Other, 106 Bad }; 107 108 enum Error { 109 NoError, 110 IllegalCharacter, 111 UnclosedStringLiteral, 112 IllegalEscapeSequence, 113 IllegalUnicodeEscapeSequence, 114 UnclosedComment, 115 IllegalExponentIndicator, 116 IllegalIdentifier 117 }; 118 119 enum ParenthesesState { 120 IgnoreParentheses, 121 CountParentheses, 122 BalancedParentheses 123 }; 124 125 enum RegExpBodyPrefix { 126 NoPrefix, 127 EqualPrefix 128 }; 129 130 bool scanRegExp(RegExpBodyPrefix prefix = NoPrefix); 131 132 QScriptNameIdImpl *pattern; 133 int flags; 134 lexerState()135 State lexerState() const 136 { return state; } 137 errorMessage()138 QString errorMessage() const 139 { return errmsg; } setErrorMessage(const QString & err)140 void setErrorMessage(const QString &err) 141 { errmsg = err; } setErrorMessage(const char * err)142 void setErrorMessage(const char *err) 143 { setErrorMessage(QString::fromLatin1(err)); } 144 error()145 Error error() const 146 { return err; } clearError()147 void clearError() 148 { err = NoError; } 149 150 private: 151 QScriptEnginePrivate *driver; 152 int yylineno; 153 bool done; 154 char *buffer8; 155 QChar *buffer16; 156 uint size8, size16; 157 uint pos8, pos16; 158 bool terminator; 159 bool restrKeyword; 160 // encountered delimiter like "'" and "}" on last run 161 bool delimited; 162 int stackToken; 163 164 State state; 165 void setDone(State s); 166 uint pos; 167 void shift(uint p); 168 int lookupKeyword(const char *); 169 170 bool isWhiteSpace() const; 171 bool isLineTerminator() const; 172 bool isHexDigit(ushort c) const; 173 bool isOctalDigit(ushort c) const; 174 175 int matchPunctuator(ushort c1, ushort c2, 176 ushort c3, ushort c4); 177 ushort singleEscape(ushort c) const; 178 ushort convertOctal(ushort c1, ushort c2, 179 ushort c3) const; 180 public: 181 static unsigned char convertHex(ushort c1); 182 static unsigned char convertHex(ushort c1, ushort c2); 183 static QChar convertUnicode(ushort c1, ushort c2, 184 ushort c3, ushort c4); 185 static bool isIdentLetter(ushort c); 186 static bool isDecimalDigit(ushort c); 187 ival()188 inline int ival() const { return qsyylval.ival; } dval()189 inline double dval() const { return qsyylval.dval; } ustr()190 inline QScriptNameIdImpl *ustr() const { return qsyylval.ustr; } 191 characterBuffer()192 const QChar *characterBuffer() const { return buffer16; } characterCount()193 int characterCount() const { return pos16; } 194 195 private: 196 void record8(ushort c); 197 void record16(QChar c); 198 void recordStartPos(); 199 200 int findReservedWord(const QChar *buffer, int size) const; 201 202 void syncProhibitAutomaticSemicolon(); 203 204 const QChar *code; 205 uint length; 206 int yycolumn; 207 int startlineno; 208 int startcolumn; 209 int bol; // begin of line 210 211 union { 212 int ival; 213 double dval; 214 QScriptNameIdImpl *ustr; 215 } qsyylval; 216 217 // current and following unicode characters 218 ushort current, next1, next2, next3; 219 220 struct keyword { 221 const char *name; 222 int token; 223 }; 224 225 QString errmsg; 226 Error err; 227 228 bool wantRx; 229 bool check_reserved; 230 231 ParenthesesState parenthesesState; 232 int parenthesesCount; 233 bool prohibitAutomaticSemicolon; 234 }; 235 236 } // namespace QScript 237 238 QT_END_NAMESPACE 239 240 #endif 241