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 31 kB

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