Skip to content

Commit a61bb8d

Browse files
committed
Refactor child token choices comment generator
- Improves readability by omitting single quotes inside backticks (@kimdv) - Cleans up the language on possible token choices (@ahoppen) - Moves token choices generation into `GrammarGenerator`, but leaves the documentation property in `Child` so it's consistent with how `Node` works.
1 parent 146f79e commit a61bb8d

12 files changed

+898
-891
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,8 @@ public class Child {
9696
/// 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-
let grammar = GrammarGenerator()
100-
101-
let choices =
102-
choices.count == 1
103-
? "kind: \(grammar.grammar(for: choices.first!))"
104-
: """
105-
kinds:
106-
\(choices.map { " - \(grammar.grammar(for: $0))" }.joined(separator: "\n"))
107-
"""
108-
10999
let tokenChoicesTrivia = SwiftSyntax.Trivia.docCommentTrivia(
110-
from: """
111-
### Tokens
112-
113-
For syntax trees generated by the parser, this is guaranteed to be one of the following \(choices)
114-
"""
100+
from: GrammarGenerator.childTokenChoices(for: choices)
115101
)
116102

117103
return SwiftSyntax.Trivia(joining: [documentationSummary, tokenChoicesTrivia])

CodeGeneration/Sources/SyntaxSupport/GrammarGenerator.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ struct GrammarGenerator {
1919
///
2020
/// - parameters:
2121
/// - tokenChoice: ``TokenChoice`` to describe
22-
public func grammar(for tokenChoice: TokenChoice) -> String {
22+
private func grammar(for tokenChoice: TokenChoice) -> String {
2323
switch tokenChoice {
2424
case .keyword(let keyword):
25-
return "`'\(keyword.spec.name)'`"
25+
return "`\(keyword.spec.name)`"
2626
case .token(let token):
2727
let tokenSpec = token.spec
2828
if let tokenText = tokenSpec.text {
29-
return "`'\(tokenText)'`"
29+
return "`\(tokenText)`"
3030
} else {
3131
return "`<\(tokenSpec.varOrCaseName)>`"
3232
}
@@ -65,4 +65,25 @@ struct GrammarGenerator {
6565
.map { " - `\($0.varOrCaseName)`: \(generator.grammar(for: $0))" }
6666
.joined(separator: "\n")
6767
}
68+
69+
/// Generates a markdown string describing possible choices for the given child
70+
/// token.
71+
static func childTokenChoices(for choices: [TokenChoice]) -> String {
72+
let grammar = GrammarGenerator()
73+
74+
if choices.count == 1 {
75+
return """
76+
### Tokens
77+
78+
For syntax trees generated by the parser, this is guaranteed to be \(grammar.grammar(for: choices.first!))
79+
"""
80+
} else {
81+
return """
82+
### Tokens
83+
84+
For syntax trees generated by the parser, this is guaranteed to be one of the following kinds:
85+
\(choices.map { " - \(grammar.grammar(for: $0))" }.joined(separator: "\n"))
86+
"""
87+
}
88+
}
6889
}

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
public protocol BracedSyntax: SyntaxProtocol {
1919
/// ### Tokens
2020
///
21-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `'{'`
21+
/// For syntax trees generated by the parser, this is guaranteed to be `{`
2222
var leftBrace: TokenSyntax {
2323
get
2424
set
2525
}
2626

2727
/// ### Tokens
2828
///
29-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `'}'`
29+
/// For syntax trees generated by the parser, this is guaranteed to be `}`
3030
var rightBrace: TokenSyntax {
3131
get
3232
set
@@ -130,8 +130,8 @@ public protocol EffectSpecifiersSyntax: SyntaxProtocol {
130130
/// ### Tokens
131131
///
132132
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds:
133-
/// - `'async'`
134-
/// - `'reasync'`
133+
/// - `async`
134+
/// - `reasync`
135135
var asyncSpecifier: TokenSyntax? {
136136
get
137137
set
@@ -145,8 +145,8 @@ public protocol EffectSpecifiersSyntax: SyntaxProtocol {
145145
/// ### Tokens
146146
///
147147
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds:
148-
/// - `'throws'`
149-
/// - `'rethrows'`
148+
/// - `throws`
149+
/// - `rethrows`
150150
var throwsSpecifier: TokenSyntax? {
151151
get
152152
set
@@ -191,15 +191,15 @@ public extension SyntaxProtocol {
191191
public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
192192
/// ### Tokens
193193
///
194-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `'#'`
194+
/// For syntax trees generated by the parser, this is guaranteed to be `#`
195195
var pound: TokenSyntax {
196196
get
197197
set
198198
}
199199

200200
/// ### Tokens
201201
///
202-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `<identifier>`
202+
/// For syntax trees generated by the parser, this is guaranteed to be `<identifier>`
203203
var macroName: TokenSyntax {
204204
get
205205
set
@@ -212,7 +212,7 @@ public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
212212

213213
/// ### Tokens
214214
///
215-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `'('`
215+
/// For syntax trees generated by the parser, this is guaranteed to be `(`
216216
var leftParen: TokenSyntax? {
217217
get
218218
set
@@ -225,7 +225,7 @@ public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
225225

226226
/// ### Tokens
227227
///
228-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `')'`
228+
/// For syntax trees generated by the parser, this is guaranteed to be `)`
229229
var rightParen: TokenSyntax? {
230230
get
231231
set
@@ -275,7 +275,7 @@ public extension SyntaxProtocol {
275275
public protocol NamedDeclSyntax: SyntaxProtocol {
276276
/// ### Tokens
277277
///
278-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `<identifier>`
278+
/// For syntax trees generated by the parser, this is guaranteed to be `<identifier>`
279279
var name: TokenSyntax {
280280
get
281281
set
@@ -319,7 +319,7 @@ public protocol MissingNodeSyntax: SyntaxProtocol {
319319
///
320320
/// ### Tokens
321321
///
322-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `<identifier>`
322+
/// For syntax trees generated by the parser, this is guaranteed to be `<identifier>`
323323
var placeholder: TokenSyntax {
324324
get
325325
set
@@ -359,15 +359,15 @@ public extension SyntaxProtocol {
359359
public protocol ParenthesizedSyntax: SyntaxProtocol {
360360
/// ### Tokens
361361
///
362-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `'('`
362+
/// For syntax trees generated by the parser, this is guaranteed to be `(`
363363
var leftParen: TokenSyntax {
364364
get
365365
set
366366
}
367367

368368
/// ### Tokens
369369
///
370-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `')'`
370+
/// For syntax trees generated by the parser, this is guaranteed to be `)`
371371
var rightParen: TokenSyntax {
372372
get
373373
set
@@ -601,7 +601,7 @@ public extension SyntaxProtocol {
601601
public protocol WithTrailingCommaSyntax: SyntaxProtocol {
602602
/// ### Tokens
603603
///
604-
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kind: `','`
604+
/// For syntax trees generated by the parser, this is guaranteed to be `,`
605605
var trailingComma: TokenSyntax? {
606606
get
607607
set

0 commit comments

Comments
 (0)