14
14
import SwiftDiagnostics
15
15
import SwiftSyntax
16
16
import SwiftParser
17
+ import SwiftOperators
17
18
import Foundation
18
19
import ArgumentParser
19
20
#if os(Windows)
@@ -64,6 +65,19 @@ class SwiftParserTest: ParsableCommand {
64
65
)
65
66
}
66
67
68
+ /// Fold all of the sequences in the given source file.
69
+ func foldAllSequences( _ tree: SourceFileSyntax ) -> ( Syntax , [ Diagnostic ] ) {
70
+ var diags : [ Diagnostic ] = [ ]
71
+
72
+ let recordOperatorError : ( OperatorError ) -> Void = { error in
73
+ diags. append ( error. asDiagnostic)
74
+ }
75
+ var operatorTable = OperatorTable . standardOperators
76
+ operatorTable. addSourceFile ( tree, errorHandler: recordOperatorError)
77
+ let resultTree = operatorTable. foldAll ( tree, errorHandler: recordOperatorError)
78
+ return ( Syntax ( resultTree) , diags)
79
+ }
80
+
67
81
class VerifyRoundTrip : ParsableCommand {
68
82
required init ( ) { }
69
83
@@ -82,6 +96,10 @@ class VerifyRoundTrip: ParsableCommand {
82
96
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
83
97
var enableBareSlashRegex : Bool ?
84
98
99
+ @Flag ( name: . long,
100
+ help: " Perform sequence folding with the standard operators " )
101
+ var foldSequences : Bool = false
102
+
85
103
enum Error : Swift . Error , CustomStringConvertible {
86
104
case roundTripFailed
87
105
@@ -96,16 +114,24 @@ class VerifyRoundTrip: ParsableCommand {
96
114
func run( ) throws {
97
115
let source = try getContentsOfSourceFile ( at: sourceFile)
98
116
99
- try Self . run ( source: source, swiftVersion: swiftVersion, enableBareSlashRegex: enableBareSlashRegex)
117
+ try Self . run ( source: source, swiftVersion: swiftVersion, enableBareSlashRegex: enableBareSlashRegex, foldSequences : foldSequences )
100
118
}
101
119
102
- static func run( source: String , swiftVersion: String ? , enableBareSlashRegex: Bool ? ) throws {
120
+ static func run( source: String , swiftVersion: String ? , enableBareSlashRegex: Bool ? , foldSequences : Bool ) throws {
103
121
let tree = try Parser . parse (
104
122
source: source,
105
123
languageVersion: swiftVersion,
106
124
enableBareSlashRegexLiteral: enableBareSlashRegex
107
125
)
108
- if tree. description != source {
126
+
127
+ let resultTree : Syntax
128
+ if foldSequences {
129
+ resultTree = foldAllSequences ( tree) . 0
130
+ } else {
131
+ resultTree = Syntax ( tree)
132
+ }
133
+
134
+ if resultTree. description != source {
109
135
throw Error . roundTripFailed
110
136
}
111
137
}
@@ -123,6 +149,10 @@ class PrintDiags: ParsableCommand {
123
149
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
124
150
var enableBareSlashRegex : Bool ?
125
151
152
+ @Flag ( name: . long,
153
+ help: " Perform sequence folding with the standard operators " )
154
+ var foldSequences : Bool = false
155
+
126
156
func run( ) throws {
127
157
let source = try getContentsOfSourceFile ( at: sourceFile)
128
158
@@ -131,7 +161,12 @@ class PrintDiags: ParsableCommand {
131
161
languageVersion: swiftVersion,
132
162
enableBareSlashRegexLiteral: enableBareSlashRegex
133
163
)
134
- let diags = ParseDiagnosticsGenerator . diagnostics ( for: tree)
164
+ var diags = ParseDiagnosticsGenerator . diagnostics ( for: tree)
165
+
166
+ if foldSequences {
167
+ diags += foldAllSequences ( tree) . 1
168
+ }
169
+
135
170
if diags. isEmpty {
136
171
print ( " No diagnostics produced " )
137
172
}
@@ -153,6 +188,10 @@ class DumpTree: ParsableCommand {
153
188
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
154
189
var enableBareSlashRegex : Bool ?
155
190
191
+ @Flag ( name: . long,
192
+ help: " Perform sequence folding with the standard operators " )
193
+ var foldSequences : Bool = false
194
+
156
195
func run( ) throws {
157
196
let source = try getContentsOfSourceFile ( at: sourceFile)
158
197
@@ -161,7 +200,14 @@ class DumpTree: ParsableCommand {
161
200
languageVersion: swiftVersion,
162
201
enableBareSlashRegexLiteral: enableBareSlashRegex
163
202
)
164
- print ( tree. recursiveDescription)
203
+
204
+ let resultTree : Syntax
205
+ if foldSequences {
206
+ resultTree = foldAllSequences ( tree) . 0
207
+ } else {
208
+ resultTree = Syntax ( tree)
209
+ }
210
+ print ( resultTree. recursiveDescription)
165
211
}
166
212
}
167
213
@@ -177,6 +223,10 @@ class Reduce: ParsableCommand {
177
223
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
178
224
var enableBareSlashRegex : Bool ?
179
225
226
+ @Flag ( name: . long,
227
+ help: " Perform sequence folding with the standard operators " )
228
+ var foldSequences : Bool = false
229
+
180
230
@Flag ( help: " Print status updates while reducing the test case " )
181
231
var verbose : Bool = false
182
232
@@ -221,6 +271,10 @@ class Reduce: ParsableCommand {
221
271
" --swift-version " , swiftVersion
222
272
]
223
273
}
274
+ if foldSequences {
275
+ process. arguments! += [ " --fold-sequences " ]
276
+ }
277
+
224
278
let sema = DispatchSemaphore ( value: 0 )
225
279
process. standardOutput = FileHandle . nullDevice
226
280
process. standardError = FileHandle . nullDevice
@@ -252,7 +306,8 @@ class Reduce: ParsableCommand {
252
306
/// Returns `true` if `source` round-tripped successfully, `false` otherwise.
253
307
private func runVerifyRoundTripInCurrentProcess( source: String ) throws -> Bool {
254
308
do {
255
- try VerifyRoundTrip . run ( source: source, swiftVersion: self . swiftVersion, enableBareSlashRegex: self . enableBareSlashRegex)
309
+ try VerifyRoundTrip . run ( source: source, swiftVersion: self . swiftVersion, enableBareSlashRegex: self . enableBareSlashRegex,
310
+ foldSequences: self . foldSequences)
256
311
} catch {
257
312
return false
258
313
}
0 commit comments