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