诸暨麻将添加redis
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2398 Zeilen
84 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Recursive descent FTW.
  35. #include <float.h>
  36. #include <limits>
  37. #include <unordered_map>
  38. #include <google/protobuf/stubs/hash.h>
  39. #include <google/protobuf/stubs/casts.h>
  40. #include <google/protobuf/stubs/logging.h>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/compiler/parser.h>
  43. #include <google/protobuf/descriptor.pb.h>
  44. #include <google/protobuf/io/tokenizer.h>
  45. #include <google/protobuf/descriptor.h>
  46. #include <google/protobuf/wire_format.h>
  47. #include <google/protobuf/stubs/strutil.h>
  48. #include <google/protobuf/stubs/map_util.h>
  49. namespace google {
  50. namespace protobuf {
  51. namespace compiler {
  52. using internal::WireFormat;
  53. namespace {
  54. typedef std::unordered_map<std::string, FieldDescriptorProto::Type> TypeNameMap;
  55. TypeNameMap MakeTypeNameTable() {
  56. TypeNameMap result;
  57. result["double"] = FieldDescriptorProto::TYPE_DOUBLE;
  58. result["float"] = FieldDescriptorProto::TYPE_FLOAT;
  59. result["uint64"] = FieldDescriptorProto::TYPE_UINT64;
  60. result["fixed64"] = FieldDescriptorProto::TYPE_FIXED64;
  61. result["fixed32"] = FieldDescriptorProto::TYPE_FIXED32;
  62. result["bool"] = FieldDescriptorProto::TYPE_BOOL;
  63. result["string"] = FieldDescriptorProto::TYPE_STRING;
  64. result["group"] = FieldDescriptorProto::TYPE_GROUP;
  65. result["bytes"] = FieldDescriptorProto::TYPE_BYTES;
  66. result["uint32"] = FieldDescriptorProto::TYPE_UINT32;
  67. result["sfixed32"] = FieldDescriptorProto::TYPE_SFIXED32;
  68. result["sfixed64"] = FieldDescriptorProto::TYPE_SFIXED64;
  69. result["int32"] = FieldDescriptorProto::TYPE_INT32;
  70. result["int64"] = FieldDescriptorProto::TYPE_INT64;
  71. result["sint32"] = FieldDescriptorProto::TYPE_SINT32;
  72. result["sint64"] = FieldDescriptorProto::TYPE_SINT64;
  73. return result;
  74. }
  75. const TypeNameMap kTypeNames = MakeTypeNameTable();
  76. // Camel-case the field name and append "Entry" for generated map entry name.
  77. // e.g. map<KeyType, ValueType> foo_map => FooMapEntry
  78. std::string MapEntryName(const std::string& field_name) {
  79. std::string result;
  80. static const char kSuffix[] = "Entry";
  81. result.reserve(field_name.size() + sizeof(kSuffix));
  82. bool cap_next = true;
  83. for (int i = 0; i < field_name.size(); ++i) {
  84. if (field_name[i] == '_') {
  85. cap_next = true;
  86. } else if (cap_next) {
  87. // Note: Do not use ctype.h due to locales.
  88. if ('a' <= field_name[i] && field_name[i] <= 'z') {
  89. result.push_back(field_name[i] - 'a' + 'A');
  90. } else {
  91. result.push_back(field_name[i]);
  92. }
  93. cap_next = false;
  94. } else {
  95. result.push_back(field_name[i]);
  96. }
  97. }
  98. result.append(kSuffix);
  99. return result;
  100. }
  101. bool IsUppercase(char c) { return c >= 'A' && c <= 'Z'; }
  102. bool IsLowercase(char c) { return c >= 'a' && c <= 'z'; }
  103. bool IsNumber(char c) { return c >= '0' && c <= '9'; }
  104. bool IsUpperCamelCase(const string& name) {
  105. if (name.empty()) {
  106. return true;
  107. }
  108. // Name must start with an upper case character.
  109. if (!IsUppercase(name[0])) {
  110. return false;
  111. }
  112. // Must not contains underscore.
  113. for (int i = 1; i < name.length(); i++) {
  114. if (name[i] == '_') {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. bool IsUpperUnderscore(const string& name) {
  121. for (int i = 0; i < name.length(); i++) {
  122. const char c = name[i];
  123. if (!IsUppercase(c) && c != '_' && !IsNumber(c)) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. bool IsLowerUnderscore(const string& name) {
  130. for (int i = 0; i < name.length(); i++) {
  131. const char c = name[i];
  132. if (!IsLowercase(c) && c != '_' && !IsNumber(c)) {
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138. bool IsNumberFollowUnderscore(const string& name) {
  139. for (int i = 1; i < name.length(); i++) {
  140. const char c = name[i];
  141. if (IsNumber(c) && name[i - 1] == '_') {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. } // anonymous namespace
  148. // Makes code slightly more readable. The meaning of "DO(foo)" is
  149. // "Execute foo and fail if it fails.", where failure is indicated by
  150. // returning false.
  151. #define DO(STATEMENT) \
  152. if (STATEMENT) { \
  153. } else \
  154. return false
  155. // ===================================================================
  156. Parser::Parser()
  157. : input_(NULL),
  158. error_collector_(NULL),
  159. source_location_table_(NULL),
  160. had_errors_(false),
  161. require_syntax_identifier_(false),
  162. stop_after_syntax_identifier_(false) {
  163. }
  164. Parser::~Parser() {}
  165. // ===================================================================
  166. inline bool Parser::LookingAt(const char* text) {
  167. return input_->current().text == text;
  168. }
  169. inline bool Parser::LookingAtType(io::Tokenizer::TokenType token_type) {
  170. return input_->current().type == token_type;
  171. }
  172. inline bool Parser::AtEnd() { return LookingAtType(io::Tokenizer::TYPE_END); }
  173. bool Parser::TryConsume(const char* text) {
  174. if (LookingAt(text)) {
  175. input_->Next();
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. }
  181. bool Parser::Consume(const char* text, const char* error) {
  182. if (TryConsume(text)) {
  183. return true;
  184. } else {
  185. AddError(error);
  186. return false;
  187. }
  188. }
  189. bool Parser::Consume(const char* text) {
  190. if (TryConsume(text)) {
  191. return true;
  192. } else {
  193. AddError("Expected \"" + string(text) + "\".");
  194. return false;
  195. }
  196. }
  197. bool Parser::ConsumeIdentifier(std::string* output, const char* error) {
  198. if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
  199. *output = input_->current().text;
  200. input_->Next();
  201. return true;
  202. } else {
  203. AddError(error);
  204. return false;
  205. }
  206. }
  207. bool Parser::ConsumeInteger(int* output, const char* error) {
  208. if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  209. uint64 value = 0;
  210. if (!io::Tokenizer::ParseInteger(input_->current().text, kint32max,
  211. &value)) {
  212. AddError("Integer out of range.");
  213. // We still return true because we did, in fact, parse an integer.
  214. }
  215. *output = value;
  216. input_->Next();
  217. return true;
  218. } else {
  219. AddError(error);
  220. return false;
  221. }
  222. }
  223. bool Parser::ConsumeSignedInteger(int* output, const char* error) {
  224. bool is_negative = false;
  225. uint64 max_value = kint32max;
  226. if (TryConsume("-")) {
  227. is_negative = true;
  228. max_value += 1;
  229. }
  230. uint64 value = 0;
  231. DO(ConsumeInteger64(max_value, &value, error));
  232. if (is_negative) value *= -1;
  233. *output = value;
  234. return true;
  235. }
  236. bool Parser::ConsumeInteger64(uint64 max_value, uint64* output,
  237. const char* error) {
  238. if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  239. if (!io::Tokenizer::ParseInteger(input_->current().text, max_value,
  240. output)) {
  241. AddError("Integer out of range.");
  242. // We still return true because we did, in fact, parse an integer.
  243. *output = 0;
  244. }
  245. input_->Next();
  246. return true;
  247. } else {
  248. AddError(error);
  249. return false;
  250. }
  251. }
  252. bool Parser::ConsumeNumber(double* output, const char* error) {
  253. if (LookingAtType(io::Tokenizer::TYPE_FLOAT)) {
  254. *output = io::Tokenizer::ParseFloat(input_->current().text);
  255. input_->Next();
  256. return true;
  257. } else if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  258. // Also accept integers.
  259. uint64 value = 0;
  260. if (!io::Tokenizer::ParseInteger(input_->current().text, kuint64max,
  261. &value)) {
  262. AddError("Integer out of range.");
  263. // We still return true because we did, in fact, parse a number.
  264. }
  265. *output = value;
  266. input_->Next();
  267. return true;
  268. } else if (LookingAt("inf")) {
  269. *output = std::numeric_limits<double>::infinity();
  270. input_->Next();
  271. return true;
  272. } else if (LookingAt("nan")) {
  273. *output = std::numeric_limits<double>::quiet_NaN();
  274. input_->Next();
  275. return true;
  276. } else {
  277. AddError(error);
  278. return false;
  279. }
  280. }
  281. bool Parser::ConsumeString(std::string* output, const char* error) {
  282. if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
  283. io::Tokenizer::ParseString(input_->current().text, output);
  284. input_->Next();
  285. // Allow C++ like concatenation of adjacent string tokens.
  286. while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
  287. io::Tokenizer::ParseStringAppend(input_->current().text, output);
  288. input_->Next();
  289. }
  290. return true;
  291. } else {
  292. AddError(error);
  293. return false;
  294. }
  295. }
  296. bool Parser::TryConsumeEndOfDeclaration(const char* text,
  297. const LocationRecorder* location) {
  298. if (LookingAt(text)) {
  299. std::string leading, trailing;
  300. std::vector<std::string> detached;
  301. input_->NextWithComments(&trailing, &detached, &leading);
  302. // Save the leading comments for next time, and recall the leading comments
  303. // from last time.
  304. leading.swap(upcoming_doc_comments_);
  305. if (location != NULL) {
  306. upcoming_detached_comments_.swap(detached);
  307. location->AttachComments(&leading, &trailing, &detached);
  308. } else if (strcmp(text, "}") == 0) {
  309. // If the current location is null and we are finishing the current scope,
  310. // drop pending upcoming detached comments.
  311. upcoming_detached_comments_.swap(detached);
  312. } else {
  313. // Otherwise, append the new detached comments to the existing upcoming
  314. // detached comments.
  315. upcoming_detached_comments_.insert(upcoming_detached_comments_.end(),
  316. detached.begin(), detached.end());
  317. }
  318. return true;
  319. } else {
  320. return false;
  321. }
  322. }
  323. bool Parser::ConsumeEndOfDeclaration(const char* text,
  324. const LocationRecorder* location) {
  325. if (TryConsumeEndOfDeclaration(text, location)) {
  326. return true;
  327. } else {
  328. AddError("Expected \"" + string(text) + "\".");
  329. return false;
  330. }
  331. }
  332. // -------------------------------------------------------------------
  333. void Parser::AddError(int line, int column, const std::string& error) {
  334. if (error_collector_ != NULL) {
  335. error_collector_->AddError(line, column, error);
  336. }
  337. had_errors_ = true;
  338. }
  339. void Parser::AddError(const std::string& error) {
  340. AddError(input_->current().line, input_->current().column, error);
  341. }
  342. void Parser::AddWarning(const string& warning) {
  343. if (error_collector_ != nullptr) {
  344. error_collector_->AddWarning(input_->current().line,
  345. input_->current().column, warning);
  346. }
  347. }
  348. // -------------------------------------------------------------------
  349. Parser::LocationRecorder::LocationRecorder(Parser* parser)
  350. : parser_(parser),
  351. source_code_info_(parser->source_code_info_),
  352. location_(parser_->source_code_info_->add_location()) {
  353. location_->add_span(parser_->input_->current().line);
  354. location_->add_span(parser_->input_->current().column);
  355. }
  356. Parser::LocationRecorder::LocationRecorder(const LocationRecorder& parent) {
  357. Init(parent, parent.source_code_info_);
  358. }
  359. Parser::LocationRecorder::LocationRecorder(const LocationRecorder& parent,
  360. int path1,
  361. SourceCodeInfo* source_code_info) {
  362. Init(parent, source_code_info);
  363. AddPath(path1);
  364. }
  365. Parser::LocationRecorder::LocationRecorder(const LocationRecorder& parent,
  366. int path1) {
  367. Init(parent, parent.source_code_info_);
  368. AddPath(path1);
  369. }
  370. Parser::LocationRecorder::LocationRecorder(const LocationRecorder& parent,
  371. int path1, int path2) {
  372. Init(parent, parent.source_code_info_);
  373. AddPath(path1);
  374. AddPath(path2);
  375. }
  376. void Parser::LocationRecorder::Init(const LocationRecorder& parent,
  377. SourceCodeInfo* source_code_info) {
  378. parser_ = parent.parser_;
  379. source_code_info_ = source_code_info;
  380. location_ = source_code_info_->add_location();
  381. location_->mutable_path()->CopyFrom(parent.location_->path());
  382. location_->add_span(parser_->input_->current().line);
  383. location_->add_span(parser_->input_->current().column);
  384. }
  385. Parser::LocationRecorder::~LocationRecorder() {
  386. if (location_->span_size() <= 2) {
  387. EndAt(parser_->input_->previous());
  388. }
  389. }
  390. void Parser::LocationRecorder::AddPath(int path_component) {
  391. location_->add_path(path_component);
  392. }
  393. void Parser::LocationRecorder::StartAt(const io::Tokenizer::Token& token) {
  394. location_->set_span(0, token.line);
  395. location_->set_span(1, token.column);
  396. }
  397. void Parser::LocationRecorder::StartAt(const LocationRecorder& other) {
  398. location_->set_span(0, other.location_->span(0));
  399. location_->set_span(1, other.location_->span(1));
  400. }
  401. void Parser::LocationRecorder::EndAt(const io::Tokenizer::Token& token) {
  402. if (token.line != location_->span(0)) {
  403. location_->add_span(token.line);
  404. }
  405. location_->add_span(token.end_column);
  406. }
  407. void Parser::LocationRecorder::RecordLegacyLocation(
  408. const Message* descriptor,
  409. DescriptorPool::ErrorCollector::ErrorLocation location) {
  410. if (parser_->source_location_table_ != NULL) {
  411. parser_->source_location_table_->Add(
  412. descriptor, location, location_->span(0), location_->span(1));
  413. }
  414. }
  415. void Parser::LocationRecorder::RecordLegacyImportLocation(
  416. const Message* descriptor, const string& name) {
  417. if (parser_->source_location_table_ != nullptr) {
  418. parser_->source_location_table_->AddImport(
  419. descriptor, name, location_->span(0), location_->span(1));
  420. }
  421. }
  422. int Parser::LocationRecorder::CurrentPathSize() const {
  423. return location_->path_size();
  424. }
  425. void Parser::LocationRecorder::AttachComments(
  426. std::string* leading, std::string* trailing,
  427. std::vector<std::string>* detached_comments) const {
  428. GOOGLE_CHECK(!location_->has_leading_comments());
  429. GOOGLE_CHECK(!location_->has_trailing_comments());
  430. if (!leading->empty()) {
  431. location_->mutable_leading_comments()->swap(*leading);
  432. }
  433. if (!trailing->empty()) {
  434. location_->mutable_trailing_comments()->swap(*trailing);
  435. }
  436. for (int i = 0; i < detached_comments->size(); ++i) {
  437. location_->add_leading_detached_comments()->swap((*detached_comments)[i]);
  438. }
  439. detached_comments->clear();
  440. }
  441. // -------------------------------------------------------------------
  442. void Parser::SkipStatement() {
  443. while (true) {
  444. if (AtEnd()) {
  445. return;
  446. } else if (LookingAtType(io::Tokenizer::TYPE_SYMBOL)) {
  447. if (TryConsumeEndOfDeclaration(";", NULL)) {
  448. return;
  449. } else if (TryConsume("{")) {
  450. SkipRestOfBlock();
  451. return;
  452. } else if (LookingAt("}")) {
  453. return;
  454. }
  455. }
  456. input_->Next();
  457. }
  458. }
  459. void Parser::SkipRestOfBlock() {
  460. while (true) {
  461. if (AtEnd()) {
  462. return;
  463. } else if (LookingAtType(io::Tokenizer::TYPE_SYMBOL)) {
  464. if (TryConsumeEndOfDeclaration("}", NULL)) {
  465. return;
  466. } else if (TryConsume("{")) {
  467. SkipRestOfBlock();
  468. }
  469. }
  470. input_->Next();
  471. }
  472. }
  473. // ===================================================================
  474. bool Parser::ValidateEnum(const EnumDescriptorProto* proto) {
  475. bool has_allow_alias = false;
  476. bool allow_alias = false;
  477. for (int i = 0; i < proto->options().uninterpreted_option_size(); i++) {
  478. const UninterpretedOption option = proto->options().uninterpreted_option(i);
  479. if (option.name_size() > 1) {
  480. continue;
  481. }
  482. if (!option.name(0).is_extension() &&
  483. option.name(0).name_part() == "allow_alias") {
  484. has_allow_alias = true;
  485. if (option.identifier_value() == "true") {
  486. allow_alias = true;
  487. }
  488. break;
  489. }
  490. }
  491. if (has_allow_alias && !allow_alias) {
  492. std::string error =
  493. "\"" + proto->name() +
  494. "\" declares 'option allow_alias = false;' which has no effect. "
  495. "Please remove the declaration.";
  496. // This needlessly clutters declarations with nops.
  497. AddError(error);
  498. return false;
  499. }
  500. std::set<int> used_values;
  501. bool has_duplicates = false;
  502. for (int i = 0; i < proto->value_size(); ++i) {
  503. const EnumValueDescriptorProto& enum_value = proto->value(i);
  504. if (used_values.find(enum_value.number()) != used_values.end()) {
  505. has_duplicates = true;
  506. break;
  507. } else {
  508. used_values.insert(enum_value.number());
  509. }
  510. }
  511. if (allow_alias && !has_duplicates) {
  512. std::string error =
  513. "\"" + proto->name() +
  514. "\" declares support for enum aliases but no enum values share field "
  515. "numbers. Please remove the unnecessary 'option allow_alias = true;' "
  516. "declaration.";
  517. // Generate an error if an enum declares support for duplicate enum values
  518. // and does not use it protect future authors.
  519. AddError(error);
  520. return false;
  521. }
  522. // Enforce that enum constants must be UPPER_CASE except in case of
  523. // enum_alias.
  524. if (!allow_alias) {
  525. for (const auto& enum_value : proto->value()) {
  526. if (!IsUpperUnderscore(enum_value.name())) {
  527. AddWarning(
  528. "Enum constant should be in UPPER_CASE. Found: " +
  529. enum_value.name() +
  530. ". See https://developers.google.com/protocol-buffers/docs/style");
  531. }
  532. }
  533. }
  534. return true;
  535. }
  536. bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) {
  537. input_ = input;
  538. had_errors_ = false;
  539. syntax_identifier_.clear();
  540. // Note that |file| could be NULL at this point if
  541. // stop_after_syntax_identifier_ is true. So, we conservatively allocate
  542. // SourceCodeInfo on the stack, then swap it into the FileDescriptorProto
  543. // later on.
  544. SourceCodeInfo source_code_info;
  545. source_code_info_ = &source_code_info;
  546. if (LookingAtType(io::Tokenizer::TYPE_START)) {
  547. // Advance to first token.
  548. input_->NextWithComments(NULL, &upcoming_detached_comments_,
  549. &upcoming_doc_comments_);
  550. }
  551. {
  552. LocationRecorder root_location(this);
  553. root_location.RecordLegacyLocation(file,
  554. DescriptorPool::ErrorCollector::OTHER);
  555. if (require_syntax_identifier_ || LookingAt("syntax")) {
  556. if (!ParseSyntaxIdentifier(root_location)) {
  557. // Don't attempt to parse the file if we didn't recognize the syntax
  558. // identifier.
  559. return false;
  560. }
  561. // Store the syntax into the file.
  562. if (file != NULL) file->set_syntax(syntax_identifier_);
  563. } else if (!stop_after_syntax_identifier_) {
  564. GOOGLE_LOG(WARNING) << "No syntax specified for the proto file: " << file->name()
  565. << ". Please use 'syntax = \"proto2\";' "
  566. << "or 'syntax = \"proto3\";' to specify a syntax "
  567. << "version. (Defaulted to proto2 syntax.)";
  568. syntax_identifier_ = "proto2";
  569. }
  570. if (stop_after_syntax_identifier_) return !had_errors_;
  571. // Repeatedly parse statements until we reach the end of the file.
  572. while (!AtEnd()) {
  573. if (!ParseTopLevelStatement(file, root_location)) {
  574. // This statement failed to parse. Skip it, but keep looping to parse
  575. // other statements.
  576. SkipStatement();
  577. if (LookingAt("}")) {
  578. AddError("Unmatched \"}\".");
  579. input_->NextWithComments(NULL, &upcoming_detached_comments_,
  580. &upcoming_doc_comments_);
  581. }
  582. }
  583. }
  584. }
  585. input_ = NULL;
  586. source_code_info_ = NULL;
  587. assert(file != NULL);
  588. source_code_info.Swap(file->mutable_source_code_info());
  589. return !had_errors_;
  590. }
  591. bool Parser::ParseSyntaxIdentifier(const LocationRecorder& parent) {
  592. LocationRecorder syntax_location(parent,
  593. FileDescriptorProto::kSyntaxFieldNumber);
  594. DO(Consume(
  595. "syntax",
  596. "File must begin with a syntax statement, e.g. 'syntax = \"proto2\";'."));
  597. DO(Consume("="));
  598. io::Tokenizer::Token syntax_token = input_->current();
  599. std::string syntax;
  600. DO(ConsumeString(&syntax, "Expected syntax identifier."));
  601. DO(ConsumeEndOfDeclaration(";", &syntax_location));
  602. syntax_identifier_ = syntax;
  603. if (syntax != "proto2" && syntax != "proto3" &&
  604. !stop_after_syntax_identifier_) {
  605. AddError(syntax_token.line, syntax_token.column,
  606. "Unrecognized syntax identifier \"" + syntax +
  607. "\". This parser "
  608. "only recognizes \"proto2\" and \"proto3\".");
  609. return false;
  610. }
  611. return true;
  612. }
  613. bool Parser::ParseTopLevelStatement(FileDescriptorProto* file,
  614. const LocationRecorder& root_location) {
  615. if (TryConsumeEndOfDeclaration(";", NULL)) {
  616. // empty statement; ignore
  617. return true;
  618. } else if (LookingAt("message")) {
  619. LocationRecorder location(root_location,
  620. FileDescriptorProto::kMessageTypeFieldNumber,
  621. file->message_type_size());
  622. return ParseMessageDefinition(file->add_message_type(), location, file);
  623. } else if (LookingAt("enum")) {
  624. LocationRecorder location(root_location,
  625. FileDescriptorProto::kEnumTypeFieldNumber,
  626. file->enum_type_size());
  627. return ParseEnumDefinition(file->add_enum_type(), location, file);
  628. } else if (LookingAt("service")) {
  629. LocationRecorder location(root_location,
  630. FileDescriptorProto::kServiceFieldNumber,
  631. file->service_size());
  632. return ParseServiceDefinition(file->add_service(), location, file);
  633. } else if (LookingAt("extend")) {
  634. LocationRecorder location(root_location,
  635. FileDescriptorProto::kExtensionFieldNumber);
  636. return ParseExtend(
  637. file->mutable_extension(), file->mutable_message_type(), root_location,
  638. FileDescriptorProto::kMessageTypeFieldNumber, location, file);
  639. } else if (LookingAt("import")) {
  640. return ParseImport(file->mutable_dependency(),
  641. file->mutable_public_dependency(),
  642. file->mutable_weak_dependency(), root_location, file);
  643. } else if (LookingAt("package")) {
  644. return ParsePackage(file, root_location, file);
  645. } else if (LookingAt("option")) {
  646. LocationRecorder location(root_location,
  647. FileDescriptorProto::kOptionsFieldNumber);
  648. return ParseOption(file->mutable_options(), location, file,
  649. OPTION_STATEMENT);
  650. } else {
  651. AddError("Expected top-level statement (e.g. \"message\").");
  652. return false;
  653. }
  654. }
  655. // -------------------------------------------------------------------
  656. // Messages
  657. bool Parser::ParseMessageDefinition(
  658. DescriptorProto* message, const LocationRecorder& message_location,
  659. const FileDescriptorProto* containing_file) {
  660. DO(Consume("message"));
  661. {
  662. LocationRecorder location(message_location,
  663. DescriptorProto::kNameFieldNumber);
  664. location.RecordLegacyLocation(message,
  665. DescriptorPool::ErrorCollector::NAME);
  666. DO(ConsumeIdentifier(message->mutable_name(), "Expected message name."));
  667. if (!IsUpperCamelCase(message->name())) {
  668. AddWarning(
  669. "Message name should be in UpperCamelCase. Found: " +
  670. message->name() +
  671. ". See https://developers.google.com/protocol-buffers/docs/style");
  672. }
  673. }
  674. DO(ParseMessageBlock(message, message_location, containing_file));
  675. return true;
  676. }
  677. namespace {
  678. const int kMaxRangeSentinel = -1;
  679. bool IsMessageSetWireFormatMessage(const DescriptorProto& message) {
  680. const MessageOptions& options = message.options();
  681. for (int i = 0; i < options.uninterpreted_option_size(); ++i) {
  682. const UninterpretedOption& uninterpreted = options.uninterpreted_option(i);
  683. if (uninterpreted.name_size() == 1 &&
  684. uninterpreted.name(0).name_part() == "message_set_wire_format" &&
  685. uninterpreted.identifier_value() == "true") {
  686. return true;
  687. }
  688. }
  689. return false;
  690. }
  691. // Modifies any extension ranges that specified 'max' as the end of the
  692. // extension range, and sets them to the type-specific maximum. The actual max
  693. // tag number can only be determined after all options have been parsed.
  694. void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) {
  695. const bool is_message_set = IsMessageSetWireFormatMessage(*message);
  696. const int max_extension_number =
  697. is_message_set ? kint32max : FieldDescriptor::kMaxNumber + 1;
  698. for (int i = 0; i < message->extension_range_size(); ++i) {
  699. if (message->extension_range(i).end() == kMaxRangeSentinel) {
  700. message->mutable_extension_range(i)->set_end(max_extension_number);
  701. }
  702. }
  703. }
  704. // Modifies any reserved ranges that specified 'max' as the end of the
  705. // reserved range, and sets them to the type-specific maximum. The actual max
  706. // tag number can only be determined after all options have been parsed.
  707. void AdjustReservedRangesWithMaxEndNumber(DescriptorProto* message) {
  708. const bool is_message_set = IsMessageSetWireFormatMessage(*message);
  709. const int max_field_number =
  710. is_message_set ? kint32max : FieldDescriptor::kMaxNumber + 1;
  711. for (int i = 0; i < message->reserved_range_size(); ++i) {
  712. if (message->reserved_range(i).end() == kMaxRangeSentinel) {
  713. message->mutable_reserved_range(i)->set_end(max_field_number);
  714. }
  715. }
  716. }
  717. } // namespace
  718. bool Parser::ParseMessageBlock(DescriptorProto* message,
  719. const LocationRecorder& message_location,
  720. const FileDescriptorProto* containing_file) {
  721. DO(ConsumeEndOfDeclaration("{", &message_location));
  722. while (!TryConsumeEndOfDeclaration("}", NULL)) {
  723. if (AtEnd()) {
  724. AddError("Reached end of input in message definition (missing '}').");
  725. return false;
  726. }
  727. if (!ParseMessageStatement(message, message_location, containing_file)) {
  728. // This statement failed to parse. Skip it, but keep looping to parse
  729. // other statements.
  730. SkipStatement();
  731. }
  732. }
  733. if (message->extension_range_size() > 0) {
  734. AdjustExtensionRangesWithMaxEndNumber(message);
  735. }
  736. if (message->reserved_range_size() > 0) {
  737. AdjustReservedRangesWithMaxEndNumber(message);
  738. }
  739. return true;
  740. }
  741. bool Parser::ParseMessageStatement(DescriptorProto* message,
  742. const LocationRecorder& message_location,
  743. const FileDescriptorProto* containing_file) {
  744. if (TryConsumeEndOfDeclaration(";", NULL)) {
  745. // empty statement; ignore
  746. return true;
  747. } else if (LookingAt("message")) {
  748. LocationRecorder location(message_location,
  749. DescriptorProto::kNestedTypeFieldNumber,
  750. message->nested_type_size());
  751. return ParseMessageDefinition(message->add_nested_type(), location,
  752. containing_file);
  753. } else if (LookingAt("enum")) {
  754. LocationRecorder location(message_location,
  755. DescriptorProto::kEnumTypeFieldNumber,
  756. message->enum_type_size());
  757. return ParseEnumDefinition(message->add_enum_type(), location,
  758. containing_file);
  759. } else if (LookingAt("extensions")) {
  760. LocationRecorder location(message_location,
  761. DescriptorProto::kExtensionRangeFieldNumber);
  762. return ParseExtensions(message, location, containing_file);
  763. } else if (LookingAt("reserved")) {
  764. return ParseReserved(message, message_location);
  765. } else if (LookingAt("extend")) {
  766. LocationRecorder location(message_location,
  767. DescriptorProto::kExtensionFieldNumber);
  768. return ParseExtend(message->mutable_extension(),
  769. message->mutable_nested_type(), message_location,
  770. DescriptorProto::kNestedTypeFieldNumber, location,
  771. containing_file);
  772. } else if (LookingAt("option")) {
  773. LocationRecorder location(message_location,
  774. DescriptorProto::kOptionsFieldNumber);
  775. return ParseOption(message->mutable_options(), location, containing_file,
  776. OPTION_STATEMENT);
  777. } else if (LookingAt("oneof")) {
  778. int oneof_index = message->oneof_decl_size();
  779. LocationRecorder oneof_location(
  780. message_location, DescriptorProto::kOneofDeclFieldNumber, oneof_index);
  781. return ParseOneof(message->add_oneof_decl(), message, oneof_index,
  782. oneof_location, message_location, containing_file);
  783. } else {
  784. LocationRecorder location(message_location,
  785. DescriptorProto::kFieldFieldNumber,
  786. message->field_size());
  787. return ParseMessageField(
  788. message->add_field(), message->mutable_nested_type(), message_location,
  789. DescriptorProto::kNestedTypeFieldNumber, location, containing_file);
  790. }
  791. }
  792. bool Parser::ParseMessageField(FieldDescriptorProto* field,
  793. RepeatedPtrField<DescriptorProto>* messages,
  794. const LocationRecorder& parent_location,
  795. int location_field_number_for_nested_type,
  796. const LocationRecorder& field_location,
  797. const FileDescriptorProto* containing_file) {
  798. {
  799. FieldDescriptorProto::Label label;
  800. if (ParseLabel(&label, field_location, containing_file)) {
  801. field->set_label(label);
  802. if (label == FieldDescriptorProto::LABEL_OPTIONAL &&
  803. syntax_identifier_ == "proto3") {
  804. AddError(
  805. "Explicit 'optional' labels are disallowed in the Proto3 syntax. "
  806. "To define 'optional' fields in Proto3, simply remove the "
  807. "'optional' label, as fields are 'optional' by default.");
  808. }
  809. }
  810. }
  811. return ParseMessageFieldNoLabel(field, messages, parent_location,
  812. location_field_number_for_nested_type,
  813. field_location, containing_file);
  814. }
  815. bool Parser::ParseMessageFieldNoLabel(
  816. FieldDescriptorProto* field, RepeatedPtrField<DescriptorProto>* messages,
  817. const LocationRecorder& parent_location,
  818. int location_field_number_for_nested_type,
  819. const LocationRecorder& field_location,
  820. const FileDescriptorProto* containing_file) {
  821. MapField map_field;
  822. // Parse type.
  823. {
  824. LocationRecorder location(field_location); // add path later
  825. location.RecordLegacyLocation(field, DescriptorPool::ErrorCollector::TYPE);
  826. bool type_parsed = false;
  827. FieldDescriptorProto::Type type = FieldDescriptorProto::TYPE_INT32;
  828. std::string type_name;
  829. // Special case map field. We only treat the field as a map field if the
  830. // field type name starts with the word "map" with a following "<".
  831. if (TryConsume("map")) {
  832. if (LookingAt("<")) {
  833. map_field.is_map_field = true;
  834. } else {
  835. // False positive
  836. type_parsed = true;
  837. type_name = "map";
  838. }
  839. }
  840. if (map_field.is_map_field) {
  841. if (field->has_oneof_index()) {
  842. AddError("Map fields are not allowed in oneofs.");
  843. return false;
  844. }
  845. if (field->has_label()) {
  846. AddError(
  847. "Field labels (required/optional/repeated) are not allowed on "
  848. "map fields.");
  849. return false;
  850. }
  851. if (field->has_extendee()) {
  852. AddError("Map fields are not allowed to be extensions.");
  853. return false;
  854. }
  855. field->set_label(FieldDescriptorProto::LABEL_REPEATED);
  856. DO(Consume("<"));
  857. DO(ParseType(&map_field.key_type, &map_field.key_type_name));
  858. DO(Consume(","));
  859. DO(ParseType(&map_field.value_type, &map_field.value_type_name));
  860. DO(Consume(">"));
  861. // Defer setting of the type name of the map field until the
  862. // field name is parsed. Add the source location though.
  863. location.AddPath(FieldDescriptorProto::kTypeNameFieldNumber);
  864. } else {
  865. // Handle the case where no explicit label is given for a non-map field.
  866. if (!field->has_label() && DefaultToOptionalFields()) {
  867. field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
  868. }
  869. if (!field->has_label()) {
  870. AddError("Expected \"required\", \"optional\", or \"repeated\".");
  871. // We can actually reasonably recover here by just assuming the user
  872. // forgot the label altogether.
  873. field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
  874. }
  875. // Handle the case where the actual type is a message or enum named "map",
  876. // which we already consumed in the code above.
  877. if (!type_parsed) {
  878. DO(ParseType(&type, &type_name));
  879. }
  880. if (type_name.empty()) {
  881. location.AddPath(FieldDescriptorProto::kTypeFieldNumber);
  882. field->set_type(type);
  883. } else {
  884. location.AddPath(FieldDescriptorProto::kTypeNameFieldNumber);
  885. field->set_type_name(type_name);
  886. }
  887. }
  888. }
  889. // Parse name and '='.
  890. io::Tokenizer::Token name_token = input_->current();
  891. {
  892. LocationRecorder location(field_location,
  893. FieldDescriptorProto::kNameFieldNumber);
  894. location.RecordLegacyLocation(field, DescriptorPool::ErrorCollector::NAME);
  895. DO(ConsumeIdentifier(field->mutable_name(), "Expected field name."));
  896. if (!IsLowerUnderscore(field->name())) {
  897. AddWarning(
  898. "Field name should be lowercase. Found: " + field->name() +
  899. ". See: https://developers.google.com/protocol-buffers/docs/style");
  900. }
  901. if (IsNumberFollowUnderscore(field->name())) {
  902. AddWarning(
  903. "Number should not come right after an underscore. Found: " +
  904. field->name() +
  905. ". See: https://developers.google.com/protocol-buffers/docs/style");
  906. }
  907. }
  908. DO(Consume("=", "Missing field number."));
  909. // Parse field number.
  910. {
  911. LocationRecorder location(field_location,
  912. FieldDescriptorProto::kNumberFieldNumber);
  913. location.RecordLegacyLocation(field,
  914. DescriptorPool::ErrorCollector::NUMBER);
  915. int number;
  916. DO(ConsumeInteger(&number, "Expected field number."));
  917. field->set_number(number);
  918. }
  919. // Parse options.
  920. DO(ParseFieldOptions(field, field_location, containing_file));
  921. // Deal with groups.
  922. if (field->has_type() && field->type() == FieldDescriptorProto::TYPE_GROUP) {
  923. // Awkward: Since a group declares both a message type and a field, we
  924. // have to create overlapping locations.
  925. LocationRecorder group_location(parent_location);
  926. group_location.StartAt(field_location);
  927. group_location.AddPath(location_field_number_for_nested_type);
  928. group_location.AddPath(messages->size());
  929. DescriptorProto* group = messages->Add();
  930. group->set_name(field->name());
  931. // Record name location to match the field name's location.
  932. {
  933. LocationRecorder location(group_location,
  934. DescriptorProto::kNameFieldNumber);
  935. location.StartAt(name_token);
  936. location.EndAt(name_token);
  937. location.RecordLegacyLocation(group,
  938. DescriptorPool::ErrorCollector::NAME);
  939. }
  940. // The field's type_name also comes from the name. Confusing!
  941. {
  942. LocationRecorder location(field_location,
  943. FieldDescriptorProto::kTypeNameFieldNumber);
  944. location.StartAt(name_token);
  945. location.EndAt(name_token);
  946. }
  947. // As a hack for backwards-compatibility, we force the group name to start
  948. // with a capital letter and lower-case the field name. New code should
  949. // not use groups; it should use nested messages.
  950. if (group->name()[0] < 'A' || 'Z' < group->name()[0]) {
  951. AddError(name_token.line, name_token.column,
  952. "Group names must start with a capital letter.");
  953. }
  954. LowerString(field->mutable_name());
  955. field->set_type_name(group->name());
  956. if (LookingAt("{")) {
  957. DO(ParseMessageBlock(group, group_location, containing_file));
  958. } else {
  959. AddError("Missing group body.");
  960. return false;
  961. }
  962. } else {
  963. DO(ConsumeEndOfDeclaration(";", &field_location));
  964. }
  965. // Create a map entry type if this is a map field.
  966. if (map_field.is_map_field) {
  967. GenerateMapEntry(map_field, field, messages);
  968. }
  969. return true;
  970. }
  971. void Parser::GenerateMapEntry(const MapField& map_field,
  972. FieldDescriptorProto* field,
  973. RepeatedPtrField<DescriptorProto>* messages) {
  974. DescriptorProto* entry = messages->Add();
  975. std::string entry_name = MapEntryName(field->name());
  976. field->set_type_name(entry_name);
  977. entry->set_name(entry_name);
  978. entry->mutable_options()->set_map_entry(true);
  979. FieldDescriptorProto* key_field = entry->add_field();
  980. key_field->set_name("key");
  981. key_field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
  982. key_field->set_number(1);
  983. if (map_field.key_type_name.empty()) {
  984. key_field->set_type(map_field.key_type);
  985. } else {
  986. key_field->set_type_name(map_field.key_type_name);
  987. }
  988. FieldDescriptorProto* value_field = entry->add_field();
  989. value_field->set_name("value");
  990. value_field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
  991. value_field->set_number(2);
  992. if (map_field.value_type_name.empty()) {
  993. value_field->set_type(map_field.value_type);
  994. } else {
  995. value_field->set_type_name(map_field.value_type_name);
  996. }
  997. // Propagate the "enforce_utf8" option to key and value fields if they
  998. // are strings. This helps simplify the implementation of code generators
  999. // and also reflection-based parsing code.
  1000. //
  1001. // The following definition:
  1002. // message Foo {
  1003. // map<string, string> value = 1 [enforce_utf8 = false];
  1004. // }
  1005. // will be interpreted as:
  1006. // message Foo {
  1007. // message ValueEntry {
  1008. // option map_entry = true;
  1009. // string key = 1 [enforce_utf8 = false];
  1010. // string value = 2 [enforce_utf8 = false];
  1011. // }
  1012. // repeated ValueEntry value = 1 [enforce_utf8 = false];
  1013. // }
  1014. //
  1015. // TODO(xiaofeng): Remove this when the "enforce_utf8" option is removed
  1016. // from protocol compiler.
  1017. for (int i = 0; i < field->options().uninterpreted_option_size(); ++i) {
  1018. const UninterpretedOption& option =
  1019. field->options().uninterpreted_option(i);
  1020. if (option.name_size() == 1 &&
  1021. option.name(0).name_part() == "enforce_utf8" &&
  1022. !option.name(0).is_extension()) {
  1023. if (key_field->type() == FieldDescriptorProto::TYPE_STRING) {
  1024. key_field->mutable_options()->add_uninterpreted_option()->CopyFrom(
  1025. option);
  1026. }
  1027. if (value_field->type() == FieldDescriptorProto::TYPE_STRING) {
  1028. value_field->mutable_options()->add_uninterpreted_option()->CopyFrom(
  1029. option);
  1030. }
  1031. }
  1032. }
  1033. }
  1034. bool Parser::ParseFieldOptions(FieldDescriptorProto* field,
  1035. const LocationRecorder& field_location,
  1036. const FileDescriptorProto* containing_file) {
  1037. if (!LookingAt("[")) return true;
  1038. LocationRecorder location(field_location,
  1039. FieldDescriptorProto::kOptionsFieldNumber);
  1040. DO(Consume("["));
  1041. // Parse field options.
  1042. do {
  1043. if (LookingAt("default")) {
  1044. // We intentionally pass field_location rather than location here, since
  1045. // the default value is not actually an option.
  1046. DO(ParseDefaultAssignment(field, field_location, containing_file));
  1047. } else if (LookingAt("json_name")) {
  1048. // Like default value, this "json_name" is not an actual option.
  1049. DO(ParseJsonName(field, field_location, containing_file));
  1050. } else {
  1051. DO(ParseOption(field->mutable_options(), location, containing_file,
  1052. OPTION_ASSIGNMENT));
  1053. }
  1054. } while (TryConsume(","));
  1055. DO(Consume("]"));
  1056. return true;
  1057. }
  1058. bool Parser::ParseDefaultAssignment(
  1059. FieldDescriptorProto* field, const LocationRecorder& field_location,
  1060. const FileDescriptorProto* containing_file) {
  1061. if (field->has_default_value()) {
  1062. AddError("Already set option \"default\".");
  1063. field->clear_default_value();
  1064. }
  1065. DO(Consume("default"));
  1066. DO(Consume("="));
  1067. LocationRecorder location(field_location,
  1068. FieldDescriptorProto::kDefaultValueFieldNumber);
  1069. location.RecordLegacyLocation(field,
  1070. DescriptorPool::ErrorCollector::DEFAULT_VALUE);
  1071. std::string* default_value = field->mutable_default_value();
  1072. if (!field->has_type()) {
  1073. // The field has a type name, but we don't know if it is a message or an
  1074. // enum yet. (If it were a primitive type, |field| would have a type set
  1075. // already.) In this case, simply take the current string as the default
  1076. // value; we will catch the error later if it is not a valid enum value.
  1077. // (N.B. that we do not check whether the current token is an identifier:
  1078. // doing so throws strange errors when the user mistypes a primitive
  1079. // typename and we assume it's an enum. E.g.: "optional int foo = 1 [default
  1080. // = 42]". In such a case the fundamental error is really that "int" is not
  1081. // a type, not that "42" is not an identifier. See b/12533582.)
  1082. *default_value = input_->current().text;
  1083. input_->Next();
  1084. return true;
  1085. }
  1086. switch (field->type()) {
  1087. case FieldDescriptorProto::TYPE_INT32:
  1088. case FieldDescriptorProto::TYPE_INT64:
  1089. case FieldDescriptorProto::TYPE_SINT32:
  1090. case FieldDescriptorProto::TYPE_SINT64:
  1091. case FieldDescriptorProto::TYPE_SFIXED32:
  1092. case FieldDescriptorProto::TYPE_SFIXED64: {
  1093. uint64 max_value = kint64max;
  1094. if (field->type() == FieldDescriptorProto::TYPE_INT32 ||
  1095. field->type() == FieldDescriptorProto::TYPE_SINT32 ||
  1096. field->type() == FieldDescriptorProto::TYPE_SFIXED32) {
  1097. max_value = kint32max;
  1098. }
  1099. // These types can be negative.
  1100. if (TryConsume("-")) {
  1101. default_value->append("-");
  1102. // Two's complement always has one more negative value than positive.
  1103. ++max_value;
  1104. }
  1105. // Parse the integer to verify that it is not out-of-range.
  1106. uint64 value;
  1107. DO(ConsumeInteger64(max_value, &value,
  1108. "Expected integer for field default value."));
  1109. // And stringify it again.
  1110. default_value->append(StrCat(value));
  1111. break;
  1112. }
  1113. case FieldDescriptorProto::TYPE_UINT32:
  1114. case FieldDescriptorProto::TYPE_UINT64:
  1115. case FieldDescriptorProto::TYPE_FIXED32:
  1116. case FieldDescriptorProto::TYPE_FIXED64: {
  1117. uint64 max_value = kuint64max;
  1118. if (field->type() == FieldDescriptorProto::TYPE_UINT32 ||
  1119. field->type() == FieldDescriptorProto::TYPE_FIXED32) {
  1120. max_value = kuint32max;
  1121. }
  1122. // Numeric, not negative.
  1123. if (TryConsume("-")) {
  1124. AddError("Unsigned field can't have negative default value.");
  1125. }
  1126. // Parse the integer to verify that it is not out-of-range.
  1127. uint64 value;
  1128. DO(ConsumeInteger64(max_value, &value,
  1129. "Expected integer for field default value."));
  1130. // And stringify it again.
  1131. default_value->append(StrCat(value));
  1132. break;
  1133. }
  1134. case FieldDescriptorProto::TYPE_FLOAT:
  1135. case FieldDescriptorProto::TYPE_DOUBLE:
  1136. // These types can be negative.
  1137. if (TryConsume("-")) {
  1138. default_value->append("-");
  1139. }
  1140. // Parse the integer because we have to convert hex integers to decimal
  1141. // floats.
  1142. double value;
  1143. DO(ConsumeNumber(&value, "Expected number."));
  1144. // And stringify it again.
  1145. default_value->append(SimpleDtoa(value));
  1146. break;
  1147. case FieldDescriptorProto::TYPE_BOOL:
  1148. if (TryConsume("true")) {
  1149. default_value->assign("true");
  1150. } else if (TryConsume("false")) {
  1151. default_value->assign("false");
  1152. } else {
  1153. AddError("Expected \"true\" or \"false\".");
  1154. return false;
  1155. }
  1156. break;
  1157. case FieldDescriptorProto::TYPE_STRING:
  1158. // Note: When file opton java_string_check_utf8 is true, if a
  1159. // non-string representation (eg byte[]) is later supported, it must
  1160. // be checked for UTF-8-ness.
  1161. DO(ConsumeString(default_value,
  1162. "Expected string for field default "
  1163. "value."));
  1164. break;
  1165. case FieldDescriptorProto::TYPE_BYTES:
  1166. DO(ConsumeString(default_value, "Expected string."));
  1167. *default_value = CEscape(*default_value);
  1168. break;
  1169. case FieldDescriptorProto::TYPE_ENUM:
  1170. DO(ConsumeIdentifier(default_value,
  1171. "Expected enum identifier for field "
  1172. "default value."));
  1173. break;
  1174. case FieldDescriptorProto::TYPE_MESSAGE:
  1175. case FieldDescriptorProto::TYPE_GROUP:
  1176. AddError("Messages can't have default values.");
  1177. return false;
  1178. }
  1179. return true;
  1180. }
  1181. bool Parser::ParseJsonName(FieldDescriptorProto* field,
  1182. const LocationRecorder& field_location,
  1183. const FileDescriptorProto* containing_file) {
  1184. if (field->has_json_name()) {
  1185. AddError("Already set option \"json_name\".");
  1186. field->clear_json_name();
  1187. }
  1188. LocationRecorder location(field_location,
  1189. FieldDescriptorProto::kJsonNameFieldNumber);
  1190. location.RecordLegacyLocation(field,
  1191. DescriptorPool::ErrorCollector::OPTION_NAME);
  1192. DO(Consume("json_name"));
  1193. DO(Consume("="));
  1194. LocationRecorder value_location(location);
  1195. value_location.RecordLegacyLocation(
  1196. field, DescriptorPool::ErrorCollector::OPTION_VALUE);
  1197. DO(ConsumeString(field->mutable_json_name(),
  1198. "Expected string for JSON name."));
  1199. return true;
  1200. }
  1201. bool Parser::ParseOptionNamePart(UninterpretedOption* uninterpreted_option,
  1202. const LocationRecorder& part_location,
  1203. const FileDescriptorProto* containing_file) {
  1204. UninterpretedOption::NamePart* name = uninterpreted_option->add_name();
  1205. std::string identifier; // We parse identifiers into this string.
  1206. if (LookingAt("(")) { // This is an extension.
  1207. DO(Consume("("));
  1208. {
  1209. LocationRecorder location(
  1210. part_location, UninterpretedOption::NamePart::kNamePartFieldNumber);
  1211. // An extension name consists of dot-separated identifiers, and may begin
  1212. // with a dot.
  1213. if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
  1214. DO(ConsumeIdentifier(&identifier, "Expected identifier."));
  1215. name->mutable_name_part()->append(identifier);
  1216. }
  1217. while (LookingAt(".")) {
  1218. DO(Consume("."));
  1219. name->mutable_name_part()->append(".");
  1220. DO(ConsumeIdentifier(&identifier, "Expected identifier."));
  1221. name->mutable_name_part()->append(identifier);
  1222. }
  1223. }
  1224. DO(Consume(")"));
  1225. name->set_is_extension(true);
  1226. } else { // This is a regular field.
  1227. LocationRecorder location(
  1228. part_location, UninterpretedOption::NamePart::kNamePartFieldNumber);
  1229. DO(ConsumeIdentifier(&identifier, "Expected identifier."));
  1230. name->mutable_name_part()->append(identifier);
  1231. name->set_is_extension(false);
  1232. }
  1233. return true;
  1234. }
  1235. bool Parser::ParseUninterpretedBlock(std::string* value) {
  1236. // Note that enclosing braces are not added to *value.
  1237. // We do NOT use ConsumeEndOfStatement for this brace because it's delimiting
  1238. // an expression, not a block of statements.
  1239. DO(Consume("{"));
  1240. int brace_depth = 1;
  1241. while (!AtEnd()) {
  1242. if (LookingAt("{")) {
  1243. brace_depth++;
  1244. } else if (LookingAt("}")) {
  1245. brace_depth--;
  1246. if (brace_depth == 0) {
  1247. input_->Next();
  1248. return true;
  1249. }
  1250. }
  1251. // TODO(sanjay): Interpret line/column numbers to preserve formatting
  1252. if (!value->empty()) value->push_back(' ');
  1253. value->append(input_->current().text);
  1254. input_->Next();
  1255. }
  1256. AddError("Unexpected end of stream while parsing aggregate value.");
  1257. return false;
  1258. }
  1259. // We don't interpret the option here. Instead we store it in an
  1260. // UninterpretedOption, to be interpreted later.
  1261. bool Parser::ParseOption(Message* options,
  1262. const LocationRecorder& options_location,
  1263. const FileDescriptorProto* containing_file,
  1264. OptionStyle style) {
  1265. // Create an entry in the uninterpreted_option field.
  1266. const FieldDescriptor* uninterpreted_option_field =
  1267. options->GetDescriptor()->FindFieldByName("uninterpreted_option");
  1268. GOOGLE_CHECK(uninterpreted_option_field != NULL)
  1269. << "No field named \"uninterpreted_option\" in the Options proto.";
  1270. const Reflection* reflection = options->GetReflection();
  1271. LocationRecorder location(
  1272. options_location, uninterpreted_option_field->number(),
  1273. reflection->FieldSize(*options, uninterpreted_option_field));
  1274. if (style == OPTION_STATEMENT) {
  1275. DO(Consume("option"));
  1276. }
  1277. UninterpretedOption* uninterpreted_option =
  1278. down_cast<UninterpretedOption*>(options->GetReflection()->AddMessage(
  1279. options, uninterpreted_option_field));
  1280. // Parse dot-separated name.
  1281. {
  1282. LocationRecorder name_location(location,
  1283. UninterpretedOption::kNameFieldNumber);
  1284. name_location.RecordLegacyLocation(
  1285. uninterpreted_option, DescriptorPool::ErrorCollector::OPTION_NAME);
  1286. {
  1287. LocationRecorder part_location(name_location,
  1288. uninterpreted_option->name_size());
  1289. DO(ParseOptionNamePart(uninterpreted_option, part_location,
  1290. containing_file));
  1291. }
  1292. while (LookingAt(".")) {
  1293. DO(Consume("."));
  1294. LocationRecorder part_location(name_location,
  1295. uninterpreted_option->name_size());
  1296. DO(ParseOptionNamePart(uninterpreted_option, part_location,
  1297. containing_file));
  1298. }
  1299. }
  1300. DO(Consume("="));
  1301. {
  1302. LocationRecorder value_location(location);
  1303. value_location.RecordLegacyLocation(
  1304. uninterpreted_option, DescriptorPool::ErrorCollector::OPTION_VALUE);
  1305. // All values are a single token, except for negative numbers, which consist
  1306. // of a single '-' symbol, followed by a positive number.
  1307. bool is_negative = TryConsume("-");
  1308. switch (input_->current().type) {
  1309. case io::Tokenizer::TYPE_START:
  1310. GOOGLE_LOG(FATAL) << "Trying to read value before any tokens have been read.";
  1311. return false;
  1312. case io::Tokenizer::TYPE_END:
  1313. AddError("Unexpected end of stream while parsing option value.");
  1314. return false;
  1315. case io::Tokenizer::TYPE_IDENTIFIER: {
  1316. value_location.AddPath(
  1317. UninterpretedOption::kIdentifierValueFieldNumber);
  1318. if (is_negative) {
  1319. AddError("Invalid '-' symbol before identifier.");
  1320. return false;
  1321. }
  1322. std::string value;
  1323. DO(ConsumeIdentifier(&value, "Expected identifier."));
  1324. uninterpreted_option->set_identifier_value(value);
  1325. break;
  1326. }
  1327. case io::Tokenizer::TYPE_INTEGER: {
  1328. uint64 value;
  1329. uint64 max_value =
  1330. is_negative ? static_cast<uint64>(kint64max) + 1 : kuint64max;
  1331. DO(ConsumeInteger64(max_value, &value, "Expected integer."));
  1332. if (is_negative) {
  1333. value_location.AddPath(
  1334. UninterpretedOption::kNegativeIntValueFieldNumber);
  1335. uninterpreted_option->set_negative_int_value(
  1336. static_cast<int64>(-value));
  1337. } else {
  1338. value_location.AddPath(
  1339. UninterpretedOption::kPositiveIntValueFieldNumber);
  1340. uninterpreted_option->set_positive_int_value(value);
  1341. }
  1342. break;
  1343. }
  1344. case io::Tokenizer::TYPE_FLOAT: {
  1345. value_location.AddPath(UninterpretedOption::kDoubleValueFieldNumber);
  1346. double value;
  1347. DO(ConsumeNumber(&value, "Expected number."));
  1348. uninterpreted_option->set_double_value(is_negative ? -value : value);
  1349. break;
  1350. }
  1351. case io::Tokenizer::TYPE_STRING: {
  1352. value_location.AddPath(UninterpretedOption::kStringValueFieldNumber);
  1353. if (is_negative) {
  1354. AddError("Invalid '-' symbol before string.");
  1355. return false;
  1356. }
  1357. std::string value;
  1358. DO(ConsumeString(&value, "Expected string."));
  1359. uninterpreted_option->set_string_value(value);
  1360. break;
  1361. }
  1362. case io::Tokenizer::TYPE_SYMBOL:
  1363. if (LookingAt("{")) {
  1364. value_location.AddPath(
  1365. UninterpretedOption::kAggregateValueFieldNumber);
  1366. DO(ParseUninterpretedBlock(
  1367. uninterpreted_option->mutable_aggregate_value()));
  1368. } else {
  1369. AddError("Expected option value.");
  1370. return false;
  1371. }
  1372. break;
  1373. }
  1374. }
  1375. if (style == OPTION_STATEMENT) {
  1376. DO(ConsumeEndOfDeclaration(";", &location));
  1377. }
  1378. return true;
  1379. }
  1380. bool Parser::ParseExtensions(DescriptorProto* message,
  1381. const LocationRecorder& extensions_location,
  1382. const FileDescriptorProto* containing_file) {
  1383. // Parse the declaration.
  1384. DO(Consume("extensions"));
  1385. int old_range_size = message->extension_range_size();
  1386. do {
  1387. // Note that kExtensionRangeFieldNumber was already pushed by the parent.
  1388. LocationRecorder location(extensions_location,
  1389. message->extension_range_size());
  1390. DescriptorProto::ExtensionRange* range = message->add_extension_range();
  1391. location.RecordLegacyLocation(range,
  1392. DescriptorPool::ErrorCollector::NUMBER);
  1393. int start, end;
  1394. io::Tokenizer::Token start_token;
  1395. {
  1396. LocationRecorder start_location(
  1397. location, DescriptorProto::ExtensionRange::kStartFieldNumber);
  1398. start_token = input_->current();
  1399. DO(ConsumeInteger(&start, "Expected field number range."));
  1400. }
  1401. if (TryConsume("to")) {
  1402. LocationRecorder end_location(
  1403. location, DescriptorProto::ExtensionRange::kEndFieldNumber);
  1404. if (TryConsume("max")) {
  1405. // Set to the sentinel value - 1 since we increment the value below.
  1406. // The actual value of the end of the range should be set with
  1407. // AdjustExtensionRangesWithMaxEndNumber.
  1408. end = kMaxRangeSentinel - 1;
  1409. } else {
  1410. DO(ConsumeInteger(&end, "Expected integer."));
  1411. }
  1412. } else {
  1413. LocationRecorder end_location(
  1414. location, DescriptorProto::ExtensionRange::kEndFieldNumber);
  1415. end_location.StartAt(start_token);
  1416. end_location.EndAt(start_token);
  1417. end = start;
  1418. }
  1419. // Users like to specify inclusive ranges, but in code we like the end
  1420. // number to be exclusive.
  1421. ++end;
  1422. range->set_start(start);
  1423. range->set_end(end);
  1424. } while (TryConsume(","));
  1425. if (LookingAt("[")) {
  1426. int range_number_index = extensions_location.CurrentPathSize();
  1427. SourceCodeInfo info;
  1428. // Parse extension range options in the first range.
  1429. ExtensionRangeOptions* options =
  1430. message->mutable_extension_range(old_range_size)->mutable_options();
  1431. {
  1432. LocationRecorder index_location(
  1433. extensions_location, 0 /* we fill this in w/ actual index below */,
  1434. &info);
  1435. LocationRecorder location(
  1436. index_location, DescriptorProto::ExtensionRange::kOptionsFieldNumber);
  1437. DO(Consume("["));
  1438. do {
  1439. DO(ParseOption(options, location, containing_file, OPTION_ASSIGNMENT));
  1440. } while (TryConsume(","));
  1441. DO(Consume("]"));
  1442. }
  1443. // Then copy the extension range options to all of the other ranges we've
  1444. // parsed.
  1445. for (int i = old_range_size + 1; i < message->extension_range_size(); i++) {
  1446. message->mutable_extension_range(i)->mutable_options()->CopyFrom(
  1447. *options);
  1448. }
  1449. // and copy source locations to the other ranges, too
  1450. for (int i = old_range_size; i < message->extension_range_size(); i++) {
  1451. for (int j = 0; j < info.location_size(); j++) {
  1452. if (info.location(j).path_size() == range_number_index + 1) {
  1453. // this location's path is up to the extension range index, but
  1454. // doesn't include options; so it's redundant with location above
  1455. continue;
  1456. }
  1457. SourceCodeInfo_Location* dest = source_code_info_->add_location();
  1458. *dest = info.location(j);
  1459. dest->set_path(range_number_index, i);
  1460. }
  1461. }
  1462. }
  1463. DO(ConsumeEndOfDeclaration(";", &extensions_location));
  1464. return true;
  1465. }
  1466. // This is similar to extension range parsing, except that it accepts field
  1467. // name literals.
  1468. bool Parser::ParseReserved(DescriptorProto* message,
  1469. const LocationRecorder& message_location) {
  1470. io::Tokenizer::Token start_token = input_->current();
  1471. // Parse the declaration.
  1472. DO(Consume("reserved"));
  1473. if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
  1474. LocationRecorder location(message_location,
  1475. DescriptorProto::kReservedNameFieldNumber);
  1476. location.StartAt(start_token);
  1477. return ParseReservedNames(message, location);
  1478. } else {
  1479. LocationRecorder location(message_location,
  1480. DescriptorProto::kReservedRangeFieldNumber);
  1481. location.StartAt(start_token);
  1482. return ParseReservedNumbers(message, location);
  1483. }
  1484. }
  1485. bool Parser::ParseReservedNames(DescriptorProto* message,
  1486. const LocationRecorder& parent_location) {
  1487. do {
  1488. LocationRecorder location(parent_location, message->reserved_name_size());
  1489. DO(ConsumeString(message->add_reserved_name(), "Expected field name."));
  1490. } while (TryConsume(","));
  1491. DO(ConsumeEndOfDeclaration(";", &parent_location));
  1492. return true;
  1493. }
  1494. bool Parser::ParseReservedNumbers(DescriptorProto* message,
  1495. const LocationRecorder& parent_location) {
  1496. bool first = true;
  1497. do {
  1498. LocationRecorder location(parent_location, message->reserved_range_size());
  1499. DescriptorProto::ReservedRange* range = message->add_reserved_range();
  1500. int start, end;
  1501. io::Tokenizer::Token start_token;
  1502. {
  1503. LocationRecorder start_location(
  1504. location, DescriptorProto::ReservedRange::kStartFieldNumber);
  1505. start_token = input_->current();
  1506. DO(ConsumeInteger(&start, (first ? "Expected field name or number range."
  1507. : "Expected field number range.")));
  1508. }
  1509. if (TryConsume("to")) {
  1510. LocationRecorder end_location(
  1511. location, DescriptorProto::ReservedRange::kEndFieldNumber);
  1512. if (TryConsume("max")) {
  1513. // Set to the sentinel value - 1 since we increment the value below.
  1514. // The actual value of the end of the range should be set with
  1515. // AdjustExtensionRangesWithMaxEndNumber.
  1516. end = kMaxRangeSentinel - 1;
  1517. } else {
  1518. DO(ConsumeInteger(&end, "Expected integer."));
  1519. }
  1520. } else {
  1521. LocationRecorder end_location(
  1522. location, DescriptorProto::ReservedRange::kEndFieldNumber);
  1523. end_location.StartAt(start_token);
  1524. end_location.EndAt(start_token);
  1525. end = start;
  1526. }
  1527. // Users like to specify inclusive ranges, but in code we like the end
  1528. // number to be exclusive.
  1529. ++end;
  1530. range->set_start(start);
  1531. range->set_end(end);
  1532. first = false;
  1533. } while (TryConsume(","));
  1534. DO(ConsumeEndOfDeclaration(";", &parent_location));
  1535. return true;
  1536. }
  1537. bool Parser::ParseReserved(EnumDescriptorProto* message,
  1538. const LocationRecorder& message_location) {
  1539. io::Tokenizer::Token start_token = input_->current();
  1540. // Parse the declaration.
  1541. DO(Consume("reserved"));
  1542. if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
  1543. LocationRecorder location(message_location,
  1544. DescriptorProto::kReservedNameFieldNumber);
  1545. location.StartAt(start_token);
  1546. return ParseReservedNames(message, location);
  1547. } else {
  1548. LocationRecorder location(message_location,
  1549. DescriptorProto::kReservedRangeFieldNumber);
  1550. location.StartAt(start_token);
  1551. return ParseReservedNumbers(message, location);
  1552. }
  1553. }
  1554. bool Parser::ParseReservedNames(EnumDescriptorProto* message,
  1555. const LocationRecorder& parent_location) {
  1556. do {
  1557. LocationRecorder location(parent_location, message->reserved_name_size());
  1558. DO(ConsumeString(message->add_reserved_name(), "Expected enum value."));
  1559. } while (TryConsume(","));
  1560. DO(ConsumeEndOfDeclaration(";", &parent_location));
  1561. return true;
  1562. }
  1563. bool Parser::ParseReservedNumbers(EnumDescriptorProto* message,
  1564. const LocationRecorder& parent_location) {
  1565. bool first = true;
  1566. do {
  1567. LocationRecorder location(parent_location, message->reserved_range_size());
  1568. EnumDescriptorProto::EnumReservedRange* range =
  1569. message->add_reserved_range();
  1570. int start, end;
  1571. io::Tokenizer::Token start_token;
  1572. {
  1573. LocationRecorder start_location(
  1574. location, EnumDescriptorProto::EnumReservedRange::kStartFieldNumber);
  1575. start_token = input_->current();
  1576. DO(ConsumeSignedInteger(&start,
  1577. (first ? "Expected enum value or number range."
  1578. : "Expected enum number range.")));
  1579. }
  1580. if (TryConsume("to")) {
  1581. LocationRecorder end_location(
  1582. location, EnumDescriptorProto::EnumReservedRange::kEndFieldNumber);
  1583. if (TryConsume("max")) {
  1584. // This is in the enum descriptor path, which doesn't have the message
  1585. // set duality to fix up, so it doesn't integrate with the sentinel.
  1586. end = INT_MAX;
  1587. } else {
  1588. DO(ConsumeSignedInteger(&end, "Expected integer."));
  1589. }
  1590. } else {
  1591. LocationRecorder end_location(
  1592. location, EnumDescriptorProto::EnumReservedRange::kEndFieldNumber);
  1593. end_location.StartAt(start_token);
  1594. end_location.EndAt(start_token);
  1595. end = start;
  1596. }
  1597. range->set_start(start);
  1598. range->set_end(end);
  1599. first = false;
  1600. } while (TryConsume(","));
  1601. DO(ConsumeEndOfDeclaration(";", &parent_location));
  1602. return true;
  1603. }
  1604. bool Parser::ParseExtend(RepeatedPtrField<FieldDescriptorProto>* extensions,
  1605. RepeatedPtrField<DescriptorProto>* messages,
  1606. const LocationRecorder& parent_location,
  1607. int location_field_number_for_nested_type,
  1608. const LocationRecorder& extend_location,
  1609. const FileDescriptorProto* containing_file) {
  1610. DO(Consume("extend"));
  1611. // Parse the extendee type.
  1612. io::Tokenizer::Token extendee_start = input_->current();
  1613. std::string extendee;
  1614. DO(ParseUserDefinedType(&extendee));
  1615. io::Tokenizer::Token extendee_end = input_->previous();
  1616. // Parse the block.
  1617. DO(ConsumeEndOfDeclaration("{", &extend_location));
  1618. bool is_first = true;
  1619. do {
  1620. if (AtEnd()) {
  1621. AddError("Reached end of input in extend definition (missing '}').");
  1622. return false;
  1623. }
  1624. // Note that kExtensionFieldNumber was already pushed by the parent.
  1625. LocationRecorder location(extend_location, extensions->size());
  1626. FieldDescriptorProto* field = extensions->Add();
  1627. {
  1628. LocationRecorder extendee_location(
  1629. location, FieldDescriptorProto::kExtendeeFieldNumber);
  1630. extendee_location.StartAt(extendee_start);
  1631. extendee_location.EndAt(extendee_end);
  1632. if (is_first) {
  1633. extendee_location.RecordLegacyLocation(
  1634. field, DescriptorPool::ErrorCollector::EXTENDEE);
  1635. is_first = false;
  1636. }
  1637. }
  1638. field->set_extendee(extendee);
  1639. if (!ParseMessageField(field, messages, parent_location,
  1640. location_field_number_for_nested_type, location,
  1641. containing_file)) {
  1642. // This statement failed to parse. Skip it, but keep looping to parse
  1643. // other statements.
  1644. SkipStatement();
  1645. }
  1646. } while (!TryConsumeEndOfDeclaration("}", NULL));
  1647. return true;
  1648. }
  1649. bool Parser::ParseOneof(OneofDescriptorProto* oneof_decl,
  1650. DescriptorProto* containing_type, int oneof_index,
  1651. const LocationRecorder& oneof_location,
  1652. const LocationRecorder& containing_type_location,
  1653. const FileDescriptorProto* containing_file) {
  1654. DO(Consume("oneof"));
  1655. {
  1656. LocationRecorder name_location(oneof_location,
  1657. OneofDescriptorProto::kNameFieldNumber);
  1658. DO(ConsumeIdentifier(oneof_decl->mutable_name(), "Expected oneof name."));
  1659. }
  1660. DO(ConsumeEndOfDeclaration("{", &oneof_location));
  1661. do {
  1662. if (AtEnd()) {
  1663. AddError("Reached end of input in oneof definition (missing '}').");
  1664. return false;
  1665. }
  1666. if (LookingAt("option")) {
  1667. LocationRecorder option_location(
  1668. oneof_location, OneofDescriptorProto::kOptionsFieldNumber);
  1669. if (!ParseOption(oneof_decl->mutable_options(), option_location,
  1670. containing_file, OPTION_STATEMENT)) {
  1671. return false;
  1672. }
  1673. continue;
  1674. }
  1675. // Print a nice error if the user accidentally tries to place a label
  1676. // on an individual member of a oneof.
  1677. if (LookingAt("required") || LookingAt("optional") ||
  1678. LookingAt("repeated")) {
  1679. AddError(
  1680. "Fields in oneofs must not have labels (required / optional "
  1681. "/ repeated).");
  1682. // We can continue parsing here because we understand what the user
  1683. // meant. The error report will still make parsing fail overall.
  1684. input_->Next();
  1685. }
  1686. LocationRecorder field_location(containing_type_location,
  1687. DescriptorProto::kFieldFieldNumber,
  1688. containing_type->field_size());
  1689. FieldDescriptorProto* field = containing_type->add_field();
  1690. field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
  1691. field->set_oneof_index(oneof_index);
  1692. if (!ParseMessageFieldNoLabel(field, containing_type->mutable_nested_type(),
  1693. containing_type_location,
  1694. DescriptorProto::kNestedTypeFieldNumber,
  1695. field_location, containing_file)) {
  1696. // This statement failed to parse. Skip it, but keep looping to parse
  1697. // other statements.
  1698. SkipStatement();
  1699. }
  1700. } while (!TryConsumeEndOfDeclaration("}", NULL));
  1701. return true;
  1702. }
  1703. // -------------------------------------------------------------------
  1704. // Enums
  1705. bool Parser::ParseEnumDefinition(EnumDescriptorProto* enum_type,
  1706. const LocationRecorder& enum_location,
  1707. const FileDescriptorProto* containing_file) {
  1708. DO(Consume("enum"));
  1709. {
  1710. LocationRecorder location(enum_location,
  1711. EnumDescriptorProto::kNameFieldNumber);
  1712. location.RecordLegacyLocation(enum_type,
  1713. DescriptorPool::ErrorCollector::NAME);
  1714. DO(ConsumeIdentifier(enum_type->mutable_name(), "Expected enum name."));
  1715. }
  1716. DO(ParseEnumBlock(enum_type, enum_location, containing_file));
  1717. DO(ValidateEnum(enum_type));
  1718. return true;
  1719. }
  1720. bool Parser::ParseEnumBlock(EnumDescriptorProto* enum_type,
  1721. const LocationRecorder& enum_location,
  1722. const FileDescriptorProto* containing_file) {
  1723. DO(ConsumeEndOfDeclaration("{", &enum_location));
  1724. while (!TryConsumeEndOfDeclaration("}", NULL)) {
  1725. if (AtEnd()) {
  1726. AddError("Reached end of input in enum definition (missing '}').");
  1727. return false;
  1728. }
  1729. if (!ParseEnumStatement(enum_type, enum_location, containing_file)) {
  1730. // This statement failed to parse. Skip it, but keep looping to parse
  1731. // other statements.
  1732. SkipStatement();
  1733. }
  1734. }
  1735. return true;
  1736. }
  1737. bool Parser::ParseEnumStatement(EnumDescriptorProto* enum_type,
  1738. const LocationRecorder& enum_location,
  1739. const FileDescriptorProto* containing_file) {
  1740. if (TryConsumeEndOfDeclaration(";", NULL)) {
  1741. // empty statement; ignore
  1742. return true;
  1743. } else if (LookingAt("option")) {
  1744. LocationRecorder location(enum_location,
  1745. EnumDescriptorProto::kOptionsFieldNumber);
  1746. return ParseOption(enum_type->mutable_options(), location, containing_file,
  1747. OPTION_STATEMENT);
  1748. } else if (LookingAt("reserved")) {
  1749. return ParseReserved(enum_type, enum_location);
  1750. } else {
  1751. LocationRecorder location(enum_location,
  1752. EnumDescriptorProto::kValueFieldNumber,
  1753. enum_type->value_size());
  1754. return ParseEnumConstant(enum_type->add_value(), location, containing_file);
  1755. }
  1756. }
  1757. bool Parser::ParseEnumConstant(EnumValueDescriptorProto* enum_value,
  1758. const LocationRecorder& enum_value_location,
  1759. const FileDescriptorProto* containing_file) {
  1760. // Parse name.
  1761. {
  1762. LocationRecorder location(enum_value_location,
  1763. EnumValueDescriptorProto::kNameFieldNumber);
  1764. location.RecordLegacyLocation(enum_value,
  1765. DescriptorPool::ErrorCollector::NAME);
  1766. DO(ConsumeIdentifier(enum_value->mutable_name(),
  1767. "Expected enum constant name."));
  1768. }
  1769. DO(Consume("=", "Missing numeric value for enum constant."));
  1770. // Parse value.
  1771. {
  1772. LocationRecorder location(enum_value_location,
  1773. EnumValueDescriptorProto::kNumberFieldNumber);
  1774. location.RecordLegacyLocation(enum_value,
  1775. DescriptorPool::ErrorCollector::NUMBER);
  1776. int number;
  1777. DO(ConsumeSignedInteger(&number, "Expected integer."));
  1778. enum_value->set_number(number);
  1779. }
  1780. DO(ParseEnumConstantOptions(enum_value, enum_value_location,
  1781. containing_file));
  1782. DO(ConsumeEndOfDeclaration(";", &enum_value_location));
  1783. return true;
  1784. }
  1785. bool Parser::ParseEnumConstantOptions(
  1786. EnumValueDescriptorProto* value,
  1787. const LocationRecorder& enum_value_location,
  1788. const FileDescriptorProto* containing_file) {
  1789. if (!LookingAt("[")) return true;
  1790. LocationRecorder location(enum_value_location,
  1791. EnumValueDescriptorProto::kOptionsFieldNumber);
  1792. DO(Consume("["));
  1793. do {
  1794. DO(ParseOption(value->mutable_options(), location, containing_file,
  1795. OPTION_ASSIGNMENT));
  1796. } while (TryConsume(","));
  1797. DO(Consume("]"));
  1798. return true;
  1799. }
  1800. // -------------------------------------------------------------------
  1801. // Services
  1802. bool Parser::ParseServiceDefinition(
  1803. ServiceDescriptorProto* service, const LocationRecorder& service_location,
  1804. const FileDescriptorProto* containing_file) {
  1805. DO(Consume("service"));
  1806. {
  1807. LocationRecorder location(service_location,
  1808. ServiceDescriptorProto::kNameFieldNumber);
  1809. location.RecordLegacyLocation(service,
  1810. DescriptorPool::ErrorCollector::NAME);
  1811. DO(ConsumeIdentifier(service->mutable_name(), "Expected service name."));
  1812. }
  1813. DO(ParseServiceBlock(service, service_location, containing_file));
  1814. return true;
  1815. }
  1816. bool Parser::ParseServiceBlock(ServiceDescriptorProto* service,
  1817. const LocationRecorder& service_location,
  1818. const FileDescriptorProto* containing_file) {
  1819. DO(ConsumeEndOfDeclaration("{", &service_location));
  1820. while (!TryConsumeEndOfDeclaration("}", NULL)) {
  1821. if (AtEnd()) {
  1822. AddError("Reached end of input in service definition (missing '}').");
  1823. return false;
  1824. }
  1825. if (!ParseServiceStatement(service, service_location, containing_file)) {
  1826. // This statement failed to parse. Skip it, but keep looping to parse
  1827. // other statements.
  1828. SkipStatement();
  1829. }
  1830. }
  1831. return true;
  1832. }
  1833. bool Parser::ParseServiceStatement(ServiceDescriptorProto* service,
  1834. const LocationRecorder& service_location,
  1835. const FileDescriptorProto* containing_file) {
  1836. if (TryConsumeEndOfDeclaration(";", NULL)) {
  1837. // empty statement; ignore
  1838. return true;
  1839. } else if (LookingAt("option")) {
  1840. LocationRecorder location(service_location,
  1841. ServiceDescriptorProto::kOptionsFieldNumber);
  1842. return ParseOption(service->mutable_options(), location, containing_file,
  1843. OPTION_STATEMENT);
  1844. } else {
  1845. LocationRecorder location(service_location,
  1846. ServiceDescriptorProto::kMethodFieldNumber,
  1847. service->method_size());
  1848. return ParseServiceMethod(service->add_method(), location, containing_file);
  1849. }
  1850. }
  1851. bool Parser::ParseServiceMethod(MethodDescriptorProto* method,
  1852. const LocationRecorder& method_location,
  1853. const FileDescriptorProto* containing_file) {
  1854. DO(Consume("rpc"));
  1855. {
  1856. LocationRecorder location(method_location,
  1857. MethodDescriptorProto::kNameFieldNumber);
  1858. location.RecordLegacyLocation(method, DescriptorPool::ErrorCollector::NAME);
  1859. DO(ConsumeIdentifier(method->mutable_name(), "Expected method name."));
  1860. }
  1861. // Parse input type.
  1862. DO(Consume("("));
  1863. {
  1864. if (LookingAt("stream")) {
  1865. LocationRecorder location(
  1866. method_location, MethodDescriptorProto::kClientStreamingFieldNumber);
  1867. location.RecordLegacyLocation(method,
  1868. DescriptorPool::ErrorCollector::OTHER);
  1869. method->set_client_streaming(true);
  1870. DO(Consume("stream"));
  1871. }
  1872. LocationRecorder location(method_location,
  1873. MethodDescriptorProto::kInputTypeFieldNumber);
  1874. location.RecordLegacyLocation(method,
  1875. DescriptorPool::ErrorCollector::INPUT_TYPE);
  1876. DO(ParseUserDefinedType(method->mutable_input_type()));
  1877. }
  1878. DO(Consume(")"));
  1879. // Parse output type.
  1880. DO(Consume("returns"));
  1881. DO(Consume("("));
  1882. {
  1883. if (LookingAt("stream")) {
  1884. LocationRecorder location(
  1885. method_location, MethodDescriptorProto::kServerStreamingFieldNumber);
  1886. location.RecordLegacyLocation(method,
  1887. DescriptorPool::ErrorCollector::OTHER);
  1888. DO(Consume("stream"));
  1889. method->set_server_streaming(true);
  1890. }
  1891. LocationRecorder location(method_location,
  1892. MethodDescriptorProto::kOutputTypeFieldNumber);
  1893. location.RecordLegacyLocation(method,
  1894. DescriptorPool::ErrorCollector::OUTPUT_TYPE);
  1895. DO(ParseUserDefinedType(method->mutable_output_type()));
  1896. }
  1897. DO(Consume(")"));
  1898. if (LookingAt("{")) {
  1899. // Options!
  1900. DO(ParseMethodOptions(method_location, containing_file,
  1901. MethodDescriptorProto::kOptionsFieldNumber,
  1902. method->mutable_options()));
  1903. } else {
  1904. DO(ConsumeEndOfDeclaration(";", &method_location));
  1905. }
  1906. return true;
  1907. }
  1908. bool Parser::ParseMethodOptions(const LocationRecorder& parent_location,
  1909. const FileDescriptorProto* containing_file,
  1910. const int optionsFieldNumber,
  1911. Message* mutable_options) {
  1912. // Options!
  1913. ConsumeEndOfDeclaration("{", &parent_location);
  1914. while (!TryConsumeEndOfDeclaration("}", NULL)) {
  1915. if (AtEnd()) {
  1916. AddError("Reached end of input in method options (missing '}').");
  1917. return false;
  1918. }
  1919. if (TryConsumeEndOfDeclaration(";", NULL)) {
  1920. // empty statement; ignore
  1921. } else {
  1922. LocationRecorder location(parent_location, optionsFieldNumber);
  1923. if (!ParseOption(mutable_options, location, containing_file,
  1924. OPTION_STATEMENT)) {
  1925. // This statement failed to parse. Skip it, but keep looping to
  1926. // parse other statements.
  1927. SkipStatement();
  1928. }
  1929. }
  1930. }
  1931. return true;
  1932. }
  1933. // -------------------------------------------------------------------
  1934. bool Parser::ParseLabel(FieldDescriptorProto::Label* label,
  1935. const LocationRecorder& field_location,
  1936. const FileDescriptorProto* containing_file) {
  1937. if (!LookingAt("optional") && !LookingAt("repeated") &&
  1938. !LookingAt("required")) {
  1939. return false;
  1940. }
  1941. LocationRecorder location(field_location,
  1942. FieldDescriptorProto::kLabelFieldNumber);
  1943. if (TryConsume("optional")) {
  1944. *label = FieldDescriptorProto::LABEL_OPTIONAL;
  1945. } else if (TryConsume("repeated")) {
  1946. *label = FieldDescriptorProto::LABEL_REPEATED;
  1947. } else {
  1948. Consume("required");
  1949. *label = FieldDescriptorProto::LABEL_REQUIRED;
  1950. }
  1951. return true;
  1952. }
  1953. bool Parser::ParseType(FieldDescriptorProto::Type* type,
  1954. std::string* type_name) {
  1955. TypeNameMap::const_iterator iter = kTypeNames.find(input_->current().text);
  1956. if (iter != kTypeNames.end()) {
  1957. *type = iter->second;
  1958. input_->Next();
  1959. } else {
  1960. DO(ParseUserDefinedType(type_name));
  1961. }
  1962. return true;
  1963. }
  1964. bool Parser::ParseUserDefinedType(std::string* type_name) {
  1965. type_name->clear();
  1966. TypeNameMap::const_iterator iter = kTypeNames.find(input_->current().text);
  1967. if (iter != kTypeNames.end()) {
  1968. // Note: The only place enum types are allowed is for field types, but
  1969. // if we are parsing a field type then we would not get here because
  1970. // primitives are allowed there as well. So this error message doesn't
  1971. // need to account for enums.
  1972. AddError("Expected message type.");
  1973. // Pretend to accept this type so that we can go on parsing.
  1974. *type_name = input_->current().text;
  1975. input_->Next();
  1976. return true;
  1977. }
  1978. // A leading "." means the name is fully-qualified.
  1979. if (TryConsume(".")) type_name->append(".");
  1980. // Consume the first part of the name.
  1981. std::string identifier;
  1982. DO(ConsumeIdentifier(&identifier, "Expected type name."));
  1983. type_name->append(identifier);
  1984. // Consume more parts.
  1985. while (TryConsume(".")) {
  1986. type_name->append(".");
  1987. DO(ConsumeIdentifier(&identifier, "Expected identifier."));
  1988. type_name->append(identifier);
  1989. }
  1990. return true;
  1991. }
  1992. // ===================================================================
  1993. bool Parser::ParsePackage(FileDescriptorProto* file,
  1994. const LocationRecorder& root_location,
  1995. const FileDescriptorProto* containing_file) {
  1996. if (file->has_package()) {
  1997. AddError("Multiple package definitions.");
  1998. // Don't append the new package to the old one. Just replace it. Not
  1999. // that it really matters since this is an error anyway.
  2000. file->clear_package();
  2001. }
  2002. LocationRecorder location(root_location,
  2003. FileDescriptorProto::kPackageFieldNumber);
  2004. location.RecordLegacyLocation(file, DescriptorPool::ErrorCollector::NAME);
  2005. DO(Consume("package"));
  2006. while (true) {
  2007. std::string identifier;
  2008. DO(ConsumeIdentifier(&identifier, "Expected identifier."));
  2009. file->mutable_package()->append(identifier);
  2010. if (!TryConsume(".")) break;
  2011. file->mutable_package()->append(".");
  2012. }
  2013. DO(ConsumeEndOfDeclaration(";", &location));
  2014. return true;
  2015. }
  2016. bool Parser::ParseImport(RepeatedPtrField<std::string>* dependency,
  2017. RepeatedField<int32>* public_dependency,
  2018. RepeatedField<int32>* weak_dependency,
  2019. const LocationRecorder& root_location,
  2020. const FileDescriptorProto* containing_file) {
  2021. LocationRecorder location(root_location,
  2022. FileDescriptorProto::kDependencyFieldNumber,
  2023. dependency->size());
  2024. DO(Consume("import"));
  2025. if (LookingAt("public")) {
  2026. LocationRecorder public_location(
  2027. root_location, FileDescriptorProto::kPublicDependencyFieldNumber,
  2028. public_dependency->size());
  2029. DO(Consume("public"));
  2030. *public_dependency->Add() = dependency->size();
  2031. } else if (LookingAt("weak")) {
  2032. LocationRecorder weak_location(
  2033. root_location, FileDescriptorProto::kWeakDependencyFieldNumber,
  2034. weak_dependency->size());
  2035. weak_location.RecordLegacyImportLocation(containing_file, "weak");
  2036. DO(Consume("weak"));
  2037. *weak_dependency->Add() = dependency->size();
  2038. }
  2039. string import_file;
  2040. DO(ConsumeString(&import_file,
  2041. "Expected a string naming the file to import."));
  2042. *dependency->Add() = import_file;
  2043. location.RecordLegacyImportLocation(containing_file, import_file);
  2044. DO(ConsumeEndOfDeclaration(";", &location));
  2045. return true;
  2046. }
  2047. // ===================================================================
  2048. SourceLocationTable::SourceLocationTable() {}
  2049. SourceLocationTable::~SourceLocationTable() {}
  2050. bool SourceLocationTable::Find(
  2051. const Message* descriptor,
  2052. DescriptorPool::ErrorCollector::ErrorLocation location, int* line,
  2053. int* column) const {
  2054. const std::pair<int, int>* result =
  2055. FindOrNull(location_map_, std::make_pair(descriptor, location));
  2056. if (result == NULL) {
  2057. *line = -1;
  2058. *column = 0;
  2059. return false;
  2060. } else {
  2061. *line = result->first;
  2062. *column = result->second;
  2063. return true;
  2064. }
  2065. }
  2066. bool SourceLocationTable::FindImport(const Message* descriptor,
  2067. const string& name, int* line,
  2068. int* column) const {
  2069. const std::pair<int, int>* result =
  2070. FindOrNull(import_location_map_, std::make_pair(descriptor, name));
  2071. if (result == nullptr) {
  2072. *line = -1;
  2073. *column = 0;
  2074. return false;
  2075. } else {
  2076. *line = result->first;
  2077. *column = result->second;
  2078. return true;
  2079. }
  2080. }
  2081. void SourceLocationTable::Add(
  2082. const Message* descriptor,
  2083. DescriptorPool::ErrorCollector::ErrorLocation location, int line,
  2084. int column) {
  2085. location_map_[std::make_pair(descriptor, location)] =
  2086. std::make_pair(line, column);
  2087. }
  2088. void SourceLocationTable::AddImport(const Message* descriptor,
  2089. const string& name, int line, int column) {
  2090. import_location_map_[std::make_pair(descriptor, name)] =
  2091. std::make_pair(line, column);
  2092. }
  2093. void SourceLocationTable::Clear() { location_map_.clear(); }
  2094. } // namespace compiler
  2095. } // namespace protobuf
  2096. } // namespace google