-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGenerator.cpp
More file actions
174 lines (156 loc) · 6 KB
/
Copy pathGenerator.cpp
File metadata and controls
174 lines (156 loc) · 6 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "Generator.h"
#include <cctype>
#include "NameFormat.h"
#include "QualifiedName.h"
const char *const Generator::signature =
"// Generated by json-cpp-gen by Viktor Chlumsky\n"
"// https://github.com/Chlumsky/json-cpp-gen\n\n";
const unsigned Generator::FEATURE_CSTDLIB = 0x01;
Generator::Generator(const std::string &className, const StringType *stringType, const Settings &settings) : mStringType(stringType), mSettings(settings) {
std::string namePart;
for (char c : className) {
if (c == ':') {
if (!namePart.empty()) {
classNamespaces.push_back((std::string &&) namePart);
namePart.clear();
}
} else
namePart.push_back(c);
}
this->className = (std::string &&) namePart;
}
void Generator::addTypeInclude(const std::string &includeAddress) {
typeIncludes.push_back(includeAddress);
}
void Generator::addFeature(unsigned featureBit) {
featureBits |= featureBit;
}
std::string Generator::getJsonMemberNameLiteral(const std::string &memberName) const {
return "\""+memberName+"\"";
}
std::string Generator::getJsonEnumValueLiteral(const std::string &enumValue) const {
return "\""+enumValue+"\"";
}
void Generator::resolveVirtualTypename(const Type *type, const Type *parentType, const std::string &memberName) {
if (type->name().substance() == TypeName::VIRTUAL && resolvedVirtualTypenames.insert(type->name().body()).second) {
std::string typeName = parentType->name().body()+"::"+memberName;
for (char c : type->name().suffix()) {
if (c == '[')
typeName += "[0]";
}
virtualTypedefs.emplace_back((std::string &&) typeName, type->name().body());
}
}
std::string Generator::generateVirtualTypedefs(const std::string &indent) {
std::string code;
if (!virtualTypedefs.empty()) {
code += indent+"template <typename T> struct NonRef { typedef T Type; };\n";
code += indent+"template <typename T> struct NonRef<T &> { typedef T Type; };\n";
code += indent+"template <typename T> struct NonRef<const T &> { typedef T Type; };\n\n";
for (const std::pair<std::string, std::string> &virtualTypedef : virtualTypedefs)
code += indent+"typedef typename NonRef<decltype("+virtualTypedef.first+")>::Type "+virtualTypedef.second+";\n";
code += "\n";
}
return code;
}
std::string Generator::generateFunctionName(const char *prefix, const Type *type) {
std::string functionName = prefix+formatName(type->name().body(), NameFormat::CAMELCASE_CAPITAL);
for (char c : type->name().suffix()) {
if ((c&0x80) || isalnum(c))
functionName.push_back(c);
else if (c == '[')
functionName.push_back('_');
}
// Resolve collision
if (usedFunctionNames.find(functionName) != usedFunctionNames.end()) {
std::string baseFunctionName = functionName;
if (!baseFunctionName.empty() && baseFunctionName.back() >= '0' && baseFunctionName.back() <= '9')
baseFunctionName.push_back('_');
int i = 0;
do {
functionName = baseFunctionName+std::to_string(++i);
} while (usedFunctionNames.find(functionName) != usedFunctionNames.end());
}
usedFunctionNames.insert(functionName);
return functionName;
}
std::string Generator::beginNamespace() const {
std::string code;
for (const std::string &namespaceName : classNamespaces)
code += "namespace "+namespaceName+" {\n\n";
return code;
}
std::string Generator::endNamespace() const {
std::string code;
for (size_t n = classNamespaces.size(); n; --n)
code += "\n}\n";
return code;
}
static bool matchSubstr(const std::string &str, size_t pos, const char *needle) {
while (pos < str.size() && *needle && str[pos++] == *needle++);
return !*needle;
}
std::string Generator::safeName(const std::string &name) {
if (name.empty() || name.front() == ':')
return name;
bool nameStart = true;
for (size_t i = 0; i < name.size() && (isNameChar(name[i]) || name[i] == ':'); ++i) {
if (nameStart && (
(matchSubstr(name, i, "Error") && !isNameChar(name.c_str()[i+sizeof("Error")-1])) ||
(matchSubstr(name, i, "NonRef") && !isNameChar(name.c_str()[i+sizeof("NonRef")-1])) ||
(matchSubstr(name, i, UNNAMED_PREFIX) && name.c_str()[i+sizeof(UNNAMED_PREFIX)-1] >= '0' && name.c_str()[i+sizeof(UNNAMED_PREFIX)-1] <= '9')
)) {
// Add global namespace if name contains names used by parsers or serializers
return "::"+name;
}
nameStart = name[i] == ':';
}
return name;
}
std::string Generator::charLiteral(char c) {
switch (c) {
case '\0': return "'\\0'";
case '\b': return "'\\b'";
case '\f': return "'\\f'";
case '\n': return "'\\n'";
case '\r': return "'\\r'";
case '\t': return "'\\t'";
case '\'': return "'\\''";
case '\\': return "'\\\\'";
}
if (c >= 0x20 && c < 0x7f) {
char buffer[] = "'#'";
buffer[1] = c;
return buffer;
} {
char buffer[] = "'\\x##'";
buffer[3] = "0123456789abcdef"[c>>4&0x0f];
buffer[4] = "0123456789abcdef"[c&0x0f];
return buffer;
}
}
std::string Generator::translateIndentation(std::string &&code, const std::string &indentation) {
if (indentation == "\t")
return (std::string &&) code;
std::string translatedCode;
translatedCode.reserve(2*code.size());
bool lineStart = true;
for (char c : code) {
switch (c) {
case '\n':
lineStart = true;
translatedCode.push_back('\n');
break;
case '\t':
if (lineStart) {
translatedCode += indentation;
break;
}
// fallthrough
default:
lineStart = false;
translatedCode.push_back(c);
}
}
return translatedCode;
}