180
183
// of the current token.
181
184
void AddError(const string& error);
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);
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 {
193
// Construct the file's "root" location.
194
LocationRecorder(Parser* parser);
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);
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);
209
// Add a path component. See SourceCodeInfo.Location.path in
211
void AddPath(int path_component);
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);
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);
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);
232
SourceCodeInfo::Location* location_;
234
void Init(const LocationRecorder& parent);
194
237
// =================================================================
195
238
// Parsers for various language constructs
210
253
// makes logic much simpler for the caller.
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);
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,
271
bool ParseOption(Message* options,
272
const LocationRecorder& options_location);
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);
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);
236
293
// Parse a field of a message. If the field is a group, its type will be
237
294
// added to "messages".
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);
241
307
// Parse an "extensions" declaration.
242
bool ParseExtensions(DescriptorProto* message);
308
bool ParseExtensions(DescriptorProto* message,
309
const LocationRecorder& extensions_location);
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);
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);
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);
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);
258
332
// Parse "required", "optional", or "repeated" and fill in "label"
259
333
// with the value.
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);
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);
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);
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);
367
// Parses a string surrounded by balanced braces. Strips off the outer
368
// braces and stores the enclosed string in *value.
370
// { foo } *value gets 'foo'
371
// { foo { bar: box } } *value gets 'foo { bar: box }'
374
// REQUIRES: LookingAt("{")
375
// When finished successfully, we are looking at the first token past
377
bool ParseUninterpretedBlock(string* value);
289
379
// =================================================================
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_;