@@ -71,7 +71,7 @@ extension Child {
71
71
/// If this node is a token that can't contain arbitrary text, generate a Swift
72
72
/// `assert` statement that verifies the variable with name var_name and of type
73
73
/// `TokenSyntax` contains one of the supported text options. Otherwise return `nil`.
74
- func generateAssertStmtTextChoices( varName: String ) -> String ? {
74
+ func generateAssertStmtTextChoices( varName: String ) -> ExpressibleAsExprBuildable ? {
75
75
guard type. isToken else {
76
76
return nil
77
77
}
@@ -91,14 +91,24 @@ extension Child {
91
91
return nil
92
92
}
93
93
94
- var assertChoices : [ String ] = [ ]
94
+ var assertChoices : [ ExpressibleAsExprBuildable ] = [ ]
95
95
if type. isOptional {
96
- assertChoices. append ( " \( varName) == nil " )
96
+ assertChoices. append ( SequenceExpr {
97
+ varName
98
+ BinaryOperatorExpr ( " == " )
99
+ NilLiteralExpr ( )
100
+ } )
97
101
}
98
- let unwrap = type. isOptional ? " ! " : " "
99
102
for textChoice in choices {
100
- assertChoices. append ( " \( varName) \( unwrap) == \" \( textChoice) \" " )
103
+ assertChoices. append ( SequenceExpr {
104
+ type. forceUnwrapped ( expr: varName)
105
+ BinaryOperatorExpr ( " == " )
106
+ StringLiteralExpr ( textChoice. replacingOccurrences ( of: " \\ " , with: " \\ \\ " ) )
107
+ } )
108
+ }
109
+ let disjunction = ExprList ( assertChoices. flatMap { [ $0, BinaryOperatorExpr ( " || " ) ] } . dropLast ( ) )
110
+ return FunctionCallExpr ( " assert " ) {
111
+ SequenceExpr ( elements: disjunction)
101
112
}
102
- return " assert( \( assertChoices. joined ( separator: " || " ) ) ) "
103
113
}
104
114
}
0 commit comments