Skip to content

Commit ccddf47

Browse files
committed
Consider wildcard as Misc instead of a keyword
1 parent ab73c54 commit ccddf47

File tree

22 files changed

+70
-76
lines changed

22 files changed

+70
-76
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/TokenSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public class LiteralSpec: TokenSpec { }
204204
public class MiscSpec: TokenSpec { }
205205

206206
public let SYNTAX_TOKENS: [TokenSpec] = [
207-
PatternKeywordSpec(name: "Wildcard", text: "_", requiresTrailingSpace: true),
207+
MiscSpec(name: "Wildcard", kind: "_", nameForDiagnostics: "wildcard", text: "_"),
208208
PunctuatorSpec(name: "LeftParen", kind: "l_paren", text: "("),
209209
PunctuatorSpec(name: "RightParen", kind: "r_paren", text: ")"),
210210
PunctuatorSpec(name: "LeftBrace", kind: "l_brace", text: "{", requiresLeadingSpace: true),

Sources/IDEUtils/generated/SyntaxClassification.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ extension SyntaxClassification {
140140
extension RawTokenKind {
141141
internal var classification: SyntaxClassification {
142142
switch self {
143-
case .wildcardKeyword:
144-
return .keyword
143+
case .wildcard:
144+
return .none
145145
case .leftParen:
146146
return .none
147147
case .rightParen:

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ open class BasicFormat: SyntaxRewriter {
185185
break
186186
}
187187
switch token.tokenKind {
188-
case .wildcardKeyword:
189-
return true
190188
case .comma:
191189
return true
192190
case .colon:

Sources/SwiftParser/Availability.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ extension Parser {
220220
return .token(star)
221221
}
222222

223-
if self.at(any: [.identifier, .wildcardKeyword]) {
223+
if self.at(any: [.identifier, .wildcard]) {
224224
if self.at(.keyword(.swift)) || self.at(.keyword(._PackageDescription)) {
225225
return .availabilityVersionRestriction(self.parsePlatformAgnosticVersionConstraintSpec())
226226
}
@@ -230,7 +230,7 @@ extension Parser {
230230
}
231231

232232
mutating func parsePlatformAgnosticVersionConstraintSpec() -> RawAvailabilityVersionRestrictionSyntax {
233-
let (unexpectedBeforePlatform, platform) = self.expectAny([.identifier, .wildcardKeyword], default: .identifier)
233+
let (unexpectedBeforePlatform, platform) = self.expectAny([.identifier, .wildcard], default: .identifier)
234234
let version = self.parseVersionTuple()
235235
return RawAvailabilityVersionRestrictionSyntax(
236236
unexpectedBeforePlatform,

Sources/SwiftParser/Declarations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ extension Parser {
250250
return RawDeclSyntax(self.parseMacroDeclaration(attrs: attrs, introducerHandle: handle))
251251
case nil:
252252
if inMemberDeclList {
253-
let isProbablyVarDecl = self.at(any: [.identifier, .wildcardKeyword]) && self.peek().rawTokenKind.is(any: [.colon, .equal, .comma])
254-
let isProbablyTupleDecl = self.at(.leftParen) && self.peek().rawTokenKind.is(any: [.identifier, .wildcardKeyword])
253+
let isProbablyVarDecl = self.at(any: [.identifier, .wildcard]) && self.peek().rawTokenKind.is(any: [.colon, .equal, .comma])
254+
let isProbablyTupleDecl = self.at(.leftParen) && self.peek().rawTokenKind.is(any: [.identifier, .wildcard])
255255

256256
if isProbablyVarDecl || isProbablyTupleDecl {
257257
return RawDeclSyntax(self.parseLetOrVarDeclaration(attrs, .missing(.keyword(.var))))
258258
}
259259

260-
let isProbablyFuncDecl = self.at(any: [.identifier, .wildcardKeyword]) || self.at(anyIn: Operator.self) != nil
260+
let isProbablyFuncDecl = self.at(any: [.identifier, .wildcard]) || self.at(anyIn: Operator.self) != nil
261261

262262
if isProbablyFuncDecl {
263263
return RawDeclSyntax(self.parseFuncDeclaration(attrs, .missing(.keyword(.func))))

Sources/SwiftParser/Expressions.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ extension Parser {
12011201
return RawExprSyntax(RawTypeExprSyntax(type: anyType, arena: self.arena))
12021202
case (.dollarIdentifier, _)?:
12031203
return RawExprSyntax(self.parseAnonymousClosureArgument())
1204-
case (.wildcardKeyword, let handle)?: // _
1204+
case (.wildcard, let handle)?: // _
12051205
let wild = self.eat(handle)
12061206
return RawExprSyntax(
12071207
RawDiscardAssignmentExprSyntax(
@@ -2174,7 +2174,7 @@ extension Parser {
21742174
// If we have a leading token that may be part of the closure signature, do a
21752175
// speculative parse to validate it and look for 'in'.
21762176
guard
2177-
self.at(any: [.atSign, .leftParen, .leftSquareBracket, .wildcardKeyword])
2177+
self.at(any: [.atSign, .leftParen, .leftSquareBracket, .wildcard])
21782178
|| self.at(.identifier)
21792179
else {
21802180
// No closure signature.
@@ -2276,7 +2276,7 @@ extension Parser {
22762276
unexpected = nil
22772277
name = identifier
22782278
} else {
2279-
(unexpected, name) = self.expect(.wildcardKeyword)
2279+
(unexpected, name) = self.expect(.wildcard)
22802280
}
22812281
keepGoing = consume(if: .comma)
22822282
params.append(
@@ -2634,13 +2634,13 @@ extension Parser.Lookahead {
26342634
}
26352635
}
26362636
// Okay, we have a closure signature.
2637-
} else if lookahead.at(.identifier) || lookahead.at(.wildcardKeyword) {
2637+
} else if lookahead.at(.identifier) || lookahead.at(.wildcard) {
26382638
// Parse identifier (',' identifier)*
26392639
lookahead.consumeAnyToken()
26402640

26412641
var parametersProgress = LoopProgressCondition()
26422642
while lookahead.consume(if: .comma) != nil && parametersProgress.evaluate(lookahead.currentToken) {
2643-
if lookahead.at(.identifier) || lookahead.at(.wildcardKeyword) {
2643+
if lookahead.at(.identifier) || lookahead.at(.wildcard) {
26442644
lookahead.consumeAnyToken()
26452645
continue
26462646
}

Sources/SwiftParser/Lexer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,8 @@ extension Lexer.Cursor {
16691669
return Lexer.Result(keywordKind)
16701670
} else if let keyword = Keyword(text), keyword.isLexerClassified {
16711671
return Lexer.Result(.keyword(keyword))
1672+
} else if text == "_" {
1673+
return Lexer.Result(.wildcard)
16721674
} else {
16731675
return Lexer.Result(.identifier)
16741676
}

Sources/SwiftParser/Names.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension Parser {
3131
self.missingToken(.identifier, text: nil)
3232
)
3333
} else {
34-
if let wildcardToken = self.consume(if: .wildcardKeyword) {
34+
if let wildcardToken = self.consume(if: .wildcard) {
3535
return (nil, wildcardToken)
3636
}
3737
return (nil, self.consumeAnyToken(remapping: .identifier))
@@ -263,7 +263,7 @@ extension Lexer.Lexeme {
263263
return false
264264
}
265265
switch self.rawTokenKind {
266-
case .identifier, .wildcardKeyword:
266+
case .identifier, .wildcard:
267267
// Identifiers, escaped identifiers, and '_' can be argument labels.
268268
return true
269269
case .dollarIdentifier:

Sources/SwiftParser/Parser.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,10 @@ extension Parser {
394394
RawUnexpectedNodesSyntax(elements: [RawSyntax(number)], arena: self.arena),
395395
self.missingToken(.identifier, text: nil)
396396
)
397-
} else if keywordRecovery, self.currentToken.rawTokenKind.isLexerClassifiedKeyword, !self.currentToken.isAtStartOfLine {
397+
} else if keywordRecovery,
398+
(self.currentToken.rawTokenKind.isLexerClassifiedKeyword || self.currentToken.rawTokenKind == .wildcard),
399+
!self.currentToken.isAtStartOfLine
400+
{
398401
let keyword = self.consumeAnyToken()
399402
return (
400403
RawUnexpectedNodesSyntax(elements: [RawSyntax(keyword)], arena: self.arena),

Sources/SwiftParser/Patterns.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension Parser {
4848
public mutating func parsePattern() -> RawPatternSyntax {
4949
enum ExpectedTokens: RawTokenKindSubset {
5050
case leftParen
51-
case wildcardKeyword
51+
case wildcard
5252
case identifier
5353
case dollarIdentifier // For recovery
5454
case letKeyword
@@ -57,7 +57,7 @@ extension Parser {
5757
init?(lexeme: Lexer.Lexeme) {
5858
switch lexeme.rawTokenKind {
5959
case .leftParen: self = .leftParen
60-
case .wildcardKeyword: self = .wildcardKeyword
60+
case .wildcard: self = .wildcard
6161
case .identifier: self = .identifier
6262
case .dollarIdentifier: self = .dollarIdentifier
6363
case .keyword(.let): self = .letKeyword
@@ -69,7 +69,7 @@ extension Parser {
6969
var rawTokenKind: RawTokenKind {
7070
switch self {
7171
case .leftParen: return .leftParen
72-
case .wildcardKeyword: return .wildcardKeyword
72+
case .wildcard: return .wildcard
7373
case .identifier: return .identifier
7474
case .dollarIdentifier: return .dollarIdentifier
7575
case .letKeyword: return .keyword(.let)
@@ -92,7 +92,7 @@ extension Parser {
9292
arena: self.arena
9393
)
9494
)
95-
case (.wildcardKeyword, let handle)?:
95+
case (.wildcard, let handle)?:
9696
let wildcard = self.eat(handle)
9797
return RawPatternSyntax(
9898
RawWildcardPatternSyntax(
@@ -290,15 +290,15 @@ extension Parser.Lookahead {
290290
mutating func canParsePattern() -> Bool {
291291
enum PatternStartTokens: RawTokenKindSubset {
292292
case identifier
293-
case wildcardKeyword
293+
case wildcard
294294
case letKeyword
295295
case varKeyword
296296
case leftParen
297297

298298
init?(lexeme: Lexer.Lexeme) {
299299
switch lexeme.rawTokenKind {
300300
case .identifier: self = .identifier
301-
case .wildcardKeyword: self = .wildcardKeyword
301+
case .wildcard: self = .wildcard
302302
case .keyword(.let): self = .letKeyword
303303
case .keyword(.var): self = .varKeyword
304304
case .leftParen: self = .leftParen
@@ -309,7 +309,7 @@ extension Parser.Lookahead {
309309
var rawTokenKind: RawTokenKind {
310310
switch self {
311311
case .identifier: return .identifier
312-
case .wildcardKeyword: return .wildcardKeyword
312+
case .wildcard: return .wildcard
313313
case .letKeyword: return .keyword(.let)
314314
case .varKeyword: return .keyword(.var)
315315
case .leftParen: return .leftParen
@@ -319,7 +319,7 @@ extension Parser.Lookahead {
319319

320320
switch self.at(anyIn: PatternStartTokens.self) {
321321
case (.identifier, let handle)?,
322-
(.wildcardKeyword, let handle)?:
322+
(.wildcard, let handle)?:
323323
self.eat(handle)
324324
return true
325325
case (.letKeyword, let handle)?,

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
698698
case stringLiteral
699699
case superKeyword
700700
case trueKeyword
701-
case wildcardKeyword
701+
case wildcard
702702

703703
init?(lexeme: Lexer.Lexeme) {
704704
switch lexeme {
@@ -721,7 +721,7 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
721721
case RawTokenKindMatch(.stringLiteral): self = .stringLiteral
722722
case RawTokenKindMatch(.super): self = .superKeyword
723723
case RawTokenKindMatch(.true): self = .trueKeyword
724-
case RawTokenKindMatch(.wildcardKeyword): self = .wildcardKeyword
724+
case RawTokenKindMatch(.wildcard): self = .wildcard
725725
default: return nil
726726
}
727727
}
@@ -747,7 +747,7 @@ enum PrimaryExpressionStart: RawTokenKindSubset {
747747
case .stringLiteral: return .stringLiteral
748748
case .superKeyword: return .keyword(.super)
749749
case .trueKeyword: return .keyword(.true)
750-
case .wildcardKeyword: return .wildcardKeyword
750+
case .wildcard: return .wildcard
751751
}
752752
}
753753
}

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public enum TokenPrecedence: Comparable {
112112
// Identifiers
113113
.dollarIdentifier, .identifier,
114114
// '_' can occur in types to replace a type identifier
115-
.wildcardKeyword,
115+
.wildcard,
116116
// String segment, string interpolation anchor and pound don't really fit anywhere else
117117
.pound, .stringSegment:
118118
self = .identifierLike

Sources/SwiftParser/Types.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ extension Parser {
241241
base = RawTypeSyntax(self.parseTupleTypeBody())
242242
case .leftSquareBracket:
243243
base = RawTypeSyntax(self.parseCollectionType())
244-
case .wildcardKeyword:
244+
case .wildcard:
245245
base = RawTypeSyntax(self.parsePlaceholderType())
246246
default:
247247
return RawTypeSyntax(RawMissingTypeSyntax(arena: self.arena))
@@ -396,7 +396,7 @@ extension Parser {
396396
/// placeholder-type → wildcard
397397
@_spi(RawSyntax)
398398
public mutating func parsePlaceholderType() -> RawSimpleTypeIdentifierSyntax {
399-
let (unexpectedBeforeName, name) = self.expect(.wildcardKeyword)
399+
let (unexpectedBeforeName, name) = self.expect(.wildcard)
400400
// FIXME: Need a better syntax node than this
401401
return RawSimpleTypeIdentifierSyntax(
402402
unexpectedBeforeName,
@@ -749,7 +749,7 @@ extension Parser.Lookahead {
749749
guard self.consume(if: .rightSquareBracket) != nil else {
750750
return false
751751
}
752-
case .wildcardKeyword:
752+
case .wildcard:
753753
self.consumeAnyToken()
754754
case .keyword(.repeat):
755755
return true

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public struct InvalidIdentifierError: ParserError {
206206
case .unknown(let text) where text.first?.isNumber == true:
207207
let name = missingIdentifier.childNameInParent ?? "identifier"
208208
return "\(name) can only start with a letter or underscore, not a number"
209-
case .wildcardKeyword:
209+
case .wildcard:
210210
return "'\(invalidIdentifier.text)' cannot be used as an identifier here"
211211
case let tokenKind where tokenKind.isLexerClassifiedKeyword:
212212
return "keyword '\(invalidIdentifier.text)' cannot be used as an identifier here"

0 commit comments

Comments
 (0)