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)
@@ -51,6 +52,19 @@ class SwiftParserTest: ParsableCommand {
51
52
)
52
53
}
53
54
55
+ /// Fold all of the sequences in the given source file.
56
+ func foldAllSequences( _ tree: SourceFileSyntax ) -> ( Syntax , [ Diagnostic ] ) {
57
+ var diags : [ Diagnostic ] = [ ]
58
+
59
+ let recordOperatorError : ( OperatorError ) -> Void = { error in
60
+ diags. append ( error. asDiagnostic)
61
+ }
62
+ var operatorTable = OperatorTable . standardOperators
63
+ operatorTable. addSourceFile ( tree, errorHandler: recordOperatorError)
64
+ let resultTree = operatorTable. foldAll ( tree, errorHandler: recordOperatorError)
65
+ return ( Syntax ( resultTree) , diags)
66
+ }
67
+
54
68
class VerifyRoundTrip : ParsableCommand {
55
69
required init ( ) { }
56
70
@@ -69,6 +83,10 @@ class VerifyRoundTrip: ParsableCommand {
69
83
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
70
84
var enableBareSlashRegex : Bool ?
71
85
86
+ @Flag ( name: . long,
87
+ help: " Perform sequence folding with the standard operators " )
88
+ var foldSequences : Bool = false
89
+
72
90
enum Error : Swift . Error , CustomStringConvertible {
73
91
case roundTripFailed
74
92
@@ -86,21 +104,30 @@ class VerifyRoundTrip: ParsableCommand {
86
104
try source. withUnsafeBufferPointer { sourceBuffer in
87
105
try Self . run (
88
106
source: sourceBuffer, swiftVersion: swiftVersion,
89
- enableBareSlashRegex: enableBareSlashRegex
107
+ enableBareSlashRegex: enableBareSlashRegex,
108
+ foldSequences: foldSequences
90
109
)
91
110
}
92
111
}
93
112
94
113
static func run(
95
114
source: UnsafeBufferPointer < UInt8 > , swiftVersion: String ? ,
96
- enableBareSlashRegex: Bool ?
115
+ enableBareSlashRegex: Bool ? , foldSequences : Bool
97
116
) throws {
98
117
let tree = try Parser . parse (
99
118
source: source,
100
119
languageVersion: swiftVersion,
101
120
enableBareSlashRegexLiteral: enableBareSlashRegex
102
121
)
103
- if tree. syntaxTextBytes != [ UInt8] ( source) {
122
+
123
+ let resultTree : Syntax
124
+ if foldSequences {
125
+ resultTree = foldAllSequences ( tree) . 0
126
+ } else {
127
+ resultTree = Syntax ( tree)
128
+ }
129
+
130
+ if resultTree. syntaxTextBytes != [ UInt8] ( source) {
104
131
throw Error . roundTripFailed
105
132
}
106
133
}
@@ -118,6 +145,10 @@ class PrintDiags: ParsableCommand {
118
145
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
119
146
var enableBareSlashRegex : Bool ?
120
147
148
+ @Flag ( name: . long,
149
+ help: " Perform sequence folding with the standard operators " )
150
+ var foldSequences : Bool = false
151
+
121
152
func run( ) throws {
122
153
let source = try getContentsOfSourceFile ( at: sourceFile)
123
154
@@ -127,7 +158,12 @@ class PrintDiags: ParsableCommand {
127
158
languageVersion: swiftVersion,
128
159
enableBareSlashRegexLiteral: enableBareSlashRegex
129
160
)
130
- let diags = ParseDiagnosticsGenerator . diagnostics ( for: tree)
161
+ var diags = ParseDiagnosticsGenerator . diagnostics ( for: tree)
162
+
163
+ if foldSequences {
164
+ diags += foldAllSequences ( tree) . 1
165
+ }
166
+
131
167
if diags. isEmpty {
132
168
print ( " No diagnostics produced " )
133
169
}
@@ -150,6 +186,10 @@ class DumpTree: ParsableCommand {
150
186
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
151
187
var enableBareSlashRegex : Bool ?
152
188
189
+ @Flag ( name: . long,
190
+ help: " Perform sequence folding with the standard operators " )
191
+ var foldSequences : Bool = false
192
+
153
193
func run( ) throws {
154
194
let source = try getContentsOfSourceFile ( at: sourceFile)
155
195
@@ -159,7 +199,15 @@ class DumpTree: ParsableCommand {
159
199
languageVersion: swiftVersion,
160
200
enableBareSlashRegexLiteral: enableBareSlashRegex
161
201
)
162
- print ( tree. recursiveDescription)
202
+
203
+ let resultTree : Syntax
204
+ if foldSequences {
205
+ resultTree = foldAllSequences ( tree) . 0
206
+ } else {
207
+ resultTree = Syntax ( tree)
208
+ }
209
+
210
+ print ( resultTree. recursiveDescription)
163
211
}
164
212
}
165
213
}
@@ -176,6 +224,10 @@ class Reduce: ParsableCommand {
176
224
@Option ( name: . long, help: " Enable or disable the use of forward slash regular-expression literal syntax " )
177
225
var enableBareSlashRegex : Bool ?
178
226
227
+ @Flag ( name: . long,
228
+ help: " Perform sequence folding with the standard operators " )
229
+ var foldSequences : Bool = false
230
+
179
231
@Flag ( help: " Print status updates while reducing the test case " )
180
232
var verbose : Bool = false
181
233
@@ -220,6 +272,10 @@ class Reduce: ParsableCommand {
220
272
" --swift-version " , swiftVersion
221
273
]
222
274
}
275
+ if foldSequences {
276
+ process. arguments! += [ " --fold-sequences " ]
277
+ }
278
+
223
279
let sema = DispatchSemaphore ( value: 0 )
224
280
process. standardOutput = FileHandle . nullDevice
225
281
process. standardError = FileHandle . nullDevice
@@ -252,7 +308,8 @@ class Reduce: ParsableCommand {
252
308
private func runVerifyRoundTripInCurrentProcess( source: [ UInt8 ] ) throws -> Bool {
253
309
do {
254
310
try source. withUnsafeBufferPointer { sourceBuffer in
255
- try VerifyRoundTrip . run ( source: sourceBuffer, swiftVersion: self . swiftVersion, enableBareSlashRegex: self . enableBareSlashRegex)
311
+ try VerifyRoundTrip . run ( source: sourceBuffer, swiftVersion: self . swiftVersion, enableBareSlashRegex: self . enableBareSlashRegex,
312
+ foldSequences: foldSequences)
256
313
}
257
314
} catch {
258
315
return false
0 commit comments