~verterok/ubuntu/lucid/protobuf/2.4.0a-backport

« back to all changes in this revision

Viewing changes to src/google/protobuf/compiler/parser.h

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-31 14:41:47 UTC
  • mfrom: (2.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110531144147-s41g5fozgvyo462l
Tags: 2.4.0a-2ubuntu1
* Merge with Debian; remaining changes:
  - Fix linking with -lpthread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
 
75
75
  // Optional fetaures:
76
76
 
 
77
  // DEPRECATED:  New code should use the SourceCodeInfo embedded in the
 
78
  //   FileDescriptorProto.
 
79
  //
77
80
  // Requests that locations of certain definitions be recorded to the given
78
81
  // SourceLocationTable while parsing.  This can be used to look up exact line
79
82
  // and column numbers for errors reported by DescriptorPool during validation.
82
85
    source_location_table_ = location_table;
83
86
  }
84
87
 
85
 
  // Requsets that errors be recorded to the given ErrorCollector while
 
88
  // Requests that errors be recorded to the given ErrorCollector while
86
89
  // parsing.  Set to NULL (the default) to discard error messages.
87
90
  void RecordErrorsTo(io::ErrorCollector* error_collector) {
88
91
    error_collector_ = error_collector;
180
183
  // of the current token.
181
184
  void AddError(const string& error);
182
185
 
183
 
  // Record the given line and column and associate it with this descriptor
184
 
  // in the SourceLocationTable.
185
 
  void RecordLocation(const Message* descriptor,
186
 
                      DescriptorPool::ErrorCollector::ErrorLocation location,
187
 
                      int line, int column);
188
 
 
189
 
  // Record the current line and column and associate it with this descriptor
190
 
  // in the SourceLocationTable.
191
 
  void RecordLocation(const Message* descriptor,
192
 
                      DescriptorPool::ErrorCollector::ErrorLocation location);
 
186
  // Records a location in the SourceCodeInfo.location table (see
 
187
  // descriptor.proto).  We use RAII to ensure that the start and end locations
 
188
  // are recorded -- the constructor records the start location and the
 
189
  // destructor records the end location.  Since the parser is
 
190
  // recursive-descent, this works out beautifully.
 
191
  class LIBPROTOBUF_EXPORT LocationRecorder {
 
192
   public:
 
193
    // Construct the file's "root" location.
 
194
    LocationRecorder(Parser* parser);
 
195
 
 
196
    // Construct a location that represents a declaration nested within the
 
197
    // given parent.  E.g. a field's location is nested within the location
 
198
    // for a message type.  The parent's path will be copied, so you should
 
199
    // call AddPath() only to add the path components leading from the parent
 
200
    // to the child (as opposed to leading from the root to the child).
 
201
    LocationRecorder(const LocationRecorder& parent);
 
202
 
 
203
    // Convenience constructors that call AddPath() one or two times.
 
204
    LocationRecorder(const LocationRecorder& parent, int path1);
 
205
    LocationRecorder(const LocationRecorder& parent, int path1, int path2);
 
206
 
 
207
    ~LocationRecorder();
 
208
 
 
209
    // Add a path component.  See SourceCodeInfo.Location.path in
 
210
    // descriptor.proto.
 
211
    void AddPath(int path_component);
 
212
 
 
213
    // By default the location is considered to start at the current token at
 
214
    // the time the LocationRecorder is created.  StartAt() sets the start
 
215
    // location to the given token instead.
 
216
    void StartAt(const io::Tokenizer::Token& token);
 
217
 
 
218
    // By default the location is considered to end at the previous token at
 
219
    // the time the LocationRecorder is destroyed.  EndAt() sets the end
 
220
    // location to the given token instead.
 
221
    void EndAt(const io::Tokenizer::Token& token);
 
222
 
 
223
    // Records the start point of this location to the SourceLocationTable that
 
224
    // was passed to RecordSourceLocationsTo(), if any.  SourceLocationTable
 
225
    // is an older way of keeping track of source locations which is still
 
226
    // used in some places.
 
227
    void RecordLegacyLocation(const Message* descriptor,
 
228
        DescriptorPool::ErrorCollector::ErrorLocation location);
 
229
 
 
230
   private:
 
231
    Parser* parser_;
 
232
    SourceCodeInfo::Location* location_;
 
233
 
 
234
    void Init(const LocationRecorder& parent);
 
235
  };
193
236
 
194
237
  // =================================================================
195
238
  // Parsers for various language constructs
210
253
  // makes logic much simpler for the caller.
211
254
 
212
255
  // Parse a top-level message, enum, service, etc.
213
 
  bool ParseTopLevelStatement(FileDescriptorProto* file);
 
256
  bool ParseTopLevelStatement(FileDescriptorProto* file,
 
257
                              const LocationRecorder& root_location);
214
258
 
215
259
  // Parse various language high-level language construrcts.
216
 
  bool ParseMessageDefinition(DescriptorProto* message);
217
 
  bool ParseEnumDefinition(EnumDescriptorProto* enum_type);
218
 
  bool ParseServiceDefinition(ServiceDescriptorProto* service);
219
 
  bool ParsePackage(FileDescriptorProto* file);
220
 
  bool ParseImport(string* import_filename);
221
 
  bool ParseOption(Message* options);
 
260
  bool ParseMessageDefinition(DescriptorProto* message,
 
261
                              const LocationRecorder& message_location);
 
262
  bool ParseEnumDefinition(EnumDescriptorProto* enum_type,
 
263
                           const LocationRecorder& enum_location);
 
264
  bool ParseServiceDefinition(ServiceDescriptorProto* service,
 
265
                              const LocationRecorder& service_location);
 
266
  bool ParsePackage(FileDescriptorProto* file,
 
267
                    const LocationRecorder& root_location);
 
268
  bool ParseImport(string* import_filename,
 
269
                   const LocationRecorder& root_location,
 
270
                   int index);
 
271
  bool ParseOption(Message* options,
 
272
                   const LocationRecorder& options_location);
222
273
 
223
274
  // These methods parse the contents of a message, enum, or service type and
224
275
  // add them to the given object.  They consume the entire block including
225
276
  // the beginning and ending brace.
226
 
  bool ParseMessageBlock(DescriptorProto* message);
227
 
  bool ParseEnumBlock(EnumDescriptorProto* enum_type);
228
 
  bool ParseServiceBlock(ServiceDescriptorProto* service);
 
277
  bool ParseMessageBlock(DescriptorProto* message,
 
278
                         const LocationRecorder& message_location);
 
279
  bool ParseEnumBlock(EnumDescriptorProto* enum_type,
 
280
                      const LocationRecorder& enum_location);
 
281
  bool ParseServiceBlock(ServiceDescriptorProto* service,
 
282
                         const LocationRecorder& service_location);
229
283
 
230
284
  // Parse one statement within a message, enum, or service block, inclunding
231
285
  // final semicolon.
232
 
  bool ParseMessageStatement(DescriptorProto* message);
233
 
  bool ParseEnumStatement(EnumDescriptorProto* message);
234
 
  bool ParseServiceStatement(ServiceDescriptorProto* message);
 
286
  bool ParseMessageStatement(DescriptorProto* message,
 
287
                             const LocationRecorder& message_location);
 
288
  bool ParseEnumStatement(EnumDescriptorProto* message,
 
289
                          const LocationRecorder& enum_location);
 
290
  bool ParseServiceStatement(ServiceDescriptorProto* message,
 
291
                             const LocationRecorder& service_location);
235
292
 
236
293
  // Parse a field of a message.  If the field is a group, its type will be
237
294
  // added to "messages".
 
295
  //
 
296
  // parent_location and location_field_number_for_nested_type are needed when
 
297
  // parsing groups -- we need to generate a nested message type within the
 
298
  // parent and record its location accordingly.  Since the parent could be
 
299
  // either a FileDescriptorProto or a DescriptorProto, we must pass in the
 
300
  // correct field number to use.
238
301
  bool ParseMessageField(FieldDescriptorProto* field,
239
 
                         RepeatedPtrField<DescriptorProto>* messages);
 
302
                         RepeatedPtrField<DescriptorProto>* messages,
 
303
                         const LocationRecorder& parent_location,
 
304
                         int location_field_number_for_nested_type,
 
305
                         const LocationRecorder& field_location);
