Skip to content

Commit 80b28f4

Browse files
authored
Merge pull request #1215 from ahoppen/ahoppen/merge-binary-operators
Merge `spacedBinaryOperator` and `unspacedBinaryOperator`
2 parents 8e6ae6e + 5286555 commit 80b28f4

37 files changed

+109
-220
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,7 @@ public let ATTRIBUTE_NODES: [Node] = [
514514
description: "The base name of the referenced function.",
515515
tokenChoices: [
516516
"Identifier",
517-
"UnspacedBinaryOperator",
518-
"SpacedBinaryOperator",
517+
"BinaryOperator",
519518
"PrefixOperator",
520519
"PostfixOperator"
521520
]),

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AvailabilityNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public let AVAILABILITY_NODES: [Node] = [
3030
Child(name: "Token",
3131
kind: "Token",
3232
tokenChoices: [
33-
"SpacedBinaryOperator",
33+
"BinaryOperator",
3434
"Identifier"
3535
]),
3636
Child(name: "AvailabilityVersionRestriction",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,7 @@ public let DECL_NODES: [Node] = [
812812
kind: "Token",
813813
tokenChoices: [
814814
"Identifier",
815-
"UnspacedBinaryOperator",
816-
"SpacedBinaryOperator",
815+
"BinaryOperator",
817816
"PrefixOperator",
818817
"PostfixOperator"
819818
]),
@@ -1312,8 +1311,7 @@ public let DECL_NODES: [Node] = [
13121311
Child(name: "Identifier",
13131312
kind: "Token",
13141313
tokenChoices: [
1315-
"UnspacedBinaryOperator",
1316-
"SpacedBinaryOperator",
1314+
"BinaryOperator",
13171315
"PrefixOperator",
13181316
"PostfixOperator"
13191317
],

CodeGeneration/Sources/SyntaxSupport/gyb_generated/ExprNodes.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public let EXPR_NODES: [Node] = [
167167
"Self",
168168
"CapitalSelf",
169169
"DollarIdentifier",
170-
"SpacedBinaryOperator"
170+
"BinaryOperator"
171171
]),
172172
Child(name: "DeclNameArguments",
173173
kind: "DeclNameArguments",
@@ -282,7 +282,10 @@ public let EXPR_NODES: [Node] = [
282282
kind: "Expr",
283283
children: [
284284
Child(name: "OperatorToken",
285-
kind: "BinaryOperatorToken")
285+
kind: "BinaryOperatorToken",
286+
tokenChoices: [
287+
"BinaryOperator"
288+
])
286289
]),
287290

288291
Node(name: "ArrowExpr",
@@ -1080,7 +1083,7 @@ public let EXPR_NODES: [Node] = [
10801083
"Self",
10811084
"CapitalSelf",
10821085
"DollarIdentifier",
1083-
"SpacedBinaryOperator",
1086+
"BinaryOperator",
10841087
"IntegerLiteral"
10851088
]),
10861089
Child(name: "DeclNameArguments",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/GenericNodes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public let GENERIC_NODES: [Node] = [
6767
Child(name: "EqualityToken",
6868
kind: "Token",
6969
tokenChoices: [
70-
"SpacedBinaryOperator",
71-
"UnspacedBinaryOperator",
70+
"BinaryOperator",
7271
"PrefixOperator",
7372
"PostfixOperator"
7473
]),

CodeGeneration/Sources/SyntaxSupport/gyb_generated/TokenSpec.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
313313
LiteralSpec(name: "RegexLiteral", kind: "regex_literal", nameForDiagnostics: "regex literal"),
314314
MiscSpec(name: "Unknown", kind: "unknown", nameForDiagnostics: "token"),
315315
MiscSpec(name: "Identifier", kind: "identifier", nameForDiagnostics: "identifier", classification: "Identifier"),
316-
MiscSpec(name: "UnspacedBinaryOperator", kind: "oper_binary_unspaced", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier"),
317-
MiscSpec(name: "SpacedBinaryOperator", kind: "oper_binary_spaced", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier", requiresLeadingSpace: true, requiresTrailingSpace: true),
316+
MiscSpec(name: "BinaryOperator", kind: "oper_binary", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier", requiresLeadingSpace: true, requiresTrailingSpace: true),
318317
MiscSpec(name: "PostfixOperator", kind: "oper_postfix", nameForDiagnostics: "postfix operator", classification: "OperatorIdentifier"),
319318
MiscSpec(name: "PrefixOperator", kind: "oper_prefix", nameForDiagnostics: "prefix operator", classification: "OperatorIdentifier"),
320319
MiscSpec(name: "DollarIdentifier", kind: "dollarident", nameForDiagnostics: "dollar identifier", classification: "DollarIdentifier"),

CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ let basicFormatFile = SourceFile {
124124
FunctionDecl("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
125125
SwitchStmt("""
126126
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
127-
case (.leftParen, .spacedBinaryOperator):
127+
case (.leftParen, .binaryOperator): // Ensures there is no space in @available(*, deprecated)
128128
return false
129129
default:
130130
break
@@ -165,9 +165,8 @@ let basicFormatFile = SourceFile {
165165
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
166166
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
167167
(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
168-
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
169-
return false
170-
case (.spacedBinaryOperator, .comma):
168+
(.tryKeyword, .postfixQuestionMark), // Ensures there is not space in `try?`
169+
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
171170
return false
172171
default:
173172
break

Sources/IDEUtils/generated/SyntaxClassification.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,7 @@ extension RawTokenKind {
358358
return .none
359359
case .identifier:
360360
return .identifier
361-
case .unspacedBinaryOperator:
362-
return .operatorIdentifier
363-
case .spacedBinaryOperator:
361+
case .binaryOperator:
364362
return .operatorIdentifier
365363
case .postfixOperator:
366364
return .operatorIdentifier

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ open class BasicFormat: SyntaxRewriter {
137137

138138
open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool {
139139
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
140-
case (.leftParen, .spacedBinaryOperator):
140+
case (.leftParen, .binaryOperator): // Ensures there is no space in @available(*, deprecated)
141141
return false
142142
default:
143143
break
@@ -155,7 +155,7 @@ open class BasicFormat: SyntaxRewriter {
155155
return true
156156
case .arrow:
157157
return true
158-
case .spacedBinaryOperator:
158+
case .binaryOperator:
159159
return true
160160
default:
161161
return false
@@ -178,9 +178,8 @@ open class BasicFormat: SyntaxRewriter {
178178
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
179179
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
180180
(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
181-
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
182-
return false
183-
case (.spacedBinaryOperator, .comma):
181+
(.tryKeyword, .postfixQuestionMark), // Ensures there is not space in `try?`
182+
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
184183
return false
185184
default:
186185
break
@@ -334,7 +333,7 @@ open class BasicFormat: SyntaxRewriter {
334333
return true
335334
case .poundHasSymbolKeyword:
336335
return true
337-
case .spacedBinaryOperator:
336+
case .binaryOperator:
338337
return true
339338
case .contextualKeyword(.async):
340339
return true

Sources/SwiftParser/Declarations.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,14 @@ extension Parser {
572572

573573
enum ExpectedTokenKind: RawTokenKindSubset {
574574
case colon
575-
case spacedBinaryOperator
576-
case unspacedBinaryOperator
575+
case binaryOperator
577576
case postfixOperator
578577
case prefixOperator
579578

580579
init?(lexeme: Lexer.Lexeme) {
581580
switch (lexeme.rawTokenKind, lexeme.tokenText) {
582581
case (.colon, _): self = .colon
583-
case (.spacedBinaryOperator, "=="): self = .spacedBinaryOperator
584-
case (.unspacedBinaryOperator, "=="): self = .unspacedBinaryOperator
582+
case (.binaryOperator, "=="): self = .binaryOperator
585583
case (.postfixOperator, "=="): self = .postfixOperator
586584
case (.prefixOperator, "=="): self = .prefixOperator
587585
default: return nil
@@ -591,8 +589,7 @@ extension Parser {
591589
var rawTokenKind: RawTokenKind {
592590
switch self {
593591
case .colon: return .colon
594-
case .spacedBinaryOperator: return .spacedBinaryOperator
595-
case .unspacedBinaryOperator: return .unspacedBinaryOperator
592+
case .binaryOperator: return .binaryOperator
596593
case .postfixOperator: return .postfixOperator
597594
case .prefixOperator: return .prefixOperator
598595
}
@@ -664,8 +661,7 @@ extension Parser {
664661
)
665662
)
666663
}
667-
case (.spacedBinaryOperator, let handle)?,
668-
(.unspacedBinaryOperator, let handle)?,
664+
case (.binaryOperator, let handle)?,
669665
(.postfixOperator, let handle)?,
670666
(.prefixOperator, let handle)?:
671667
let equal = self.eat(handle)
@@ -1271,7 +1267,7 @@ extension Parser {
12711267
name = SyntaxText(rebasing: name.dropLast())
12721268
}
12731269
unexpectedBeforeIdentifier = nil
1274-
identifier = self.consumePrefix(name, as: .spacedBinaryOperator)
1270+
identifier = self.consumePrefix(name, as: .binaryOperator)
12751271
} else {
12761272
(unexpectedBeforeIdentifier, identifier) = self.expectIdentifier(keywordRecovery: true)
12771273
}
@@ -1846,7 +1842,7 @@ extension Parser {
18461842
} else {
18471843
unexpectedBeforeName = nil
18481844
}
1849-
name = missingToken(.spacedBinaryOperator, text: nil)
1845+
name = missingToken(.binaryOperator, text: nil)
18501846
}
18511847

18521848
// Eat any subsequent tokens that are not separated to the operator by trivia.

Sources/SwiftParser/Expressions.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ extension Parser {
227227
pattern: PatternContext
228228
) -> (operator: RawExprSyntax, rhs: RawExprSyntax?)? {
229229
enum ExpectedTokenKind: RawTokenKindSubset {
230-
case spacedBinaryOperator
231-
case unspacedBinaryOperator
230+
case binaryOperator
232231
case infixQuestionMark
233232
case equal
234233
case isKeyword
@@ -239,8 +238,7 @@ extension Parser {
239238

240239
init?(lexeme: Lexer.Lexeme) {
241240
switch lexeme {
242-
case RawTokenKindMatch(.spacedBinaryOperator): self = .spacedBinaryOperator
243-
case RawTokenKindMatch(.unspacedBinaryOperator): self = .unspacedBinaryOperator
241+
case RawTokenKindMatch(.binaryOperator): self = .binaryOperator
244242
case RawTokenKindMatch(.infixQuestionMark): self = .infixQuestionMark
245243
case RawTokenKindMatch(.equal): self = .equal
246244
case RawTokenKindMatch(.isKeyword): self = .isKeyword
@@ -254,8 +252,7 @@ extension Parser {
254252

255253
var rawTokenKind: RawTokenKind {
256254
switch self {
257-
case .spacedBinaryOperator: return .spacedBinaryOperator
258-
case .unspacedBinaryOperator: return .unspacedBinaryOperator
255+
case .binaryOperator: return .binaryOperator
259256
case .infixQuestionMark: return .infixQuestionMark
260257
case .equal: return .equal
261258
case .isKeyword: return .isKeyword
@@ -268,7 +265,7 @@ extension Parser {
268265
}
269266

270267
switch self.at(anyIn: ExpectedTokenKind.self) {
271-
case (.spacedBinaryOperator, let handle)?, (.unspacedBinaryOperator, let handle)?:
268+
case (.binaryOperator, let handle)?:
272269
// Parse the operator.
273270
let operatorToken = self.eat(handle)
274271
let op = RawBinaryOperatorExprSyntax(operatorToken: operatorToken, arena: arena)
@@ -1047,7 +1044,7 @@ extension Parser {
10471044
if self.at(any: [
10481045
.postfixOperator, .postfixQuestionMark,
10491046
.exclamationMark, .prefixOperator,
1050-
.unspacedBinaryOperator,
1047+
.binaryOperator,
10511048
]),
10521049
let numComponents = getNumOptionalKeyPathPostfixComponents(
10531050
self.currentToken.tokenText
@@ -2413,7 +2410,7 @@ extension Parser {
24132410
// this case lexes as a binary operator because it neither leads nor
24142411
// follows a proper subexpression.
24152412
let expr: RawExprSyntax
2416-
if self.at(anyIn: BinaryOperator.self) != nil
2413+
if self.at(.binaryOperator)
24172414
&& (self.peek().rawTokenKind == .comma || self.peek().rawTokenKind == .rightParen || self.peek().rawTokenKind == .rightSquareBracket)
24182415
{
24192416
let (ident, args) = self.parseDeclNameRef(.operators)
@@ -2574,8 +2571,7 @@ extension Parser.Lookahead {
25742571
.colon,
25752572
.equal,
25762573
.postfixOperator,
2577-
.spacedBinaryOperator,
2578-
.unspacedBinaryOperator:
2574+
.binaryOperator:
25792575
return !backtrack.currentToken.isAtStartOfLine
25802576
default:
25812577
return false

Sources/SwiftParser/Lexer.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,11 +1791,7 @@ extension Lexer.Cursor {
17911791
}
17921792

17931793
if leftBound == rightBound {
1794-
if leftBound {
1795-
return Lexer.Result(.unspacedBinaryOperator)
1796-
} else {
1797-
return Lexer.Result(.spacedBinaryOperator)
1798-
}
1794+
return Lexer.Result(.binaryOperator)
17991795
} else if leftBound {
18001796
return Lexer.Result(.postfixOperator)
18011797
} else {

Sources/SwiftParser/Names.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension Parser {
6767
if self.at(.identifier) || self.at(any: [.selfKeyword, .capitalSelfKeyword, .initKeyword]) {
6868
ident = self.expectIdentifierWithoutRecovery()
6969
} else if flags.contains(.operators), let (_, _) = self.at(anyIn: Operator.self) {
70-
ident = self.consumeAnyToken(remapping: .unspacedBinaryOperator)
70+
ident = self.consumeAnyToken(remapping: .binaryOperator)
7171
} else if flags.contains(.keywords) && self.currentToken.rawTokenKind.isKeyword {
7272
ident = self.consumeAnyToken(remapping: .identifier)
7373
} else {

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,6 @@ enum AccessorKind: RawTokenKindSubset {
9595
}
9696
}
9797

98-
enum BinaryOperator: RawTokenKindSubset {
99-
case spacedBinaryOperator
100-
case unspacedBinaryOperator
101-
102-
init?(lexeme: Lexer.Lexeme) {
103-
switch lexeme.rawTokenKind {
104-
case .spacedBinaryOperator: self = .spacedBinaryOperator
105-
case .unspacedBinaryOperator: self = .unspacedBinaryOperator
106-
default: return nil
107-
}
108-
}
109-
110-
var rawTokenKind: RawTokenKind {
111-
switch self {
112-
case .spacedBinaryOperator: return .spacedBinaryOperator
113-
case .unspacedBinaryOperator: return .unspacedBinaryOperator
114-
}
115-
}
116-
}
117-
11898
enum CanBeStatementStart: RawTokenKindSubset {
11999
case breakKeyword
120100
case continueKeyword
@@ -460,15 +440,13 @@ enum IdentifierOrRethrowsTokens: RawTokenKindSubset {
460440
}
461441

462442
enum Operator: RawTokenKindSubset {
463-
case spacedBinaryOperator
464-
case unspacedBinaryOperator
443+
case binaryOperator
465444
case postfixOperator
466445
case prefixOperator
467446

468447
init?(lexeme: Lexer.Lexeme) {
469448
switch lexeme.rawTokenKind {
470-
case .spacedBinaryOperator: self = .spacedBinaryOperator
471-
case .unspacedBinaryOperator: self = .unspacedBinaryOperator
449+
case .binaryOperator: self = .binaryOperator
472450
case .postfixOperator: self = .postfixOperator
473451
case .prefixOperator: self = .prefixOperator
474452
default: return nil
@@ -477,8 +455,7 @@ enum Operator: RawTokenKindSubset {
477455

478456
var rawTokenKind: RawTokenKind {
479457
switch self {
480-
case .spacedBinaryOperator: return .spacedBinaryOperator
481-
case .unspacedBinaryOperator: return .unspacedBinaryOperator
458+
case .binaryOperator: return .binaryOperator
482459
case .postfixOperator: return .postfixOperator
483460
case .prefixOperator: return .prefixOperator
484461
}
@@ -536,7 +513,7 @@ enum OperatorLike: RawTokenKindSubset {
536513

537514
var precedence: TokenPrecedence? {
538515
switch self {
539-
case .regexLiteral: return TokenPrecedence(.spacedBinaryOperator)
516+
case .regexLiteral: return TokenPrecedence(.binaryOperator)
540517
default: return nil
541518
}
542519
}

Sources/SwiftParser/Statements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ extension Parser.Lookahead {
12761276
// context. We always consider it an apply expression of a function
12771277
// called `yield` for the purposes of the parse.
12781278
return false
1279-
case .spacedBinaryOperator, .unspacedBinaryOperator:
1279+
case .binaryOperator:
12801280
// 'yield &= x' treats yield as an identifier.
12811281
return false
12821282
default:

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public enum TokenPrecedence: Comparable {
125125
// Keywords in function types (we should be allowed to skip them inside parenthesis)
126126
.rethrowsKeyword, .throwsKeyword,
127127
// Operators can occur inside expressions
128-
.postfixOperator, .prefixOperator, .spacedBinaryOperator, .unspacedBinaryOperator,
128+
.postfixOperator, .prefixOperator, .binaryOperator,
129129
// Consider 'any' and 'inout' like a prefix operator to a type and a type is expression-like.
130130
.anyKeyword, .inoutKeyword,
131131
// 'where' can only occur in the signature of declarations. Consider the signature expression-like.

0 commit comments

Comments
 (0)