aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor/conditionalstreamreader.h
blob: db86ac1e6259ee224006a8490b894478628a2984 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt for Python.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef CONDITIONALSTREAMREADER_H
#define CONDITIONALSTREAMREADER_H

#include <QtCore/QXmlStreamReader>

#include <utility>

QT_FORWARD_DECLARE_CLASS(QDebug)

class ProxyEntityResolver;

/// ConditionalStreamReader encapsulates QXmlStreamReader, offering the same
/// API (except readNextStartElement() and similar conveniences) and internally
/// uses Processing Instructions like:
///   <?if keyword1 !keyword2?> ... <?endif?>
/// to exclude/include sections depending on a list of condition keywords,
/// containing for example the OS.
/// It should be possible to use it as a drop-in replacement for
/// QXmlStreamReader for any parsing code based on readNext().
/// It also allows for specifying entities using a Processing Instruction:
/// <?entity name value?>
/// which can be used in conjunction with conditional processing.
class ConditionalStreamReader
{
public:
    using TokenType = QXmlStreamReader::TokenType;
    explicit ConditionalStreamReader(QIODevice *iod);
    explicit ConditionalStreamReader(const QString &s);
    ~ConditionalStreamReader();

    QIODevice *device() const { return m_reader.device(); }

    // Note: Caching of entity values is done internally by
    // ConditionalStreamReader.
    void setEntityResolver(QXmlStreamEntityResolver *resolver);
    QXmlStreamEntityResolver *entityResolver() const;

    bool atEnd() const { return m_reader.atEnd(); }
    TokenType readNext();

    TokenType tokenType() const { return  m_reader.tokenType(); }

    qint64 lineNumber() const { return m_reader.lineNumber(); }
    qint64 columnNumber() const { return m_reader.columnNumber(); }

    QXmlStreamAttributes attributes() const { return m_reader.attributes(); }

    QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour = QXmlStreamReader::ErrorOnUnexpectedElement)
    { return m_reader.readElementText(behaviour); }

    QStringView name() const { return m_reader.name(); }
    QStringView qualifiedName() const { return m_reader.qualifiedName(); }

    QStringView text() const { return m_reader.text(); }

    QString errorString() const { return m_reader.errorString(); }
    QXmlStreamReader::Error error() const { return m_reader.error(); }

    bool hasError() const { return m_reader.hasError(); }

    // Additional functions (not delegating to QXmlStreamReader)
    void defineEntity(const QString &name, const QString &value);

    const QStringList &conditions() const { return m_conditions; }
    void setConditions(const QStringList &newConditions);

    static QStringList platformConditions();

private:
    enum class PiTokens { None, If, Endif, EntityDefinition };

    using ExtendedToken = std::pair<TokenType, PiTokens>;
    ExtendedToken readNextInternal();
    void init();
    bool conditionMatches() const;
    bool readEntityDefinitonPi();

    QXmlStreamReader m_reader;
    ProxyEntityResolver *m_proxyEntityResolver = nullptr;
    QStringList m_conditions = ConditionalStreamReader::platformConditions();
};

QDebug operator<<(QDebug dbg, const QXmlStreamAttributes &a);

#endif // CONDITIONALSTREAMREADER_H