@@ -58,6 +58,12 @@ public enum SyntaxParser {
58
58
/// - source: The source string to parse.
59
59
/// - parseTransition: Optional mechanism for incremental re-parsing.
60
60
/// - 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.
61
67
/// - diagnosticHandler: Optional callback that will be called for all
62
68
/// diagnostics the parser emits
63
69
/// - Returns: A top-level Syntax node representing the contents of the tree,
@@ -67,6 +73,8 @@ public enum SyntaxParser {
67
73
source: String ,
68
74
parseTransition: IncrementalParseTransition ? = nil ,
69
75
filenameForDiagnostics: String = " " ,
76
+ languageVersion: String ? = nil ,
77
+ enableBareSlashRegexLiteral: Bool ? = nil ,
70
78
diagnosticHandler: ( ( Diagnostic ) -> Void ) ? = nil
71
79
) throws -> SourceFileSyntax {
72
80
guard nodeHashVerifyResult && cnodeLayoutHashVerifyResult else {
@@ -78,8 +86,12 @@ public enum SyntaxParser {
78
86
var utf8Source = source
79
87
utf8Source. makeContiguousUTF8 ( )
80
88
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)
83
95
84
96
let base = _SyntaxParserInterop. nodeFromRetainedOpaqueRawSyntax ( rawSyntax)
85
97
guard let file = base. as ( SourceFileSyntax . self) else {
@@ -92,33 +104,55 @@ public enum SyntaxParser {
92
104
///
93
105
/// - Parameters:
94
106
/// - 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.
95
113
/// - diagnosticHandler: Optional callback that will be called for all
96
114
/// diagnostics the parser emits
97
115
/// - Returns: A top-level Syntax node representing the contents of the tree,
98
116
/// if the parse was successful.
99
117
/// - 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 {
102
124
// Avoid using `String(contentsOf:)` because it creates a wrapped NSString.
103
125
let fileData = try Data ( contentsOf: url)
104
126
let source = fileData. withUnsafeBytes { buf in
105
127
return String ( decoding: buf. bindMemory ( to: UInt8 . self) , as: UTF8 . self)
106
128
}
107
129
return try parse ( source: source, filenameForDiagnostics: url. path,
130
+ languageVersion: languageVersion,
131
+ enableBareSlashRegexLiteral: enableBareSlashRegexLiteral,
108
132
diagnosticHandler: diagnosticHandler)
109
133
}
110
134
111
135
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 ) ?
116
142
) -> CClientNode {
117
143
precondition ( source. isContiguousUTF8)
118
144
let c_parser = swiftparse_parser_create ( )
119
145
defer {
120
146
swiftparse_parser_dispose ( c_parser)
121
147
}
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
+ }
122
156
123
157
let nodeHandler = { ( cnode: CSyntaxNodePtr!) - > UnsafeMutableRawPointer in
124
158
return _SyntaxParserInterop. getRetainedOpaqueRawSyntax ( cnode: cnode, source: source)
0 commit comments