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)
@@ -56,6 +57,19 @@ class SwiftParserTest: ParsableCommand {
56
57
)
57
58
}
58
59
60
+ /// Fold all of the sequences in the given source file.
61
+ func foldAllSequences( _ tree: SourceFileSyntax ) -> ( Syntax , [ Diagnostic ] ) {
62
+ var diags : [ Diagnostic ] = [ ]
63
+
64
+ let recordOperatorError : ( OperatorError ) -> Void = { error in
65
+ diags. append ( error. asDiagnostic)
66
+ }
67
+ var operatorTable = OperatorTable . standardOperators
68
+ operatorTable. addSourceFile ( tree, errorHandler: recordOperatorError)
69
+ let resultTree = operatorTable. foldAll ( tree, errorHandler: recordOperatorError)
70
+ return ( Syntax ( resultTree) , diags)
71
+ }
72
+
59
73
class VerifyRoundTrip : ParsableCommand {
60
74
required init ( ) { }
61
75
@@ -74,6 +88,10 @@ class VerifyRoundTrip: ParsableCommand {
74
88
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
75
89
var enableBareSlashRegex : Bool ?
76
90
91
+ @Flag ( name: . long,
92
+ help: " Perform sequence folding with the standard operators " )
93
+ var foldSequences : Bool = false
94
+
77
95
enum Error : Swift . Error , CustomStringConvertible {
78
96
case roundTripFailed
79
97
@@ -91,21 +109,30 @@ class VerifyRoundTrip: ParsableCommand {
91
109
try source. withUnsafeBufferPointer { sourceBuffer in
92
110
try Self . run (
93
111
source: sourceBuffer, swiftVersion: swiftVersion,
94
- enableBareSlashRegex: enableBareSlashRegex
112
+ enableBareSlashRegex: enableBareSlashRegex,
113
+ foldSequences: foldSequences
95
114
)
96
115
}
97
116
}
98
117
99
118
static func run(
100
119
source: UnsafeBufferPointer < UInt8 > , swiftVersion: String ? ,
101
- enableBareSlashRegex: Bool ?
120
+ enableBareSlashRegex: Bool ? , foldSequences : Bool
102
121
) throws {
103
122
let tree = try Parser . parse (
104
123
source: source,
105
124
languageVersion: swiftVersion,
106
125
enableBareSlashRegexLiteral: enableBareSlashRegex
107
126
)
108
- if tree. syntaxTextBytes != [ UInt8] ( source) {
127
+
128
+ let resultTree : Syntax
129
+ if foldSequences {
130
+ resultTree = foldAllSequences ( tree) . 0
131
+ } else {
132
+ resultTree = Syntax ( tree)
133
+ }
134
+
135
+ if resultTree. syntaxTextBytes != [ UInt8] ( source) {
109
136
throw Error . roundTripFailed
110
137
}
111
138
}
@@ -123,6 +150,10 @@ class PrintDiags: ParsableCommand {
123
150
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
124
151
var enableBareSlashRegex : Bool ?
125
152
153
+ @Flag ( name: . long,
154
+ help: " Perform sequence folding with the standard operators " )
155
+ var foldSequences : Bool = false
156
+
126
157
func run( ) throws {
127
158
let source = try getContentsOfSourceFile ( at: sourceFile)
128
159
@@ -132,7 +163,12 @@ class PrintDiags: ParsableCommand {
132
163
languageVersion: swiftVersion,
133
164
enableBareSlashRegexLiteral: enableBareSlashRegex
134
165
)
135
- let diags = ParseDiagnosticsGenerator . diagnostics ( for: tree)
166
+ var diags = ParseDiagnosticsGenerator . diagnostics ( for: tree)
167
+
168
+ if foldSequences {
169
+ diags += foldAllSequences ( tree) . 1
170
+ }
171
+
136
172
if diags. isEmpty {
137
173
print ( " No diagnostics produced " )
138
174
}
@@ -155,6 +191,10 @@ class DumpTree: ParsableCommand {
155
191
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
156
192
var enableBareSlashRegex : Bool ?
157
193
194
+ @Flag ( name: . long,
195
+ help: " Perform sequence folding with the standard operators " )
196
+ var foldSequences : Bool = false
197
+
158
198
func run( ) throws {
159
199
let source = try getContentsOfSourceFile ( at: sourceFile)
160
200
@@ -164,7 +204,15 @@ class DumpTree: ParsableCommand {
164
204
languageVersion: swiftVersion,
165
205
enableBareSlashRegexLiteral: enableBareSlashRegex
166
206
)
167
- print ( tree. recursiveDescription)
207
+
208
+ let resultTree : Syntax
209
+ if foldSequences {
210
+ resultTree = foldAllSequences ( tree) . 0
211
+ } else {
212
+ resultTree = Syntax ( tree)
213
+ }
214
+
215
+ print ( resultTree. recursiveDescription)
168
216
}
169
217
}
170
218
}
@@ -181,6 +229,10 @@ class Reduce: ParsableCommand {
181
229
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
182
230
var enableBareSlashRegex : Bool ?
183
231
232
+ @Flag ( name: . long,
233
+ help: " Perform sequence folding with the standard operators " )
234
+ var foldSequences : Bool = false
235
+
184
236
@Flag ( help: " Print status updates while reducing the test case " )
185
237
var verbose : Bool = false
186
238
@@ -225,6 +277,10 @@ class Reduce: ParsableCommand {
225
277
" --swift-version " , swiftVersion
226
278
]
227
279
}
280
+ if foldSequences {
281
+ process. arguments! += [ " --fold-sequences " ]
282
+ }
283
+
228
284
let sema = DispatchSemaphore ( value: 0 )
229
285
process. standardOutput = FileHandle . nullDevice
230
286
process. standardError = FileHandle . nullDevice
@@ -257,7 +313,8 @@ class Reduce: ParsableCommand {
257
313
private func runVerifyRoundTripInCurrentProcess( source: [ UInt8 ] ) throws -> Bool {
258
314
do {
259
315
try source. withUnsafeBufferPointer { sourceBuffer in
260
- try VerifyRoundTrip . run ( source: sourceBuffer, swiftVersion: self . swiftVersion, enableBareSlashRegex: self . enableBareSlashRegex)
316
+ try VerifyRoundTrip . run ( source: sourceBuffer, swiftVersion: self . swiftVersion, enableBareSlashRegex: self . enableBareSlashRegex,
317
+ foldSequences: foldSequences)
261
318
}
262
319
} catch {
263
320
return false
0 commit comments