| 1 3 package org.codehaus.groovy.antlr.parser; 4 import org.codehaus.groovy.antlr.*; 5 import java.util.*; 6 import java.io.InputStream ; 7 import java.io.Reader ; 8 import antlr.InputBuffer; 9 import antlr.LexerSharedInputState; 10 11 import antlr.TokenBuffer; 12 import antlr.TokenStreamException; 13 import antlr.TokenStreamIOException; 14 import antlr.ANTLRException; 15 import antlr.LLkParser; 16 import antlr.Token; 17 import antlr.TokenStream; 18 import antlr.RecognitionException; 19 import antlr.NoViableAltException; 20 import antlr.MismatchedTokenException; 21 import antlr.SemanticException; 22 import antlr.ParserSharedInputState; 23 import antlr.collections.impl.BitSet; 24 import antlr.collections.AST; 25 import java.util.Hashtable ; 26 import antlr.ASTFactory; 27 import antlr.ASTPair; 28 import antlr.collections.impl.ASTArray; 29 30 206 public class GroovyRecognizer extends antlr.LLkParser implements GroovyTokenTypes 207 { 208 209 210 public static GroovyRecognizer make(GroovyLexer lexer) { 211 GroovyRecognizer parser = new GroovyRecognizer(lexer.plumb()); 212 parser.lexer = lexer; 214 lexer.parser = parser; 215 parser.setASTNodeClass("org.codehaus.groovy.antlr.GroovySourceAST"); 216 parser.warningList = new ArrayList(); 217 return parser; 218 } 219 public static GroovyRecognizer make(InputStream in) { return make(new GroovyLexer(in)); } 221 public static GroovyRecognizer make(Reader in) { return make(new GroovyLexer(in)); } 222 public static GroovyRecognizer make(InputBuffer in) { return make(new GroovyLexer(in)); } 223 public static GroovyRecognizer make(LexerSharedInputState in) { return make(new GroovyLexer(in)); } 224 225 private static GroovySourceAST dummyVariableToforceClassLoaderToFindASTClass = new GroovySourceAST(); 226 227 List warningList; 228 public List getWarningList() { return warningList; } 229 230 boolean compatibilityMode = true; public boolean isCompatibilityMode() { return compatibilityMode; } 232 public void setCompatibilityMode(boolean z) { compatibilityMode = z; } 233 234 GroovyLexer lexer; 235 public GroovyLexer getLexer() { return lexer; } 236 public void setFilename(String f) { super.setFilename(f); lexer.setFilename(f); } 237 private SourceBuffer sourceBuffer; 238 public void setSourceBuffer(SourceBuffer sourceBuffer) { 239 this.sourceBuffer = sourceBuffer; 240 } 241 242 249 public AST create(int type, String txt, Token first, Token last) { 250 AST t = astFactory.create(type,txt); 251 if ( t != null && first != null) { 252 t.initialize(first); 254 t.initialize(type,txt); 256 } 257 258 if ((t instanceof GroovySourceAST) && last != null) { 259 GroovySourceAST node = (GroovySourceAST)t; 260 node.setLast(last); 261 262 if (sourceBuffer != null) { 264 String snippet = sourceBuffer.getSnippet( 265 new LineColumn(first.getLine(),first.getColumn()), 266 new LineColumn(last.getLine(),last.getColumn()) 267 ); 268 node.setSnippet(snippet); 269 } 270 } 271 return t; 272 } 273 274 275 public static boolean tracing = false; public void traceIn(String rname) throws TokenStreamException { 278 if (!GroovyRecognizer.tracing) return; 279 super.traceIn(rname); 280 } 281 public void traceOut(String rname) throws TokenStreamException { 282 if (!GroovyRecognizer.tracing) return; 283 if (returnAST != null) rname += returnAST.toStringList(); 284 super.traceOut(rname); 285 } 286 287 public void requireFailed(String problem, String solution) throws SemanticException { 289 Token lt = null; 291 try { lt = LT(1); } 292 catch (TokenStreamException ee) { } 293 if (lt == null) lt = Token.badToken; 294 throw new SemanticException(problem + ";\n solution: " + solution, 295 getFilename(), lt.getLine(), lt.getColumn()); 296 } 297 298 public void addWarning(String warning, String solution) { 299 Token lt = null; 300 try { lt = LT(1); } 301 catch (TokenStreamException ee) { } 302 if (lt == null) lt = Token.badToken; 303 304 Map row = new HashMap(); 305 row.put("warning" ,warning); 306 row.put("solution",solution); 307 row.put("filename",getFilename()); 308 row.put("line" ,new Integer (lt.getLine())); 309 row.put("column" ,new Integer (lt.getColumn())); 310 warningList.add(row); 312 } 313 314 private void require(boolean z, String problem, String solution) throws SemanticException { 316 if (!z) requireFailed(problem, solution); 317 } 318 319 320 private boolean isUpperCase(Token x) { 323 if (x == null || x.getType() != IDENT) return false; String xtext = x.getText(); 325 return (xtext.length() > 0 && Character.isUpperCase(xtext.charAt(0))); 326 } 327 328 private AST currentClass = null; private boolean isConstructorIdent(Token x) { 332 if (currentClass == null) return false; 333 if (currentClass.getType() != IDENT) return false; String cname = currentClass.getText(); 335 336 if (x == null || x.getType() != IDENT) return false; return cname.equals(x.getText()); 338 } 339 340 private int sepToken = EOF; 344 345 private boolean argListHasLabels = false; 348 349 private AST lastPathExpression = null; 352 353 private final int LC_STMT = 1, LC_INIT = 2; 359 360 366 private int ltCounter = 0; 367 368 374 private static final boolean ANTLR_LOOP_EXIT = false; 381 382 protected GroovyRecognizer(TokenBuffer tokenBuf, int k) { 383 super(tokenBuf,k); 384 tokenNames = _tokenNames; 385 buildTokenTypeASTClassMap(); 386 astFactory = new ASTFactory(getTokenTypeToASTClassMap()); 387 } 388 389 public GroovyRecognizer(TokenBuffer tokenBuf) { 390 this(tokenBuf,3); 391 } 392 393 protected GroovyRecognizer(TokenStream lexer, int k) { 394 super(lexer,k); 395 tokenNames = _tokenNames; 396 buildTokenTypeASTClassMap(); 397 astFactory = new ASTFactory(getTokenTypeToASTClassMap()); 398 } 399 400 public GroovyRecognizer(TokenStream lexer) { 401 this(lexer,3); 402 } 403 404 public GroovyRecognizer(ParserSharedInputState state) { 405 super(state,3); 406 tokenNames = _tokenNames; 407 buildTokenTypeASTClassMap(); 408 astFactory = new ASTFactory(getTokenTypeToASTClassMap()); 409 } 410 411 public final void compilationUnit() throws RecognitionException, TokenStreamException { 412 413 returnAST = null; 414 ASTPair currentAST = new ASTPair(); 415 AST compilationUnit_AST = null; 416 417 { 418 switch ( LA(1)) { 419 case SH_COMMENT: 420 { 421 match(SH_COMMENT); 422 break; 423 } 424 case EOF: 425 case FINAL: 426 case ABSTRACT: 427 case STRICTFP: 428 case LITERAL_package: 429 case LITERAL_import: 430 case LITERAL_static: 431 case LITERAL_def: 432 case AT: 433 case IDENT: 434 case LBRACK: 435 case LPAREN: 436 case LITERAL_class: 437 case LITERAL_interface: 438 case LITERAL_enum: 439 case LITERAL_super: 440 case LITERAL_void: 441 case LITERAL_boolean: 442 case LITERAL_byte: 443 case LITERAL_char: 444 case LITERAL_short: 445 case LITERAL_int: 446 case LITERAL_float: 447 case LITERAL_long: 448 case LITERAL_double: 449 case LITERAL_any: 450 case STAR: 451 case LITERAL_private: 452 case LITERAL_public: 453 case LITERAL_protected: 454 case LITERAL_transient: 455 case LITERAL_native: 456 case LITERAL_threadsafe: 457 case LITERAL_synchronized: 458 case LITERAL_volatile: 459 case LCURLY: 460 case SEMI: 461 case NLS: 462 case LITERAL_this: 463 case STRING_LITERAL: 464 case LITERAL_if: 465 case LITERAL_while: 466 case LITERAL_with: 467 case LITERAL_switch: 468 case LITERAL_for: 469 case LITERAL_return: 470 case LITERAL_break: 471 case LITERAL_continue: 472 case LITERAL_throw: 473 case LITERAL_assert: 474 case PLUS: 475 case MINUS: 476 case LITERAL_try: 477 case INC: 478 case DEC: 479 case BNOT: 480 case LNOT: 481 case DOLLAR: 482 case STRING_CTOR_START: 483 case LITERAL_new: 484 case LITERAL_true: 485 case LITERAL_false: 486 case LITERAL_null: 487 case NUM_INT: 488 case NUM_FLOAT: 489 case NUM_LONG: 490 case NUM_DOUBLE: 491 case NUM_BIG_INT: 492 case NUM_BIG_DECIMAL: 493 { 494 break; 495 } 496 default: 497 { 498 throw new NoViableAltException(LT(1), getFilename()); 499 } 500 } 501 } 502 nls(); 503 { 504 boolean synPredMatched5 = false; 505 if (((LA(1)==LITERAL_package||LA(1)==AT) && (LA(2)==IDENT) && (_tokenSet_0.member(LA(3))))) { 506 int _m5 = mark(); 507 synPredMatched5 = true; 508 inputState.guessing++; 509 try { 510 { 511 annotationsOpt(); 512 match(LITERAL_package); 513 } 514 } 515 catch (RecognitionException pe) { 516 synPredMatched5 = false; 517 } 518 rewind(_m5); 519 inputState.guessing--; 520 } 521 if ( synPredMatched5 ) { 522 packageDefinition(); 523 astFactory.addASTChild(currentAST, returnAST); 524 } 525 else if ((_tokenSet_1.member(LA(1))) && (_tokenSet_2.member(LA(2))) && (_tokenSet_3.member(LA(3)))) { 526 { 527 switch ( LA(1)) { 528 case FINAL: 529 case ABSTRACT: 530 case STRICTFP: 531 case LITERAL_import: 532 case LITERAL_static: 533 case LITERAL_def: 534 case AT: 535 case IDENT: 536 case LBRACK: 537 case LPAREN: 538 case LITERAL_class: 539 case LITERAL_interface: 540 case LITERAL_enum: 541 case LITERAL_super: 542 case LITERAL_void: 543 case LITERAL_boolean: 544 case LITERAL_byte: 545 case LITERAL_char: 546 case LITERAL_short: 547 case LITERAL_int: 548 case LITERAL_float: 549 case LITERAL_long: 550 case LITERAL_double: 551 case LITERAL_any: 552 case STAR: 553 case LITERAL_private: 554 case LITERAL_public: 555 case LITERAL_protected: 556 case LITERAL_transient: 557 case LITERAL_native: 558 case LITERAL_threadsafe: 559 case LITERAL_synchronized: 560 case LITERAL_volatile: 561 case LCURLY: 562 case LITERAL_this: 563 case STRING_LITERAL: 564 case LITERAL_if: 565 case LITERAL_while: 566 case LITERAL_with: 567 case LITERAL_switch: 568 case LITERAL_for: 569 case LITERAL_return: 570 case LITERAL_break: 571 case LITERAL_continue: 572 case LITERAL_throw: 573 case LITERAL_assert: 574 case PLUS: 575 case MINUS: 576 case LITERAL_try: 577 case INC: 578 case DEC: 579 case BNOT: 580 case LNOT: 581 case DOLLAR: 582 case STRING_CTOR_START: 583 case LITERAL_new: 584 case LITERAL_true: 585 case LITERAL_false: 586 case LITERAL_null: 587 case NUM_INT: 588 case NUM_FLOAT: 589 case NUM_LONG: 590 case NUM_DOUBLE: 591 case NUM_BIG_INT: 592 case NUM_BIG_DECIMAL: 593 { 594 statement(EOF); 595 astFactory.addASTChild(currentAST, returnAST); 596 break; 597 } 598 case EOF: 599 case SEMI: 600 case NLS: 601 { 602 break; 603 } 604 default: 605 { 606 throw new NoViableAltException(LT(1), getFilename()); 607 } 608 } 609 } 610 } 611 else { 612 throw new NoViableAltException(LT(1), getFilename()); 613 } 614 615 } 616 { 617 _loop9: 618 do { 619 if ((LA(1)==SEMI||LA(1)==NLS)) { 620 sep(); 621 { 622 switch ( LA(1)) { 623 case FINAL: 624 case ABSTRACT: 625 case STRICTFP: 626 case LITERAL_import: 627 case LITERAL_static: 628 case LITERAL_def: 629 case AT: 630 case IDENT: 631 case LBRACK: 632 case LPAREN: 633 case LITERAL_class: 634 case LITERAL_interface: 635 case LITERAL_enum: 636 case LITERAL_super: 637 case LITERAL_void: 638 case LITERAL_boolean: 639 case LITERAL_byte: 640 case LITERAL_char: 641 case LITERAL_short: 642 case LITERAL_int: 643 case LITERAL_float: 644 case LITERAL_long: 645 case LITERAL_double: 646 case LITERAL_any: 647 case STAR: 648 case LITERAL_private: 649 case LITERAL_public: 650 case LITERAL_protected: 651 case LITERAL_transient: 652 case LITERAL_native: 653 case LITERAL_threadsafe: 654 case LITERAL_synchronized: 655 case LITERAL_volatile: 656 case LCURLY: 657 case LITERAL_this: 658 case STRING_LITERAL: 659 case LITERAL_if: 660 case LITERAL_while: 661 case LITERAL_with: 662 case LITERAL_switch: 663 case LITERAL_for: 664 case LITERAL_return: 665 case LITERAL_break: 666 case LITERAL_continue: 667 case LITERAL_throw: 668 case LITERAL_assert: 669 case PLUS: 670 case MINUS: 671 case LITERAL_try: 672 case INC: 673 case DEC: 674 case BNOT: 675 case LNOT: 676 case DOLLAR: 677 case STRING_CTOR_START: 678 case LITERAL_new: 679 case LITERAL_true: 680 case LITERAL_false: 681 case LITERAL_null: 682 case NUM_INT: 683 case NUM_FLOAT: 684 case NUM_LONG: 685 case NUM_DOUBLE: 686 case NUM_BIG_INT: 687 case NUM_BIG_DECIMAL: 688 { 689 statement(sepToken); 690 astFactory.addASTChild(currentAST, returnAST); 691 break; 692 } 693 case EOF: 694 case SEMI: 695 case NLS: 696 { 697 break; 698 } 699 default: 700 { 701 throw new NoViableAltException(LT(1), getFilename()); 702 } 703 } 704 } 705 } 706 else { 707 break _loop9; 708 } 709 710 } while (true); 711 } 712 match(Token.EOF_TYPE); 713 compilationUnit_AST = (AST)currentAST.root; 714 returnAST = compilationUnit_AST; 715 } 716 717 718 public final void nls() throws RecognitionException, TokenStreamException { 719 720 returnAST = null; 721 ASTPair currentAST = new ASTPair(); 722 AST nls_AST = null; 723 724 { 725 if ((LA(1)==NLS) && (_tokenSet_4.member(LA(2))) && (_tokenSet_5.member(LA(3)))) { 726 match(NLS); 727 } 728 else if ((_tokenSet_4.member(LA(1))) && (_tokenSet_5.member(LA(2))) && (_tokenSet_5.member(LA(3)))) { 729 } 730 else { 731 throw new NoViableAltException(LT(1), getFilename()); 732 } 733 734 } 735 returnAST = nls_AST; 736 } 737 738 public final void annotationsOpt() throws RecognitionException, TokenStreamException { 739 740 returnAST = null; 741 ASTPair currentAST = new ASTPair(); 742 AST annotationsOpt_AST = null; 743 Token first = LT(1); 744 745 { 746 _loop79: 747 do { 748 if ((LA(1)==AT)) { 749 annotation(); 750 astFactory.addASTChild(currentAST, returnAST); 751 nls(); 752 } 753 else { 754 break _loop79; 755 } 756 757 } while (true); 758 } 759 if ( inputState.guessing==0 ) { 760 annotationsOpt_AST = (AST)currentAST.root; 761 annotationsOpt_AST = (AST)astFactory.make( (new ASTArray(2)).add(create(ANNOTATIONS,"ANNOTATIONS",first,LT(1))).add(annotationsOpt_AST)); 762 currentAST.root = annotationsOpt_AST; 763 currentAST.child = annotationsOpt_AST!=null &&annotationsOpt_AST.getFirstChild()!=null ? 764 annotationsOpt_AST.getFirstChild() : annotationsOpt_AST; 765 currentAST.advanceChildToEnd(); 766 } 767 annotationsOpt_AST = (AST)currentAST.root; 768 returnAST = annotationsOpt_AST; 769 } 770 771 public final void packageDefinition() throws RecognitionException, TokenStreamException { 772 773 returnAST = null; 774 ASTPair currentAST = new ASTPair(); 775 AST packageDefinition_AST = null; 776 Token p = null; 777 AST p_AST = null; 778 779 annotationsOpt(); 780 astFactory.addASTChild(currentAST, returnAST); 781 p = LT(1); 782 p_AST = astFactory.create(p); 783 astFactory.makeASTRoot(currentAST, p_AST); 784 match(LITERAL_package); 785 if ( inputState.guessing==0 ) { 786 p_AST.setType(PACKAGE_DEF); 787 } 788 identifier(); 789 astFactory.addASTChild(currentAST, returnAST); 790 packageDefinition_AST = (AST)currentAST.root; 791 returnAST = packageDefinition_AST; 792 } 793 794 798 public final void statement( 799 int prevToken 800 ) throws RecognitionException, TokenStreamException { 801 802 returnAST = null; 803 ASTPair currentAST = new ASTPair(); 804 AST statement_AST = null; 805 AST pfx_AST = null; 806 AST m_AST = null; 807 Token sp = null; 808 AST sp_AST = null; 809 810 switch ( LA(1)) { 811 case LITERAL_if: 812 { 813 AST tmp4_AST = null; 814 tmp4_AST = astFactory.create(LT(1)); 815 astFactory.makeASTRoot(currentAST, tmp4_AST); 816 match(LITERAL_if); 817 match(LPAREN); 818 strictContextExpression(); 819 astFactory.addASTChild(currentAST, returnAST); 820 match(RPAREN); 821 nlsWarn(); 822 compatibleBodyStatement(); 823 astFactory.addASTChild(currentAST, returnAST); 824 { 825 boolean synPredMatched263 = false; 826 if (((_tokenSet_6.member(LA(1))) && (_tokenSet_7.member(LA(2))) && (_tokenSet_8.member(LA(3))))) { 827 int _m263 = mark(); 828 synPredMatched263 = true; 829 inputState.guessing++; 830 try { 831 { 832 { 833 switch ( LA(1)) { 834 case SEMI: 835 case NLS: 836 { 837 sep(); 838 break; 839 } 840 case LITERAL_else: 841 { 842 break; 843 } 844 default: 845 { 846 throw new NoViableAltException(LT(1), getFilename()); 847 &n
|