File tree Expand file tree Collapse file tree 2 files changed +11
-9
lines changed
Sources/SwiftSyntaxBuilderGeneration Expand file tree Collapse file tree 2 files changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ class Child {
26
26
let classification : SyntaxClassification ?
27
27
/// A restricted set of token kinds that will be accepted for this child.
28
28
let tokenChoices : [ Token ]
29
+ let tokenCanContainArbitraryText : Bool
29
30
30
31
var swiftName : String {
31
32
return lowercaseFirstWord ( name: name)
@@ -95,14 +96,18 @@ class Child {
95
96
self . requiresLeadingNewline = requiresLeadingNewline
96
97
self . isOptional = isOptional
97
98
99
+ let isToken = syntaxKind. hasSuffix ( " Token " )
98
100
var mappedTokenChoices = [ Token] ( )
99
101
100
- if syntaxKind . hasSuffix ( " Token " ) , let token = SYNTAX_TOKEN_MAP [ syntaxKind] {
102
+ if isToken , let token = SYNTAX_TOKEN_MAP [ syntaxKind] {
101
103
mappedTokenChoices. append ( token)
102
104
}
103
105
104
- mappedTokenChoices. append ( contentsOf: tokenChoices. compactMap { SYNTAX_TOKEN_MAP [ $0 ] } )
106
+ mappedTokenChoices. append ( contentsOf: tokenChoices. compactMap { SYNTAX_TOKEN_MAP [ " \( $0 ) Token " ] } )
105
107
self . tokenChoices = mappedTokenChoices
108
+
109
+ // If mappedTokenChoices contains `nil`, the token can contain arbitrary text
110
+ self . tokenCanContainArbitraryText = mappedTokenChoices. contains { $0. text == nil }
106
111
107
112
// A list of valid text for tokens, if specified.
108
113
// This will force validation logic to check the text passed into the
Original file line number Diff line number Diff line change @@ -79,14 +79,11 @@ extension Child {
79
79
let choices : [ String ]
80
80
if !textChoices. isEmpty {
81
81
choices = textChoices
82
+ } else if tokenCanContainArbitraryText {
83
+ // Don't generate an assert statement if token can contain arbitrary text.
84
+ return nil
82
85
} 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)
90
87
} else {
91
88
return nil
92
89
}
You can’t perform that action at this time.
0 commit comments