1 22 23 package org.javacc.parser; 24 25 import java.io.File ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 34 public class Options { 35 36 39 protected Options() { 40 } 41 42 48 protected static Map optionValues = null; 49 50 53 protected static int intValue(final String option) { 54 return ((Integer ) optionValues.get(option)).intValue(); 55 } 56 57 60 protected static boolean booleanValue(final String option) { 61 return ((Boolean ) optionValues.get(option)).booleanValue(); 62 } 63 64 67 protected static String stringValue(final String option) { 68 return (String ) optionValues.get(option); 69 } 70 71 76 private static Set cmdLineSetting = null; 77 78 83 private static Set inputFileSetting = null; 84 85 88 public static void init() { 89 optionValues = new HashMap (); 90 cmdLineSetting = new HashSet (); 91 inputFileSetting = new HashSet (); 92 93 optionValues.put("LOOKAHEAD", new Integer (1)); 94 optionValues.put("CHOICE_AMBIGUITY_CHECK", new Integer (2)); 95 optionValues.put("OTHER_AMBIGUITY_CHECK", new Integer (1)); 96 97 optionValues.put("STATIC", Boolean.TRUE); 98 optionValues.put("DEBUG_PARSER", Boolean.FALSE); 99 optionValues.put("DEBUG_LOOKAHEAD", Boolean.FALSE); 100 optionValues.put("DEBUG_TOKEN_MANAGER", Boolean.FALSE); 101 optionValues.put("ERROR_REPORTING", Boolean.TRUE); 102 optionValues.put("JAVA_UNICODE_ESCAPE", Boolean.FALSE); 103 optionValues.put("UNICODE_INPUT", Boolean.FALSE); 104 optionValues.put("IGNORE_CASE", Boolean.FALSE); 105 optionValues.put("USER_TOKEN_MANAGER", Boolean.FALSE); 106 optionValues.put("USER_CHAR_STREAM", Boolean.FALSE); 107 optionValues.put("BUILD_PARSER", Boolean.TRUE); 108 optionValues.put("BUILD_TOKEN_MANAGER", Boolean.TRUE); 109 optionValues.put("TOKEN_MANAGER_USES_PARSER", Boolean.FALSE); 110 optionValues.put("SANITY_CHECK", Boolean.TRUE); 111 optionValues.put("FORCE_LA_CHECK", Boolean.FALSE); 112 optionValues.put("COMMON_TOKEN_ACTION", Boolean.FALSE); 113 optionValues.put("CACHE_TOKENS", Boolean.FALSE); 114 optionValues.put("KEEP_LINE_COLUMN", Boolean.TRUE); 115 116 optionValues.put("OUTPUT_DIRECTORY", "."); 117 optionValues.put("JDK_VERSION", "1.4"); 118 } 119 120 128 public static boolean isOption(final String opt) { 129 return opt != null && opt.length() > 1 && opt.charAt(0) == '-'; 130 } 131 132 public static void setInputFileOption(Object nameloc, Object valueloc, 133 String name, int value) { 134 String s = name.toUpperCase(); 135 if (!optionValues.containsKey(s)) { 136 JavaCCErrors.warning(nameloc, "Bad option name \"" + name 137 + "\". Option setting will be ignored."); 138 return; 139 } 140 Object Val = optionValues.get(s); 141 if (Val != null) { 142 if (!(Val instanceof Integer ) || value <= 0) { 143 JavaCCErrors.warning(valueloc, "Bad option value \"" + value 144 + "\" for \"" + name 145 + "\". Option setting will be ignored."); 146 return; 147 } 148 if (inputFileSetting.contains(s)) { 149 JavaCCErrors.warning(nameloc, "Duplicate option setting for \"" 150 + name + "\" will be ignored."); 151 return; 152 } 153 if (cmdLineSetting.contains(s)) { 154 if (((Integer ) Val).intValue() != value) { 155 JavaCCErrors.warning(nameloc, "Command line setting of \"" 156 + name + "\" modifies option value in file."); 157 } 158 return; 159 } 160 } 161 162 optionValues.put(s, new Integer (value)); 163 inputFileSetting.add(s); 164 } 165 166 public static void setInputFileOption(Object nameloc, Object valueloc, 167 String name, boolean value) { 168 String s = name.toUpperCase(); 169 if (!optionValues.containsKey(s)) { 170 JavaCCErrors.warning(nameloc, "Bad option name \"" + name 171 + "\". Option setting will be ignored."); 172 return; 173 } 174 Object Val = optionValues.get(s); 175 if (Val != null) { 176 if (!(Val instanceof Boolean )) { 177 JavaCCErrors.warning(valueloc, "Bad option value \"" + value 178 + "\" for \"" + name 179 + "\". Option setting will be ignored."); 180 return; 181 } 182 if (inputFileSetting.contains(s)) { 183 JavaCCErrors.warning(nameloc, "Duplicate option setting for \"" 184 + name + "\" will be ignored."); 185 return; 186 } 187 if (cmdLineSetting.contains(s)) { 188 if (((Boolean ) Val).booleanValue() != value) { 189 JavaCCErrors.warning(nameloc, "Command line setting of \"" 190 + name + "\" modifies option value in file."); 191 } 192 return; 193 } 194 } 195 196 optionValues.put(s, (value ? Boolean.TRUE : Boolean.FALSE)); 197 inputFileSetting.add(s); 198 } 199 200 public static void setInputFileOption(Object nameloc, Object valueloc, 201 String name, String value) { 202 String s = name.toUpperCase(); 203 if (!optionValues.containsKey(s)) { 204 JavaCCErrors.warning(nameloc, "Bad option name \"" + name 205 + "\". Option setting will be ignored."); 206 return; 207 } 208 Object Val = optionValues.get(s); 209 if (Val != null) { 210 if (!(Val instanceof String )) { 211 JavaCCErrors.warning(valueloc, "Bad option value \"" + value 212 + "\" for \"" + name 213 + "\". Option setting will be ignored."); 214 return; 215 } 216 if (inputFileSetting.contains(s)) { 217 JavaCCErrors.warning(nameloc, "Duplicate option setting for \"" 218 + name + "\" will be ignored."); 219 return; 220 } 221 if (cmdLineSetting.contains(s)) { 222 if (!Val.equals(value)) { 223 JavaCCErrors.warning(nameloc, "Command line setting of \"" 224 + name + "\" modifies option value in file."); 225 } 226 return; 227 } 228 } 229 230 optionValues.put(s, value); 231 inputFileSetting.add(s); 232 } 233 234 238 public static void setCmdLineOption(String arg) { 239 String s = arg.toUpperCase(); 240 int index = 0; 241 String name; 242 Object Val; 243 while (index < s.length() && s.charAt(index) != '=' 244 && s.charAt(index) != ':') { 245 index++; 246 } 247 if (index < 2 || index >= s.length() - 1) { 248 if (index == s.length()) { 249 if (s.length() > 3 && s.charAt(1) == 'N' && s.charAt(2) == 'O') { 250 name = s.substring(3); 251 Val = Boolean.FALSE; 252 } else { 253 name = s.substring(1); 254 Val = Boolean.TRUE; 255 } 256 } else { 257 System.out.println("Warning: Bad option \"" + arg 258 + "\" will be ignored."); 259 return; 260 } 261 } else { 262 if (s.substring(index + 1).equals("TRUE")) { 263 Val = Boolean.TRUE; 264 } else if (s.substring(index + 1).equals("FALSE")) { 265 Val = Boolean.FALSE; 266 } else { 267 try { 268 int i = Integer.parseInt(s.substring(index + 1)); 269 if (i <= 0) { 270 System.out.println("Warning: Bad option value in \"" 271 + arg + "\" will be ignored."); 272 return; 273 } 274 Val = new Integer (i); 275 } catch (NumberFormatException e) { 276 Val = arg.substring(index + 1); 277 if (arg.length() > index + 2) { 278 if (arg.charAt(index + 1) == '"' 280 && arg.charAt(arg.length() - 1) == '"') { 281 Val = arg.substring(index + 2, arg.length() - 1); 283 } 284 } 285 } 286 } 287 name = s.substring(1, index); 288 } 289 if (!optionValues.containsKey(name)) { 290 System.out.println("Warning: Bad option \"" + arg 291 + "\" will be ignored."); 292 return; 293 } 294 Object valOrig = optionValues.get(name); 295 if (Val.getClass() != valOrig.getClass()) { 296 System.out.println("Warning: Bad option value in \"" + arg 297 + "\" will be ignored."); 298 return; 299 } 300 if (cmdLineSetting.contains(name)) { 301 System.out.println("Warning: Duplicate option setting \"" + arg 302 + "\" will be ignored."); 303 return; 304 } 305 306 optionValues.put(name, Val); 307 cmdLineSetting.add(name); 308 } 309 310 public static void normalize() { 311 if (getDebugLookahead() && !getDebugParser()) { 312 if (cmdLineSetting.contains("DEBUG_PARSER") 313 || inputFileSetting.contains("DEBUG_PARSER")) { 314 JavaCCErrors 315 .warning("True setting of option DEBUG_LOOKAHEAD overrides false setting of option DEBUG_PARSER."); 316 } 317 optionValues.put("DEBUG_PARSER", Boolean.TRUE); 318 } 319 } 320 321 326 public static int getLookahead() { 327 return intValue("LOOKAHEAD"); 328 } 329 330 335 public static int getChoiceAmbiguityCheck() { 336 return intValue("CHOICE_AMBIGUITY_CHECK"); 337 } 338 339 344 public static int getOtherAmbiguityCheck() { 345 return intValue("OTHER_AMBIGUITY_CHECK"); 346 } 347 348 353 public static boolean getStatic() { 354 return booleanValue("STATIC"); 355 } 356 357 362 public static boolean getDebugParser() { 363 return booleanValue("DEBUG_PARSER"); 364 } 365 366 371 public static boolean getDebugLookahead() { 372 return booleanValue("DEBUG_LOOKAHEAD"); 373 } 374 375 380 public static boolean getDebugTokenManager() { 381 return booleanValue("DEBUG_TOKEN_MANAGER"); 382 } 383 384 389 public static boolean getErrorReporting() { 390 return booleanValue("ERROR_REPORTING"); 391 } 392 393 398 public static boolean getJavaUnicodeEscape() { 399 return booleanValue("JAVA_UNICODE_ESCAPE"); 400 } 401 402 407 public static boolean getUnicodeInput() { 408 return booleanValue("UNICODE_INPUT"); 409 } 410 411 416 public static boolean getIgnoreCase() { 417 return booleanValue("IGNORE_CASE"); 418 } 419 420 425 public static boolean getUserTokenManager() { 426 return booleanValue("USER_TOKEN_MANAGER"); 427 } 428 429 434 public static boolean getUserCharStream() { 435 return booleanValue("USER_CHAR_STREAM"); 436 } 437 438 443 public static boolean getBuildParser() { 444 return booleanValue("BUILD_PARSER"); 445 } 446 447 452 public static boolean getBuildTokenManager() { 453 return booleanValue("BUILD_TOKEN_MANAGER"); 454 } 455 456 461 public static boolean getTokenManagerUsesParser(){ 462 return booleanValue("TOKEN_MANAGER_USES_PARSER"); 463 } 464 465 470 public static boolean getSanityCheck() { 471 return booleanValue("SANITY_CHECK"); 472 } 473 474 479 public static boolean getForceLaCheck() { 480 return booleanValue("FORCE_LA_CHECK"); 481 } 482 483 488 489 public static boolean getCommonTokenAction() { 490 return booleanValue("COMMON_TOKEN_ACTION"); 491 } 492 493 498 public static boolean getCacheTokens() { 499 return booleanValue("CACHE_TOKENS"); 500 } 501 502 507 public static boolean getKeepLineColumn() { 508 return booleanValue("KEEP_LINE_COLUMN"); 509 } 510 511 516 public static String getJdkVersion() { 517 return stringValue("JDK_VERSION"); 518 } 519 520 525 public static File getOutputDirectory() { 526 return new File (stringValue("OUTPUT_DIRECTORY")); 527 } 528 } 529 | Popular Tags |