-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproto.hpp
More file actions
242 lines (204 loc) · 6.2 KB
/
proto.hpp
File metadata and controls
242 lines (204 loc) · 6.2 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
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
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
#include <iostream>
#include <string>
#include <vector>
struct GImport {
boost::optional<int> kind;
std::string filename;
};
inline std::ostream& operator<<(std::ostream& stream, const GImport& val) {
return stream << "GImport{" << (val.kind ? val.kind.get() : 0) << "|" << val.filename << "}";
}
inline std::ostream& operator<<(std::ostream& stream, const std::vector<std::string> & val) {
stream << "v<s>{";
for(const auto & s : val)
stream << s << "|";
return stream << "}";
}
BOOST_FUSION_ADAPT_STRUCT(GImport,
(boost::optional<int>, kind)
(std::string, filename)
)
struct GOption {
std::string option_name;
std::string constant;
};
inline std::ostream& operator<<(std::ostream& stream, const GOption& val) {
return stream << "GOption{" << val.option_name << "|" << val.constant << "}";
}
BOOST_FUSION_ADAPT_STRUCT(GOption,
(std::string, option_name)
(std::string, constant)
)
enum class GFieldKind { OPTIONAL, REQUIRED, REPEATED, ONEOF };
// ONEOF is not parsed from file, but assigned in code as a shortcut
// when generating field inside oneof group
struct GField {
GFieldKind kind = GFieldKind::OPTIONAL;
std::string type;
std::string name;
std::string number;
std::vector<GOption> options;
bool is_true_optional()const{
for(const auto & op : options)
if(op.option_name == "protocute.optional" && op.constant == "true")
return true;
return false;
}
bool is_packed()const{
for(const auto & op : options)
if(op.option_name == "packed" && op.constant == "true")
return true;
return false;
}
// std::string get_default()const{
// for(const auto & op : options)
// if(op.option_name == "default")
// return op.constant;
// return std::string{};
// }
};
inline std::ostream& operator<<(std::ostream& stream, const GField& val) {
return stream << "GField{" << int(val.kind) << "|" << val.type << "|" << val.name << "|" << val.number << "|" << val.options.size() << "}";
}
BOOST_FUSION_ADAPT_STRUCT(GField,
(GFieldKind, kind)
(std::string, type)
(std::string, name)
(std::string, number)
(std::vector<GOption>, options)
)
struct GEnumField {
std::string name;
std::string value;
std::vector<GOption> options;
};
struct GEmptyStatement {};
struct GEnum {
typedef boost::variant<GEnumField, GOption> GEnumFieldVariant;
std::string name;
std::vector<GEnumFieldVariant> fields;
};
inline std::ostream& operator<<(std::ostream& stream, const GEnumField& val) {
return stream << "GEnumField{" << val.name << "|" << val.value << "|" << val.options.size() << "}";
}
inline std::ostream& operator<<(std::ostream& stream, const GEnum& val) {
stream << "GEnum{" << val.name << "|";
for(const auto & s : val.fields)
if(s.type() == typeid(GEnumField))
stream << boost::get<GEnumField>(s) << "|";
else if(s.type() == typeid(GOption))
stream << boost::get<GOption>(s) << "|";
return stream << "}";
}
BOOST_FUSION_ADAPT_STRUCT(GEnumField,
(std::string, name)
(std::string, value)
(std::vector<GOption>, options)
)
BOOST_FUSION_ADAPT_STRUCT(GEnum,
(std::string, name)
(std::vector<GEnum::GEnumFieldVariant>, fields)
)
struct GOneOfField {
std::string type;
std::string name;
std::string number;
std::vector<GOption> options;
};
inline std::ostream& operator<<(std::ostream& stream, const GOneOfField& val) {
return stream << "GOneOfField{" << val.type << "|" << val.name << "|" << val.number << "|" << val.options.size() << "}";
}
BOOST_FUSION_ADAPT_STRUCT(GOneOfField,
(std::string, type)
(std::string, name)
(std::string, number)
(std::vector<GOption>, options)
)
struct GOneOf {
std::string name;
std::vector<GOneOfField> fields;
};
inline std::ostream& operator<<(std::ostream& stream, const GOneOf& val) {
return stream << "GOneOf{" << val.name << "|" << val.fields.size() << "}";
}
BOOST_FUSION_ADAPT_STRUCT(GOneOf,
(std::string, name)
(std::vector<GOneOfField>, fields)
)
struct GMessage;
typedef boost::variant<boost::recursive_wrapper<GMessage>, GField, GEnum, GOption, GOneOf, GEmptyStatement> GMessageFieldVariant;
struct GMessage {
std::string name;
std::vector<GMessageFieldVariant> fields;
};
struct PrintVisitor {
std::ostream& stream;
explicit PrintVisitor(std::ostream& stream):stream(stream) {}
void operator()(GField const& s) const
{
stream << s;
}
void operator()(GEnum const& s) const
{
stream << s;
}
void operator()(GOption const& s) const
{
stream << s;
}
void operator()(GOneOf const& s) const
{
stream << s;
}
void operator()(GMessage const& s) const;
void operator()(GEmptyStatement const& s) const
{}
};
inline std::ostream& operator<<(std::ostream& stream, const GMessage& val) {
stream << "GMessage{" << val.name << "|";
for(const auto & s : val.fields){
boost::apply_visitor(PrintVisitor{stream}, s);
stream << "|";
}
return stream << "}";
}
inline void PrintVisitor::operator()(GMessage const& s) const
{
stream << s;
}
BOOST_FUSION_ADAPT_STRUCT(GMessage,
(std::string, name)
(std::vector<GMessageFieldVariant>, fields)
)
// std::string - package
struct GProtoFile {
typedef boost::variant<GImport, std::string, GOption, GEnum, GMessage, GEmptyStatement> GProtoFileFieldVariant;
std::string syntax;
std::vector<GProtoFileFieldVariant> fields;
};
inline std::ostream& operator<<(std::ostream& stream, const GProtoFile& val) {
stream << "GProtoFile{" << val.syntax << "|";
for(const auto & s : val.fields)
if(s.type() == typeid(GImport))
stream << boost::get<GImport>(s) << "|";
else if(s.type() == typeid(std::string))
stream << boost::get<std::string>(s) << "|";
else if(s.type() == typeid(GOption))
stream << boost::get<GOption>(s) << "|";
else if(s.type() == typeid(GEnum))
stream << boost::get<GEnum>(s) << "|";
else if(s.type() == typeid(GMessage))
stream << boost::get<GMessage>(s) << "|";
return stream << "}";
}
BOOST_FUSION_ADAPT_STRUCT(GProtoFile,
(std::string, syntax)
(std::vector<GProtoFile::GProtoFileFieldVariant>, fields)
)
bool parse_proto(const std::string & str, GProtoFile & result);
bool parse_ident_split(const std::string & str, std::vector<std::string> & result);
void test_rules();