1 2 17 package com.lutris.util; 18 import java.io.*; 19 import java.util.*; 20 21 public class ConfigParser implements ConfigParserConstants { 22 23 private String comments=""; 24 private ConfigFile configFile = null; 25 26 public static void main(String args[]) 27 throws ParseException, ConfigException 28 { 29 ConfigParser parser = new ConfigParser(System.in); 30 ConfigFile cf = new ConfigFile(); 31 parser.process(cf); 32 cf.write(System.out); 33 } 34 35 public void process(ConfigFile configFile) 36 throws ParseException, ConfigException 37 { 38 this.configFile = configFile; 39 try { 40 try { 41 ParseConfigFile(); 42 } catch (TokenMgrError e) { 43 throw generateParseException(); 44 } 45 } catch (ParseException e) { 46 System.err.println("Syntax error in config file at line " + 47 e.currentToken.next.beginLine + 48 ", column " + e.currentToken.next.beginColumn + "."); 49 throw e; 50 } 51 } 52 53 private static final boolean isodigit(char c) { 54 switch (c) { 55 case '0': case '1': case '2': case '3': 56 case '4': case '5': case '6': case '7': 57 return true; 58 } 59 return false; 60 } 61 62 private static final boolean isxdigit(char c) { 63 switch (c) { 64 case '0': case '1': case '2': case '3': 65 case '4': case '5': case '6': case '7': 66 case '8': case '9': 67 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 68 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 69 return true; 70 } 71 return false; 72 } 73 74 final public void ParseConfigFile() throws ParseException, ConfigException { 76 boolean done; 77 label_1: 78 while (true) { 79 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 80 case 0: 81 case KEYWORD: 82 case WHITESPACE: 83 case COMMENT: 84 case EOL: 85 ; 86 break; 87 default: 88 jj_la1[0] = jj_gen; 89 break label_1; 90 } 91 done = ConfigEntry(); 92 if (done) {if (true) return;} 93 } 94 } 95 96 final public boolean ConfigEntry() throws ParseException, ConfigException { 97 Token comment, kw, val, ws; 98 String keyword=null; 99 Vector vlist=null; 100 boolean isArray=false; 101 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 102 case WHITESPACE: 103 comment = jj_consume_token(WHITESPACE); 104 comments += comment.toString(); 105 break; 106 default: 107 jj_la1[1] = jj_gen; 108 ; 109 } 110 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 111 case EOL: 112 comment = jj_consume_token(EOL); 113 comments += comment.toString(); 114 break; 115 case COMMENT: 116 comment = jj_consume_token(COMMENT); 117 comments += comment.toString(); 118 break; 119 case KEYWORD: 120 kw = jj_consume_token(KEYWORD); 121 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 122 case WHITESPACE: 123 jj_consume_token(WHITESPACE); 124 break; 125 default: 126 jj_la1[2] = jj_gen; 127 ; 128 } 129 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 130 case 1: 131 isArray = arraySpec(); 132 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 133 case WHITESPACE: 134 jj_consume_token(WHITESPACE); 135 break; 136 default: 137 jj_la1[3] = jj_gen; 138 ; 139 } 140 break; 141 default: 142 jj_la1[4] = jj_gen; 143 ; 144 } 145 jj_consume_token(ASSIGN); 146 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 147 case WHITESPACE: 148 jj_consume_token(WHITESPACE); 149 break; 150 default: 151 jj_la1[5] = jj_gen; 152 ; 153 } 154 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 155 case QSTRING: 156 case STRING: 157 vlist = ValueList(); 158 break; 159 default: 160 jj_la1[6] = jj_gen; 161 ; 162 } 163 jj_consume_token(EOL); 164 if (vlist == null) { 165 try { 166 if (isArray) { 167 configFile.addEntry(kw.image, 168 new String [0], comments); 169 } else { 170 configFile.addEntry(kw.image, "", comments); 171 } 172 } catch (KeywordValueException e) { 173 {if (true) throw new ConfigException( 174 "Key " + kw.image + " is not valid.");} 175 } 176 } else { 177 Enumeration en = vlist.elements(); 178 String [] values = new String [vlist.size()]; 179 int pos = values.length - 1; 180 while (en.hasMoreElements()) { 181 values[pos--] = (String ) en.nextElement(); 182 } 183 try { 184 if (isArray) { 185 configFile.addEntry(kw.image, values, comments); 186 } else { 187 if (values.length != 1) { 188 {if (true) throw new ConfigException( 189 "Key " + kw.image + 190 " is not a list. Use " + 191 kw.image +"[]");} 192 } 193 configFile.addEntry(kw.image, values[0], comments); 194 } 195 } catch (KeywordValueException e) { 196 {if (true) throw new ConfigException( 197 "Key " + kw.image + " is not valid.");} 198 } 199 } 200 comments = ""; 201 break; 202 case 0: 203 jj_consume_token(0); 204 try { 205 configFile.addEntry(ConfigFile.TRAILING_COMMENT, 207 (String [])null, comments); 208 } catch (KeywordValueException e) { 209 System.err.println("ConfigFile unable to add entry for " 210 + ConfigFile.TRAILING_COMMENT + "."); 211 } 212 comments = ""; 213 {if (true) return true;} 215 break; 216 default: 217 jj_la1[7] = jj_gen; 218 jj_consume_token(-1); 219 throw new ParseException(); 220 } 221 {if (true) return false;} 223 throw new Error ("Missing return statement in function"); 224 } 225 226 final public Vector ValueList() throws ParseException { 227 String sval=null; 228 Vector vlist=null; 229 sval = Value(); 230 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 231 case COMMA: 232 jj_consume_token(COMMA); 233 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 234 case WHITESPACE: 235 jj_consume_token(WHITESPACE); 236 break; 237 default: 238 jj_la1[8] = jj_gen; 239 ; 240 } 241 vlist = ValueList(); 242 break; 243 default: 244 jj_la1[9] = jj_gen; 245 ; 246 } 247 if (vlist == null) { 248 vlist = new Vector(); 249 vlist.addElement(sval); 250 } else { 251 vlist.addElement(sval); 252 } 253 {if (true) return vlist;} 254 throw new Error ("Missing return statement in function"); 255 } 256 257 final public String Value() throws ParseException { 258 String lhs="", rhs=""; 259 lhs = ValueFragment(); 260 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 261 case PLUS: 262 jj_consume_token(PLUS); 263 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 264 case WHITESPACE: 265 jj_consume_token(WHITESPACE); 266 break; 267 default: 268 jj_la1[10] = jj_gen; 269 ; 270 } 271 rhs = Value(); 272 break; 273 default: 274 jj_la1[11] = jj_gen; 275 ; 276 } 277 {if (true) return lhs + rhs;} 278 throw new Error ("Missing return statement in function"); 279 } 280 281 final public String ValueFragment() throws ParseException { 282 Token val; 283 String sval; 284 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 285 case QSTRING: 286 sval = QuotedString(); 287 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 288 case WHITESPACE: 289 jj_consume_token(WHITESPACE); 290 break; 291 default: 292 jj_la1[12] = jj_gen; 293 ; 294 } 295 {if (true) return sval;} 296 break; 297 case STRING: 298 val = jj_consume_token(STRING); 299 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 300 case WHITESPACE: 301 jj_consume_token(WHITESPACE); 302 break; 303 default: 304 jj_la1[13] = jj_gen; 305 ; 306 } 307 {if (true) return val.image;} 308 break; 309 default: 310 jj_la1[14] = jj_gen; 311 jj_consume_token(-1); 312 throw new ParseException(); 313 } 314 throw new Error ("Missing return statement in function"); 315 } 316 317 final public boolean arraySpec() throws ParseException { 318 String lhs="", rhs=""; 319 jj_consume_token(1); 320 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 321 case WHITESPACE: 322 jj_consume_token(WHITESPACE); 323 break; 324 default: 325 jj_la1[15] = jj_gen; 326 ; 327 } 328 jj_consume_token(2); 329 {if (true) return true;} 330 throw new Error ("Missing return statement in function"); 331 } 332 333 final public String QuotedString() throws ParseException { 334 Token qs; 335 StringBuffer sbuf; 336 int i; 337 char[] cbuf; 338 qs = jj_consume_token(QSTRING); 339 sbuf = new StringBuffer (); 340 cbuf = qs.image.toCharArray(); 341 for (i=1; i < (cbuf.length - 1); i++) { 342 switch (cbuf[i]) { 343 case '\\': 344 switch (cbuf[i+1]) { 345 case 'n': 346 sbuf.append("\n"); i += 1; break; 347 case 't': 348 sbuf.append("\t"); i += 1; break; 349 case 'b': 350 sbuf.append("\b"); i += 1; break; 351 case 'r': 352 sbuf.append("\r"); i += 1; break; 353 case 'f': 354 sbuf.append("\f"); i += 1; break; 355 case 'u': 356 if ((cbuf.length - i >= 6) && 357 (isxdigit(cbuf[i+2])) && 358 (isxdigit(cbuf[i+3])) && 359 (isxdigit(cbuf[i+4])) && 360 (isxdigit(cbuf[i+5]))) { 361 try { 362 String s = new String (cbuf, i+2, 4); 363 int val = Integer.parseInt(s, 16); 364 sbuf.append((char)val); 365 i += 5; 366 } catch (Throwable t) { 367 sbuf.append(cbuf[i+1]); 368 i += 1; 369 } 370 } else { 371 sbuf.append(cbuf[i+1]); 372 i += 1; 373 } 374 break; 375 case 'x': 376 if ((cbuf.length - i >= 4) && 377 (isxdigit(cbuf[i+2])) && 378 (isxdigit(cbuf[i+3]))) { 379 try { 380 String s = new String (cbuf, i+2, 2); 381 int val = Integer.parseInt(s, 16); 382 sbuf.append((char)val); 383 i += 3; 384 } catch (Throwable t) { 385 sbuf.append(cbuf[i+1]); 386 i += 1; 387 } 388 } else { 389 sbuf.append(cbuf[i+1]); 390 i += 1; 391 } 392 break; 393 default: 394 if ((cbuf.length - i >= 4) && 395 (isodigit(cbuf[i+1])) && 396 (isodigit(cbuf[i+2])) && 397 (isodigit(cbuf[i+3]))) { 398 try { 399 String s = new String (cbuf, i+1, 3); 400 int val = Integer.parseInt(s, 8); 401 sbuf.append((char)val); 402 i += 3; 403 } catch (Throwable t) { 404 sbuf.append(cbuf[i+1]); 405 i += 1; 406 } 407 } else { 408 sbuf.append(cbuf[i+1]); 409 i += 1; 410 } 411 break; 412 } break; 414 default: 415 sbuf.append(cbuf[i]); 416 break; 417 } } {if (true) return sbuf.toString();} 420 throw new Error ("Missing return statement in function"); 421 } 422 423 public ConfigParserTokenManager token_source; 424 SimpleCharStream jj_input_stream; 425 public Token token, jj_nt; 426 private int jj_ntk; 427 private int jj_gen; 428 final private int[] jj_la1 = new int[16]; 429 static private int[] jj_la1_0; 430 static { 431 jj_la1_0(); 432 } 433 private static void jj_la1_0() { 434 jj_la1_0 = new int[] {0x171,0x20,0x20,0x20,0x2,0x20,0x600,0x151,0x20,0x2000,0x20,0x1000,0x20,0x20,0x600,0x20,}; 435 } 436 437 public ConfigParser(java.io.InputStream stream) { 438 jj_input_stream = new SimpleCharStream(stream, 1, 1); 439 token_source = new ConfigParserTokenManager(jj_input_stream); 440 token = new Token(); 441 jj_ntk = -1; 442 jj_gen = 0; 443 for (int i = 0; i < 16; i++) jj_la1[i] = -1; 444 } 445 446 public void ReInit(java.io.InputStream stream) { 447 jj_input_stream.ReInit(stream, 1, 1); 448 token_source.ReInit(jj_input_stream); 449 token = new Token(); 450 jj_ntk = -1; 451 jj_gen = 0; 452 for (int i = 0; i < 16; i++) jj_la1[i] = -1; 453 } 454 455 public ConfigParser(java.io.Reader stream) { 456 jj_input_stream = new SimpleCharStream(stream, 1, 1); 457 token_source = new ConfigParserTokenManager(jj_input_stream); 458 token = new Token(); 459 jj_ntk = -1; 460 jj_gen = 0; 461 for (int i = 0; i < 16; i++) jj_la1[i] = -1; 462 } 463 464 public void ReInit(java.io.Reader stream) { 465 jj_input_stream.ReInit(stream, 1, 1); 466 token_source.ReInit(jj_input_stream); 467 token = new Token(); 468 jj_ntk = -1; 469 jj_gen = 0; 470 for (int i = 0; i < 16; i++) jj_la1[i] = -1; 471 } 472 473 public ConfigParser(ConfigParserTokenManager tm) { 474 token_source = tm; 475 token = new Token(); 476 jj_ntk = -1; 477 jj_gen = 0; 478 for (int i = 0; i < 16; i++) jj_la1[i] = -1; 479 } 480 481 public void ReInit(ConfigParserTokenManager tm) { 482 token_source = tm; 483 token = new Token(); 484 jj_ntk = -1; 485 jj_gen = 0; 486 for (int i = 0; i < 16; i++) jj_la1[i] = -1; 487 } 488 489 final private Token jj_consume_token(int kind) throws ParseException { 490 Token oldToken; 491 if ((oldToken = token).next != null) token = token.next; 492 else token = token.next = token_source.getNextToken(); 493 jj_ntk = -1; 494 if (token.kind == kind) { 495 jj_gen++; 496 return token; 497 } 498 token = oldToken; 499 jj_kind = kind; 500 throw generateParseException(); 501 } 502 503 final public Token getNextToken() { 504 if (token.next != null) token = token.next; 505 else token = token.next = token_source.getNextToken(); 506 jj_ntk = -1; 507 jj_gen++; 508 return token; 509 } 510 511 final public Token getToken(int index) { 512 Token t = token; 513 for (int i = 0; i < index; i++) { 514 if (t.next != null) t = t.next; 515 else t = t.next = token_source.getNextToken(); 516 } 517 return t; 518 } 519 520 final private int jj_ntk() { 521 if ((jj_nt=token.next) == null) 522 return (jj_ntk = (token.next=token_source.getNextToken()).kind); 523 else 524 return (jj_ntk = jj_nt.kind); 525 } 526 527 private java.util.Vector jj_expentries = new java.util.Vector (); 528 private int[] jj_expentry; 529 private int jj_kind = -1; 530 531 public ParseException generateParseException() { 532 jj_expentries.removeAllElements(); 533 boolean[] la1tokens = new boolean[16]; 534 for (int i = 0; i < 16; i++) { 535 la1tokens[i] = false; 536 } 537 if (jj_kind >= 0) { 538 la1tokens[jj_kind] = true; 539 jj_kind = -1; 540 } 541 for (int i = 0; i < 16; i++) { 542 if (jj_la1[i] == jj_gen) { 543 for (int j = 0; j < 32; j++) { 544 if ((jj_la1_0[i] & (1<<j)) != 0) { 545 la1tokens[j] = true; 546 } 547 } 548 } 549 } 550 for (int i = 0; i < 16; i++) { 551 if (la1tokens[i]) { 552 jj_expentry = new int[1]; 553 jj_expentry[0] = i; 554 jj_expentries.addElement(jj_expentry); 555 } 556 } 557 int[][] exptokseq = new int[jj_expentries.size()][]; 558 for (int i = 0; i < jj_expentries.size(); i++) { 559 exptokseq[i] = (int[])jj_expentries.elementAt(i); 560 } 561 return new ParseException(token, exptokseq, tokenImage); 562 } 563 564 final public void enable_tracing() { 565 } 566 567 final public void disable_tracing() { 568 } 569 570 } 571 572 class ConfigElement { 573 576 public String comments; 577 578 579 582 public String key; 583 584 587 public String [] values; 588 } 589 | Popular Tags |