Skip to content

Commit 6ea8d50

Browse files
authored
Merge pull request swiftlang#2539 from bnbarham/add-introducer
Add an introducer property to DeclGroupSyntax
2 parents e154573 + 49e7930 commit 6ea8d50

File tree

5 files changed

+120
-11
lines changed

5 files changed

+120
-11
lines changed

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public let TRAITS: [Trait] = [
4747
children: [
4848
Child(name: "attributes", kind: .node(kind: .attributeList)),
4949
Child(name: "modifiers", kind: .node(kind: .declModifierList)),
50+
Child(
51+
name: "introducer",
52+
kind: .token(choices: [.keyword(.actor), .keyword(.class), .keyword(.enum), .keyword(.extension), .keyword(.protocol), .keyword(.struct)]),
53+
documentation: "The token that introduces this declaration, eg. `class` for a class declaration."
54+
),
5055
Child(name: "inheritanceClause", kind: .node(kind: .inheritanceClause), isOptional: true),
5156
Child(
5257
name: "genericWhereClause",

Release Notes/600.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- `String.isValidIdentifier(for:)`
3636
- Description: `SwiftParser` adds an extension on `String` to check if it can be used as an identifier in a given context.
3737
- Pull Request: https://github.com/apple/swift-syntax/pull/2434
38-
38+
3939
- `MacroDeclSyntax.expand`
4040
- the `expand(argumentList:definition:replacements:)` method gains a new parameter 'genericReplacements:' that is defaulted to an empty array.
4141
- The method's signature is now `expand(argumentList:definition:replacements:genericReplacements:)`
@@ -65,11 +65,16 @@
6565
- Type specifiers
6666
- Description: `AttributedTypeSyntax` can now contain multiple specifiers and these types are used to model the list of specifiers. Additionally, type specifiers can now contain arguments, like `borrow(data)`. To facilitate this, the following new types were introduces: `LifetimeSpecifierArgumentListSyntax`, `LifetimeSpecifierArgumentSyntax`, `LifetimeSpecifierArgumentsSyntax`, `LifetimeTypeSpecifierSyntax`, `SimpleTypeSpecifierSyntax`, `TypeSpecifierListSyntax`
6767
- Pull request: https://github.com/apple/swift-syntax/pull/2433
68-
68+
69+
- `DeclGroupSyntax.introducer`
70+
- Description: The `DeclGroupSyntax` trait has an extra `introducer` property, ie. the keyword that introduces the declaration.
71+
- Issue: https://github.com/apple/sourcekit-lsp/issues/2535
72+
- Pull Request: https://github.com/apple/swift-syntax/pull/2539
73+
6974
## API Behavior Changes
7075

7176
## Deprecations
72-
77+
7378
- Child Choice Node Casts
7479
- Description: `is`, `as`, and `cast` methods for types not contained in the choice node are marked as deprecated. The deprecated methods will emit a warning, indicating that the cast will always fail.
7580
- Issue: https://github.com/apple/swift-syntax/issues/2092
@@ -85,7 +90,7 @@
8590
- `MacroExpansion{Error|Warning|FixIt}Message` moved to the `SwiftSyntaxMacros` module
8691
- Description: Move the `MacroExpansion{Error|Warning|FixIt}Message` types from the `SwiftSyntaxMacroExpansion` module to `SwiftSyntaxMacros`. Deprecated typealiases in `SwiftSyntaxMacroExpansion` forward to `SwiftSyntaxMacros`.
8792
- Pull request: https://github.com/apple/swift-syntax/pull/2338
88-
- Notes: The expansion diagnostic messages were defined in `SwiftSyntaxMacroExpansion`, which is intended as an implementation detail of the plugin server and should not need to be imported by macros.
93+
- Notes: The expansion diagnostic messages were defined in `SwiftSyntaxMacroExpansion`, which is intended as an implementation detail of the plugin server and should not need to be imported by macros.
8994

9095
- `EditorPlaceholderDeclSyntax` and `EditorPlaceholderExprSyntax`:
9196
- Description: `EditorPlaceholderDeclSyntax` and `EditorPlaceholderExprSyntax` are now deprecated and placeholders are instead parsed as identifiers within a `MissingDeclSyntax` or `DeclReferenceExprSyntax`.
@@ -105,15 +110,15 @@
105110
- Effect specifiers:
106111
- Description: The `unexpectedAfterThrowsSpecifier` node of the various effect specifiers has been removed.
107112
- Pull request: https://github.com/apple/swift-syntax/pull/2219
108-
- Migration steps: Check `unexpectedBetweenThrowsSpecifierAndThrownError` and `unexpectedAfterThrownError` instead.
113+
- Migration steps: Check `unexpectedBetweenThrowsSpecifierAndThrownError` and `unexpectedAfterThrownError` instead.
109114

110115
- `SyntaxKind` removed conformance to `CaseIterable`
111-
- Description: `SyntaxKind` no longer conforms to `CaseIterable` since there is no good use case to iterate over all syntax kinds.
112-
- Pull request: https://github.com/apple/swift-syntax/pull/2292
116+
- Description: `SyntaxKind` no longer conforms to `CaseIterable` since there is no good use case to iterate over all syntax kinds.
117+
- Pull request: https://github.com/apple/swift-syntax/pull/2292
113118
- Migration steps: Exhaustively check all the syntax nodes that your program supports. There should be no use case to iterate over all cases in `SyntaxKind`.
114119

115120
- `IntegerLiteralExprSyntax.Radix` removed conformance to `CaseIterable`
116-
- Description: `IntegerLiteralExprSyntax.Radix` no longer conforms to `CaseIterable` since there is no good use case to iterate over all radix kinds.
121+
- Description: `IntegerLiteralExprSyntax.Radix` no longer conforms to `CaseIterable` since there is no good use case to iterate over all radix kinds.
117122
- Pull request: https://github.com/apple/swift-syntax/pull/2292
118123
- Migration steps: There should be no use case to iterate over all cases in `IntegerLiteralExprSyntax.Radix`.
119124

@@ -127,7 +132,7 @@
127132
- The new classification case covers the first names of parameters in function-like declarations and the label of arguments in function-like calls.
128133
- Pull request: https://github.com/apple/swift-syntax/pull/2375
129134
- Migration steps: In exhaustive switches over `SyntaxClassification`, cover the new case.
130-
135+
131136
- `SyntaxEnum` and `SyntaxKind` gained new cases: `throwsClause`
132137
- The new cases cover the newly introduced `ThrowsClauseSyntax`
133138
- Pull request: https://github.com/apple/swift-syntax/pull/2379
@@ -137,10 +142,10 @@
137142
- Description: The new property provides the lexical context in which the macro is expanded, and has several paired API changes. Types that conform to `MacroExpansionContext` will need to implement this property. Additionally, the `HostToPluginMessage` cases `expandFreestandingMacro` and `expandAttachedMacro` now include an optional `lexicalContext`. Finally, the `SyntaxProtocol.expand(macros:in:indentationWidth:)` syntactic expansion operation has been deprecated in favor of a new version `expand(macros:contextGenerator:indentationWidth:)` that takes a function produces a new macro expansion context for each expansion.
138143
- Pull request: https://github.com/apple/swift-syntax/pull/1554
139144
- Migration steps: Add the new property `lexicalContext` to any `MacroExpansionContext`-conforming types. If implementing the host-to-plugin message protocol, add support for `lexicalContext`. For macro expansion operations going through `SyntaxProtocol.expand`, provide a context generator that creates a fresh context including the lexical context.
140-
145+
141146
- `TriviaPiece.isBackslash` in `SwiftParserDiagnostics` removed
142147
- Description: `TriviaPiece.isBackslash` was not intended to be public API.
143-
- Pull request: xxx
148+
- Pull request: https://github.com/apple/swift-syntax/pull/2531
144149
- Migration steps: Use `if case .backslash = triviaPiece` instead
145150

146151
## Template

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_swift_syntax_library(SwiftSyntax
1414
BumpPtrAllocator.swift
1515
CommonAncestor.swift
1616
Convenience.swift
17+
CustomTraits.swift
1718
MemoryLayout.swift
1819
MissingNodeInitializers.swift
1920
SourceEdit.swift
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
extension ActorDeclSyntax {
14+
public var introducer: TokenSyntax {
15+
get {
16+
return actorKeyword
17+
}
18+
set {
19+
actorKeyword = newValue
20+
}
21+
}
22+
}
23+
24+
extension ClassDeclSyntax {
25+
public var introducer: TokenSyntax {
26+
get {
27+
return classKeyword
28+
}
29+
set {
30+
classKeyword = newValue
31+
}
32+
}
33+
}
34+
35+
extension EnumDeclSyntax {
36+
public var introducer: TokenSyntax {
37+
get {
38+
return enumKeyword
39+
}
40+
set {
41+
enumKeyword = newValue
42+
}
43+
}
44+
}
45+
46+
extension ExtensionDeclSyntax {
47+
public var introducer: TokenSyntax {
48+
get {
49+
return extensionKeyword
50+
}
51+
set {
52+
extensionKeyword = newValue
53+
}
54+
}
55+
}
56+
57+
extension ProtocolDeclSyntax {
58+
public var introducer: TokenSyntax {
59+
get {
60+
return protocolKeyword
61+
}
62+
set {
63+
protocolKeyword = newValue
64+
}
65+
}
66+
}
67+
68+
extension StructDeclSyntax {
69+
public var introducer: TokenSyntax {
70+
get {
71+
return structKeyword
72+
}
73+
set {
74+
structKeyword = newValue
75+
}
76+
}
77+
}
78+
79+
//==========================================================================//
80+
// IMPORTANT: If you are tempted to add an extension here, please insert //
81+
// it in alphabetical order above //
82+
//==========================================================================//

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public protocol DeclGroupSyntax: SyntaxProtocol, DeclSyntaxProtocol {
7272
set
7373
}
7474

75+
/// The token that introduces this declaration, eg. `class` for a class declaration.
76+
///
77+
/// ### Tokens
78+
///
79+
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds:
80+
/// - `actor`
81+
/// - `class`
82+
/// - `enum`
83+
/// - `extension`
84+
/// - `protocol`
85+
/// - `struct`
86+
var introducer: TokenSyntax {
87+
get
88+
set
89+
}
90+
7591
var inheritanceClause: InheritanceClauseSyntax? {
7692
get
7793
set

0 commit comments

Comments
 (0)