OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // http://code.google.com/p/protobuf/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 20 matching lines...) Expand all Loading... |
31 // Author: jschorr@google.com (Joseph Schorr) | 31 // Author: jschorr@google.com (Joseph Schorr) |
32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
34 // | 34 // |
35 // Utilities for printing and parsing protocol messages in a human-readable, | 35 // Utilities for printing and parsing protocol messages in a human-readable, |
36 // text-based format. | 36 // text-based format. |
37 | 37 |
38 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__ | 38 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__ |
39 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__ | 39 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__ |
40 | 40 |
| 41 #include <map> |
41 #include <string> | 42 #include <string> |
| 43 #include <vector> |
| 44 #include <google/protobuf/stubs/common.h> |
42 #include <google/protobuf/message.h> | 45 #include <google/protobuf/message.h> |
43 #include <google/protobuf/descriptor.h> | 46 #include <google/protobuf/descriptor.h> |
44 | 47 |
45 namespace google { | 48 namespace google { |
46 namespace protobuf { | 49 namespace protobuf { |
47 | 50 |
48 namespace io { | 51 namespace io { |
49 class ErrorCollector; // tokenizer.h | 52 class ErrorCollector; // tokenizer.h |
50 } | 53 } |
51 | 54 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 public: | 216 public: |
214 virtual ~Finder(); | 217 virtual ~Finder(); |
215 | 218 |
216 // Try to find an extension of *message by fully-qualified field | 219 // Try to find an extension of *message by fully-qualified field |
217 // name. Returns NULL if no extension is known for this name or number. | 220 // name. Returns NULL if no extension is known for this name or number. |
218 virtual const FieldDescriptor* FindExtension( | 221 virtual const FieldDescriptor* FindExtension( |
219 Message* message, | 222 Message* message, |
220 const string& name) const = 0; | 223 const string& name) const = 0; |
221 }; | 224 }; |
222 | 225 |
| 226 // A location in the parsed text. |
| 227 struct ParseLocation { |
| 228 int line; |
| 229 int column; |
| 230 |
| 231 ParseLocation() : line(-1), column(-1) {} |
| 232 ParseLocation(int line_param, int column_param) |
| 233 : line(line_param), column(column_param) {} |
| 234 }; |
| 235 |
| 236 // Data structure which is populated with the locations of each field |
| 237 // value parsed from the text. |
| 238 class LIBPROTOBUF_EXPORT ParseInfoTree { |
| 239 public: |
| 240 ParseInfoTree(); |
| 241 ~ParseInfoTree(); |
| 242 |
| 243 // Returns the parse location for index-th value of the field in the parsed |
| 244 // text. If none exists, returns a location with line = -1. Index should be |
| 245 // -1 for not-repeated fields. |
| 246 ParseLocation GetLocation(const FieldDescriptor* field, int index) const; |
| 247 |
| 248 // Returns the parse info tree for the given field, which must be a message |
| 249 // type. The nested information tree is owned by the root tree and will be |
| 250 // deleted when it is deleted. |
| 251 ParseInfoTree* GetTreeForNested(const FieldDescriptor* field, |
| 252 int index) const; |
| 253 |
| 254 private: |
| 255 // Allow the text format parser to record information into the tree. |
| 256 friend class TextFormat; |
| 257 |
| 258 // Records the starting location of a single value for a field. |
| 259 void RecordLocation(const FieldDescriptor* field, ParseLocation location); |
| 260 |
| 261 // Create and records a nested tree for a nested message field. |
| 262 ParseInfoTree* CreateNested(const FieldDescriptor* field); |
| 263 |
| 264 // Defines the map from the index-th field descriptor to its parse location. |
| 265 typedef map<const FieldDescriptor*, vector<ParseLocation> > LocationMap; |
| 266 |
| 267 // Defines the map from the index-th field descriptor to the nested parse |
| 268 // info tree. |
| 269 typedef map<const FieldDescriptor*, vector<ParseInfoTree*> > NestedMap; |
| 270 |
| 271 LocationMap locations_; |
| 272 NestedMap nested_; |
| 273 |
| 274 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParseInfoTree); |
| 275 }; |
| 276 |
223 // For more control over parsing, use this class. | 277 // For more control over parsing, use this class. |
224 class LIBPROTOBUF_EXPORT Parser { | 278 class LIBPROTOBUF_EXPORT Parser { |
225 public: | 279 public: |
226 Parser(); | 280 Parser(); |
227 ~Parser(); | 281 ~Parser(); |
228 | 282 |
229 // Like TextFormat::Parse(). | 283 // Like TextFormat::Parse(). |
230 bool Parse(io::ZeroCopyInputStream* input, Message* output); | 284 bool Parse(io::ZeroCopyInputStream* input, Message* output); |
231 // Like TextFormat::ParseFromString(). | 285 // Like TextFormat::ParseFromString(). |
232 bool ParseFromString(const string& input, Message* output); | 286 bool ParseFromString(const string& input, Message* output); |
233 // Like TextFormat::Merge(). | 287 // Like TextFormat::Merge(). |
234 bool Merge(io::ZeroCopyInputStream* input, Message* output); | 288 bool Merge(io::ZeroCopyInputStream* input, Message* output); |
235 // Like TextFormat::MergeFromString(). | 289 // Like TextFormat::MergeFromString(). |
236 bool MergeFromString(const string& input, Message* output); | 290 bool MergeFromString(const string& input, Message* output); |
237 | 291 |
238 // Set where to report parse errors. If NULL (the default), errors will | 292 // Set where to report parse errors. If NULL (the default), errors will |
239 // be printed to stderr. | 293 // be printed to stderr. |
240 void RecordErrorsTo(io::ErrorCollector* error_collector) { | 294 void RecordErrorsTo(io::ErrorCollector* error_collector) { |
241 error_collector_ = error_collector; | 295 error_collector_ = error_collector; |
242 } | 296 } |
243 | 297 |
244 // Set how parser finds extensions. If NULL (the default), the | 298 // Set how parser finds extensions. If NULL (the default), the |
245 // parser will use the standard Reflection object associated with | 299 // parser will use the standard Reflection object associated with |
246 // the message being parsed. | 300 // the message being parsed. |
247 void SetFinder(Finder* finder) { | 301 void SetFinder(Finder* finder) { |
248 finder_ = finder; | 302 finder_ = finder; |
249 } | 303 } |
250 | 304 |
| 305 // Sets where location information about the parse will be written. If NULL |
| 306 // (the default), then no location will be written. |
| 307 void WriteLocationsTo(ParseInfoTree* tree) { |
| 308 parse_info_tree_ = tree; |
| 309 } |
| 310 |
251 // Normally parsing fails if, after parsing, output->IsInitialized() | 311 // Normally parsing fails if, after parsing, output->IsInitialized() |
252 // returns false. Call AllowPartialMessage(true) to skip this check. | 312 // returns false. Call AllowPartialMessage(true) to skip this check. |
253 void AllowPartialMessage(bool allow) { | 313 void AllowPartialMessage(bool allow) { |
254 allow_partial_ = allow; | 314 allow_partial_ = allow; |
255 } | 315 } |
256 | 316 |
257 // Like TextFormat::ParseFieldValueFromString | 317 // Like TextFormat::ParseFieldValueFromString |
258 bool ParseFieldValueFromString(const string& input, | 318 bool ParseFieldValueFromString(const string& input, |
259 const FieldDescriptor* field, | 319 const FieldDescriptor* field, |
260 Message* output); | 320 Message* output); |
261 | 321 |
| 322 |
262 private: | 323 private: |
263 // Forward declaration of an internal class used to parse text | 324 // Forward declaration of an internal class used to parse text |
264 // representations (see text_format.cc for implementation). | 325 // representations (see text_format.cc for implementation). |
265 class ParserImpl; | 326 class ParserImpl; |
266 | 327 |
267 // Like TextFormat::Merge(). The provided implementation is used | 328 // Like TextFormat::Merge(). The provided implementation is used |
268 // to do the parsing. | 329 // to do the parsing. |
269 bool MergeUsingImpl(io::ZeroCopyInputStream* input, | 330 bool MergeUsingImpl(io::ZeroCopyInputStream* input, |
270 Message* output, | 331 Message* output, |
271 ParserImpl* parser_impl); | 332 ParserImpl* parser_impl); |
272 | 333 |
273 io::ErrorCollector* error_collector_; | 334 io::ErrorCollector* error_collector_; |
274 Finder* finder_; | 335 Finder* finder_; |
| 336 ParseInfoTree* parse_info_tree_; |
275 bool allow_partial_; | 337 bool allow_partial_; |
| 338 bool allow_unknown_field_; |
276 }; | 339 }; |
277 | 340 |
278 private: | 341 private: |
279 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat); | 342 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat); |
280 }; | 343 }; |
281 | 344 |
282 } // namespace protobuf | 345 } // namespace protobuf |
283 | 346 |
284 } // namespace google | 347 } // namespace google |
285 #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__ | 348 #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__ |
OLD | NEW |