Protocol Buffers 版本 3 语言规范
Protocol Buffers 语言(proto3)版本 3 的语言规范参考。
语法使用扩展巴克斯-诺尔范式 (EBNF) 指定。
| alternation
() grouping
[] option (zero or one time)
{} repetition (any number of times)
有关使用 proto3 的更多信息,请参阅语言指南。
词法元素
字母和数字
letter = "A" ... "Z" | "a" ... "z"
decimalDigit = "0" ... "9"
octalDigit = "0" ... "7"
hexDigit = "0" ... "9" | "A" ... "F" | "a" ... "f"
标识符
ident = letter { letter | decimalDigit | "_" }
fullIdent = ident { "." ident }
messageName = ident
enumName = ident
fieldName = ident
oneofName = ident
mapName = ident
serviceName = ident
rpcName = ident
messageType = [ "." ] { ident "." } messageName
enumType = [ "." ] { ident "." } enumName
整数字面量
intLit = decimalLit | octalLit | hexLit
decimalLit = [-] ( "1" ... "9" ) { decimalDigit }
octalLit = [-] "0" { octalDigit }
hexLit = [-] "0" ( "x" | "X" ) hexDigit { hexDigit }
浮点数字面量
floatLit = [-] ( decimals "." [ decimals ] [ exponent ] | decimals exponent | "."decimals [ exponent ] ) | "inf" | "nan"
decimals = [-] decimalDigit { decimalDigit }
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals
布尔值
boolLit = "true" | "false"
字符串字面量
strLit = strLitSingle { strLitSingle }
strLitSingle = ( "'" { charValue } "'" ) | ( '"' { charValue } '"' )
charValue = hexEscape | octEscape | charEscape | unicodeEscape | unicodeLongEscape | /[^\0\n\\]/
hexEscape = '\' ( "x" | "X" ) hexDigit [ hexDigit ]
octEscape = '\' octalDigit [ octalDigit [ octalDigit ] ]
charEscape = '\' ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | '\' | "'" | '"' )
unicodeEscape = '\' "u" hexDigit hexDigit hexDigit hexDigit
unicodeLongEscape = '\' "U" ( "000" hexDigit hexDigit hexDigit hexDigit hexDigit |
"0010" hexDigit hexDigit hexDigit hexDigit
空语句
emptyStatement = ";"
常量
constant = fullIdent | ( [ "-" | "+" ] intLit ) | ( [ "-" | "+" ] floatLit ) |
strLit | boolLit | MessageValue
MessageValue
在文本格式语言规范 中定义。
语法
语法语句用于定义 protobuf 版本。
syntax = "syntax" "=" ("'" "proto3" "'" | '"' "proto3" '"') ";"
示例
syntax = "proto3";
导入语句
导入语句用于导入另一个 .proto 的定义。
import = "import" [ "weak" | "public" ] strLit ";"
示例
import public "other.proto";
包
包说明符可用于防止协议消息类型之间的名称冲突。
package = "package" fullIdent ";"
示例
package foo.bar;
选项
选项可用于 proto 文件、消息、枚举和服务中。选项可以是 protobuf 定义的选项或自定义选项。有关更多信息,请参阅语言指南中的选项。
option = "option" optionName "=" constant ";"
optionName = ( ident | bracedFullIdent ) { "." ( ident | bracedFullIdent ) }
bracedFullIdent = "(" ["."] fullIdent ")"
optionNamePart = { ident | "(" ["."] fullIdent ")" }
示例
option java_package = "com.example.foo";
字段
字段是协议缓冲区消息的基本元素。字段可以是普通字段、oneof 字段或映射字段。字段具有类型和字段编号。
type = "double" | "float" | "int32" | "int64" | "uint32" | "uint64"
| "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64"
| "bool" | "string" | "bytes" | messageType | enumType
fieldNumber = intLit;
普通字段
每个字段都有类型、名称和字段编号。它可能具有字段选项。
field = [ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
fieldOptions = fieldOption { "," fieldOption }
fieldOption = optionName "=" constant
示例
foo.Bar nested_message = 2;
repeated int32 samples = 4 [packed=true];
Oneof 和 Oneof 字段
一个 oneof 由 oneof 字段和一个 oneof 名称组成。
oneof = "oneof" oneofName "{" { option | oneofField } "}"
oneofField = type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
示例
oneof foo {
string name = 4;
SubMessage sub_message = 9;
}
映射字段
映射字段具有键类型、值类型、名称和字段编号。键类型可以是任何整型或字符串类型。
mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
keyType = "int32" | "int64" | "uint32" | "uint64" | "sint32" | "sint64" |
"fixed32" | "fixed64" | "sfixed32" | "sfixed64" | "bool" | "string"
示例
map<string, Project> projects = 3;
保留
保留语句声明在此消息中不能使用的字段编号或字段名称范围。
reserved = "reserved" ( ranges | strFieldNames ) ";"
ranges = range { "," range }
range = intLit [ "to" ( intLit | "max" ) ]
strFieldNames = strFieldName { "," strFieldName }
strFieldName = "'" fieldName "'" | '"' fieldName '"'
示例
reserved 2, 15, 9 to 11;
reserved "foo", "bar";
顶级定义
枚举定义
枚举定义由名称和枚举体组成。枚举体可以包含选项、枚举字段和保留语句。
enum = "enum" enumName enumBody
enumBody = "{" { option | enumField | emptyStatement | reserved } "}"
enumField = ident "=" [ "-" ] intLit [ "[" enumValueOption { "," enumValueOption } "]" ]";"
enumValueOption = optionName "=" constant
示例
enum EnumAllowingAlias {
option allow_alias = true;
EAA_UNSPECIFIED = 0;
EAA_STARTED = 1;
EAA_RUNNING = 2 [(custom_option) = "hello world"];
}
消息定义
消息由消息名称和消息体组成。消息体可以包含字段、嵌套枚举定义、嵌套消息定义、选项、oneof、映射字段和保留语句。消息不能在同一消息架构中包含两个名称相同的字段。
message = "message" messageName messageBody
messageBody = "{" { field | enum | message | option | oneof | mapField |
reserved | emptyStatement } "}"
示例
message Outer {
option (my_option).a = true;
message Inner { // Level 2
int64 ival = 1;
}
map<int32, string> my_map = 2;
}
在消息内部声明的实体都不能有冲突的名称。以下所有情况都禁止
message MyMessage {
optional string foo = 1;
message foo {}
}
message MyMessage {
optional string foo = 1;
oneof foo {
string bar = 2;
}
}
message MyMessage {
optional string foo = 1;
enum E {
foo = 0;
}
}
服务定义
service = "service" serviceName "{" { option | rpc | emptyStatement } "}"
rpc = "rpc" rpcName "(" [ "stream" ] messageType ")" "returns" "(" [ "stream" ]
messageType ")" (( "{" {option | emptyStatement } "}" ) | ";")
示例
service SearchService {
rpc Search (SearchRequest) returns (SearchResponse);
}
Proto 文件
proto = [syntax] { import | package | option | topLevelDef | emptyStatement }
topLevelDef = message | enum | service
一个 .proto 文件示例
syntax = "proto3";
import public "other.proto";
option java_package = "com.example.foo";
enum EnumAllowingAlias {
option allow_alias = true;
EAA_UNSPECIFIED = 0;
EAA_STARTED = 1;
EAA_RUNNING = 1;
EAA_FINISHED = 2 [(custom_option) = "hello world"];
}
message Outer {
option (my_option).a = true;
message Inner { // Level 2
int64 ival = 1;
}
repeated Inner inner_message = 2;
EnumAllowingAlias enum_field = 3;
map<int32, string> my_map = 4;
}