~verterok/ubuntu/lucid/protobuf/2.4.0a-backport

« back to all changes in this revision

Viewing changes to src/google/protobuf/compiler/parser.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-31 14:41:47 UTC
  • mfrom: (2.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110531144147-s41g5fozgvyo462l
Tags: 2.4.0a-2ubuntu1
* Merge with Debian; remaining changes:
  - Fix linking with -lpthread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
250
250
  AddError(input_->current().line, input_->current().column, error);
251
251
}
252
252
 
253
 
void Parser::RecordLocation(
254
 
    const Message* descriptor,
255
 
    DescriptorPool::ErrorCollector::ErrorLocation location,
256
 
    int line, int column) {
257
 
  if (source_location_table_ != NULL) {
258
 
    source_location_table_->Add(descriptor, location, line, column);
259
 
  }
260
 
}
261
 
 
262
 
void Parser::RecordLocation(
263
 
    const Message* descriptor,
 
253
// -------------------------------------------------------------------
 
254
 
 
255
Parser::LocationRecorder::LocationRecorder(Parser* parser)
 
256
  : parser_(parser),
 
257
    location_(parser_->source_code_info_->add_location()) {
 
258
  location_->add_span(parser_->input_->current().line);
 
259
  location_->add_span(parser_->input_->current().column);
 
260
}
 
261
 
 
262
Parser::LocationRecorder::LocationRecorder(const LocationRecorder& parent) {
 
263
  Init(parent);
 
264
}
 
265
 
 
266
Parser::LocationRecorder::LocationRecorder(const LocationRecorder& parent,
 
267
                                           int path1) {
 
268
  Init(parent);
 
269
  AddPath(path1);
 
270
}
 
271
 
 
272
Parser::LocationRecorder::LocationRecorder(const LocationRecorder& parent,
 
273
                                           int path1, int path2) {
 
274
  Init(parent);
 
275
  AddPath(path1);
 
276
  AddPath(path2);
 
277
}
 
278
 
 
279
void Parser::LocationRecorder::Init(const LocationRecorder& parent) {
 
280
  parser_ = parent.parser_;
 
281
  location_ = parser_->source_code_info_->add_location();
 
282
  location_->mutable_path()->CopyFrom(parent.location_->path());
 
283
 
 
284
  location_->add_span(parser_->input_->current().line);
 
285
  location_->add_span(parser_->input_->current().column);
 
286
}
 
287
 
 
288
Parser::LocationRecorder::~LocationRecorder() {
 
289
  if (location_->span_size() <= 2) {
 
290
    EndAt(parser_->input_->previous());
 
291
  }
 
292
}
 
293
 
 
294
void Parser::LocationRecorder::AddPath(int path_component) {
 
295
  location_->add_path(path_component);
 
296
}
 
297
 
 
298
void Parser::LocationRecorder::StartAt(const io::Tokenizer::Token& token) {
 
299
  location_->set_span(0, token.line);
 
300
  location_->set_span(1, token.column);
 
301
}
 
302
 
 
303
void Parser::LocationRecorder::EndAt(const io::Tokenizer::Token& token) {
 
304
  if (token.line != location_->span(0)) {
 
305
    location_->add_span(token.line);
 
306
  }
 
307
  location_->add_span(token.end_column);
 
308
}
 
309
 
 
310
void Parser::LocationRecorder::RecordLegacyLocation(const Message* descriptor,
264
311
    DescriptorPool::ErrorCollector::ErrorLocation location) {
265
 
  RecordLocation(descriptor, location,
266
 
                 input_->current().line, input_->current().column);
 
312
  if (parser_->source_location_table_ != NULL) {
 
313
    parser_->source_location_table_->Add(
 
314
        descriptor, location, location_->span(0), location_->span(1));
 
315
  }
267
316
}
268
317
 
269
318
// -------------------------------------------------------------------
308
357
  had_errors_ = false;
309
358
  syntax_identifier_.clear();
310
359
 
 
360
  // Note that |file| could be NULL at this point if
 
361
  // stop_after_syntax_identifier_ is true.  So, we conservatively allocate
 
362
  // SourceCodeInfo on the stack, then swap it into the FileDescriptorProto
 
363
  // later on.
 
364
  SourceCodeInfo source_code_info;
 
365
  source_code_info_ = &source_code_info;
 
366
 
311
367
  if (LookingAtType(io::Tokenizer::TYPE_START)) {
312
368
    // Advance to first token.
313
369
    input_->Next();
314
370
  }
315
371
 
316
 
  if (require_syntax_identifier_ || LookingAt("syntax")) {
317
 
    if (!ParseSyntaxIdentifier()) {
318
 
      // Don't attempt to parse the file if we didn't recognize the syntax
319
 
      // identifier.
320
 
      return false;
 
372
  {
 
373
    LocationRecorder root_location(this);
 
374
 
 
375
    if (require_syntax_identifier_ || LookingAt("syntax")) {
 
376
      if (!ParseSyntaxIdentifier()) {
 
377
        // Don't attempt to parse the file if we didn't recognize the syntax
 
378
        // identifier.
 
379
        return false;
 
380
      }
 
381
    } else if (!stop_after_syntax_identifier_) {
 
382
      syntax_identifier_ = "proto2";
321
383
    }
322
 
  } else if (!stop_after_syntax_identifier_) {
323
 
    syntax_identifier_ = "proto2";
324
 
  }
325
 
 
326
 
  if (stop_after_syntax_identifier_) return !had_errors_;
327
 
 
328
 
  // Repeatedly parse statements until we reach the end of the file.
329
 
  while (!AtEnd()) {
330
 
    if (!ParseTopLevelStatement(file)) {
331
 
      // This statement failed to parse.  Skip it, but keep looping to parse
332
 
      // other statements.
333
 
      SkipStatement();
334
 
 
335
 
      if (LookingAt("}")) {
336
 
        AddError("Unmatched \"}\".");
337
 
        input_->Next();
 
384
 
 
385
    if (stop_after_syntax_identifier_) return !had_errors_;
 
386
 
 
387
    // Repeatedly parse statements until we reach the end of the file.
 
388
    while (!AtEnd()) {
 
389
      if (!ParseTopLevelStatement(file, root_location)) {
 
390
        // This statement failed to parse.  Skip it, but keep looping to parse
 
391
        // other statements.
 
392
        SkipStatement();
 
393
 
 
394
        if (LookingAt("}")) {
 
395
          AddError("Unmatched \"}\".");
 
396
          input_->Next();
 
397
        }
338
398
      }
339
399
    }
340
400
  }
341
401
 
342
402
  input_ = NULL;
 
403
  source_code_info_ = NULL;
 
404
  source_code_info.Swap(file->mutable_source_code_info());
343
405
  return !had_errors_;
344
406
}
345
407
 
363
425
  return true;
364
426
}
365
427
 
366
 
bool Parser::ParseTopLevelStatement(FileDescriptorProto* file) {
 
428
bool Parser::ParseTopLevelStatement(FileDescriptorProto* file,
 
429
                                    const LocationRecorder& root_location) {
367
430
  if (TryConsume(";")) {
368
431
    // empty statement; ignore
369
432
    return true;
370
433
  } else if (LookingAt("message")) {
371
 
    return ParseMessageDefinition(file->add_message_type());
 
434
    LocationRecorder location(root_location,
 
435
      FileDescriptorProto::kMessageTypeFieldNumber, file->message_type_size());
 
436
    return ParseMessageDefinition(file->add_message_type(), location);
372
437
  } else if (LookingAt("enum")) {
373
 
    return ParseEnumDefinition(file->add_enum_type());
 
438
    LocationRecorder location(root_location,
 
439
      FileDescriptorProto::kEnumTypeFieldNumber, file->enum_type_size());
 
440
    return ParseEnumDefinition(file->add_enum_type(), location);
374
441
  } else if (LookingAt("service")) {
375
 
    return ParseServiceDefinition(file->add_service());
 
442
    LocationRecorder location(root_location,
 
443
      FileDescriptorProto::kServiceFieldNumber, file->service_size());
 
444
    return ParseServiceDefinition(file->add_service(), location);
376
445
  } else if (LookingAt("extend")) {
 
446
    LocationRecorder location(root_location,
 
447
        FileDescriptorProto::kExtensionFieldNumber);
377
448
    return ParseExtend(file->mutable_extension(),
378
 
                       file->mutable_message_type());
 
449
                       file->mutable_message_type(),
 
450
                       root_location,
 
451
                       FileDescriptorProto::kMessageTypeFieldNumber,
 
452
                       location);
379
453
  } else if (LookingAt("import")) {
380
 
    return ParseImport(file->add_dependency());
 
454
    int index = file->dependency_size();
 
455
    return ParseImport(file->add_dependency(), root_location, index);
381
456
  } else if (LookingAt("package")) {
382
 
    return ParsePackage(file);
 
457
    return ParsePackage(file, root_location);
383
458
  } else if (LookingAt("option")) {
384
 
    return ParseOption(file->mutable_options());
 
459
    LocationRecorder location(root_location,
 
460
        FileDescriptorProto::kOptionsFieldNumber);
 
461
    return ParseOption(file->mutable_options(), location);
385
462
  } else {
386
463
    AddError("Expected top-level statement (e.g. \"message\").");
387
464
    return false;
391
468
// -------------------------------------------------------------------
392
469
// Messages
393
470
 
394
 
bool Parser::ParseMessageDefinition(DescriptorProto* message) {
 
471
bool Parser::ParseMessageDefinition(DescriptorProto* message,
 
472
                                    const LocationRecorder& message_location) {
395
473
  DO(Consume("message"));
396
 
  RecordLocation(message, DescriptorPool::ErrorCollector::NAME);
397
 
  DO(ConsumeIdentifier(message->mutable_name(), "Expected message name."));
398
 
  DO(ParseMessageBlock(message));
 
474
  {
 
475
    LocationRecorder location(message_location,
 
476
                              DescriptorProto::kNameFieldNumber);
 
477
    location.RecordLegacyLocation(
 
478
        message, DescriptorPool::ErrorCollector::NAME);
 
479
    DO(ConsumeIdentifier(message->mutable_name(), "Expected message name."));
 
480
  }
 
481
  DO(ParseMessageBlock(message, message_location));
399
482
  return true;
400
483
}
401
484
 
402
 
bool Parser::ParseMessageBlock(DescriptorProto* message) {
 
485
bool Parser::ParseMessageBlock(DescriptorProto* message,
 
486
                               const LocationRecorder& message_location) {
403
487
  DO(Consume("{"));
404
488
 
405
489
  while (!TryConsume("}")) {
408
492
      return false;
409
493
    }
410
494
 
411
 
    if (!ParseMessageStatement(message)) {
 
495
    if (!ParseMessageStatement(message, message_location)) {
412
496
      // This statement failed to parse.  Skip it, but keep looping to parse
413
497
      // other statements.
414
498
      SkipStatement();
418
502
  return true;
419
503
}
420
504
 
421
 
bool Parser::ParseMessageStatement(DescriptorProto* message) {
 
505
bool Parser::ParseMessageStatement(DescriptorProto* message,
 
506
                                   const LocationRecorder& message_location) {
422
507
  if (TryConsume(";")) {
423
508
    // empty statement; ignore
424
509
    return true;
425
510
  } else if (LookingAt("message")) {
426
 
    return ParseMessageDefinition(message->add_nested_type());
 
511
    LocationRecorder location(message_location,
 
512
                              DescriptorProto::kNestedTypeFieldNumber,
 
513
                              message->nested_type_size());
 
514
    return ParseMessageDefinition(message->add_nested_type(), location);
427
515
  } else if (LookingAt("enum")) {
428
 
    return ParseEnumDefinition(message->add_enum_type());
 
516
    LocationRecorder location(message_location,
 
517
                              DescriptorProto::kEnumTypeFieldNumber,
 
518
                              message->enum_type_size());
 
519
    return ParseEnumDefinition(message->add_enum_type(), location);
429
520
  } else if (LookingAt("extensions")) {
430
 
    return ParseExtensions(message);
 
521
    LocationRecorder location(message_location,
 
522
                              DescriptorProto::kExtensionRangeFieldNumber);
 
523
    return ParseExtensions(message, location);
431
524
  } else if (LookingAt("extend")) {
 
525
    LocationRecorder location(message_location,
 
526
                              DescriptorProto::kExtensionFieldNumber);
432
527
    return ParseExtend(message->mutable_extension(),
433
 
                       message->mutable_nested_type());
 
528
                       message->mutable_nested_type(),
 
529
                       message_location,
 
530
                       DescriptorProto::kNestedTypeFieldNumber,
 
531
                       location);
434
532
  } else if (LookingAt("option")) {
435
 
    return ParseOption(message->mutable_options());
 
533
    LocationRecorder location(message_location,
 
534
                              DescriptorProto::kOptionsFieldNumber);
 
535
    return ParseOption(message->mutable_options(), location);
436
536
  } else {
 
537
    LocationRecorder location(message_location,
 
538
                              DescriptorProto::kFieldFieldNumber,
 
539
                              message->field_size());
437
540
    return ParseMessageField(message->add_field(),
438
 
                             message->mutable_nested_type());
 
541
                             message->mutable_nested_type(),
 
542
                             message_location,
 
543
                             DescriptorProto::kNestedTypeFieldNumber,
 
544
                             location);
439
545
  }
440
546
}
441
547
 
442
548
bool Parser::ParseMessageField(FieldDescriptorProto* field,
443
 
                               RepeatedPtrField<DescriptorProto>* messages) {
 
549
                               RepeatedPtrField<DescriptorProto>* messages,
 
550
                               const LocationRecorder& parent_location,
 
551
                               int location_field_number_for_nested_type,
 
552
                               const LocationRecorder& field_location) {
444
553
  // Parse label and type.
445
 
  FieldDescriptorProto::Label label;
446
 
  DO(ParseLabel(&label));
447
 
  field->set_label(label);
448
 
 
449
 
  RecordLocation(field, DescriptorPool::ErrorCollector::TYPE);
450
 
  FieldDescriptorProto::Type type = FieldDescriptorProto::TYPE_INT32;
451
 
  string type_name;
452
 
  DO(ParseType(&type, &type_name));
453
 
  if (type_name.empty()) {
454
 
    field->set_type(type);
455
 
  } else {
456
 
    field->set_type_name(type_name);
 
554
  io::Tokenizer::Token label_token = input_->current();
 
555
  {
 
556
    LocationRecorder location(field_location,
 
557
                              FieldDescriptorProto::kLabelFieldNumber);
 
558
    FieldDescriptorProto::Label label;
 
559
    DO(ParseLabel(&label));
 
560
    field->set_label(label);
 
561
  }
 
562
 
 
563
  {
 
564
    LocationRecorder location(field_location);  // add path later
 
565
    location.RecordLegacyLocation(field, DescriptorPool::ErrorCollector::TYPE);
 
566
 
 
567
    FieldDescriptorProto::Type type = FieldDescriptorProto::TYPE_INT32;
 
568
    string type_name;
 
569
    DO(ParseType(&type, &type_name));
 
570
    if (type_name.empty()) {
 
571
      location.AddPath(FieldDescriptorProto::kTypeFieldNumber);
 
572
      field->set_type(type);
 
573
    } else {
 
574
      location.AddPath(FieldDescriptorProto::kTypeNameFieldNumber);
 
575
      field->set_type_name(type_name);
 
576
    }
457
577
  }
458
578
 
459
579
  // Parse name and '='.
460
 
  RecordLocation(field, DescriptorPool::ErrorCollector::NAME);
461
580
  io::Tokenizer::Token name_token = input_->current();
462
 
  DO(ConsumeIdentifier(field->mutable_name(), "Expected field name."));
 
581
  {
 
582
    LocationRecorder location(field_location,
 
583
                              FieldDescriptorProto::kNameFieldNumber);
 
584
    location.RecordLegacyLocation(field, DescriptorPool::ErrorCollector::NAME);
 
585
    DO(ConsumeIdentifier(field->mutable_name(), "Expected field name."));
 
586
  }
463
587
  DO(Consume("=", "Missing field number."));
464
588
 
465
589
  // Parse field number.
466
 
  RecordLocation(field, DescriptorPool::ErrorCollector::NUMBER);
467
 
  int number;
468
 
  DO(ConsumeInteger(&number, "Expected field number."));
469
 
  field->set_number(number);
 
590
  {
 
591
    LocationRecorder location(field_location,
 
592
                              FieldDescriptorProto::kNumberFieldNumber);
 
593
    location.RecordLegacyLocation(
 
594
        field, DescriptorPool::ErrorCollector::NUMBER);
 
595
    int number;
 
596
    DO(ConsumeInteger(&number, "Expected field number."));
 
597
    field->set_number(number);
 
598
  }
470
599
 
471
600
  // Parse options.
472
 
  DO(ParseFieldOptions(field));
 
601
  DO(ParseFieldOptions(field, field_location));
473
602
 
474
603
  // Deal with groups.
475
 
  if (type_name.empty() && type == FieldDescriptorProto::TYPE_GROUP) {
 
604
  if (field->has_type() && field->type() == FieldDescriptorProto::TYPE_GROUP) {
 
605
    // Awkward:  Since a group declares both a message type and a field, we
 
606
    //   have to create overlapping locations.
 
607
    LocationRecorder group_location(parent_location);
 
608
    group_location.StartAt(label_token);
 
609
    group_location.AddPath(location_field_number_for_nested_type);
 
610
    group_location.AddPath(messages->size());
 
611
 
476
612
    DescriptorProto* group = messages->Add();
477
613
    group->set_name(field->name());
 
614
 
478
615
    // Record name location to match the field name's location.
479
 
    RecordLocation(group, DescriptorPool::ErrorCollector::NAME,
480
 
                   name_token.line, name_token.column);
 
616
    {
 
617
      LocationRecorder location(group_location,
 
618
                                DescriptorProto::kNameFieldNumber);
 
619
      location.StartAt(name_token);
 
620
      location.EndAt(name_token);
 
621
      location.RecordLegacyLocation(
 
622
          group, DescriptorPool::ErrorCollector::NAME);
 
623
    }
 
624
 
 
625
    // The field's type_name also comes from the name.  Confusing!
 
626
    {
 
627
      LocationRecorder location(field_location,
 
628
                                FieldDescriptorProto::kTypeNameFieldNumber);
 
629
      location.StartAt(name_token);
 
630
      location.EndAt(name_token);
 
631
    }
481
632
 
482
633
    // As a hack for backwards-compatibility, we force the group name to start
483
634
    // with a capital letter and lower-case the field name.  New code should
490
641
 
491
642
    field->set_type_name(group->name());
492
643
    if (LookingAt("{")) {
493
 
      DO(ParseMessageBlock(group));
 
644
      DO(ParseMessageBlock(group, group_location));
494
645
    } else {
495
646
      AddError("Missing group body.");
496
647
      return false;
502
653
  return true;
503
654
}
504
655
 
505
 
bool Parser::ParseFieldOptions(FieldDescriptorProto* field) {
506
 
  if (!TryConsume("[")) return true;
 
656
bool Parser::ParseFieldOptions(FieldDescriptorProto* field,
 
657
                               const LocationRecorder& field_location) {
 
658
  if (!LookingAt("[")) return true;
 
659
 
 
660
  LocationRecorder location(field_location,
 
661
                            FieldDescriptorProto::kOptionsFieldNumber);
 
662
 
 
663
  DO(Consume("["));
507
664
 
508
665
  // Parse field options.
509
666
  do {
510
667
    if (LookingAt("default")) {
511
 
      DO(ParseDefaultAssignment(field));
 
668
      // We intentionally pass field_location rather than location here, since
 
669
      // the default value is not actually an option.
 
670
      DO(ParseDefaultAssignment(field, field_location));
512
671
    } else {
513
 
      DO(ParseOptionAssignment(field->mutable_options()));
 
672
      DO(ParseOptionAssignment(field->mutable_options(), location));
514
673
    }
515
674
  } while (TryConsume(","));
516
675
 
518
677
  return true;
519
678
}
520
679
 
521
 
bool Parser::ParseDefaultAssignment(FieldDescriptorProto* field) {
 
680
bool Parser::ParseDefaultAssignment(FieldDescriptorProto* field,
 
681
                                    const LocationRecorder& field_location) {
522
682
  if (field->has_default_value()) {
523
683
    AddError("Already set option \"default\".");
524
684
    field->clear_default_value();
527
687
  DO(Consume("default"));
528
688
  DO(Consume("="));
529
689
 
530
 
  RecordLocation(field, DescriptorPool::ErrorCollector::DEFAULT_VALUE);
 
690
  LocationRecorder location(field_location,
 
691
                            FieldDescriptorProto::kDefaultValueFieldNumber);
 
692
  location.RecordLegacyLocation(
 
693
      field, DescriptorPool::ErrorCollector::DEFAULT_VALUE);
531
694
  string* default_value = field->mutable_default_value();
532
695
 
533
696
  if (!field->has_type()) {
634
797
  return true;
635
798
}
636
799
 
637
 
bool Parser::ParseOptionNamePart(UninterpretedOption* uninterpreted_option) {
 
800
bool Parser::ParseOptionNamePart(UninterpretedOption* uninterpreted_option,
 
801
                                 const LocationRecorder& part_location) {
638
802
  UninterpretedOption::NamePart* name = uninterpreted_option->add_name();
639
803
  string identifier;  // We parse identifiers into this string.
640
804
  if (LookingAt("(")) {  // This is an extension.
641
805
    DO(Consume("("));
642
 
    // An extension name consists of dot-separated identifiers, and may begin
643
 
    // with a dot.
644
 
    if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
645
 
      DO(ConsumeIdentifier(&identifier, "Expected identifier."));
646
 
      name->mutable_name_part()->append(identifier);
647
 
    }
648
 
    while (LookingAt(".")) {
649
 
      DO(Consume("."));
650
 
      name->mutable_name_part()->append(".");
651
 
      DO(ConsumeIdentifier(&identifier, "Expected identifier."));
652
 
      name->mutable_name_part()->append(identifier);
653
 
    }
 
806
 
 
807
    {
 
808
      LocationRecorder location(
 
809
          part_location, UninterpretedOption::NamePart::kNamePartFieldNumber);
 
810
      // An extension name consists of dot-separated identifiers, and may begin
 
811
      // with a dot.
 
812
      if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
 
813
        DO(ConsumeIdentifier(&identifier, "Expected identifier."));
 
814
        name->mutable_name_part()->append(identifier);
 
815
      }
 
816
      while (LookingAt(".")) {
 
817
        DO(Consume("."));
 
818
        name->mutable_name_part()->append(".");
 
819
        DO(ConsumeIdentifier(&identifier, "Expected identifier."));
 
820
        name->mutable_name_part()->append(identifier);
 
821
      }
 
822
    }
 
823
 
654
824
    DO(Consume(")"));
655
825
    name->set_is_extension(true);
656
826
  } else {  // This is a regular field.
 
827
    LocationRecorder location(
 
828
        part_location, UninterpretedOption::NamePart::kNamePartFieldNumber);
657
829
    DO(ConsumeIdentifier(&identifier, "Expected identifier."));
658
830
    name->mutable_name_part()->append(identifier);
659
831
    name->set_is_extension(false);
661
833
  return true;
662
834
}
663
835
 
 
836
bool Parser::ParseUninterpretedBlock(string* value) {
 
837
  // Note that enclosing braces are not added to *value.
 
838
  DO(Consume("{"));
 
839
  int brace_depth = 1;
 
840
  while (!AtEnd()) {
 
841
    if (LookingAt("{")) {
 
842
      brace_depth++;
 
843
    } else if (LookingAt("}")) {
 
844
      brace_depth--;
 
845
      if (brace_depth == 0) {
 
846
        input_->Next();
 
847
        return true;
 
848
      }
 
849
    }
 
850
    // TODO(sanjay): Interpret line/column numbers to preserve formatting
 
851
    if (!value->empty()) value->push_back(' ');
 
852
    value->append(input_->current().text);
 
853
    input_->Next();
 
854
  }
 
855
  AddError("Unexpected end of stream while parsing aggregate value.");
 
856
  return false;
 
857
}
 
858
 
664
859
// We don't interpret the option here. Instead we store it in an
665
860
// UninterpretedOption, to be interpreted later.
666
 
bool Parser::ParseOptionAssignment(Message* options) {
 
861
bool Parser::ParseOptionAssignment(Message* options,
 
862
                                   const LocationRecorder& options_location) {
667
863
  // Create an entry in the uninterpreted_option field.
668
864
  const FieldDescriptor* uninterpreted_option_field = options->GetDescriptor()->
669
865
      FindFieldByName("uninterpreted_option");
670
866
  GOOGLE_CHECK(uninterpreted_option_field != NULL)
671
867
      << "No field named \"uninterpreted_option\" in the Options proto.";
672
868
 
 
869
  const Reflection* reflection = options->GetReflection();
 
870
 
 
871
  LocationRecorder location(
 
872
      options_location, uninterpreted_option_field->number(),
 
873
      reflection->FieldSize(*options, uninterpreted_option_field));
 
874
 
673
875
  UninterpretedOption* uninterpreted_option = down_cast<UninterpretedOption*>(
674
876
      options->GetReflection()->AddMessage(options,
675
877
                                           uninterpreted_option_field));
676
878
 
677
879
  // Parse dot-separated name.
678
 
  RecordLocation(uninterpreted_option,
679
 
                 DescriptorPool::ErrorCollector::OPTION_NAME);
680
 
 
681
 
  DO(ParseOptionNamePart(uninterpreted_option));
682
 
 
683
 
  while (LookingAt(".")) {
684
 
    DO(Consume("."));
685
 
    DO(ParseOptionNamePart(uninterpreted_option));
 
880
  {
 
881
    LocationRecorder name_location(location,
 
882
                                   UninterpretedOption::kNameFieldNumber);
 
883
    name_location.RecordLegacyLocation(
 
884
        uninterpreted_option, DescriptorPool::ErrorCollector::OPTION_NAME);
 
885
 
 
886
    {
 
887
      LocationRecorder part_location(name_location,
 
888
                                     uninterpreted_option->name_size());
 
889
      DO(ParseOptionNamePart(uninterpreted_option, part_location));
 
890
    }
 
891
 
 
892
    while (LookingAt(".")) {
 
893
      DO(Consume("."));
 
894
      LocationRecorder part_location(name_location,
 
895
                                     uninterpreted_option->name_size());
 
896
      DO(ParseOptionNamePart(uninterpreted_option, part_location));
 
897
    }
686
898
  }
687
899
 
688
900
  DO(Consume("="));
689
901
 
690
 
  RecordLocation(uninterpreted_option,
691
 
                 DescriptorPool::ErrorCollector::OPTION_VALUE);
 
902
  LocationRecorder value_location(location);
 
903
  value_location.RecordLegacyLocation(
 
904
      uninterpreted_option, DescriptorPool::ErrorCollector::OPTION_VALUE);
692
905
 
693
906
  // All values are a single token, except for negative numbers, which consist
694
907
  // of a single '-' symbol, followed by a positive number.
704
917
      return false;
705
918
 
706
919
    case io::Tokenizer::TYPE_IDENTIFIER: {
 
920
      value_location.AddPath(UninterpretedOption::kIdentifierValueFieldNumber);
707
921
      if (is_negative) {
708
922
        AddError("Invalid '-' symbol before identifier.");
709
923
        return false;
720
934
          is_negative ? static_cast<uint64>(kint64max) + 1 : kuint64max;
721
935
      DO(ConsumeInteger64(max_value, &value, "Expected integer."));
722
936
      if (is_negative) {
723
 
        uninterpreted_option->set_negative_int_value(-value);
 
937
        value_location.AddPath(
 
938
            UninterpretedOption::kNegativeIntValueFieldNumber);
 
939
        uninterpreted_option->set_negative_int_value(-static_cast<int64>(value));
724
940
      } else {
 
941
        value_location.AddPath(
 
942
            UninterpretedOption::kPositiveIntValueFieldNumber);
725
943
        uninterpreted_option->set_positive_int_value(value);
726
944
      }
727
945
      break;
728
946
    }
729
947
 
730
948
    case io::Tokenizer::TYPE_FLOAT: {
 
949
      value_location.AddPath(UninterpretedOption::kDoubleValueFieldNumber);
731
950
      double value;
732
951
      DO(ConsumeNumber(&value, "Expected number."));
733
952
      uninterpreted_option->set_double_value(is_negative ? -value : value);
735
954
    }
736
955
 
737
956
    case io::Tokenizer::TYPE_STRING: {
 
957
      value_location.AddPath(UninterpretedOption::kStringValueFieldNumber);
738
958
      if (is_negative) {
739
959
        AddError("Invalid '-' symbol before string.");
740
960
        return false;
746
966
    }
747
967
 
748
968
    case io::Tokenizer::TYPE_SYMBOL:
749
 
      AddError("Expected option value.");
750
 
      return false;
 
969
      if (LookingAt("{")) {
 
970
        value_location.AddPath(UninterpretedOption::kAggregateValueFieldNumber);
 
971
        DO(ParseUninterpretedBlock(
 
972
            uninterpreted_option->mutable_aggregate_value()));
 
973
      } else {
 
974
        AddError("Expected option value.");
 
975
        return false;
 
976
      }
 
977
      break;
751
978
  }
752
979
 
753
980
  return true;
754
981
}
755
982
 
756
 
bool Parser::ParseExtensions(DescriptorProto* message) {
 
983
bool Parser::ParseExtensions(DescriptorProto* message,
 
984
                             const LocationRecorder& extensions_location) {
757
985
  // Parse the declaration.
758
986
  DO(Consume("extensions"));
759
987
 
760
988
  do {
 
989
    // Note that kExtensionRangeFieldNumber was already pushed by the parent.
 
990
    LocationRecorder location(extensions_location,
 
991
                              message->extension_range_size());
 
992
 
761
993
    DescriptorProto::ExtensionRange* range = message->add_extension_range();
762
 
    RecordLocation(range, DescriptorPool::ErrorCollector::NUMBER);
 
994
    location.RecordLegacyLocation(
 
995
        range, DescriptorPool::ErrorCollector::NUMBER);
763
996
 
764
997
    int start, end;
765
 
    DO(ConsumeInteger(&start, "Expected field number range."));
 
998
    io::Tokenizer::Token start_token;
 
999
 
 
1000
    {
 
1001
      LocationRecorder start_location(
 
1002
          location, DescriptorProto::ExtensionRange::kStartFieldNumber);
 
1003
      start_token = input_->current();
 
1004
      DO(ConsumeInteger(&start, "Expected field number range."));
 
1005
    }
766
1006
 
767
1007
    if (TryConsume("to")) {
 
1008
      LocationRecorder end_location(
 
1009
          location, DescriptorProto::ExtensionRange::kEndFieldNumber);
768
1010
      if (TryConsume("max")) {
769
1011
        end = FieldDescriptor::kMaxNumber;
770
1012
      } else {
771
1013
        DO(ConsumeInteger(&end, "Expected integer."));
772
1014
      }
773
1015
    } else {
 
1016
      LocationRecorder end_location(
 
1017
          location, DescriptorProto::ExtensionRange::kEndFieldNumber);
 
1018
      end_location.StartAt(start_token);
 
1019
      end_location.EndAt(start_token);
774
1020
      end = start;
775
1021
    }
776
1022
 
787
1033
}
788
1034
 
789
1035
bool Parser::ParseExtend(RepeatedPtrField<FieldDescriptorProto>* extensions,
790
 
                         RepeatedPtrField<DescriptorProto>* messages) {
 
1036
                         RepeatedPtrField<DescriptorProto>* messages,
 
1037
                         const LocationRecorder& parent_location,
 
1038
                         int location_field_number_for_nested_type,
 
1039
                         const LocationRecorder& extend_location) {
791
1040
  DO(Consume("extend"));
792
1041
 
793
 
  // We expect to see at least one extension field defined in the extend block.
794
 
  // We need to create it now so we can record the extendee's location.
795
 
  FieldDescriptorProto* first_field = extensions->Add();
796
 
 
797
1042
  // Parse the extendee type.
798
 
  RecordLocation(first_field, DescriptorPool::ErrorCollector::EXTENDEE);
799
 
  DO(ParseUserDefinedType(first_field->mutable_extendee()));
 
1043
  io::Tokenizer::Token extendee_start = input_->current();
 
1044
  string extendee;
 
1045
  DO(ParseUserDefinedType(&extendee));
 
1046
  io::Tokenizer::Token extendee_end = input_->previous();
800
1047
 
801
1048
  // Parse the block.
802
1049
  DO(Consume("{"));
809
1056
      return false;
810
1057
    }
811
1058
 
812
 
    FieldDescriptorProto* field;
813
 
    if (is_first) {
814
 
      field = first_field;
815
 
      is_first = false;
816
 
    } else {
817
 
      field = extensions->Add();
818
 
      field->set_extendee(first_field->extendee());
 
1059
    // Note that kExtensionFieldNumber was already pushed by the parent.
 
1060
    LocationRecorder location(extend_location, extensions->size());
 
1061
 
 
1062
    FieldDescriptorProto* field = extensions->Add();
 
1063
 
 
1064
    {
 
1065
      LocationRecorder extendee_location(
 
1066
          location, FieldDescriptorProto::kExtendeeFieldNumber);
 
1067
      extendee_location.StartAt(extendee_start);
 
1068
      extendee_location.EndAt(extendee_end);
 
1069
 
 
1070
      if (is_first) {
 
1071
        extendee_location.RecordLegacyLocation(
 
1072
            field, DescriptorPool::ErrorCollector::EXTENDEE);
 
1073
        is_first = false;
 
1074
      }
819
1075
    }
820
1076
 
821
 
    if (!ParseMessageField(field, messages)) {
 
1077
    field->set_extendee(extendee);
 
1078
 
 
1079
    if (!ParseMessageField(field, messages, parent_location,
 
1080
                           location_field_number_for_nested_type,
 
1081
                           location)) {
822
1082
      // This statement failed to parse.  Skip it, but keep looping to parse
823
1083
      // other statements.
824
1084
      SkipStatement();
831
1091
// -------------------------------------------------------------------
832
1092
// Enums
833
1093
 
834
 
bool Parser::ParseEnumDefinition(EnumDescriptorProto* enum_type) {
 
1094
bool Parser::ParseEnumDefinition(EnumDescriptorProto* enum_type,
 
1095
                                 const LocationRecorder& enum_location) {
835
1096
  DO(Consume("enum"));
836
 
  RecordLocation(enum_type, DescriptorPool::ErrorCollector::NAME);
837
 
  DO(ConsumeIdentifier(enum_type->mutable_name(), "Expected enum name."));
838
 
  DO(ParseEnumBlock(enum_type));
 
1097
 
 
1098
  {
 
1099
    LocationRecorder location(enum_location,
 
1100
                              EnumDescriptorProto::kNameFieldNumber);
 
1101
    location.RecordLegacyLocation(
 
1102
        enum_type, DescriptorPool::ErrorCollector::NAME);
 
1103
    DO(ConsumeIdentifier(enum_type->mutable_name(), "Expected enum name."));
 
1104
  }
 
1105
 
 
1106
  DO(ParseEnumBlock(enum_type, enum_location));
839
1107
  return true;
840
1108
}
841
1109
 
842
 
bool Parser::ParseEnumBlock(EnumDescriptorProto* enum_type) {
 
1110
bool Parser::ParseEnumBlock(EnumDescriptorProto* enum_type,
 
1111
                            const LocationRecorder& enum_location) {
843
1112
  DO(Consume("{"));
844
1113
 
845
1114
  while (!TryConsume("}")) {
848
1117
      return false;
849
1118
    }
850
1119
 
851
 
    if (!ParseEnumStatement(enum_type)) {
 
1120
    if (!ParseEnumStatement(enum_type, enum_location)) {
852
1121
      // This statement failed to parse.  Skip it, but keep looping to parse
853
1122
      // other statements.
854
1123
      SkipStatement();
858
1127
  return true;
859
1128
}
860
1129
 
861
 
bool Parser::ParseEnumStatement(EnumDescriptorProto* enum_type) {
 
1130
bool Parser::ParseEnumStatement(EnumDescriptorProto* enum_type,
 
1131
                                const LocationRecorder& enum_location) {
862
1132
  if (TryConsume(";")) {
863
1133
    // empty statement; ignore
864
1134
    return true;
865
1135
  } else if (LookingAt("option")) {
866
 
    return ParseOption(enum_type->mutable_options());
 
1136
    LocationRecorder location(enum_location,
 
1137
                              EnumDescriptorProto::kOptionsFieldNumber);
 
1138
    return ParseOption(enum_type->mutable_options(), location);
867
1139
  } else {
868
 
    return ParseEnumConstant(enum_type->add_value());
 
1140
    LocationRecorder location(enum_location,
 
1141
        EnumDescriptorProto::kValueFieldNumber, enum_type->value_size());
 
1142
    return ParseEnumConstant(enum_type->add_value(), location);
869
1143
  }
870
1144
}
871
1145
 
872
 
bool Parser::ParseEnumConstant(EnumValueDescriptorProto* enum_value) {
873
 
  RecordLocation(enum_value, DescriptorPool::ErrorCollector::NAME);
874
 
  DO(ConsumeIdentifier(enum_value->mutable_name(),
875
 
                       "Expected enum constant name."));
 
1146
bool Parser::ParseEnumConstant(EnumValueDescriptorProto* enum_value,
 
1147
                               const LocationRecorder& enum_value_location) {
 
1148
  // Parse name.
 
1149
  {
 
1150
    LocationRecorder location(enum_value_location,
 
1151
                              EnumValueDescriptorProto::kNameFieldNumber);
 
1152
    location.RecordLegacyLocation(
 
1153
        enum_value, DescriptorPool::ErrorCollector::NAME);
 
1154
    DO(ConsumeIdentifier(enum_value->mutable_name(),
 
1155
                         "Expected enum constant name."));
 
1156
  }
 
1157
 
876
1158
  DO(Consume("=", "Missing numeric value for enum constant."));
877
1159
 
878
 
  bool is_negative = TryConsume("-");
879
 
  int number;
880
 
  DO(ConsumeInteger(&number, "Expected integer."));
881
 
  if (is_negative) number *= -1;
882
 
  enum_value->set_number(number);
883
 
 
884
 
  DO(ParseEnumConstantOptions(enum_value));
 
1160
  // Parse value.
 
1161
  {
 
1162
    LocationRecorder location(
 
1163
        enum_value_location, EnumValueDescriptorProto::kNumberFieldNumber);
 
1164
    location.RecordLegacyLocation(
 
1165
        enum_value, DescriptorPool::ErrorCollector::NUMBER);
 
1166
 
 
1167
    bool is_negative = TryConsume("-");
 
1168
    int number;
 
1169
    DO(ConsumeInteger(&number, "Expected integer."));
 
1170
    if (is_negative) number *= -1;
 
1171
    enum_value->set_number(number);
 
1172
  }
 
1173
 
 
1174
  DO(ParseEnumConstantOptions(enum_value, enum_value_location));
885
1175
 
886
1176
  DO(Consume(";"));
887
1177
 
888
1178
  return true;
889
1179
}
890
1180
 
891
 
bool Parser::ParseEnumConstantOptions(EnumValueDescriptorProto* value) {
892
 
  if (!TryConsume("[")) return true;
 
1181
bool Parser::ParseEnumConstantOptions(
 
1182
    EnumValueDescriptorProto* value,
 
1183
    const LocationRecorder& enum_value_location) {
 
1184
  if (!LookingAt("[")) return true;
 
1185
 
 
1186
  LocationRecorder location(
 
1187
      enum_value_location, EnumValueDescriptorProto::kOptionsFieldNumber);
 
1188
 
 
1189
  DO(Consume("["));
893
1190
 
894
1191
  do {
895
 
    DO(ParseOptionAssignment(value->mutable_options()));
 
1192
    DO(ParseOptionAssignment(value->mutable_options(), location));
896
1193
  } while (TryConsume(","));
897
1194
 
898
1195
  DO(Consume("]"));
902
1199
// -------------------------------------------------------------------
903
1200
// Services
904
1201
 
905
 
bool Parser::ParseServiceDefinition(ServiceDescriptorProto* service) {
 
1202
bool Parser::ParseServiceDefinition(ServiceDescriptorProto* service,
 
1203
                                    const LocationRecorder& service_location) {
906
1204
  DO(Consume("service"));
907
 
  RecordLocation(service, DescriptorPool::ErrorCollector::NAME);
908
 
  DO(ConsumeIdentifier(service->mutable_name(), "Expected service name."));
909
 
  DO(ParseServiceBlock(service));
 
1205
 
 
1206
  {
 
1207
    LocationRecorder location(service_location,
 
1208
                              ServiceDescriptorProto::kNameFieldNumber);
 
1209
    location.RecordLegacyLocation(
 
1210
        service, DescriptorPool::ErrorCollector::NAME);
 
1211
    DO(ConsumeIdentifier(service->mutable_name(), "Expected service name."));
 
1212
  }
 
1213
 
 
1214
  DO(ParseServiceBlock(service, service_location));
910
1215
  return true;
911
1216
}
912
1217
 
913
 
bool Parser::ParseServiceBlock(ServiceDescriptorProto* service) {
 
1218
bool Parser::ParseServiceBlock(ServiceDescriptorProto* service,
 
1219
                               const LocationRecorder& service_location) {
914
1220
  DO(Consume("{"));
915
1221
 
916
1222
  while (!TryConsume("}")) {
919
1225
      return false;
920
1226
    }
921
1227
 
922
 
    if (!ParseServiceStatement(service)) {
 
1228
    if (!ParseServiceStatement(service, service_location)) {
923
1229
      // This statement failed to parse.  Skip it, but keep looping to parse
924
1230
      // other statements.
925
1231
      SkipStatement();
929
1235
  return true;
930
1236
}
931
1237
 
932
 
bool Parser::ParseServiceStatement(ServiceDescriptorProto* service) {
 
1238
bool Parser::ParseServiceStatement(ServiceDescriptorProto* service,
 
1239
                                   const LocationRecorder& service_location) {
933
1240
  if (TryConsume(";")) {
934
1241
    // empty statement; ignore
935
1242
    return true;
936
1243
  } else if (LookingAt("option")) {
937
 
    return ParseOption(service->mutable_options());
 
1244
    LocationRecorder location(
 
1245
        service_location, ServiceDescriptorProto::kOptionsFieldNumber);
 
1246
    return ParseOption(service->mutable_options(), location);
938
1247
  } else {
939
 
    return ParseServiceMethod(service->add_method());
 
1248
    LocationRecorder location(service_location,
 
1249
        ServiceDescriptorProto::kMethodFieldNumber, service->method_size());
 
1250
    return ParseServiceMethod(service->add_method(), location);
940
1251
  }
941
1252
}
942
1253
 
943
 
bool Parser::ParseServiceMethod(MethodDescriptorProto* method) {
 
1254
bool Parser::ParseServiceMethod(MethodDescriptorProto* method,
 
1255
                                const LocationRecorder& method_location) {
944
1256
  DO(Consume("rpc"));
945
 
  RecordLocation(method, DescriptorPool::ErrorCollector::NAME);
946
 
  DO(ConsumeIdentifier(method->mutable_name(), "Expected method name."));
 
1257
 
 
1258
  {
 
1259
    LocationRecorder location(method_location,
 
1260
                              MethodDescriptorProto::kNameFieldNumber);
 
1261
    location.RecordLegacyLocation(
 
1262
        method, DescriptorPool::ErrorCollector::NAME);
 
1263
    DO(ConsumeIdentifier(method->mutable_name(), "Expected method name."));
 
1264
  }
947
1265
 
948
1266
  // Parse input type.
949
1267
  DO(Consume("("));
950
 
  RecordLocation(method, DescriptorPool::ErrorCollector::INPUT_TYPE);
951
 
  DO(ParseUserDefinedType(method->mutable_input_type()));
 
1268
  {
 
1269
    LocationRecorder location(method_location,
 
1270
                              MethodDescriptorProto::kInputTypeFieldNumber);
 
1271
    location.RecordLegacyLocation(
 
1272
        method, DescriptorPool::ErrorCollector::INPUT_TYPE);
 
1273
    DO(ParseUserDefinedType(method->mutable_input_type()));
 
1274
  }
952
1275
  DO(Consume(")"));
953
1276
 
954
1277
  // Parse output type.
955
1278
  DO(Consume("returns"));
956
1279
  DO(Consume("("));
957
 
  RecordLocation(method, DescriptorPool::ErrorCollector::OUTPUT_TYPE);
958
 
  DO(ParseUserDefinedType(method->mutable_output_type()));
 
1280
  {
 
1281
    LocationRecorder location(method_location,
 
1282
                              MethodDescriptorProto::kOutputTypeFieldNumber);
 
1283
    location.RecordLegacyLocation(
 
1284
        method, DescriptorPool::ErrorCollector::OUTPUT_TYPE);
 
1285
    DO(ParseUserDefinedType(method->mutable_output_type()));
 
1286
  }
959
1287
  DO(Consume(")"));
960
1288
 
961
1289
  if (TryConsume("{")) {
969
1297
      if (TryConsume(";")) {
970
1298
        // empty statement; ignore
971
1299
      } else {
972
 
        if (!ParseOption(method->mutable_options())) {
 
1300
        LocationRecorder location(method_location,
 
1301
                                  MethodDescriptorProto::kOptionsFieldNumber);
 
1302
        if (!ParseOption(method->mutable_options(), location)) {
973
1303
          // This statement failed to parse.  Skip it, but keep looping to
974
1304
          // parse other statements.
975
1305
          SkipStatement();
1053
1383
 
1054
1384
// ===================================================================
1055
1385
 
1056
 
bool Parser::ParsePackage(FileDescriptorProto* file) {
 
1386
bool Parser::ParsePackage(FileDescriptorProto* file,
 
1387
                          const LocationRecorder& root_location) {
1057
1388
  if (file->has_package()) {
1058
1389
    AddError("Multiple package definitions.");
1059
1390
    // Don't append the new package to the old one.  Just replace it.  Not
1063
1394
 
1064
1395
  DO(Consume("package"));
1065
1396
 
1066
 
  RecordLocation(file, DescriptorPool::ErrorCollector::NAME);
 
1397
  {
 
1398
    LocationRecorder location(root_location,
 
1399
                              FileDescriptorProto::kPackageFieldNumber);
 
1400
    location.RecordLegacyLocation(file, DescriptorPool::ErrorCollector::NAME);
1067
1401
 
1068
 
  while (true) {
1069
 
    string identifier;
1070
 
    DO(ConsumeIdentifier(&identifier, "Expected identifier."));
1071
 
    file->mutable_package()->append(identifier);
1072
 
    if (!TryConsume(".")) break;
1073
 
    file->mutable_package()->append(".");
 
1402
    while (true) {
 
1403
      string identifier;
 
1404
      DO(ConsumeIdentifier(&identifier, "Expected identifier."));
 
1405
      file->mutable_package()->append(identifier);
 
1406
      if (!TryConsume(".")) break;
 
1407
      file->mutable_package()->append(".");
 
1408
    }
1074
1409
  }
1075
1410
 
1076
1411
  DO(Consume(";"));
1077
1412
  return true;
1078
1413
}
1079
1414
 
1080
 
bool Parser::ParseImport(string* import_filename) {
 
1415
bool Parser::ParseImport(string* import_filename,
 
1416
                         const LocationRecorder& root_location,
 
1417
                         int index) {
1081
1418
  DO(Consume("import"));
1082
 
  DO(ConsumeString(import_filename,
1083
 
    "Expected a string naming the file to import."));
 
1419
  {
 
1420
    LocationRecorder location(root_location,
 
1421
                              FileDescriptorProto::kDependencyFieldNumber,
 
1422
                              index);
 
1423
    DO(ConsumeString(import_filename,
 
1424
      "Expected a string naming the file to import."));
 
1425
  }
1084
1426
  DO(Consume(";"));
1085
1427
  return true;
1086
1428
}
1087
1429
 
1088
 
bool Parser::ParseOption(Message* options) {
 
1430
bool Parser::ParseOption(Message* options,
 
1431
                         const LocationRecorder& options_location) {
1089
1432
  DO(Consume("option"));
1090
 
  DO(ParseOptionAssignment(options));
 
1433
  DO(ParseOptionAssignment(options, options_location));
1091
1434
  DO(Consume(";"));
1092
1435
  return true;
1093
1436
}