parser.h

This section contains reference documentation for working with protocol buffer classes in C++.

#include <google/protobuf/compiler/parser.h>
namespace google::protobuf::compiler

Implements parsing of .proto files to FileDescriptorProtos.

Classes in this file

Implements parsing of protocol definitions (such as .proto files).
A table mapping (descriptor, ErrorLocation) pairs – as reported by DescriptorPool when validating descriptors – to line and column numbers within the original source code.

class Parser

#include <google/protobuf/compiler/parser.h>
namespace google::protobuf::compiler

Implements parsing of protocol definitions (such as .proto files).

Note that most users will be more interested in the Importer class. Parser is a lower-level class which simply converts a single .proto file to a FileDescriptorProto. It does not resolve import directives or perform many other kinds of validation needed to construct a complete FileDescriptor.

Members

Parser()
~Parser()
bool
Parse(io::Tokenizer * input, FileDescriptorProto * file)
Parse the entire input and construct a FileDescriptorProto representing it. more...
void
RecordSourceLocationsTo(SourceLocationTable * location_table)
DEPRECATED: New code should use the SourceCodeInfo embedded in the FileDescriptorProto. more...
void
RecordErrorsTo(io::ErrorCollector * error_collector)
Requests that errors be recorded to the given ErrorCollector while parsing. more...
const std::string &
GetSyntaxIdentifier()
Returns the identifier used in the "syntax = " declaration, if one was seen during the last call to Parse(), or the empty string otherwise.
void
SetRequireSyntaxIdentifier(bool value)
If set true, input files will be required to begin with a syntax identifier. more...
void
SetStopAfterSyntaxIdentifier(bool value)
Call SetStopAfterSyntaxIdentifier(true) to tell the parser to stop parsing as soon as it has seen the syntax identifier, or lack thereof. more...

bool Parser::Parse(
        io::Tokenizer * input,
        FileDescriptorProto * file)

Parse the entire input and construct a FileDescriptorProto representing it.

Returns true if no errors occurred, false otherwise.


void Parser::RecordSourceLocationsTo(
        SourceLocationTable * location_table)

DEPRECATED: New code should use the SourceCodeInfo embedded in the FileDescriptorProto.

Requests that locations of certain definitions be recorded to the given SourceLocationTable while parsing. This can be used to look up exact line and column numbers for errors reported by DescriptorPool during validation. Set to NULL (the default) to discard source location information.


void Parser::RecordErrorsTo(
        io::ErrorCollector * error_collector)

Requests that errors be recorded to the given ErrorCollector while parsing.

Set to NULL (the default) to discard error messages.


void Parser::SetRequireSyntaxIdentifier(
        bool value)

If set true, input files will be required to begin with a syntax identifier.

Otherwise, files may omit this. If a syntax identifier is provided, it must be 'syntax = "proto2";' and must appear at the top of this file regardless of whether or not it was required.


void Parser::SetStopAfterSyntaxIdentifier(
        bool value)

Call SetStopAfterSyntaxIdentifier(true) to tell the parser to stop parsing as soon as it has seen the syntax identifier, or lack thereof.

This is useful for quickly identifying the syntax of the file without parsing the whole thing. If this is enabled, no error will be recorded if the syntax identifier is something other than "proto2" (since presumably the caller intends to deal with that), but other kinds of errors (e.g. parse errors) will still be reported. When this is enabled, you may pass a NULL FileDescriptorProto to Parse().

class SourceLocationTable

#include <google/protobuf/compiler/parser.h>
namespace google::protobuf::compiler

A table mapping (descriptor, ErrorLocation) pairs – as reported by DescriptorPool when validating descriptors – to line and column numbers within the original source code.

This is semi-obsolete: FileDescriptorProto.source_code_info now contains far more complete information about source locations. However, as of this writing you still need to use SourceLocationTable when integrating with DescriptorPool.

Members

SourceLocationTable()
~SourceLocationTable()
bool
Find(const Message * descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, int * line, int * column) const
Finds the precise location of the given error and fills in *line and *column with the line and column numbers. more...
bool
FindImport(const Message * descriptor, const std::string & name, int * line, int * column) const
void
Add(const Message * descriptor, DescriptorPool::ErrorCollector::ErrorLocation location, int line, int column)
Adds a location to the table.
void
AddImport(const Message * descriptor, const std::string & name, int line, int column)
void
Clear()
Clears the contents of the table.

bool SourceLocationTable::Find(
        const Message * descriptor,
        DescriptorPool::ErrorCollector::ErrorLocation location,
        int * line,
        int * column) const

Finds the precise location of the given error and fills in *line and *column with the line and column numbers.

If not found, sets *line to -1 and *column to 0 (since line = -1 is used to mean "error has no exact location" in the ErrorCollector interface). Returns true if found, false otherwise.