Skip to content

Commit 07a4bac

Browse files
committed
Replace where parameter in consume(if:) and friends by allowTokenAtStartOfLine
rdar://103774625
1 parent 8e6ae6e commit 07a4bac

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
@@ -115,7 +115,7 @@ extension Parser {
115115
// Custom attributes are stricter than normal attributes about their
116116
// argument lists "immediately" following the attribute name.
117117
guard self.lookahead().isCustomAttributeArgument(),
118-
let leftParen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine })
118+
let leftParen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false)
119119
else {
120120
return RawCustomAttributeSyntax(
121121
atSignToken: atSign,
@@ -1028,7 +1028,7 @@ extension Parser.Lookahead {
10281028
return false
10291029
}
10301030

1031-
if self.at(.leftParen, where: { !$0.isAtStartOfLine }) && self.lookahead().isCustomAttributeArgument() {
1031+
if self.at(.leftParen, allowTokenAtStartOfLine: false) && self.lookahead().isCustomAttributeArgument() {
10321032
self.skipSingle()
10331033
}
10341034

Sources/SwiftParser/Declarations.swift

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

826826
let associatedValue: RawParameterClauseSyntax?
827-
if self.at(.leftParen, where: { !$0.isAtStartOfLine }) {
827+
if self.at(.leftParen, allowTokenAtStartOfLine: false) {
828828
associatedValue = self.parseParameterClause(for: .enumCase)
829829
} else {
830830
associatedValue = nil
@@ -1020,7 +1020,7 @@ extension Parser {
10201020
) -> RawDeinitializerDeclSyntax {
10211021
let (unexpectedBeforeDeinitKeyword, deinitKeyword) = self.eat(handle)
10221022
var unexpectedNameAndSignature: [RawSyntax?] = []
1023-
unexpectedNameAndSignature.append(self.consume(if: .identifier, where: { !$0.isAtStartOfLine }).map(RawSyntax.init))
1023+
unexpectedNameAndSignature.append(self.consume(if: .identifier, allowTokenAtStartOfLine: false).map(RawSyntax.init))
10241024
if self.at(.leftParen) && !self.currentToken.isAtStartOfLine {
10251025
unexpectedNameAndSignature.append(RawSyntax(parseFunctionSignature()))
10261026
}
@@ -1598,7 +1598,7 @@ extension Parser {
15981598
}
15991599

16001600
// diagnose 'throw'/'try'.
1601-
if let throwTry = self.consume(ifAny: [.throwKeyword, .tryKeyword], where: { !$0.isAtStartOfLine }) {
1601+
if let throwTry = self.consume(ifAny: [.throwKeyword, .tryKeyword], allowTokenAtStartOfLine: false) {
16021602
return throwTry
16031603
}
16041604

@@ -1840,7 +1840,7 @@ extension Parser {
18401840
case (_, let handle)?:
18411841
(unexpectedBeforeName, name) = self.eat(handle)
18421842
default:
1843-
if let identifier = self.consume(ifAny: [.identifier, .dollarIdentifier], where: { !$0.isAtStartOfLine }) {
1843+
if let identifier = self.consume(ifAny: [.identifier, .dollarIdentifier], allowTokenAtStartOfLine: false) {
18441844
// Recover if the developer tried to use an identifier as the operator name
18451845
unexpectedBeforeName = RawUnexpectedNodesSyntax([identifier], arena: self.arena)
18461846
} else {
@@ -2028,7 +2028,7 @@ extension Parser {
20282028
let (unexpectedBeforeColon, colon) = self.expect(.colon)
20292029
let (unexpectedBeforeFlag, flag) = self.expectAny([.trueKeyword, .falseKeyword], default: .trueKeyword)
20302030
let unexpectedAfterFlag: RawUnexpectedNodesSyntax?
2031-
if flag.isMissing, let unexpectedIdentifier = self.consume(if: .identifier, where: { !$0.isAtStartOfLine }) {
2031+
if flag.isMissing, let unexpectedIdentifier = self.consume(if: .identifier, allowTokenAtStartOfLine: false) {
20322032
unexpectedAfterFlag = RawUnexpectedNodesSyntax([unexpectedIdentifier], arena: self.arena)
20332033
} else {
20342034
unexpectedAfterFlag = nil
@@ -2254,7 +2254,7 @@ extension Parser {
22542254
}
22552255

22562256
// Parse the optional parenthesized argument list.
2257-
let leftParen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine })
2257+
let leftParen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false)
22582258
let args: [RawTupleExprElementSyntax]
22592259
let unexpectedBeforeRightParen: RawUnexpectedNodesSyntax?
22602260
let rightParen: RawTokenSyntax?

Sources/SwiftParser/Expressions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ extension Parser {
728728
}
729729

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

@@ -759,7 +759,7 @@ extension Parser {
759759

760760
// Check for a [expr] suffix.
761761
// Note that this cannot be the start of a new line.
762-
if let lsquare = self.consume(if: .leftSquareBracket, where: { !$0.isAtStartOfLine }) {
762+
if let lsquare = self.consume(if: .leftSquareBracket, allowTokenAtStartOfLine: false) {
763763
let args: [RawTupleExprElementSyntax]
764764
if self.at(.rightSquareBracket) {
765765
args = []
@@ -1000,7 +1000,7 @@ extension Parser {
10001000
while loopCondition.evaluate(currentToken) {
10011001
// Check for a [] or .[] suffix. The latter is only permitted when there
10021002
// are no components.
1003-
if self.at(.leftSquareBracket, where: { !$0.isAtStartOfLine })
1003+
if self.at(.leftSquareBracket, allowTokenAtStartOfLine: false)
10041004
|| (components.isEmpty && self.at(.period) && self.peek().rawTokenKind == .leftSquareBracket)
10051005
{
10061006
// Consume the '.', if it's allowed here.
@@ -1321,7 +1321,7 @@ extension Parser {
13211321
}
13221322

13231323
// Parse the optional parenthesized argument list.
1324-
let leftParen = self.consume(if: .leftParen, where: { !$0.isAtStartOfLine })
1324+
let leftParen = self.consume(if: .leftParen, allowTokenAtStartOfLine: false)
13251325
let args: [RawTupleExprElementSyntax]
13261326
let unexpectedBeforeRightParen: RawUnexpectedNodesSyntax?
13271327
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`.
@@ -81,9 +84,12 @@ extension TokenConsumer {
8184
@_spi(RawSyntax)
8285
public func at(
8386
any kinds: [RawTokenKind],
84-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
87+
allowTokenAtStartOfLine: Bool = true
8588
) -> Bool {
86-
return kinds.contains(where: { RawTokenKindMatch($0) ~= self.currentToken }) && condition(self.currentToken)
89+
if !allowTokenAtStartOfLine && self.currentToken.isAtStartOfLine {
90+
return false
91+
}
92+
return kinds.contains(where: { RawTokenKindMatch($0) ~= self.currentToken })
8793
}
8894

8995
/// Checks whether the parser is currently positioned at any token in `Subset`.
@@ -140,9 +146,9 @@ extension TokenConsumer {
140146
public mutating func consume(
141147
if kind: RawTokenKind,
142148
remapping: RawTokenKind? = nil,
143-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
149+
allowTokenAtStartOfLine: Bool = true
144150
) -> Token? {
145-
if self.at(kind, where: condition) {
151+
if self.at(kind, allowTokenAtStartOfLine: allowTokenAtStartOfLine) {
146152
if let remapping = remapping {
147153
return self.consumeAnyToken(remapping: remapping)
148154
} else if case .contextualKeyword = kind {
@@ -175,9 +181,9 @@ extension TokenConsumer {
175181
@_spi(RawSyntax)
176182
public mutating func consume(
177183
ifAny kinds: [RawTokenKind],
178-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
184+
allowTokenAtStartOfLine: Bool = true
179185
) -> Token? {
180-
if self.at(any: kinds, where: condition) {
186+
if self.at(any: kinds, allowTokenAtStartOfLine: allowTokenAtStartOfLine) {
181187
return self.consumeAnyToken()
182188
}
183189
return nil
@@ -232,11 +238,8 @@ extension TokenConsumer {
232238
/// - Parameter condition: An additional condition that must be satisfied for
233239
/// the token to be consumed.
234240
/// - Returns: A token of the given kind.
235-
public mutating func expectWithoutRecovery(
236-
_ kind: RawTokenKind,
237-
where condition: (Lexer.Lexeme) -> Bool = { _ in true }
238-
) -> Token {
239-
if let token = self.consume(if: kind, where: condition) {
241+
public mutating func expectWithoutRecovery(_ kind: RawTokenKind) -> Token {
242+
if let token = self.consume(if: kind) {
240243
return token
241244
} else {
242245
return missingToken(kind, text: nil)

0 commit comments

Comments
 (0)