-
Notifications
You must be signed in to change notification settings - Fork 2
/
pb_parser.cpp
401 lines (367 loc) · 14.1 KB
/
pb_parser.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "pb_parser.h"
#include <google/protobuf/compiler/plugin.pb.h>
#include <google/protobuf/util/json_util.h>
#include "pb_include.h"
namespace parser {
namespace pb {
using namespace google::protobuf;
void WrappedErrorCollector::AddError(int line, io::ColumnNumber column, const std::string& message) {
this->errors.push_back(ErrorInfo{
.filename = "",
.elemName = "",
.line = line,
.column = int(column),
.message = message,
});
}
void WrappedErrorCollector::AddError(const std::string& filename, int line, int column, const std::string& message) {
this->errors.push_back(ErrorInfo{
.filename = filename,
.elemName = {},
.line = line,
.column = int(column),
.message = message,
});
}
void WrappedErrorCollector::AddError(const string& filename, const string& element_name, const Message* descriptor, ErrorLocation location, const string& message) {
this->errors.push_back(ErrorInfo{
.filename = filename,
.elemName = element_name,
.line = -1,
.column = 0,
.message = message,
});
}
io::ZeroCopyInputStream* MemorySourceTree::Open(const std::string& filename) {
if (this->parser_option_ == nullptr || this->parser_option_->include_path_.empty()) {
auto file = this->files_.find(filename);
if (file == this->files_.end()) {
return nullptr;
}
return new io::ArrayInputStream(file->second.data_, int(file->second.size_));
}
for (const auto& include_path : this->parser_option_->include_path_) {
decltype(this->files_.end()) file;
if (include_path == "" || include_path == ".") {
file = this->files_.find(filename);
} else {
file = this->files_.find(include_path + "/" + filename);
}
if (file == this->files_.end()) {
continue;
}
return new io::ArrayInputStream(file->second.data_, int(file->second.size_));
}
return nullptr;
}
bool findGoogleProtobuf(const ::std::string& filename, google::protobuf::FileDescriptorProto* output);
bool MemorySourceTree::FindFileByName(const std::string& filename, google::protobuf::FileDescriptorProto* output) {
std::unique_ptr<io::ZeroCopyInputStream> input_stream(this->Open(filename));
if (input_stream == nullptr) {
if (findGoogleProtobuf(filename, output)) {
return true;
}
if (this->error_collector_ != nullptr) {
this->error_collector_->AddError(filename, -1, 0, this->GetLastErrorMessage());
}
return false;
}
compiler::Parser parser;
MemorySourceTree::FileErrorCollector file_error_collector(filename, this->error_collector_);
parser.RecordErrorsTo(&file_error_collector);
if (this->parser_option_ != nullptr) {
parser.SetRequireSyntaxIdentifier(this->parser_option_->require_syntax_identifier_);
}
io::Tokenizer tokenizer(input_stream.get(), &file_error_collector);
output->set_name(filename);
return (parser.Parse(&tokenizer, output) && !file_error_collector.HadErrors());
}
bool MemorySourceTree::FindFileContainingSymbol(const std::string& symbol_name, google::protobuf::FileDescriptorProto* output) {
return false;
}
bool MemorySourceTree::FindFileContainingExtension(const std::string& containing_type, int field_number, google::protobuf::FileDescriptorProto* output) {
return false;
}
inline std::unique_ptr<std::string> messageToBinary(Message& msg);
inline std::unique_ptr<std::string> messageToJSON(Message& msg);
std::unique_ptr<std::string> ParsePBFile(const char* file, size_t size, io::ErrorCollector* error_collector, PbParserOption* option) {
using namespace std;
compiler::Parser parser;
parser.SetRequireSyntaxIdentifier(option->require_syntax_identifier_);
parser.RecordErrorsTo(error_collector);
io::ArrayInputStream raw_input(file, int(size));
io::Tokenizer input(&raw_input, error_collector);
FileDescriptorProto desc;
if (!parser.Parse(&input, &desc)) {
return std::unique_ptr<std::string>();
}
if (!option->with_source_code_info_) {
desc.clear_source_code_info();
}
switch (option->message_type_) {
case PbParserOption::Json:
return messageToJSON(desc);
default:
return messageToBinary(desc);
}
}
typedef WrappedErrorCollector Errors;
bool loadFileDescriptorSet(FileDescriptorSet* files, const FileDescriptor* main, Errors* errors, std::unordered_set<std::string>* walk_set, PbParserOption* option);
std::unique_ptr<std::string> ParseMultiPBFile(const std::string& main, const StringMap& treeMap, Errors* errors, PbParserOption* option) {
MemorySourceTree sourceTree(treeMap);
sourceTree.SetErrorCollector(errors);
sourceTree.SetPbParserOption(option);
DescriptorPool descriptorPool(&sourceTree, errors);
auto mainDesc = descriptorPool.FindFileByName(main);
if (mainDesc == nullptr) {
if (errors->errors.empty()) {
errors->AddError(main, -1, 0, "Not Found File");
}
return std::unique_ptr<std::string>();
}
std::unordered_set<std::string> walk_set;
FileDescriptorSet fileSet;
if (!loadFileDescriptorSet(&fileSet, mainDesc, errors, &walk_set, option)) {
return std::unique_ptr<std::string>();
}
PbParserOption::MessageType message_type = PbParserOption::MessageType::PB;
if (option != nullptr) {
message_type = option->message_type_;
}
switch (message_type) {
case PbParserOption::MessageType::Json:
return messageToJSON(fileSet);
default:
return messageToBinary(fileSet);
}
}
inline char* dupString(std::string& str) {
char* desc_str = new char[str.length()];
if (str.length() == 0) {
return desc_str;
}
memmove(desc_str, str.c_str(), str.length());
return desc_str;
}
inline std::unique_ptr<std::string> messageToBinary(Message& msg) {
auto str = std::unique_ptr<std::string>(new std::string());
str->reserve(64);
io::StringOutputStream sstream(str.get());
io::CodedOutputStream output(&sstream);
msg.SerializePartialToCodedStream(&output);
return str;
}
inline std::unique_ptr<std::string> messageToJSON(Message& msg) {
auto out = std::unique_ptr<std::string>(new std::string());
out->reserve(64 * 2);
util::MessageToJsonString(msg, out.get());
return out;
}
bool findGoogleProtobuf(const ::std::string& filename, google::protobuf::FileDescriptorProto* output) {
typedef ::std::unordered_map<::std::string, ::std::function<bool(FileDescriptorProto*)>> DescMap;
static const DescMap* google_protobuf = new DescMap{
{
"google/protobuf/any.proto",
BUILD_FILE_DESCRIPTOR_PROTO(Any),
},
{
"google/protobuf/api.proto",
BUILD_FILE_DESCRIPTOR_PROTO(Api),
},
// {
// "google/protobuf/compiler/plugin.proto",
// BUILD_FILE_DESCRIPTOR_PROTO(compiler::Version),
// },
{
"google/protobuf/descriptor.proto",
BUILD_FILE_DESCRIPTOR_PROTO(FileDescriptorProto),
},
{
"google/protobuf/duration.proto",
BUILD_FILE_DESCRIPTOR_PROTO(Duration),
},
{
"google/protobuf/empty.proto",
BUILD_FILE_DESCRIPTOR_PROTO(Empty),
},
{
"google/protobuf/field_mask.proto",
BUILD_FILE_DESCRIPTOR_PROTO(FieldMask),
},
{
"google/protobuf/source_context.proto",
BUILD_FILE_DESCRIPTOR_PROTO(SourceContext),
},
{
"google/protobuf/struct.proto",
BUILD_FILE_DESCRIPTOR_PROTO(Struct),
},
{
"google/protobuf/timestamp.proto",
BUILD_FILE_DESCRIPTOR_PROTO(Timestamp),
},
{
"google/protobuf/type.proto",
BUILD_FILE_DESCRIPTOR_PROTO(Field),
},
{
"google/protobuf/wrappers.proto",
BUILD_FILE_DESCRIPTOR_PROTO(BoolValue),
}};
auto result = google_protobuf->find(filename);
if (result == google_protobuf->end()) {
return false;
}
return result->second(output);
}
bool loadFileDescriptorSet(FileDescriptorSet* files, const FileDescriptor* main, Errors* errors, std::unordered_set<std::string>* walk_set, PbParserOption* option) {
typedef ::std::unordered_set<::std::string> std_string_set;
static const std_string_set* google_protobuf = new std_string_set{
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/compiler/plugin.proto",
"google/protobuf/descriptor.proto",
"google/protobuf/duration.proto",
"google/protobuf/empty.proto",
"google/protobuf/field_mask.proto",
"google/protobuf/source_context.proto",
"google/protobuf/struct.proto",
"google/protobuf/timestamp.proto",
"google/protobuf/type.proto",
"google/protobuf/wrappers.proto",
};
if (option != nullptr && !option->with_google_protobuf_) {
auto result = google_protobuf->find(main->name());
if (result != google_protobuf->end()) {
return true;
}
}
auto exist = walk_set->find(main->name());
if (exist != walk_set->end()) {
return true;
}
if (main->name().find_first_of(""))
walk_set->insert(main->name());
auto newFileDesc = files->add_file();
main->CopyTo(newFileDesc);
if (option != nullptr) {
if (option->with_source_code_info_) {
main->CopySourceCodeInfoTo(newFileDesc);
}
if (option->with_json_tag_) {
main->CopyJsonNameTo(newFileDesc);
}
}
newFileDesc->set_syntax(main->SyntaxName(main->syntax()));
for (int i = 0; i < main->dependency_count(); ++i) {
auto dep = main->dependency(i);
if (dep == nullptr) {
errors->AddError("", -1, 0, "not found file");
return false;
}
if (!loadFileDescriptorSet(files, dep, errors, walk_set, option)) {
return false;
}
}
return true;
}
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#include "cgo.h"
#endif
struct ParserResult* ParsePBFile_C(const char* file, int size, struct ParserOption* option) {
WrappedErrorCollector collector;
PbParserOption cpp_option;
cpp_option.message_type_ = PbParserOption::MessageType(option->message_type);
cpp_option.require_syntax_identifier_ = bool(option->require_syntax_identifier);
cpp_option.with_source_code_info_ = bool(option->with_source_code_info);
cpp_option.with_json_tag_ = bool(option->with_json_tag);
cpp_option.with_google_protobuf_ = bool(option->with_google_protobuf);
auto desc = ParsePBFile(file, size_t(size), &collector, &cpp_option);
auto result = new ParserResult{};
result->desc_pointer = desc.release();
result->errors_size = int(collector.errors.size());
result->errors = new ParserErrorMessage[collector.errors.size()];
for (int x = 0; x < int(collector.errors.size()); x++) {
auto item = collector.errors.at(x);
result->errors[x] = ParserErrorMessage{
.line = item.line,
.column = item.column,
.message = parser::pb::dupString(item.message),
.message_size = int(item.message.length()),
};
}
return result;
}
struct ParserResult* ParseMultiPBFile_C(struct ParerFiles* files, struct ParserOption* option) {
StringMap tree;
tree.reserve(files->files_size);
for (int x = 0; x < files->files_size; x++) {
auto file = files->files[x];
tree[file.filename] = {
.data_ = file.content,
.size_ = size_t(file.content_size),
};
}
// std::cout << "main: " << files->main_filename << std::endl;
// for (const auto& item : tree) {
// std::cout << "filename: " << item.first << std::endl;
// std::cout << "content: " << item.second.data_ << std::endl;
// }
WrappedErrorCollector collector;
PbParserOption cpp_option;
for (int x = 0; x < option->include_name_size; x++) {
cpp_option.include_path_.push_back(option->include_name[x]);
}
cpp_option.message_type_ = PbParserOption::MessageType(option->message_type);
cpp_option.require_syntax_identifier_ = bool(option->require_syntax_identifier);
cpp_option.with_source_code_info_ = bool(option->with_source_code_info);
cpp_option.with_json_tag_ = bool(option->with_json_tag);
cpp_option.with_google_protobuf_ = bool(option->with_google_protobuf);
auto desc = ParseMultiPBFile(std::string(files->main_filename), tree, &collector, &cpp_option);
auto result = new ParserResult{};
result->desc_pointer = desc.release();
if (collector.errors.size() > 0) {
result->errors_size = int(collector.errors.size());
result->errors = new ParserErrorMessage[collector.errors.size()];
}
for (int x = 0; x < int(collector.errors.size()); x++) {
auto& item = collector.errors.at(x);
result->errors[x] = ParserErrorMessage{
.line = item.line,
.column = item.column,
.message = parser::pb::dupString(item.message),
.message_size = int(item.message.length()),
.filename = parser::pb::dupString(item.filename),
};
}
return result;
}
void Delete_ParserResult(struct ParserResult* r) {
for (int i = 0; i < r->errors_size; ++i) {
delete r->errors[i].message;
delete r->errors[i].filename;
}
delete[] r->errors;
delete (std::string*)r->desc_pointer;
delete r;
}
const char* ParserResult_GetDesc(struct ParserResult* r) {
if (r->desc_pointer == nullptr) {
return nullptr;
}
std::string* desc = (std::string*)r->desc_pointer;
return desc->c_str();
}
int ParserResult_GetDescSize(struct ParserResult* r) {
if (r->desc_pointer == nullptr) {
return 0;
}
std::string* desc = (std::string*)r->desc_pointer;
return int(desc->size());
}
#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif
} // namespace pb
} // namespace parser