1// Copyright (C) 2022 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 QQMLDOMSCANNER_P_H
5#define QQMLDOMSCANNER_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 "qqmldom_global.h"
19#include "qqmldomstringdumper_p.h"
20
21#include <QStringList>
22#include <QStringView>
23#include <QtQml/private/qqmljslexer_p.h>
24#include <QtQml/private/qqmljsgrammar_p.h>
25
26QT_BEGIN_NAMESPACE
27
28namespace QQmlJS {
29namespace Dom {
30
31class QMLDOM_EXPORT Token
32{
33 Q_GADGET
34public:
35 static bool lexKindIsDelimiter(int kind);
36 static bool lexKindIsJSKeyword(int kind);
37 static bool lexKindIsIdentifier(int kind);
38 static bool lexKindIsStringType(int kind);
39 static bool lexKindIsInvalid(int kind);
40 static bool lexKindIsQmlReserved(int kind);
41 static bool lexKindIsComment(int kind);
42
43 inline Token() = default;
44 inline Token(int o, int l, int lexKind) : offset(o), length(l), lexKind(lexKind) { }
45 inline int begin() const { return offset; }
46 inline int end() const { return offset + length; }
47 void dump(Sink s, QStringView line = QStringView()) const;
48 QString toString(QStringView line = QStringView()) const
49 {
50 return dumperToString([line, this](Sink s) { this->dump(s, line); });
51 }
52
53 static int compare(const Token &t1, const Token &t2)
54 {
55 if (int c = t1.offset - t2.offset)
56 return c;
57 if (int c = t1.length - t2.length)
58 return c;
59 return int(t1.lexKind) - int(t2.lexKind);
60 }
61
62 int offset = 0;
63 int length = 0;
64 int lexKind = QQmlJSGrammar::T_NONE;
65};
66
67inline int operator==(const Token &t1, const Token &t2)
68{
69 return Token::compare(t1, t2) == 0;
70}
71inline int operator!=(const Token &t1, const Token &t2)
72{
73 return Token::compare(t1, t2) != 0;
74}
75
76class QMLDOM_EXPORT Scanner
77{
78public:
79 struct QMLDOM_EXPORT State
80 {
81 Lexer::State state {};
82 bool regexpMightFollow = true;
83 bool isMultiline() const;
84 bool isMultilineComment() const;
85 };
86
87 QList<Token> operator()(QStringView text, const State &startState);
88 State state() const;
89
90private:
91 bool _qmlMode = true;
92 State _state;
93};
94
95} // namespace Dom
96} // namespace QQmlJS
97QT_END_NAMESPACE
98#endif
99

source code of qtdeclarative/src/qmldom/qqmldomscanner_p.h