-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.cpp
More file actions
128 lines (101 loc) · 3.71 KB
/
schema.cpp
File metadata and controls
128 lines (101 loc) · 3.71 KB
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
#include "schema.h"
#include "crc32.h"
#include <qt-json/json.h>
SCHEMA deserializeJSON(QString jsonText)
{
JsonObject json = QtJson::parse(jsonText).toMap();
SCHEMA schema;
JsonArray constructors = json["constructors"].toList();
constructors.append(json["methods"].toList());
for (qint32 i = 0; i < constructors.size(); i++) {
JsonObject constructorJSON = constructors[i].toMap();
bool isMethod = !constructorJSON["method"].toString().isEmpty();
CONSTRUCTOR constructor;
constructor.id = constructorJSON["id"].toInt();
if (isMethod)
constructor.predicate = constructorJSON["method"].toString();
else
constructor.predicate = constructorJSON["predicate"].toString();
constructor.type = constructorJSON["type"].toString();
JsonArray params = constructorJSON["params"].toList();
for (qint32 j = 0; j < params.size(); j++) {
JsonObject paramJSON = params[j].toMap();
PARAM param;
param.name = paramJSON["name"].toString();
param.type = paramJSON["type"].toString();
constructor.params.append(param);
}
if (isMethod)
schema.methods.append(constructor);
else
schema.constructors.append(constructor);
}
return schema;
}
SCHEMA deserializeTL(QString tlText)
{
tlText.replace("\r\n", "\n") //replace CRLF -> LF
.replace("\r", "\n") //remaining CR -> LF
.replace(QRegExp("\\/\\*(\\*(?!\\/)|[^*])*\\*\\/"), " ") //remove block comments
.replace(QRegExp("\\/\\/[^\\n]*"), " "); //remove line comments
QStringList lines = tlText.split("\n");
SCHEMA schema;
bool methodsSection = false;
foreach (QString line, lines) {
line = line.simplified();
if (line.isEmpty()) {
continue;
}
if (line == "---types---") {
methodsSection = false;
continue;
}
if (line == "---functions---") {
methodsSection = true;
continue;
}
QStringList tokens = line.split(" ", QString::SkipEmptyParts);
if (tokens.isEmpty()) {
continue;
}
CONSTRUCTOR constructor;
QString fullType = tokens[0];
qint32 indexOfId = fullType.indexOf("#");
if (indexOfId == -1) {
constructor.predicate = fullType;
constructor.id = CRC32(line.toUtf8());
} else {
constructor.predicate = fullType.left(indexOfId);
constructor.id = fullType.mid(indexOfId + 1).toUInt(0, 16);
}
qint32 typeSeparatorIndex = line.indexOf("=");
if (typeSeparatorIndex != -1) {
constructor.type = line.mid(typeSeparatorIndex + 1).simplified();
if (constructor.type.endsWith(";"))
constructor.type = constructor.type.mid(0, constructor.type.size() - 1);
}
foreach (QString token, tokens.mid(1)) {
if (token == "=") {
break;
}
//Skip generics because we don't need them
if (token.startsWith("{") && token.endsWith("}")) {
continue;
}
PARAM param;
qint32 indexOfType = token.indexOf(":");
//Same for parameters without type
if (indexOfType == -1) {
continue;
}
param.name = token.left(indexOfType);
param.type = token.mid(indexOfType + 1);
constructor.params.append(param);
}
if (methodsSection)
schema.methods.append(constructor);
else
schema.constructors.append(constructor);
}
return schema;
}