| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 ParserImpl(const Descriptor* root_message_type, | 227 ParserImpl(const Descriptor* root_message_type, |
| 228 io::ZeroCopyInputStream* input_stream, | 228 io::ZeroCopyInputStream* input_stream, |
| 229 io::ErrorCollector* error_collector, | 229 io::ErrorCollector* error_collector, |
| 230 TextFormat::Finder* finder, | 230 TextFormat::Finder* finder, |
| 231 ParseInfoTree* parse_info_tree, | 231 ParseInfoTree* parse_info_tree, |
| 232 SingularOverwritePolicy singular_overwrite_policy, | 232 SingularOverwritePolicy singular_overwrite_policy, |
| 233 bool allow_case_insensitive_field, | 233 bool allow_case_insensitive_field, |
| 234 bool allow_unknown_field, | 234 bool allow_unknown_field, |
| 235 bool allow_unknown_enum, | 235 bool allow_unknown_enum, |
| 236 bool allow_field_number, | 236 bool allow_field_number, |
| 237 bool allow_relaxed_whitespace) | 237 bool allow_relaxed_whitespace, |
| 238 bool allow_partial) |
| 238 : error_collector_(error_collector), | 239 : error_collector_(error_collector), |
| 239 finder_(finder), | 240 finder_(finder), |
| 240 parse_info_tree_(parse_info_tree), | 241 parse_info_tree_(parse_info_tree), |
| 241 tokenizer_error_collector_(this), | 242 tokenizer_error_collector_(this), |
| 242 tokenizer_(input_stream, &tokenizer_error_collector_), | 243 tokenizer_(input_stream, &tokenizer_error_collector_), |
| 243 root_message_type_(root_message_type), | 244 root_message_type_(root_message_type), |
| 244 singular_overwrite_policy_(singular_overwrite_policy), | 245 singular_overwrite_policy_(singular_overwrite_policy), |
| 245 allow_case_insensitive_field_(allow_case_insensitive_field), | 246 allow_case_insensitive_field_(allow_case_insensitive_field), |
| 246 allow_unknown_field_(allow_unknown_field), | 247 allow_unknown_field_(allow_unknown_field), |
| 247 allow_unknown_enum_(allow_unknown_enum), | 248 allow_unknown_enum_(allow_unknown_enum), |
| 248 allow_field_number_(allow_field_number), | 249 allow_field_number_(allow_field_number), |
| 250 allow_partial_(allow_partial), |
| 249 had_errors_(false) { | 251 had_errors_(false) { |
| 250 // For backwards-compatibility with proto1, we need to allow the 'f' suffix | 252 // For backwards-compatibility with proto1, we need to allow the 'f' suffix |
| 251 // for floats. | 253 // for floats. |
| 252 tokenizer_.set_allow_f_after_float(true); | 254 tokenizer_.set_allow_f_after_float(true); |
| 253 | 255 |
| 254 // '#' starts a comment. | 256 // '#' starts a comment. |
| 255 tokenizer_.set_comment_style(io::Tokenizer::SH_COMMENT_STYLE); | 257 tokenizer_.set_comment_style(io::Tokenizer::SH_COMMENT_STYLE); |
| 256 | 258 |
| 257 if (allow_relaxed_whitespace) { | 259 if (allow_relaxed_whitespace) { |
| 258 tokenizer_.set_require_space_after_number(false); | 260 tokenizer_.set_require_space_after_number(false); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 &any_value_field) && | 386 &any_value_field) && |
| 385 TryConsume("[")) { | 387 TryConsume("[")) { |
| 386 string full_type_name, prefix; | 388 string full_type_name, prefix; |
| 387 DO(ConsumeAnyTypeUrl(&full_type_name, &prefix)); | 389 DO(ConsumeAnyTypeUrl(&full_type_name, &prefix)); |
| 388 DO(Consume("]")); | 390 DO(Consume("]")); |
| 389 TryConsume(":"); // ':' is optional between message labels and values. | 391 TryConsume(":"); // ':' is optional between message labels and values. |
| 390 string serialized_value; | 392 string serialized_value; |
| 391 DO(ConsumeAnyValue(full_type_name, | 393 DO(ConsumeAnyValue(full_type_name, |
| 392 message->GetDescriptor()->file()->pool(), | 394 message->GetDescriptor()->file()->pool(), |
| 393 &serialized_value)); | 395 &serialized_value)); |
| 396 if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) { |
| 397 // Fail if any_type_url_field has already been specified. |
| 398 if ((!any_type_url_field->is_repeated() && |
| 399 reflection->HasField(*message, any_type_url_field)) || |
| 400 (!any_value_field->is_repeated() && |
| 401 reflection->HasField(*message, any_value_field))) { |
| 402 ReportError("Non-repeated Any specified multiple times."); |
| 403 return false; |
| 404 } |
| 405 } |
| 394 reflection->SetString( | 406 reflection->SetString( |
| 395 message, any_type_url_field, | 407 message, any_type_url_field, |
| 396 string(prefix + full_type_name)); | 408 string(prefix + full_type_name)); |
| 397 reflection->SetString(message, any_value_field, serialized_value); | 409 reflection->SetString(message, any_value_field, serialized_value); |
| 398 return true; | 410 return true; |
| 399 } | 411 } |
| 400 if (TryConsume("[")) { | 412 if (TryConsume("[")) { |
| 401 // Extension. | 413 // Extension. |
| 402 DO(ConsumeFullTypeName(&field_name)); | 414 DO(ConsumeFullTypeName(&field_name)); |
| 403 DO(Consume("]")); | 415 DO(Consume("]")); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 // Perform special handling for embedded message types. | 518 // Perform special handling for embedded message types. |
| 507 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | 519 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 508 // ':' is optional here. | 520 // ':' is optional here. |
| 509 TryConsume(":"); | 521 TryConsume(":"); |
| 510 } else { | 522 } else { |
| 511 // ':' is required here. | 523 // ':' is required here. |
| 512 DO(Consume(":")); | 524 DO(Consume(":")); |
| 513 } | 525 } |
| 514 | 526 |
| 515 if (field->is_repeated() && TryConsume("[")) { | 527 if (field->is_repeated() && TryConsume("[")) { |
| 516 // Short repeated format, e.g. "foo: [1, 2, 3]" | 528 // Short repeated format, e.g. "foo: [1, 2, 3]". |
| 517 while (true) { | 529 if (!TryConsume("]")) { |
| 518 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | 530 // "foo: []" is treated as empty. |
| 519 // Perform special handling for embedded message types. | 531 while (true) { |
| 520 DO(ConsumeFieldMessage(message, reflection, field)); | 532 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 521 } else { | 533 // Perform special handling for embedded message types. |
| 522 DO(ConsumeFieldValue(message, reflection, field)); | 534 DO(ConsumeFieldMessage(message, reflection, field)); |
| 535 } else { |
| 536 DO(ConsumeFieldValue(message, reflection, field)); |
| 537 } |
| 538 if (TryConsume("]")) { |
| 539 break; |
| 540 } |
| 541 DO(Consume(",")); |
| 523 } | 542 } |
| 524 if (TryConsume("]")) { | |
| 525 break; | |
| 526 } | |
| 527 DO(Consume(",")); | |
| 528 } | 543 } |
| 529 } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | 544 } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 530 DO(ConsumeFieldMessage(message, reflection, field)); | 545 DO(ConsumeFieldMessage(message, reflection, field)); |
| 531 } else { | 546 } else { |
| 532 DO(ConsumeFieldValue(message, reflection, field)); | 547 DO(ConsumeFieldValue(message, reflection, field)); |
| 533 } | 548 } |
| 534 | 549 |
| 535 // For historical reasons, fields may optionally be separated by commas or | 550 // For historical reasons, fields may optionally be separated by commas or |
| 536 // semicolons. | 551 // semicolons. |
| 537 TryConsume(";") || TryConsume(","); | 552 TryConsume(";") || TryConsume(","); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 // Find the enumeration value. | 726 // Find the enumeration value. |
| 712 enum_value = enum_type->FindValueByName(value); | 727 enum_value = enum_type->FindValueByName(value); |
| 713 | 728 |
| 714 } else if (LookingAt("-") || | 729 } else if (LookingAt("-") || |
| 715 LookingAtType(io::Tokenizer::TYPE_INTEGER)) { | 730 LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
| 716 int64 int_value; | 731 int64 int_value; |
| 717 DO(ConsumeSignedInteger(&int_value, kint32max)); | 732 DO(ConsumeSignedInteger(&int_value, kint32max)); |
| 718 value = SimpleItoa(int_value); // for error reporting | 733 value = SimpleItoa(int_value); // for error reporting |
| 719 enum_value = enum_type->FindValueByNumber(int_value); | 734 enum_value = enum_type->FindValueByNumber(int_value); |
| 720 } else { | 735 } else { |
| 721 ReportError("Expected integer or identifier."); | 736 ReportError("Expected integer or identifier, got: " + |
| 737 tokenizer_.current().text); |
| 722 return false; | 738 return false; |
| 723 } | 739 } |
| 724 | 740 |
| 725 if (enum_value == NULL) { | 741 if (enum_value == NULL) { |
| 726 if (!allow_unknown_enum_) { | 742 if (!allow_unknown_enum_) { |
| 727 ReportError("Unknown enumeration value of \"" + value + "\" for " | 743 ReportError("Unknown enumeration value of \"" + value + "\" for " |
| 728 "field \"" + field->name() + "\"."); | 744 "field \"" + field->name() + "\"."); |
| 729 return false; | 745 return false; |
| 730 } else { | 746 } else { |
| 731 ReportWarning("Unknown enumeration value of \"" + value + "\" for " | 747 ReportWarning("Unknown enumeration value of \"" + value + "\" for " |
| (...skipping 17 matching lines...) Expand all Loading... |
| 749 return true; | 765 return true; |
| 750 } | 766 } |
| 751 | 767 |
| 752 bool SkipFieldValue() { | 768 bool SkipFieldValue() { |
| 753 if (LookingAtType(io::Tokenizer::TYPE_STRING)) { | 769 if (LookingAtType(io::Tokenizer::TYPE_STRING)) { |
| 754 while (LookingAtType(io::Tokenizer::TYPE_STRING)) { | 770 while (LookingAtType(io::Tokenizer::TYPE_STRING)) { |
| 755 tokenizer_.Next(); | 771 tokenizer_.Next(); |
| 756 } | 772 } |
| 757 return true; | 773 return true; |
| 758 } | 774 } |
| 775 if (TryConsume("[")) { |
| 776 while (true) { |
| 777 if (!LookingAt("{") && !LookingAt("<")) { |
| 778 DO(SkipFieldValue()); |
| 779 } else { |
| 780 DO(SkipFieldMessage()); |
| 781 } |
| 782 if (TryConsume("]")) { |
| 783 break; |
| 784 } |
| 785 DO(Consume(",")); |
| 786 } |
| 787 return true; |
| 788 } |
| 759 // Possible field values other than string: | 789 // Possible field values other than string: |
| 760 // 12345 => TYPE_INTEGER | 790 // 12345 => TYPE_INTEGER |
| 761 // -12345 => TYPE_SYMBOL + TYPE_INTEGER | 791 // -12345 => TYPE_SYMBOL + TYPE_INTEGER |
| 762 // 1.2345 => TYPE_FLOAT | 792 // 1.2345 => TYPE_FLOAT |
| 763 // -1.2345 => TYPE_SYMBOL + TYPE_FLOAT | 793 // -1.2345 => TYPE_SYMBOL + TYPE_FLOAT |
| 764 // inf => TYPE_IDENTIFIER | 794 // inf => TYPE_IDENTIFIER |
| 765 // -inf => TYPE_SYMBOL + TYPE_IDENTIFIER | 795 // -inf => TYPE_SYMBOL + TYPE_IDENTIFIER |
| 766 // TYPE_INTEGER => TYPE_IDENTIFIER | 796 // TYPE_INTEGER => TYPE_IDENTIFIER |
| 767 // Divides them into two group, one with TYPE_SYMBOL | 797 // Divides them into two group, one with TYPE_SYMBOL |
| 768 // and the other without: | 798 // and the other without: |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 | 854 |
| 825 // If allow_field_numer_ or allow_unknown_field_ is true, we should able | 855 // If allow_field_numer_ or allow_unknown_field_ is true, we should able |
| 826 // to parse integer identifiers. | 856 // to parse integer identifiers. |
| 827 if ((allow_field_number_ || allow_unknown_field_) | 857 if ((allow_field_number_ || allow_unknown_field_) |
| 828 && LookingAtType(io::Tokenizer::TYPE_INTEGER)) { | 858 && LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
| 829 *identifier = tokenizer_.current().text; | 859 *identifier = tokenizer_.current().text; |
| 830 tokenizer_.Next(); | 860 tokenizer_.Next(); |
| 831 return true; | 861 return true; |
| 832 } | 862 } |
| 833 | 863 |
| 834 ReportError("Expected identifier."); | 864 ReportError("Expected identifier, got: " + tokenizer_.current().text); |
| 835 return false; | 865 return false; |
| 836 } | 866 } |
| 837 | 867 |
| 838 // Consume a string of form "<id1>.<id2>....<idN>". | 868 // Consume a string of form "<id1>.<id2>....<idN>". |
| 839 bool ConsumeFullTypeName(string* name) { | 869 bool ConsumeFullTypeName(string* name) { |
| 840 DO(ConsumeIdentifier(name)); | 870 DO(ConsumeIdentifier(name)); |
| 841 while (TryConsume(".")) { | 871 while (TryConsume(".")) { |
| 842 string part; | 872 string part; |
| 843 DO(ConsumeIdentifier(&part)); | 873 DO(ConsumeIdentifier(&part)); |
| 844 *name += "."; | 874 *name += "."; |
| 845 *name += part; | 875 *name += part; |
| 846 } | 876 } |
| 847 return true; | 877 return true; |
| 848 } | 878 } |
| 849 | 879 |
| 850 // Consumes a string and saves its value in the text parameter. | 880 // Consumes a string and saves its value in the text parameter. |
| 851 // Returns false if the token is not of type STRING. | 881 // Returns false if the token is not of type STRING. |
| 852 bool ConsumeString(string* text) { | 882 bool ConsumeString(string* text) { |
| 853 if (!LookingAtType(io::Tokenizer::TYPE_STRING)) { | 883 if (!LookingAtType(io::Tokenizer::TYPE_STRING)) { |
| 854 ReportError("Expected string."); | 884 ReportError("Expected string, got: " + tokenizer_.current().text); |
| 855 return false; | 885 return false; |
| 856 } | 886 } |
| 857 | 887 |
| 858 text->clear(); | 888 text->clear(); |
| 859 while (LookingAtType(io::Tokenizer::TYPE_STRING)) { | 889 while (LookingAtType(io::Tokenizer::TYPE_STRING)) { |
| 860 io::Tokenizer::ParseStringAppend(tokenizer_.current().text, text); | 890 io::Tokenizer::ParseStringAppend(tokenizer_.current().text, text); |
| 861 | 891 |
| 862 tokenizer_.Next(); | 892 tokenizer_.Next(); |
| 863 } | 893 } |
| 864 | 894 |
| 865 return true; | 895 return true; |
| 866 } | 896 } |
| 867 | 897 |
| 868 // Consumes a uint64 and saves its value in the value parameter. | 898 // Consumes a uint64 and saves its value in the value parameter. |
| 869 // Returns false if the token is not of type INTEGER. | 899 // Returns false if the token is not of type INTEGER. |
| 870 bool ConsumeUnsignedInteger(uint64* value, uint64 max_value) { | 900 bool ConsumeUnsignedInteger(uint64* value, uint64 max_value) { |
| 871 if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) { | 901 if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
| 872 ReportError("Expected integer."); | 902 ReportError("Expected integer, got: " + tokenizer_.current().text); |
| 873 return false; | 903 return false; |
| 874 } | 904 } |
| 875 | 905 |
| 876 if (!io::Tokenizer::ParseInteger(tokenizer_.current().text, | 906 if (!io::Tokenizer::ParseInteger(tokenizer_.current().text, |
| 877 max_value, value)) { | 907 max_value, value)) { |
| 878 ReportError("Integer out of range."); | 908 ReportError("Integer out of range (" + tokenizer_.current().text + ")"); |
| 879 return false; | 909 return false; |
| 880 } | 910 } |
| 881 | 911 |
| 882 tokenizer_.Next(); | 912 tokenizer_.Next(); |
| 883 return true; | 913 return true; |
| 884 } | 914 } |
| 885 | 915 |
| 886 // Consumes an int64 and saves its value in the value parameter. | 916 // Consumes an int64 and saves its value in the value parameter. |
| 887 // Note that since the tokenizer does not support negative numbers, | 917 // Note that since the tokenizer does not support negative numbers, |
| 888 // we actually may consume an additional token (for the minus sign) in this | 918 // we actually may consume an additional token (for the minus sign) in this |
| 889 // method. Returns false if the token is not an integer | 919 // method. Returns false if the token is not an integer |
| 890 // (signed or otherwise). | 920 // (signed or otherwise). |
| 891 bool ConsumeSignedInteger(int64* value, uint64 max_value) { | 921 bool ConsumeSignedInteger(int64* value, uint64 max_value) { |
| 892 bool negative = false; | 922 bool negative = false; |
| 893 | 923 |
| 894 if (TryConsume("-")) { | 924 if (TryConsume("-")) { |
| 895 negative = true; | 925 negative = true; |
| 896 // Two's complement always allows one more negative integer than | 926 // Two's complement always allows one more negative integer than |
| 897 // positive. | 927 // positive. |
| 898 ++max_value; | 928 ++max_value; |
| 899 } | 929 } |
| 900 | 930 |
| 901 uint64 unsigned_value; | 931 uint64 unsigned_value; |
| 902 | 932 |
| 903 DO(ConsumeUnsignedInteger(&unsigned_value, max_value)); | 933 DO(ConsumeUnsignedInteger(&unsigned_value, max_value)); |
| 904 | 934 |
| 905 *value = static_cast<int64>(unsigned_value); | |
| 906 | |
| 907 if (negative) { | 935 if (negative) { |
| 908 *value = -*value; | 936 if ((static_cast<uint64>(kint64max) + 1) == unsigned_value) { |
| 937 *value = kint64min; |
| 938 } else { |
| 939 *value = -static_cast<int64>(unsigned_value); |
| 940 } |
| 941 } else { |
| 942 *value = static_cast<int64>(unsigned_value); |
| 909 } | 943 } |
| 910 | 944 |
| 911 return true; | 945 return true; |
| 912 } | 946 } |
| 913 | 947 |
| 914 // Consumes a uint64 and saves its value in the value parameter. | 948 // Consumes a uint64 and saves its value in the value parameter. |
| 915 // Accepts decimal numbers only, rejects hex or oct numbers. | 949 // Accepts decimal numbers only, rejects hex or oct numbers. |
| 916 bool ConsumeUnsignedDecimalInteger(uint64* value, uint64 max_value) { | 950 bool ConsumeUnsignedDecimalInteger(uint64* value, uint64 max_value) { |
| 917 if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) { | 951 if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
| 918 ReportError("Expected integer."); | 952 ReportError("Expected integer, got: " + tokenizer_.current().text); |
| 919 return false; | 953 return false; |
| 920 } | 954 } |
| 921 | 955 |
| 922 const string& text = tokenizer_.current().text; | 956 const string& text = tokenizer_.current().text; |
| 923 if (IsHexNumber(text) || IsOctNumber(text)) { | 957 if (IsHexNumber(text) || IsOctNumber(text)) { |
| 924 ReportError("Expect a decimal number."); | 958 ReportError("Expect a decimal number, got: " + text); |
| 925 return false; | 959 return false; |
| 926 } | 960 } |
| 927 | 961 |
| 928 if (!io::Tokenizer::ParseInteger(text, max_value, value)) { | 962 if (!io::Tokenizer::ParseInteger(text, max_value, value)) { |
| 929 ReportError("Integer out of range."); | 963 ReportError("Integer out of range (" + text + ")"); |
| 930 return false; | 964 return false; |
| 931 } | 965 } |
| 932 | 966 |
| 933 tokenizer_.Next(); | 967 tokenizer_.Next(); |
| 934 return true; | 968 return true; |
| 935 } | 969 } |
| 936 | 970 |
| 937 // Consumes a double and saves its value in the value parameter. | 971 // Consumes a double and saves its value in the value parameter. |
| 938 // Note that since the tokenizer does not support negative numbers, | 972 // Note that since the tokenizer does not support negative numbers, |
| 939 // we actually may consume an additional token (for the minus sign) in this | 973 // we actually may consume an additional token (for the minus sign) in this |
| (...skipping 24 matching lines...) Expand all Loading... |
| 964 string text = tokenizer_.current().text; | 998 string text = tokenizer_.current().text; |
| 965 LowerString(&text); | 999 LowerString(&text); |
| 966 if (text == "inf" || | 1000 if (text == "inf" || |
| 967 text == "infinity") { | 1001 text == "infinity") { |
| 968 *value = std::numeric_limits<double>::infinity(); | 1002 *value = std::numeric_limits<double>::infinity(); |
| 969 tokenizer_.Next(); | 1003 tokenizer_.Next(); |
| 970 } else if (text == "nan") { | 1004 } else if (text == "nan") { |
| 971 *value = std::numeric_limits<double>::quiet_NaN(); | 1005 *value = std::numeric_limits<double>::quiet_NaN(); |
| 972 tokenizer_.Next(); | 1006 tokenizer_.Next(); |
| 973 } else { | 1007 } else { |
| 974 ReportError("Expected double."); | 1008 ReportError("Expected double, got: " + text); |
| 975 return false; | 1009 return false; |
| 976 } | 1010 } |
| 977 } else { | 1011 } else { |
| 978 ReportError("Expected double."); | 1012 ReportError("Expected double, got: " + tokenizer_.current().text); |
| 979 return false; | 1013 return false; |
| 980 } | 1014 } |
| 981 | 1015 |
| 982 if (negative) { | 1016 if (negative) { |
| 983 *value = -*value; | 1017 *value = -*value; |
| 984 } | 1018 } |
| 985 | 1019 |
| 986 return true; | 1020 return true; |
| 987 } | 1021 } |
| 988 | 1022 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1026 DynamicMessageFactory factory; | 1060 DynamicMessageFactory factory; |
| 1027 const Message* value_prototype = factory.GetPrototype(value_descriptor); | 1061 const Message* value_prototype = factory.GetPrototype(value_descriptor); |
| 1028 if (value_prototype == NULL) { | 1062 if (value_prototype == NULL) { |
| 1029 return false; | 1063 return false; |
| 1030 } | 1064 } |
| 1031 google::protobuf::scoped_ptr<Message> value(value_prototype->New()); | 1065 google::protobuf::scoped_ptr<Message> value(value_prototype->New()); |
| 1032 string sub_delimiter; | 1066 string sub_delimiter; |
| 1033 DO(ConsumeMessageDelimiter(&sub_delimiter)); | 1067 DO(ConsumeMessageDelimiter(&sub_delimiter)); |
| 1034 DO(ConsumeMessage(value.get(), sub_delimiter)); | 1068 DO(ConsumeMessage(value.get(), sub_delimiter)); |
| 1035 | 1069 |
| 1036 value->AppendToString(serialized_value); | 1070 if (allow_partial_) { |
| 1071 value->AppendPartialToString(serialized_value); |
| 1072 } else { |
| 1073 if (!value->IsInitialized()) { |
| 1074 ReportError( |
| 1075 "Value of type \"" + full_type_name + |
| 1076 "\" stored in google.protobuf.Any has missing required fields"); |
| 1077 return false; |
| 1078 } |
| 1079 value->AppendToString(serialized_value); |
| 1080 } |
| 1037 return true; | 1081 return true; |
| 1038 } | 1082 } |
| 1039 | 1083 |
| 1040 // Consumes a token and confirms that it matches that specified in the | 1084 // Consumes a token and confirms that it matches that specified in the |
| 1041 // value parameter. Returns false if the token found does not match that | 1085 // value parameter. Returns false if the token found does not match that |
| 1042 // which was specified. | 1086 // which was specified. |
| 1043 bool Consume(const string& value) { | 1087 bool Consume(const string& value) { |
| 1044 const string& current_value = tokenizer_.current().text; | 1088 const string& current_value = tokenizer_.current().text; |
| 1045 | 1089 |
| 1046 if (current_value != value) { | 1090 if (current_value != value) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1091 TextFormat::Finder* finder_; | 1135 TextFormat::Finder* finder_; |
| 1092 ParseInfoTree* parse_info_tree_; | 1136 ParseInfoTree* parse_info_tree_; |
| 1093 ParserErrorCollector tokenizer_error_collector_; | 1137 ParserErrorCollector tokenizer_error_collector_; |
| 1094 io::Tokenizer tokenizer_; | 1138 io::Tokenizer tokenizer_; |
| 1095 const Descriptor* root_message_type_; | 1139 const Descriptor* root_message_type_; |
| 1096 SingularOverwritePolicy singular_overwrite_policy_; | 1140 SingularOverwritePolicy singular_overwrite_policy_; |
| 1097 const bool allow_case_insensitive_field_; | 1141 const bool allow_case_insensitive_field_; |
| 1098 const bool allow_unknown_field_; | 1142 const bool allow_unknown_field_; |
| 1099 const bool allow_unknown_enum_; | 1143 const bool allow_unknown_enum_; |
| 1100 const bool allow_field_number_; | 1144 const bool allow_field_number_; |
| 1145 const bool allow_partial_; |
| 1101 bool had_errors_; | 1146 bool had_errors_; |
| 1102 }; | 1147 }; |
| 1103 | 1148 |
| 1104 #undef DO | 1149 #undef DO |
| 1105 | 1150 |
| 1106 // =========================================================================== | 1151 // =========================================================================== |
| 1107 // Internal class for writing text to the io::ZeroCopyOutputStream. Adapted | 1152 // Internal class for writing text to the io::ZeroCopyOutputStream. Adapted |
| 1108 // from the Printer found in //google/protobuf/io/printer.h | 1153 // from the Printer found in //google/protobuf/io/printer.h |
| 1109 class TextFormat::Printer::TextGenerator { | 1154 class TextFormat::Printer::TextGenerator { |
| 1110 public: | 1155 public: |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1252 ParserImpl::SingularOverwritePolicy overwrites_policy = | 1297 ParserImpl::SingularOverwritePolicy overwrites_policy = |
| 1253 allow_singular_overwrites_ | 1298 allow_singular_overwrites_ |
| 1254 ? ParserImpl::ALLOW_SINGULAR_OVERWRITES | 1299 ? ParserImpl::ALLOW_SINGULAR_OVERWRITES |
| 1255 : ParserImpl::FORBID_SINGULAR_OVERWRITES; | 1300 : ParserImpl::FORBID_SINGULAR_OVERWRITES; |
| 1256 | 1301 |
| 1257 ParserImpl parser(output->GetDescriptor(), input, error_collector_, | 1302 ParserImpl parser(output->GetDescriptor(), input, error_collector_, |
| 1258 finder_, parse_info_tree_, | 1303 finder_, parse_info_tree_, |
| 1259 overwrites_policy, | 1304 overwrites_policy, |
| 1260 allow_case_insensitive_field_, allow_unknown_field_, | 1305 allow_case_insensitive_field_, allow_unknown_field_, |
| 1261 allow_unknown_enum_, allow_field_number_, | 1306 allow_unknown_enum_, allow_field_number_, |
| 1262 allow_relaxed_whitespace_); | 1307 allow_relaxed_whitespace_, allow_partial_); |
| 1263 return MergeUsingImpl(input, output, &parser); | 1308 return MergeUsingImpl(input, output, &parser); |
| 1264 } | 1309 } |
| 1265 | 1310 |
| 1266 bool TextFormat::Parser::ParseFromString(const string& input, | 1311 bool TextFormat::Parser::ParseFromString(const string& input, |
| 1267 Message* output) { | 1312 Message* output) { |
| 1268 io::ArrayInputStream input_stream(input.data(), input.size()); | 1313 io::ArrayInputStream input_stream(input.data(), input.size()); |
| 1269 return Parse(&input_stream, output); | 1314 return Parse(&input_stream, output); |
| 1270 } | 1315 } |
| 1271 | 1316 |
| 1272 bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input, | 1317 bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input, |
| 1273 Message* output) { | 1318 Message* output) { |
| 1274 ParserImpl parser(output->GetDescriptor(), input, error_collector_, | 1319 ParserImpl parser(output->GetDescriptor(), input, error_collector_, |
| 1275 finder_, parse_info_tree_, | 1320 finder_, parse_info_tree_, |
| 1276 ParserImpl::ALLOW_SINGULAR_OVERWRITES, | 1321 ParserImpl::ALLOW_SINGULAR_OVERWRITES, |
| 1277 allow_case_insensitive_field_, allow_unknown_field_, | 1322 allow_case_insensitive_field_, allow_unknown_field_, |
| 1278 allow_unknown_enum_, allow_field_number_, | 1323 allow_unknown_enum_, allow_field_number_, |
| 1279 allow_relaxed_whitespace_); | 1324 allow_relaxed_whitespace_, allow_partial_); |
| 1280 return MergeUsingImpl(input, output, &parser); | 1325 return MergeUsingImpl(input, output, &parser); |
| 1281 } | 1326 } |
| 1282 | 1327 |
| 1283 bool TextFormat::Parser::MergeFromString(const string& input, | 1328 bool TextFormat::Parser::MergeFromString(const string& input, |
| 1284 Message* output) { | 1329 Message* output) { |
| 1285 io::ArrayInputStream input_stream(input.data(), input.size()); | 1330 io::ArrayInputStream input_stream(input.data(), input.size()); |
| 1286 return Merge(&input_stream, output); | 1331 return Merge(&input_stream, output); |
| 1287 } | 1332 } |
| 1288 | 1333 |
| 1289 bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */, | 1334 bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1303 bool TextFormat::Parser::ParseFieldValueFromString( | 1348 bool TextFormat::Parser::ParseFieldValueFromString( |
| 1304 const string& input, | 1349 const string& input, |
| 1305 const FieldDescriptor* field, | 1350 const FieldDescriptor* field, |
| 1306 Message* output) { | 1351 Message* output) { |
| 1307 io::ArrayInputStream input_stream(input.data(), input.size()); | 1352 io::ArrayInputStream input_stream(input.data(), input.size()); |
| 1308 ParserImpl parser(output->GetDescriptor(), &input_stream, error_collector_, | 1353 ParserImpl parser(output->GetDescriptor(), &input_stream, error_collector_, |
| 1309 finder_, parse_info_tree_, | 1354 finder_, parse_info_tree_, |
| 1310 ParserImpl::ALLOW_SINGULAR_OVERWRITES, | 1355 ParserImpl::ALLOW_SINGULAR_OVERWRITES, |
| 1311 allow_case_insensitive_field_, allow_unknown_field_, | 1356 allow_case_insensitive_field_, allow_unknown_field_, |
| 1312 allow_unknown_enum_, allow_field_number_, | 1357 allow_unknown_enum_, allow_field_number_, |
| 1313 allow_relaxed_whitespace_); | 1358 allow_relaxed_whitespace_, allow_partial_); |
| 1314 return parser.ParseField(field, output); | 1359 return parser.ParseField(field, output); |
| 1315 } | 1360 } |
| 1316 | 1361 |
| 1317 /* static */ bool TextFormat::Parse(io::ZeroCopyInputStream* input, | 1362 /* static */ bool TextFormat::Parse(io::ZeroCopyInputStream* input, |
| 1318 Message* output) { | 1363 Message* output) { |
| 1319 return Parser().Parse(input, output); | 1364 return Parser().Parse(input, output); |
| 1320 } | 1365 } |
| 1321 | 1366 |
| 1322 /* static */ bool TextFormat::Merge(io::ZeroCopyInputStream* input, | 1367 /* static */ bool TextFormat::Merge(io::ZeroCopyInputStream* input, |
| 1323 Message* output) { | 1368 Message* output) { |
| (...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1966 generator.Outdent(); | 2011 generator.Outdent(); |
| 1967 generator.Print("}\n"); | 2012 generator.Print("}\n"); |
| 1968 } | 2013 } |
| 1969 break; | 2014 break; |
| 1970 } | 2015 } |
| 1971 } | 2016 } |
| 1972 } | 2017 } |
| 1973 | 2018 |
| 1974 } // namespace protobuf | 2019 } // namespace protobuf |
| 1975 } // namespace google | 2020 } // namespace google |
| OLD | NEW |