Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljslexer_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLJSLEXER_P_H
6#define QQMLJSLEXER_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmljsglobal_p.h>
20#include <private/qqmljsgrammar_p.h>
21
22#include <QtCore/qstring.h>
23#include <QtCore/qstack.h>
24
25QT_BEGIN_NAMESPACE
26
27class QDebug;
28
29namespace QQmlJS {
30
31class Engine;
33class Directives;
34
35class QML_PARSER_EXPORT Lexer: public QQmlJSGrammar
36{
37public:
50
55
63
69
74
75 enum class LexMode { WholeCode, LineByLine };
76
78
79public:
81
82 bool qmlMode() const;
83 bool yieldIsKeyWord() const { return _state.generatorLevel != 0; }
85
86 QString code() const;
87 void setCode(const QString &code, int lineno, bool qmlMode = true,
89
90 int lex();
91
94
95 int regExpFlags() const { return _state.patternFlags; }
96 QString regExpPattern() const { return _tokenText; }
97
98 int tokenKind() const { return _state.tokenKind; }
100 int tokenLength() const { return _tokenLength; }
101
102 int tokenStartLine() const { return _tokenLine; }
103 int tokenStartColumn() const { return _tokenColumn; }
104
105 inline QStringView tokenSpell() const { return _tokenSpell; }
106 inline QStringView rawString() const { return _rawString; }
107 double tokenValue() const { return _state.tokenValue; }
108 QString tokenText() const;
109
110 Error errorCode() const;
111 QString errorMessage() const;
112
114
115 bool canInsertAutomaticSemicolon(int token) const;
116
122
124
127
128 struct State
129 {
131
133 double tokenValue = 0;
134
135 // parentheses state
138
139 // template string stack
141 int bracesCount = -1;
142
143 int stackToken = -1;
144
146 int tokenKind = 0;
148
149 bool validTokenText = false;
151 bool restrictedKeyword = false;
152 bool terminator = false;
154 bool delimited = true;
155 bool handlingDirectives = false;
158
159 friend bool operator==(State const &s1, State const &s2)
160 {
161 if (s1.errorCode != s2.errorCode)
162 return false;
164 return false;
165 if (s1.tokenValue != s2.tokenValue)
166 return false;
168 return false;
170 return false;
172 return false;
174 return false;
175 if (s1.stackToken != s2.stackToken)
176 return false;
178 return false;
179 if (s1.tokenKind != s2.tokenKind)
180 return false;
182 return false;
184 return false;
186 return false;
188 return false;
189 if (s1.terminator != s2.terminator)
190 return false;
192 return false;
193 if (s1.delimited != s2.delimited)
194 return false;
196 return false;
198 return false;
199 return true;
200 }
201
202 friend bool operator!=(State const &s1, State const &s2) { return !(s1 == s2); }
203
205 };
206
207 const State &state() const;
208 void setState(const State &state);
209
210protected:
211 static int classify(QStringView s, int parseModeFlags);
212
213private:
214 int parseModeFlags() const;
215 bool prevTerminator() const;
216 bool followsClosingBrace() const;
217 inline void scanChar();
218 inline QChar peekChar();
219 int scanToken();
220 int scanNumber(QChar ch);
222 enum ScanStringMode : char16_t {
223 SingleQuote = '\'',
224 DoubleQuote = '"',
225 TemplateHead = '`',
227 };
229
230 bool isLineTerminator() const;
231 unsigned isLineTerminatorSequence() const;
232 static bool isIdentLetter(QChar c);
233 static bool isDecimalDigit(ushort c);
234 static bool isHexDigit(QChar c);
235 static bool isOctalDigit(ushort c);
236
240
242
243private:
245
248 const QChar *_endPtr;
249 bool _qmlMode;
250 bool _staticIsKeyword = false;
251
252 bool _skipLinefeed = false;
253
254 int _currentLineNumber = 0;
255 int _currentColumnNumber = 0;
256 int _currentOffset = 0;
257
258 int _tokenLength = 0;
259 int _tokenLine = 0;
260 int _tokenColumn = 0;
261
266
267 const QChar *_codePtr = nullptr;
268 const QChar *_tokenStartPtr = nullptr;
269
271};
272
273} // end of namespace QQmlJS
274
275QT_END_NAMESPACE
276
277#endif // LEXER_H
static int regExpFlagFromChar(const QChar &ch)
static bool isIdentifierPart(uint ch)
static QChar convertHex(QChar c1, QChar c2)
static const int uriTokens[]
static bool isUriToken(int token)
static unsigned char convertHex(ushort c)
static bool isIdentifierStart(uint ch)