240
306
 
241
307
  // Parse an "extensions" declaration.
242
 
  bool ParseExtensions(DescriptorProto* message);
 
308
  bool ParseExtensions(DescriptorProto* message,
 
309
                       const LocationRecorder& extensions_location);
243
310
 
244
 
  // Parse an "extend" declaration.
 
311
  // Parse an "extend" declaration.  (See also comments for
 
312
  // ParseMessageField().)
245
313
  bool ParseExtend(RepeatedPtrField<FieldDescriptorProto>* extensions,
246
 
                   RepeatedPtrField<DescriptorProto>* messages);
 
314
                   RepeatedPtrField<DescriptorProto>* messages,
 
315
                   const LocationRecorder& parent_location,
 
316
                   int location_field_number_for_nested_type,
 
317
                   const LocationRecorder& extend_location);
247
318
 
248
319
  // Parse a single enum value within an enum block.
249
 
  bool ParseEnumConstant(EnumValueDescriptorProto* enum_value);
 
320
  bool ParseEnumConstant(EnumValueDescriptorProto* enum_value,
 
321
                         const LocationRecorder& enum_value_location);
250
322
 
251
323
  // Parse enum constant options, i.e. the list in square brackets at the end
252
324
  // of the enum constant value definition.
253
 
  bool ParseEnumConstantOptions(EnumValueDescriptorProto* value);
 
