Skip to content

Commit 1bea8d6

Browse files
Clean up reserved grammar changes and add tests
1 parent e5ca5c8 commit 1bea8d6

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/parse.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ function parse(source, root, options) {
8080
pkg,
8181
imports,
8282
weakImports,
83-
edition = "proto2",
84-
isProto3 = false,
85-
isProto2 = true;
83+
edition = "proto2";
8684

8785
var ptr = root;
8886

@@ -151,13 +149,17 @@ function parse(source, root, options) {
151149
function readRanges(target, acceptStrings) {
152150
var token, start;
153151
do {
154-
if (acceptStrings && ((token = peek()) === "\"" || token === "'"))
155-
target.push(readString());
156-
else {
152+
if (acceptStrings && ((token = peek()) === "\"" || token === "'")) {
153+
var str = readString();
154+
target.push(str);
155+
if (edition >= 2023) {
156+
throw illegal(str);
157+
}
158+
} else {
157159
try {
158160
target.push([ start = parseId(next()), skip("to", true) ? parseId(next()) : start ]);
159161
} catch (err) {
160-
if (typeRefRe.test(token) && (!isProto2 && !isProto3)) {
162+
if (acceptStrings && typeRefRe.test(token) && edition >= 2023) {
161163
target.push(token);
162164
} else {
163165
throw err;
@@ -278,11 +280,9 @@ function parse(source, root, options) {
278280
function parseSyntax() {
279281
skip("=");
280282
edition = readString();
281-
isProto3 = edition === "proto3";
282-
isProto2 = edition === "proto2";
283283

284284
/* istanbul ignore if */
285-
if (!isProto3 && !isProto2)
285+
if (edition < 2023)
286286
throw illegal(edition, "syntax");
287287

288288
skip(";");
@@ -291,8 +291,6 @@ function parse(source, root, options) {
291291
function parseEdition() {
292292
skip("=");
293293
edition = readString();
294-
isProto3 = false;
295-
isProto2 = false;
296294
const supportedEditions = ["2023"];
297295

298296
/* istanbul ignore if */
@@ -370,7 +368,7 @@ function parse(source, root, options) {
370368
break;
371369

372370
case "required":
373-
if (!isProto2)
371+
if (edition !== "proto2")
374372
throw illegal(token);
375373
/* eslint-disable no-fallthrough */
376374
case "repeated":
@@ -379,9 +377,9 @@ function parse(source, root, options) {
379377

380378
case "optional":
381379
/* istanbul ignore if */
382-
if (isProto3) {
380+
if (edition === "proto3") {
383381
parseField(type, "proto3_optional");
384-
} else if (!isProto2) {
382+
} else if (edition !== "proto2") {
385383
throw illegal(token);
386384
} else {
387385
parseField(type, "optional");
@@ -402,7 +400,7 @@ function parse(source, root, options) {
402400

403401
default:
404402
/* istanbul ignore if */
405-
if (isProto2 || !typeRefRe.test(token)) {
403+
if (edition === "proto2" || !typeRefRe.test(token)) {
406404
throw illegal(token);
407405
}
408406

@@ -507,7 +505,7 @@ function parse(source, root, options) {
507505

508506
case "optional":
509507
/* istanbul ignore if */
510-
if (isProto3) {
508+
if (edition === "proto3") {
511509
parseField(type, "proto3_optional");
512510
} else {
513511
parseField(type, "optional");
@@ -605,6 +603,7 @@ function parse(source, root, options) {
605603

606604
case "reserved":
607605
readRanges(enm.reserved || (enm.reserved = []), true);
606+
if(enm.reserved === undefined) enm.reserved = [];
608607
break;
609608

610609
default:
@@ -865,7 +864,7 @@ function parse(source, root, options) {
865864

866865
case "optional":
867866
/* istanbul ignore if */
868-
if (isProto3) {
867+
if (edition === "proto3") {
869868
parseField(parent, "proto3_optional", reference);
870869
} else {
871870
parseField(parent, "optional", reference);
@@ -874,7 +873,7 @@ function parse(source, root, options) {
874873

875874
default:
876875
/* istanbul ignore if */
877-
if (isProto2 || !typeRefRe.test(token))
876+
if (edition === "proto2" || !typeRefRe.test(token))
878877
throw illegal(token);
879878
push(token);
880879
parseField(parent, "optional", reference);

0 commit comments

Comments
 (0)