Parser will allow a table or vector to have a trailing comma.

Unless in --strict-json mode.
Also added strict_json option to the parser, which in
addition controls if field names without quotes are allowed.

Change-Id: Id56fe5c780bdb9170958050ffa8fa23cf2babe95
Tested: on Linux.
This commit is contained in:
Wouter van Oortmerssen
2015-01-16 16:57:04 -08:00
parent e568f17096
commit 6c2dc41e0d
6 changed files with 25 additions and 15 deletions

View File

@@ -102,7 +102,8 @@ static void Error(const char *err, const char *obj, bool usage,
printf(
" -o PATH Prefix PATH to all generated files.\n"
" -I PATH Search for includes in the specified path.\n"
" --strict-json Strict JSON: add quotes to field names.\n"
" --strict-json Strict JSON: field names must be / will be quoted,\n"
" no trailing commas in tables/vectors.\n"
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
" --gen-includes Generate include statements for included schemas the\n"
" generated file depends on (C++).\n"
@@ -174,7 +175,7 @@ int main(int argc, const char *argv[]) {
"specify one of -c -g -j -t -b etc.", true);
// Now process the files:
flatbuffers::Parser parser(proto_mode);
flatbuffers::Parser parser(opts.strict_json, proto_mode);
for (auto file_it = filenames.begin();
file_it != filenames.end();
++file_it) {

View File

@@ -480,9 +480,11 @@ void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
uoffset_t Parser::ParseTable(const StructDef &struct_def) {
Expect('{');
size_t fieldn = 0;
if (!IsNext('}')) for (;;) {
for (;;) {
if ((!strict_json_ || !fieldn) && IsNext('}')) break;
std::string name = attribute_;
if (!IsNext(kTokenStringConstant)) Expect(kTokenIdentifier);
if (!IsNext(kTokenStringConstant))
Expect(strict_json_ ? kTokenStringConstant : kTokenIdentifier);
auto field = struct_def.fields.Lookup(name);
if (!field) Error("unknown field: " + name);
if (struct_def.fixed && (fieldn >= struct_def.fields.vec.size()
@@ -574,16 +576,16 @@ uoffset_t Parser::ParseTable(const StructDef &struct_def) {
uoffset_t Parser::ParseVector(const Type &type) {
int count = 0;
if (token_ != ']') for (;;) {
for (;;) {
if ((!strict_json_ || !count) && IsNext(']')) break;
Value val;
val.type = type;
ParseAnyValue(val, NULL);
field_stack_.push_back(std::make_pair(val, nullptr));
count++;
if (token_ == ']') break;
if (IsNext(']')) break;
Expect(',');
}
Next();
builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
InlineAlignment(type));