325
  bool ParseEnumConstantOptions(EnumValueDescriptorProto* value,
 
326
                                const LocationRecorder& enum_value_location);
254
327
 
255
328
  // Parse a single method within a service definition.
256
 
  bool ParseServiceMethod(MethodDescriptorProto* method);
 
329
  bool ParseServiceMethod(MethodDescriptorProto* method,
 
330
                          const LocationRecorder& method_location);
257
331
 
258
332
  // Parse "required", "optional", or "repeated" and fill in "label"
259
333
  // with the value.
269
343
 
270
344
  // Parses field options, i.e. the stuff in square brackets at the end
271
345
  // of a field definition.  Also parses default value.
272
 
  bool ParseFieldOptions(FieldDescriptorProto* field);
 
346
  bool ParseFieldOptions(FieldDescriptorProto* field,
 
347
                         const LocationRecorder& field_location);
273
348
 
274
349
  // Parse the "default" option.  This needs special handling because its
275
350
  // type is the field's type.
276
 
  bool ParseDefaultAssignment(FieldDescriptorProto* field);
 
351
  bool ParseDefaultAssignment(FieldDescriptorProto* field,
 
352
                              const LocationRecorder& field_location);
277
353
 
278
354
  // Parse a single option name/value pair, e.g. "ctype = CORD".  The name
279
355
  // identifies a field of the given Message, and the value of that field
280
356
  // is set to the parsed value.
281
 
  bool ParseOptionAssignment(Message* options);
 
357
  bool ParseOptionAssignment(Message* options,
 
358
                             const LocationRecorder& options_location);
282
359
 
283
360
  // Parses a single part of a multipart option name. A multipart name consists
284
361
  // of names separated by dots. Each name is either an identifier or a series
285
362
  // of identifiers separated by dots and enclosed in parentheses. E.g.,
286
363
  // "foo.(bar.baz).qux".
287
 
  bool ParseOptionNamePart(UninterpretedOption* uninterpreted_option);
 
364
  bool ParseOptionNamePart(UninterpretedOption* uninterpreted_option,
 
365
                           const LocationRecorder& part_location);
 
366
 
 
367
  // Parses a string surrounded by balanced braces.  Strips off the outer
 
368
  // braces and stores the enclosed string in *value.
 
369
  // E.g.,
 
370
  //     { foo }                     *value gets 'foo'
 
371
  //     { foo { bar: box } }        *value gets 'foo { bar: box }'
 
372
  //     {}                          *value gets ''
 
373
  //
 
374
  // REQUIRES: LookingAt("{")
 
375
  // When finished successfully, we are looking at the first token past
 
376
  // the ending brace.
 
377
  bool ParseUninterpretedBlock(string* value);
288
378
 
289
379
  // =================================================================
290
380
 
291
381
  io::Tokenizer* input_;
292
382
  io::ErrorCollector* error_collector_;
293
 
  SourceLocationTable* source_location_table_;
 
383
  SourceCodeInfo* source_code_info_;
 
384
  SourceLocationTable* source_location_table_;  // legacy
294
385
  bool had_errors_;
295
386
  bool require_syntax_identifier_;
296
387
  bool stop_after_syntax_identifier_;
302
393
// A table mapping (descriptor, ErrorLocation) pairs -- as reported by
303
394
// DescriptorPool when validating descriptors -- to line and column numbers
304
395
// within the original source code.
 
396
//
 
397
// This is semi-obsolete:  FileDescriptorProto.source_code_info now contains
 
398
// far more complete information about source locations.  However, as of this
 
399
// writing you still need to use SourceLocationTable when integrating with
 
400
// DescriptorPool.
305
401
class LIBPROTOBUF_EXPORT SourceLocationTable {
306
402
 public:
307
403
  SourceLocationTable();