Skip to content

Commit 8ee7df4

Browse files
committed
Improve token choices doc comment
- Moves complexity of generating the comment into `Child.swift` and uses `grammar.grammar()` directly. - Appends a `TriviaPiece.newlines(1)` between trivia pieces when merging with the original comment.
1 parent 5625a8c commit 8ee7df4

File tree

12 files changed

+1153
-654
lines changed

12 files changed

+1153
-654
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,34 @@ public class Child {
9393
public let documentationSummary: SwiftSyntax.Trivia
9494

9595
/// A docc comment describing the child, including the trivia provided when
96-
/// initializing the Child, and the list of possible token choices inferred automatically.
96+
/// initializing the ``Child``, and the list of possible token choices inferred automatically.
9797
public var documentation: SwiftSyntax.Trivia {
9898
if case .token(let choices, _, _) = kind {
99-
var tokenKindsComment =
99+
let grammar = GrammarGenerator()
100+
101+
var tokensDocumentation = """
102+
### Tokens
103+
For syntax trees generated by the parser, this is guaranteed to be the following
104+
"""
105+
106+
tokensDocumentation +=
100107
choices.count == 1
101-
? "For syntax trees generated by the parser, this is guaranteed to be the following kind:"
102-
: "For syntax trees generated by the parser, this is guaranteed to be one of the following token kinds:\n"
103-
tokenKindsComment += GrammarGenerator.childTokenChoices(for: self)
108+
? "kind: \(grammar.grammar(for: choices.first!))"
109+
: """
110+
kinds:
111+
\(choices.map { " - \(grammar.grammar(for: $0))" }.joined(separator: "\n"))
112+
"""
104113

105-
let trivia = docCommentTrivia(from: tokenKindsComment)
106-
return documentationSummary.appending(trivia)
114+
// trivia's last piece does not end in \n.
115+
// So if the existing documentation is not emtpy, we need to append an
116+
// additional TriviaPiece.newlines(1) before appending the token choices.
117+
if documentationSummary.isEmpty {
118+
return docCommentTrivia(from: tokensDocumentation)
119+
} else {
120+
return documentationSummary
121+
.appending(TriviaPiece.newlines(1))
122+
.appending(docCommentTrivia(from: tokensDocumentation))
123+
}
107124
}
108125

109126
// If this child is not a token kind, return documentation summary without the choices list.

CodeGeneration/Sources/SyntaxSupport/GrammarGenerator.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ struct GrammarGenerator {
1919
///
2020
/// - parameters:
2121
/// - tokenChoice: ``TokenChoice`` to describe
22-
/// - backticks: Whether to wrap the token choice in backticks
23-
private func grammar(for tokenChoice: TokenChoice) -> String {
22+
public func grammar(for tokenChoice: TokenChoice) -> String {
2423
switch tokenChoice {
2524
case .keyword(let keyword):
2625
return "`'\(keyword.spec.name)'`"
@@ -66,20 +65,4 @@ struct GrammarGenerator {
6665
.map { " - `\($0.varOrCaseName)`: \(generator.grammar(for: $0))" }
6766
.joined(separator: "\n")
6867
}
69-
70-
/// Generates the list of possible token kinds for this particular ``Child``.
71-
/// The child must be of kind ``ChildKind/token``. Otherwise, an empty string will be returned.
72-
static func childTokenChoices(for child: Child) -> String {
73-
let generator = GrammarGenerator()
74-
75-
if case .token(let choices, _, _) = child.kind {
76-
if choices.count == 1 {
77-
return " \(generator.grammar(for: choices.first!))"
78-
} else {
79-
return choices.map { " - \(generator.grammar(for: $0))" }.joined(separator: "\n")
80-
}
81-
} else {
82-
return ""
83-
}
84-
}
8568
}

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717

1818
public protocol BracedSyntax: SyntaxProtocol {
19-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: '{'
19+
/// ### Tokens
20+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `'{'`
2021
var leftBrace: TokenSyntax {
2122
get
2223
set
2324
}
2425

25-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: '}'
26+
/// ### Tokens
27+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `'}'`
2628
var rightBrace: TokenSyntax {
2729
get
2830
set
@@ -123,9 +125,10 @@ public protocol EffectSpecifiersSyntax: SyntaxProtocol {
123125
set
124126
}
125127

126-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following token kinds:
127-
/// - 'async'
128-
/// - 'reasync'
128+
/// ### Tokens
129+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkinds:
130+
/// - `'async'`
131+
/// - `'reasync'`
129132
var asyncSpecifier: TokenSyntax? {
130133
get
131134
set
@@ -136,9 +139,10 @@ public protocol EffectSpecifiersSyntax: SyntaxProtocol {
136139
set
137140
}
138141

139-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following token kinds:
140-
/// - 'throws'
141-
/// - 'rethrows'
142+
/// ### Tokens
143+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkinds:
144+
/// - `'throws'`
145+
/// - `'rethrows'`
142146
var throwsSpecifier: TokenSyntax? {
143147
get
144148
set
@@ -181,13 +185,15 @@ public extension SyntaxProtocol {
181185

182186

183187
public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
184-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: '#'
188+
/// ### Tokens
189+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `'#'`
185190
var pound: TokenSyntax {
186191
get
187192
set
188193
}
189194

190-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: <identifier>
195+
/// ### Tokens
196+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `<identifier>`
191197
var macroName: TokenSyntax {
192198
get
193199
set
@@ -198,7 +204,8 @@ public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
198204
set
199205
}
200206

201-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: '('
207+
/// ### Tokens
208+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `'('`
202209
var leftParen: TokenSyntax? {
203210
get
204211
set
@@ -209,7 +216,8 @@ public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
209216
set
210217
}
211218

212-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: ')'
219+
/// ### Tokens
220+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `')'`
213221
var rightParen: TokenSyntax? {
214222
get
215223
set
@@ -257,7 +265,8 @@ public extension SyntaxProtocol {
257265

258266

259267
public protocol NamedDeclSyntax: SyntaxProtocol {
260-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: <identifier>
268+
/// ### Tokens
269+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `<identifier>`
261270
var name: TokenSyntax {
262271
get
263272
set
@@ -297,7 +306,9 @@ public extension SyntaxProtocol {
297306
///
298307
/// See the types conforming to this protocol for examples of where missing nodes can occur.
299308
public protocol MissingNodeSyntax: SyntaxProtocol {
300-
/// A placeholder, i.e. `<#placeholder#>`, that can be inserted into the source code to represent the missing node./// For syntax trees generated by the parser, this is guaranteed to be the following kind: <identifier>
309+
/// A placeholder, i.e. `<#placeholder#>`, that can be inserted into the source code to represent the missing node.
310+
/// ### Tokens
311+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `<identifier>`
301312
var placeholder: TokenSyntax {
302313
get
303314
set
@@ -335,13 +346,15 @@ public extension SyntaxProtocol {
335346

336347

337348
public protocol ParenthesizedSyntax: SyntaxProtocol {
338-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: '('
349+
/// ### Tokens
350+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `'('`
339351
var leftParen: TokenSyntax {
340352
get
341353
set
342354
}
343355

344-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: ')'
356+
/// ### Tokens
357+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `')'`
345358
var rightParen: TokenSyntax {
346359
get
347360
set
@@ -573,7 +586,8 @@ public extension SyntaxProtocol {
573586

574587

575588
public protocol WithTrailingCommaSyntax: SyntaxProtocol {
576-
/// For syntax trees generated by the parser, this is guaranteed to be the following kind: ','
589+
/// ### Tokens
590+
/// For syntax trees generated by the parser, this is guaranteed to be the followingkind: `','`
577591
var trailingComma: TokenSyntax? {
578592
get
579593
set

0 commit comments

Comments
 (0)