@@ -58,6 +58,12 @@ public enum SyntaxParser {
5858 /// - source: The source string to parse.
5959 /// - parseTransition: Optional mechanism for incremental re-parsing.
6060 /// - filenameForDiagnostics: Optional file name used for SourceLocation.
61+ /// - languageVersion: Interpret input according to a specific Swift
62+ /// language version number. If `nil`, this inherits the default from
63+ /// the syntax parser library.
64+ /// - enableBareSlashRegexLiteral: Enable or disable the use of forward
65+ /// slash regular-expression literal syntax. If `nil`, this inherits the
66+ /// default from the syntax parser library.
6167 /// - diagnosticHandler: Optional callback that will be called for all
6268 /// diagnostics the parser emits
6369 /// - Returns: A top-level Syntax node representing the contents of the tree,
@@ -67,6 +73,8 @@ public enum SyntaxParser {
6773 source: String ,
6874 parseTransition: IncrementalParseTransition ? = nil ,
6975 filenameForDiagnostics: String = " " ,
76+ languageVersion: String ? = nil ,
77+ enableBareSlashRegexLiteral: Bool ? = nil ,
7078 diagnosticHandler: ( ( Diagnostic ) -> Void ) ? = nil
7179 ) throws -> SourceFileSyntax {
7280 guard nodeHashVerifyResult && cnodeLayoutHashVerifyResult else {
@@ -78,8 +86,12 @@ public enum SyntaxParser {
7886 var utf8Source = source
7987 utf8Source. makeContiguousUTF8 ( )
8088
81- let rawSyntax = parseRaw ( utf8Source, parseTransition, filenameForDiagnostics,
82- diagnosticHandler)
89+ let rawSyntax = parseRaw ( source: utf8Source,
90+ parseTransition: parseTransition,
91+ filenameForDiagnostics: filenameForDiagnostics,
92+ languageVersion: languageVersion,
93+ enableBareSlashRegexLiteral: enableBareSlashRegexLiteral,
94+ diagnosticHandler: diagnosticHandler)
8395
8496 let base = _SyntaxParserInterop. nodeFromRetainedOpaqueRawSyntax ( rawSyntax)
8597 guard let file = base. as ( SourceFileSyntax . self) else {
@@ -92,33 +104,55 @@ public enum SyntaxParser {
92104 ///
93105 /// - Parameters:
94106 /// - url: The file URL to parse.
107+ /// - languageVersion: Interpret input according to a specific Swift
108+ /// language version number. If `nil`, this inherits the default from
109+ /// the syntax parser library.
110+ /// - enableBareSlashRegexLiteral: Enable or disable the use of forward
111+ /// slash regular-expression literal syntax. If `nil`, this inherits the
112+ /// default from the syntax parser library.
95113 /// - diagnosticHandler: Optional callback that will be called for all
96114 /// diagnostics the parser emits
97115 /// - Returns: A top-level Syntax node representing the contents of the tree,
98116 /// if the parse was successful.
99117 /// - Throws: `ParserError`
100- public static func parse( _ url: URL ,
101- diagnosticHandler: ( ( Diagnostic ) -> Void ) ? = nil ) throws -> SourceFileSyntax {
118+ public static func parse(
119+ _ url: URL ,
120+ languageVersion: String ? = nil ,
121+ enableBareSlashRegexLiteral: Bool ? = nil ,
122+ diagnosticHandler: ( ( Diagnostic ) -> Void ) ? = nil
123+ ) throws -> SourceFileSyntax {
102124 // Avoid using `String(contentsOf:)` because it creates a wrapped NSString.
103125 let fileData = try Data ( contentsOf: url)
104126 let source = fileData. withUnsafeBytes { buf in
105127 return String ( decoding: buf. bindMemory ( to: UInt8 . self) , as: UTF8 . self)
106128 }
107129 return try parse ( source: source, filenameForDiagnostics: url. path,
130+ languageVersion: languageVersion,
131+ enableBareSlashRegexLiteral: enableBareSlashRegexLiteral,
108132 diagnosticHandler: diagnosticHandler)
109133 }
110134
111135 private static func parseRaw(
112- _ source: String ,
113- _ parseTransition: IncrementalParseTransition ? ,
114- _ filenameForDiagnostics: String ,
115- _ diagnosticHandler: ( ( Diagnostic ) -> Void ) ?
136+ source: String ,
137+ parseTransition: IncrementalParseTransition ? ,
138+ filenameForDiagnostics: String ,
139+ languageVersion: String ? ,
140+ enableBareSlashRegexLiteral: Bool ? ,
141+ diagnosticHandler: ( ( Diagnostic ) -> Void ) ?
116142 ) -> CClientNode {
117143 precondition ( source. isContiguousUTF8)
118144 let c_parser = swiftparse_parser_create ( )
119145 defer {
120146 swiftparse_parser_dispose ( c_parser)
121147 }
148+ if let languageVersion = languageVersion {
149+ languageVersion. withCString { languageVersionCString in
150+ swiftparse_parser_set_language_version ( c_parser, languageVersionCString)
151+ }
152+ }
153+ if let enableBareSlashRegexLiteral = enableBareSlashRegexLiteral {
154+ swiftparse_parser_set_enable_bare_slash_regex_literal ( c_parser, enableBareSlashRegexLiteral)
155+ }
122156
123157 let nodeHandler = { ( cnode: CSyntaxNodePtr!) - > UnsafeMutableRawPointer in
124158 return _SyntaxParserInterop. getRetainedOpaqueRawSyntax ( cnode: cnode, source: source)
0 commit comments