Skip to content

Commit 1a3cb08

Browse files
committed
Ignore reserved fields for enums
ProtoBuf5 ignores reserved fields except in the case of enums. Fixes protobufjs#1284 Signed-off-by: Gari Singh <[email protected]>
1 parent 2c2ffbc commit 1a3cb08

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

src/ProtoBuf/DotProto/Parser.js

+65-60
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @param {string} source Source
1111
* @constructor
1212
*/
13-
var Parser = function(source) {
13+
var Parser = function (source) {
1414

1515
/**
1616
* Tokenizer.
@@ -38,7 +38,7 @@ var ParserPrototype = Parser.prototype;
3838
* @throws {Error} If the source cannot be parsed
3939
* @expose
4040
*/
41-
ParserPrototype.parse = function() {
41+
ParserPrototype.parse = function () {
4242
var topLevel = {
4343
"name": "[ROOT]", // temporary
4444
"package": null,
@@ -105,7 +105,7 @@ ParserPrototype.parse = function() {
105105
}
106106
}
107107
} catch (e) {
108-
e.message = "Parse error at line "+this.tn.line+": " + e.message;
108+
e.message = "Parse error at line " + this.tn.line + ": " + e.message;
109109
throw e;
110110
}
111111
delete topLevel["name"];
@@ -118,7 +118,7 @@ ParserPrototype.parse = function() {
118118
* @throws {Error} If the source cannot be parsed
119119
* @expose
120120
*/
121-
Parser.parse = function(source) {
121+
Parser.parse = function (source) {
122122
return new Parser(source).parse();
123123
};
124124

@@ -146,7 +146,7 @@ function mkId(value, mayBeNegative) {
146146
id = parseInt(value.substring(1), 8);
147147
else
148148
throw Error("illegal id value: " + (sign < 0 ? '-' : '') + value);
149-
id = (sign*id)|0; // Force to 32bit
149+
id = (sign * id) | 0; // Force to 32bit
150150
if (!mayBeNegative && id < 0)
151151
throw Error("illegal id value: " + (sign < 0 ? '-' : '') + value);
152152
return id;
@@ -186,14 +186,14 @@ function mkNumber(val) {
186186
* @returns {string}
187187
* @private
188188
*/
189-
ParserPrototype._readString = function() {
189+
ParserPrototype._readString = function () {
190190
var value = "",
191191
token,
192192
delim;
193193
do {
194194
delim = this.tn.next();
195195
if (delim !== "'" && delim !== '"')
196-
throw Error("illegal string delimiter: "+delim);
196+
throw Error("illegal string delimiter: " + delim);
197197
value += this.tn.next();
198198
this.tn.skip(delim);
199199
token = this.tn.peek();
@@ -207,7 +207,7 @@ ParserPrototype._readString = function() {
207207
* @returns {number|boolean|string}
208208
* @private
209209
*/
210-
ParserPrototype._readValue = function(mayBeTypeRef) {
210+
ParserPrototype._readValue = function (mayBeTypeRef) {
211211
var token = this.tn.peek(),
212212
value;
213213
if (token === '"' || token === "'")
@@ -219,7 +219,7 @@ ParserPrototype._readValue = function(mayBeTypeRef) {
219219
return (token.toLowerCase() === 'true');
220220
if (mayBeTypeRef && Lang.TYPEREF.test(token))
221221
return token;
222-
throw Error("illegal value: "+token);
222+
throw Error("illegal value: " + token);
223223

224224
};
225225

@@ -231,7 +231,7 @@ ParserPrototype._readValue = function(mayBeTypeRef) {
231231
* @param {boolean=} isList
232232
* @private
233233
*/
234-
ParserPrototype._parseOption = function(parent, isList) {
234+
ParserPrototype._parseOption = function (parent, isList) {
235235
var token = this.tn.next(),
236236
custom = false;
237237
if (token === '(') {
@@ -241,11 +241,11 @@ ParserPrototype._parseOption = function(parent, isList) {
241241
if (!Lang.TYPEREF.test(token))
242242
// we can allow options of the form google.protobuf.* since they will just get ignored anyways
243243
// if (!/google\.protobuf\./.test(token)) // FIXME: Why should that not be a valid typeref?
244-
throw Error("illegal option name: "+token);
244+
throw Error("illegal option name: " + token);
245245
var name = token;
246246
if (custom) { // (my_method_option).foo, (my_method_option), some_method_option, (foo.my_option).bar
247247
this.tn.skip(')');
248-
name = '('+name+')';
248+
name = '(' + name + ')';
249249
token = this.tn.peek();
250250
if (Lang.FQTYPEREF.test(token)) {
251251
name += token;
@@ -270,7 +270,7 @@ function setOption(options, name, value) {
270270
options[name] = value;
271271
else {
272272
if (!Array.isArray(options[name]))
273-
options[name] = [ options[name] ];
273+
options[name] = [options[name]];
274274
options[name].push(value);
275275
}
276276
}
@@ -281,7 +281,7 @@ function setOption(options, name, value) {
281281
* @param {string} name
282282
* @private
283283
*/
284-
ParserPrototype._parseOptionValue = function(parent, name) {
284+
ParserPrototype._parseOptionValue = function (parent, name) {
285285
var token = this.tn.peek();
286286
if (token !== '{') { // Plain value
287287
setOption(parent["options"], name, this._readValue(true));
@@ -303,10 +303,10 @@ ParserPrototype._parseOptionValue = function(parent, name) {
303303
* @param {!Object} parent Parent definition
304304
* @private
305305
*/
306-
ParserPrototype._parseService = function(parent) {
306+
ParserPrototype._parseService = function (parent) {
307307
var token = this.tn.next();
308308
if (!Lang.NAME.test(token))
309-
throw Error("illegal service name at line "+this.tn.line+": "+token);
309+
throw Error("illegal service name at line " + this.tn.line + ": " + token);
310310
var name = token;
311311
var svc = {
312312
"name": name,
@@ -320,7 +320,7 @@ ParserPrototype._parseService = function(parent) {
320320
else if (token === 'rpc')
321321
this._parseServiceRPC(svc);
322322
else
323-
throw Error("illegal service token: "+token);
323+
throw Error("illegal service token: " + token);
324324
}
325325
this.tn.omit(";");
326326
parent["services"].push(svc);
@@ -331,11 +331,11 @@ ParserPrototype._parseService = function(parent) {
331331
* @param {!Object} svc Service definition
332332
* @private
333333
*/
334-
ParserPrototype._parseServiceRPC = function(svc) {
334+
ParserPrototype._parseServiceRPC = function (svc) {
335335
var type = "rpc",
336336
token = this.tn.next();
337337
if (!Lang.NAME.test(token))
338-
throw Error("illegal rpc service method name: "+token);
338+
throw Error("illegal rpc service method name: " + token);
339339
var name = token;
340340
var method = {
341341
"request": null,
@@ -347,21 +347,21 @@ ParserPrototype._parseServiceRPC = function(svc) {
347347
this.tn.skip("(");
348348
token = this.tn.next();
349349
if (token.toLowerCase() === "stream") {
350-
method["request_stream"] = true;
351-
token = this.tn.next();
350+
method["request_stream"] = true;
351+
token = this.tn.next();
352352
}
353353
if (!Lang.TYPEREF.test(token))
354-
throw Error("illegal rpc service request type: "+token);
354+
throw Error("illegal rpc service request type: " + token);
355355
method["request"] = token;
356356
this.tn.skip(")");
357357
token = this.tn.next();
358358
if (token.toLowerCase() !== "returns")
359-
throw Error("illegal rpc service request type delimiter: "+token);
359+
throw Error("illegal rpc service request type delimiter: " + token);
360360
this.tn.skip("(");
361361
token = this.tn.next();
362362
if (token.toLowerCase() === "stream") {
363-
method["response_stream"] = true;
364-
token = this.tn.next();
363+
method["response_stream"] = true;
364+
token = this.tn.next();
365365
}
366366
method["response"] = token;
367367
this.tn.skip(")");
@@ -389,7 +389,7 @@ ParserPrototype._parseServiceRPC = function(svc) {
389389
* @returns {!Object}
390390
* @private
391391
*/
392-
ParserPrototype._parseMessage = function(parent, fld) {
392+
ParserPrototype._parseMessage = function (parent, fld) {
393393
var isGroup = !!fld,
394394
token = this.tn.next();
395395
var msg = {
@@ -403,7 +403,7 @@ ParserPrototype._parseMessage = function(parent, fld) {
403403
// "extensions": undefined
404404
};
405405
if (!Lang.NAME.test(token))
406-
throw Error("illegal "+(isGroup ? "group" : "message")+" name: "+token);
406+
throw Error("illegal " + (isGroup ? "group" : "message") + " name: " + token);
407407
msg["name"] = token;
408408
if (isGroup) {
409409
this.tn.skip("=");
@@ -439,10 +439,10 @@ ParserPrototype._parseMessage = function(parent, fld) {
439439
this._parseExtend(msg);
440440
else if (Lang.TYPEREF.test(token)) {
441441
if (!this.proto3)
442-
throw Error("illegal field rule: "+token);
442+
throw Error("illegal field rule: " + token);
443443
this._parseMessageField(msg, "optional", token);
444444
} else
445-
throw Error("illegal message token: "+token);
445+
throw Error("illegal message token: " + token);
446446
}
447447
this.tn.omit(";");
448448
parent["messages"].push(msg);
@@ -453,7 +453,7 @@ ParserPrototype._parseMessage = function(parent, fld) {
453453
* Parses an ignored statement.
454454
* @private
455455
*/
456-
ParserPrototype._parseIgnored = function() {
456+
ParserPrototype._parseIgnored = function () {
457457
while (this.tn.peek() !== ';')
458458
this.tn.next();
459459
this.tn.skip(";");
@@ -467,9 +467,9 @@ ParserPrototype._parseIgnored = function() {
467467
* @returns {!Object} Field descriptor
468468
* @private
469469
*/
470-
ParserPrototype._parseMessageField = function(msg, rule, type) {
470+
ParserPrototype._parseMessageField = function (msg, rule, type) {
471471
if (!Lang.RULE.test(rule))
472-
throw Error("illegal message field rule: "+rule);
472+
throw Error("illegal message field rule: " + rule);
473473
var fld = {
474474
"rule": rule,
475475
"type": "",
@@ -515,7 +515,7 @@ ParserPrototype._parseMessageField = function(msg, rule, type) {
515515
// converted to lower-case so that it does not conflict with the former)."
516516
var grp = this._parseMessage(msg, fld);
517517
if (!/^[A-Z]/.test(grp["name"]))
518-
throw Error('illegal group name: '+grp["name"]);
518+
throw Error('illegal group name: ' + grp["name"]);
519519
fld["type"] = grp["name"];
520520
fld["name"] = grp["name"].toLowerCase();
521521
this.tn.omit(";");
@@ -547,10 +547,10 @@ ParserPrototype._parseMessageField = function(msg, rule, type) {
547547
* @param {!Object} msg Message definition
548548
* @private
549549
*/
550-
ParserPrototype._parseMessageOneOf = function(msg) {
550+
ParserPrototype._parseMessageOneOf = function (msg) {
551551
var token = this.tn.next();
552552
if (!Lang.NAME.test(token))
553-
throw Error("illegal oneof name: "+token);
553+
throw Error("illegal oneof name: " + token);
554554
var name = token,
555555
fld;
556556
var fields = [];
@@ -569,7 +569,7 @@ ParserPrototype._parseMessageOneOf = function(msg) {
569569
* @param {!Object} fld Field definition
570570
* @private
571571
*/
572-
ParserPrototype._parseFieldOptions = function(fld) {
572+
ParserPrototype._parseFieldOptions = function (fld) {
573573
this.tn.skip("[");
574574
var token,
575575
first = true;
@@ -587,33 +587,38 @@ ParserPrototype._parseFieldOptions = function(fld) {
587587
* @param {!Object} msg Message definition
588588
* @private
589589
*/
590-
ParserPrototype._parseEnum = function(msg) {
590+
ParserPrototype._parseEnum = function (msg) {
591591
var enm = {
592592
"name": "",
593593
"values": [],
594594
"options": {}
595595
};
596596
var token = this.tn.next();
597597
if (!Lang.NAME.test(token))
598-
throw Error("illegal name: "+token);
598+
throw Error("illegal name: " + token);
599599
enm["name"] = token;
600600
this.tn.skip("{");
601601
while ((token = this.tn.next()) !== '}') {
602-
if (token === "option")
603-
this._parseOption(enm);
604-
else {
605-
if (!Lang.NAME.test(token))
606-
throw Error("illegal name: "+token);
607-
this.tn.skip("=");
608-
var val = {
609-
"name": token,
610-
"id": mkId(this.tn.next(), true)
611-
};
612-
token = this.tn.peek();
613-
if (token === "[")
614-
this._parseFieldOptions({ "options": {} });
615-
this.tn.skip(";");
616-
enm["values"].push(val);
602+
switch (token) {
603+
case "option":
604+
this._parseOption(enm);
605+
break;
606+
case "reserved":
607+
this._parseIgnored();
608+
break;
609+
default:
610+
if (!Lang.NAME.test(token))
611+
throw Error("illegal name: " + token);
612+
this.tn.skip("=");
613+
var val = {
614+
"name": token,
615+
"id": mkId(this.tn.next(), true)
616+
};
617+
token = this.tn.peek();
618+
if (token === "[")
619+
this._parseFieldOptions({ "options": {} });
620+
this.tn.skip(";");
621+
enm["values"].push(val);
617622
}
618623
}
619624
this.tn.omit(";");
@@ -625,7 +630,7 @@ ParserPrototype._parseEnum = function(msg) {
625630
* @returns {!Array.<!Array.<number>>}
626631
* @private
627632
*/
628-
ParserPrototype._parseExtensionRanges = function() {
633+
ParserPrototype._parseExtensionRanges = function () {
629634
var ranges = [];
630635
var token,
631636
range,
@@ -665,10 +670,10 @@ ParserPrototype._parseExtensionRanges = function() {
665670
* @param {!Object} parent Parent object
666671
* @private
667672
*/
668-
ParserPrototype._parseExtend = function(parent) {
673+
ParserPrototype._parseExtend = function (parent) {
669674
var token = this.tn.next();
670675
if (!Lang.TYPEREF.test(token))
671-
throw Error("illegal extend reference: "+token);
676+
throw Error("illegal extend reference: " + token);
672677
var ext = {
673678
"ref": token,
674679
"fields": []
@@ -679,10 +684,10 @@ ParserPrototype._parseExtend = function(parent) {
679684
this._parseMessageField(ext, token);
680685
else if (Lang.TYPEREF.test(token)) {
681686
if (!this.proto3)
682-
throw Error("illegal field rule: "+token);
687+
throw Error("illegal field rule: " + token);
683688
this._parseMessageField(ext, "optional", token);
684689
} else
685-
throw Error("illegal extend token: "+token);
690+
throw Error("illegal extend token: " + token);
686691
}
687692
this.tn.omit(";");
688693
parent["messages"].push(ext);
@@ -695,6 +700,6 @@ ParserPrototype._parseExtend = function(parent) {
695700
* Returns a string representation of this parser.
696701
* @returns {string}
697702
*/
698-
ParserPrototype.toString = function() {
699-
return "Parser at line "+this.tn.line;
703+
ParserPrototype.toString = function () {
704+
return "Parser at line " + this.tn.line;
700705
};

0 commit comments

Comments
 (0)