@@ -33,7 +33,6 @@ var base10Re = /^[1-9][0-9]*$/,
3333 * @property {string|undefined } package Package name, if declared
3434 * @property {string[]|undefined } imports Imports, if any
3535 * @property {string[]|undefined } weakImports Weak imports, if any
36- * @property {string|undefined } syntax Syntax, if specified (either `"proto2"` or `"proto3"`)
3736 * @property {Root } root Populated root instance
3837 */
3938
@@ -81,9 +80,9 @@ function parse(source, root, options) {
8180 pkg ,
8281 imports ,
8382 weakImports ,
84- syntax ,
85- edition = false ,
86- isProto3 = false ;
83+ edition = "proto2" ,
84+ isProto3 = false ,
85+ isProto2 = true ;
8786
8887 var ptr = root ;
8988
@@ -145,7 +144,7 @@ function parse(source, root, options) {
145144 try {
146145 target . push ( [ start = parseId ( next ( ) ) , skip ( "to" , true ) ? parseId ( next ( ) ) : start ] ) ;
147146 } catch ( err ) {
148- if ( typeRefRe . test ( token ) && edition ) {
147+ if ( typeRefRe . test ( token ) && ( ! isProto2 && ! isProto3 ) ) {
149148 target . push ( token ) ;
150149 } else {
151150 throw err ;
@@ -228,7 +227,6 @@ function parse(source, root, options) {
228227 }
229228
230229 function parsePackage ( ) {
231-
232230 /* istanbul ignore if */
233231 if ( pkg !== undefined )
234232 throw illegal ( "package" ) ;
@@ -240,6 +238,12 @@ function parse(source, root, options) {
240238 throw illegal ( pkg , "name" ) ;
241239
242240 ptr = ptr . define ( pkg ) ;
241+
242+ var oldEdition = ptr . getOption ( "edition" ) ;
243+ if ( oldEdition && oldEdition !== edition ) {
244+ throw new Error ( "incompatible editions detected in package " + pkg + ": " + edition + " vs " + oldEdition ) ;
245+ }
246+ ptr . setOption ( "edition" , edition ) ;
243247 skip ( ";" ) ;
244248 }
245249
@@ -265,23 +269,26 @@ function parse(source, root, options) {
265269
266270 function parseSyntax ( ) {
267271 skip ( "=" ) ;
268- syntax = readString ( ) ;
269- isProto3 = syntax === "proto3" ;
272+ edition = readString ( ) ;
273+ isProto3 = edition === "proto3" ;
274+ isProto2 = edition === "proto2" ;
270275
271276 /* istanbul ignore if */
272- if ( ! isProto3 && syntax !== "proto2" )
273- throw illegal ( syntax , "syntax" ) ;
277+ if ( ! isProto3 && ! isProto2 )
278+ throw illegal ( edition , "syntax" ) ;
274279
275280 // Syntax is needed to understand the meaning of the optional field rule
276281 // Otherwise the meaning is ambiguous between proto2 and proto3
277- root . setOption ( "syntax " , syntax ) ;
282+ root . setOption ( "edition " , edition ) ;
278283
279284 skip ( ";" ) ;
280285 }
281286
282287 function parseEdition ( ) {
283288 skip ( "=" ) ;
284289 edition = readString ( ) ;
290+ isProto3 = false ;
291+ isProto2 = false ;
285292 const supportedEditions = [ "2023" ] ;
286293
287294 /* istanbul ignore if */
@@ -361,7 +368,7 @@ function parse(source, root, options) {
361368 break ;
362369
363370 case "required" :
364- if ( edition )
371+ if ( ! isProto2 )
365372 throw illegal ( token ) ;
366373 /* eslint-disable no-fallthrough */
367374 case "repeated" :
@@ -372,7 +379,7 @@ function parse(source, root, options) {
372379 /* istanbul ignore if */
373380 if ( isProto3 ) {
374381 parseField ( type , "proto3_optional" ) ;
375- } else if ( edition ) {
382+ } else if ( ! isProto2 ) {
376383 throw illegal ( token ) ;
377384 } else {
378385 parseField ( type , "optional" ) ;
@@ -393,7 +400,7 @@ function parse(source, root, options) {
393400
394401 default :
395402 /* istanbul ignore if */
396- if ( ! isProto3 && ! edition || ! typeRefRe . test ( token ) ) {
403+ if ( isProto2 || ! typeRefRe . test ( token ) ) {
397404 throw illegal ( token ) ;
398405 }
399406
@@ -854,7 +861,7 @@ function parse(source, root, options) {
854861
855862 default :
856863 /* istanbul ignore if */
857- if ( ! isProto3 && ! edition || ! typeRefRe . test ( token ) )
864+ if ( isProto2 || ! typeRefRe . test ( token ) )
858865 throw illegal ( token ) ;
859866 push ( token ) ;
860867 parseField ( parent , "optional" , reference ) ;
@@ -924,7 +931,6 @@ function parse(source, root, options) {
924931 "package" : pkg ,
925932 "imports" : imports ,
926933 weakImports : weakImports ,
927- syntax : syntax ,
928934 root : root
929935 } ;
930936}
0 commit comments