@@ -560,19 +560,21 @@ extension Parser {
560
560
/// Implements the paradigm shared across all `expect` methods.
561
561
@inline ( __always)
562
562
private mutating func expectImpl(
563
+ skipUnexpectedModuleSelector: Bool = true ,
563
564
consume: ( inout Parser ) -> RawTokenSyntax ? ,
564
565
canRecoverTo: ( inout Lookahead ) -> RecoveryConsumptionHandle ? ,
565
566
makeMissing: ( inout Parser ) -> RawTokenSyntax
566
567
) -> ( unexpected: RawUnexpectedNodesSyntax ? , token: RawTokenSyntax ) {
568
+ let unexpectedSelector = skipUnexpectedModuleSelector ? unexpected ( self . parseModuleSelector ( ) ) : nil
567
569
if let tok = consume ( & self ) {
568
- return ( nil , tok)
570
+ return ( unexpectedSelector , tok)
569
571
}
570
572
var lookahead = self . lookahead ( )
571
573
if let handle = canRecoverTo ( & lookahead) {
572
574
let ( unexpectedTokens, token) = self . eat ( handle)
573
- return ( unexpectedTokens, token)
575
+ return ( RawUnexpectedNodesSyntax ( combining : unexpectedSelector , unexpectedTokens, arena : self . arena ) , token)
574
576
}
575
- return ( nil , makeMissing ( & self ) )
577
+ return ( unexpectedSelector , makeMissing ( & self ) )
576
578
}
577
579
578
580
/// Attempts to consume a token that matches the given `spec`.
@@ -581,6 +583,8 @@ extension Parser {
581
583
/// specified by `spec` and see if the token occurs after that unexpected.
582
584
/// 2. If the token couldn't be found after skipping unexpected, it synthesizes
583
585
/// a missing token of the requested kind.
586
+ /// - Note: If you expect a module selector, parse it before calling this method; it will consume module selectors
587
+ /// as unexpected syntax.
584
588
@inline ( __always)
585
589
mutating func expect(
586
590
_ spec: TokenSpec
@@ -599,6 +603,8 @@ extension Parser {
599
603
/// kinds occurs after the unexpected.
600
604
/// 2. If the token couldn't be found after skipping unexpected, it synthesizes
601
605
/// a missing token of `defaultKind`.
606
+ /// - Note: If you expect a module selector, parse it before calling this method; it will consume module selectors
607
+ /// as unexpected syntax.
602
608
@inline ( __always)
603
609
mutating func expect(
604
610
_ spec1: TokenSpec ,
@@ -619,6 +625,8 @@ extension Parser {
619
625
/// kinds occurs after the unexpected.
620
626
/// 2. If the token couldn't be found after skipping unexpected, it synthesizes
621
627
/// a missing token of `defaultKind`.
628
+ /// - Note: If you expect a module selector, parse it before calling this method; it will consume module selectors
629
+ /// as unexpected syntax.
622
630
@inline ( __always)
623
631
mutating func expect(
624
632
_ spec1: TokenSpec ,
@@ -633,6 +641,15 @@ extension Parser {
633
641
)
634
642
}
635
643
644
+ /// Attempts to consume a token that matches the given `specSet`.
645
+ /// If it cannot be found, the parser tries
646
+ /// 1. To eat unexpected tokens that have lower ``TokenPrecedence`` than the
647
+ /// lowest precedence of the spec and see if a token of the requested
648
+ /// kinds occurs after the unexpected.
649
+ /// 2. If the token couldn't be found after skipping unexpected, it synthesizes
650
+ /// a missing token of `defaultKind`.
651
+ /// - Note: If you expect a module selector, parse it before calling this method; it will consume module selectors
652
+ /// as unexpected syntax.
636
653
@inline ( __always)
637
654
mutating func expect< SpecSet: TokenSpecSet > (
638
655
anyIn specSet: SpecSet . Type ,
@@ -652,12 +669,16 @@ extension Parser {
652
669
/// specified by `TokenSpec(tokenKind)` and see if the token occurs after that unexpected.
653
670
/// 2. If the token couldn't be found after skipping unexpected, it synthesizes
654
671
/// a missing token of the requested kind.
672
+ /// - Note: If you expect a module selector, parse it before calling this method; it will consume module selectors
673
+ /// as unexpected syntax (except when used to split a colon).
655
674
@inline ( __always)
656
675
mutating func expect(
657
676
prefix: SyntaxText , as tokenKind: RawTokenKind
658
677
) -> ( unexpected: RawUnexpectedNodesSyntax ? , token: RawTokenSyntax ) {
659
678
let spec = TokenSpec ( tokenKind)
660
679
return expectImpl (
680
+ // Don't consume a .colonColon if we're trying to split it
681
+ skipUnexpectedModuleSelector: !prefix. hasPrefix ( " : " ) ,
661
682
consume: { $0. consume ( ifPrefix: prefix, as: tokenKind) } ,
662
683
canRecoverTo: { $0. canRecoverTo ( spec) } ,
663
684
makeMissing: { $0. missingToken ( spec) }
@@ -691,34 +712,38 @@ extension Parser {
691
712
/// to and identifier.
692
713
/// - Returns: The consumed token and any unexpected tokens that were skipped.
693
714
/// The token is always guaranteed to be of `TokenKind.identifier`
715
+ /// - Note: If you expect a module selector, parse it before calling this method; it will consume module selectors
716
+ /// as unexpected syntax.
694
717
mutating func expectIdentifier(
695
718
keywordRecovery: Bool = false ,
696
719
allowSelfOrCapitalSelfAsIdentifier: Bool = false ,
697
720
allowKeywordsAsIdentifier: Bool = false
698
721
) -> ( RawUnexpectedNodesSyntax ? , RawTokenSyntax ) {
722
+ let unexpectedSelector = unexpected ( self . parseModuleSelector ( ) )
723
+
699
724
if let identifier = self . consume ( if: . identifier) {
700
- return ( nil , identifier)
725
+ return ( unexpectedSelector , identifier)
701
726
}
702
727
if allowKeywordsAsIdentifier, self . currentToken. isLexerClassifiedKeyword {
703
- return ( nil , self . consumeAnyToken ( remapping: . identifier) )
728
+ return ( unexpectedSelector , self . consumeAnyToken ( remapping: . identifier) )
704
729
}
705
730
if allowSelfOrCapitalSelfAsIdentifier,
706
731
let selfOrCapitalSelf = self . consume (
707
732
if: TokenSpec ( . self , remapping: . identifier) ,
708
733
TokenSpec ( . Self, remapping: . identifier)
709
734
)
710
735
{
711
- return ( nil , selfOrCapitalSelf)
736
+ return ( unexpectedSelector , selfOrCapitalSelf)
712
737
}
713
738
if let unknown = self . consume ( if: . unknown) {
714
739
return (
715
- RawUnexpectedNodesSyntax ( [ unknown] , arena: self . arena) ,
740
+ RawUnexpectedNodesSyntax ( combining : unexpectedSelector , unknown, arena: self . arena) ,
716
741
self . missingToken ( . identifier)
717
742
)
718
743
}
719
744
if let number = self . consume ( if: . integerLiteral, . floatLiteral, . dollarIdentifier) {
720
745
return (
721
- RawUnexpectedNodesSyntax ( [ number] , arena: self . arena) ,
746
+ RawUnexpectedNodesSyntax ( combining : unexpectedSelector , number, arena: self . arena) ,
722
747
self . missingToken ( . identifier)
723
748
)
724
749
} else if keywordRecovery,
@@ -727,12 +752,12 @@ extension Parser {
727
752
{
728
753
let keyword = self . consumeAnyToken ( )
729
754
return (
730
- RawUnexpectedNodesSyntax ( [ keyword] , arena: self . arena) ,
755
+ RawUnexpectedNodesSyntax ( combining : unexpectedSelector , keyword, arena: self . arena) ,
731
756
self . missingToken ( . identifier)
732
757
)
733
758
}
734
759
return (
735
- nil ,
760
+ unexpectedSelector ,
736
761
self . missingToken ( . identifier)
737
762
)
738
763
}
0 commit comments