Skip to content

Merge spacedBinaryOperator and unspacedBinaryOperator #1215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ public let ATTRIBUTE_NODES: [Node] = [
description: "The base name of the referenced function.",
tokenChoices: [
"Identifier",
"UnspacedBinaryOperator",
"SpacedBinaryOperator",
"BinaryOperator",
"PrefixOperator",
"PostfixOperator"
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public let AVAILABILITY_NODES: [Node] = [
Child(name: "Token",
kind: "Token",
tokenChoices: [
"SpacedBinaryOperator",
"BinaryOperator",
"Identifier"
]),
Child(name: "AvailabilityVersionRestriction",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,7 @@ public let DECL_NODES: [Node] = [
kind: "Token",
tokenChoices: [
"Identifier",
"UnspacedBinaryOperator",
"SpacedBinaryOperator",
"BinaryOperator",
"PrefixOperator",
"PostfixOperator"
]),
Expand Down Expand Up @@ -1312,8 +1311,7 @@ public let DECL_NODES: [Node] = [
Child(name: "Identifier",
kind: "Token",
tokenChoices: [
"UnspacedBinaryOperator",
"SpacedBinaryOperator",
"BinaryOperator",
"PrefixOperator",
"PostfixOperator"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public let EXPR_NODES: [Node] = [
"Self",
"CapitalSelf",
"DollarIdentifier",
"SpacedBinaryOperator"
"BinaryOperator"
]),
Child(name: "DeclNameArguments",
kind: "DeclNameArguments",
Expand Down Expand Up @@ -282,7 +282,10 @@ public let EXPR_NODES: [Node] = [
kind: "Expr",
children: [
Child(name: "OperatorToken",
kind: "BinaryOperatorToken")
kind: "BinaryOperatorToken",
tokenChoices: [
"BinaryOperator"
])
]),

Node(name: "ArrowExpr",
Expand Down Expand Up @@ -1080,7 +1083,7 @@ public let EXPR_NODES: [Node] = [
"Self",
"CapitalSelf",
"DollarIdentifier",
"SpacedBinaryOperator",
"BinaryOperator",
"IntegerLiteral"
]),
Child(name: "DeclNameArguments",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public let GENERIC_NODES: [Node] = [
Child(name: "EqualityToken",
kind: "Token",
tokenChoices: [
"SpacedBinaryOperator",
"UnspacedBinaryOperator",
"BinaryOperator",
"PrefixOperator",
"PostfixOperator"
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
LiteralSpec(name: "RegexLiteral", kind: "regex_literal", nameForDiagnostics: "regex literal"),
MiscSpec(name: "Unknown", kind: "unknown", nameForDiagnostics: "token"),
MiscSpec(name: "Identifier", kind: "identifier", nameForDiagnostics: "identifier", classification: "Identifier"),
MiscSpec(name: "UnspacedBinaryOperator", kind: "oper_binary_unspaced", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier"),
MiscSpec(name: "SpacedBinaryOperator", kind: "oper_binary_spaced", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier", requiresLeadingSpace: true, requiresTrailingSpace: true),
MiscSpec(name: "BinaryOperator", kind: "oper_binary", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier", requiresLeadingSpace: true, requiresTrailingSpace: true),
MiscSpec(name: "PostfixOperator", kind: "oper_postfix", nameForDiagnostics: "postfix operator", classification: "OperatorIdentifier"),
MiscSpec(name: "PrefixOperator", kind: "oper_prefix", nameForDiagnostics: "prefix operator", classification: "OperatorIdentifier"),
MiscSpec(name: "DollarIdentifier", kind: "dollarident", nameForDiagnostics: "dollar identifier", classification: "DollarIdentifier"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ let basicFormatFile = SourceFile {
FunctionDecl("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
SwitchStmt("""
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
case (.leftParen, .spacedBinaryOperator):
case (.leftParen, .binaryOperator): // Ensures there is no space in @available(*, deprecated)
return false
default:
break
Expand Down Expand Up @@ -165,9 +165,8 @@ let basicFormatFile = SourceFile {
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
return false
case (.spacedBinaryOperator, .comma):
(.tryKeyword, .postfixQuestionMark), // Ensures there is not space in `try?`
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
return false
default:
break
Expand Down
4 changes: 1 addition & 3 deletions Sources/IDEUtils/generated/SyntaxClassification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ extension RawTokenKind {
return .none
case .identifier:
return .identifier
case .unspacedBinaryOperator:
return .operatorIdentifier
case .spacedBinaryOperator:
case .binaryOperator:
return .operatorIdentifier
case .postfixOperator:
return .operatorIdentifier
Expand Down
11 changes: 5 additions & 6 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ open class BasicFormat: SyntaxRewriter {

open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool {
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
case (.leftParen, .spacedBinaryOperator):
case (.leftParen, .binaryOperator): // Ensures there is no space in @available(*, deprecated)
return false
default:
break
Expand All @@ -155,7 +155,7 @@ open class BasicFormat: SyntaxRewriter {
return true
case .arrow:
return true
case .spacedBinaryOperator:
case .binaryOperator:
return true
default:
return false
Expand All @@ -178,9 +178,8 @@ open class BasicFormat: SyntaxRewriter {
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
return false
case (.spacedBinaryOperator, .comma):
(.tryKeyword, .postfixQuestionMark), // Ensures there is not space in `try?`
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
return false
default:
break
Expand Down Expand Up @@ -334,7 +333,7 @@ open class BasicFormat: SyntaxRewriter {
return true
case .poundHasSymbolKeyword:
return true
case .spacedBinaryOperator:
case .binaryOperator:
return true
case .contextualKeyword(.async):
return true
Expand Down
16 changes: 6 additions & 10 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -572,16 +572,14 @@ extension Parser {

enum ExpectedTokenKind: RawTokenKindSubset {
case colon
case spacedBinaryOperator
case unspacedBinaryOperator
case binaryOperator
case postfixOperator
case prefixOperator

init?(lexeme: Lexer.Lexeme) {
switch (lexeme.rawTokenKind, lexeme.tokenText) {
case (.colon, _): self = .colon
case (.spacedBinaryOperator, "=="): self = .spacedBinaryOperator
case (.unspacedBinaryOperator, "=="): self = .unspacedBinaryOperator
case (.binaryOperator, "=="): self = .binaryOperator
case (.postfixOperator, "=="): self = .postfixOperator
case (.prefixOperator, "=="): self = .prefixOperator
default: return nil
Expand All @@ -591,8 +589,7 @@ extension Parser {
var rawTokenKind: RawTokenKind {
switch self {
case .colon: return .colon
case .spacedBinaryOperator: return .spacedBinaryOperator
case .unspacedBinaryOperator: return .unspacedBinaryOperator
case .binaryOperator: return .binaryOperator
case .postfixOperator: return .postfixOperator
case .prefixOperator: return .prefixOperator
}
Expand Down Expand Up @@ -664,8 +661,7 @@ extension Parser {
)
)
}
case (.spacedBinaryOperator, let handle)?,
(.unspacedBinaryOperator, let handle)?,
case (.binaryOperator, let handle)?,
(.postfixOperator, let handle)?,
(.prefixOperator, let handle)?:
let equal = self.eat(handle)
Expand Down Expand Up @@ -1271,7 +1267,7 @@ extension Parser {
name = SyntaxText(rebasing: name.dropLast())
}
unexpectedBeforeIdentifier = nil
identifier = self.consumePrefix(name, as: .spacedBinaryOperator)
identifier = self.consumePrefix(name, as: .binaryOperator)
} else {
(unexpectedBeforeIdentifier, identifier) = self.expectIdentifier(keywordRecovery: true)
}
Expand Down Expand Up @@ -1846,7 +1842,7 @@ extension Parser {
} else {
unexpectedBeforeName = nil
}
name = missingToken(.spacedBinaryOperator, text: nil)
name = missingToken(.binaryOperator, text: nil)
}

// Eat any subsequent tokens that are not separated to the operator by trivia.
Expand Down
18 changes: 7 additions & 11 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ extension Parser {
pattern: PatternContext
) -> (operator: RawExprSyntax, rhs: RawExprSyntax?)? {
enum ExpectedTokenKind: RawTokenKindSubset {
case spacedBinaryOperator
case unspacedBinaryOperator
case binaryOperator
case infixQuestionMark
case equal
case isKeyword
Expand All @@ -239,8 +238,7 @@ extension Parser {

init?(lexeme: Lexer.Lexeme) {
switch lexeme {
case RawTokenKindMatch(.spacedBinaryOperator): self = .spacedBinaryOperator
case RawTokenKindMatch(.unspacedBinaryOperator): self = .unspacedBinaryOperator
case RawTokenKindMatch(.binaryOperator): self = .binaryOperator
case RawTokenKindMatch(.infixQuestionMark): self = .infixQuestionMark
case RawTokenKindMatch(.equal): self = .equal
case RawTokenKindMatch(.isKeyword): self = .isKeyword
Expand All @@ -254,8 +252,7 @@ extension Parser {

var rawTokenKind: RawTokenKind {
switch self {
case .spacedBinaryOperator: return .spacedBinaryOperator
case .unspacedBinaryOperator: return .unspacedBinaryOperator
case .binaryOperator: return .binaryOperator
case .infixQuestionMark: return .infixQuestionMark
case .equal: return .equal
case .isKeyword: return .isKeyword
Expand All @@ -268,7 +265,7 @@ extension Parser {
}

switch self.at(anyIn: ExpectedTokenKind.self) {
case (.spacedBinaryOperator, let handle)?, (.unspacedBinaryOperator, let handle)?:
case (.binaryOperator, let handle)?:
// Parse the operator.
let operatorToken = self.eat(handle)
let op = RawBinaryOperatorExprSyntax(operatorToken: operatorToken, arena: arena)
Expand Down Expand Up @@ -1047,7 +1044,7 @@ extension Parser {
if self.at(any: [
.postfixOperator, .postfixQuestionMark,
.exclamationMark, .prefixOperator,
.unspacedBinaryOperator,
.binaryOperator,
]),
let numComponents = getNumOptionalKeyPathPostfixComponents(
self.currentToken.tokenText
Expand Down Expand Up @@ -2413,7 +2410,7 @@ extension Parser {
// this case lexes as a binary operator because it neither leads nor
// follows a proper subexpression.
let expr: RawExprSyntax
if self.at(anyIn: BinaryOperator.self) != nil
if self.at(.binaryOperator)
&& (self.peek().rawTokenKind == .comma || self.peek().rawTokenKind == .rightParen || self.peek().rawTokenKind == .rightSquareBracket)
{
let (ident, args) = self.parseDeclNameRef(.operators)
Expand Down Expand Up @@ -2574,8 +2571,7 @@ extension Parser.Lookahead {
.colon,
.equal,
.postfixOperator,
.spacedBinaryOperator,
.unspacedBinaryOperator:
.binaryOperator:
return !backtrack.currentToken.isAtStartOfLine
default:
return false
Expand Down
6 changes: 1 addition & 5 deletions Sources/SwiftParser/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1791,11 +1791,7 @@ extension Lexer.Cursor {
}

if leftBound == rightBound {
if leftBound {
return Lexer.Result(.unspacedBinaryOperator)
} else {
return Lexer.Result(.spacedBinaryOperator)
}
return Lexer.Result(.binaryOperator)
} else if leftBound {
return Lexer.Result(.postfixOperator)
} else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension Parser {
if self.at(.identifier) || self.at(any: [.selfKeyword, .capitalSelfKeyword, .initKeyword]) {
ident = self.expectIdentifierWithoutRecovery()
} else if flags.contains(.operators), let (_, _) = self.at(anyIn: Operator.self) {
ident = self.consumeAnyToken(remapping: .unspacedBinaryOperator)
ident = self.consumeAnyToken(remapping: .binaryOperator)
} else if flags.contains(.keywords) && self.currentToken.rawTokenKind.isKeyword {
ident = self.consumeAnyToken(remapping: .identifier)
} else {
Expand Down
31 changes: 4 additions & 27 deletions Sources/SwiftParser/RawTokenKindSubset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,6 @@ enum AccessorKind: RawTokenKindSubset {
}
}

enum BinaryOperator: RawTokenKindSubset {
case spacedBinaryOperator
case unspacedBinaryOperator

init?(lexeme: Lexer.Lexeme) {
switch lexeme.rawTokenKind {
case .spacedBinaryOperator: self = .spacedBinaryOperator
case .unspacedBinaryOperator: self = .unspacedBinaryOperator
default: return nil
}
}

var rawTokenKind: RawTokenKind {
switch self {
case .spacedBinaryOperator: return .spacedBinaryOperator
case .unspacedBinaryOperator: return .unspacedBinaryOperator
}
}
}

enum CanBeStatementStart: RawTokenKindSubset {
case breakKeyword
case continueKeyword
Expand Down Expand Up @@ -460,15 +440,13 @@ enum IdentifierOrRethrowsTokens: RawTokenKindSubset {
}

enum Operator: RawTokenKindSubset {
case spacedBinaryOperator
case unspacedBinaryOperator
case binaryOperator
case postfixOperator
case prefixOperator

init?(lexeme: Lexer.Lexeme) {
switch lexeme.rawTokenKind {
case .spacedBinaryOperator: self = .spacedBinaryOperator
case .unspacedBinaryOperator: self = .unspacedBinaryOperator
case .binaryOperator: self = .binaryOperator
case .postfixOperator: self = .postfixOperator
case .prefixOperator: self = .prefixOperator
default: return nil
Expand All @@ -477,8 +455,7 @@ enum Operator: RawTokenKindSubset {

var rawTokenKind: RawTokenKind {
switch self {
case .spacedBinaryOperator: return .spacedBinaryOperator
case .unspacedBinaryOperator: return .unspacedBinaryOperator
case .binaryOperator: return .binaryOperator
case .postfixOperator: return .postfixOperator
case .prefixOperator: return .prefixOperator
}
Expand Down Expand Up @@ -536,7 +513,7 @@ enum OperatorLike: RawTokenKindSubset {

var precedence: TokenPrecedence? {
switch self {
case .regexLiteral: return TokenPrecedence(.spacedBinaryOperator)
case .regexLiteral: return TokenPrecedence(.binaryOperator)
default: return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ extension Parser.Lookahead {
// context. We always consider it an apply expression of a function
// called `yield` for the purposes of the parse.
return false
case .spacedBinaryOperator, .unspacedBinaryOperator:
case .binaryOperator:
// 'yield &= x' treats yield as an identifier.
return false
default:
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/TokenPrecedence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public enum TokenPrecedence: Comparable {
// Keywords in function types (we should be allowed to skip them inside parenthesis)
.rethrowsKeyword, .throwsKeyword,
// Operators can occur inside expressions
.postfixOperator, .prefixOperator, .spacedBinaryOperator, .unspacedBinaryOperator,
.postfixOperator, .prefixOperator, .binaryOperator,
// Consider 'any' and 'inout' like a prefix operator to a type and a type is expression-like.
.anyKeyword, .inoutKeyword,
// 'where' can only occur in the signature of declarations. Consider the signature expression-like.
Expand Down
Loading