aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmldom/qqmldomscanner_p.h
blob: 5ff1c5b767978edbb1da0b7828d7862bdee3a0ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QQMLDOMSCANNER_P_H
#define QQMLDOMSCANNER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qqmldom_global.h"
#include "qqmldomstringdumper_p.h"

#include <QStringList>
#include <QStringView>
#include <QtQml/private/qqmljslexer_p.h>
#include <QtQml/private/qqmljsgrammar_p.h>

QT_BEGIN_NAMESPACE

namespace QQmlJS {
namespace Dom {

class QMLDOM_EXPORT Token
{
    Q_GADGET
public:
    static bool lexKindIsDelimiter(int kind);
    static bool lexKindIsJSKeyword(int kind);
    static bool lexKindIsIdentifier(int kind);
    static bool lexKindIsStringType(int kind);
    static bool lexKindIsInvalid(int kind);
    static bool lexKindIsQmlReserved(int kind);
    static bool lexKindIsComment(int kind);

    inline Token() = default;
    inline Token(int o, int l, int lexKind) : offset(o), length(l), lexKind(lexKind) { }
    inline int begin() const { return offset; }
    inline int end() const { return offset + length; }
    void dump(const Sink &s, QStringView line = QStringView()) const;
    QString toString(QStringView line = QStringView()) const
    {
        return dumperToString([line, this](const Sink &s) { this->dump(s, line); });
    }

    static int compare(const Token &t1, const Token &t2)
    {
        if (int c = t1.offset - t2.offset)
            return c;
        if (int c = t1.length - t2.length)
            return c;
        return int(t1.lexKind) - int(t2.lexKind);
    }

    int offset = 0;
    int length = 0;
    int lexKind = QQmlJSGrammar::T_NONE;
};

inline int operator==(const Token &t1, const Token &t2)
{
    return Token::compare(t1, t2) == 0;
}
inline int operator!=(const Token &t1, const Token &t2)
{
    return Token::compare(t1, t2) != 0;
}

class QMLDOM_EXPORT Scanner
{
public:
    struct QMLDOM_EXPORT State
    {
        Lexer::State state {};
        bool regexpMightFollow = true;
        bool isMultiline() const;
        bool isMultilineComment() const;
    };

    QList<Token> operator()(QStringView text, const State &startState);
    State state() const;

private:
    bool _qmlMode = true;
    State _state;
};

} // namespace Dom
} // namespace QQmlJS
QT_END_NAMESPACE
#endif