Skip to content

Commit b3ddae7

Browse files
committed
Simplify checking if a token can be an argument label
1 parent 680942e commit b3ddae7

File tree

8 files changed

+31
-29
lines changed

8 files changed

+31
-29
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ extension Parser {
20832083
let unexpectedBeforeLabel: RawUnexpectedNodesSyntax?
20842084
let label: RawTokenSyntax?
20852085
let colon: RawTokenSyntax?
2086-
if currentToken.canBeArgumentLabel(allowDollarIdentifier: true) && self.peek().rawTokenKind == .colon {
2086+
if self.atArgumentLabel(allowDollarIdentifier: true) && self.peek().rawTokenKind == .colon {
20872087
(unexpectedBeforeLabel, label) = parseArgumentLabel()
20882088
colon = consumeAnyToken()
20892089
} else {
@@ -2168,7 +2168,7 @@ extension Parser.Lookahead {
21682168
// Fast path: the next two tokens must be a label and a colon.
21692169
// But 'default:' is ambiguous with switch cases and we disallow it
21702170
// (unless escaped) even outside of switches.
2171-
if !self.currentToken.canBeArgumentLabel()
2171+
if !self.atArgumentLabel()
21722172
|| self.at(.keyword(.default))
21732173
|| self.peek().rawTokenKind != .colon
21742174
{

Sources/SwiftParser/Names.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension Parser {
2424
}
2525

2626
mutating func parseArgumentLabel() -> (RawUnexpectedNodesSyntax?, RawTokenSyntax) {
27-
guard self.currentToken.canBeArgumentLabel(allowDollarIdentifier: true) else {
27+
guard self.atArgumentLabel(allowDollarIdentifier: true) else {
2828
return (nil, missingToken(.identifier))
2929
}
3030
if let dollarIdent = self.consume(if: .dollarIdentifier) {
@@ -98,7 +98,7 @@ extension Parser {
9898
// A close parenthesis, if empty lists are allowed.
9999
let nextIsRParen = flags.contains(.zeroArgCompoundNames) && next.rawTokenKind == .rightParen
100100
// An argument label.
101-
let nextIsArgLabel = next.canBeArgumentLabel() || next.rawTokenKind == .colon
101+
let nextIsArgLabel = next.isArgumentLabel() || next.rawTokenKind == .colon
102102

103103
guard nextIsRParen || nextIsArgLabel else {
104104
return nil
@@ -117,7 +117,7 @@ extension Parser {
117117
var loopProgress = LoopProgressCondition()
118118
while !self.at(.endOfFile, .rightParen) && loopProgress.evaluate(currentToken) {
119119
// Check to see if there is an argument label.
120-
precondition(self.currentToken.canBeArgumentLabel() && self.peek().rawTokenKind == .colon)
120+
precondition(self.atArgumentLabel() && self.peek().rawTokenKind == .colon)
121121
let name = self.consumeAnyToken()
122122
let (unexpectedBeforeColon, colon) = self.expect(.colon)
123123
elements.append(
@@ -242,7 +242,7 @@ extension Parser.Lookahead {
242242
var loopProgress = LoopProgressCondition()
243243
while !lookahead.at(.endOfFile, .rightParen) && loopProgress.evaluate(lookahead.currentToken) {
244244
// Check to see if there is an argument label.
245-
guard lookahead.currentToken.canBeArgumentLabel() && lookahead.peek().rawTokenKind == .colon else {
245+
guard lookahead.atArgumentLabel() && lookahead.peek().rawTokenKind == .colon else {
246246
return false
247247
}
248248

@@ -261,18 +261,16 @@ extension Parser.Lookahead {
261261
}
262262

263263
extension Lexer.Lexeme {
264-
func canBeArgumentLabel(allowDollarIdentifier: Bool = false) -> Bool {
265-
// `inout` is reserved as an argument label for historical reasons.
266-
if TypeSpecifier(lexeme: self) == .inout {
267-
return false
268-
}
269-
270-
switch self.rawTokenKind {
264+
func isArgumentLabel(allowDollarIdentifier: Bool = false) -> Bool {
265+
switch self {
271266
case .identifier, .wildcard:
272267
// Identifiers, escaped identifiers, and '_' can be argument labels.
273268
return true
274269
case .dollarIdentifier:
275270
return allowDollarIdentifier
271+
case .keyword(.inout):
272+
// `inout` cannot be an argument label for historical reasons.
273+
return false
276274
default:
277275
// All other keywords can be argument labels.
278276
return self.isLexerClassifiedKeyword

Sources/SwiftParser/Parameters.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ extension Parser {
6464
fileprivate mutating func parseParameterNames() -> ParameterNames {
6565
let unexpectedBeforeFirstName: RawUnexpectedNodesSyntax?
6666
let firstName: RawTokenSyntax?
67-
if self.currentToken.canBeArgumentLabel(allowDollarIdentifier: true) {
67+
if self.atArgumentLabel(allowDollarIdentifier: true) {
6868
(unexpectedBeforeFirstName, firstName) = self.parseArgumentLabel()
6969
} else {
7070
(unexpectedBeforeFirstName, firstName) = (nil, nil)
7171
}
7272

7373
let unexpectedBeforeSecondName: RawUnexpectedNodesSyntax?
7474
let secondName: RawTokenSyntax?
75-
if self.currentToken.canBeArgumentLabel(allowDollarIdentifier: true) {
75+
if self.atArgumentLabel(allowDollarIdentifier: true) {
7676
(unexpectedBeforeSecondName, secondName) = self.parseArgumentLabel()
7777
} else {
7878
(unexpectedBeforeSecondName, secondName) = (nil, nil)
@@ -280,7 +280,7 @@ extension Parser {
280280
// to be an argument label, don't parse any parameters.
281281
let shouldSkipParameterParsing =
282282
lparen.isMissing
283-
&& (!currentToken.canBeArgumentLabel(allowDollarIdentifier: true) || currentToken.isLexerClassifiedKeyword)
283+
&& (!self.atArgumentLabel(allowDollarIdentifier: true) || currentToken.isLexerClassifiedKeyword)
284284
if !shouldSkipParameterParsing {
285285
var keepGoing = true
286286
var loopProgress = LoopProgressCondition()

Sources/SwiftParser/Parser.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ public struct Parser {
9090
var arena: ParsingSyntaxArena
9191
/// A view of the sequence of lexemes in the input.
9292
var lexemes: Lexer.LexemeSequence
93-
/// The current token. If there was no input, this token will have a kind of `.endOfFile`.
93+
/// The current token that should be consumed next.
94+
///
95+
/// If the end of the source file is reached, this is `.endOfFile`.
96+
///
97+
/// - Important: You should almost never need to access this token directly
98+
/// in the parser. Instead, prefer using the `at` methods.
9499
var currentToken: Lexer.Lexeme
95100

96101
/// The current nesting level, i.e. the number of tokens that

Sources/SwiftParser/Patterns.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ extension Parser.Lookahead {
386386
}
387387

388388
// To have a parameter name here, we need a name.
389-
guard self.currentToken.canBeArgumentLabel(allowDollarIdentifier: true) else {
389+
guard self.atArgumentLabel(allowDollarIdentifier: true) else {
390390
return false
391391
}
392392

@@ -397,7 +397,7 @@ extension Parser.Lookahead {
397397
}
398398

399399
// If the next token can be an argument label, we might have a name.
400-
if nextTok.canBeArgumentLabel(allowDollarIdentifier: true) {
400+
if nextTok.isArgumentLabel(allowDollarIdentifier: true) {
401401
// If the first name wasn't a contextual keyword, we're done.
402402
if !self.at(.keyword(.isolated))
403403
&& !self.at(.keyword(.some))
@@ -420,7 +420,7 @@ extension Parser.Lookahead {
420420
return true // isolated :
421421
}
422422
self.consumeAnyToken()
423-
return self.currentToken.canBeArgumentLabel(allowDollarIdentifier: true) && self.peek().rawTokenKind == .colon
423+
return self.atArgumentLabel(allowDollarIdentifier: true) && self.peek().rawTokenKind == .colon
424424
}
425425
}
426426

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,9 @@ extension TokenConsumer {
302302
return false
303303
}
304304
}
305+
306+
/// Whether the current token can be a function argument label.
307+
func atArgumentLabel(allowDollarIdentifier: Bool = false) -> Bool {
308+
return self.currentToken.isArgumentLabel(allowDollarIdentifier: allowDollarIdentifier)
309+
}
305310
}

Sources/SwiftParser/TopLevel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension Parser {
2626
/// as unexpected nodes that have the `isMaximumNestingLevelOverflow` bit set.
2727
/// Check this in places that are likely to cause deep recursion and if this returns non-nil, abort parsing.
2828
mutating func remainingTokensIfMaximumNestingLevelReached() -> RawUnexpectedNodesSyntax? {
29-
if nestingLevel > self.maximumNestingLevel && self.currentToken.rawTokenKind != .endOfFile {
29+
if nestingLevel > self.maximumNestingLevel && !self.at(.endOfFile) {
3030
let remainingTokens = self.consumeRemainingTokens()
3131
return RawUnexpectedNodesSyntax(elements: remainingTokens, isMaximumNestingLevelOverflow: true, arena: self.arena)
3232
} else {

Sources/SwiftParser/Types.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ extension Parser {
508508
second = nil
509509
unexpectedBeforeColon = nil
510510
colon = parsedColon
511-
} else if self.currentToken.canBeArgumentLabel(allowDollarIdentifier: true) && self.peek().rawTokenKind == .colon {
511+
} else if self.atArgumentLabel(allowDollarIdentifier: true) && self.peek().rawTokenKind == .colon {
512512
(unexpectedBeforeSecond, second) = self.parseArgumentLabel()
513513
(unexpectedBeforeColon, colon) = self.expect(.colon)
514514
} else {
@@ -802,7 +802,7 @@ extension Parser.Lookahead {
802802
// by a type annotation.
803803
if self.startsParameterName(isClosure: false, allowMisplacedSpecifierRecovery: false) {
804804
self.consumeAnyToken()
805-
if self.currentToken.canBeArgumentLabel() {
805+
if self.atArgumentLabel() {
806806
self.consumeAnyToken()
807807
guard self.at(.colon) else {
808808
return false
@@ -1061,12 +1061,6 @@ extension Parser {
10611061
}
10621062

10631063
extension Lexer.Lexeme {
1064-
var isAnyOperator: Bool {
1065-
return self.rawTokenKind == .binaryOperator
1066-
|| self.rawTokenKind == .postfixOperator
1067-
|| self.rawTokenKind == .prefixOperator
1068-
}
1069-
10701064
var isGenericTypeDisambiguatingToken: Bool {
10711065
switch self.rawTokenKind {
10721066
case .rightParen,

0 commit comments

Comments
 (0)