@@ -816,33 +816,20 @@ extension Lexer.Cursor {
816
816
817
817
// Otherwise try lex a magic pound literal.
818
818
return self . lexOperatorIdentifier ( sourceBufferStart: sourceBufferStart)
819
- case UInt8 ( ascii: " ! " ) :
820
- if self . isLeftBound ( sourceBufferStart: sourceBufferStart) {
821
- _ = self . advance ( )
822
- return Lexer . Result ( . exclamationMark)
823
- }
824
- return self . lexOperatorIdentifier ( sourceBufferStart: sourceBufferStart)
825
-
826
- case UInt8 ( ascii: " ? " ) :
827
- if self . isLeftBound ( sourceBufferStart: sourceBufferStart) {
828
- _ = self . advance ( )
829
- return Lexer . Result ( . postfixQuestionMark)
819
+ case UInt8 ( ascii: " ! " ) , UInt8 ( ascii: " ? " ) :
820
+ if let result = lexPostfixOptionalChain ( sourceBufferStart: sourceBufferStart) {
821
+ return result
830
822
}
831
823
return self . lexOperatorIdentifier ( sourceBufferStart: sourceBufferStart)
832
824
833
825
case UInt8 ( ascii: " < " ) :
834
- if self . is ( offset: 1 , at: " # " ) {
835
- return self . tryLexEditorPlaceholder ( sourceBufferStart: sourceBufferStart)
826
+ if self . is ( offset: 1 , at: " # " ) ,
827
+ let result = self . tryLexEditorPlaceholder ( sourceBufferStart: sourceBufferStart)
828
+ {
829
+ return result
836
830
}
837
831
return self . lexOperatorIdentifier ( sourceBufferStart: sourceBufferStart)
838
- case UInt8 ( ascii: " > " ) :
839
- return self . lexOperatorIdentifier ( sourceBufferStart: sourceBufferStart)
840
832
841
- case UInt8 ( ascii: " = " ) , UInt8 ( ascii: " - " ) , UInt8 ( ascii: " + " ) ,
842
- UInt8 ( ascii: " * " ) , UInt8 ( ascii: " % " ) , UInt8 ( ascii: " & " ) ,
843
- UInt8 ( ascii: " | " ) , UInt8 ( ascii: " ^ " ) , UInt8 ( ascii: " ~ " ) ,
844
- UInt8 ( ascii: " . " ) :
845
- return self . lexOperatorIdentifier ( sourceBufferStart: sourceBufferStart)
846
833
case UInt8 ( ascii: " A " ) , UInt8 ( ascii: " B " ) , UInt8 ( ascii: " C " ) ,
847
834
UInt8 ( ascii: " D " ) , UInt8 ( ascii: " E " ) , UInt8 ( ascii: " F " ) ,
848
835
UInt8 ( ascii: " G " ) , UInt8 ( ascii: " H " ) , UInt8 ( ascii: " I " ) ,
@@ -1924,6 +1911,25 @@ extension Lexer.Cursor {
1924
1911
return Lexer . Result ( . backtick)
1925
1912
}
1926
1913
1914
+ /// Attempt to lex a postfix '!' or '?'.
1915
+ mutating func lexPostfixOptionalChain( sourceBufferStart: Lexer . Cursor ) -> Lexer . Result ? {
1916
+ // Must be left bound, otherwise this isn't postfix.
1917
+ guard self . isLeftBound ( sourceBufferStart: sourceBufferStart) else { return nil }
1918
+
1919
+ let kind : RawTokenKind = {
1920
+ switch self . peek ( ) {
1921
+ case UInt8 ( ascii: " ! " ) :
1922
+ return . exclamationMark
1923
+ case UInt8 ( ascii: " ? " ) :
1924
+ return . postfixQuestionMark
1925
+ default :
1926
+ fatalError ( " Must be at '!' or '?' " )
1927
+ }
1928
+ } ( )
1929
+ _ = self . advance ( )
1930
+ return Lexer . Result ( kind)
1931
+ }
1932
+
1927
1933
mutating func lexOperatorIdentifier( sourceBufferStart: Lexer . Cursor ) -> Lexer . Result {
1928
1934
let tokStart = self
1929
1935
let didStart = self . advance ( if: { $0. isOperatorStartCodePoint } )
@@ -2065,7 +2071,7 @@ extension Lexer.Cursor {
2065
2071
// MARK: - Editor Placeholders
2066
2072
2067
2073
extension Lexer . Cursor {
2068
- mutating func tryLexEditorPlaceholder( sourceBufferStart: Lexer . Cursor ) -> Lexer . Result {
2074
+ mutating func tryLexEditorPlaceholder( sourceBufferStart: Lexer . Cursor ) -> Lexer . Result ? {
2069
2075
precondition ( self . is ( at: " < " ) && self . is ( offset: 1 , at: " # " ) )
2070
2076
let start = self
2071
2077
var ptr = self
@@ -2092,7 +2098,7 @@ extension Lexer.Cursor {
2092
2098
}
2093
2099
2094
2100
// Not a well-formed placeholder.
2095
- return self . lexOperatorIdentifier ( sourceBufferStart : sourceBufferStart )
2101
+ return nil
2096
2102
}
2097
2103
}
2098
2104
0 commit comments