1#!/usr/bin/ruby 2# encoding: utf-8 3 4require 'antlr3/test/functional' 5 6 7class TestASTRewritingTreeParsers < ANTLR3::Test::Functional 8 inline_grammar( <<-'END' ) 9 grammar FlatList; 10 options { 11 language=Ruby; 12 output=AST; 13 } 14 a : ID INT; 15 ID : 'a'..'z'+ ; 16 INT : '0'..'9'+; 17 WS : (' '|'\n') {$channel=HIDDEN;} ; 18 END 19 20 inline_grammar( <<-'END' ) 21 tree grammar FlatListWalker; 22 options { 23 language=Ruby; 24 output=AST; 25 ASTLabelType=CommonTree; 26 tokenVocab=FlatList; 27 } 28 29 a : ID INT -> INT ID; 30 END 31 32 inline_grammar( <<-'END' ) 33 grammar SimpleTree; 34 options { 35 language=Ruby; 36 output=AST; 37 } 38 a : ID INT -> ^(ID INT); 39 ID : 'a'..'z'+ ; 40 INT : '0'..'9'+; 41 WS : (' '|'\n') {$channel=HIDDEN;} ; 42 END 43 44 inline_grammar( <<-'END' ) 45 tree grammar SimpleTreeWalker; 46 options { 47 language=Ruby; 48 output=AST; 49 ASTLabelType=CommonTree; 50 tokenVocab=SimpleTree; 51 } 52 a : ^(ID INT) -> ^(INT ID); 53 END 54 55 inline_grammar( <<-END ) 56 grammar CombinedRewriteAndAuto; 57 options { 58 language=Ruby; 59 output=AST; 60 } 61 a : ID INT -> ^(ID INT) | INT ; 62 ID : 'a'..'z'+ ; 63 INT : '0'..'9'+; 64 WS : (' '|'\\n') {$channel=HIDDEN;} ; 65 END 66 67 inline_grammar( <<-END ) 68 tree grammar CombinedRewriteAndAutoTree; 69 options { 70 language=Ruby; 71 output=AST; 72 ASTLabelType=CommonTree; 73 tokenVocab=CombinedRewriteAndAuto; 74 } 75 a : ^(ID INT) -> ^(INT ID) | INT; 76 END 77 78 inline_grammar( <<-'END' ) 79 grammar AvoidDup; 80 options { 81 language=Ruby; 82 output=AST; 83 } 84 a : ID ; 85 ID : 'a'..'z'+ ; 86 INT : '0'..'9'+; 87 WS : (' '|'\n') {$channel=HIDDEN;} ; 88 END 89 90 inline_grammar( <<-'END' ) 91 tree grammar AvoidDupWalker; 92 options { 93 language=Ruby; 94 output=AST; 95 ASTLabelType=CommonTree; 96 tokenVocab=AvoidDup; 97 } 98 a : ID -> ^(ID ID); 99 END 100 101 inline_grammar( <<-'END' ) 102 grammar Loop; 103 options { 104 language=Ruby; 105 output=AST; 106 } 107 a : ID+ INT+ -> (^(ID INT))+ ; 108 ID : 'a'..'z'+ ; 109 INT : '0'..'9'+; 110 WS : (' '|'\n') {$channel=HIDDEN;} ; 111 END 112 113 inline_grammar( <<-'END' ) 114 tree grammar LoopWalker; 115 options { 116 language=Ruby; 117 output=AST; 118 ASTLabelType=CommonTree; 119 tokenVocab=Loop; 120 } 121 a : (^(ID INT))+ -> INT+ ID+; 122 END 123 124 inline_grammar( <<-'END' ) 125 grammar AutoDup; 126 options { 127 language=Ruby; 128 output=AST; 129 } 130 a : ID ; 131 ID : 'a'..'z'+ ; 132 INT : '0'..'9'+; 133 WS : (' '|'\n') {$channel=HIDDEN;} ; 134 END 135 136 inline_grammar( <<-'END' ) 137 tree grammar AutoDupWalker; 138 options { 139 language=Ruby; 140 output=AST; 141 ASTLabelType=CommonTree; 142 tokenVocab=AutoDup; 143 } 144 a : ID; 145 END 146 147 inline_grammar( <<-'END' ) 148 grammar AutoDupRule; 149 options { 150 language=Ruby; 151 output=AST; 152 } 153 a : ID INT ; 154 ID : 'a'..'z'+ ; 155 INT : '0'..'9'+; 156 WS : (' '|'\n') {$channel=HIDDEN;} ; 157 END 158 159 inline_grammar( <<-'END' ) 160 tree grammar AutoDupRuleWalker; 161 options { 162 language=Ruby; 163 output=AST; 164 ASTLabelType=CommonTree; 165 tokenVocab=AutoDupRule; 166 } 167 a : b c ; 168 b : ID ; 169 c : INT ; 170 END 171 172 inline_grammar( <<-'END' ) 173 grammar AutoWildcard; 174 options {language=Ruby;output=AST;} 175 a : ID INT ; 176 ID : 'a'..'z'+ ; 177 INT : '0'..'9'+; 178 WS : (' '|'\n') {$channel=HIDDEN;} ; 179 END 180 181 inline_grammar( <<-'END' ) 182 tree grammar AutoWildcardWalker; 183 options {language=Ruby;output=AST; ASTLabelType=CommonTree; tokenVocab=AutoWildcard;} 184 a : ID . 185 ; 186 END 187 188 inline_grammar( <<-'END' ) 189 grammar AutoWildcard2; 190 options {language=Ruby;output=AST;} 191 a : ID INT -> ^(ID INT); 192 ID : 'a'..'z'+ ; 193 INT : '0'..'9'+; 194 WS : (' '|'\n') {$channel=HIDDEN;} ; 195 END 196 197 inline_grammar( <<-'END' ) 198 tree grammar AutoWildcard2Walker; 199 options {language=Ruby;output=AST; ASTLabelType=CommonTree; tokenVocab=AutoWildcard2;} 200 a : ^(ID .) 201 ; 202 END 203 204 inline_grammar( <<-'END' ) 205 grammar AutoWildcardWithLabel; 206 options {language=Ruby;output=AST;} 207 a : ID INT ; 208 ID : 'a'..'z'+ ; 209 INT : '0'..'9'+; 210 WS : (' '|'\n') {$channel=HIDDEN;} ; 211 END 212 213 inline_grammar( <<-'END' ) 214 tree grammar AutoWildcardWithLabelWalker; 215 options {language=Ruby;output=AST; ASTLabelType=CommonTree; tokenVocab=AutoWildcardWithLabel;} 216 a : ID c=. 217 ; 218 END 219 220 inline_grammar( <<-'END' ) 221 grammar AutoWildcardWithListLabel; 222 options {language=Ruby;output=AST;} 223 a : ID INT ; 224 ID : 'a'..'z'+ ; 225 INT : '0'..'9'+; 226 WS : (' '|'\n') {$channel=HIDDEN;} ; 227 END 228 229 inline_grammar( <<-'END' ) 230 tree grammar AutoWildcardWithListLabelWalker; 231 options {language=Ruby;output=AST; ASTLabelType=CommonTree; tokenVocab=AutoWildcardWithListLabel;} 232 a : ID c+=. 233 ; 234 END 235 236 inline_grammar( <<-'END' ) 237 grammar AutoDupMultiple; 238 options { 239 language=Ruby; 240 output=AST; 241 } 242 a : ID ID INT; 243 ID : 'a'..'z'+ ; 244 INT : '0'..'9'+; 245 WS : (' '|'\n') {$channel=HIDDEN;} ; 246 END 247 248 inline_grammar( <<-'END' ) 249 tree grammar AutoDupMultipleWalker; 250 options { 251 language=Ruby; 252 output=AST; 253 ASTLabelType=CommonTree; 254 tokenVocab=AutoDupMultiple; 255 } 256 a : ID ID INT 257 ; 258 END 259 260 inline_grammar( <<-'END' ) 261 grammar AutoDupTree; 262 options { 263 language=Ruby; 264 output=AST; 265 } 266 a : ID INT -> ^(ID INT); 267 ID : 'a'..'z'+ ; 268 INT : '0'..'9'+; 269 WS : (' '|'\n') {$channel=HIDDEN;} ; 270 END 271 272 inline_grammar( <<-'END' ) 273 tree grammar AutoDupTreeWalker; 274 options { 275 language=Ruby; 276 output=AST; 277 ASTLabelType=CommonTree; 278 tokenVocab=AutoDupTree; 279 } 280 a : ^(ID INT) 281 ; 282 END 283 284 inline_grammar( <<-'END' ) 285 grammar AutoDupTreeWithLabels; 286 options { 287 language=Ruby; 288 output=AST; 289 } 290 a : ID INT -> ^(ID INT); 291 ID : 'a'..'z'+ ; 292 INT : '0'..'9'+; 293 WS : (' '|'\n') {$channel=HIDDEN;} ; 294 END 295 296 inline_grammar( <<-'END' ) 297 tree grammar AutoDupTreeWithLabelsWalker; 298 options { 299 language=Ruby; 300 output=AST; 301 ASTLabelType=CommonTree; 302 tokenVocab=AutoDupTreeWithLabels; 303 } 304 a : ^(x=ID y=INT) 305 ; 306 END 307 308 inline_grammar( <<-'END' ) 309 grammar AutoDupTreeWithListLabels; 310 options { 311 language=Ruby; 312 output=AST; 313 } 314 a : ID INT -> ^(ID INT); 315 ID : 'a'..'z'+ ; 316 INT : '0'..'9'+; 317 WS : (' '|'\n') {$channel=HIDDEN;} ; 318 END 319 320 inline_grammar( <<-'END' ) 321 tree grammar AutoDupTreeWithListLabelsWalker; 322 options { 323 language=Ruby; 324 output=AST; 325 ASTLabelType=CommonTree; 326 tokenVocab=AutoDupTreeWithListLabels; 327 } 328 a : ^(x+=ID y+=INT) 329 ; 330 END 331 332 inline_grammar( <<-'END' ) 333 grammar AutoDupTreeWithRuleRoot; 334 options { 335 language=Ruby; 336 output=AST; 337 } 338 a : ID INT -> ^(ID INT); 339 ID : 'a'..'z'+ ; 340 INT : '0'..'9'+; 341 WS : (' '|'\n') {$channel=HIDDEN;} ; 342 END 343 344 inline_grammar( <<-'END' ) 345 tree grammar AutoDupTreeWithRuleRootWalker; 346 options { 347 language=Ruby; 348 output=AST; 349 ASTLabelType=CommonTree; 350 tokenVocab=AutoDupTreeWithRuleRoot; 351 } 352 a : ^(b INT) ; 353 b : ID ; 354 END 355 356 inline_grammar( <<-'END' ) 357 grammar AutoDupTreeWithRuleRootAndLabels; 358 options { 359 language=Ruby; 360 output=AST; 361 } 362 a : ID INT -> ^(ID INT); 363 ID : 'a'..'z'+ ; 364 INT : '0'..'9'+; 365 WS : (' '|'\n') {$channel=HIDDEN;} ; 366 END 367 368 inline_grammar( <<-'END' ) 369 tree grammar AutoDupTreeWithRuleRootAndLabelsWalker; 370 options { 371 language=Ruby; 372 output=AST; 373 ASTLabelType=CommonTree; 374 tokenVocab=AutoDupTreeWithRuleRootAndLabels; 375 } 376 a : ^(x=b INT) ; 377 b : ID ; 378 END 379 380 inline_grammar( <<-'END' ) 381 grammar AutoDupTreeWithRuleRootAndListLabels; 382 options { 383 language=Ruby; 384 output=AST; 385 } 386 a : ID INT -> ^(ID INT); 387 ID : 'a'..'z'+ ; 388 INT : '0'..'9'+; 389 WS : (' '|'\n') {$channel=HIDDEN;} ; 390 END 391 392 inline_grammar( <<-'END' ) 393 tree grammar AutoDupTreeWithRuleRootAndListLabelsWalker; 394 options { 395 language=Ruby; 396 output=AST; 397 ASTLabelType=CommonTree; 398 tokenVocab=AutoDupTreeWithRuleRootAndListLabels; 399 } 400 a : ^(x+=b y+=c) ; 401 b : ID ; 402 c : INT ; 403 END 404 405 inline_grammar( <<-'END' ) 406 grammar AutoDupNestedTree; 407 options { 408 language=Ruby; 409 output=AST; 410 } 411 a : x=ID y=ID INT -> ^($x ^($y INT)); 412 ID : 'a'..'z'+ ; 413 INT : '0'..'9'+; 414 WS : (' '|'\n') {$channel=HIDDEN;} ; 415 END 416 417 inline_grammar( <<-'END' ) 418 tree grammar AutoDupNestedTreeWalker; 419 options { 420 language=Ruby; 421 output=AST; 422 ASTLabelType=CommonTree; 423 tokenVocab=AutoDupNestedTree; 424 } 425 a : ^(ID ^(ID INT)) 426 ; 427 END 428 429 inline_grammar( <<-'END' ) 430 grammar Delete; 431 options { 432 language=Ruby; 433 output=AST; 434 } 435 a : ID ; 436 ID : 'a'..'z'+ ; 437 INT : '0'..'9'+; 438 WS : (' '|'\n') {$channel=HIDDEN;} ; 439 END 440 441 inline_grammar( <<-'END' ) 442 tree grammar DeleteWalker; 443 options { 444 language=Ruby; 445 output=AST; 446 ASTLabelType=CommonTree; 447 tokenVocab=Delete; 448 } 449 a : ID -> 450 ; 451 END 452 453 inline_grammar( <<-'END' ) 454 grammar SetMatchNoRewrite; 455 options { 456 language=Ruby; 457 output=AST; 458 } 459 a : ID INT ; 460 ID : 'a'..'z'+ ; 461 INT : '0'..'9'+; 462 WS : (' '|'\n') {$channel=HIDDEN;} ; 463 END 464 465 inline_grammar( <<-'END' ) 466 tree grammar SetMatchNoRewriteWalker; 467 options { 468 language=Ruby; 469 output=AST; 470 ASTLabelType=CommonTree; 471 tokenVocab=SetMatchNoRewrite; 472 } 473 a : b INT; 474 b : ID | INT; 475 END 476 477 inline_grammar( <<-'END' ) 478 grammar SetOptionalMatchNoRewrite; 479 options { 480 language=Ruby; 481 output=AST; 482 } 483 a : ID INT ; 484 ID : 'a'..'z'+ ; 485 INT : '0'..'9'+; 486 WS : (' '|'\n') {$channel=HIDDEN;} ; 487 END 488 489 inline_grammar( <<-'END' ) 490 tree grammar SetOptionalMatchNoRewriteWalker; 491 options { 492 language=Ruby; 493 output=AST; 494 ASTLabelType=CommonTree; 495 tokenVocab=SetOptionalMatchNoRewrite; 496 } 497 a : (ID|INT)? INT ; 498 END 499 500 inline_grammar( <<-'END' ) 501 grammar SetMatchNoRewriteLevel2; 502 options { 503 language=Ruby; 504 output=AST; 505 } 506 a : x=ID INT -> ^($x INT); 507 ID : 'a'..'z'+ ; 508 INT : '0'..'9'+; 509 WS : (' '|'\n') {$channel=HIDDEN;} ; 510 END 511 512 inline_grammar( <<-'END' ) 513 tree grammar SetMatchNoRewriteLevel2Walker; 514 options { 515 language=Ruby; 516 output=AST; 517 ASTLabelType=CommonTree; 518 tokenVocab=SetMatchNoRewriteLevel2; 519 } 520 a : ^(ID (ID | INT) ) ; 521 END 522 523 inline_grammar( <<-'END' ) 524 grammar SetMatchNoRewriteLevel2Root; 525 options { 526 language=Ruby; 527 output=AST; 528 } 529 a : x=ID INT -> ^($x INT); 530 ID : 'a'..'z'+ ; 531 INT : '0'..'9'+; 532 WS : (' '|'\n') {$channel=HIDDEN;} ; 533 END 534 535 inline_grammar( <<-'END' ) 536 tree grammar SetMatchNoRewriteLevel2RootWalker; 537 options { 538 language=Ruby; 539 output=AST; 540 ASTLabelType=CommonTree; 541 tokenVocab=SetMatchNoRewriteLevel2Root; 542 } 543 a : ^((ID | INT) INT) ; 544 END 545 546 inline_grammar( <<-END ) 547 grammar RewriteModeCombinedRewriteAndAuto; 548 options { 549 language=Ruby; 550 output=AST; 551 } 552 a : ID INT -> ^(ID INT) | INT ; 553 ID : 'a'..'z'+ ; 554 INT : '0'..'9'+; 555 WS : (' '|'\\n') {$channel=HIDDEN;} ; 556 END 557 558 inline_grammar( <<-END ) 559 tree grammar RewriteModeCombinedRewriteAndAutoTree; 560 options { 561 language=Ruby; 562 output=AST; 563 ASTLabelType=CommonTree; 564 tokenVocab=RewriteModeCombinedRewriteAndAuto; 565 rewrite=true; 566 } 567 a : ^(ID INT) -> ^(ID["ick"] INT) 568 | INT // leaves it alone, returning $a.start 569 ; 570 END 571 572 inline_grammar( <<-'END' ) 573 grammar RewriteModeFlatTree; 574 options { 575 language=Ruby; 576 output=AST; 577 } 578 a : ID INT -> ID INT | INT ; 579 ID : 'a'..'z'+ ; 580 INT : '0'..'9'+; 581 WS : (' '|'\n') {$channel=HIDDEN;} ; 582 END 583 584 inline_grammar( <<-'END' ) 585 tree grammar RewriteModeFlatTreeWalker; 586 options { 587 language=Ruby; 588 output=AST; 589 ASTLabelType=CommonTree; 590 tokenVocab=RewriteModeFlatTree; 591 rewrite=true; 592 } 593 s : ID a ; 594 a : INT -> INT["1"] 595 ; 596 END 597 598 inline_grammar( <<-'END' ) 599 grammar RewriteModeChainRuleFlatTree; 600 options {language=Ruby; output=AST;} 601 a : ID INT -> ID INT | INT ; 602 ID : 'a'..'z'+ ; 603 INT : '0'..'9'+; 604 WS : (' '|'\n') {$channel=HIDDEN;} ; 605 END 606 607 inline_grammar( <<-'END' ) 608 tree grammar RewriteModeChainRuleFlatTreeWalker; 609 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteModeChainRuleFlatTree; rewrite=true;} 610 s : a ; 611 a : b ; 612 b : ID INT -> INT ID 613 ; 614 END 615 616 inline_grammar( <<-'END' ) 617 grammar RewriteModeChainRuleTree; 618 options {language=Ruby; output=AST;} 619 a : ID INT -> ^(ID INT) ; 620 ID : 'a'..'z'+ ; 621 INT : '0'..'9'+; 622 WS : (' '|'\n') {$channel=HIDDEN;} ; 623 END 624 625 inline_grammar( <<-'END' ) 626 tree grammar RewriteModeChainRuleTreeWalker; 627 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteModeChainRuleTree; rewrite=true;} 628 s : a ; 629 a : b ; // a.tree must become b.tree 630 b : ^(ID INT) -> INT 631 ; 632 END 633 634 inline_grammar( <<-'END' ) 635 grammar RewriteModeChainRuleTree2; 636 options {language=Ruby; output=AST;} 637 a : ID INT -> ^(ID INT) ; 638 ID : 'a'..'z'+ ; 639 INT : '0'..'9'+; 640 WS : (' '|'\n') {$channel=HIDDEN;} ; 641 END 642 643 inline_grammar( <<-'END' ) 644 tree grammar RewriteModeChainRuleTree2Walker; 645 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteModeChainRuleTree2; rewrite=true;} 646 tokens { X; } 647 s : a* b ; // only b contributes to tree, but it's after a*; s.tree = b.tree 648 a : X ; 649 b : ^(ID INT) -> INT 650 ; 651 END 652 653 inline_grammar( <<-'END' ) 654 grammar RewriteModeChainRuleTree3; 655 options {language=Ruby; output=AST;} 656 a : 'boo' ID INT -> 'boo' ^(ID INT) ; 657 ID : 'a'..'z'+ ; 658 INT : '0'..'9'+; 659 WS : (' '|'\n') {$channel=HIDDEN;} ; 660 END 661 662 inline_grammar( <<-'END' ) 663 tree grammar RewriteModeChainRuleTree3Walker; 664 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteModeChainRuleTree3; rewrite=true;} 665 tokens { X; } 666 s : 'boo' a* b ; // don't reset s.tree to b.tree due to 'boo' 667 a : X ; 668 b : ^(ID INT) -> INT 669 ; 670 END 671 672 inline_grammar( <<-'END' ) 673 grammar RewriteModeChainRuleTree4; 674 options {language=Ruby; output=AST;} 675 a : 'boo' ID INT -> ^('boo' ^(ID INT)) ; 676 ID : 'a'..'z'+ ; 677 INT : '0'..'9'+; 678 WS : (' '|'\n') {$channel=HIDDEN;} ; 679 END 680 681 inline_grammar( <<-'END' ) 682 tree grammar RewriteModeChainRuleTree4Walker; 683 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteModeChainRuleTree4; rewrite=true;} 684 tokens { X; } 685 s : ^('boo' a* b) ; // don't reset s.tree to b.tree due to 'boo' 686 a : X ; 687 b : ^(ID INT) -> INT 688 ; 689 END 690 691 inline_grammar( <<-'END' ) 692 grammar RewriteModeChainRuleTree5; 693 options {language=Ruby; output=AST;} 694 a : 'boo' ID INT -> ^('boo' ^(ID INT)) ; 695 ID : 'a'..'z'+ ; 696 INT : '0'..'9'+; 697 WS : (' '|'\n') {$channel=HIDDEN;} ; 698 END 699 700 inline_grammar( <<-'END' ) 701 tree grammar RewriteModeChainRuleTree5Walker; 702 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteModeChainRuleTree5; rewrite=true;} 703 tokens { X; } 704 s : ^(a b) ; // s.tree is a.tree 705 a : 'boo' ; 706 b : ^(ID INT) -> INT 707 ; 708 END 709 710 inline_grammar( <<-'END' ) 711 grammar RewriteOfRuleRef; 712 options {language=Ruby; output=AST;} 713 a : ID INT -> ID INT | INT ; 714 ID : 'a'..'z'+ ; 715 INT : '0'..'9'+; 716 WS : (' '|'\n') {$channel=HIDDEN;} ; 717 END 718 719 inline_grammar( <<-'END' ) 720 tree grammar RewriteOfRuleRefWalker; 721 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteOfRuleRef; rewrite=true;} 722 s : a -> a ; 723 a : ID INT -> ID INT ; 724 END 725 726 inline_grammar( <<-'END' ) 727 grammar RewriteOfRuleRefRoot; 728 options {language=Ruby; output=AST;} 729 a : ID INT INT -> ^(INT ^(ID INT)); 730 ID : 'a'..'z'+ ; 731 INT : '0'..'9'+; 732 WS : (' '|'\n') {$channel=HIDDEN;} ; 733 END 734 735 inline_grammar( <<-'END' ) 736 tree grammar RewriteOfRuleRefRootWalker; 737 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteOfRuleRefRoot; rewrite=true;} 738 s : ^(a ^(ID INT)) -> a ; 739 a : INT ; 740 END 741 742 inline_grammar( <<-'END' ) 743 grammar RewriteOfRuleRefRootLabeled; 744 options {language=Ruby; output=AST;} 745 a : ID INT INT -> ^(INT ^(ID INT)); 746 ID : 'a'..'z'+ ; 747 INT : '0'..'9'+; 748 WS : (' '|'\n') {$channel=HIDDEN;} ; 749 END 750 751 inline_grammar( <<-'END' ) 752 tree grammar RewriteOfRuleRefRootLabeledWalker; 753 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteOfRuleRefRootLabeled; rewrite=true;} 754 s : ^(label=a ^(ID INT)) -> a ; 755 a : INT ; 756 END 757 758 inline_grammar( <<-'END' ) 759 grammar RewriteOfRuleRefRootListLabeled; 760 options {language=Ruby; output=AST;} 761 a : ID INT INT -> ^(INT ^(ID INT)); 762 ID : 'a'..'z'+ ; 763 INT : '0'..'9'+; 764 WS : (' '|'\n') {$channel=HIDDEN;} ; 765 END 766 767 inline_grammar( <<-'END' ) 768 tree grammar RewriteOfRuleRefRootListLabeledWalker; 769 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteOfRuleRefRootListLabeled; rewrite=true;} 770 s : ^(label+=a ^(ID INT)) -> a ; 771 a : INT ; 772 END 773 774 inline_grammar( <<-'END' ) 775 grammar RewriteOfRuleRefChild; 776 options {language=Ruby; output=AST;} 777 a : ID INT -> ^(ID ^(INT INT)); 778 ID : 'a'..'z'+ ; 779 INT : '0'..'9'+; 780 WS : (' '|'\n') {$channel=HIDDEN;} ; 781 END 782 783 inline_grammar( <<-'END' ) 784 tree grammar RewriteOfRuleRefChildWalker; 785 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteOfRuleRefChild; rewrite=true;} 786 s : ^(ID a) -> a ; 787 a : ^(INT INT) ; 788 END 789 790 inline_grammar( <<-'END' ) 791 grammar RewriteOfRuleRefLabel; 792 options {language=Ruby; output=AST;} 793 a : ID INT -> ^(ID ^(INT INT)); 794 ID : 'a'..'z'+ ; 795 INT : '0'..'9'+; 796 WS : (' '|'\n') {$channel=HIDDEN;} ; 797 END 798 799 inline_grammar( <<-'END' ) 800 tree grammar RewriteOfRuleRefLabelWalker; 801 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteOfRuleRefLabel; rewrite=true;} 802 s : ^(ID label=a) -> a ; 803 a : ^(INT INT) ; 804 END 805 806 inline_grammar( <<-'END' ) 807 grammar RewriteOfRuleRefListLabel; 808 options {language=Ruby; output=AST;} 809 a : ID INT -> ^(ID ^(INT INT)); 810 ID : 'a'..'z'+ ; 811 INT : '0'..'9'+; 812 WS : (' '|'\n') {$channel=HIDDEN;} ; 813 END 814 815 inline_grammar( <<-'END' ) 816 tree grammar RewriteOfRuleRefListLabelWalker; 817 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=RewriteOfRuleRefListLabel; rewrite=true;} 818 s : ^(ID label+=a) -> a ; 819 a : ^(INT INT) ; 820 END 821 822 inline_grammar( <<-'END' ) 823 grammar RewriteModeWithPredicatedRewrites; 824 options { 825 language=Ruby; 826 output=AST; 827 } 828 a : ID INT -> ^(ID["root"] ^(ID INT)) | INT -> ^(ID["root"] INT) ; 829 ID : 'a'..'z'+ ; 830 INT : '0'..'9'+; 831 WS : (' '|'\n') {$channel=HIDDEN;} ; 832 END 833 834 inline_grammar( <<-'END' ) 835 tree grammar RewriteModeWithPredicatedRewritesWalker; 836 options { 837 language=Ruby; 838 output=AST; 839 ASTLabelType=CommonTree; 840 tokenVocab=RewriteModeWithPredicatedRewrites; 841 rewrite=true; 842 } 843 s : ^(ID a) { 844 # self.buf += $s.start.inspect 845 }; 846 a : ^(ID INT) -> {true}? ^(ID["ick"] INT) 847 -> INT 848 ; 849 END 850 851 inline_grammar( <<-'END' ) 852 grammar WildcardSingleNode; 853 options { 854 language=Ruby; 855 output=AST; 856 } 857 a : ID INT -> ^(ID["root"] INT); 858 ID : 'a'..'z'+ ; 859 INT : '0'..'9'+; 860 WS : (' '|'\n') {$channel=HIDDEN;} ; 861 END 862 863 inline_grammar( <<-'END' ) 864 tree grammar WildcardSingleNodeWalker; 865 options { 866 language=Ruby; 867 output=AST; 868 ASTLabelType=CommonTree; 869 tokenVocab=WildcardSingleNode; 870 } 871 s : ^(ID c=.) -> $c 872 ; 873 END 874 875 inline_grammar( <<-'END' ) 876 grammar WildcardUnlabeledSingleNode; 877 options {language=Ruby; output=AST;} 878 a : ID INT -> ^(ID INT); 879 ID : 'a'..'z'+ ; 880 INT : '0'..'9'+; 881 WS : (' '|'\n') {$channel=HIDDEN;} ; 882 END 883 884 inline_grammar( <<-'END' ) 885 tree grammar WildcardUnlabeledSingleNodeWalker; 886 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=WildcardUnlabeledSingleNode;} 887 s : ^(ID .) -> ID 888 ; 889 END 890 891 inline_grammar( <<-'END' ) 892 grammar WildcardListLabel; 893 options {language=Ruby; output=AST;} 894 a : INT INT INT ; 895 ID : 'a'..'z'+ ; 896 INT : '0'..'9'+; 897 WS : (' '|'\n') {$channel=HIDDEN;} ; 898 END 899 900 inline_grammar( <<-'END' ) 901 tree grammar WildcardListLabelWalker; 902 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=WildcardListLabel;} 903 s : (c+=.)+ -> $c+ 904 ; 905 END 906 907 inline_grammar( <<-'END' ) 908 grammar WildcardListLabel2; 909 options {language=Ruby; output=AST; ASTLabelType=CommonTree;} 910 a : x=INT y=INT z=INT -> ^($x ^($y $z) ^($y $z)); 911 ID : 'a'..'z'+ ; 912 INT : '0'..'9'+; 913 WS : (' '|'\n') {$channel=HIDDEN;} ; 914 END 915 916 inline_grammar( <<-'END' ) 917 tree grammar WildcardListLabel2Walker; 918 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=WildcardListLabel2; rewrite=true;} 919 s : ^(INT (c+=.)+) -> $c+ 920 ; 921 END 922 923 inline_grammar( <<-'END' ) 924 grammar WildcardGrabsSubtree; 925 options {language=Ruby; output=AST;} 926 a : ID x=INT y=INT z=INT -> ^(ID[\"root\"] ^($x $y $z)); 927 ID : 'a'..'z'+ ; 928 INT : '0'..'9'+; 929 WS : (' '|'\n') {$channel=HIDDEN;} ; 930 END 931 932 inline_grammar( <<-'END' ) 933 tree grammar WildcardGrabsSubtreeWalker; 934 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=WildcardGrabsSubtree;} 935 s : ^(ID c=.) -> $c 936 ; 937 END 938 939 inline_grammar( <<-'END' ) 940 grammar WildcardGrabsSubtree2; 941 options {language=Ruby; output=AST;} 942 a : ID x=INT y=INT z=INT -> ID ^($x $y $z); 943 ID : 'a'..'z'+ ; 944 INT : '0'..'9'+; 945 WS : (' '|'\n') {$channel=HIDDEN;} ; 946 END 947 948 inline_grammar( <<-'END' ) 949 tree grammar WildcardGrabsSubtree2Walker; 950 options {language=Ruby; output=AST; ASTLabelType=CommonTree; tokenVocab=WildcardGrabsSubtree2;} 951 s : ID c=. -> $c 952 ; 953 END 954 955 inline_grammar( <<-END ) 956 grammar CombinedRewriteAndAuto; 957 options { 958 language=Ruby; 959 output=AST; 960 } 961 a : ID INT -> ^(ID INT) | INT ; 962 ID : 'a'..'z'+ ; 963 INT : '0'..'9'+; 964 WS : (' '|'\\n') {$channel=HIDDEN;} ; 965 END 966 967 inline_grammar( <<-END ) 968 tree grammar CombinedRewriteAndAutoWalker; 969 options { 970 language=Ruby; 971 output=AST; 972 ASTLabelType=CommonTree; 973 tokenVocab=CombinedRewriteAndAuto; 974 } 975 a : ^(ID INT) -> ^(INT ID) | INT; 976 END 977 978 inline_grammar( <<-END ) 979 grammar RewriteModeCombinedRewriteAndAuto; 980 options { 981 language=Ruby; 982 output=AST; 983 } 984 a : ID INT -> ^(ID INT) | INT ; 985 ID : 'a'..'z'+ ; 986 INT : '0'..'9'+; 987 WS : (' '|'\\n') {$channel=HIDDEN;} ; 988 END 989 990 inline_grammar( <<-END ) 991 tree grammar RewriteModeCombinedRewriteAndAutoWalker; 992 options { 993 language=Ruby; 994 output=AST; 995 ASTLabelType=CommonTree; 996 tokenVocab=RewriteModeCombinedRewriteAndAuto; 997 rewrite=true; 998 } 999 a : ^(ID INT) -> ^(ID["ick"] INT) 1000 | INT // leaves it alone, returning $a.start 1001 ; 1002 END 1003 1004 example "flat list" do 1005 lexer = FlatList::Lexer.new( "abc 34" ) 1006 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1007 parser = FlatList::Parser.new( tokens ) 1008 1009 result = parser.a 1010 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1011 nodes.token_stream = tokens 1012 walker = FlatListWalker::TreeParser.new( nodes ) 1013 result = walker.a 1014 stree = result.tree.nil? ? '' : result.tree.inspect 1015 stree.should == "34 abc" 1016 end 1017 1018 example "simple tree" do 1019 lexer = SimpleTree::Lexer.new( "abc 34" ) 1020 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1021 parser = SimpleTree::Parser.new( tokens ) 1022 1023 result = parser.a 1024 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1025 nodes.token_stream = tokens 1026 walker = SimpleTreeWalker::TreeParser.new( nodes ) 1027 result = walker.a 1028 stree = result.tree.nil? ? '' : result.tree.inspect 1029 stree.should == "(34 abc)" 1030 end 1031 1032 example "combined rewrite and auto" do 1033 lexer = CombinedRewriteAndAuto::Lexer.new( "abc 34" ) 1034 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1035 parser = CombinedRewriteAndAuto::Parser.new( tokens ) 1036 1037 result = parser.a 1038 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1039 nodes.token_stream = tokens 1040 walker = CombinedRewriteAndAutoWalker::TreeParser.new( nodes ) 1041 result = walker.a.tree 1042 result.inspect.should == '(34 abc)' 1043 lexer = CombinedRewriteAndAuto::Lexer.new( "34" ) 1044 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1045 parser = CombinedRewriteAndAuto::Parser.new( tokens ) 1046 1047 result = parser.a 1048 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1049 nodes.token_stream = tokens 1050 walker = CombinedRewriteAndAutoWalker::TreeParser.new( nodes ) 1051 result = walker.a.tree 1052 result.inspect.should == '34' 1053 end 1054 1055 example "avoid dup" do 1056 lexer = AvoidDup::Lexer.new( "abc" ) 1057 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1058 parser = AvoidDup::Parser.new( tokens ) 1059 1060 result = parser.a 1061 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1062 nodes.token_stream = tokens 1063 walker = AvoidDupWalker::TreeParser.new( nodes ) 1064 result = walker.a 1065 stree = result.tree.nil? ? '' : result.tree.inspect 1066 stree.should == "(abc abc)" 1067 end 1068 1069 example "loop" do 1070 lexer = Loop::Lexer.new( "a b c 3 4 5" ) 1071 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1072 parser = Loop::Parser.new( tokens ) 1073 1074 result = parser.a 1075 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1076 nodes.token_stream = tokens 1077 walker = LoopWalker::TreeParser.new( nodes ) 1078 result = walker.a 1079 stree = result.tree.nil? ? '' : result.tree.inspect 1080 stree.should == "3 4 5 a b c" 1081 end 1082 1083 example "auto dup" do 1084 lexer = AutoDup::Lexer.new( "abc" ) 1085 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1086 parser = AutoDup::Parser.new( tokens ) 1087 1088 result = parser.a 1089 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1090 nodes.token_stream = tokens 1091 walker = AutoDupWalker::TreeParser.new( nodes ) 1092 result = walker.a 1093 stree = result.tree.nil? ? '' : result.tree.inspect 1094 stree.should == "abc" 1095 end 1096 1097 example "auto dup rule" do 1098 lexer = AutoDupRule::Lexer.new( "a 1" ) 1099 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1100 parser = AutoDupRule::Parser.new( tokens ) 1101 1102 result = parser.a 1103 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1104 nodes.token_stream = tokens 1105 walker = AutoDupRuleWalker::TreeParser.new( nodes ) 1106 result = walker.a 1107 stree = result.tree.nil? ? '' : result.tree.inspect 1108 stree.should == "a 1" 1109 end 1110 1111 example "auto wildcard" do 1112 lexer = AutoWildcard::Lexer.new( "abc 34" ) 1113 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1114 parser = AutoWildcard::Parser.new( tokens ) 1115 1116 result = parser.a 1117 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1118 nodes.token_stream = tokens 1119 walker = AutoWildcardWalker::TreeParser.new( nodes ) 1120 result = walker.a 1121 stree = result.tree.nil? ? '' : result.tree.inspect 1122 stree.should == "abc 34" 1123 end 1124 1125 example "auto wildcard2" do 1126 lexer = AutoWildcard2::Lexer.new( "abc 34" ) 1127 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1128 parser = AutoWildcard2::Parser.new( tokens ) 1129 1130 result = parser.a 1131 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1132 nodes.token_stream = tokens 1133 walker = AutoWildcard2Walker::TreeParser.new( nodes ) 1134 result = walker.a 1135 stree = result.tree.nil? ? '' : result.tree.inspect 1136 stree.should == "(abc 34)" 1137 end 1138 1139 example "auto wildcard with label" do 1140 lexer = AutoWildcardWithLabel::Lexer.new( "abc 34" ) 1141 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1142 parser = AutoWildcardWithLabel::Parser.new( tokens ) 1143 1144 result = parser.a 1145 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1146 nodes.token_stream = tokens 1147 walker = AutoWildcardWithLabelWalker::TreeParser.new( nodes ) 1148 result = walker.a 1149 stree = result.tree.nil? ? '' : result.tree.inspect 1150 stree.should == "abc 34" 1151 end 1152 1153 example "auto wildcard with list label" do 1154 lexer = AutoWildcardWithListLabel::Lexer.new( "abc 34" ) 1155 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1156 parser = AutoWildcardWithListLabel::Parser.new( tokens ) 1157 1158 result = parser.a 1159 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1160 nodes.token_stream = tokens 1161 walker = AutoWildcardWithListLabelWalker::TreeParser.new( nodes ) 1162 result = walker.a 1163 stree = result.tree.nil? ? '' : result.tree.inspect 1164 stree.should == "abc 34" 1165 end 1166 1167 example "auto dup multiple" do 1168 lexer = AutoDupMultiple::Lexer.new( "a b 3" ) 1169 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1170 parser = AutoDupMultiple::Parser.new( tokens ) 1171 1172 result = parser.a 1173 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1174 nodes.token_stream = tokens 1175 walker = AutoDupMultipleWalker::TreeParser.new( nodes ) 1176 result = walker.a 1177 stree = result.tree.nil? ? '' : result.tree.inspect 1178 stree.should == "a b 3" 1179 end 1180 1181 example "auto dup tree" do 1182 lexer = AutoDupTree::Lexer.new( "a 3" ) 1183 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1184 parser = AutoDupTree::Parser.new( tokens ) 1185 1186 result = parser.a 1187 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1188 nodes.token_stream = tokens 1189 walker = AutoDupTreeWalker::TreeParser.new( nodes ) 1190 result = walker.a 1191 stree = result.tree.nil? ? '' : result.tree.inspect 1192 stree.should == "(a 3)" 1193 end 1194 1195 example "auto dup tree with labels" do 1196 lexer = AutoDupTreeWithLabels::Lexer.new( "a 3" ) 1197 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1198 parser = AutoDupTreeWithLabels::Parser.new( tokens ) 1199 1200 result = parser.a 1201 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1202 nodes.token_stream = tokens 1203 walker = AutoDupTreeWithLabelsWalker::TreeParser.new( nodes ) 1204 result = walker.a 1205 stree = result.tree.nil? ? '' : result.tree.inspect 1206 stree.should == "(a 3)" 1207 end 1208 1209 example "auto dup tree with list labels" do 1210 lexer = AutoDupTreeWithListLabels::Lexer.new( "a 3" ) 1211 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1212 parser = AutoDupTreeWithListLabels::Parser.new( tokens ) 1213 1214 result = parser.a 1215 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1216 nodes.token_stream = tokens 1217 walker = AutoDupTreeWithListLabelsWalker::TreeParser.new( nodes ) 1218 result = walker.a 1219 stree = result.tree.nil? ? '' : result.tree.inspect 1220 stree.should == "(a 3)" 1221 end 1222 1223 example "auto dup tree with rule root" do 1224 lexer = AutoDupTreeWithRuleRoot::Lexer.new( "a 3" ) 1225 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1226 parser = AutoDupTreeWithRuleRoot::Parser.new( tokens ) 1227 1228 result = parser.a 1229 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1230 nodes.token_stream = tokens 1231 walker = AutoDupTreeWithRuleRootWalker::TreeParser.new( nodes ) 1232 result = walker.a 1233 stree = result.tree.nil? ? '' : result.tree.inspect 1234 stree.should == "(a 3)" 1235 end 1236 1237 example "auto dup tree with rule root and labels" do 1238 lexer = AutoDupTreeWithRuleRootAndLabels::Lexer.new( "a 3" ) 1239 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1240 parser = AutoDupTreeWithRuleRootAndLabels::Parser.new( tokens ) 1241 1242 result = parser.a 1243 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1244 nodes.token_stream = tokens 1245 walker = AutoDupTreeWithRuleRootAndLabelsWalker::TreeParser.new( nodes ) 1246 result = walker.a 1247 stree = result.tree.nil? ? '' : result.tree.inspect 1248 stree.should == "(a 3)" 1249 end 1250 1251 example "auto dup tree with rule root and list labels" do 1252 lexer = AutoDupTreeWithRuleRootAndListLabels::Lexer.new( "a 3" ) 1253 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1254 parser = AutoDupTreeWithRuleRootAndListLabels::Parser.new( tokens ) 1255 1256 result = parser.a 1257 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1258 nodes.token_stream = tokens 1259 walker = AutoDupTreeWithRuleRootAndListLabelsWalker::TreeParser.new( nodes ) 1260 result = walker.a 1261 stree = result.tree.nil? ? '' : result.tree.inspect 1262 stree.should == "(a 3)" 1263 end 1264 1265 example "auto dup nested tree" do 1266 lexer = AutoDupNestedTree::Lexer.new( "a b 3" ) 1267 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1268 parser = AutoDupNestedTree::Parser.new( tokens ) 1269 1270 result = parser.a 1271 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1272 nodes.token_stream = tokens 1273 walker = AutoDupNestedTreeWalker::TreeParser.new( nodes ) 1274 result = walker.a 1275 stree = result.tree.nil? ? '' : result.tree.inspect 1276 stree.should == "(a (b 3))" 1277 end 1278 1279 example "delete" do 1280 lexer = Delete::Lexer.new( "abc" ) 1281 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1282 parser = Delete::Parser.new( tokens ) 1283 1284 result = parser.a 1285 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1286 nodes.token_stream = tokens 1287 walker = DeleteWalker::TreeParser.new( nodes ) 1288 result = walker.a 1289 stree = result.tree.nil? ? '' : result.tree.inspect 1290 stree.should == "" 1291 end 1292 1293 example "set match no rewrite" do 1294 lexer = SetMatchNoRewrite::Lexer.new( "abc 34" ) 1295 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1296 parser = SetMatchNoRewrite::Parser.new( tokens ) 1297 1298 result = parser.a 1299 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1300 nodes.token_stream = tokens 1301 walker = SetMatchNoRewriteWalker::TreeParser.new( nodes ) 1302 result = walker.a 1303 stree = result.tree.nil? ? '' : result.tree.inspect 1304 stree.should == "abc 34" 1305 end 1306 1307 example "set optional match no rewrite" do 1308 lexer = SetOptionalMatchNoRewrite::Lexer.new( "abc 34" ) 1309 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1310 parser = SetOptionalMatchNoRewrite::Parser.new( tokens ) 1311 1312 result = parser.a 1313 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1314 nodes.token_stream = tokens 1315 walker = SetOptionalMatchNoRewriteWalker::TreeParser.new( nodes ) 1316 result = walker.a 1317 stree = result.tree.nil? ? '' : result.tree.inspect 1318 stree.should == "abc 34" 1319 end 1320 1321 example "set match no rewrite level2" do 1322 lexer = SetMatchNoRewriteLevel2::Lexer.new( "abc 34" ) 1323 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1324 parser = SetMatchNoRewriteLevel2::Parser.new( tokens ) 1325 1326 result = parser.a 1327 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1328 nodes.token_stream = tokens 1329 walker = SetMatchNoRewriteLevel2Walker::TreeParser.new( nodes ) 1330 result = walker.a 1331 stree = result.tree.nil? ? '' : result.tree.inspect 1332 stree.should == "(abc 34)" 1333 end 1334 1335 example "set match no rewrite level2 root" do 1336 lexer = SetMatchNoRewriteLevel2Root::Lexer.new( "abc 34" ) 1337 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1338 parser = SetMatchNoRewriteLevel2Root::Parser.new( tokens ) 1339 1340 result = parser.a 1341 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1342 nodes.token_stream = tokens 1343 walker = SetMatchNoRewriteLevel2RootWalker::TreeParser.new( nodes ) 1344 result = walker.a 1345 stree = result.tree.nil? ? '' : result.tree.inspect 1346 stree.should == "(abc 34)" 1347 end 1348 1349 example "rewrite mode combined rewrite and auto" do 1350 1351 parser_test = proc do |input, expected_output| 1352 lexer = RewriteModeCombinedRewriteAndAuto::Lexer.new( input ) 1353 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1354 parser = RewriteModeCombinedRewriteAndAuto::Parser.new( tokens ) 1355 result = parser.a 1356 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1357 nodes.token_stream = tokens 1358 walker = RewriteModeCombinedRewriteAndAutoWalker::TreeParser.new( nodes ) 1359 result = walker.a 1360 stree = result.tree.nil? ? '' : result.tree.inspect 1361 stree.should == expected_output 1362 end 1363 1364 parser_test[ 'abc 34', '(ick 34)' ] 1365 parser_test[ '34', '34' ] 1366 end 1367 1368 example "rewrite mode flat tree" do 1369 lexer = RewriteModeFlatTree::Lexer.new( "abc 34" ) 1370 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1371 parser = RewriteModeFlatTree::Parser.new( tokens ) 1372 1373 result = parser.a 1374 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1375 nodes.token_stream = tokens 1376 walker = RewriteModeFlatTreeWalker::TreeParser.new( nodes ) 1377 result = walker.s 1378 stree = result.tree.nil? ? '' : result.tree.inspect 1379 stree.should == "abc 1" 1380 end 1381 1382 example "rewrite mode chain rule flat tree" do 1383 lexer = RewriteModeChainRuleFlatTree::Lexer.new( "abc 34" ) 1384 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1385 parser = RewriteModeChainRuleFlatTree::Parser.new( tokens ) 1386 1387 result = parser.a 1388 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1389 nodes.token_stream = tokens 1390 walker = RewriteModeChainRuleFlatTreeWalker::TreeParser.new( nodes ) 1391 result = walker.s 1392 stree = result.tree.nil? ? '' : result.tree.inspect 1393 stree.should == "34 abc" 1394 end 1395 1396 example "rewrite mode chain rule tree" do 1397 lexer = RewriteModeChainRuleTree::Lexer.new( "abc 34" ) 1398 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1399 parser = RewriteModeChainRuleTree::Parser.new( tokens ) 1400 1401 result = parser.a 1402 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1403 nodes.token_stream = tokens 1404 walker = RewriteModeChainRuleTreeWalker::TreeParser.new( nodes ) 1405 result = walker.s 1406 stree = result.tree.nil? ? '' : result.tree.inspect 1407 stree.should == "34" 1408 end 1409 1410 example "rewrite mode chain rule tree2" do 1411 lexer = RewriteModeChainRuleTree2::Lexer.new( "abc 34" ) 1412 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1413 parser = RewriteModeChainRuleTree2::Parser.new( tokens ) 1414 1415 result = parser.a 1416 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1417 nodes.token_stream = tokens 1418 walker = RewriteModeChainRuleTree2Walker::TreeParser.new( nodes ) 1419 result = walker.s 1420 stree = result.tree.nil? ? '' : result.tree.inspect 1421 stree.should == "34" 1422 end 1423 1424 example "rewrite mode chain rule tree3" do 1425 lexer = RewriteModeChainRuleTree3::Lexer.new( "boo abc 34" ) 1426 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1427 parser = RewriteModeChainRuleTree3::Parser.new( tokens ) 1428 1429 result = parser.a 1430 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1431 nodes.token_stream = tokens 1432 walker = RewriteModeChainRuleTree3Walker::TreeParser.new( nodes ) 1433 result = walker.s 1434 stree = result.tree.nil? ? '' : result.tree.inspect 1435 stree.should == "boo 34" 1436 end 1437 1438 example "rewrite mode chain rule tree4" do 1439 lexer = RewriteModeChainRuleTree4::Lexer.new( "boo abc 34" ) 1440 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1441 parser = RewriteModeChainRuleTree4::Parser.new( tokens ) 1442 1443 result = parser.a 1444 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1445 nodes.token_stream = tokens 1446 walker = RewriteModeChainRuleTree4Walker::TreeParser.new( nodes ) 1447 result = walker.s 1448 stree = result.tree.nil? ? '' : result.tree.inspect 1449 stree.should == "(boo 34)" 1450 end 1451 1452 example "rewrite mode chain rule tree5" do 1453 lexer = RewriteModeChainRuleTree5::Lexer.new( "boo abc 34" ) 1454 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1455 parser = RewriteModeChainRuleTree5::Parser.new( tokens ) 1456 1457 result = parser.a 1458 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1459 nodes.token_stream = tokens 1460 walker = RewriteModeChainRuleTree5Walker::TreeParser.new( nodes ) 1461 result = walker.s 1462 stree = result.tree.nil? ? '' : result.tree.inspect 1463 stree.should == "(boo 34)" 1464 end 1465 1466 example "rewrite of rule ref" do 1467 lexer = RewriteOfRuleRef::Lexer.new( "abc 34" ) 1468 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1469 parser = RewriteOfRuleRef::Parser.new( tokens ) 1470 1471 result = parser.a 1472 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1473 nodes.token_stream = tokens 1474 walker = RewriteOfRuleRefWalker::TreeParser.new( nodes ) 1475 result = walker.s 1476 stree = result.tree.nil? ? '' : result.tree.inspect 1477 stree.should == "abc 34" 1478 end 1479 1480 example "rewrite of rule ref root" do 1481 lexer = RewriteOfRuleRefRoot::Lexer.new( "abc 12 34" ) 1482 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1483 parser = RewriteOfRuleRefRoot::Parser.new( tokens ) 1484 1485 result = parser.a 1486 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1487 nodes.token_stream = tokens 1488 walker = RewriteOfRuleRefRootWalker::TreeParser.new( nodes ) 1489 result = walker.s 1490 stree = result.tree.nil? ? '' : result.tree.inspect 1491 stree.should == "(12 (abc 34))" 1492 end 1493 1494 example "rewrite of rule ref root labeled" do 1495 lexer = RewriteOfRuleRefRootLabeled::Lexer.new( "abc 12 34" ) 1496 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1497 parser = RewriteOfRuleRefRootLabeled::Parser.new( tokens ) 1498 1499 result = parser.a 1500 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1501 nodes.token_stream = tokens 1502 walker = RewriteOfRuleRefRootLabeledWalker::TreeParser.new( nodes ) 1503 result = walker.s 1504 stree = result.tree.nil? ? '' : result.tree.inspect 1505 stree.should == "(12 (abc 34))" 1506 end 1507 1508 example "rewrite of rule ref root list labeled" do 1509 lexer = RewriteOfRuleRefRootListLabeled::Lexer.new( "abc 12 34" ) 1510 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1511 parser = RewriteOfRuleRefRootListLabeled::Parser.new( tokens ) 1512 1513 result = parser.a 1514 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1515 nodes.token_stream = tokens 1516 walker = RewriteOfRuleRefRootListLabeledWalker::TreeParser.new( nodes ) 1517 result = walker.s 1518 stree = result.tree.nil? ? '' : result.tree.inspect 1519 stree.should == "(12 (abc 34))" 1520 end 1521 1522 example "rewrite of rule ref child" do 1523 lexer = RewriteOfRuleRefChild::Lexer.new( "abc 34" ) 1524 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1525 parser = RewriteOfRuleRefChild::Parser.new( tokens ) 1526 1527 result = parser.a 1528 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1529 nodes.token_stream = tokens 1530 walker = RewriteOfRuleRefChildWalker::TreeParser.new( nodes ) 1531 result = walker.s 1532 stree = result.tree.nil? ? '' : result.tree.inspect 1533 stree.should == "(34 34)" 1534 end 1535 1536 example "rewrite of rule ref label" do 1537 lexer = RewriteOfRuleRefLabel::Lexer.new( "abc 34" ) 1538 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1539 parser = RewriteOfRuleRefLabel::Parser.new( tokens ) 1540 1541 result = parser.a 1542 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1543 nodes.token_stream = tokens 1544 walker = RewriteOfRuleRefLabelWalker::TreeParser.new( nodes ) 1545 result = walker.s 1546 stree = result.tree.nil? ? '' : result.tree.inspect 1547 stree.should == "(34 34)" 1548 end 1549 1550 example "rewrite of rule ref list label" do 1551 lexer = RewriteOfRuleRefListLabel::Lexer.new( "abc 34" ) 1552 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1553 parser = RewriteOfRuleRefListLabel::Parser.new( tokens ) 1554 1555 result = parser.a 1556 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1557 nodes.token_stream = tokens 1558 walker = RewriteOfRuleRefListLabelWalker::TreeParser.new( nodes ) 1559 result = walker.s 1560 stree = result.tree.nil? ? '' : result.tree.inspect 1561 stree.should == "(34 34)" 1562 end 1563 1564 example "rewrite mode with predicated rewrites" do 1565 lexer = RewriteModeWithPredicatedRewrites::Lexer.new( "abc 34" ) 1566 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1567 parser = RewriteModeWithPredicatedRewrites::Parser.new( tokens ) 1568 1569 result = parser.a 1570 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1571 nodes.token_stream = tokens 1572 walker = RewriteModeWithPredicatedRewritesWalker::TreeParser.new( nodes ) 1573 result = walker.s 1574 stree = result.tree.nil? ? '' : result.tree.inspect 1575 stree.should == "(root (ick 34))" 1576 end 1577 1578 example "wildcard single node" do 1579 lexer = WildcardSingleNode::Lexer.new( "abc 34" ) 1580 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1581 parser = WildcardSingleNode::Parser.new( tokens ) 1582 1583 result = parser.a 1584 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1585 nodes.token_stream = tokens 1586 walker = WildcardSingleNodeWalker::TreeParser.new( nodes ) 1587 result = walker.s 1588 stree = result.tree.nil? ? '' : result.tree.inspect 1589 stree.should == "34" 1590 end 1591 1592 example "wildcard unlabeled single node" do 1593 lexer = WildcardUnlabeledSingleNode::Lexer.new( "abc 34" ) 1594 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1595 parser = WildcardUnlabeledSingleNode::Parser.new( tokens ) 1596 1597 result = parser.a 1598 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1599 nodes.token_stream = tokens 1600 walker = WildcardUnlabeledSingleNodeWalker::TreeParser.new( nodes ) 1601 result = walker.s 1602 stree = result.tree.nil? ? '' : result.tree.inspect 1603 stree.should == "abc" 1604 end 1605 1606 example "wildcard grabs subtree" do 1607 lexer = WildcardGrabsSubtree::Lexer.new( "abc 1 2 3" ) 1608 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1609 parser = WildcardGrabsSubtree::Parser.new( tokens ) 1610 1611 result = parser.a 1612 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1613 nodes.token_stream = tokens 1614 walker = WildcardGrabsSubtreeWalker::TreeParser.new( nodes ) 1615 result = walker.s 1616 stree = result.tree.nil? ? '' : result.tree.inspect 1617 stree.should == "(1 2 3)" 1618 end 1619 1620 example "wildcard grabs subtree2" do 1621 lexer = WildcardGrabsSubtree2::Lexer.new( "abc 1 2 3" ) 1622 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1623 parser = WildcardGrabsSubtree2::Parser.new( tokens ) 1624 1625 result = parser.a 1626 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1627 nodes.token_stream = tokens 1628 walker = WildcardGrabsSubtree2Walker::TreeParser.new( nodes ) 1629 result = walker.s 1630 stree = result.tree.nil? ? '' : result.tree.inspect 1631 stree.should == "(1 2 3)" 1632 end 1633 1634 example "wildcard list label" do 1635 lexer = WildcardListLabel::Lexer.new( "1 2 3" ) 1636 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1637 parser = WildcardListLabel::Parser.new( tokens ) 1638 1639 result = parser.a 1640 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1641 nodes.token_stream = tokens 1642 walker = WildcardListLabelWalker::TreeParser.new( nodes ) 1643 result = walker.s 1644 stree = result.tree.nil? ? '' : result.tree.inspect 1645 stree.should == "1 2 3" 1646 end 1647 1648 example "wildcard list label2" do 1649 lexer = WildcardListLabel2::Lexer.new( "1 2 3" ) 1650 tokens = ANTLR3::CommonTokenStream.new( lexer ) 1651 parser = WildcardListLabel2::Parser.new( tokens ) 1652 1653 result = parser.a 1654 nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree ) 1655 nodes.token_stream = tokens 1656 walker = WildcardListLabel2Walker::TreeParser.new( nodes ) 1657 result = walker.s 1658 stree = result.tree.nil? ? '' : result.tree.inspect 1659 stree.should == "(2 3) (2 3)" 1660 end 1661 1662end 1663