parser.cc 77 KB

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