@@ -226,32 +226,21 @@ extension Parser {
226
226
}
227
227
228
228
switch recoveryResult {
229
+ case ( . group( let introducer) , let handle) ? :
230
+ let ( header, shouldContinueParsing) = parseHeaderForDeclarationGroup (
231
+ attrs: attrs,
232
+ introducer: introducer,
233
+ introducerHandle: handle
234
+ )
235
+ return parseDeclarationGroup ( for: header, shouldParseMemberBlock: shouldContinueParsing)
229
236
case ( . simple( . import) , let handle) ? :
230
237
return RawDeclSyntax ( self . parseImportDeclaration ( attrs, handle) )
231
- case ( . group( . class) , let handle) ? :
232
- return RawDeclSyntax (
233
- self . parseNominalTypeDeclaration ( for: RawClassDeclSyntax . self, attrs: attrs, introucerHandle: handle)
234
- )
235
- case ( . group( . enum) , let handle) ? :
236
- return RawDeclSyntax (
237
- self . parseNominalTypeDeclaration ( for: RawEnumDeclSyntax . self, attrs: attrs, introucerHandle: handle)
238
- )
239
238
case ( . simple( . case) , let handle) ? :
240
239
return RawDeclSyntax ( self . parseEnumCaseDeclaration ( attrs, handle) )
241
- case ( . group( . struct) , let handle) ? :
242
- return RawDeclSyntax (
243
- self . parseNominalTypeDeclaration ( for: RawStructDeclSyntax . self, attrs: attrs, introucerHandle: handle)
244
- )
245
- case ( . group( . protocol) , let handle) ? :
246
- return RawDeclSyntax (
247
- self . parseNominalTypeDeclaration ( for: RawProtocolDeclSyntax . self, attrs: attrs, introucerHandle: handle)
248
- )
249
240
case ( . simple( . associatedtype) , let handle) ? :
250
241
return RawDeclSyntax ( self . parseAssociatedTypeDeclaration ( attrs, handle) )
251
242
case ( . simple( . typealias) , let handle) ? :
252
243
return RawDeclSyntax ( self . parseTypealiasDeclaration ( attrs, handle) )
253
- case ( . group( . extension) , let handle) ? :
254
- return RawDeclSyntax ( self . parseExtensionDeclaration ( attrs, handle) )
255
244
case ( . simple( . func) , let handle) ? :
256
245
return RawDeclSyntax ( self . parseFuncDeclaration ( attrs, handle) )
257
246
case ( . simple( . subscript) , let handle) ? :
@@ -264,10 +253,6 @@ extension Parser {
264
253
return RawDeclSyntax ( self . parseOperatorDeclaration ( attrs, handle) )
265
254
case ( . simple( . precedencegroup) , let handle) ? :
266
255
return RawDeclSyntax ( self . parsePrecedenceGroupDeclaration ( attrs, handle) )
267
- case ( . group( . actor ) , let handle) ? :
268
- return RawDeclSyntax (
269
- self . parseNominalTypeDeclaration ( for: RawActorDeclSyntax . self, attrs: attrs, introucerHandle: handle)
270
- )
271
256
case ( . simple( . macro) , let handle) ? :
272
257
return RawDeclSyntax ( self . parseMacroDeclaration ( attrs: attrs, introducerHandle: handle) )
273
258
case ( . simple( . pound) , let handle) ? :
@@ -418,7 +403,56 @@ extension Parser {
418
403
introducer: DeclGroupHeaderSyntax . IntroducerOptions ,
419
404
introducerHandle: RecoveryConsumptionHandle
420
405
) -> ( RawDeclGroupHeaderSyntax , shouldContinueParsing: Bool ) {
421
- fatalError ( " not yet implemented " )
406
+ func eraseToRawDeclGroupHeaderSyntax(
407
+ _ result: ( some RawDeclGroupHeaderSyntaxNodeProtocol , Bool )
408
+ ) -> ( RawDeclGroupHeaderSyntax , shouldContinueParsing: Bool ) {
409
+ return ( RawDeclGroupHeaderSyntax ( result. 0 ) , result. 1 )
410
+ }
411
+
412
+ switch introducer {
413
+ case . class:
414
+ return eraseToRawDeclGroupHeaderSyntax (
415
+ self . parseNominalTypeDeclarationHeader (
416
+ for: RawClassDeclHeaderSyntax . self,
417
+ attrs: attrs,
418
+ introducerHandle: introducerHandle
419
+ )
420
+ )
421
+ case . enum:
422
+ return eraseToRawDeclGroupHeaderSyntax (
423
+ self . parseNominalTypeDeclarationHeader (
424
+ for: RawEnumDeclHeaderSyntax . self,
425
+ attrs: attrs,
426
+ introducerHandle: introducerHandle
427
+ )
428
+ )
429
+ case . struct:
430
+ return eraseToRawDeclGroupHeaderSyntax (
431
+ self . parseNominalTypeDeclarationHeader (
432
+ for: RawStructDeclHeaderSyntax . self,
433
+ attrs: attrs,
434
+ introducerHandle: introducerHandle
435
+ )
436
+ )
437
+ case . protocol:
438
+ return eraseToRawDeclGroupHeaderSyntax (
439
+ self . parseNominalTypeDeclarationHeader (
440
+ for: RawProtocolDeclHeaderSyntax . self,
441
+ attrs: attrs,
442
+ introducerHandle: introducerHandle
443
+ )
444
+ )
445
+ case . extension:
446
+ return ( RawDeclGroupHeaderSyntax ( self . parseExtensionDeclarationHeader ( attrs, introducerHandle) ) , true )
447
+ case . actor :
448
+ return eraseToRawDeclGroupHeaderSyntax (
449
+ self . parseNominalTypeDeclarationHeader (
450
+ for: RawActorDeclHeaderSyntax . self,
451
+ attrs: attrs,
452
+ introducerHandle: introducerHandle
453
+ )
454
+ )
455
+ }
422
456
}
423
457
424
458
/// Returns `true` if it looks like the parser is positioned at a function declaration that’s missing the `func` keyword.
@@ -483,12 +517,121 @@ extension Parser {
483
517
}
484
518
}
485
519
520
+ protocol DeclarationGroupHeaderTrait {
521
+ var introducer : RawTokenSyntax { get }
522
+
523
+ associatedtype Declaration : RawDeclSyntaxNodeProtocol
524
+
525
+ func makeDeclaration(
526
+ memberBlock: RawMemberBlockSyntax ,
527
+ arena: __shared SyntaxArena
528
+ ) -> Declaration
529
+ }
530
+
531
+ extension RawExtensionDeclHeaderSyntax : DeclarationGroupHeaderTrait {
532
+ var introducer : RawTokenSyntax {
533
+ return self . extensionKeyword
534
+ }
535
+
536
+ func makeDeclaration( memberBlock: RawMemberBlockSyntax , arena: SyntaxArena ) -> RawExtensionDeclSyntax {
537
+ RawExtensionDeclSyntax ( extensionHeader: self , memberBlock: memberBlock, arena: arena)
538
+ }
539
+ }
540
+
541
+ extension RawMissingDeclHeaderSyntax : DeclarationGroupHeaderTrait {
542
+ var introducer : RawTokenSyntax {
543
+ return self . placeholder
544
+ }
545
+
546
+ func makeDeclaration( memberBlock: RawMemberBlockSyntax , arena: SyntaxArena ) -> RawMissingDeclSyntax {
547
+ RawMissingDeclSyntax (
548
+ self . unexpectedBeforeAttributes,
549
+ attributes: self . attributes,
550
+ self . unexpectedBetweenAttributesAndModifiers,
551
+ modifiers: self . modifiers,
552
+ self . unexpectedBetweenModifiersAndPlaceholder,
553
+ placeholder: self . placeholder,
554
+ RawUnexpectedNodesSyntax (
555
+ combining: [
556
+ self . unexpectedBetweenPlaceholderAndInheritanceClause,
557
+ RawUnexpectedNodesSyntax ( [ self . inheritanceClause] , arena: arena) ,
558
+ self . unexpectedBetweenInheritanceClauseAndGenericWhereClause,
559
+ RawUnexpectedNodesSyntax ( [ self . genericWhereClause] , arena: arena) ,
560
+ self . unexpectedAfterGenericWhereClause,
561
+ ] ,
562
+ RawUnexpectedNodesSyntax ( [ memberBlock] , arena: arena) !,
563
+ arena: arena
564
+ ) ,
565
+ arena: arena
566
+ )
567
+ }
568
+ }
569
+
570
+ extension RawDeclGroupHeaderSyntax : DeclarationGroupHeaderTrait {
571
+ private typealias ConcreteExistentialSelf = RawSyntaxNodeProtocol & DeclarationGroupHeaderTrait
572
+ private var asConcreteExistentialSelf : any ConcreteExistentialSelf {
573
+ let subtypes : [ any ConcreteExistentialSelf . Type ] = [
574
+ RawExtensionDeclHeaderSyntax . self,
575
+ RawProtocolDeclHeaderSyntax . self,
576
+ RawClassDeclHeaderSyntax . self,
577
+ RawActorDeclHeaderSyntax . self,
578
+ RawStructDeclHeaderSyntax . self,
579
+ RawEnumDeclHeaderSyntax . self,
580
+ RawMissingDeclHeaderSyntax . self,
581
+ ]
582
+
583
+ for subtype in subtypes {
584
+ if let result = subtype. init ( self ) {
585
+ return result
586
+ }
587
+ }
588
+
589
+ fatalError ( " Node \( self ) does not have a known subtype " )
590
+ }
591
+
592
+ func makeDeclaration( memberBlock: RawMemberBlockSyntax , arena: SyntaxArena ) -> RawDeclSyntax {
593
+ return RawDeclSyntax ( asConcreteExistentialSelf. makeDeclaration ( memberBlock: memberBlock, arena: arena) )
594
+ }
595
+
596
+ var introducer : RawTokenSyntax {
597
+ return asConcreteExistentialSelf. introducer
598
+ }
599
+ }
600
+
486
601
extension Parser {
602
+ /// Parse a declaration group (class, struct, enum, actor, protocol, extension) body given its header.
603
+ mutating func parseDeclarationGroup< T> (
604
+ for header: T ,
605
+ shouldParseMemberBlock: Bool = true
606
+ ) -> T . Declaration where T: DeclarationGroupHeaderTrait {
607
+ let memberBlock =
608
+ if shouldParseMemberBlock {
609
+ self . parseMemberBlock ( introducer: header. introducer)
610
+ } else {
611
+ RawMemberBlockSyntax (
612
+ leftBrace: missingToken ( . leftBrace) ,
613
+ members: RawMemberBlockItemListSyntax ( elements: [ ] , arena: self . arena) ,
614
+ rightBrace: missingToken ( . rightBrace) ,
615
+ arena: self . arena
616
+ )
617
+ }
618
+ return header. makeDeclaration ( memberBlock: memberBlock, arena: self . arena)
619
+ }
620
+
487
621
/// Parse an extension declaration.
488
622
mutating func parseExtensionDeclaration(
489
623
_ attrs: DeclAttributes ,
490
624
_ handle: RecoveryConsumptionHandle
491
625
) -> RawExtensionDeclSyntax {
626
+ let header = parseExtensionDeclarationHeader ( attrs, handle)
627
+ return parseDeclarationGroup ( for: header)
628
+ }
629
+
630
+ /// Parse the header of an extension declaration.
631
+ mutating func parseExtensionDeclarationHeader(
632
+ _ attrs: DeclAttributes ,
633
+ _ handle: RecoveryConsumptionHandle
634
+ ) -> RawExtensionDeclHeaderSyntax {
492
635
let ( unexpectedBeforeExtensionKeyword, extensionKeyword) = self . eat ( handle)
493
636
let type = self . parseType ( )
494
637
@@ -505,16 +648,14 @@ extension Parser {
505
648
} else {
506
649
whereClause = nil
507
650
}
508
- let memberBlock = self . parseMemberBlock ( introducer: extensionKeyword)
509
- return RawExtensionDeclSyntax (
651
+ return RawExtensionDeclHeaderSyntax (
510
652
attributes: attrs. attributes,
511
653
modifiers: attrs. modifiers,
512
654
unexpectedBeforeExtensionKeyword,
513
655
extensionKeyword: extensionKeyword,
514
656
extendedType: type,
515
657
inheritanceClause: inheritance,
516
658
genericWhereClause: whereClause,
517
- memberBlock: memberBlock,
518
659
arena: self . arena
519
660
)
520
661
}
0 commit comments