1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 3 <head> 4 <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 5 <title>t042ast</title> 6 7 <!-- ANTLR includes --> 8 <script type="text/javascript" src="../../lib/antlr3-all.js"></script> 9 <script type="text/javascript" src="t042astLexer.js"></script> 10 <script type="text/javascript" src="t042astParser.js"></script> 11 12 13 <!-- JsUnit include --> 14 <script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script> 15 16 <!-- Test Code --> 17 <script type="text/javascript"> 18 var TLexer = function() { 19 TLexer.superclass.constructor.apply(this, arguments); 20 }; 21 org.antlr.lang.extend(TLexer, t042astLexer, { 22 recover: function(re) { 23 /* don't recover, just crash */ 24 throw re; 25 } 26 }); 27 28 var TParser = function() { 29 TParser.superclass.constructor.apply(this, arguments); 30 }; 31 org.antlr.lang.extend(TParser, t042astParser, { 32 recover: function(re) { 33 /* don't recover, just crash */ 34 throw re; 35 } 36 }); 37 38 39 function parse(text, method, rArgs, kwargs) { 40 var cstream = new org.antlr.runtime.ANTLRStringStream(text), 41 lexer = new TLexer(cstream), 42 tstream = new org.antlr.runtime.CommonTokenStream(lexer), 43 parser = new TParser(tstream); 44 45 rArgs = rArgs || []; 46 kwargs = kwargs || {}; 47 48 org.antlr.lang.augmentObject(parser, kwargs); 49 return parser[method].apply(parser, rArgs); 50 } 51 52 function testR1() { 53 var r = parse("1 + 2", "r1"); 54 assertEquals(r.tree.toStringTree(), "(+ 1 2)"); 55 } 56 57 function testR2a() { 58 var r = parse("assert 2+3;", "r2"); 59 assertEquals(r.tree.toStringTree(), "(assert (+ 2 3))"); 60 } 61 62 function testR2b() { 63 var r = parse("assert 2+3 : 5;", "r2"); 64 assertEquals(r.tree.toStringTree(), "(assert (+ 2 3) 5)"); 65 } 66 67 function testR3a() { 68 var r = parse("if 1 fooze", "r3"); 69 assertEquals(r.tree.toStringTree(), "(if 1 fooze)"); 70 } 71 72 function testR3b() { 73 var r = parse("if 1 fooze else fooze", "r3"); 74 assertEquals(r.tree.toStringTree(), "(if 1 fooze fooze)"); 75 } 76 77 function testR4a() { 78 var r = parse("while 2 fooze", "r4"); 79 assertEquals(r.tree.toStringTree(), "(while 2 fooze)"); 80 } 81 82 function testR4b() { 83 var r = parse("while 2 fooze", "r4"); 84 assertEquals(r.tree.toStringTree(), "(while 2 fooze)"); 85 } 86 87 function testR5a() { 88 var r = parse("return;", "r5"); 89 assertEquals(r.tree.toStringTree(), "return"); 90 } 91 92 function testR5b() { 93 var r = parse("return 2+3;", "r5"); 94 assertEquals(r.tree.toStringTree(), "(return (+ 2 3))"); 95 } 96 97 function testR6a() { 98 var r = parse("3", "r6"); 99 assertEquals(r.tree.toStringTree(), "3"); 100 } 101 102 function testR6b() { 103 var r = parse("3 a", "r6"); 104 assertEquals(r.tree.toStringTree(), "3 a"); 105 } 106 107 function testR7() { 108 var r = parse("3", "r7"); 109 assertNull(r.tree); 110 } 111 112 function testR8() { 113 var r = parse("var foo:bool", "r8"); 114 assertEquals(r.tree.toStringTree(), "(var bool foo)"); 115 } 116 117 function testR9() { 118 var r = parse("int foo;", "r9"); 119 assertEquals(r.tree.toStringTree(), "(VARDEF int foo)"); 120 } 121 122 function testR10() { 123 var r = parse("10", "r10"); 124 assertEquals(r.tree.toStringTree(), "10.0"); 125 } 126 127 function testR11a() { 128 var r = parse("1+2", "r11"); 129 assertEquals(r.tree.toStringTree(), "(EXPR (+ 1 2))"); 130 } 131 132 function testR11b() { 133 var r = parse("", "r11"); 134 assertEquals(r.tree.toStringTree(), "EXPR"); 135 } 136 137 function testR12a() { 138 var r = parse("foo", "r12"); 139 assertEquals(r.tree.toStringTree(), "foo"); 140 } 141 142 function testR12b() { 143 var r = parse("foo, bar, gnurz", "r12"); 144 assertEquals(r.tree.toStringTree(), "foo bar gnurz"); 145 } 146 147 function testR13a() { 148 var r = parse("int foo;", "r13"); 149 assertEquals(r.tree.toStringTree(), "(int foo)"); 150 } 151 152 function testR13b() { 153 var r = parse("bool foo, bar, gnurz;", "r13"); 154 assertEquals(r.tree.toStringTree(), "(bool foo bar gnurz)"); 155 } 156 157 function testR14a() { 158 var r = parse("1+2 int", "r14"); 159 assertEquals(r.tree.toStringTree(), "(EXPR (+ 1 2) int)"); 160 } 161 162 function testR14b() { 163 var r = parse("1+2 int bool", "r14"); 164 assertEquals(r.tree.toStringTree(), "(EXPR (+ 1 2) int bool)"); 165 } 166 167 function testR14c() { 168 var r = parse("int bool", "r14"); 169 assertEquals(r.tree.toStringTree(), "(EXPR int bool)"); 170 } 171 172 function testR14d() { 173 var r = parse("fooze fooze int bool", "r14"); 174 assertEquals(r.tree.toStringTree(), "(EXPR fooze fooze int bool)"); 175 } 176 177 function testR14e() { 178 var r = parse("7+9 fooze fooze int bool", "r14"); 179 assertEquals(r.tree.toStringTree(), "(EXPR (+ 7 9) fooze fooze int bool)"); 180 } 181 182 function testR15() { 183 var r = parse("7", "r15"); 184 assertEquals(r.tree.toStringTree(), "7 7"); 185 } 186 187 function testR16a() { 188 var r = parse("int foo", "r16"); 189 assertEquals(r.tree.toStringTree(), "(int foo)"); 190 } 191 192 function testR16b() { 193 var r = parse("int foo, bar, gnurz", "r16"); 194 assertEquals(r.tree.toStringTree(), "(int foo) (int bar) (int gnurz)"); 195 } 196 197 function testR17a() { 198 var r = parse("for ( fooze ; 1 + 2 ; fooze ) fooze", "r17"); 199 assertEquals(r.tree.toStringTree(), "(for fooze (+ 1 2) fooze fooze)"); 200 } 201 202 function testR18a() { 203 var r = parse("for", "r18"); 204 assertEquals(r.tree.toStringTree(), "BLOCK"); 205 } 206 207 function testR19a() { 208 var r = parse("for", "r19"); 209 assertEquals(r.tree.toStringTree(), "for"); 210 } 211 212 function testR20a() { 213 var r = parse("for", "r20"); 214 assertEquals(r.tree.toStringTree(), "FOR"); 215 } 216 217 function testR21a() { 218 var r = parse("for", "r21"); 219 assertEquals(r.tree.toStringTree(), "BLOCK"); 220 } 221 222 function testR22a() { 223 var r = parse("for", "r22"); 224 assertEquals(r.tree.toStringTree(), "for"); 225 } 226 227 function testR23a() { 228 var r = parse("for", "r23"); 229 assertEquals(r.tree.toStringTree(), "FOR"); 230 } 231 232 function testR24a() { 233 var r = parse("fooze 1 + 2", "r24"); 234 assertEquals(r.tree.toStringTree(), "(fooze (+ 1 2))"); 235 } 236 237 function testR25a() { 238 var r = parse("fooze, fooze 1 + 2", "r25"); 239 assertEquals(r.tree.toStringTree(), "(fooze (+ 1 2))"); 240 } 241 242 function testR26a() { 243 var r = parse("fooze, fooze2", "r26"); 244 assertEquals(r.tree.toStringTree(), "(BLOCK fooze fooze2)"); 245 } 246 247 function testR27a() { 248 var r = parse("fooze 1 + 2", "r27"); 249 assertEquals(r.tree.toStringTree(), "(fooze (fooze (+ 1 2)))"); 250 } 251 252 function testR28() { 253 var r = parse("foo28a", "r28"); 254 assertNull(r.getTree()); 255 } 256 257 function testR29() { 258 try { 259 var r = parse("", "r29"); 260 fail("should not reach here"); 261 } catch(e) {} 262 } 263 264 function testR31a() { 265 var r = parse("public int gnurz = 1 + 2;", "r31", null, {flag:0}); 266 assertEquals(r.tree.toStringTree(), '(VARDEF gnurz public int (+ 1 2))'); 267 } 268 269 function testR31b() { 270 var r = parse("public int gnurz = 1 + 2;", "r31", null, {flag:1}); 271 assertEquals(r.tree.toStringTree(), '(VARIABLE gnurz public int (+ 1 2))'); 272 } 273 274 function testR31c() { 275 var r = parse("public int gnurz = 1 + 2;", "r31", null, {flag:2}); 276 assertEquals(r.tree.toStringTree(), '(FIELD gnurz public int (+ 1 2))'); 277 } 278 279 function testR32a() { 280 var r = parse("gnurz 32", "r32", [1], {flag:2}); 281 assertEquals(r.tree.toStringTree(), 'gnurz'); 282 } 283 284 function testR32b() { 285 var r = parse("gnurz 32", "r32", [2], {flag:2}); 286 assertEquals(r.tree.toStringTree(), '32'); 287 } 288 289 function testR32c() { 290 var r = parse("gnurz 32", "r32", [3], {flag:2}); 291 assertNull(r.tree); 292 } 293 294 function testR33a() { 295 var r = parse("public private fooze", "r33", [2], {flag:2}); 296 assertEquals(r.tree.toStringTree(), 'fooze'); 297 } 298 299 function testR34a() { 300 var r = parse("public class gnurz { fooze fooze2 }", "r34"); 301 assertEquals(r.tree.toStringTree(), "(class gnurz public fooze fooze2)"); 302 } 303 304 function testR34b() { 305 var r = parse("public class gnurz extends bool implements int, bool { fooze fooze2 }", "r34"); 306 assertEquals(r.tree.toStringTree(), "(class gnurz public (extends bool) (implements int bool) fooze fooze2)"); 307 } 308 309 function testR35() { 310 try { 311 var r = parse("{ extends }", "r35"); 312 fail("should not reach here"); 313 } catch(e) {} 314 } 315 316 function testR36a() { 317 var r = parse("if ( 1 + 2 ) fooze", "r36"); 318 assertEquals(r.tree.toStringTree(), "(if (EXPR (+ 1 2)) fooze)"); 319 } 320 321 function testR36b() { 322 var r = parse("if ( 1 + 2 ) fooze else fooze2", "r36"); 323 assertEquals(r.tree.toStringTree(), "(if (EXPR (+ 1 2)) fooze fooze2)"); 324 } 325 326 function testR37() { 327 var r = parse("1 + 2 + 3", "r37"); 328 assertEquals(r.tree.toStringTree(), "(+ (+ 1 2) 3)"); 329 } 330 331 function testR38() { 332 var r = parse("1 + 2 + 3", "r38"); 333 assertEquals(r.tree.toStringTree(), "(+ (+ 1 2) 3)"); 334 } 335 336 function testR39a() { 337 var r = parse("gnurz[1]", "r39"); 338 assertEquals(r.tree.toStringTree(), "(INDEX gnurz 1)"); 339 } 340 341 function testR39b() { 342 var r = parse("gnurz(2)", "r39"); 343 assertEquals(r.tree.toStringTree(), "(CALL gnurz 2)"); 344 } 345 346 function testR39c() { 347 var r = parse("gnurz.gnurz", "r39"); 348 assertEquals(r.tree.toStringTree(), "(FIELDACCESS gnurz gnurz)"); 349 } 350 351 function testR39d() { 352 var r = parse("gnurz.gnarz.gnorz", "r39"); 353 assertEquals(r.tree.toStringTree(), "(FIELDACCESS (FIELDACCESS gnurz gnarz) gnorz)"); 354 } 355 356 function testR40() { 357 var r = parse("1 + 2 + 3;", "r40"); 358 assertEquals(r.tree.toStringTree(), "(+ 1 2 3)"); 359 } 360 361 function testR41() { 362 var r = parse("1 + 2 + 3;", "r41"); 363 assertEquals(r.tree.toStringTree(), "(3 (2 1))"); 364 } 365 366 function testR42() { 367 var r = parse("gnurz, gnarz, gnorz", "r42"); 368 assertEquals(r.tree.toStringTree(), "gnurz gnarz gnorz"); 369 } 370 371 function testR43() { 372 var r = parse("gnurz, gnarz, gnorz", "r43"); 373 assertNull(r.tree); 374 assertArrayEquals(r.res, ["gnurz", "gnarz", "gnorz"]); 375 } 376 377 function testR44() { 378 var r = parse("gnurz, gnarz, gnorz", "r44"); 379 assertEquals(r.tree.toStringTree(), "(gnorz (gnarz gnurz))"); 380 } 381 382 function testR45() { 383 var r = parse("gnurz", "r45"); 384 assertEquals(r.tree.toStringTree(), "gnurz"); 385 } 386 387 function testR46() { 388 var r = parse("gnurz, gnarz, gnorz", "r46"); 389 assertNull(r.tree); 390 assertArrayEquals(r.res, ["gnurz", "gnarz", "gnorz"]); 391 } 392 393 function testR47() { 394 var r = parse("gnurz, gnarz, gnorz", "r47"); 395 assertEquals(r.tree.toStringTree(), "gnurz gnarz gnorz"); 396 } 397 398 function testR48() { 399 var r = parse("gnurz, gnarz, gnorz", "r48"); 400 assertEquals(r.tree.toStringTree(), "gnurz gnarz gnorz"); 401 } 402 403 function testR49() { 404 var r = parse("gnurz gnorz", "r49"); 405 assertEquals(r.tree.toStringTree(), "(gnurz gnorz)"); 406 } 407 408 function testR50() { 409 var r = parse("gnurz", "r50"); 410 assertEquals(r.tree.toStringTree(), "(1.0 gnurz)"); 411 } 412 413 function testR51() { 414 var r = parse("gnurza gnurzb gnurzc", "r51"); 415 assertEquals(r.res.toStringTree(), "gnurzb"); 416 } 417 418 function testR52() { 419 var r = parse("gnurz", "r52"); 420 assertEquals(r.res.toStringTree(), "gnurz"); 421 } 422 423 function testR53() { 424 var r = parse("gnurz", "r53"); 425 assertEquals(r.res.toStringTree(), "gnurz"); 426 } 427 428 function testR54() { 429 var r = parse("gnurza 1 + 2 gnurzb", "r54"); 430 assertEquals(r.tree.toStringTree(), "(+ 1 2)"); 431 } 432 433 function testR55a() { 434 var r = parse("public private 1 + 2", "r55"); 435 assertEquals(r.tree.toStringTree(), "public private (+ 1 2)"); 436 } 437 438 function testR55b() { 439 var r = parse("public fooze", "r55"); 440 assertEquals(r.tree.toStringTree(), "public fooze"); 441 } 442 443 function testR56() { 444 var r = parse("a b c d", "r56"); 445 assertEquals(r.tree.toStringTree(), "foo"); 446 } 447 448 function testR57() { 449 var r = parse("a b c d", "r57"); 450 assertEquals(r.tree.toStringTree(), "foo"); 451 } 452 453 function testR59() { 454 var r = parse("a b c fooze", "r59"); 455 assertEquals(r.tree.toStringTree(), "(a fooze) (b fooze) (c fooze)"); 456 } 457 </script> 458 459 </head> 460 <body> 461 <h1>t042ast</h1> 462 </body> 463 </html> 464