@@ -42,6 +42,7 @@ namespace Antlr4.Build.Tasks
42
42
#else
43
43
using Registry = Microsoft . Win32 . Registry ;
44
44
#endif
45
+ using StringBuilder = System . Text . StringBuilder ;
45
46
46
47
internal class AntlrClassGenerationTaskInternal : MarshalByRefObject
47
48
{
@@ -243,11 +244,11 @@ public bool Execute()
243
244
244
245
List < string > arguments = new List < string > ( ) ;
245
246
arguments . Add ( "-cp" ) ;
246
- arguments . Add ( '"' + ToolPath + '"' ) ;
247
+ arguments . Add ( ToolPath ) ;
247
248
arguments . Add ( "org.antlr.v4.CSharpTool" ) ;
248
249
249
250
arguments . Add ( "-o" ) ;
250
- arguments . Add ( '"' + OutputPath + '"' ) ;
251
+ arguments . Add ( OutputPath ) ;
251
252
252
253
if ( GenerateListener )
253
254
arguments . Add ( "-listener" ) ;
@@ -288,7 +289,7 @@ public bool Execute()
288
289
289
290
arguments . AddRange ( SourceCodeFiles ) ;
290
291
291
- ProcessStartInfo startInfo = new ProcessStartInfo ( java , string . Join ( " " , arguments . ToArray ( ) ) )
292
+ ProcessStartInfo startInfo = new ProcessStartInfo ( java , JoinArguments ( arguments ) )
292
293
{
293
294
UseShellExecute = false ,
294
295
CreateNoWindow = true ,
@@ -330,6 +331,35 @@ public bool Execute()
330
331
}
331
332
}
332
333
334
+ private static string JoinArguments ( IEnumerable < string > arguments )
335
+ {
336
+ if ( arguments == null )
337
+ throw new ArgumentNullException ( "arguments" ) ;
338
+
339
+ StringBuilder builder = new StringBuilder ( ) ;
340
+ foreach ( string argument in arguments )
341
+ {
342
+ if ( builder . Length > 0 )
343
+ builder . Append ( ' ' ) ;
344
+
345
+ if ( argument . IndexOfAny ( new [ ] { '"' , ' ' } ) < 0 )
346
+ {
347
+ builder . Append ( argument ) ;
348
+ continue ;
349
+ }
350
+
351
+ // escape a backslash appearing before a quote
352
+ string arg = argument . Replace ( "\\ \" " , "\\ \\ \" " ) ;
353
+ // escape double quotes
354
+ arg = arg . Replace ( "\" " , "\\ \" " ) ;
355
+
356
+ // wrap the argument in outer quotes
357
+ builder . Append ( '"' ) . Append ( arg ) . Append ( '"' ) ;
358
+ }
359
+
360
+ return builder . ToString ( ) ;
361
+ }
362
+
333
363
private static readonly Regex GeneratedFileMessageFormat = new Regex ( @"^Generating file '(?<OUTPUT>.*?)' for grammar '(?<GRAMMAR>.*?)'$" , RegexOptions . Compiled ) ;
334
364
335
365
private void HandleErrorDataReceived ( object sender , DataReceivedEventArgs e )
0 commit comments