text_format.h 25 KB

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