1 package polyglot.main; 2 import java.io.File ; 3 import java.io.PrintStream ; 4 import java.util.Collection ; 5 import java.util.HashSet ; 6 import java.util.Iterator ; 7 import java.util.LinkedList ; 8 import java.util.Set ; 9 import java.util.StringTokenizer ; 10 11 import polyglot.frontend.ExtensionInfo; 12 13 16 public class Options { 17 21 public static Options global; 22 23 26 protected ExtensionInfo extension = null; 27 28 31 public int error_count = 100; 32 public Collection source_path; public File output_directory; 34 public String default_classpath; 35 public String classpath; 36 public String bootclasspath = null; 37 public boolean assertions = false; 38 39 public String [] source_ext = null; public String output_ext = "java"; public boolean output_stdout = false; public String post_compiler; 43 45 public int output_width = 120; 46 public boolean fully_qualified_names = false; 47 48 49 public boolean serialize_type_info = true; 50 51 52 public Set dump_ast = new HashSet (); 53 54 55 public Set print_ast = new HashSet (); 56 57 58 public Set disable_passes = new HashSet (); 59 60 61 public boolean keep_output_files = true; 62 63 64 67 public Options(ExtensionInfo extension) { 68 this.extension = extension; 69 setDefaultValues(); 70 } 71 72 75 public void setDefaultValues() { 76 String default_bootpath = System.getProperty("sun.boot.class.path"); 77 if (default_bootpath == null) { 78 default_bootpath = System.getProperty("java.home") + 79 File.separator + "jre" + 80 File.separator + "lib" + 81 File.separator + "rt.jar"; 82 } 83 84 default_classpath = System.getProperty("java.class.path") + 85 File.pathSeparator + default_bootpath; 86 classpath = default_classpath; 87 88 89 String java_home = System.getProperty("java.home"); 90 String current_dir = System.getProperty("user.dir"); 91 92 source_path = new LinkedList (); 93 source_path.add(new File (current_dir)); 94 95 output_directory = new File (current_dir); 96 97 post_compiler = java_home + File.separator + ".." + File.separator + 106 "bin" + File.separator + "javac"; 107 108 if (! new File (post_compiler).exists()) { 109 post_compiler = java_home + File.separator + 110 "bin" + File.separator + "javac"; 111 112 if (! new File (post_compiler).exists()) { 113 post_compiler = "javac"; 114 } 115 } 116 } 117 118 123 public void parseCommandLine(String args[], Set source) throws UsageError { 124 if(args.length < 1) { 125 throw new UsageError("No command line arguments given"); 126 } 127 128 for(int i = 0; i < args.length; ) { 129 try { 130 int ni = parseCommand(args, i, source); 131 if (ni == i) { 132 throw new UsageError("illegal option -- " + args[i]); 133 } 134 135 i = ni; 136 137 } 138 catch (ArrayIndexOutOfBoundsException e) { 139 throw new UsageError("missing argument"); 140 } 141 } 142 143 if (source.size() < 1) { 144 throw new UsageError("must specify at least one source file"); 145 } 146 } 147 148 153 protected int parseCommand(String args[], int index, Set source) 154 throws UsageError, Main.TerminationException { 155 int i = index; 156 if (args[i].equals("-h") || 157 args[i].equals("-help") || 158 args[i].equals("--help")) { 159 throw new UsageError("", 0); 160 } 161 else if (args[i].equals("-version")) { 162 StringBuffer sb = new StringBuffer (); 163 if (extension != null) { 164 sb.append(extension.compilerName() + 165 " version " + extension.version() + "\n"); 166 } 167 sb.append("Polyglot compiler toolkit version " + 168 new polyglot.ext.jl.Version()); 169 throw new Main.TerminationException(sb.toString(), 0); 170 } 171 else if (args[i].equals("-d")) 172 { 173 i++; 174 output_directory = new File (args[i]); 175 i++; 176 } 177 else if (args[i].equals("-classpath") || 178 args[i].equals("-cp")) { 179 i++; 180 classpath = args[i] + System.getProperty("path.separator") + 181 default_classpath; 182 i++; 183 } 184 else if (args[i].equals("-bootclasspath")) { 185 i++; 186 bootclasspath = args[i]; 187 i++; 188 } 189 else if (args[i].equals("-sourcepath")) 190 { 191 i++; 192 StringTokenizer st = new StringTokenizer (args[i], File.pathSeparator); 193 while(st.hasMoreTokens()) 194 { 195 File f = new File (st.nextToken()); 196 if (f != null && !source_path.contains(f)) 197 source_path.add(f); 198 } 199 i++; 200 } 201 else if (args[i].equals("-assert")) 202 { 203 i++; 204 assertions = true; 205 } 206 else if (args[i].equals("-fqcn")) 207 { 208 i++; 209 fully_qualified_names = true; 210 } 211 else if (args[i].equals("-c")) 212 { 213 post_compiler = null; 214 i++; 215 } 216 else if (args[i].equals("-errors")) 217 { 218 i++; 219 try { 220 error_count = Integer.parseInt(args[i]); 221 } catch (NumberFormatException e) {} 222 i++; 223 } 224 else if (args[i].equals("-w")) 225 { 226 i++; 227 try { 228 output_width = Integer.parseInt(args[i]); 229 } catch (NumberFormatException e) {} 230 i++; 231 } 232 else if (args[i].equals("-post")) 233 { 234 i++; 235 post_compiler = args[i]; 236 i++; 237 } 238 else if (args[i].equals("-stdout")) 239 { 240 i++; 241 output_stdout = true; 242 } 243 else if (args[i].equals("-sx")) 244 { 245 i++; 246 if (source_ext == null) { 247 source_ext = new String [] { args[i] }; 248 } 249 else { 250 String [] s = new String [source_ext.length+1]; 251 System.arraycopy(source_ext, 0, s, 0, source_ext.length); 252 s[s.length-1] = args[i]; 253 source_ext = s; 254 } 255 i++; 256 } 257 else if (args[i].equals("-ox")) 258 { 259 i++; 260 output_ext = args[i]; 261 i++; 262 } 263 else if (args[i].equals("-noserial")) 264 { 265 i++; 266 serialize_type_info = false; 267 } 268 else if (args[i].equals("-dump")) 269 { 270 i++; 271 String pass_name = args[i]; 272 dump_ast.add(pass_name); 273 i++; 274 } 275 else if (args[i].equals("-print")) 276 { 277 i++; 278 String pass_name = args[i]; 279 print_ast.add(pass_name); 280 i++; 281 } 282 else if (args[i].equals("-disable")) 283 { 284 i++; 285 String pass_name = args[i]; 286 disable_passes.add(pass_name); 287 i++; 288 } 289 else if (args[i].equals("-nooutput")) 290 { 291 i++; 292 keep_output_files = false; 293 output_width = 1000; } 297 else if (args[i].equals("-v") || args[i].equals("-verbose")) 298 { 299 i++; 300 Report.addTopic("verbose", 1); 301 } 302 else if (args[i].equals("-report")) { 303 i++; 304 String report_option = args[i]; 305 StringTokenizer st = new StringTokenizer (args[i], "="); 306 String topic = ""; int level = 0; 307 if (st.hasMoreTokens()) topic = st.nextToken(); 308 if (st.hasMoreTokens()) { 309 try { 310 level = Integer.parseInt(st.nextToken()); 311 } 312 catch (NumberFormatException e) {} 313 } 314 Report.addTopic(topic, level); 315 i++; 316 } 317 else if (!args[i].startsWith("-")) { 318 source.add(args[i]); 319 File f = new File (args[i]).getParentFile(); 320 if (f != null && !source_path.contains(f)) 321 source_path.add(f); 322 i++; 323 } 324 325 return i; 326 } 327 328 331 public void usage(PrintStream out) { 332 out.println("usage: " + extension.compilerName() + " [options] " + 333 "<source-file>." + extension.fileExtensions()[0] + " ..."); 334 out.println("where [options] includes:"); 335 usageForFlag(out, "@<file>", "read options from <file>"); 336 usageForFlag(out, "-d <directory>", "output directory"); 337 usageForFlag(out, "-assert", "recognize the assert keyword"); 338 usageForFlag(out, "-sourcepath <path>", "source path"); 339 usageForFlag(out, "-bootclasspath <path>", 340 "path for bootstrap class files"); 341 usageForFlag(out, "-ext <extension>", "use language extension"); 342 usageForFlag(out, "-extclass <ext-class>", "use language extension"); 343 usageForFlag(out, "-fqcn", "use fully-qualified class names"); 344 usageForFlag(out, "-sx <ext>", "set source extension"); 345 usageForFlag(out, "-ox <ext>", "set output extension"); 346 usageForFlag(out, "-errors <num>", "set the maximum number of errors"); 347 usageForFlag(out, "-w <num>", 348 "set the maximum width of the .java output files"); 349 usageForFlag(out, "-dump <pass>", "dump the ast after pass <pass>"); 350 usageForFlag(out, "-print <pass>", 351 "pretty-print the ast after pass <pass>"); 352 usageForFlag(out, "-disable <pass>", "disable pass <pass>"); 353 usageForFlag(out, "-noserial", "disable class serialization"); 355 usageForFlag(out, "-nooutput", "delete output files after compilation"); 356 usageForFlag(out, "-c", "compile only to .java"); 357 usageForFlag(out, "-post <compiler>", 358 "run javac-like compiler after translation"); 359 usageForFlag(out, "-v -verbose", "print verbose debugging information"); 360 usageForFlag(out, "-report <topic>=<level>", 361 "print verbose debugging information about " + 362 "topic at specified verbosity"); 363 364 StringBuffer allowedTopics = new StringBuffer ("Allowed topics: "); 365 for (Iterator iter = Report.topics.iterator(); iter.hasNext(); ) { 366 allowedTopics.append(iter.next().toString()); 367 if (iter.hasNext()) { 368 allowedTopics.append(", "); 369 } 370 } 371 usageSubsection(out, allowedTopics.toString()); 372 373 usageForFlag(out, "-version", "print version info"); 374 usageForFlag(out, "-h", "print this message"); 375 } 376 377 381 protected int USAGE_SCREEN_WIDTH = 76; 382 387 protected int USAGE_FLAG_WIDTH = 27; 388 392 protected int USAGE_SUBSECTION_INDENT = 8; 393 394 403 protected void usageForFlag(PrintStream out, String flag, String description) { 404 out.print(" "); 405 out.print(flag); 406 int cur = flag.length() + 2; 408 409 if (cur < USAGE_FLAG_WIDTH) { 411 printSpaces(out, USAGE_FLAG_WIDTH - cur); 412 } 413 else { 414 out.println(); 417 printSpaces(out, USAGE_FLAG_WIDTH); 418 } 419 cur = USAGE_FLAG_WIDTH; 420 421 StringTokenizer st = new StringTokenizer (description); 423 while (st.hasMoreTokens()) { 424 String s = st.nextToken(); 425 if (cur + s.length() > USAGE_SCREEN_WIDTH) { 426 out.println(); 427 printSpaces(out, USAGE_FLAG_WIDTH); 428 cur = USAGE_FLAG_WIDTH; 429 } 430 out.print(s); 431 cur += s.length(); 432 if (st.hasMoreTokens()) { 433 if (cur + 1 > USAGE_SCREEN_WIDTH) { 434 out.println(); 435 printSpaces(out, USAGE_FLAG_WIDTH); 436 cur = USAGE_FLAG_WIDTH; 437 } 438 else { 439 out.print(" "); 440 cur++; 441 } 442 } 443 } 444 out.println(); 445 } 446 447 455 protected void usageSubsection(PrintStream out, String text) { 456 printSpaces(out, USAGE_SUBSECTION_INDENT); 458 459 int cur = USAGE_SUBSECTION_INDENT; 461 462 StringTokenizer st = new StringTokenizer (text); 464 while (st.hasMoreTokens()) { 465 String s = st.nextToken(); 466 if (cur + s.length() > USAGE_SCREEN_WIDTH) { 467 out.println(); 468 printSpaces(out, USAGE_SUBSECTION_INDENT); 469 cur = USAGE_SUBSECTION_INDENT; 470 } 471 out.print(s); 472 cur += s.length(); 473 if (st.hasMoreTokens()) { 474 if (cur + 1 > USAGE_SCREEN_WIDTH) { 475 out.println(); 476 printSpaces(out, USAGE_SUBSECTION_INDENT); 477 cur = USAGE_SUBSECTION_INDENT; 478 } 479 else { 480 out.print(' '); 481 cur++; 482 } 483 } 484 } 485 out.println(); 486 } 487 488 493 protected static void printSpaces(PrintStream out, int n) { 494 while (n-- > 0) { 495 out.print(' '); 496 } 497 } 498 499 public String constructFullClasspath() { 500 StringBuffer fullcp = new StringBuffer (); 501 if (bootclasspath != null) { 502 fullcp.append(bootclasspath); 503 } 504 fullcp.append(classpath); 505 return fullcp.toString(); 506 } 507 508 public String constructPostCompilerClasspath() { 509 return output_directory + File.pathSeparator 510 + "." + File.pathSeparator 511 + System.getProperty("java.class.path"); 512 } 513 } 514
| Popular Tags
|