@@ -33,7 +33,6 @@ var base10Re = /^[1-9][0-9]*$/,
33
33
* @property {string|undefined } package Package name, if declared
34
34
* @property {string[]|undefined } imports Imports, if any
35
35
* @property {string[]|undefined } weakImports Weak imports, if any
36
- * @property {string|undefined } syntax Syntax, if specified (either `"proto2"` or `"proto3"`)
37
36
* @property {Root } root Populated root instance
38
37
*/
39
38
@@ -81,9 +80,9 @@ function parse(source, root, options) {
81
80
pkg ,
82
81
imports ,
83
82
weakImports ,
84
- syntax ,
85
- edition = false ,
86
- isProto3 = false ;
83
+ edition = "proto2" ,
84
+ isProto3 = false ,
85
+ isProto2 = true ;
87
86
88
87
var ptr = root ;
89
88
@@ -145,7 +144,7 @@ function parse(source, root, options) {
145
144
try {
146
145
target . push ( [ start = parseId ( next ( ) ) , skip ( "to" , true ) ? parseId ( next ( ) ) : start ] ) ;
147
146
} catch ( err ) {
148
- if ( typeRefRe . test ( token ) && edition ) {
147
+ if ( typeRefRe . test ( token ) && ( ! isProto2 && ! isProto3 ) ) {
149
148
target . push ( token ) ;
150
149
} else {
151
150
throw err ;
@@ -228,7 +227,6 @@ function parse(source, root, options) {
228
227
}
229
228
230
229
function parsePackage ( ) {
231
-
232
230
/* istanbul ignore if */
233
231
if ( pkg !== undefined )
234
232
throw illegal ( "package" ) ;
@@ -240,6 +238,12 @@ function parse(source, root, options) {
240
238
throw illegal ( pkg , "name" ) ;
241
239
242
240
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 ) ;
243
247
skip ( ";" ) ;
244
248
}
245
249
@@ -265,23 +269,26 @@ function parse(source, root, options) {
265
269
266
270
function parseSyntax ( ) {
267
271
skip ( "=" ) ;
268
- syntax = readString ( ) ;
269
- isProto3 = syntax === "proto3" ;
272
+ edition = readString ( ) ;
273
+ isProto3 = edition === "proto3" ;
274
+ isProto2 = edition === "proto2" ;
270
275
271
276
/* istanbul ignore if */
272
- if ( ! isProto3 && syntax !== "proto2" )
273
- throw illegal ( syntax , "syntax" ) ;
277
+ if ( ! isProto3 && ! isProto2 )
278
+ throw illegal ( edition , "syntax" ) ;
274
279
275
280
// Syntax is needed to understand the meaning of the optional field rule
276
281
// Otherwise the meaning is ambiguous between proto2 and proto3
277
- root . setOption ( "syntax " , syntax ) ;
282
+ root . setOption ( "edition " , edition ) ;
278
283
279
284
skip ( ";" ) ;
280
285
}
281
286
282
287
function parseEdition ( ) {
283
288
skip ( "=" ) ;
284
289
edition = readString ( ) ;
290
+ isProto3 = false ;
291
+ isProto2 = false ;
285
292
const supportedEditions = [ "2023" ] ;
286
293
287
294
/* istanbul ignore if */
@@ -361,7 +368,7 @@ function parse(source, root, options) {
361
368
break ;
362
369
363
370
case "required" :
364
- if ( edition )
371
+ if ( ! isProto2 )
365
372
throw illegal ( token ) ;
366
373
/* eslint-disable no-fallthrough */
367
374
case "repeated" :
@@ -372,7 +379,7 @@ function parse(source, root, options) {
372
379
/* istanbul ignore if */
373
380
if ( isProto3 ) {
374
381
parseField ( type , "proto3_optional" ) ;
375
- } else if ( edition ) {
382
+ } else if ( ! isProto2 ) {
376
383
throw illegal ( token ) ;
377
384
} else {
378
385
parseField ( type , "optional" ) ;
@@ -393,7 +400,7 @@ function parse(source, root, options) {
393
400
394
401
default :
395
402
/* istanbul ignore if */
396
- if ( ! isProto3 && ! edition || ! typeRefRe . test ( token ) ) {
403
+ if ( isProto2 || ! typeRefRe . test ( token ) ) {
397
404
throw illegal ( token ) ;
398
405
}
399
406
@@ -854,7 +861,7 @@ function parse(source, root, options) {
854
861
855
862
default :
856
863
/* istanbul ignore if */
857
- if ( ! isProto3 && ! edition || ! typeRefRe . test ( token ) )
864
+ if ( isProto2 || ! typeRefRe . test ( token ) )
858
865
throw illegal ( token ) ;
859
866
push ( token ) ;
860
867
parseField ( parent , "optional" , reference ) ;
@@ -924,7 +931,6 @@ function parse(source, root, options) {
924
931
"package" : pkg ,
925
932
"imports" : imports ,
926
933
weakImports : weakImports ,
927
- syntax : syntax ,
928
934
root : root
929
935
} ;
930
936
}
0 commit comments