1 2 3 package org.sablecc.sablecc.launcher.syntax3.parser; 4 5 import org.sablecc.sablecc.launcher.syntax3.lexer.*; 6 import org.sablecc.sablecc.launcher.syntax3.node.*; 7 import org.sablecc.sablecc.launcher.syntax3.analysis.*; 8 import java.util.*; 9 10 import java.io.DataInputStream ; 11 import java.io.BufferedInputStream ; 12 import java.io.IOException ; 13 14 @SuppressWarnings ("nls") 15 public class Parser 16 { 17 public final Analysis ignoredTokens = new AnalysisAdapter(); 18 19 protected ArrayList nodeList; 20 21 private final Lexer lexer; 22 private final ListIterator stack = new LinkedList().listIterator(); 23 private int last_pos; 24 private int last_line; 25 private Token last_token; 26 private final TokenIndex converter = new TokenIndex(); 27 private final int[] action = new int[2]; 28 29 private final static int SHIFT = 0; 30 private final static int REDUCE = 1; 31 private final static int ACCEPT = 2; 32 private final static int ERROR = 3; 33 34 public Parser(@SuppressWarnings ("hiding") Lexer lexer) 35 { 36 this.lexer = lexer; 37 } 38 39 @SuppressWarnings ({"unchecked","unused"}) 40 private void push(int numstate, ArrayList listNode) throws ParserException, LexerException, IOException 41 { 42 this.nodeList = listNode; 43 44 if(!this.stack.hasNext()) 45 { 46 this.stack.add(new State(numstate, this.nodeList)); 47 return; 48 } 49 50 State s = (State) this.stack.next(); 51 s.state = numstate; 52 s.nodes = this.nodeList; 53 } 54 55 private int goTo(int index) 56 { 57 int state = state(); 58 int low = 1; 59 int high = gotoTable[index].length - 1; 60 int value = gotoTable[index][0][1]; 61 62 while(low <= high) 63 { 64 int middle = (low + high) / 2; 65 66 if(state < gotoTable[index][middle][0]) 67 { 68 high = middle - 1; 69 } 70 else if(state > gotoTable[index][middle][0]) 71 { 72 low = middle + 1; 73 } 74 else 75 { 76 value = gotoTable[index][middle][1]; 77 break; 78 } 79 } 80 81 return value; 82 } 83 84 private int state() 85 { 86 State s = (State) this.stack.previous(); 87 this.stack.next(); 88 return s.state; 89 } 90 91 private ArrayList pop() 92 { 93 return ((State) this.stack.previous()).nodes; 94 } 95 96 private int index(Switchable token) 97 { 98 this.converter.index = -1; 99 token.apply(this.converter); 100 return this.converter.index; 101 } 102 103 @SuppressWarnings ("unchecked") 104 public Start parse() throws ParserException, LexerException, IOException 105 { 106 push(0, null); 107 List<Node> ign = null; 108 while(true) 109 { 110 while(index(this.lexer.peek()) == -1) 111 { 112 if(ign == null) 113 { 114 ign = new LinkedList<Node>(); 115 } 116 117 ign.add(this.lexer.next()); 118 } 119 120 if(ign != null) 121 { 122 this.ignoredTokens.setIn(this.lexer.peek(), ign); 123 ign = null; 124 } 125 126 this.last_pos = this.lexer.peek().getPos(); 127 this.last_line = this.lexer.peek().getLine(); 128 this.last_token = this.lexer.peek(); 129 130 int index = index(this.lexer.peek()); 131 this.action[0] = Parser.actionTable[state()][0][1]; 132 this.action[1] = Parser.actionTable[state()][0][2]; 133 134 int low = 1; 135 int high = Parser.actionTable[state()].length - 1; 136 137 while(low <= high) 138 { 139 int middle = (low + high) / 2; 140 141 if(index < Parser.actionTable[state()][middle][0]) 142 { 143 high = middle - 1; 144 } 145 else if(index > Parser.actionTable[state()][middle][0]) 146 { 147 low = middle + 1; 148 } 149 else 150 { 151 this.action[0] = Parser.actionTable[state()][middle][1]; 152 this.action[1] = Parser.actionTable[state()][middle][2]; 153 break; 154 } 155 } 156 157 switch(this.action[0]) 158 { 159 case SHIFT: 160 { 161 ArrayList list = new ArrayList(); 162 list.add(this.lexer.next()); 163 push(this.action[1], list); 164 } 165 break; 166 case REDUCE: 167 switch(this.action[1]) 168 { 169 case 0: 170 { 171 ArrayList list = new0(); 172 push(goTo(0), list); 173 } 174 break; 175 case 1: 176 { 177 ArrayList list = new1(); 178 push(goTo(0), list); 179 } 180 break; 181 case 2: 182 { 183 ArrayList list = new2(); 184 push(goTo(0), list); 185 } 186 break; 187 case 3: 188 { 189 ArrayList list = new3(); 190 push(goTo(1), list); 191 } 192 break; 193 case 4: 194 { 195 ArrayList list = new4(); 196 push(goTo(1), list); 197 } 198 break; 199 case 5: 200 { 201 ArrayList list = new5(); 202 push(goTo(2), list); 203 } 204 break; 205 case 6: 206 { 207 ArrayList list = new6(); 208 push(goTo(3), list); 209 } 210 break; 211 case 7: 212 { 213 ArrayList list = new7(); 214 push(goTo(3), list); 215 } 216 break; 217 case 8: 218 { 219 ArrayList list = new8(); 220 push(goTo(4), list); 221 } 222 break; 223 case 9: 224 { 225 ArrayList list = new9(); 226 push(goTo(4), list); 227 } 228 break; 229 case 10: 230 { 231 ArrayList list = new10(); 232 push(goTo(5), list); 233 } 234 break; 235 case 11: 236 { 237 ArrayList list = new11(); 238 push(goTo(5), list); 239 } 240 break; 241 case 12: 242 { 243 ArrayList list = new12(); 244 push(goTo(5), list); 245 } 246 break; 247 case 13: 248 { 249 ArrayList list = new13(); 250 push(goTo(6), list); 251 } 252 break; 253 case 14: 254 { 255 ArrayList list = new14(); 256 push(goTo(6), list); 257 } 258 break; 259 } 260 break; 261 case ACCEPT: 262 { 263 EOF node2 = (EOF) this.lexer.next(); 264 PArgument node1 = (PArgument) pop().get(0); 265 Start node = new Start(node1, node2); 266 return node; 267 } 268 case ERROR: 269 throw new ParserException(this.last_token, 270 "[" + this.last_line + "," + this.last_pos + "] " + 271 Parser.errorMessages[Parser.errors[this.action[1]]]); 272 } 273 } 274 } 275 276 277 278 @SuppressWarnings ("unchecked") 279 ArrayList new0() 280 { 281 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 282 283 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 284 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 285 PArgument pargumentNode1; 286 { 287 LinkedList listNode3 = new LinkedList(); 289 { 290 LinkedList listNode2 = new LinkedList(); 292 listNode2 = (LinkedList)nodeArrayList2.get(0); 293 if(listNode2 != null) 294 { 295 listNode3.addAll(listNode2); 296 } 297 } 298 299 pargumentNode1 = new AShortOptionsArgument(listNode3); 300 } 301 nodeList.add(pargumentNode1); 302 return nodeList; 303 } 304 305 306 307 @SuppressWarnings ("unchecked") 308 ArrayList new1() 309 { 310 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 311 312 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 313 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 314 PArgument pargumentNode1; 315 { 316 PLongOption plongoptionNode2; 318 plongoptionNode2 = (PLongOption)nodeArrayList2.get(0); 319 320 pargumentNode1 = new ALongOptionArgument(plongoptionNode2); 321 } 322 nodeList.add(pargumentNode1); 323 return nodeList; 324 } 325 326 327 328 @SuppressWarnings ("unchecked") 329 ArrayList new2() 330 { 331 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 332 333 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 334 PArgument pargumentNode1; 335 { 336 TText ttextNode2; 338 ttextNode2 = (TText)nodeArrayList1.get(0); 339 340 pargumentNode1 = new APlainArgument(ttextNode2); 341 } 342 nodeList.add(pargumentNode1); 343 return nodeList; 344 } 345 346 347 348 @SuppressWarnings ("unchecked") 349 ArrayList new3() 350 { 351 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 352 353 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 354 LinkedList listNode2 = new LinkedList(); 355 { 356 PShortOption pshortoptionNode1; 358 pshortoptionNode1 = (PShortOption)nodeArrayList1.get(0); 359 if(pshortoptionNode1 != null) 360 { 361 listNode2.add(pshortoptionNode1); 362 } 363 } 364 nodeList.add(listNode2); 365 return nodeList; 366 } 367 368 369 370 @SuppressWarnings ("unchecked") 371 ArrayList new4() 372 { 373 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 374 375 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 376 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 377 LinkedList listNode3 = new LinkedList(); 378 { 379 LinkedList listNode1 = new LinkedList(); 381 PShortOption pshortoptionNode2; 382 listNode1 = (LinkedList)nodeArrayList1.get(0); 383 pshortoptionNode2 = (PShortOption)nodeArrayList2.get(0); 384 if(listNode1 != null) 385 { 386 listNode3.addAll(listNode1); 387 } 388 if(pshortoptionNode2 != null) 389 { 390 listNode3.add(pshortoptionNode2); 391 } 392 } 393 nodeList.add(listNode3); 394 return nodeList; 395 } 396 397 398 399 @SuppressWarnings ("unchecked") 400 ArrayList new5() 401 { 402 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 403 404 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 405 PShortOption pshortoptionNode1; 406 { 407 TShortName tshortnameNode2; 409 @SuppressWarnings ("unused") Object nullNode3 = null; 410 tshortnameNode2 = (TShortName)nodeArrayList1.get(0); 411 412 pshortoptionNode1 = new AShortOption(tshortnameNode2, null); 413 } 414 nodeList.add(pshortoptionNode1); 415 return nodeList; 416 } 417 418 419 420 @SuppressWarnings ("unchecked") 421 ArrayList new6() 422 { 423 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 424 425 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 426 PShortOption pshortoptionNode1; 427 pshortoptionNode1 = (PShortOption)nodeArrayList1.get(0); 428 nodeList.add(pshortoptionNode1); 429 return nodeList; 430 } 431 432 433 434 @SuppressWarnings ("unchecked") 435 ArrayList new7() 436 { 437 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 438 439 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 440 PShortOption pshortoptionNode1; 441 pshortoptionNode1 = (PShortOption)nodeArrayList1.get(0); 442 nodeList.add(pshortoptionNode1); 443 return nodeList; 444 } 445 446 447 448 @SuppressWarnings ("unchecked") 449 ArrayList new8() 450 { 451 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 452 453 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 454 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 455 PShortOption pshortoptionNode1; 456 { 457 TShortName tshortnameNode2; 459 POperand poperandNode3; 460 tshortnameNode2 = (TShortName)nodeArrayList1.get(0); 461 { 462 @SuppressWarnings ("unused") Object nullNode4 = null; 464 465 poperandNode3 = new AOperand(null); 466 } 467 468 pshortoptionNode1 = new AShortOption(tshortnameNode2, poperandNode3); 469 } 470 nodeList.add(pshortoptionNode1); 471 return nodeList; 472 } 473 474 475 476 @SuppressWarnings ("unchecked") 477 ArrayList new9() 478 { 479 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 480 481 @SuppressWarnings ("unused") ArrayList nodeArrayList3 = pop(); 482 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 483 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 484 PShortOption pshortoptionNode1; 485 { 486 TShortName tshortnameNode2; 488 POperand poperandNode3; 489 tshortnameNode2 = (TShortName)nodeArrayList1.get(0); 490 { 491 TOperandText toperandtextNode4; 493 toperandtextNode4 = (TOperandText)nodeArrayList3.get(0); 494 495 poperandNode3 = new AOperand(toperandtextNode4); 496 } 497 498 pshortoptionNode1 = new AShortOption(tshortnameNode2, poperandNode3); 499 } 500 nodeList.add(pshortoptionNode1); 501 return nodeList; 502 } 503 504 505 506 @SuppressWarnings ("unchecked") 507 ArrayList new10() 508 { 509 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 510 511 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 512 PLongOption plongoptionNode1; 513 { 514 TLongName tlongnameNode2; 516 @SuppressWarnings ("unused") Object nullNode3 = null; 517 tlongnameNode2 = (TLongName)nodeArrayList1.get(0); 518 519 plongoptionNode1 = new ALongOption(tlongnameNode2, null); 520 } 521 nodeList.add(plongoptionNode1); 522 return nodeList; 523 } 524 525 526 527 @SuppressWarnings ("unchecked") 528 ArrayList new11() 529 { 530 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 531 532 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 533 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 534 PLongOption plongoptionNode1; 535 { 536 TLongName tlongnameNode2; 538 POperand poperandNode3; 539 tlongnameNode2 = (TLongName)nodeArrayList1.get(0); 540 { 541 @SuppressWarnings ("unused") Object nullNode4 = null; 543 544 poperandNode3 = new AOperand(null); 545 } 546 547 plongoptionNode1 = new ALongOption(tlongnameNode2, poperandNode3); 548 } 549 nodeList.add(plongoptionNode1); 550 return nodeList; 551 } 552 553 554 555 @SuppressWarnings ("unchecked") 556 ArrayList new12() 557 { 558 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 559 560 @SuppressWarnings ("unused") ArrayList nodeArrayList3 = pop(); 561 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 562 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 563 PLongOption plongoptionNode1; 564 { 565 TLongName tlongnameNode2; 567 POperand poperandNode3; 568 tlongnameNode2 = (TLongName)nodeArrayList1.get(0); 569 { 570 TOperandText toperandtextNode4; 572 toperandtextNode4 = (TOperandText)nodeArrayList3.get(0); 573 574 poperandNode3 = new AOperand(toperandtextNode4); 575 } 576 577 plongoptionNode1 = new ALongOption(tlongnameNode2, poperandNode3); 578 } 579 nodeList.add(plongoptionNode1); 580 return nodeList; 581 } 582 583 584 585 @SuppressWarnings ("unchecked") 586 ArrayList new13() 587 { 588 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 589 590 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 591 LinkedList listNode2 = new LinkedList(); 592 { 593 PShortOption pshortoptionNode1; 595 pshortoptionNode1 = (PShortOption)nodeArrayList1.get(0); 596 if(pshortoptionNode1 != null) 597 { 598 listNode2.add(pshortoptionNode1); 599 } 600 } 601 nodeList.add(listNode2); 602 return nodeList; 603 } 604 605 606 607 @SuppressWarnings ("unchecked") 608 ArrayList new14() 609 { 610 @SuppressWarnings ("hiding") ArrayList nodeList = new ArrayList(); 611 612 @SuppressWarnings ("unused") ArrayList nodeArrayList2 = pop(); 613 @SuppressWarnings ("unused") ArrayList nodeArrayList1 = pop(); 614 LinkedList listNode3 = new LinkedList(); 615 { 616 LinkedList listNode1 = new LinkedList(); 618 PShortOption pshortoptionNode2; 619 listNode1 = (LinkedList)nodeArrayList1.get(0); 620 pshortoptionNode2 = (PShortOption)nodeArrayList2.get(0); 621 if(listNode1 != null) 622 { 623 listNode3.addAll(listNode1); 624 } 625 if(pshortoptionNode2 != null) 626 { 627 listNode3.add(pshortoptionNode2); 628 } 629 } 630 nodeList.add(listNode3); 631 return nodeList; 632 } 633 634 635 636 private static int[][][] actionTable; 637 658 private static int[][][] gotoTable; 659 668 private static String [] errorMessages; 669 679 private static int[] errors; 680 683 684 static 685 { 686 try 687 { 688 DataInputStream s = new DataInputStream ( 689 new BufferedInputStream ( 690 Parser.class.getResourceAsStream("parser.dat"))); 691 692 int length = s.readInt(); 694 Parser.actionTable = new int[length][][]; 695 for(int i = 0; i < Parser.actionTable.length; i++) 696 { 697 length = s.readInt(); 698 Parser.actionTable[i] = new int[length][3]; 699 for(int j = 0; j < Parser.actionTable[i].length; j++) 700 { 701 for(int k = 0; k < 3; k++) 702 { 703 Parser.actionTable[i][j][k] = s.readInt(); 704 } 705 } 706 } 707 708 length = s.readInt(); 710 gotoTable = new int[length][][]; 711 for(int i = 0; i < gotoTable.length; i++) 712 { 713 length = s.readInt(); 714 gotoTable[i] = new int[length][2]; 715 for(int j = 0; j < gotoTable[i].length; j++) 716 { 717 for(int k = 0; k < 2; k++) 718 { 719 gotoTable[i][j][k] = s.readInt(); 720 } 721 } 722 } 723 724 length = s.readInt(); 726 errorMessages = new String [length]; 727 for(int i = 0; i < errorMessages.length; i++) 728 { 729 length = s.readInt(); 730 StringBuffer buffer = new StringBuffer (); 731 732 for(int j = 0; j < length; j++) 733 { 734 buffer.append(s.readChar()); 735 } 736 errorMessages[i] = buffer.toString(); 737 } 738 739 length = s.readInt(); 741 errors = new int[length]; 742 for(int i = 0; i < errors.length; i++) 743 { 744 errors[i] = s.readInt(); 745 } 746 747 s.close(); 748 } 749 catch(Exception e) 750 { 751 throw new RuntimeException ("The file \"parser.dat\" is either missing or corrupted."); 752 } 753 } 754 } 755 | Popular Tags |