@@ -117,6 +117,9 @@ public struct Parser {
117117 /// Parser should own a ``LookaheadTracker`` so that we can share one `furthestOffset` in a parse.
118118 let lookaheadTrackerOwner : LookaheadTrackerOwner
119119
120+ /// The Swift version as which this source file should be parsed.
121+ let swiftVersion : SwiftVersion
122+
120123 /// The experimental features that have been enabled.
121124 let experimentalFeatures : ExperimentalFeatures
122125
@@ -129,6 +132,9 @@ public struct Parser {
129132 static let defaultMaximumNestingLevel = 256
130133 #endif
131134
135+ /// The Swift version as which source files should be parsed if no Swift version is explicitly specified in the parser.
136+ static let defaultSwiftVersion : SwiftVersion = . v6
137+
132138 var _emptyRawMultipleTrailingClosureElementListSyntax : RawMultipleTrailingClosureElementListSyntax ?
133139
134140 /// Create an empty collection of the given type.
@@ -187,26 +193,28 @@ public struct Parser {
187193 /// arena is created automatically, and `input` copied into the
188194 /// arena. If non-`nil`, `input` must be within its registered
189195 /// source buffer or allocator.
196+ /// - swiftVersion: The version of Swift using which the file should be parsed.
197+ /// Defaults to the latest version.
190198 /// - experimentalFeatures: The experimental features enabled for the parser.
191199 private init (
192200 buffer input: UnsafeBufferPointer < UInt8 > ,
193201 maximumNestingLevel: Int ? ,
194202 parseTransition: IncrementalParseTransition ? ,
195203 arena: ParsingSyntaxArena ? ,
204+ swiftVersion: SwiftVersion ? ,
196205 experimentalFeatures: ExperimentalFeatures
197206 ) {
198207 var input = input
199208 if let arena {
200209 self . arena = arena
201210 precondition ( arena. contains ( text: SyntaxText ( baseAddress: input. baseAddress, count: input. count) ) )
202211 } else {
203- self . arena = ParsingSyntaxArena (
204- parseTriviaFunction: TriviaParser . parseTrivia ( _: position: )
205- )
212+ self . arena = ParsingSyntaxArena ( parseTriviaFunction: TriviaParser . parseTrivia)
206213 input = self . arena. internSourceBuffer ( input)
207214 }
208215
209216 self . maximumNestingLevel = maximumNestingLevel ?? Self . defaultMaximumNestingLevel
217+ self . swiftVersion = swiftVersion ?? Self . defaultSwiftVersion
210218 self . experimentalFeatures = experimentalFeatures
211219 self . lookaheadTrackerOwner = LookaheadTrackerOwner ( )
212220
@@ -224,6 +232,7 @@ public struct Parser {
224232 string input: String ,
225233 maximumNestingLevel: Int ? ,
226234 parseTransition: IncrementalParseTransition ? ,
235+ swiftVersion: SwiftVersion ? ,
227236 experimentalFeatures: ExperimentalFeatures
228237 ) {
229238 var input = input
@@ -234,6 +243,7 @@ public struct Parser {
234243 maximumNestingLevel: maximumNestingLevel,
235244 parseTransition: parseTransition,
236245 arena: nil ,
246+ swiftVersion: swiftVersion,
237247 experimentalFeatures: experimentalFeatures
238248 )
239249 }
@@ -243,13 +253,15 @@ public struct Parser {
243253 public init (
244254 _ input: String ,
245255 maximumNestingLevel: Int ? = nil ,
246- parseTransition: IncrementalParseTransition ? = nil
256+ parseTransition: IncrementalParseTransition ? = nil ,
257+ swiftVersion: SwiftVersion ? = nil
247258 ) {
248259 // Chain to the private String initializer.
249260 self . init (
250261 string: input,
251262 maximumNestingLevel: maximumNestingLevel,
252263 parseTransition: parseTransition,
264+ swiftVersion: swiftVersion,
253265 experimentalFeatures: [ ]
254266 )
255267 }
@@ -277,14 +289,16 @@ public struct Parser {
277289 _ input: UnsafeBufferPointer < UInt8 > ,
278290 maximumNestingLevel: Int ? = nil ,
279291 parseTransition: IncrementalParseTransition ? = nil ,
280- arena: ParsingSyntaxArena ? = nil
292+ arena: ParsingSyntaxArena ? = nil ,
293+ swiftVersion: SwiftVersion ? = nil
281294 ) {
282295 // Chain to the private buffer initializer.
283296 self . init (
284297 buffer: input,
285298 maximumNestingLevel: maximumNestingLevel,
286299 parseTransition: parseTransition,
287300 arena: arena,
301+ swiftVersion: swiftVersion,
288302 experimentalFeatures: [ ]
289303 )
290304 }
@@ -296,13 +310,15 @@ public struct Parser {
296310 _ input: String ,
297311 maximumNestingLevel: Int ? = nil ,
298312 parseTransition: IncrementalParseTransition ? = nil ,
313+ swiftVersion: SwiftVersion ? = nil ,
299314 experimentalFeatures: ExperimentalFeatures
300315 ) {
301316 // Chain to the private String initializer.
302317 self . init (
303318 string: input,
304319 maximumNestingLevel: maximumNestingLevel,
305320 parseTransition: parseTransition,
321+ swiftVersion: swiftVersion,
306322 experimentalFeatures: experimentalFeatures
307323 )
308324 }
@@ -315,6 +331,7 @@ public struct Parser {
315331 maximumNestingLevel: Int ? = nil ,
316332 parseTransition: IncrementalParseTransition ? = nil ,
317333 arena: ParsingSyntaxArena ? = nil ,
334+ swiftVersion: SwiftVersion ? = nil ,
318335 experimentalFeatures: ExperimentalFeatures
319336 ) {
320337 // Chain to the private buffer initializer.
@@ -323,6 +340,7 @@ public struct Parser {
323340 maximumNestingLevel: maximumNestingLevel,
324341 parseTransition: parseTransition,
325342 arena: arena,
343+ swiftVersion: swiftVersion,
326344 experimentalFeatures: experimentalFeatures
327345 )
328346 }
0 commit comments