Skip to content

Commit 2d58011

Browse files
feat: add handling for extension range options (#1990)
* Add handling for extension range options * Fix CI failures * Fix more test issues
1 parent 2f846fe commit 2d58011

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

src/parse.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,24 @@ function parse(source, root, options) {
144144
else
145145
target.push([ start = parseId(next()), skip("to", true) ? parseId(next()) : start ]);
146146
} while (skip(",", true));
147-
skip(";");
147+
var dummy = {options: undefined};
148+
dummy.setOption = function(name, value) {
149+
if (this.options === undefined) this.options = {};
150+
this.options[name] = value;
151+
};
152+
ifBlock(
153+
dummy,
154+
function parseRange_block(token) {
155+
/* istanbul ignore else */
156+
if (token === "option") {
157+
parseOption(dummy, token); // skip
158+
skip(";");
159+
} else
160+
throw illegal(token);
161+
},
162+
function parseRange_line() {
163+
parseInlineOptions(dummy); // skip
164+
});
148165
}
149166

150167
function parseNumber(token, insideTryCatch) {

tests/data/google/protobuf/descriptor.proto

+60-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ message DescriptorProto {
101101
message ExtensionRange {
102102
optional int32 start = 1;
103103
optional int32 end = 2;
104+
105+
optional ExtensionRangeOptions options = 3;
104106
}
105107
repeated ExtensionRange extension_range = 5;
106108

@@ -121,6 +123,63 @@ message DescriptorProto {
121123
repeated string reserved_name = 10;
122124
}
123125

126+
127+
message ExtensionRangeOptions {
128+
// The parser stores options it doesn't recognize here. See above.
129+
repeated UninterpretedOption uninterpreted_option = 999;
130+
131+
message Declaration {
132+
// The extension number declared within the extension range.
133+
optional int32 number = 1;
134+
135+
// The fully-qualified name of the extension field. There must be a leading
136+
// dot in front of the full name.
137+
optional string full_name = 2;
138+
139+
// The fully-qualified type name of the extension field. Unlike
140+
// Metadata.type, Declaration.type must have a leading dot for messages
141+
// and enums.
142+
optional string type = 3;
143+
144+
// If true, indicates that the number is reserved in the extension range,
145+
// and any extension field with the number will fail to compile. Set this
146+
// when a declared extension field is deleted.
147+
optional bool reserved = 5;
148+
149+
// If true, indicates that the extension must be defined as repeated.
150+
// Otherwise the extension must be defined as optional.
151+
optional bool repeated = 6;
152+
153+
reserved 4; // removed is_repeated
154+
}
155+
156+
// For external users: DO NOT USE. We are in the process of open sourcing
157+
// extension declaration and executing internal cleanups before it can be
158+
// used externally.
159+
repeated Declaration declaration = 2 [retention = RETENTION_SOURCE];
160+
161+
// The verification state of the extension range.
162+
enum VerificationState {
163+
// All the extensions of the range must be declared.
164+
DECLARATION = 0;
165+
UNVERIFIED = 1;
166+
}
167+
168+
// The verification state of the range.
169+
// TODO(b/278783756): flip the default to DECLARATION once all empty ranges
170+
// are marked as UNVERIFIED.
171+
optional VerificationState verification = 3
172+
[default = UNVERIFIED, retention = RETENTION_SOURCE];
173+
174+
// Clients can define custom options in extensions of this message. See above.
175+
// BEGIN GOOGLE-INTERNAL
176+
// Extension numbers > 530,000,000 must be forward-declared in
177+
// google3/third_party/protobuf/extdecl_extension_range_options.h. See
178+
// go/extension-declaration-reserved-numbers for more information.
179+
// END GOOGLE-INTERNAL
180+
extensions 1000 to max;
181+
}
182+
124183
// Describes a field within a message.
125184
message FieldDescriptorProto {
126185
enum Type {
@@ -813,4 +872,4 @@ message GeneratedCodeInfo {
813872
// the last relevant byte (so the length of the text = end - begin).
814873
optional int32 end = 4;
815874
}
816-
}
875+
}

tests/data/uncommon.proto

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ message Test2 {
4747
extend Test;
4848
extend Test{};
4949
extend Test{required int32 a=1;} // not validated by the parser
50+
extend Test{Test inner_ext=1000;} // not validated by the parser
51+
52+
extensions 1000 to 1999 [declaration = {
53+
number: 1000
54+
full_name: 'uncommon.Test.inner_ext'
55+
type: 'uncommon.Test'
56+
}];
5057
};
5158

5259
enum Test3;

0 commit comments

Comments
 (0)