Protocol Buffers バージョン 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
Boolean
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
は、テキスト形式言語仕様 で定義されています。
構文
syntax ステートメントは、protobuf のバージョンを定義するために使用されます。
syntax = "syntax" "=" ("'" "proto3" "'" | '"' "proto3" '"') ";"
例
syntax = "proto3";
import 文
import 文は、別の .proto の定義をインポートするために使用されます。
import = "import" [ "weak" | "public" ] strLit ";"
例
import public "other.proto";
パッケージ
package 指定子は、プロトコルメッセージタイプ間の名前の衝突を防ぐために使用できます。
package = "package" fullIdent ";"
例
package foo.bar;
オプション
オプションは、proto ファイル、メッセージ、enum、およびサービスで使用できます。オプションは、protobuf 定義のオプションまたはカスタムオプションにすることができます。詳細については、言語ガイドの オプション を参照してください。
option = "option" optionName "=" constant ";"
optionName = ( ident | bracedFullIdent ) { "." ( ident | bracedFullIdent ) }
bracedFullIdent = "(" ["."] fullIdent ")"
optionNamePart = { ident | "(" ["."] fullIdent ")" }
例
option java_package = "com.example.foo";
フィールド
フィールドは、Protocol Buffer メッセージの基本的な要素です。フィールドは、通常のフィールド、oneof フィールド、またはマップフィールドにすることができます。フィールドには、型とフィールド番号があります。
type = "double" | "float" | "int32" | "int64" | "uint32" | "uint64"
| "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64"
| "bool" | "string" | "bytes" | messageType | enumType
fieldNumber = intLit;
通常のフィールド
各フィールドには、型、名前、およびフィールド番号があります。フィールドオプションを持つ場合があります。
field = [ "repeated" | "optional" ] 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 = "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 定義は、名前と enum 本体で構成されています。enum 本体には、オプション、enum フィールド、および reserved ステートメントを含めることができます。
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"];
}
メッセージ定義
メッセージは、メッセージ名とメッセージ本体で構成されています。メッセージ本体には、フィールド、ネストされた enum 定義、ネストされたメッセージ定義、オプション、oneof、マップフィールド、および reserved ステートメントを含めることができます。メッセージは、同じメッセージスキーマ内で同じ名前を持つ 2 つのフィールドを含むことはできません。
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;
}