1import unittest 2import textwrap 3import antlr3 4import testbase 5 6class t042ast(testbase.ANTLRTest): 7## def lexerClass(self, base): 8## class TLexer(base): 9## def reportError(self, re): 10## # no error recovery yet, just crash! 11## raise re 12 13## return TLexer 14 15 16 def parserClass(self, base): 17 class TParser(base): 18 def recover(self, input, re): 19 # no error recovery yet, just crash! 20 raise 21 22 return TParser 23 24 25 def parse(self, text, method, rArgs=(), **kwargs): 26 self.compileGrammar() #options='-trace') 27 28 cStream = antlr3.StringStream(text) 29 self.lexer = self.getLexer(cStream) 30 tStream = antlr3.CommonTokenStream(self.lexer) 31 self.parser = self.getParser(tStream) 32 33 for attr, val in kwargs.items(): 34 setattr(self.parser, attr, val) 35 36 return getattr(self.parser, method)(*rArgs) 37 38 39 def testR1(self): 40 r = self.parse("1 + 2", 'r1') 41 self.assertEqual( 42 r.tree.toStringTree(), 43 '(+ 1 2)' 44 ) 45 46 47 def testR2a(self): 48 r = self.parse("assert 2+3;", 'r2') 49 self.assertEqual( 50 r.tree.toStringTree(), 51 '(assert (+ 2 3))' 52 ) 53 54 55 def testR2b(self): 56 r = self.parse("assert 2+3 : 5;", 'r2') 57 self.assertEqual( 58 r.tree.toStringTree(), 59 '(assert (+ 2 3) 5)' 60 ) 61 62 63 def testR3a(self): 64 r = self.parse("if 1 fooze", 'r3') 65 self.assertEqual( 66 r.tree.toStringTree(), 67 '(if 1 fooze)' 68 ) 69 70 71 def testR3b(self): 72 r = self.parse("if 1 fooze else fooze", 'r3') 73 self.assertEqual( 74 r.tree.toStringTree(), 75 '(if 1 fooze fooze)' 76 ) 77 78 79 def testR4a(self): 80 r = self.parse("while 2 fooze", 'r4') 81 self.assertEqual( 82 r.tree.toStringTree(), 83 '(while 2 fooze)' 84 ) 85 86 87 def testR5a(self): 88 r = self.parse("return;", 'r5') 89 self.assertEqual( 90 r.tree.toStringTree(), 91 'return' 92 ) 93 94 95 def testR5b(self): 96 r = self.parse("return 2+3;", 'r5') 97 self.assertEqual( 98 r.tree.toStringTree(), 99 '(return (+ 2 3))' 100 ) 101 102 103 def testR6a(self): 104 r = self.parse("3", 'r6') 105 self.assertEqual( 106 r.tree.toStringTree(), 107 '3' 108 ) 109 110 111 def testR6b(self): 112 r = self.parse("3 a", 'r6') 113 self.assertEqual( 114 r.tree.toStringTree(), 115 '3 a' 116 ) 117 118 119 def testR7(self): 120 r = self.parse("3", 'r7') 121 self.assertIsNone(r.tree) 122 123 124 def testR8(self): 125 r = self.parse("var foo:bool", 'r8') 126 self.assertEqual( 127 r.tree.toStringTree(), 128 '(var bool foo)' 129 ) 130 131 132 def testR9(self): 133 r = self.parse("int foo;", 'r9') 134 self.assertEqual( 135 r.tree.toStringTree(), 136 '(VARDEF int foo)' 137 ) 138 139 140 def testR10(self): 141 r = self.parse("10", 'r10') 142 self.assertEqual( 143 r.tree.toStringTree(), 144 '10.0' 145 ) 146 147 148 def testR11a(self): 149 r = self.parse("1+2", 'r11') 150 self.assertEqual( 151 r.tree.toStringTree(), 152 '(EXPR (+ 1 2))' 153 ) 154 155 156 def testR11b(self): 157 r = self.parse("", 'r11') 158 self.assertEqual( 159 r.tree.toStringTree(), 160 'EXPR' 161 ) 162 163 164 def testR12a(self): 165 r = self.parse("foo", 'r12') 166 self.assertEqual( 167 r.tree.toStringTree(), 168 'foo' 169 ) 170 171 172 def testR12b(self): 173 r = self.parse("foo, bar, gnurz", 'r12') 174 self.assertEqual( 175 r.tree.toStringTree(), 176 'foo bar gnurz' 177 ) 178 179 180 def testR13a(self): 181 r = self.parse("int foo;", 'r13') 182 self.assertEqual( 183 r.tree.toStringTree(), 184 '(int foo)' 185 ) 186 187 188 def testR13b(self): 189 r = self.parse("bool foo, bar, gnurz;", 'r13') 190 self.assertEqual( 191 r.tree.toStringTree(), 192 '(bool foo bar gnurz)' 193 ) 194 195 196 def testR14a(self): 197 r = self.parse("1+2 int", 'r14') 198 self.assertEqual( 199 r.tree.toStringTree(), 200 '(EXPR (+ 1 2) int)' 201 ) 202 203 204 def testR14b(self): 205 r = self.parse("1+2 int bool", 'r14') 206 self.assertEqual( 207 r.tree.toStringTree(), 208 '(EXPR (+ 1 2) int bool)' 209 ) 210 211 212 def testR14c(self): 213 r = self.parse("int bool", 'r14') 214 self.assertEqual( 215 r.tree.toStringTree(), 216 '(EXPR int bool)' 217 ) 218 219 220 def testR14d(self): 221 r = self.parse("fooze fooze int bool", 'r14') 222 self.assertEqual( 223 r.tree.toStringTree(), 224 '(EXPR fooze fooze int bool)' 225 ) 226 227 228 def testR14e(self): 229 r = self.parse("7+9 fooze fooze int bool", 'r14') 230 self.assertEqual( 231 r.tree.toStringTree(), 232 '(EXPR (+ 7 9) fooze fooze int bool)' 233 ) 234 235 236 def testR15(self): 237 r = self.parse("7", 'r15') 238 self.assertEqual( 239 r.tree.toStringTree(), 240 '7 7' 241 ) 242 243 244 def testR16a(self): 245 r = self.parse("int foo", 'r16') 246 self.assertEqual( 247 r.tree.toStringTree(), 248 '(int foo)' 249 ) 250 251 252 def testR16b(self): 253 r = self.parse("int foo, bar, gnurz", 'r16') 254 255 self.assertEqual( 256 r.tree.toStringTree(), 257 '(int foo) (int bar) (int gnurz)' 258 ) 259 260 261 def testR17a(self): 262 r = self.parse("for ( fooze ; 1 + 2 ; fooze ) fooze", 'r17') 263 self.assertEqual( 264 r.tree.toStringTree(), 265 '(for fooze (+ 1 2) fooze fooze)' 266 ) 267 268 269 def testR18a(self): 270 r = self.parse("for", 'r18') 271 self.assertEqual( 272 r.tree.toStringTree(), 273 'BLOCK' 274 ) 275 276 277 def testR19a(self): 278 r = self.parse("for", 'r19') 279 self.assertEqual( 280 r.tree.toStringTree(), 281 'for' 282 ) 283 284 285 def testR20a(self): 286 r = self.parse("for", 'r20') 287 self.assertEqual( 288 r.tree.toStringTree(), 289 'FOR' 290 ) 291 292 293 def testR21a(self): 294 r = self.parse("for", 'r21') 295 self.assertEqual( 296 r.tree.toStringTree(), 297 'BLOCK' 298 ) 299 300 301 def testR22a(self): 302 r = self.parse("for", 'r22') 303 self.assertEqual( 304 r.tree.toStringTree(), 305 'for' 306 ) 307 308 309 def testR23a(self): 310 r = self.parse("for", 'r23') 311 self.assertEqual( 312 r.tree.toStringTree(), 313 'FOR' 314 ) 315 316 317 def testR24a(self): 318 r = self.parse("fooze 1 + 2", 'r24') 319 self.assertEqual( 320 r.tree.toStringTree(), 321 '(fooze (+ 1 2))' 322 ) 323 324 325 def testR25a(self): 326 r = self.parse("fooze, fooze2 1 + 2", 'r25') 327 self.assertEqual( 328 r.tree.toStringTree(), 329 '(fooze (+ 1 2))' 330 ) 331 332 333 def testR26a(self): 334 r = self.parse("fooze, fooze2", 'r26') 335 self.assertEqual( 336 r.tree.toStringTree(), 337 '(BLOCK fooze fooze2)' 338 ) 339 340 341 def testR27a(self): 342 r = self.parse("fooze 1 + 2", 'r27') 343 self.assertEqual( 344 r.tree.toStringTree(), 345 '(fooze (fooze (+ 1 2)))' 346 ) 347 348 349 def testR28(self): 350 r = self.parse("foo28a", 'r28') 351 self.assertIsNone(r.tree) 352 353 354 def testR29(self): 355 self.assertRaises(RuntimeError, self.parse, "", 'r29') 356 357 358# FIXME: broken upstream? 359## def testR30(self): 360## try: 361## r = self.parse("fooze fooze", 'r30') 362## self.fail(r.tree.toStringTree()) 363## except RuntimeError: 364## pass 365 366 367 def testR31a(self): 368 r = self.parse("public int gnurz = 1 + 2;", 'r31', flag=0) 369 self.assertEqual( 370 r.tree.toStringTree(), 371 '(VARDEF gnurz public int (+ 1 2))' 372 ) 373 374 375 def testR31b(self): 376 r = self.parse("public int gnurz = 1 + 2;", 'r31', flag=1) 377 self.assertEqual( 378 r.tree.toStringTree(), 379 '(VARIABLE gnurz public int (+ 1 2))' 380 ) 381 382 383 def testR31c(self): 384 r = self.parse("public int gnurz = 1 + 2;", 'r31', flag=2) 385 self.assertEqual( 386 r.tree.toStringTree(), 387 '(FIELD gnurz public int (+ 1 2))' 388 ) 389 390 391 def testR32a(self): 392 r = self.parse("gnurz 32", 'r32', [1], flag=2) 393 self.assertEqual( 394 r.tree.toStringTree(), 395 'gnurz' 396 ) 397 398 399 def testR32b(self): 400 r = self.parse("gnurz 32", 'r32', [2], flag=2) 401 self.assertEqual( 402 r.tree.toStringTree(), 403 '32' 404 ) 405 406 407 def testR32c(self): 408 r = self.parse("gnurz 32", 'r32', [3], flag=2) 409 self.assertIsNone(r.tree) 410 411 412 def testR33a(self): 413 r = self.parse("public private fooze", 'r33') 414 self.assertEqual( 415 r.tree.toStringTree(), 416 'fooze' 417 ) 418 419 420 def testR34a(self): 421 r = self.parse("public class gnurz { fooze fooze2 }", 'r34') 422 self.assertEqual( 423 r.tree.toStringTree(), 424 '(class gnurz public fooze fooze2)' 425 ) 426 427 428 def testR34b(self): 429 r = self.parse("public class gnurz extends bool implements int, bool { fooze fooze2 }", 'r34') 430 self.assertEqual( 431 r.tree.toStringTree(), 432 '(class gnurz public (extends bool) (implements int bool) fooze fooze2)' 433 ) 434 435 436 def testR35(self): 437 self.assertRaises(RuntimeError, self.parse, "{ extends }", 'r35') 438 439 440 def testR36a(self): 441 r = self.parse("if ( 1 + 2 ) fooze", 'r36') 442 self.assertEqual( 443 r.tree.toStringTree(), 444 '(if (EXPR (+ 1 2)) fooze)' 445 ) 446 447 448 def testR36b(self): 449 r = self.parse("if ( 1 + 2 ) fooze else fooze2", 'r36') 450 self.assertEqual( 451 r.tree.toStringTree(), 452 '(if (EXPR (+ 1 2)) fooze fooze2)' 453 ) 454 455 456 def testR37(self): 457 r = self.parse("1 + 2 + 3", 'r37') 458 self.assertEqual( 459 r.tree.toStringTree(), 460 '(+ (+ 1 2) 3)' 461 ) 462 463 464 def testR38(self): 465 r = self.parse("1 + 2 + 3", 'r38') 466 self.assertEqual( 467 r.tree.toStringTree(), 468 '(+ (+ 1 2) 3)' 469 ) 470 471 472 def testR39a(self): 473 r = self.parse("gnurz[1]", 'r39') 474 self.assertEqual( 475 r.tree.toStringTree(), 476 '(INDEX gnurz 1)' 477 ) 478 479 480 def testR39b(self): 481 r = self.parse("gnurz(2)", 'r39') 482 self.assertEqual( 483 r.tree.toStringTree(), 484 '(CALL gnurz 2)' 485 ) 486 487 488 def testR39c(self): 489 r = self.parse("gnurz.gnarz", 'r39') 490 self.assertEqual( 491 r.tree.toStringTree(), 492 '(FIELDACCESS gnurz gnarz)' 493 ) 494 495 496 def testR39d(self): 497 r = self.parse("gnurz.gnarz.gnorz", 'r39') 498 self.assertEqual( 499 r.tree.toStringTree(), 500 '(FIELDACCESS (FIELDACCESS gnurz gnarz) gnorz)' 501 ) 502 503 504 def testR40(self): 505 r = self.parse("1 + 2 + 3;", 'r40') 506 self.assertEqual( 507 r.tree.toStringTree(), 508 '(+ 1 2 3)' 509 ) 510 511 512 def testR41(self): 513 r = self.parse("1 + 2 + 3;", 'r41') 514 self.assertEqual( 515 r.tree.toStringTree(), 516 '(3 (2 1))' 517 ) 518 519 520 def testR42(self): 521 r = self.parse("gnurz, gnarz, gnorz", 'r42') 522 self.assertEqual( 523 r.tree.toStringTree(), 524 'gnurz gnarz gnorz' 525 ) 526 527 528 def testR43(self): 529 r = self.parse("gnurz, gnarz, gnorz", 'r43') 530 self.assertIsNone(r.tree) 531 self.assertEqual( 532 r.res, 533 ['gnurz', 'gnarz', 'gnorz'] 534 ) 535 536 537 def testR44(self): 538 r = self.parse("gnurz, gnarz, gnorz", 'r44') 539 self.assertEqual( 540 r.tree.toStringTree(), 541 '(gnorz (gnarz gnurz))' 542 ) 543 544 545 def testR45(self): 546 r = self.parse("gnurz", 'r45') 547 self.assertEqual( 548 r.tree.toStringTree(), 549 'gnurz' 550 ) 551 552 553 def testR46(self): 554 r = self.parse("gnurz, gnarz, gnorz", 'r46') 555 self.assertIsNone(r.tree) 556 self.assertEqual( 557 r.res, 558 ['gnurz', 'gnarz', 'gnorz'] 559 ) 560 561 562 def testR47(self): 563 r = self.parse("gnurz, gnarz, gnorz", 'r47') 564 self.assertEqual( 565 r.tree.toStringTree(), 566 'gnurz gnarz gnorz' 567 ) 568 569 570 def testR48(self): 571 r = self.parse("gnurz, gnarz, gnorz", 'r48') 572 self.assertEqual( 573 r.tree.toStringTree(), 574 'gnurz gnarz gnorz' 575 ) 576 577 578 def testR49(self): 579 r = self.parse("gnurz gnorz", 'r49') 580 self.assertEqual( 581 r.tree.toStringTree(), 582 '(gnurz gnorz)' 583 ) 584 585 586 def testR50(self): 587 r = self.parse("gnurz", 'r50') 588 self.assertEqual( 589 r.tree.toStringTree(), 590 '(1.0 gnurz)' 591 ) 592 593 594 def testR51(self): 595 r = self.parse("gnurza gnurzb gnurzc", 'r51') 596 self.assertEqual( 597 r.res.toStringTree(), 598 'gnurzb' 599 ) 600 601 602 def testR52(self): 603 r = self.parse("gnurz", 'r52') 604 self.assertEqual( 605 r.res.toStringTree(), 606 'gnurz' 607 ) 608 609 610 def testR53(self): 611 r = self.parse("gnurz", 'r53') 612 self.assertEqual( 613 r.res.toStringTree(), 614 'gnurz' 615 ) 616 617 618 def testR54(self): 619 r = self.parse("gnurza 1 + 2 gnurzb", 'r54') 620 self.assertEqual( 621 r.tree.toStringTree(), 622 '(+ 1 2)' 623 ) 624 625 626 def testR55a(self): 627 r = self.parse("public private 1 + 2", 'r55') 628 self.assertEqual( 629 r.tree.toStringTree(), 630 'public private (+ 1 2)' 631 ) 632 633 634 def testR55b(self): 635 r = self.parse("public fooze", 'r55') 636 self.assertEqual( 637 r.tree.toStringTree(), 638 'public fooze' 639 ) 640 641 642 def testR56(self): 643 r = self.parse("a b c d", 'r56') 644 self.assertEqual( 645 r.tree.toStringTree(), 646 'foo' 647 ) 648 649 650 def testR57(self): 651 r = self.parse("a b c d", 'r57') 652 self.assertEqual( 653 r.tree.toStringTree(), 654 'foo' 655 ) 656 657 658 def testR59(self): 659 r = self.parse("a b c fooze", 'r59') 660 self.assertEqual( 661 r.tree.toStringTree(), 662 '(a fooze) (b fooze) (c fooze)' 663 ) 664 665 666 667if __name__ == '__main__': 668 unittest.main() 669 670