| 1 2 package org.nfunk.jep; 3 4 import java.util.Vector ; 5 import org.nfunk.jep.function.*; 6 import org.nfunk.jep.type.*; 7 8 public class Parserimplements ParserTreeConstants, ParserConstants { 9 protected JJTParserState jjtree = new JJTParserState();private JEP jep; 10 private SymbolTable symTab; 11 private OperatorSet opSet; 12 private int initialTokenManagerState = DEFAULT; 13 14 public Node parseStream(java.io.Reader stream, JEP jep_in) 15 throws ParseException { 16 restart(stream,jep_in); 17 enable_tracing(); 19 Node node = Start(); 20 if (node == null) throw new ParseException("No expression entered"); 21 return node.jjtGetChild(0); 22 } 23 24 28 public void restart(java.io.Reader stream, JEP jep_in) 29 { 30 ReInit(stream); 31 this.token_source.SwitchTo(initialTokenManagerState); 32 jep = jep_in; 33 symTab = jep.getSymbolTable(); 34 opSet = jep.getOperatorSet(); 35 } 36 55 public Node continueParse() throws ParseException 56 { 57 ASTStart node = Start(); 58 if (node==null) return null; 59 return node.jjtGetChild(0); 60 } 61 62 private void addToErrorList(String errorStr) { 63 jep.errorList.addElement(errorStr); 64 } 65 66 72 public void setInitialTokenManagerState(int state) 73 { 74 initialTokenManagerState = state; 75 } 76 83 private String replaceEscape(String inputStr) { 84 int len = inputStr.length(); 85 int p = 0; 86 int i; 87 String metachars = "tnrbf\\\"'"; 88 String chars = "\t\n\r\b\f\\\"'"; 89 90 StringBuffer output = new StringBuffer (); 91 92 while ((i = inputStr.indexOf('\\', p)) != -1) { 93 output.append(inputStr.substring(p, i)); 94 95 if (i+1 == len) break; 96 97 char metac = inputStr.charAt(i+1); 99 100 int k = metachars.indexOf(metac); 102 if (k == -1) { 103 output.append('\\'); 108 output.append(metac); 109 } else { 110 output.append(chars.charAt(k)); 112 } 113 114 p = i + 2; 116 } 117 118 if (p < len) 120 output.append(inputStr.substring(p)); 121 122 return output.toString(); 123 } 124 125 128 final public ASTStart Start() throws ParseException { 129 130 ASTStart jjtn000 = new ASTStart(JJTSTART); 131 boolean jjtc000 = true; 132 jjtree.openNodeScope(jjtn000); 133 try { 134 if (jj_2_1(1)) { 135 Expression(); 136 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 137 case 0: 138 jj_consume_token(0); 139 break; 140 case SEMI: 141 jj_consume_token(SEMI); 142 break; 143 default: 144 jj_la1[0] = jj_gen; 145 jj_consume_token(-1); 146 throw new ParseException(); 147 } 148 jjtree.closeNodeScope(jjtn000, true); 149 jjtc000 = false; 150 {if (true) return jjtn000;} 151 } else { 152 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 153 case 0: 154 case SEMI: 155 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 156 case 0: 157 jj_consume_token(0); 158 break; 159 case SEMI: 160 jj_consume_token(SEMI); 161 break; 162 default: 163 jj_la1[1] = jj_gen; 164 jj_consume_token(-1); 165 throw new ParseException(); 166 } 167 jjtree.closeNodeScope(jjtn000, true); 168 jjtc000 = false; 169 {if (true) return null;} 177 break; 178 default: 179 jj_la1[2] = jj_gen; 180 jj_consume_token(-1); 181 throw new ParseException(); 182 } 183 } 184 } catch (Throwable jjte000) { 185 if (jjtc000) { 186 jjtree.clearNodeScope(jjtn000); 187 jjtc000 = false; 188 } else { 189 jjtree.popNode(); 190 } 191 if (jjte000 instanceof RuntimeException ) { 192 {if (true) throw (RuntimeException )jjte000;} 193 } 194 if (jjte000 instanceof ParseException) { 195 {if (true) throw (ParseException)jjte000;} 196 } 197 {if (true) throw (Error )jjte000;} 198 } finally { 199 if (jjtc000) { 200 jjtree.closeNodeScope(jjtn000, true); 201 } 202 } 203 throw new Error ("Missing return statement in function"); 204 } 205 206 final public void Expression() throws ParseException { 210 if (jj_2_2(3)) { 211 AssignExpression(); 212 } else if (jj_2_3(1)) { 213 OrExpression(); 214 } else { 215 jj_consume_token(-1); 216 throw new ParseException(); 217 } 218 } 219 220 final public void AssignExpression() throws ParseException { 221 ASTFunNode jjtn001 = new ASTFunNode(JJTFUNNODE); 222 boolean jjtc001 = true; 223 jjtree.openNodeScope(jjtn001); 224 try { 225 Variable(); 226 jj_consume_token(ASSIGN); 227 Expression(); 228 jjtree.closeNodeScope(jjtn001, 2); 229 jjtc001 = false; 230 if (!jep.getAllowAssignment()) {if (true) throw new ParseException( 231 "Syntax Error (assignment not enabled)");} 232 233 jjtn001.setOperator(opSet.getAssign()); 234 } catch (Throwable jjte001) { 235 if (jjtc001) { 236 jjtree.clearNodeScope(jjtn001); 237 jjtc001 = false; 238 } else { 239 jjtree.popNode(); 240 } 241 if (jjte001 instanceof RuntimeException ) { 242 {if (true) throw (RuntimeException )jjte001;} 243 } 244 if (jjte001 instanceof ParseException) { 245 {if (true) throw (ParseException)jjte001;} 246 } 247 {if (true) throw (Error )jjte001;} 248 } finally { 249 if (jjtc001) { 250 jjtree.closeNodeScope(jjtn001, 2); 251 } 252 } 253 } 254 255 final public void OrExpression() throws ParseException { 256 AndExpression(); 257 label_1: 258 while (true) { 259 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 260 case OR: 261 ; 262 break; 263 default: 264 jj_la1[3] = jj_gen; 265 break label_1; 266 } 267 ASTFunNode jjtn001 = new ASTFunNode(JJTFUNNODE); 268 boolean jjtc001 = true; 269 jjtree.openNodeScope(jjtn001); 270 try { 271 jj_consume_token(OR); 272 AndExpression(); 273 jjtree.closeNodeScope(jjtn001, 2); 274 jjtc001 = false; 275 jjtn001.setOperator(opSet.getOr()); 276 } catch (Throwable jjte001) { 277 if (jjtc001) { 278 jjtree.clearNodeScope(jjtn001); 279 jjtc001 = false; 280 } else { 281 jjtree.popNode(); 282 } 283 if (jjte001 instanceof RuntimeException ) { 284 {if (true) throw (RuntimeException )jjte001;} 285 } 286 if (jjte001 instanceof ParseException) { 287 {if (true) throw (ParseException)jjte001;} 288 } 289 {if (true) throw (Error )jjte001;} 290 } finally { 291 if (jjtc001) { 292 jjtree.closeNodeScope(jjtn001, 2); 293 } 294 } 295 } 296 } 297 298 final public void AndExpression() throws ParseException { 299 EqualExpression(); 300 label_2: 301 while (true) { 302 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 303 case AND: 304 ; 305 break; 306 default: 307 jj_la1[4] = jj_gen; 308 break label_2; 309 } 310 ASTFunNode jjtn001 = new ASTFunNode(JJTFUNNODE); 311 boolean jjtc001 = true; 312 jjtree.openNodeScope(jjtn001); 313 try { 314 jj_consume_token(AND); 315 EqualExpression(); 316 jjtree.closeNodeScope(jjtn001, 2); 317 jjtc001 = false; 318 jjtn001.setOperator(opSet.getAnd()); 319 } catch (Throwable jjte001) { 320 if (jjtc001) { 321 jjtree.clearNodeScope(jjtn001); 322 jjtc001 = false; 323 } else { 324 jjtree.popNode(); 325 } 326 if (jjte001 instanceof RuntimeException ) { 327 {if (true) throw (RuntimeException )jjte001;} 328 } 329 if (jjte001 instanceof ParseException) { 330 {if (true) throw (ParseException)jjte001;} 331 } 332 {if (true) throw (Error )jjte001;} 333 } finally { 334 if (jjtc001) { 335 jjtree.closeNodeScope(jjtn001, 2); 336 } 337 } 338 } 339 } 340 341 final public void EqualExpression() throws ParseException { 342 RelationalExpression(); 343 label_3: 344 while (true) { 345 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 346 case EQ: 347 case NE: 348 ; 349 break; 350 default: 351 jj_la1[5] = jj_gen; 352 break label_3; 353 } 354 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 355 case NE: 356 ASTFunNode jjtn001 = new ASTFunNode(JJTFUNNODE); 357 boolean jjtc001 = true; 358 jjtree.openNodeScope(jjtn001); 359 try { 360 jj_consume_token(NE); 361 RelationalExpression(); 362 jjtree.closeNodeScope(jjtn001, 2); 363 jjtc001 = false; 364 jjtn001.setOperator(opSet.getNE()); 365 } catch (Throwable jjte001) { 366 if (jjtc001) { 367 jjtree.clearNodeScope(jjtn001); 368 jjtc001 = false; 369 } else { 370 jjtree.popNode(); 371 } 372 if (jjte001 instanceof RuntimeException ) { 373 {if (true) throw (RuntimeException )jjte001;} 374 } 375 if (jjte001 instanceof ParseException) { 376 {if (true) throw (ParseException)jjte001;} 377 } 378 {if (true) throw (Error )jjte001;} 379 } finally { 380 if (jjtc001) { 381 jjtree.closeNodeScope(jjtn001, 2); 382 } 383 } 384 break; 385 case EQ: 386 ASTFunNode jjtn002 = new ASTFunNode(JJTFUNNODE); 387 boolean jjtc002 = true; 388 jjtree.openNodeScope(jjtn002); 389 try { 390 jj_consume_token(EQ); 391 RelationalExpression(); 392 jjtree.closeNodeScope(jjtn002, 2); 393 jjtc002 = false; 394 jjtn002.setOperator(opSet.getEQ()); 395 } catch (Throwable jjte002) { 396 if (jjtc002) { 397 jjtree.clearNodeScope(jjtn002); 398 jjtc002 = false; 399 } else { 400 jjtree.popNode(); 401 } 402 if (jjte002 instanceof RuntimeException ) { 403 {if (true) throw (RuntimeException )jjte002;} 404 } 405 if (jjte002 instanceof ParseException) { 406 {if (true) throw (ParseException)jjte002;} 407 } 408 {if (true) throw (Error )jjte002;} 409 } finally { 410 if (jjtc002) { 411 jjtree.closeNodeScope(jjtn002, 2); 412 } 413 } 414 break; 415 default: 416 jj_la1[6] = jj_gen; 417 jj_consume_token(-1); 418 throw new ParseException(); 419 } 420 } 421 } 422 423 final public void RelationalExpression() throws ParseException { 424 AdditiveExpression(); 425 label_4: 426 while (true) { 427 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 428 case GT: 429 case LT: 430 case LE: 431 case GE: 432 ; 433 break; 434 default: 435 jj_la1[7] = jj_gen; 436 break label_4; 437 } 438 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 439 case LT: 440 ASTFunNode jjtn001 = new ASTFunNode(JJTFUNNODE); 441 boolean jjtc001 = true; 442 jjtree.openNodeScope(jjtn001); 443 try { 444 jj_consume_token(LT); 445 AdditiveExpression(); 446 jjtree.closeNodeScope(jjtn001, 2); 447 jjtc001 = false; 448 jjtn001.setOperator(opSet.getLT()); 449 } catch (Throwable jjte001) { 450 if (jjtc001) { 451 jjtree.clearNodeScope(jjtn001); 452 jjtc001 = false; 453 } else { 454 jjtree.popNode(); 455 } 456 if (jjte001 instanceof RuntimeException ) { 457 {if (true) throw (RuntimeException )jjte001;} 458 } 459 if (jjte001 instanceof ParseException) { 460 {if (true) throw (ParseException)jjte001;} 461 } 462 {if (true) throw (Error )jjte001;} 463 } finally { 464 if (jjtc001) { 465 jjtree.closeNodeScope(jjtn001, 2); 466 } 467 } 468 break; 469 case GT: 470 ASTFunNode jjtn002 = new ASTFunNode(JJTFUNNODE); 471 boolean jjtc002 = true; 472 jjtree.openNodeScope(jjtn002); 473 try { 474 jj_consume_token(GT); 475 AdditiveExpression(); 476 jjtree.closeNodeScope(jjtn002, 2); 477 jjtc002 = false; 478 jjtn002.setOperator(opSet.getGT()); 479 } catch (Throwable jjte002) { 480 if (jjtc002) { 481 jjtree.clearNodeScope(jjtn002); 482 jjtc002 = false; 483 } else { 484 jjtree.popNode(); 485 } 486 if (jjte002 instanceof RuntimeException ) { 487 {if (true) throw (RuntimeException )jjte002;} 488 } 489 if (jjte002 instanceof ParseException) { 490 {if (true) throw (ParseException)jjte002;} 491 } 492 {if (true) throw (Error )jjte002;} 493 } finally { 494 if (jjtc002) { 495 jjtree.closeNodeScope(jjtn002, 2); 496 } 497 } 498 break; 499 case LE: 500 ASTFunNode jjtn003 = new ASTFunNode(JJTFUNNODE); 501 boolean jjtc003 = true; 502 jjtree.openNodeScope(jjtn003); 503 try { 504 jj_consume_token(LE); 505 AdditiveExpression(); 506 jjtree.closeNodeScope(jjtn003, 2); 507 jjtc003 = false; 508 jjtn003.setOperator(opSet.getLE()); 509 } catch (Throwable jjte003) { 510 if (jjtc003) { 511 jjtree.clearNodeScope(jjtn003); 512 jjtc003 = false; 513 } else { 514 jjtree.popNode(); 515 } 516 if (jjte003 instanceof RuntimeException ) { 517 {if (true) throw (RuntimeException )jjte003;} 518 } 519 if (jjte003 instanceof ParseException) { 520 {if (true) throw (ParseException)jjte003;} 521 } 522 {if (true) throw (Error )jjte003;} 523 } finally { 524 if (jjtc003) { 525 jjtree.closeNodeScope(jjtn003, 2); 526 } 527 } 528 break; 529 case GE: 530 ASTFunNode jjtn004 = new ASTFunNode(JJTFUNNODE); 531 boolean jjtc004 = true; 532 jjtree.openNodeScope(jjtn004); 533 try { 534 jj_consume_token(GE); 535 AdditiveExpression(); 536 jjtree.closeNodeScope(jjtn004, 2); 537 jjtc004 = false; 538 jjtn004.setOperator(opSet.getGE()); 539 } catch (Throwable jjte004) { 540 if (jjtc004) { 541 jjtree.clearNodeScope(jjtn004); 542 jjtc004 = false; 543 } else { 544 jjtree.popNode(); 545 } 546 if (jjte004 instanceof RuntimeException ) { 547 {if (true) throw (RuntimeException )jjte004;} 548 } 549 if (jjte004 instanceof ParseException) { 550 {if (true) throw (ParseException)jjte004;} 551 } 552 {if (true) throw (Error )jjte004;} 553 } finally { 554 if (jjtc004) { 555 jjtree.closeNodeScope(jjtn004, 2); 556 } 557 } 558 break; 559 default: 560 jj_la1[8] = jj_gen; 561 jj_consume_token(-1); 562 throw new ParseException(); 563 } 564 } 565 } 566 567 final public void AdditiveExpression() throws ParseException { 568 MultiplicativeExpression(); 569 label_5: 570 while (true) { 571 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 572 case PLUS: 573 case MINUS: 574 ; 575 break; 576 default: 577 jj_la1[9] = jj_gen; 578 break label_5; 579 } 580 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 581 case PLUS: 582 ASTFunNode jjtn001 = new ASTFunNode(JJTFUNNODE); 583 boolean jjtc001 = true; 584 jjtree.openNodeScope(jjtn001); 585 try { 586 jj_consume_token(PLUS); 587 MultiplicativeExpression(); 588 jjtree.closeNodeScope(jjtn001, 2); 589 jjtc001 = false; 590 jjtn001.setOperator(opSet.getAdd()); 591 } catch (Throwable jjte001) { 592 if (jjtc001) { 593 jjtree.clearNodeScope(jjtn001); 594 jjtc001 = false; 595 } else { 596 jjtree.popNode(); 597 } 598 if (jjte001 instanceof RuntimeException ) { 599 {if (true) throw (RuntimeException )jjte001;} 600 } 601 if (jjte001 instanceof ParseException) { 602 {if (true) throw (ParseException)jjte001;} 603 } 604 {if (true) throw (Error )jjte001;} 605 } finally { 606 if (jjtc001) { 607 jjtree.closeNodeScope(jjtn001, 2); 608 } 609 } 610 break; 611 case MINUS: 612 ASTFunNode jjtn002 = new ASTFunNode(JJTFUNNODE); 613 boolean jjtc002 = true; 614 jjtree.openNodeScope(jjtn002); 615 try { 616 jj_consume_token(MINUS); 617 MultiplicativeExpression(); 618 jjtree.closeNodeScope(jjtn002, 2); 619 jjtc002 = false; 620 jjtn002.setOperator(opSet.getSubtract()); 621 } catch (Throwable jjte002) { 622 if (jjtc002) { 623 jjtree.clearNodeScope(jjtn002); 624 jjtc002 = false; 625 } else { 626 jjtree.popNode(); 627 } 628 if (jjte002 instanceof RuntimeException ) { 629 {if (true) throw (RuntimeException )jjte002;} 630 } 631 if (jjte002 instanceof ParseException) { 632 {if (true) throw (ParseException)jjte002;} 633 } 634 {if (true) throw (Error )jjte002;} 635 } finally { 636 if (jjtc002) { 637 jjtree.closeNodeScope(jjtn002, 2); 638 } 639 } 640 break; 641 default: 642 jj_la1[10] = jj_gen; 643 jj_consume_token(-1); 644 throw new ParseException(); 645 } 646 } 647 } 648 649 final public void MultiplicativeExpression() throws ParseException { 650 UnaryExpression(); 651 label_6: 652 while (true) { 653 if (jj_2_4(1)) { 654 ; 655 } else { 656 break label_6; 657 } 658 if (jj_2_5(1)) { 659 ASTFunNode jjtn001 = new ASTFunNode(JJTFUNNODE); 660 boolean jjtc001 = true; 661 jjtree.openNodeScope(jjtn001); 662 try { 663 PowerExpression(); 664 jjtree.closeNodeScope(jjtn001, 2); 665 jjtc001 = false; 666 if (!jep.implicitMul) {if (true) throw new ParseException( 667 "Syntax Error (implicit multiplication not enabled)");} 668 669 jjtn001.setOperator(opSet.getMultiply()); 670 } catch (Throwable jjte001) { 671 if (jjtc001) { 672 jjtree.clearNodeScope(jjtn001); 673 jjtc001 = false; 674 } else { 675 jjtree.popNode(); 676 } 677 if (jjte001 instanceof RuntimeException ) { 678 {if (true) throw (RuntimeException )jjte001;} 679 } 680 if (jjte001 instanceof ParseException) { 681 {if (true) throw (ParseException)jjte001;} 682 } 683 {if (true) throw (Error )jjte001;} 684 } finally { 685 if (jjtc001) { 686 jjtree.closeNodeScope(jjtn001, 2); 687 } 688 } 689 } else { 690 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { 691 case MUL: 692 ASTFunNode jjtn002 = new ASTFunNode(JJTFUNNODE); 693 boolean jjtc002 = true; 694 jjtree.openNodeScope(jjtn002); 695 try { 696 jj_consume_token(MUL); 697 UnaryExpression(); 698 jjtree.closeNodeScope(jjtn002, 2); 699 jjtc002 = false; 700 jjtn002.setOperator(opSet.getMultiply()); 701 } catch (Throwable jjte002) { 702 if (jjtc002) { 703 jjtree.clearNodeScope(jjtn002); 704 jjtc002 = false; 705 } else { 706 jjtree.popNode(); 707 } 708 if (jjte002 instanceof RuntimeException ) { 709 {if (true) throw (RuntimeException )jjte002;} 710 } 711 if (jjte002 instanceof ParseException) { 712 {if (true) throw (ParseException)jjte002;} 713 } 714 {if (true) throw (Error )jjte002;} 715 } finally { 716 if (jjtc002) { 717 jjtree.closeNodeScope(jjtn002, 2); 718 } 719 } 720 break; 721 case DOT: 722 ASTFunNode jjtn003 = new ASTFunNode(JJTFUNNODE); 723 boolean jjtc003 = true; 724 jjtree.openNodeScope(jjtn003); 725 try { 726 jj_consume_token(DOT); 727 UnaryExpression(); 728 jjtree.closeNodeScope(jjtn003, 2); 729 jjtc003 = false; 730 jjtn003.setOperator(opSet.getDot()); 731 } catch (Throwable jjte003) { 732 if (jjtc003) { 733 jjtree.clearNodeScope(jjtn003); 734 jjtc003 = false; 735 } else { 736 jjtree.popNode(); 737 } 738 if (jjte003 instanceof RuntimeException ) { 739 {if (true) throw (RuntimeException )jjte003;} 740 } 741 if (jjte003 instanceof ParseException) { 742 {if (true) throw (ParseException)jjte003;} 743 } 744 {if (true) throw (Error )jjte003;} 745 } finally { 746 if (jjtc003) { 747 jjtree.closeNodeScope(jjtn003, 2); 748 } 749 } 750 break; 751 case CROSS: 752 ASTFunNode jjtn004 = new ASTFunNode(JJTFUNNODE); 753 boolean jjtc004 = true; 754 jjtree.openNodeScope(jjtn004); 755 try { 756 jj_consume_token(CROSS); 757 UnaryExpression(); 758 jjtree.closeNodeScope(jjtn004, 2); 759 jjtc004 = false; 760 jjtn004.setOperator(opSet.getCross()); 761 } catch (Throwable jjte004) { 762 if (jjtc004) { 763 jjtree.clearNodeScope(jjtn004); 764 jjtc004 = false; 765 } else { 766 jjtree.popNode(); 767 } 768 if (jjte004 instanceof RuntimeException ) { 769 {if (true) throw (RuntimeException )jjte004;} 770 } 771 if (jjte004 instanceof ParseException) { 772 {if (true) throw (ParseException)jjte004;} 773 } 774 {if (true) throw (Error )jjte004;} 775 } finally { 776 if (jjtc004) { 777 jjtree.closeNodeScope(jjtn004, 2); 778 } 779 } 780 break; 781 case DIV: 782 ASTFunNode jjtn005 = new ASTFunNode(JJTFUNNODE); 783 boolean jjtc005 = true;
|