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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#ifndef QTYAML_H
#define QTYAML_H
#include <functional>
#include <vector>
#include <chrono>
#include <QtCore/QJsonParseError>
#include <QtCore/QVector>
#include <QtCore/QByteArray>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtAppManCommon/global.h>
#include <QtAppManCommon/exception.h>
QT_BEGIN_NAMESPACE_AM
namespace QtYaml {
class ParseError
{
public:
ParseError() { }
QString errorString() const { return m_errorString; }
int line = -1;
int column = -1;
// QJsonParseError compatibility
int offset = -1;
QJsonParseError::ParseError error = QJsonParseError::NoError;
// not really public
ParseError(const QString &_errorString, int _line = -1, int _column = -1, int _offset = -1)
: line(_line)
, column(_column)
, offset(_offset)
, error(QJsonParseError::UnterminatedObject) // not really, but there are no suitable options
, m_errorString(_errorString)
{ }
private:
QString m_errorString;
};
enum YamlStyle { FlowStyle, BlockStyle };
QByteArray yamlFromVariantDocuments(const QVector<QVariant> &maps, YamlStyle style = BlockStyle);
} // namespace QtYaml
class YamlParserPrivate;
class YamlParserException;
class YamlParser
{
public:
YamlParser(const QByteArray &data, const QString &fileName = QString());
~YamlParser();
QString sourceUrl() const;
QString sourceDir() const;
QString sourceName() const;
static QVector<QVariant> parseAllDocuments(const QByteArray &yaml);
QPair<QString, int> parseHeader();
bool nextDocument();
void nextEvent();
bool isScalar() const;
QVariant parseScalar();
QString parseString();
int parseInt(int min = std::numeric_limits<int>::min(), int max = std::numeric_limits<int>::max());
bool parseBool();
std::chrono::seconds parseDurationAsSec(QStringView defaultUnit = { });
std::chrono::milliseconds parseDurationAsMSec(QStringView defaultUnit = { });
std::chrono::microseconds parseDurationAsUSec(QStringView defaultUnit = { });
bool isMap() const;
QVariantMap parseMap();
QMap<QString, QString> parseStringMap();
bool isList() const;
QVariantList parseList();
void parseList(const std::function<void()> &callback);
// convenience
QVariant parseVariant();
QStringList parseStringOrStringList();
enum FieldType : int { Scalar = 0x01, List = 0x02, Map = 0x04 };
Q_DECLARE_FLAGS(FieldTypes, FieldType)
struct Field
{
QString name;
bool required : 1;
bool enabled : 1;
FieldTypes types;
std::function<void()> callback;
Field(const char *_name, bool _required, FieldTypes _types,
const std::function<void()> &_callback)
: name(QString::fromLatin1(_name))
, required(_required)
, enabled(true)
, types(_types)
, callback(_callback)
{ }
Field(bool _enabled, const char *_name, bool _required, FieldTypes _types,
const std::function<void()> &_callback)
: name(QString::fromLatin1(_name))
, required(_required)
, enabled(_enabled)
, types(_types)
, callback(_callback)
{ }
};
typedef std::vector<Field> Fields;
void parseFields(const Fields &fields);
private:
Q_DISABLE_COPY_MOVE(YamlParser)
QString parseMapKey();
YamlParserPrivate *d;
friend class YamlParserException;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(YamlParser::FieldTypes)
class YamlParserException : public Exception // clazy:exclude=copyable-polymorphic
{
public:
explicit YamlParserException(YamlParser *p, const char *errorString);
};
QT_END_NAMESPACE_AM
// We mean it. Dummy comment since syncqt needs this also for completely private Qt modules.
#endif // QTYAML_H
|