@@ -54,6 +54,15 @@ Filename(cl::Positional, cl::desc("source file"), cl::Required);
5454static cl::opt<unsigned >
5555NumParses (" n" , cl::desc(" number of invocations" ), cl::init(1 ));
5656
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" ));
5766}
5867
5968namespace {
@@ -136,8 +145,14 @@ makeNode(const swiftparse_syntax_node_t *raw_node, StringRef source) {
136145
137146static swiftparse_client_node_t
138147parse (StringRef source, swiftparse_node_handler_t node_handler,
148+ StringRef swift_version, bool enable_bare_slash_regex,
139149 swiftparse_diagnostic_handler_t diag_handler = nullptr ) {
140150 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);
141156 swiftparse_parser_set_node_handler (parser, node_handler);
142157 swiftparse_parser_set_diagnostic_handler (parser, diag_handler);
143158 swiftparse_client_node_t top =
@@ -146,13 +161,15 @@ parse(StringRef source, swiftparse_node_handler_t node_handler,
146161 return top;
147162}
148163
149- static int dumpTree (StringRef source) {
164+ static int dumpTree (StringRef source, StringRef swiftVersion,
165+ bool enableBareSlashRegex) {
150166 swiftparse_node_handler_t nodeHandler =
151167 ^swiftparse_client_node_t (const swiftparse_syntax_node_t *raw_node) {
152168 return makeNode (raw_node, source);
153169 };
154170
155- std::unique_ptr<SPNode> top = convertClientNode (parse (source, nodeHandler));
171+ std::unique_ptr<SPNode> top = convertClientNode (
172+ parse (source, nodeHandler, swiftVersion, enableBareSlashRegex));
156173 top->dump (outs ());
157174
158175 return 0 ;
@@ -217,16 +234,18 @@ static void printDiagInfo(const swiftparser_diagnostic_t diag,
217234}
218235
219236static int dumpDiagnostics (StringRef source, llvm::SourceMgr &SM,
220- unsigned BufferId) {
237+ unsigned BufferId, StringRef swiftVersion,
238+ bool enableBareSlashRegex) {
221239 swiftparse_node_handler_t nodeHandler =
222240 ^swiftparse_client_node_t (const swiftparse_syntax_node_t *raw_node) {
223241 return makeNode (raw_node, source);
224242 };
225243 std::shared_ptr<PrintDiagData> pData = std::make_shared<PrintDiagData>();
226- convertClientNode (parse (source, nodeHandler,
244+ convertClientNode (parse (
245+ source, nodeHandler, swiftVersion, enableBareSlashRegex,
227246 ^(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+ }));
230249 return 0 ;
231250}
232251
@@ -255,7 +274,8 @@ static void printTimeRecord(unsigned numInvoks, const TimeRecord &total,
255274 OS << ' \n ' ;
256275}
257276
258- static int timeParsing (StringRef source, unsigned numInvoks) {
277+ static int timeParsing (StringRef source, unsigned numInvoks,
278+ StringRef swiftVersion, bool enableBareSlashRegex) {
259279 swiftparse_node_handler_t nodeHandler =
260280 ^swiftparse_client_node_t (const swiftparse_syntax_node_t *raw_node) {
261281 return nullptr ;
@@ -264,7 +284,7 @@ static int timeParsing(StringRef source, unsigned numInvoks) {
264284 Timer timer;
265285 timer.startTimer ();
266286 for (unsigned i = 0 ; i != numInvoks; ++i) {
267- parse (source, nodeHandler);
287+ parse (source, nodeHandler, swiftVersion, enableBareSlashRegex );
268288 }
269289 timer.stopTimer ();
270290
@@ -288,10 +308,13 @@ int main(int argc, char *argv[]) {
288308 auto BufferId = SM.AddNewSourceBuffer (std::move (*fileBufOrErr), SMLoc ());
289309 switch (options::Action) {
290310 case ActionType::DumpTree:
291- return dumpTree (source);
311+ return dumpTree (source, options::SwiftVersion,
312+ options::EnableBareSlashRegex);
292313 case ActionType::Time:
293- return timeParsing (source, options::NumParses);
314+ return timeParsing (source, options::NumParses, options::SwiftVersion,
315+ options::EnableBareSlashRegex);
294316 case ActionType::Diagnostics:
295- return dumpDiagnostics (source, SM, BufferId);
317+ return dumpDiagnostics (source, SM, BufferId, options::SwiftVersion,
318+ options::EnableBareSlashRegex);
296319 }
297320}
0 commit comments