Made .proto parsing understand nested declarations.

Bug: 24401812
Change-Id: I196a03b8c5ef0bcd3c26178239c764e40ca1950d
Tested: on Linux.
parent a35c32e3
...@@ -84,7 +84,7 @@ $(document).ready(function(){initNavTree('md__compiler.html','');}); ...@@ -84,7 +84,7 @@ $(document).ready(function(){initNavTree('md__compiler.html','');});
<li><code>--gen-mutable</code> : Generate additional non-const accessors for mutating FlatBuffers in-place.</li> <li><code>--gen-mutable</code> : Generate additional non-const accessors for mutating FlatBuffers in-place.</li>
<li><code>--gen-onefile</code> : Generate single output file (useful for C#)</li> <li><code>--gen-onefile</code> : Generate single output file (useful for C#)</li>
<li><code>--raw-binary</code> : Allow binaries without a file_indentifier to be read. This may crash flatc given a mismatched schema.</li> <li><code>--raw-binary</code> : Allow binaries without a file_indentifier to be read. This may crash flatc given a mismatched schema.</li>
<li><code>--proto</code>: Expect input files to be .proto files (protocol buffers). Output the corresponding .fbs file. Currently supports: <code>package</code>, <code>message</code>, <code>enum</code>. Does not support, but will skip without error: <code>import</code>, <code>option</code>. Does not support, will generate error: <code>service</code>, <code>extend</code>, <code>extensions</code>, <code>oneof</code>, <code>group</code>, custom options, nested declarations.</li> <li><code>--proto</code>: Expect input files to be .proto files (protocol buffers). Output the corresponding .fbs file. Currently supports: <code>package</code>, <code>message</code>, <code>enum</code>, nested declarations. Does not support, but will skip without error: <code>import</code>, <code>option</code>. Does not support, will generate error: <code>service</code>, <code>extend</code>, <code>extensions</code>, <code>oneof</code>, <code>group</code>, custom options.</li>
<li><code>--schema</code>: Serialize schemas instead of JSON (use with -b). This will output a binary version of the specified schema that itself corresponds to the reflection/reflection.fbs schema. Loading this binary file is the basis for reflection functionality. </li> <li><code>--schema</code>: Serialize schemas instead of JSON (use with -b). This will output a binary version of the specified schema that itself corresponds to the reflection/reflection.fbs schema. Loading this binary file is the basis for reflection functionality. </li>
</ul> </ul>
</div></div><!-- contents --> </div></div><!-- contents -->
......
...@@ -71,10 +71,10 @@ be generated for each file processed: ...@@ -71,10 +71,10 @@ be generated for each file processed:
- `--proto`: Expect input files to be .proto files (protocol buffers). - `--proto`: Expect input files to be .proto files (protocol buffers).
Output the corresponding .fbs file. Output the corresponding .fbs file.
Currently supports: `package`, `message`, `enum`. Currently supports: `package`, `message`, `enum`, nested declarations.
Does not support, but will skip without error: `import`, `option`. Does not support, but will skip without error: `import`, `option`.
Does not support, will generate error: `service`, `extend`, `extensions`, Does not support, will generate error: `service`, `extend`, `extensions`,
`oneof`, `group`, custom options, nested declarations. `oneof`, `group`, custom options.
- `--schema`: Serialize schemas instead of JSON (use with -b). This will - `--schema`: Serialize schemas instead of JSON (use with -b). This will
output a binary version of the specified schema that itself corresponds output a binary version of the specified schema that itself corresponds
......
...@@ -1052,45 +1052,50 @@ void Parser::ParseProtoDecl() { ...@@ -1052,45 +1052,50 @@ void Parser::ParseProtoDecl() {
struct_def.doc_comment = struct_comment; struct_def.doc_comment = struct_comment;
Expect('{'); Expect('{');
while (token_ != '}') { while (token_ != '}') {
std::vector<std::string> field_comment = doc_comment_; if (attribute_ == "message" || attribute_ == "enum") {
// Parse the qualifier. // Nested declarations.
bool required = false; ParseProtoDecl();
bool repeated = false;
if (attribute_ == "optional") {
// This is the default.
} else if (attribute_ == "required") {
required = true;
} else if (attribute_ == "repeated") {
repeated = true;
} else { } else {
Error("expecting optional/required/repeated, got: " + attribute_); std::vector<std::string> field_comment = doc_comment_;
} // Parse the qualifier.
Type type = ParseTypeFromProtoType(); bool required = false;
// Repeated elements get mapped to a vector. bool repeated = false;
if (repeated) { if (attribute_ == "optional") {
type.element = type.base_type; // This is the default.
type.base_type = BASE_TYPE_VECTOR; } else if (attribute_ == "required") {
} required = true;
std::string name = attribute_; } else if (attribute_ == "repeated") {
Expect(kTokenIdentifier); repeated = true;
// Parse the field id. Since we're just translating schemas, not } else {
// any kind of binary compatibility, we can safely ignore these, and Error("expecting optional/required/repeated, got: " + attribute_);
// assign our own. }
Expect('='); Type type = ParseTypeFromProtoType();
Expect(kTokenIntegerConstant); // Repeated elements get mapped to a vector.
auto &field = AddField(struct_def, name, type); if (repeated) {
field.doc_comment = field_comment; type.element = type.base_type;
if (!IsScalar(type.base_type)) field.required = required; type.base_type = BASE_TYPE_VECTOR;
// See if there's a default specified. }
if (IsNext('[')) { std::string name = attribute_;
if (attribute_ != "default") Error("\'default\' expected"); Expect(kTokenIdentifier);
Next(); // Parse the field id. Since we're just translating schemas, not
// any kind of binary compatibility, we can safely ignore these, and
// assign our own.
Expect('='); Expect('=');
field.value.constant = attribute_; Expect(kTokenIntegerConstant);
Next(); auto &field = AddField(struct_def, name, type);
Expect(']'); field.doc_comment = field_comment;
if (!IsScalar(type.base_type)) field.required = required;
// See if there's a default specified.
if (IsNext('[')) {
if (attribute_ != "default") Error("\'default\' expected");
Next();
Expect('=');
field.value.constant = attribute_;
Next();
Expect(']');
}
Expect(';');
} }
Expect(';');
} }
Next(); Next();
} else if (attribute_ == "enum") { } else if (attribute_ == "enum") {
......
...@@ -9,12 +9,6 @@ enum ProtoEnum : short { ...@@ -9,12 +9,6 @@ enum ProtoEnum : short {
BAR = 5, BAR = 5,
} }
table OtherMessage {
a:double;
/// doc comment for b.
b:float = 3.14149;
}
/// 2nd table doc comment with /// 2nd table doc comment with
/// many lines. /// many lines.
table ProtoMessage { table ProtoMessage {
...@@ -39,3 +33,9 @@ table ProtoMessage { ...@@ -39,3 +33,9 @@ table ProtoMessage {
o:[string]; o:[string];
} }
table OtherMessage {
a:double;
/// doc comment for b.
b:float = 3.14149;
}
...@@ -12,16 +12,16 @@ enum ProtoEnum { ...@@ -12,16 +12,16 @@ enum ProtoEnum {
BAR = 5; BAR = 5;
} }
// Ignored non-doc comment.
message OtherMessage {
optional double a = 26;
/// doc comment for b.
optional float b = 32 [default = 3.14149];
}
/// 2nd table doc comment with /// 2nd table doc comment with
/// many lines. /// many lines.
message ProtoMessage { message ProtoMessage {
// Ignored non-doc comment.
// A nested message declaration, will be moved to top level in .fbs
message OtherMessage {
optional double a = 26;
/// doc comment for b.
optional float b = 32 [default = 3.14149];
}
optional int32 c = 12 [default = 16]; optional int32 c = 12 [default = 16];
optional int64 d = 1 [default = 0]; optional int64 d = 1 [default = 0];
optional uint32 p = 1; optional uint32 p = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment