You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

text_format.h 36 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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: jschorr@google.com (Joseph Schorr)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Utilities for printing and parsing protocol messages in a human-readable,
  35. // text-based format.
  36. #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
  37. #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
  38. #include <map>
  39. #include <memory>
  40. #include <string>
  41. #include <vector>
  42. #include <google/protobuf/stubs/common.h>
  43. #include <google/protobuf/port.h>
  44. #include <google/protobuf/descriptor.h>
  45. #include <google/protobuf/message.h>
  46. #include <google/protobuf/message_lite.h>
  47. // Must be included last.
  48. #include <google/protobuf/port_def.inc>
  49. #ifdef SWIG
  50. #error "You cannot SWIG proto headers"
  51. #endif
  52. namespace google
  53. {
  54. namespace protobuf
  55. {
  56. namespace internal
  57. {
  58. PROTOBUF_EXPORT extern const char kDebugStringSilentMarker[1];
  59. PROTOBUF_EXPORT extern const char kDebugStringSilentMarkerForDetection[3];
  60. } // namespace internal
  61. namespace io
  62. {
  63. class ErrorCollector; // tokenizer.h
  64. }
  65. // This class implements protocol buffer text format, colloquially known as text
  66. // proto. Printing and parsing protocol messages in text format is useful for
  67. // debugging and human editing of messages.
  68. //
  69. // This class is really a namespace that contains only static methods.
  70. class PROTOBUF_EXPORT TextFormat
  71. {
  72. public:
  73. // Outputs a textual representation of the given message to the given
  74. // output stream. Returns false if printing fails.
  75. static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
  76. // Print the fields in an UnknownFieldSet. They are printed by tag number
  77. // only. Embedded messages are heuristically identified by attempting to
  78. // parse them. Returns false if printing fails.
  79. static bool PrintUnknownFields(const UnknownFieldSet& unknown_fields, io::ZeroCopyOutputStream* output);
  80. // Like Print(), but outputs directly to a string.
  81. // Note: output will be cleared prior to printing, and will be left empty
  82. // even if printing fails. Returns false if printing fails.
  83. static bool PrintToString(const Message& message, std::string* output);
  84. // Like PrintUnknownFields(), but outputs directly to a string. Returns
  85. // false if printing fails.
  86. static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields, std::string* output);
  87. // Outputs a textual representation of the value of the field supplied on
  88. // the message supplied. For non-repeated fields, an index of -1 must
  89. // be supplied. Note that this method will print the default value for a
  90. // field if it is not set.
  91. static void PrintFieldValueToString(const Message& message, const FieldDescriptor* field, int index, std::string* output);
  92. class PROTOBUF_EXPORT BaseTextGenerator
  93. {
  94. public:
  95. virtual ~BaseTextGenerator();
  96. virtual void Indent()
  97. {
  98. }
  99. virtual void Outdent()
  100. {
  101. }
  102. // Returns the current indentation size in characters.
  103. virtual size_t GetCurrentIndentationSize() const
  104. {
  105. return 0;
  106. }
  107. // Print text to the output stream.
  108. virtual void Print(const char* text, size_t size) = 0;
  109. void PrintString(const std::string& str)
  110. {
  111. Print(str.data(), str.size());
  112. }
  113. template<size_t n>
  114. void PrintLiteral(const char (&text)[n])
  115. {
  116. Print(text, n - 1); // n includes the terminating zero character.
  117. }
  118. };
  119. // The default printer that converts scalar values from fields into their
  120. // string representation.
  121. // You can derive from this FastFieldValuePrinter if you want to have fields
  122. // to be printed in a different way and register it at the Printer.
  123. class PROTOBUF_EXPORT FastFieldValuePrinter
  124. {
  125. public:
  126. FastFieldValuePrinter();
  127. virtual ~FastFieldValuePrinter();
  128. virtual void PrintBool(bool val, BaseTextGenerator* generator) const;
  129. virtual void PrintInt32(int32_t val, BaseTextGenerator* generator) const;
  130. virtual void PrintUInt32(uint32_t val, BaseTextGenerator* generator) const;
  131. virtual void PrintInt64(int64_t val, BaseTextGenerator* generator) const;
  132. virtual void PrintUInt64(uint64_t val, BaseTextGenerator* generator) const;
  133. virtual void PrintFloat(float val, BaseTextGenerator* generator) const;
  134. virtual void PrintDouble(double val, BaseTextGenerator* generator) const;
  135. virtual void PrintString(const std::string& val, BaseTextGenerator* generator) const;
  136. virtual void PrintBytes(const std::string& val, BaseTextGenerator* generator) const;
  137. virtual void PrintEnum(int32_t val, const std::string& name, BaseTextGenerator* generator) const;
  138. virtual void PrintFieldName(const Message& message, int field_index, int field_count, const Reflection* reflection, const FieldDescriptor* field, BaseTextGenerator* generator) const;
  139. virtual void PrintFieldName(const Message& message, const Reflection* reflection, const FieldDescriptor* field, BaseTextGenerator* generator) const;
  140. virtual void PrintMessageStart(const Message& message, int field_index, int field_count, bool single_line_mode, BaseTextGenerator* generator) const;
  141. // Allows to override the logic on how to print the content of a message.
  142. // Return false to use the default printing logic. Note that it is legal for
  143. // this function to print something and then return false to use the default
  144. // content printing (although at that point it would behave similarly to
  145. // PrintMessageStart).
  146. virtual bool PrintMessageContent(const Message& message, int field_index, int field_count, bool single_line_mode, BaseTextGenerator* generator) const;
  147. virtual void PrintMessageEnd(const Message& message, int field_index, int field_count, bool single_line_mode, BaseTextGenerator* generator) const;
  148. private:
  149. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastFieldValuePrinter);
  150. };
  151. // Deprecated: please use FastFieldValuePrinter instead.
  152. class PROTOBUF_EXPORT FieldValuePrinter
  153. {
  154. public:
  155. FieldValuePrinter();
  156. virtual ~FieldValuePrinter();
  157. virtual std::string PrintBool(bool val) const;
  158. virtual std::string PrintInt32(int32_t val) const;
  159. virtual std::string PrintUInt32(uint32_t val) const;
  160. virtual std::string PrintInt64(int64_t val) const;
  161. virtual std::string PrintUInt64(uint64_t val) const;
  162. virtual std::string PrintFloat(float val) const;
  163. virtual std::string PrintDouble(double val) const;
  164. virtual std::string PrintString(const std::string& val) const;
  165. virtual std::string PrintBytes(const std::string& val) const;
  166. virtual std::string PrintEnum(int32_t val, const std::string& name) const;
  167. virtual std::string PrintFieldName(const Message& message, const Reflection* reflection, const FieldDescriptor* field) const;
  168. virtual std::string PrintMessageStart(const Message& message, int field_index, int field_count, bool single_line_mode) const;
  169. virtual std::string PrintMessageEnd(const Message& message, int field_index, int field_count, bool single_line_mode) const;
  170. private:
  171. FastFieldValuePrinter delegate_;
  172. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldValuePrinter);
  173. };
  174. class PROTOBUF_EXPORT MessagePrinter
  175. {
  176. public:
  177. MessagePrinter()
  178. {
  179. }
  180. virtual ~MessagePrinter()
  181. {
  182. }
  183. virtual void Print(const Message& message, bool single_line_mode, BaseTextGenerator* generator) const = 0;
  184. private:
  185. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessagePrinter);
  186. };
  187. // Interface that Printers or Parsers can use to find extensions, or types
  188. // referenced in Any messages.
  189. class PROTOBUF_EXPORT Finder
  190. {
  191. public:
  192. virtual ~Finder();
  193. // Try to find an extension of *message by fully-qualified field
  194. // name. Returns nullptr if no extension is known for this name or number.
  195. // The base implementation uses the extensions already known by the message.
  196. virtual const FieldDescriptor* FindExtension(Message* message, const std::string& name) const;
  197. // Similar to FindExtension, but uses a Descriptor and the extension number
  198. // instead of using a Message and the name when doing the look up.
  199. virtual const FieldDescriptor* FindExtensionByNumber(
  200. const Descriptor* descriptor, int number
  201. ) const;
  202. // Find the message type for an Any proto.
  203. // Returns nullptr if no message is known for this name.
  204. // The base implementation only accepts prefixes of type.googleprod.com/ or
  205. // type.googleapis.com/, and searches the DescriptorPool of the parent
  206. // message.
  207. virtual const Descriptor* FindAnyType(const Message& message, const std::string& prefix, const std::string& name) const;
  208. // Find the message factory for the given extension field. This can be used
  209. // to generalize the Parser to add extension fields to a message in the same
  210. // way as the "input" message for the Parser.
  211. virtual MessageFactory* FindExtensionFactory(
  212. const FieldDescriptor* field
  213. ) const;
  214. };
  215. // Class for those users which require more fine-grained control over how
  216. // a protobuffer message is printed out.
  217. class PROTOBUF_EXPORT Printer
  218. {
  219. public:
  220. Printer();
  221. // Like TextFormat::Print
  222. bool Print(const Message& message, io::ZeroCopyOutputStream* output) const;
  223. // Like TextFormat::PrintUnknownFields
  224. bool PrintUnknownFields(const UnknownFieldSet& unknown_fields, io::ZeroCopyOutputStream* output) const;
  225. // Like TextFormat::PrintToString
  226. bool PrintToString(const Message& message, std::string* output) const;
  227. // Like TextFormat::PrintUnknownFieldsToString
  228. bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields, std::string* output) const;
  229. // Like TextFormat::PrintFieldValueToString
  230. void PrintFieldValueToString(const Message& message, const FieldDescriptor* field, int index, std::string* output) const;
  231. // Adjust the initial indent level of all output. Each indent level is
  232. // equal to two spaces.
  233. void SetInitialIndentLevel(int indent_level)
  234. {
  235. initial_indent_level_ = indent_level;
  236. }
  237. // If printing in single line mode, then the entire message will be output
  238. // on a single line with no line breaks.
  239. void SetSingleLineMode(bool single_line_mode)
  240. {
  241. single_line_mode_ = single_line_mode;
  242. }
  243. bool IsInSingleLineMode() const
  244. {
  245. return single_line_mode_;
  246. }
  247. // If use_field_number is true, uses field number instead of field name.
  248. void SetUseFieldNumber(bool use_field_number)
  249. {
  250. use_field_number_ = use_field_number;
  251. }
  252. // Set true to print repeated primitives in a format like:
  253. // field_name: [1, 2, 3, 4]
  254. // instead of printing each value on its own line. Short format applies
  255. // only to primitive values -- i.e. everything except strings and
  256. // sub-messages/groups.
  257. void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives)
  258. {
  259. use_short_repeated_primitives_ = use_short_repeated_primitives;
  260. }
  261. // Set true to output UTF-8 instead of ASCII. The only difference
  262. // is that bytes >= 0x80 in string fields will not be escaped,
  263. // because they are assumed to be part of UTF-8 multi-byte
  264. // sequences. This will change the default FastFieldValuePrinter.
  265. void SetUseUtf8StringEscaping(bool as_utf8);
  266. // Set the default FastFieldValuePrinter that is used for all fields that
  267. // don't have a field-specific printer registered.
  268. // Takes ownership of the printer.
  269. void SetDefaultFieldValuePrinter(const FastFieldValuePrinter* printer);
  270. PROTOBUF_DEPRECATED_MSG("Please use FastFieldValuePrinter")
  271. void SetDefaultFieldValuePrinter(const FieldValuePrinter* printer);
  272. // Sets whether we want to hide unknown fields or not.
  273. // Usually unknown fields are printed in a generic way that includes the
  274. // tag number of the field instead of field name. However, sometimes it
  275. // is useful to be able to print the message without unknown fields (e.g.
  276. // for the python protobuf version to maintain consistency between its pure
  277. // python and c++ implementations).
  278. void SetHideUnknownFields(bool hide)
  279. {
  280. hide_unknown_fields_ = hide;
  281. }
  282. // If print_message_fields_in_index_order is true, fields of a proto message
  283. // will be printed using the order defined in source code instead of the
  284. // field number, extensions will be printed at the end of the message
  285. // and their relative order is determined by the extension number.
  286. // By default, use the field number order.
  287. void SetPrintMessageFieldsInIndexOrder(
  288. bool print_message_fields_in_index_order
  289. )
  290. {
  291. print_message_fields_in_index_order_ =
  292. print_message_fields_in_index_order;
  293. }
  294. // If expand==true, expand google.protobuf.Any payloads. The output
  295. // will be of form
  296. // [type_url] { <value_printed_in_text> }
  297. //
  298. // If expand==false, print Any using the default printer. The output will
  299. // look like
  300. // type_url: "<type_url>" value: "serialized_content"
  301. void SetExpandAny(bool expand)
  302. {
  303. expand_any_ = expand;
  304. }
  305. // Set how parser finds message for Any payloads.
  306. void SetFinder(const Finder* finder)
  307. {
  308. finder_ = finder;
  309. }
  310. // If non-zero, we truncate all string fields that are longer than
  311. // this threshold. This is useful when the proto message has very long
  312. // strings, e.g., dump of encoded image file.
  313. //
  314. // NOTE(hfgong): Setting a non-zero value breaks round-trip safe
  315. // property of TextFormat::Printer. That is, from the printed message, we
  316. // cannot fully recover the original string field any more.
  317. void SetTruncateStringFieldLongerThan(
  318. const int64_t truncate_string_field_longer_than
  319. )
  320. {
  321. truncate_string_field_longer_than_ = truncate_string_field_longer_than;
  322. }
  323. // Register a custom field-specific FastFieldValuePrinter for fields
  324. // with a particular FieldDescriptor.
  325. // Returns "true" if the registration succeeded, or "false", if there is
  326. // already a printer for that FieldDescriptor.
  327. // Takes ownership of the printer on successful registration.
  328. bool RegisterFieldValuePrinter(const FieldDescriptor* field, const FastFieldValuePrinter* printer);
  329. PROTOBUF_DEPRECATED_MSG("Please use FastFieldValuePrinter")
  330. bool RegisterFieldValuePrinter(const FieldDescriptor* field, const FieldValuePrinter* printer);
  331. // Register a custom message-specific MessagePrinter for messages with a
  332. // particular Descriptor.
  333. // Returns "true" if the registration succeeded, or "false" if there is
  334. // already a printer for that Descriptor.
  335. bool RegisterMessagePrinter(const Descriptor* descriptor, const MessagePrinter* printer);
  336. private:
  337. friend std::string Message::DebugString() const;
  338. friend std::string Message::ShortDebugString() const;
  339. friend std::string Message::Utf8DebugString() const;
  340. // Sets whether *DebugString should insert a silent marker.
  341. void SetInsertSilentMarker(bool v)
  342. {
  343. insert_silent_marker_ = v;
  344. }
  345. // Forward declaration of an internal class used to print the text
  346. // output to the OutputStream (see text_format.cc for implementation).
  347. class TextGenerator;
  348. // Forward declaration of an internal class used to print field values for
  349. // DebugString APIs (see text_format.cc for implementation).
  350. class DebugStringFieldValuePrinter;
  351. // Forward declaration of an internal class used to print UTF-8 escaped
  352. // strings (see text_format.cc for implementation).
  353. class FastFieldValuePrinterUtf8Escaping;
  354. static const char* const kDoNotParse;
  355. // Internal Print method, used for writing to the OutputStream via
  356. // the TextGenerator class.
  357. void Print(const Message& message, TextGenerator* generator) const;
  358. // Print a single field.
  359. void PrintField(const Message& message, const Reflection* reflection, const FieldDescriptor* field, TextGenerator* generator) const;
  360. // Print a repeated primitive field in short form.
  361. void PrintShortRepeatedField(const Message& message, const Reflection* reflection, const FieldDescriptor* field, TextGenerator* generator) const;
  362. // Print the name of a field -- i.e. everything that comes before the
  363. // ':' for a single name/value pair.
  364. void PrintFieldName(const Message& message, int field_index, int field_count, const Reflection* reflection, const FieldDescriptor* field, TextGenerator* generator) const;
  365. // Outputs a textual representation of the value of the field supplied on
  366. // the message supplied or the default value if not set.
  367. void PrintFieldValue(const Message& message, const Reflection* reflection, const FieldDescriptor* field, int index, TextGenerator* generator) const;
  368. // Print the fields in an UnknownFieldSet. They are printed by tag number
  369. // only. Embedded messages are heuristically identified by attempting to
  370. // parse them (subject to the recursion budget).
  371. void PrintUnknownFields(const UnknownFieldSet& unknown_fields, TextGenerator* generator, int recursion_budget) const;
  372. bool PrintAny(const Message& message, TextGenerator* generator) const;
  373. const FastFieldValuePrinter* GetFieldPrinter(
  374. const FieldDescriptor* field
  375. ) const
  376. {
  377. auto it = custom_printers_.find(field);
  378. return it == custom_printers_.end() ? default_field_value_printer_.get() : it->second.get();
  379. }
  380. int initial_indent_level_;
  381. bool single_line_mode_;
  382. bool use_field_number_;
  383. bool use_short_repeated_primitives_;
  384. bool insert_silent_marker_;
  385. bool hide_unknown_fields_;
  386. bool print_message_fields_in_index_order_;
  387. bool expand_any_;
  388. int64_t truncate_string_field_longer_than_;
  389. std::unique_ptr<const FastFieldValuePrinter> default_field_value_printer_;
  390. typedef std::map<const FieldDescriptor*, std::unique_ptr<const FastFieldValuePrinter>>
  391. CustomPrinterMap;
  392. CustomPrinterMap custom_printers_;
  393. typedef std::map<const Descriptor*, std::unique_ptr<const MessagePrinter>>
  394. CustomMessagePrinterMap;
  395. CustomMessagePrinterMap custom_message_printers_;
  396. const Finder* finder_;
  397. };
  398. // Parses a text-format protocol message from the given input stream to
  399. // the given message object. This function parses the human-readable
  400. // serialization format written by Print(). Returns true on success. The
  401. // message is cleared first, even if the function fails -- See Merge() to
  402. // avoid this behavior.
  403. //
  404. // Example input: "user {\n id: 123 extra { gender: MALE language: 'en' }\n}"
  405. //
  406. // One common use for this function is parsing handwritten strings in test
  407. // code.
  408. //
  409. // If you would like to read a protocol buffer serialized in the
  410. // (non-human-readable) binary wire format, see
  411. // google::protobuf::MessageLite::ParseFromString().
  412. static bool Parse(io::ZeroCopyInputStream* input, Message* output);
  413. // Like Parse(), but reads directly from a string.
  414. static bool ParseFromString(ConstStringParam input, Message* output);
  415. // Like Parse(), but the data is merged into the given message, as if
  416. // using Message::MergeFrom().
  417. static bool Merge(io::ZeroCopyInputStream* input, Message* output);
  418. // Like Merge(), but reads directly from a string.
  419. static bool MergeFromString(ConstStringParam input, Message* output);
  420. // Parse the given text as a single field value and store it into the
  421. // given field of the given message. If the field is a repeated field,
  422. // the new value will be added to the end
  423. static bool ParseFieldValueFromString(const std::string& input, const FieldDescriptor* field, Message* message);
  424. // A location in the parsed text.
  425. struct ParseLocation
  426. {
  427. int line;
  428. int column;
  429. ParseLocation() :
  430. line(-1),
  431. column(-1)
  432. {
  433. }
  434. ParseLocation(int line_param, int column_param) :
  435. line(line_param),
  436. column(column_param)
  437. {
  438. }
  439. };
  440. // A range of locations in the parsed text, including `start` and excluding
  441. // `end`.
  442. struct ParseLocationRange
  443. {
  444. ParseLocation start;
  445. ParseLocation end;
  446. ParseLocationRange() :
  447. start(),
  448. end()
  449. {
  450. }
  451. ParseLocationRange(ParseLocation start_param, ParseLocation end_param) :
  452. start(start_param),
  453. end(end_param)
  454. {
  455. }
  456. };
  457. // Data structure which is populated with the locations of each field
  458. // value parsed from the text.
  459. class PROTOBUF_EXPORT ParseInfoTree
  460. {
  461. public:
  462. ParseInfoTree() = default;
  463. ParseInfoTree(const ParseInfoTree&) = delete;
  464. ParseInfoTree& operator=(const ParseInfoTree&) = delete;
  465. // Returns the parse location range for index-th value of the field in
  466. // the parsed text. If none exists, returns a location with start and end
  467. // line -1. Index should be -1 for not-repeated fields.
  468. ParseLocationRange GetLocationRange(const FieldDescriptor* field, int index) const;
  469. // Returns the starting parse location for index-th value of the field in
  470. // the parsed text. If none exists, returns a location with line = -1. Index
  471. // should be -1 for not-repeated fields.
  472. ParseLocation GetLocation(const FieldDescriptor* field, int index) const
  473. {
  474. return GetLocationRange(field, index).start;
  475. }
  476. // Returns the parse info tree for the given field, which must be a message
  477. // type. The nested information tree is owned by the root tree and will be
  478. // deleted when it is deleted.
  479. ParseInfoTree* GetTreeForNested(const FieldDescriptor* field, int index) const;
  480. private:
  481. // Allow the text format parser to record information into the tree.
  482. friend class TextFormat;
  483. // Records the starting and ending locations of a single value for a field.
  484. void RecordLocation(const FieldDescriptor* field, ParseLocationRange range);
  485. // Create and records a nested tree for a nested message field.
  486. ParseInfoTree* CreateNested(const FieldDescriptor* field);
  487. // Defines the map from the index-th field descriptor to its parse location.
  488. typedef std::map<const FieldDescriptor*, std::vector<ParseLocationRange>>
  489. LocationMap;
  490. // Defines the map from the index-th field descriptor to the nested parse
  491. // info tree.
  492. typedef std::map<const FieldDescriptor*, std::vector<std::unique_ptr<ParseInfoTree>>>
  493. NestedMap;
  494. LocationMap locations_;
  495. NestedMap nested_;
  496. };
  497. // For more control over parsing, use this class.
  498. class PROTOBUF_EXPORT Parser
  499. {
  500. public:
  501. Parser();
  502. ~Parser();
  503. // Like TextFormat::Parse().
  504. bool Parse(io::ZeroCopyInputStream* input, Message* output);
  505. // Like TextFormat::ParseFromString().
  506. bool ParseFromString(ConstStringParam input, Message* output);
  507. // Like TextFormat::Merge().
  508. bool Merge(io::ZeroCopyInputStream* input, Message* output);
  509. // Like TextFormat::MergeFromString().
  510. bool MergeFromString(ConstStringParam input, Message* output);
  511. // Set where to report parse errors. If nullptr (the default), errors will
  512. // be printed to stderr.
  513. void RecordErrorsTo(io::ErrorCollector* error_collector)
  514. {
  515. error_collector_ = error_collector;
  516. }
  517. // Set how parser finds extensions. If nullptr (the default), the
  518. // parser will use the standard Reflection object associated with
  519. // the message being parsed.
  520. void SetFinder(const Finder* finder)
  521. {
  522. finder_ = finder;
  523. }
  524. // Sets where location information about the parse will be written. If
  525. // nullptr
  526. // (the default), then no location will be written.
  527. void WriteLocationsTo(ParseInfoTree* tree)
  528. {
  529. parse_info_tree_ = tree;
  530. }
  531. // Normally parsing fails if, after parsing, output->IsInitialized()
  532. // returns false. Call AllowPartialMessage(true) to skip this check.
  533. void AllowPartialMessage(bool allow)
  534. {
  535. allow_partial_ = allow;
  536. }
  537. // Allow field names to be matched case-insensitively.
  538. // This is not advisable if there are fields that only differ in case, or
  539. // if you want to enforce writing in the canonical form.
  540. // This is 'false' by default.
  541. void AllowCaseInsensitiveField(bool allow)
  542. {
  543. allow_case_insensitive_field_ = allow;
  544. }
  545. // Like TextFormat::ParseFieldValueFromString
  546. bool ParseFieldValueFromString(const std::string& input, const FieldDescriptor* field, Message* output);
  547. // When an unknown extension is met, parsing will fail if this option is
  548. // set to false (the default). If true, unknown extensions will be ignored
  549. // and a warning message will be generated.
  550. // Beware! Setting this option true may hide some errors (e.g. spelling
  551. // error on extension name). This allows data loss; unlike binary format,
  552. // text format cannot preserve unknown extensions. Avoid using this option
  553. // if possible.
  554. void AllowUnknownExtension(bool allow)
  555. {
  556. allow_unknown_extension_ = allow;
  557. }
  558. // When an unknown field is met, parsing will fail if this option is set
  559. // to false (the default). If true, unknown fields will be ignored and
  560. // a warning message will be generated.
  561. // Beware! Setting this option true may hide some errors (e.g. spelling
  562. // error on field name). This allows data loss; unlike binary format, text
  563. // format cannot preserve unknown fields. Avoid using this option
  564. // if possible.
  565. void AllowUnknownField(bool allow)
  566. {
  567. allow_unknown_field_ = allow;
  568. }
  569. void AllowFieldNumber(bool allow)
  570. {
  571. allow_field_number_ = allow;
  572. }
  573. // Sets maximum recursion depth which parser can use. This is effectively
  574. // the maximum allowed nesting of proto messages.
  575. void SetRecursionLimit(int limit)
  576. {
  577. recursion_limit_ = limit;
  578. }
  579. private:
  580. // Forward declaration of an internal class used to parse text
  581. // representations (see text_format.cc for implementation).
  582. class ParserImpl;
  583. // Like TextFormat::Merge(). The provided implementation is used
  584. // to do the parsing.
  585. bool MergeUsingImpl(io::ZeroCopyInputStream* input, Message* output, ParserImpl* parser_impl);
  586. io::ErrorCollector* error_collector_;
  587. const Finder* finder_;
  588. ParseInfoTree* parse_info_tree_;
  589. bool allow_partial_;
  590. bool allow_case_insensitive_field_;
  591. bool allow_unknown_field_;
  592. bool allow_unknown_extension_;
  593. bool allow_unknown_enum_;
  594. bool allow_field_number_;
  595. bool allow_relaxed_whitespace_;
  596. bool allow_singular_overwrites_;
  597. int recursion_limit_;
  598. };
  599. private:
  600. // Hack: ParseInfoTree declares TextFormat as a friend which should extend
  601. // the friendship to TextFormat::Parser::ParserImpl, but unfortunately some
  602. // old compilers (e.g. GCC 3.4.6) don't implement this correctly. We provide
  603. // helpers for ParserImpl to call methods of ParseInfoTree.
  604. static inline void RecordLocation(ParseInfoTree* info_tree, const FieldDescriptor* field, ParseLocationRange location);
  605. static inline ParseInfoTree* CreateNested(ParseInfoTree* info_tree, const FieldDescriptor* field);
  606. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat);
  607. };
  608. inline void TextFormat::RecordLocation(ParseInfoTree* info_tree, const FieldDescriptor* field, ParseLocationRange location)
  609. {
  610. info_tree->RecordLocation(field, location);
  611. }
  612. inline TextFormat::ParseInfoTree* TextFormat::CreateNested(
  613. ParseInfoTree* info_tree, const FieldDescriptor* field
  614. )
  615. {
  616. return info_tree->CreateNested(field);
  617. }
  618. } // namespace protobuf
  619. } // namespace google
  620. #include <google/protobuf/port_undef.inc>
  621. #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__