Skip to content

Commit 3ea29ea

Browse files
committed
Refactor operator lexing in lexNormal a bit
Many of these can fall through to the default case to lex the operator. Also factor out `lexPostfixOptionalChain` in preparation for a bit of custom state transition logic.
1 parent b177710 commit 3ea29ea

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

Sources/SwiftParser/Lexer/Cursor.swift

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -816,33 +816,20 @@ extension Lexer.Cursor {
816816

817817
// Otherwise try lex a magic pound literal.
818818
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
830822
}
831823
return self.lexOperatorIdentifier(sourceBufferStart: sourceBufferStart)
832824

833825
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
836830
}
837831
return self.lexOperatorIdentifier(sourceBufferStart: sourceBufferStart)
838-
case UInt8(ascii: ">"):
839-
return self.lexOperatorIdentifier(sourceBufferStart: sourceBufferStart)
840832

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)
846833
case UInt8(ascii: "A"), UInt8(ascii: "B"), UInt8(ascii: "C"),
847834
UInt8(ascii: "D"), UInt8(ascii: "E"), UInt8(ascii: "F"),
848835
UInt8(ascii: "G"), UInt8(ascii: "H"), UInt8(ascii: "I"),
@@ -1924,6 +1911,25 @@ extension Lexer.Cursor {
19241911
return Lexer.Result(.backtick)
19251912
}
19261913

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+
19271933
mutating func lexOperatorIdentifier(sourceBufferStart: Lexer.Cursor) -> Lexer.Result {
19281934
let tokStart = self
19291935
let didStart = self.advance(if: { $0.isOperatorStartCodePoint })
@@ -2065,7 +2071,7 @@ extension Lexer.Cursor {
20652071
// MARK: - Editor Placeholders
20662072

20672073
extension Lexer.Cursor {
2068-
mutating func tryLexEditorPlaceholder(sourceBufferStart: Lexer.Cursor) -> Lexer.Result {
2074+
mutating func tryLexEditorPlaceholder(sourceBufferStart: Lexer.Cursor) -> Lexer.Result? {
20692075
precondition(self.is(at: "<") && self.is(offset: 1, at: "#"))
20702076
let start = self
20712077
var ptr = self
@@ -2092,7 +2098,7 @@ extension Lexer.Cursor {
20922098
}
20932099

20942100
// Not a well-formed placeholder.
2095-
return self.lexOperatorIdentifier(sourceBufferStart: sourceBufferStart)
2101+
return nil
20962102
}
20972103
}
20982104

0 commit comments

Comments
 (0)