Skip to content

Commit dd25371

Browse files
committed
NFC: Introduce peek(isAtAnyIn:)
1 parent 636a911 commit dd25371

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ extension Parser {
223223
)
224224
}
225225

226-
switch DeclarationAttributeWithSpecialSyntax(lexeme: self.peek()) {
226+
switch peek(isAtAnyIn: DeclarationAttributeWithSpecialSyntax.self) {
227227
case .available, ._spi_available:
228228
return parseAttribute(argumentMode: .required) { parser in
229229
return .availability(parser.parseAvailabilityArgumentSpecList())

Sources/SwiftParser/Statements.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,15 +943,13 @@ extension Parser.Lookahead {
943943
return false
944944
}
945945

946-
let next = peek()
947-
948946
// If 'then' is followed by a binary or postfix operator, prefer to parse as
949947
// an expr.
950-
if BinaryOperatorLike(lexeme: next) != nil || PostfixOperatorLike(lexeme: next) != nil {
948+
if peek(isAtAnyIn: BinaryOperatorLike.self) != nil || peek(isAtAnyIn: PostfixOperatorLike.self) != nil {
951949
return false
952950
}
953951

954-
switch PrepareForKeywordMatch(next) {
952+
switch PrepareForKeywordMatch(peek()) {
955953
case TokenSpec(.is), TokenSpec(.as):
956954
// Treat 'is' and 'as' like the binary operator case, and parse as an
957955
// expr.
@@ -965,7 +963,7 @@ extension Parser.Lookahead {
965963
// These are handled based on whether there is trivia between the 'then'
966964
// and the token. If so, it's a 'then' statement. Otherwise it should
967965
// be treated as an expression, e.g `then(...)`, `then[...]`, `then.foo`.
968-
return !self.currentToken.trailingTriviaText.isEmpty || !next.leadingTriviaText.isEmpty
966+
return !self.currentToken.trailingTriviaText.isEmpty || !peek().leadingTriviaText.isEmpty
969967
default:
970968
break
971969
}

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ extension TokenConsumer {
148148
return nil
149149
}
150150

151+
/// Checks whether the parser's next token is any token in `SpecSet`.
152+
/// If this is the case, return the corresponding `SpecSet` case.
153+
@inline(__always)
154+
func peek<SpecSet: TokenSpecSet>(isAtAnyIn specSet: SpecSet.Type) -> SpecSet? {
155+
guard let matchedKind = SpecSet(lexeme: self.peek()) else {
156+
return nil
157+
}
158+
precondition(matchedKind.spec ~= self.peek())
159+
return matchedKind
160+
}
161+
151162
/// Whether the current token’s text starts with the given prefix.
152163
@inline(__always)
153164
mutating func at(prefix: SyntaxText) -> Bool {

Sources/SwiftParser/Types.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ extension Parser.Lookahead {
798798
return true
799799
}
800800

801-
if EffectSpecifier(lexeme: self.peek()) != nil {
801+
if peek(isAtAnyIn: EffectSpecifier.self) != nil {
802802
var backtrack = self.lookahead()
803803
backtrack.consumeAnyToken()
804804
backtrack.consumeAnyToken()
@@ -909,9 +909,7 @@ extension Parser {
909909
}
910910

911911
mutating func parseTypeAttribute() -> RawAttributeListSyntax.Element {
912-
let typeAttr = TypeAttribute(lexeme: self.peek())
913-
914-
switch typeAttr {
912+
switch peek(isAtAnyIn: TypeAttribute.self) {
915913
case ._local, ._noMetadata, .async, .escaping, .noDerivative, .noescape, .Sendable, .unchecked, .autoclosure:
916914
// Known type attribute that doesn't take any arguments
917915
return parseAttributeWithoutArguments()

0 commit comments

Comments
 (0)