@@ -560,19 +560,21 @@ extension Parser {
560560 /// Implements the paradigm shared across all `expect` methods.
561561 @inline ( __always)
562562 private mutating func expectImpl(
563+ skipUnexpectedModuleSelector: Bool = true ,
563564 consume: ( inout Parser ) -> RawTokenSyntax ? ,
564565 canRecoverTo: ( inout Lookahead ) -> RecoveryConsumptionHandle ? ,
565566 makeMissing: ( inout Parser ) -> RawTokenSyntax
566567 ) -> ( unexpected: RawUnexpectedNodesSyntax ? , token: RawTokenSyntax ) {
568+ let unexpectedSelector = skipUnexpectedModuleSelector ? unexpected ( self . parseModuleSelector ( ) ) : nil
567569 if let tok = consume ( & self ) {
568- return ( nil , tok)
570+ return ( unexpectedSelector , tok)
569571 }
570572 var lookahead = self . lookahead ( )
571573 if let handle = canRecoverTo ( & lookahead) {
572574 let ( unexpectedTokens, token) = self . eat ( handle)
573- return ( unexpectedTokens, token)
575+ return ( RawUnexpectedNodesSyntax ( combining : unexpectedSelector , unexpectedTokens, arena : self . arena ) , token)
574576 }
575- return ( nil , makeMissing ( & self ) )
577+ return ( unexpectedSelector , makeMissing ( & self ) )
576578 }
577579
578580 /// Attempts to consume a token that matches the given `spec`.
@@ -581,6 +583,8 @@ extension Parser {
581583 /// specified by `spec` and see if the token occurs after that unexpected.
582584 /// 2. If the token couldn't be found after skipping unexpected, it synthesizes
583585 /// 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.
584588 @inline ( __always)
585589 mutating func expect(
586590 _ spec: TokenSpec
@@ -599,6 +603,8 @@ extension Parser {
599603 /// kinds occurs after the unexpected.
600604 /// 2. If the token couldn't be found after skipping unexpected, it synthesizes
601605 /// 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.
602608 @inline ( __always)
603609 mutating func expect(
604610 _ spec1: TokenSpec ,
@@ -619,6 +625,8 @@ extension Parser {
619625 /// kinds occurs after the unexpected.
620626 /// 2. If the token couldn't be found after skipping unexpected, it synthesizes
621627 /// 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.
622630 @inline ( __always)
623631 mutating func expect(
624632 _ spec1: TokenSpec ,
@@ -633,6 +641,15 @@ extension Parser {
633641 )
634642 }
635643
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.
636653 @inline ( __always)
637654 mutating func expect< SpecSet: TokenSpecSet > (
638655 anyIn specSet: SpecSet . Type ,
@@ -652,12 +669,16 @@ extension Parser {
652669 /// specified by `TokenSpec(tokenKind)` and see if the token occurs after that unexpected.
653670 /// 2. If the token couldn't be found after skipping unexpected, it synthesizes
654671 /// 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).
655674 @inline ( __always)
656675 mutating func expect(
657676 prefix: SyntaxText , as tokenKind: RawTokenKind
658677 ) -> ( unexpected: RawUnexpectedNodesSyntax ? , token: RawTokenSyntax ) {
659678 let spec = TokenSpec ( tokenKind)
660679 return expectImpl (
680+ // Don't consume a .colonColon if we're trying to split it
681+ skipUnexpectedModuleSelector: !prefix. hasPrefix ( " : " ) ,
661682 consume: { $0. consume ( ifPrefix: prefix, as: tokenKind) } ,
662683 canRecoverTo: { $0. canRecoverTo ( spec) } ,
663684 makeMissing: { $0. missingToken ( spec) }
@@ -691,34 +712,38 @@ extension Parser {
691712 /// to and identifier.
692713 /// - Returns: The consumed token and any unexpected tokens that were skipped.
693714 /// 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.
694717 mutating func expectIdentifier(
695718 keywordRecovery: Bool = false ,
696719 allowSelfOrCapitalSelfAsIdentifier: Bool = false ,
697720 allowKeywordsAsIdentifier: Bool = false
698721 ) -> ( RawUnexpectedNodesSyntax ? , RawTokenSyntax ) {
722+ let unexpectedSelector = unexpected ( self . parseModuleSelector ( ) )
723+
699724 if let identifier = self . consume ( if: . identifier) {
700- return ( nil , identifier)
725+ return ( unexpectedSelector , identifier)
701726 }
702727 if allowKeywordsAsIdentifier, self . currentToken. isLexerClassifiedKeyword {
703- return ( nil , self . consumeAnyToken ( remapping: . identifier) )
728+ return ( unexpectedSelector , self . consumeAnyToken ( remapping: . identifier) )
704729 }
705730 if allowSelfOrCapitalSelfAsIdentifier,
706731 let selfOrCapitalSelf = self . consume (
707732 if: TokenSpec ( . self , remapping: . identifier) ,
708733 TokenSpec ( . Self, remapping: . identifier)
709734 )
710735 {
711- return ( nil , selfOrCapitalSelf)
736+ return ( unexpectedSelector , selfOrCapitalSelf)
712737 }
713738 if let unknown = self . consume ( if: . unknown) {
714739 return (
715- RawUnexpectedNodesSyntax ( [ unknown] , arena: self . arena) ,
740+ RawUnexpectedNodesSyntax ( combining : unexpectedSelector , unknown, arena: self . arena) ,
716741 self . missingToken ( . identifier)
717742 )
718743 }
719744 if let number = self . consume ( if: . integerLiteral, . floatLiteral, . dollarIdentifier) {
720745 return (
721- RawUnexpectedNodesSyntax ( [ number] , arena: self . arena) ,
746+ RawUnexpectedNodesSyntax ( combining : unexpectedSelector , number, arena: self . arena) ,
722747 self . missingToken ( . identifier)
723748 )
724749 } else if keywordRecovery,
@@ -727,12 +752,12 @@ extension Parser {
727752 {
728753 let keyword = self . consumeAnyToken ( )
729754 return (
730- RawUnexpectedNodesSyntax ( [ keyword] , arena: self . arena) ,
755+ RawUnexpectedNodesSyntax ( combining : unexpectedSelector , keyword, arena: self . arena) ,
731756 self . missingToken ( . identifier)
732757 )
733758 }
734759 return (
735- nil ,
760+ unexpectedSelector ,
736761 self . missingToken ( . identifier)
737762 )
738763 }
0 commit comments