Skip to content

Commit f3ea90f

Browse files
committed
Replace where parameter in consume(if:) and friends by allowTokenAtStartOfLine
rdar://103774625
1 parent c5fa09c commit f3ea90f

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ extension Parser {
170170
case .required:
171171
shouldParseArgument = true
172172
case .customAttribute:
173-
shouldParseArgument = self.lookahead().isCustomAttributeArgument() && self.at(.leftParen, where: { !$0.isAtStartOfLine })
173+
shouldParseArgument = self.lookahead().isCustomAttributeArgument() && self.at(.leftParen, allowTokenAtStartOfLine: false)
174174
case .optional:
175175
shouldParseArgument = self.at(.leftParen)
176176
}
@@ -1157,7 +1157,7 @@ extension Parser.Lookahead {
11571157
return false
11581158
}
11591159

1160-
if self.at(.leftParen, where: { !$0.isAtStartOfLine }) && self.lookahead().isCustomAttributeArgument() {
1160+
if self.at(.leftParen, allowTokenAtStartOfLine: false) && self.lookahead().isCustomAttributeArgument() {
11611161
self.skipSingle()
11621162
}
11631163

Sources/SwiftParser/Declarations.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ extension Parser {
820820
let (unexpectedBeforeName, name) = self.expectIdentifier(allowIdentifierLikeKeywords: false, keywordRecovery: true)
821821

822822
let associatedValue: RawParameterClauseSyntax?
823-
if self.at(.leftParen, where: { !$0.isAtStartOfLine }) {
823+
if self.at(.leftParen, allowTokenAtStartOfLine: false) {
824824
associatedValue = self.parseParameterClause(for: .enumCase)
825825
} else {
826826
associatedValue = nil
@@ -1016,7 +1016,7 @@ extension Parser {
10161016
) -> RawDeinitializerDeclSyntax {
10171017
let (unexpectedBeforeDeinitKeyword, deinitKeyword) = self.eat(handle)
10181018
var unexpectedNameAndSignature: [RawSyntax?] = []
1019-
unexpectedNameAndSignature.append(self.consume(if: .identifier, where: { !$0.isAtStartOfLine }).map(RawSyntax.init))
1019+
unexpectedNameAndSignature.append(self.consume(if: .identifier, allowTokenAtStartOfLine: false).map(RawSyntax.init))
10201020
if self.at(.leftParen) && !self.currentToken.isAtStartOfLine {
10211021
unexpectedNameAndSignature.append(RawSyntax(parseFunctionSignature()))
10221022
}
@@ -1596,7 +1596,7 @@ extension Parser {
15961596
}
15971597

15981598
// diagnose 'throw'/'try'.
1599-
if let throwTry = self.consume(ifAny: [.keyword(.throw), .keyword(.try)], where: { !$0.isAtStartOfLine }) {
1599+
if let throwTry = self.consume(ifAny: [.keyword(.throw), .keyword(.try)], allowTokenAtStartOfLine: false) {
16001600
return throwTry
16011601
}
16021602

@@ -1853,7 +1853,7 @@ extension Parser {
18531853
case (_, let handle)?:
18541854
(unexpectedBeforeName, name) = self.eat(handle)
18551855
default:
1856-
if let identifier = self.consume(ifAny: [.identifier, .dollarIdentifier], where: { !$0.isAtStartOfLine }) {
1856+
if let identifier = self.consume(ifAny: [.identifier, .dollarIdentifier], allowTokenAtStartOfLine: false) {
18571857
// Recover if the developer tried to use an identifier as the operator name
18581858
unexpectedBeforeName = RawUnexpectedNodesSyntax([identifier], arena: self.arena)
18591859
} else {
@@ -2041,7 +2041,7 @@ extension Parser {
20412041
let (unexpectedBeforeColon, colon) = self.expect(.colon)
20422042
let (unexpectedBeforeFlag, flag) = self.expectAny([.keyword(.true), .keyword(.false)], default: .keyword(.true))
20432043
let unexpectedAfterFlag: RawUnexpectedNodesSyntax?
2044-
if flag.isMissing, let unexpectedIdentifier = self.consume(if: .identifier, where: { !$0.isAtStartOfLine }) {
2044+
if flag.isMissing, let unexpectedIdentifier = self.consume(if: .identifier, allowTokenAtStartOfLine: false) {
20452045
unexpectedAfterFlag = RawUnexpectedNodesSyntax([unexpectedIdentifier], arena: self.arena)
20462046
} else {
20472047
unexpectedAfterFlag = nil
@@ -2267,7 +2267,7 @@ extension Parser {
22672267
}
22682268

22692269
// Parse the optional parenthesized argument list.
2270-
let leftParen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine })
2270+
let leftParen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false)
22712271
let args: [RawTupleExprElementSyntax]
22722272
let unexpectedBeforeRightParen: RawUnexpectedNodesSyntax?
22732273
let rightParen: RawTokenSyntax?

Sources/SwiftParser/Expressions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ extension Parser {
725725
}
726726

727727
// If there is an expr-call-suffix, parse it and form a call.
728-
if let lparen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine }) {
728+
if let lparen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false) {
729729
let args = self.parseArgumentListElements(pattern: pattern)
730730
let (unexpectedBeforeRParen, rparen) = self.expect(.rightParen)
731731

@@ -756,7 +756,7 @@ extension Parser {
756756

757757
// Check for a [expr] suffix.
758758
// Note that this cannot be the start of a new line.
759-
if let lsquare = self.consume(if: .leftSquareBracket, where: { !$0.isAtStartOfLine }) {
759+
if let lsquare = self.consume(if: .leftSquareBracket, allowTokenAtStartOfLine: false) {
760760
let args: [RawTupleExprElementSyntax]
761761
if self.at(.rightSquareBracket) {
762762
args = []
@@ -997,7 +997,7 @@ extension Parser {
997997
while loopCondition.evaluate(currentToken) {
998998
// Check for a [] or .[] suffix. The latter is only permitted when there
999999
// are no components.
1000-
if self.at(.leftSquareBracket, where: { !$0.isAtStartOfLine })
1000+
if self.at(.leftSquareBracket, allowTokenAtStartOfLine: false)
10011001
|| (components.isEmpty && self.at(.period) && self.peek().rawTokenKind == .leftSquareBracket)
10021002
{
10031003
// Consume the '.', if it's allowed here.
@@ -1326,7 +1326,7 @@ extension Parser {
13261326
}
13271327

13281328
// Parse the optional parenthesized argument list.
1329-
let leftParen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine })
1329+
let leftParen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false)
13301330
let args: [RawTupleExprElementSyntax]
13311331
let unexpectedBeforeRightParen: RawUnexpectedNodesSyntax?
13321332
let rightParen: RawTokenSyntax?

Sources/SwiftParser/Names.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ extension Parser {
8585
}
8686

8787
// Is the current token a left paren?
88-
guard self.at(.leftParen, where: { !$0.isAtStartOfLine }) else {
88+
guard self.at(.leftParen, allowTokenAtStartOfLine: false) else {
8989
return nil
9090
}
9191

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ extension TokenConsumer {
5959
/// - Returns: `true` if the given `kind` matches the current token's kind.
6060
public func at(
6161
_ kind: RawTokenKind,
62-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
62+
allowTokenAtStartOfLine: Bool = true
6363
) -> Bool {
64-
return RawTokenKindMatch(kind) ~= self.currentToken && condition(self.currentToken)
64+
if !allowTokenAtStartOfLine && self.currentToken.isAtStartOfLine {
65+
return false
66+
}
67+
return RawTokenKindMatch(kind) ~= self.currentToken
6568
}
6669

6770
/// Returns whether the current token is an operator with the given `name`.
@@ -80,9 +83,12 @@ extension TokenConsumer {
8083
@_spi(RawSyntax)
8184
public func at(
8285
any kinds: [RawTokenKind],
83-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
86+
allowTokenAtStartOfLine: Bool = true
8487
) -> Bool {
85-
return kinds.contains(where: { RawTokenKindMatch($0) ~= self.currentToken }) && condition(self.currentToken)
88+
if !allowTokenAtStartOfLine && self.currentToken.isAtStartOfLine {
89+
return false
90+
}
91+
return kinds.contains(where: { RawTokenKindMatch($0) ~= self.currentToken })
8692
}
8793

8894
/// Checks whether the parser is currently positioned at any token in `Subset`.
@@ -139,9 +145,9 @@ extension TokenConsumer {
139145
public mutating func consume(
140146
if kind: RawTokenKind,
141147
remapping: RawTokenKind? = nil,
142-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
148+
allowTokenAtStartOfLine: Bool = true
143149
) -> Token? {
144-
if self.at(kind, where: condition) {
150+
if self.at(kind, allowTokenAtStartOfLine: allowTokenAtStartOfLine) {
145151
if let remapping = remapping {
146152
return self.consumeAnyToken(remapping: remapping)
147153
} else if case .keyword = kind {
@@ -173,9 +179,9 @@ extension TokenConsumer {
173179
@_spi(RawSyntax)
174180
public mutating func consume(
175181
ifAny kinds: [RawTokenKind],
176-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
182+
allowTokenAtStartOfLine: Bool = true
177183
) -> Token? {
178-
if self.at(any: kinds, where: condition) {
184+
if self.at(any: kinds, allowTokenAtStartOfLine: allowTokenAtStartOfLine) {
179185
return self.consumeAnyToken()
180186
}
181187
return nil
@@ -230,11 +236,8 @@ extension TokenConsumer {
230236
/// - Parameter condition: An additional condition that must be satisfied for
231237
/// the token to be consumed.
232238
/// - Returns: A token of the given kind.
233-
public mutating func expectWithoutRecovery(
234-
_ kind: RawTokenKind,
235-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
236-
) -> Token {
237-
if let token = self.consume(if: kind, where: condition) {
239+
public mutating func expectWithoutRecovery(_ kind: RawTokenKind) -> Token {
240+
if let token = self.consume(if: kind) {
238241
return token
239242
} else {
240243
return missingToken(kind, text: nil)

0 commit comments

Comments
 (0)