Skip to content

Commit 217363d

Browse files
committed
Add tokenCanContainArbitraryText
1 parent 719d14c commit 217363d

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Sources/SwiftSyntaxBuilderGeneration/Child.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Child {
2626
let classification: SyntaxClassification?
2727
/// A restricted set of token kinds that will be accepted for this child.
2828
let tokenChoices: [Token]
29+
let tokenCanContainArbitraryText: Bool
2930

3031
var swiftName: String {
3132
return lowercaseFirstWord(name: name)
@@ -95,14 +96,18 @@ class Child {
9596
self.requiresLeadingNewline = requiresLeadingNewline
9697
self.isOptional = isOptional
9798

98-
var mappedTokenChoices = [Token]()
99+
let isToken = syntaxKind.hasSuffix("Token")
100+
var mappedTokenChoices = [Token?]()
99101

100-
if syntaxKind.hasSuffix("Token"), let token = SYNTAX_TOKEN_MAP[syntaxKind] {
102+
if isToken, let token = SYNTAX_TOKEN_MAP[syntaxKind] {
101103
mappedTokenChoices.append(token)
102104
}
103105

104-
mappedTokenChoices.append(contentsOf: tokenChoices.compactMap { SYNTAX_TOKEN_MAP["\($0)Token"] })
105-
self.tokenChoices = mappedTokenChoices
106+
mappedTokenChoices.append(contentsOf: tokenChoices.map { SYNTAX_TOKEN_MAP["\($0)Token"] })
107+
self.tokenChoices = mappedTokenChoices.compactMap { $0 }
108+
109+
// If mappedTokenChoices contains `nil`, the token can contain arbitrary text
110+
self.tokenCanContainArbitraryText = mappedTokenChoices.count != self.tokenChoices.count
106111

107112
// A list of valid text for tokens, if specified.
108113
// This will force validation logic to check the text passed into the

Sources/SwiftSyntaxBuilderGeneration/SyntaxBuildableChild.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ extension Child {
7979
let choices: [String]
8080
if !textChoices.isEmpty {
8181
choices = textChoices
82+
} else if tokenCanContainArbitraryText {
83+
// Don't generate an assert statement if token can contain arbitrary text.
84+
return nil
8285
} else if !tokenChoices.isEmpty {
83-
let optionalChoices = tokenChoices.map { SYNTAX_TOKEN_MAP["\($0.name)Token"]?.text }
84-
choices = optionalChoices.compactMap { $0 }
85-
guard choices.count == optionalChoices.count else {
86-
// If `nil` is in the text choices, one of the token options can contain arbitrary
87-
// text. Don't generate an assert statement.
88-
return nil
89-
}
86+
choices = tokenChoices.compactMap(\.text)
9087
} else {
9188
return nil
9289
}

0 commit comments

Comments
 (0)