Skip to content

Remove Keyword suffix from Pound* #1862

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
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
37 changes: 13 additions & 24 deletions CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,26 @@ public class TokenSpec {
public let name: String
public let nameForDiagnostics: String
public let text: String?
public let isKeyword: Bool
public let associatedValueClass: String?

public var swiftKind: String {
let name = lowercaseFirstWord(name: self.name)

if isKeyword {
return name + "Keyword"
} else {
return name
}
return lowercaseFirstWord(name: self.name)
}

init(
name: String,
nameForDiagnostics: String,
text: String? = nil,
isKeyword: Bool = false,
associatedValueClass: String? = nil
) {
self.name = name
self.nameForDiagnostics = nameForDiagnostics
self.text = text
self.isKeyword = isKeyword
self.associatedValueClass = associatedValueClass
}
}

public class PoundKeywordSpec: TokenSpec {
public class PoundSpec: TokenSpec {
init(
name: String,
nameForDiagnostics: String? = nil,
Expand All @@ -52,13 +43,12 @@ public class PoundKeywordSpec: TokenSpec {
super.init(
name: name,
nameForDiagnostics: nameForDiagnostics ?? text,
text: text,
isKeyword: true
text: text
)
}
}

public class PoundObjectLiteralSpec: PoundKeywordSpec {
public class PoundObjectLiteralSpec: PoundSpec {
let `protocol`: String

init(
Expand All @@ -76,9 +66,9 @@ public class PoundObjectLiteralSpec: PoundKeywordSpec {
}
}

public class PoundConfigSpec: PoundKeywordSpec {}
public class PoundConfigSpec: PoundSpec {}

public class PoundDirectiveKeywordSpec: PoundKeywordSpec {
public class PoundDirectiveSpec: PoundSpec {
init(
name: String,
text: String
Expand All @@ -90,7 +80,7 @@ public class PoundDirectiveKeywordSpec: PoundKeywordSpec {
}
}

public class PoundConditionalDirectiveKeywordSpec: PoundDirectiveKeywordSpec {
public class PoundConditionalDirectiveSpec: PoundDirectiveSpec {
override init(
name: String,
text: String
Expand All @@ -110,8 +100,7 @@ public class PunctuatorSpec: TokenSpec {
super.init(
name: name,
nameForDiagnostics: text,
text: text,
isKeyword: false
text: text
)
}
}
Expand Down Expand Up @@ -149,11 +138,11 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
PunctuatorSpec(name: "PostfixQuestionMark", text: "?"),
PunctuatorSpec(name: "Pound", text: "#"),
PoundConfigSpec(name: "PoundAvailable", text: "#available"),
PoundConditionalDirectiveKeywordSpec(name: "PoundElse", text: "#else"),
PoundConditionalDirectiveKeywordSpec(name: "PoundElseif", text: "#elseif"),
PoundConditionalDirectiveKeywordSpec(name: "PoundEndif", text: "#endif"),
PoundConditionalDirectiveKeywordSpec(name: "PoundIf", text: "#if"),
PoundDirectiveKeywordSpec(name: "PoundSourceLocation", text: "#sourceLocation"),
PoundConditionalDirectiveSpec(name: "PoundElse", text: "#else"),
PoundConditionalDirectiveSpec(name: "PoundElseif", text: "#elseif"),
PoundConditionalDirectiveSpec(name: "PoundEndif", text: "#endif"),
PoundConditionalDirectiveSpec(name: "PoundIf", text: "#if"),
PoundDirectiveSpec(name: "PoundSourceLocation", text: "#sourceLocation"),
PoundConfigSpec(name: "PoundUnavailable", text: "#unavailable"),
PunctuatorSpec(name: "PrefixAmpersand", text: "&"),
MiscSpec(name: "PrefixOperator", nameForDiagnostics: "prefix operator"),
Expand Down
3 changes: 0 additions & 3 deletions CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ public extension Child {
guard let token = token, isToken else {
return type.defaultValue.map { InitializerClauseSyntax(value: $0) }
}
if token.isKeyword {
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()"))
}
if token.text != nil {
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
}
Expand Down
4 changes: 1 addition & 3 deletions CodeGeneration/Sources/Utils/SyntaxBuildableType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public struct SyntaxBuildableType: Hashable {
if isOptional {
return ExprSyntax(NilLiteralExprSyntax())
} else if let token = token {
if token.isKeyword {
return ExprSyntax(".\(raw: token.swiftKind)()")
} else if token.text != nil {
if token.text != nil {
return ExprSyntax(".\(raw: lowercaseFirstWord(name: token.name))Token()")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ let isLexerClassifiedFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
"""
) {
try! SwitchExprSyntax("switch self") {
for token in SYNTAX_TOKENS where token.isKeyword {
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
StmtSyntax("return true")
}
}

SwitchCaseSyntax("case .keyword(let keyword):") {
StmtSyntax("return keyword.isLexerClassified")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
IfConfigDeclSyntax(
clauses: try IfConfigClauseListSyntax {
IfConfigClauseSyntax(
poundKeyword: .poundIfKeyword(),
poundKeyword: .poundIfToken(),
condition: ExprSyntax("SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION"),
elements: .statements(
try CodeBlockItemListSyntax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
""",
clauses: IfConfigClauseListSyntax {
IfConfigClauseSyntax(
poundKeyword: .poundIfKeyword(),
poundKeyword: .poundIfToken(),
condition: ExprSyntax("DEBUG"),
elements: .statements(
try CodeBlockItemListSyntax {
Expand Down Expand Up @@ -250,7 +250,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
)
)
IfConfigClauseSyntax(
poundKeyword: .poundElseKeyword(),
poundKeyword: .poundElseToken(),
elements: .statements(
CodeBlockItemListSyntax {
try! FunctionDeclSyntax("private func visit(_ data: SyntaxData) -> Syntax") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,7 @@ import Utils
let tokensFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
try! ExtensionDeclSyntax("extension TokenSyntax") {
for token in SYNTAX_TOKENS {
if token.isKeyword {
DeclSyntax(
"""
public static func \(raw: token.swiftKind)(
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = [],
presence: SourcePresence = .present
) -> TokenSyntax {
return TokenSyntax(
.\(raw: token.swiftKind),
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia,
presence: presence
)
}
"""
)
} else if let text = token.text {
if let text = token.text {
DeclSyntax(
"""
public static func \(raw: token.swiftKind)Token(
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ open class BasicFormat: SyntaxRewriter {
(.multilineStringQuote, .stringSegment), // segment starting a multi-line string literal
(.stringSegment, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
(.rightParen, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
(.poundEndifKeyword, _),
(_, .poundElseKeyword),
(_, .poundElseifKeyword),
(_, .poundEndifKeyword),
(.poundEndif, _),
(_, .poundElse),
(_, .poundElseif),
(_, .poundEndif),
(_, .rightBrace):
return true
default:
Expand Down Expand Up @@ -283,7 +283,7 @@ open class BasicFormat: SyntaxRewriter {
(.postfixQuestionMark, .leftParen), // init?() or myOptionalClosure?()
(.postfixQuestionMark, .period), // someOptional?.someProperty
(.pound, _),
(.poundUnavailableKeyword, .leftParen), // #unavailable(...)
(.poundUnavailable, .leftParen), // #unavailable(...)
(.prefixAmpersand, _),
(.prefixOperator, _),
(.rawStringDelimiter, .leftParen), // opening raw string delimiter should never be separate by a space
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftIDEUtils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_swift_host_library(SwiftIDEUtils
SwiftIDEUtilsCompatibility.swift
Syntax+Classifications.swift
SyntaxClassification.swift
SyntaxClassifier.swift
Expand Down
27 changes: 27 additions & 0 deletions Sources/SwiftIDEUtils/SwiftIDEUtilsCompatibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// This file provides compatiblity aliases to keep dependents of SwiftSyntax building.
// All users of the declarations in this file should transition away from them ASAP.

public extension SyntaxClassification {
/// A `#` keyword like `#warning`.
@available(*, deprecated, renamed: "poundDirective")
static var poundDirectiveKeyword: SyntaxClassification {
return .poundDirective
}
}

//==========================================================================//
// IMPORTANT: If you are tempted to add a compatiblity layer code here //
// please insert it in alphabetical order above //
//==========================================================================//
32 changes: 16 additions & 16 deletions Sources/SwiftIDEUtils/SyntaxClassification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public enum SyntaxClassification {
case objectLiteral
/// An identifier referring to an operator.
case operatorIdentifier
/// A `#` keyword like `#warning`.
case poundDirectiveKeyword
/// A `#` token like `#warning`.
case poundDirective
/// A regex literal, including multiline regex literals.
case regexLiteral
/// The opening and closing parenthesis of string interpolation.
Expand Down Expand Up @@ -156,20 +156,20 @@ extension RawTokenKind {
return .none
case .pound:
return .none
case .poundAvailableKeyword:
return .keyword
case .poundElseKeyword:
return .poundDirectiveKeyword
case .poundElseifKeyword:
return .poundDirectiveKeyword
case .poundEndifKeyword:
return .poundDirectiveKeyword
case .poundIfKeyword:
return .poundDirectiveKeyword
case .poundSourceLocationKeyword:
return .poundDirectiveKeyword
case .poundUnavailableKeyword:
return .keyword
case .poundAvailable:
return .none
case .poundElse:
return .poundDirective
case .poundElseif:
return .poundDirective
case .poundEndif:
return .poundDirective
case .poundIf:
return .poundDirective
case .poundSourceLocation:
return .poundDirective
case .poundUnavailable:
return .none
case .prefixAmpersand:
return .none
case .prefixOperator:
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

extension Parser {
mutating func parseAttributeList() -> RawAttributeListSyntax? {
guard self.at(.atSign, .poundIfKeyword) else {
guard self.at(.atSign, .poundIf) else {
return nil
}

Expand All @@ -23,7 +23,7 @@ extension Parser {
repeat {
let attribute = self.parseAttribute()
elements.append(attribute)
} while self.at(.atSign, .poundIfKeyword) && loopProgress.evaluate(currentToken)
} while self.at(.atSign, .poundIf) && loopProgress.evaluate(currentToken)
return RawAttributeListSyntax(elements: elements, arena: self.arena)
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ extension Parser {
}

mutating func parseAttribute() -> RawAttributeListSyntax.Element {
if self.at(.poundIfKeyword) {
if self.at(.poundIf) {
return .ifConfigDecl(
self.parsePoundIfDirective { (parser, _) -> RawAttributeListSyntax.Element in
return parser.parseAttribute()
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension TokenConsumer {
allowInitDecl: Bool = true,
allowRecovery: Bool = false
) -> Bool {
if self.at(.poundIfKeyword) {
if self.at(.poundIf) {
return true
}

Expand Down Expand Up @@ -85,12 +85,12 @@ extension TokenConsumer {
}

if hasAttribute {
if subparser.at(.rightBrace) || subparser.at(.endOfFile) || subparser.at(.poundEndifKeyword) {
if subparser.at(.rightBrace) || subparser.at(.endOfFile) || subparser.at(.poundEndif) {
return true
}
}

if subparser.at(.poundIfKeyword) {
if subparser.at(.poundIf) {
var attrLookahead = subparser.lookahead()
return attrLookahead.consumeIfConfigOfAttributes()
}
Expand Down Expand Up @@ -196,7 +196,7 @@ extension Parser {
mutating func parseDeclaration(inMemberDeclList: Bool = false) -> RawDeclSyntax {
// If we are at a `#if` of attributes, the `#if` directive should be
// parsed when we're parsing the attributes.
if self.at(.poundIfKeyword) && !self.withLookahead({ $0.consumeIfConfigOfAttributes() }) {
if self.at(.poundIf) && !self.withLookahead({ $0.consumeIfConfigOfAttributes() }) {
let directive = self.parsePoundIfDirective { (parser, _) in
let parsedDecl = parser.parseDeclaration()
let semicolon = parser.consume(if: .semicolon)
Expand Down Expand Up @@ -722,7 +722,7 @@ extension Parser {
}

let decl: RawDeclSyntax
if self.at(.poundSourceLocationKeyword) {
if self.at(.poundSourceLocation) {
decl = RawDeclSyntax(self.parsePoundSourceLocationDirective())
} else {
decl = self.parseDeclaration(inMemberDeclList: true)
Expand Down
Loading