@@ -54,6 +54,15 @@ Filename(cl::Positional, cl::desc("source file"), cl::Required);
54
54
static cl::opt<unsigned >
55
55
NumParses (" n" , cl::desc(" number of invocations" ), cl::init(1 ));
56
56
57
+ static cl::opt<std::string>
58
+ SwiftVersion (" swift-version" ,
59
+ cl::desc (" Interpret input according to a specific Swift "
60
+ " language version number" ));
61
+
62
+ static cl::opt<bool >
63
+ EnableBareSlashRegex (" enable-bare-slash-regex" ,
64
+ cl::desc (" Enable or disable the use of forward slash "
65
+ " regular-expression literal syntax" ));
57
66
}
58
67
59
68
namespace {
@@ -136,8 +145,14 @@ makeNode(const swiftparse_syntax_node_t *raw_node, StringRef source) {
136
145
137
146
static swiftparse_client_node_t
138
147
parse (StringRef source, swiftparse_node_handler_t node_handler,
148
+ StringRef swift_version, bool enable_bare_slash_regex,
139
149
swiftparse_diagnostic_handler_t diag_handler = nullptr ) {
140
150
swiftparse_parser_t parser = swiftparse_parser_create ();
151
+ if (!swift_version.empty ()) {
152
+ swiftparse_parser_set_language_version (parser, swift_version.str ().c_str ());
153
+ }
154
+ swiftparse_parser_set_enable_bare_slash_regex_literal (
155
+ parser, enable_bare_slash_regex);
141
156
swiftparse_parser_set_node_handler (parser, node_handler);
142
157
swiftparse_parser_set_diagnostic_handler (parser, diag_handler);
143
158
swiftparse_client_node_t top =
@@ -146,13 +161,15 @@ parse(StringRef source, swiftparse_node_handler_t node_handler,
146
161
return top;
147
162
}
148
163
149
- static int dumpTree (StringRef source) {
164
+ static int dumpTree (StringRef source, StringRef swiftVersion,
165
+ bool enableBareSlashRegex) {
150
166
swiftparse_node_handler_t nodeHandler =
151
167
^swiftparse_client_node_t (const swiftparse_syntax_node_t *raw_node) {
152
168
return makeNode (raw_node, source);
153
169
};
154
170
155
- std::unique_ptr<SPNode> top = convertClientNode (parse (source, nodeHandler));
171
+ std::unique_ptr<SPNode> top = convertClientNode (
172
+ parse (source, nodeHandler, swiftVersion, enableBareSlashRegex));
156
173
top->dump (outs ());
157
174
158
175
return 0 ;
@@ -217,16 +234,18 @@ static void printDiagInfo(const swiftparser_diagnostic_t diag,
217
234
}
218
235
219
236
static int dumpDiagnostics (StringRef source, llvm::SourceMgr &SM,
220
- unsigned BufferId) {
237
+ unsigned BufferId, StringRef swiftVersion,
238
+ bool enableBareSlashRegex) {
221
239
swiftparse_node_handler_t nodeHandler =
222
240
^swiftparse_client_node_t (const swiftparse_syntax_node_t *raw_node) {
223
241
return makeNode (raw_node, source);
224
242
};
225
243
std::shared_ptr<PrintDiagData> pData = std::make_shared<PrintDiagData>();
226
- convertClientNode (parse (source, nodeHandler,
244
+ convertClientNode (parse (
245
+ source, nodeHandler, swiftVersion, enableBareSlashRegex,
227
246
^(const swiftparser_diagnostic_t diag) {
228
- printDiagInfo (diag, SM, BufferId, const_cast <PrintDiagData&>(*pData));
229
- }));
247
+ printDiagInfo (diag, SM, BufferId, const_cast <PrintDiagData &>(*pData));
248
+ }));
230
249
return 0 ;
231
250
}
232
251
@@ -255,7 +274,8 @@ static void printTimeRecord(unsigned numInvoks, const TimeRecord &total,
255
274
OS << ' \n ' ;
256
275
}
257
276
258
- static int timeParsing (StringRef source, unsigned numInvoks) {
277
+ static int timeParsing (StringRef source, unsigned numInvoks,
278
+ StringRef swiftVersion, bool enableBareSlashRegex) {
259
279
swiftparse_node_handler_t nodeHandler =
260
280
^swiftparse_client_node_t (const swiftparse_syntax_node_t *raw_node) {
261
281
return nullptr ;
@@ -264,7 +284,7 @@ static int timeParsing(StringRef source, unsigned numInvoks) {
264
284
Timer timer;
265
285
timer.startTimer ();
266
286
for (unsigned i = 0 ; i != numInvoks; ++i) {
267
- parse (source, nodeHandler);
287
+ parse (source, nodeHandler, swiftVersion, enableBareSlashRegex );
268
288
}
269
289
timer.stopTimer ();
270
290
@@ -288,10 +308,13 @@ int main(int argc, char *argv[]) {
288
308
auto BufferId = SM.AddNewSourceBuffer (std::move (*fileBufOrErr), SMLoc ());
289
309
switch (options::Action) {
290
310
case ActionType::DumpTree:
291
- return dumpTree (source);
311
+ return dumpTree (source, options::SwiftVersion,
312
+ options::EnableBareSlashRegex);
292
313
case ActionType::Time:
293
- return timeParsing (source, options::NumParses);
314
+ return timeParsing (source, options::NumParses, options::SwiftVersion,
315
+ options::EnableBareSlashRegex);
294
316
case ActionType::Diagnostics:
295
- return dumpDiagnostics (source, SM, BufferId);
317
+ return dumpDiagnostics (source, SM, BufferId, options::SwiftVersion,
318
+ options::EnableBareSlashRegex);
296
319
}
297
320
}
0 commit comments