Initial support for parsing (and generating) Protobuf ASCII.

Change-Id: I955b4b3eed27f26773d7dc0acceff13c88d1333d
Tested: on Linux.
This commit is contained in:
Wouter van Oortmerssen
2017-06-16 11:57:58 -07:00
parent 88a85ffbbd
commit f325cce6fd
4 changed files with 39 additions and 7 deletions

View File

@@ -80,7 +80,7 @@ template<typename T> bool PrintVector(const Vector<T> &v, Type type,
text += NewLine(opts);
for (uoffset_t i = 0; i < v.size(); i++) {
if (i) {
text += ",";
if (!opts.protobuf_ascii_alike) text += ",";
text += NewLine(opts);
}
text.append(indent + Indent(opts), ' ');
@@ -207,12 +207,15 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
!fd.deprecated;
if (is_present || output_anyway) {
if (fieldout++) {
text += ",";
if (!opts.protobuf_ascii_alike) text += ",";
}
text += NewLine(opts);
text.append(indent + Indent(opts), ' ');
OutputIdentifier(fd.name, opts, _text);
text += ": ";
if (!opts.protobuf_ascii_alike ||
(fd.value.type.base_type != BASE_TYPE_STRUCT &&
fd.value.type.base_type != BASE_TYPE_VECTOR)) text += ":";
text += " ";
if (is_present) {
switch (fd.value.type.base_type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \

View File

@@ -758,6 +758,11 @@ CheckedError Parser::ParseString(Value &val) {
return NoError();
}
CheckedError Parser::ParseComma() {
if (!opts.protobuf_ascii_alike) EXPECT(',');
return NoError();
}
CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
size_t parent_fieldn,
const StructDef *parent_struct_def) {
@@ -786,7 +791,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
// Remember where we are in the source file, so we can come back here.
auto backup = *static_cast<ParserState *>(this);
ECHECK(SkipAnyJsonValue()); // The table.
EXPECT(',');
ECHECK(ParseComma());
auto next_name = attribute_;
if (Is(kTokenStringConstant)) {
NEXT();
@@ -891,11 +896,11 @@ CheckedError Parser::ParseTableDelimiters(size_t &fieldn,
} else {
EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier);
}
EXPECT(':');
if (!opts.protobuf_ascii_alike || !(Is('{') || Is('['))) EXPECT(':');
}
ECHECK(body(name));
if (Is(terminator)) break;
EXPECT(',');
ECHECK(ParseComma());
}
NEXT();
if (is_nested_vector && fieldn != struct_def->fields.vec.size()) {
@@ -1056,7 +1061,7 @@ CheckedError Parser::ParseVectorDelimiters(size_t &count,
ECHECK(body());
count++;
if (Is(']')) break;
EXPECT(',');
ECHECK(ParseComma());
}
NEXT();
return NoError();