Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QQMLJSLEXER_P_H
5#define QQMLJSLEXER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <private/qqmljsglobal_p.h>
19#include <private/qqmljsgrammar_p.h>
20
21#include <QtCore/qstring.h>
22#include <QtCore/qstack.h>
23
25
26class QDebug;
27
28namespace QQmlJS {
29
30class Engine;
31struct DiagnosticMessage;
32class Directives;
33
34class QML_PARSER_EXPORT Lexer: public QQmlJSGrammar
35{
36public:
37 enum {
38 T_ABSTRACT = T_RESERVED_WORD,
39 T_BOOLEAN = T_RESERVED_WORD,
40 T_BYTE = T_RESERVED_WORD,
41 T_CHAR = T_RESERVED_WORD,
42 T_DOUBLE = T_RESERVED_WORD,
43 T_FINAL = T_RESERVED_WORD,
44 T_FLOAT = T_RESERVED_WORD,
45 T_GOTO = T_RESERVED_WORD,
46 T_IMPLEMENTS = T_RESERVED_WORD,
47 T_INT = T_RESERVED_WORD,
48 T_INTERFACE = T_RESERVED_WORD,
49 T_LONG = T_RESERVED_WORD,
50 T_NATIVE = T_RESERVED_WORD,
51 T_PACKAGE = T_RESERVED_WORD,
52 T_PRIVATE = T_RESERVED_WORD,
53 T_PROTECTED = T_RESERVED_WORD,
54 T_SHORT = T_RESERVED_WORD,
55 T_SYNCHRONIZED = T_RESERVED_WORD,
56 T_THROWS = T_RESERVED_WORD,
57 T_TRANSIENT = T_RESERVED_WORD,
58 T_VOLATILE = T_RESERVED_WORD
59 };
60
73
76 EqualPrefix
77 };
78
80 RegExp_Global = 0x01,
81 RegExp_IgnoreCase = 0x02,
82 RegExp_Multiline = 0x04,
83 RegExp_Unicode = 0x08,
84 RegExp_Sticky = 0x10
85 };
86
88 QmlMode = 0x1,
89 YieldIsKeyword = 0x2,
90 StaticIsKeyword = 0x4
91 };
92
93 enum class ImportState {
94 SawImport,
95 NoQmlImport
96 };
97
98 enum class LexMode { WholeCode, LineByLine };
99
100 enum class CodeContinuation { Reset, Continue };
101
102public:
103 Lexer(Engine *engine, LexMode lexMode = LexMode::WholeCode);
104
105 bool qmlMode() const;
106 bool yieldIsKeyWord() const { return _state.generatorLevel != 0; }
107 void setStaticIsKeyword(bool b) { _staticIsKeyword = b; }
108
109 QString code() const;
110 void setCode(const QString &code, int lineno, bool qmlMode = true,
111 CodeContinuation codeContinuation = CodeContinuation::Reset);
112
113 int lex();
114
115 bool scanRegExp(RegExpBodyPrefix prefix = NoPrefix);
116 bool scanDirectives(Directives *directives, DiagnosticMessage *error);
117
118 int regExpFlags() const { return _state.patternFlags; }
119 QString regExpPattern() const { return _tokenText; }
120
121 int tokenKind() const { return _state.tokenKind; }
122 int tokenOffset() const { return _currentOffset + _tokenStartPtr - _code.unicode(); }
123 int tokenLength() const { return _tokenLength; }
124
125 int tokenStartLine() const { return _tokenLine; }
126 int tokenStartColumn() const { return _tokenColumn; }
127
128 inline QStringView tokenSpell() const { return _tokenSpell; }
129 inline QStringView rawString() const { return _rawString; }
130 double tokenValue() const { return _state.tokenValue; }
131 QString tokenText() const;
132
133 Error errorCode() const;
134 QString errorMessage() const;
135
136 bool canInsertAutomaticSemicolon(int token) const;
137
143
144 enum class CommentState { NoComment, HadComment, InMultilineComment };
145
146 void enterGeneratorBody() { ++_state.generatorLevel; }
147 void leaveGeneratorBody() { --_state.generatorLevel; }
148
149 struct State
150 {
151 Error errorCode = NoError;
152
153 QChar currentChar = u'\n';
154 double tokenValue = 0;
155
156 // parentheses state
157 ParenthesesState parenthesesState = IgnoreParentheses;
158 int parenthesesCount = 0;
159
160 // template string stack
162 int bracesCount = -1;
163
164 int stackToken = -1;
165
166 int patternFlags = 0;
167 int tokenKind = 0;
168 ImportState importState = ImportState::NoQmlImport;
169
170 bool validTokenText = false;
171 bool prohibitAutomaticSemicolon = false;
172 bool restrictedKeyword = false;
173 bool terminator = false;
174 bool followsClosingBrace = false;
175 bool delimited = true;
176 bool handlingDirectives = false;
177 CommentState comments = CommentState::NoComment;
178 int generatorLevel = 0;
179
180 friend bool operator==(State const &s1, State const &s2)
181 {
182 if (s1.errorCode != s2.errorCode)
183 return false;
184 if (s1.currentChar != s2.currentChar)
185 return false;
186 if (s1.tokenValue != s2.tokenValue)
187 return false;
188 if (s1.parenthesesState != s2.parenthesesState)
189 return false;
190 if (s1.parenthesesCount != s2.parenthesesCount)
191 return false;
192 if (s1.outerTemplateBraceCount != s2.outerTemplateBraceCount)
193 return false;
194 if (s1.bracesCount != s2.bracesCount)
195 return false;
196 if (s1.stackToken != s2.stackToken)
197 return false;
198 if (s1.patternFlags != s2.patternFlags)
199 return false;
200 if (s1.tokenKind != s2.tokenKind)
201 return false;
202 if (s1.importState != s2.importState)
203 return false;
204 if (s1.validTokenText != s2.validTokenText)
205 return false;
206 if (s1.prohibitAutomaticSemicolon != s2.prohibitAutomaticSemicolon)
207 return false;
208 if (s1.restrictedKeyword != s2.restrictedKeyword)
209 return false;
210 if (s1.terminator != s2.terminator)
211 return false;
212 if (s1.followsClosingBrace != s2.followsClosingBrace)
213 return false;
214 if (s1.delimited != s2.delimited)
215 return false;
216 if (s1.handlingDirectives != s2.handlingDirectives)
217 return false;
218 if (s1.generatorLevel != s2.generatorLevel)
219 return false;
220 return true;
221 }
222
223 friend bool operator!=(State const &s1, State const &s2) { return !(s1 == s2); }
224
225 friend QML_PARSER_EXPORT QDebug operator<<(QDebug dbg, State const &s);
226 };
227
228 const State &state() const;
229 void setState(const State &state);
230
231protected:
232 static int classify(const QChar *s, int n, int parseModeFlags);
233
234private:
235 int parseModeFlags() const;
236 bool prevTerminator() const;
237 bool followsClosingBrace() const;
238 inline void scanChar();
239 inline QChar peekChar();
240 int scanToken();
241 int scanNumber(QChar ch);
242 int scanVersionNumber(QChar ch);
243 enum ScanStringMode : char16_t {
244 SingleQuote = '\'',
245 DoubleQuote = '"',
246 TemplateHead = '`',
247 TemplateContinuation = 0
248 };
249 int scanString(ScanStringMode mode);
250
251 bool isLineTerminator() const;
252 unsigned isLineTerminatorSequence() const;
253 static bool isIdentLetter(QChar c);
254 static bool isDecimalDigit(ushort c);
255 static bool isHexDigit(QChar c);
256 static bool isOctalDigit(ushort c);
257
258 void syncProhibitAutomaticSemicolon();
259 uint decodeUnicodeEscapeCharacter(bool *ok);
260 QChar decodeHexEscapeCharacter(bool *ok);
261
262 friend QML_PARSER_EXPORT QDebug operator<<(QDebug dbg, const Lexer &l);
263
264private:
265 Engine *_engine;
266
267 LexMode _lexMode = LexMode::WholeCode;
268 QString _code;
269 const QChar *_endPtr;
270 bool _qmlMode;
271 bool _staticIsKeyword = false;
272
273 bool _skipLinefeed = false;
274
275 int _currentLineNumber = 0;
276 int _currentColumnNumber = 0;
277 int _currentOffset = 0;
278
279 int _tokenLength = 0;
280 int _tokenLine = 0;
281 int _tokenColumn = 0;
282
283 QString _tokenText;
284 QString _errorMessage;
285 QStringView _tokenSpell;
286 QStringView _rawString;
287
288 const QChar *_codePtr = nullptr;
289 const QChar *_tokenStartPtr = nullptr;
290
291 State _state;
292};
293
294} // end of namespace QQmlJS
295
297
298#endif // LEXER_H
\inmodule QtCore
\inmodule QtCore
void setStaticIsKeyword(bool b)
int tokenKind() const
int tokenLength() const
int tokenStartColumn() const
@ IllegalUnicodeEscapeSequence
void leaveGeneratorBody()
QString regExpPattern() const
QStringView rawString() const
int tokenOffset() const
bool yieldIsKeyWord() const
int regExpFlags() const
int tokenStartLine() const
double tokenValue() const
void enterGeneratorBody()
QStringView tokenSpell() const
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
else opt state
[0]
Token token
Definition keywords.cpp:444
Combined button and popup list for selecting options.
static bool isHexDigit(const char c)
DBusConnection const char DBusError * error
GLboolean GLboolean GLboolean b
GLenum mode
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
#define QML_PARSER_EXPORT
#define s2
@ NoError
Definition main.cpp:34
unsigned int uint
Definition qtypes.h:34
unsigned short ushort
Definition qtypes.h:33
static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &errorSource, qsizetype errorPosition)
Definition qurl.cpp:3517
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QJSEngine engine
[0]
friend bool operator==(State const &s1, State const &s2)
friend bool operator!=(State const &s1, State const &s2)
QStack< int > outerTemplateBraceCount