@@ -311,7 +311,8 @@ function parse(source, root, options) {
311311 }
312312
313313
314- function parseCommon ( parent , token ) {
314+ function parseCommon ( parent , token , depth ) {
315+ depth = util . checkDepth ( depth ) ;
315316 switch ( token ) {
316317
317318 case "option" :
@@ -320,7 +321,7 @@ function parse(source, root, options) {
320321 return true ;
321322
322323 case "message" :
323- parseType ( parent , token ) ;
324+ parseType ( parent , token , depth + 1 ) ;
324325 return true ;
325326
326327 case "enum" :
@@ -341,14 +342,14 @@ function parse(source, root, options) {
341342 }
342343 /* eslint-disable no-warning-comments */
343344 // TODO: actually enforce visiblity modifiers like protoc does.
344- return parseCommon ( parent , token ) ;
345+ return parseCommon ( parent , token , depth ) ;
345346
346347 case "service" :
347- parseService ( parent , token ) ;
348+ parseService ( parent , token , depth + 1 ) ;
348349 return true ;
349350
350351 case "extend" :
351- parseExtension ( parent , token ) ;
352+ parseExtension ( parent , token , depth ) ;
352353 return true ;
353354 }
354355 return false ;
@@ -376,15 +377,16 @@ function parse(source, root, options) {
376377 }
377378 }
378379
379- function parseType ( parent , token ) {
380+ function parseType ( parent , token , depth ) {
381+ depth = util . checkDepth ( depth ) ;
380382
381383 /* istanbul ignore if */
382384 if ( ! nameRe . test ( token = next ( ) ) )
383385 throw illegal ( token , "type name" ) ;
384386
385387 var type = new Type ( token ) ;
386388 ifBlock ( type , function parseType_block ( token ) {
387- if ( parseCommon ( type , token ) )
389+ if ( parseCommon ( type , token , depth ) )
388390 return ;
389391
390392 switch ( token ) {
@@ -401,22 +403,22 @@ function parse(source, root, options) {
401403 throw illegal ( token ) ;
402404 /* eslint-disable no-fallthrough */
403405 case "repeated" :
404- parseField ( type , token ) ;
406+ parseField ( type , token , undefined , depth + 1 ) ;
405407 break ;
406408
407409 case "optional" :
408410 /* istanbul ignore if */
409411 if ( edition === "proto3" ) {
410- parseField ( type , "proto3_optional" ) ;
412+ parseField ( type , "proto3_optional" , undefined , depth + 1 ) ;
411413 } else if ( edition !== "proto2" ) {
412414 throw illegal ( token ) ;
413415 } else {
414- parseField ( type , "optional" ) ;
416+ parseField ( type , "optional" , undefined , depth + 1 ) ;
415417 }
416418 break ;
417419
418420 case "oneof" :
419- parseOneOf ( type , token ) ;
421+ parseOneOf ( type , token , depth + 1 ) ;
420422 break ;
421423
422424 case "extensions" :
@@ -434,7 +436,7 @@ function parse(source, root, options) {
434436 }
435437
436438 push ( token ) ;
437- parseField ( type , "optional" ) ;
439+ parseField ( type , "optional" , undefined , depth + 1 ) ;
438440 break ;
439441 }
440442 } ) ;
@@ -444,10 +446,10 @@ function parse(source, root, options) {
444446 }
445447 }
446448
447- function parseField ( parent , rule , extend ) {
449+ function parseField ( parent , rule , extend , depth ) {
448450 var type = next ( ) ;
449451 if ( type === "group" ) {
450- parseGroup ( parent , rule , extend ) ;
452+ parseGroup ( parent , rule , extend , depth ) ;
451453 return ;
452454 }
453455 // Type names can consume multiple tokens, in multiple variants:
@@ -504,7 +506,8 @@ function parse(source, root, options) {
504506 }
505507 }
506508
507- function parseGroup ( parent , rule , extend ) {
509+ function parseGroup ( parent , rule , extend , depth ) {
510+ depth = util . checkDepth ( depth ) ;
508511 if ( edition >= 2023 ) {
509512 throw illegal ( "group" ) ;
510513 }
@@ -535,20 +538,20 @@ function parse(source, root, options) {
535538 break ;
536539 case "required" :
537540 case "repeated" :
538- parseField ( type , token ) ;
541+ parseField ( type , token , undefined , depth + 1 ) ;
539542 break ;
540543
541544 case "optional" :
542545 /* istanbul ignore if */
543546 if ( edition === "proto3" ) {
544- parseField ( type , "proto3_optional" ) ;
547+ parseField ( type , "proto3_optional" , undefined , depth + 1 ) ;
545548 } else {
546- parseField ( type , "optional" ) ;
549+ parseField ( type , "optional" , undefined , depth + 1 ) ;
547550 }
548551 break ;
549552
550553 case "message" :
551- parseType ( type , token ) ;
554+ parseType ( type , token , depth + 1 ) ;
552555 break ;
553556
554557 case "enum" :
@@ -567,10 +570,10 @@ function parse(source, root, options) {
567570 token = next ( ) ;
568571 switch ( token ) {
569572 case "message" :
570- parseType ( type , token ) ;
573+ parseType ( type , token , depth + 1 ) ;
571574 break ;
572575 case "enum" :
573- parseType ( type , token ) ;
576+ parseType ( type , token , depth + 1 ) ;
574577 break ;
575578 default :
576579 throw illegal ( token ) ;
@@ -629,7 +632,7 @@ function parse(source, root, options) {
629632 parent . add ( field ) ;
630633 }
631634
632- function parseOneOf ( parent , token ) {
635+ function parseOneOf ( parent , token , depth ) {
633636
634637 /* istanbul ignore if */
635638 if ( ! nameRe . test ( token = next ( ) ) )
@@ -642,7 +645,7 @@ function parse(source, root, options) {
642645 skip ( ";" ) ;
643646 } else {
644647 push ( token ) ;
645- parseField ( oneof , "optional" ) ;
648+ parseField ( oneof , "optional" , undefined , depth ) ;
646649 }
647650 } ) ;
648651 parent . add ( oneof ) ;
@@ -750,7 +753,8 @@ function parse(source, root, options) {
750753 setParsedOption ( parent , option , optionValue , propName ) ;
751754 }
752755
753- function parseOptionValue ( parent , name ) {
756+ function parseOptionValue ( parent , name , depth ) {
757+ depth = util . checkDepth ( depth ) ;
754758 // { a: "foo" b { c: "bar" } }
755759 if ( skip ( "{" , true ) ) {
756760 var objectResult = { } ;
@@ -773,7 +777,7 @@ function parse(source, root, options) {
773777 // option (my_option) = {
774778 // repeated_value: [ "foo", "bar" ]
775779 // };
776- value = parseOptionValue ( parent , name + "." + token ) ;
780+ value = parseOptionValue ( parent , name + "." + token , depth + 1 ) ;
777781 } else if ( peek ( ) === "[" ) {
778782 value = [ ] ;
779783 var lastValue ;
@@ -840,15 +844,16 @@ function parse(source, root, options) {
840844 return parent ;
841845 }
842846
843- function parseService ( parent , token ) {
847+ function parseService ( parent , token , depth ) {
848+ depth = util . checkDepth ( depth ) ;
844849
845850 /* istanbul ignore if */
846851 if ( ! nameRe . test ( token = next ( ) ) )
847852 throw illegal ( token , "service name" ) ;
848853
849854 var service = new Service ( token ) ;
850855 ifBlock ( service , function parseService_block ( token ) {
851- if ( parseCommon ( service , token ) ) {
856+ if ( parseCommon ( service , token , depth ) ) {
852857 return ;
853858 }
854859
@@ -918,7 +923,7 @@ function parse(source, root, options) {
918923 parent . add ( method ) ;
919924 }
920925
921- function parseExtension ( parent , token ) {
926+ function parseExtension ( parent , token , depth ) {
922927
923928 /* istanbul ignore if */
924929 if ( ! typeRefRe . test ( token = next ( ) ) )
@@ -930,15 +935,15 @@ function parse(source, root, options) {
930935
931936 case "required" :
932937 case "repeated" :
933- parseField ( parent , token , reference ) ;
938+ parseField ( parent , token , reference , depth + 1 ) ;
934939 break ;
935940
936941 case "optional" :
937942 /* istanbul ignore if */
938943 if ( edition === "proto3" ) {
939- parseField ( parent , "proto3_optional" , reference ) ;
944+ parseField ( parent , "proto3_optional" , reference , depth + 1 ) ;
940945 } else {
941- parseField ( parent , "optional" , reference ) ;
946+ parseField ( parent , "optional" , reference , depth + 1 ) ;
942947 }
943948 break ;
944949
@@ -947,7 +952,7 @@ function parse(source, root, options) {
947952 if ( edition === "proto2" || ! typeRefRe . test ( token ) )
948953 throw illegal ( token ) ;
949954 push ( token ) ;
950- parseField ( parent , "optional" , reference ) ;
955+ parseField ( parent , "optional" , reference , depth + 1 ) ;
951956 break ;
952957 }
953958 } ) ;
@@ -998,7 +1003,7 @@ function parse(source, root, options) {
9981003 default :
9991004
10001005 /* istanbul ignore else */
1001- if ( parseCommon ( ptr , token ) ) {
1006+ if ( parseCommon ( ptr , token , 0 ) ) {
10021007 head = false ;
10031008 continue ;
10041009 }
0 commit comments