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>t053hetero</title> 6 7 <!-- ANTLR includes --> 8 <script type="text/javascript" src="../../lib/antlr3-all.js"></script> 9 10 <script type="text/javascript" src="t053heteroT1Lexer.js"></script> 11 <script type="text/javascript" src="t053heteroT1Parser.js"></script> 12 13 <script type="text/javascript" src="t053heteroT2Lexer.js"></script> 14 <script type="text/javascript" src="t053heteroT2Parser.js"></script> 15 16 <script type="text/javascript" src="t053heteroT3Lexer.js"></script> 17 <script type="text/javascript" src="t053heteroT3Parser.js"></script> 18 19 <script type="text/javascript" src="t053heteroT4Lexer.js"></script> 20 <script type="text/javascript" src="t053heteroT4Parser.js"></script> 21 22 <script type="text/javascript" src="t053heteroT5Lexer.js"></script> 23 <script type="text/javascript" src="t053heteroT5Parser.js"></script> 24 25 <script type="text/javascript" src="t053heteroT6Lexer.js"></script> 26 <script type="text/javascript" src="t053heteroT6Parser.js"></script> 27 28 <script type="text/javascript" src="t053heteroT7Lexer.js"></script> 29 <script type="text/javascript" src="t053heteroT7Parser.js"></script> 30 31 <script type="text/javascript" src="t053heteroT8Lexer.js"></script> 32 <script type="text/javascript" src="t053heteroT8Parser.js"></script> 33 34 <script type="text/javascript" src="t053heteroT9Lexer.js"></script> 35 <script type="text/javascript" src="t053heteroT9Parser.js"></script> 36 37 <script type="text/javascript" src="t053heteroT10Lexer.js"></script> 38 <script type="text/javascript" src="t053heteroT10Parser.js"></script> 39 40 <script type="text/javascript" src="t053heteroT11Lexer.js"></script> 41 <script type="text/javascript" src="t053heteroT11Parser.js"></script> 42 43 <script type="text/javascript" src="t053heteroT12Lexer.js"></script> 44 <script type="text/javascript" src="t053heteroT12Parser.js"></script> 45 46 <script type="text/javascript" src="t053heteroT13Lexer.js"></script> 47 <script type="text/javascript" src="t053heteroT13Parser.js"></script> 48 <script type="text/javascript" src="t053heteroTP13Parser.js"></script> 49 50 <script type="text/javascript" src="t053heteroT14Lexer.js"></script> 51 <script type="text/javascript" src="t053heteroT14Parser.js"></script> 52 <script type="text/javascript" src="t053heteroTP14Parser.js"></script> 53 54 <script type="text/javascript" src="t053heteroT15Lexer.js"></script> 55 <script type="text/javascript" src="t053heteroT15Parser.js"></script> 56 <script type="text/javascript" src="t053heteroTP15Parser.js"></script> 57 58 <script type="text/javascript" src="t053heteroT16Lexer.js"></script> 59 <script type="text/javascript" src="t053heteroT16Parser.js"></script> 60 <script type="text/javascript" src="t053heteroTP16Parser.js"></script> 61 62 <script type="text/javascript" src="t053heteroT17Lexer.js"></script> 63 <script type="text/javascript" src="t053heteroT17Parser.js"></script> 64 <script type="text/javascript" src="t053heteroTP17Parser.js"></script> 65 66 <script type="text/javascript" src="t053heteroT18Lexer.js"></script> 67 <script type="text/javascript" src="t053heteroT18Parser.js"></script> 68 <script type="text/javascript" src="t053heteroTP18Parser.js"></script> 69 70 <script type="text/javascript" src="t053heteroT18Lexer.js"></script> 71 <script type="text/javascript" src="t053heteroT18Parser.js"></script> 72 <script type="text/javascript" src="t053heteroTP18Parser.js"></script> 73 74 <script type="text/javascript" src="t053heteroTLexer.js"></script> 75 <script type="text/javascript" src="t053heteroTParser.js"></script> 76 <script type="text/javascript" src="t053heteroTPParser.js"></script> 77 78 <!-- JsUnit include --> 79 <script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script> 80 81 <!-- Test Code --> 82 <script type="text/javascript"> 83 // Parser Tests 84 85 function execParser(lexerCls, parserCls, grammarEntry, xinput) { 86 var cstream = new org.antlr.runtime.ANTLRStringStream(xinput), 87 lexer = new (EnhancedParser(lexerCls))(cstream), 88 tstream = new org.antlr.runtime.CommonTokenStream(lexer), 89 parser = new (EnhancedParser(parserCls))(tstream); 90 var r = parser[grammarEntry](); 91 92 if (r) { 93 return r.getTree().toStringTree(); 94 } 95 return ""; 96 } 97 98 function execTreeParser(lexerCls, parserCls, grammarEntry, walkerCls, treeEntry, xinput) 99 { 100 var cstream = new org.antlr.runtime.ANTLRStringStream(xinput), 101 lexer = new (EnhancedParser(lexerCls))(cstream), 102 tstream = new org.antlr.runtime.CommonTokenStream(lexer), 103 parser = new (EnhancedParser(parserCls))(tstream); 104 var r = parser[grammarEntry](); 105 106 var nodes = new org.antlr.runtime.tree.CommonTreeNodeStream(r.getTree()); 107 nodes.setTokenStream(tstream); 108 var walker = new walkerCls(nodes); 109 r = walker[treeEntry](); 110 111 if (r) { 112 return r.getTree().toStringTree(); 113 } 114 return ""; 115 } 116 117 118 function EnhancedParser(base) { 119 var T = function() { 120 T.superclass.constructor.apply(this, arguments); 121 this.traces = []; 122 this.buf = ""; 123 }; 124 125 org.antlr.lang.extend(T, base, { 126 capture: function(t) { 127 this.buf += t; 128 }, 129 traceIn: function(ruleName, ruleIndex) { 130 this.traces.push(">"+ruleName); 131 }, 132 traceOut: function(ruleName, ruleIndex) { 133 this.traces.push("<"+ruleName); 134 }, 135 reportError: function(input, re) { 136 throw re; 137 } 138 }); 139 140 return T; 141 } 142 143 // PARSERS -- AUTO AST 144 145 function testToken() { 146 var found = execParser( 147 t053heteroT1Lexer, 148 t053heteroT1Parser, 149 "a", 150 "a"); 151 assertEquals("a<V>", found); 152 } 153 154 function testTokenWithLabel() { 155 var found = execParser( 156 t053heteroT2Lexer, 157 t053heteroT2Parser, 158 "a", 159 "a"); 160 assertEquals("a<V>", found); 161 } 162 163 function testTokenWithListLabel() { 164 var found = execParser( 165 t053heteroT3Lexer, 166 t053heteroT3Parser, 167 "a", 168 "a"); 169 assertEquals("a<V>", found); 170 } 171 172 function testTokenRoot() { 173 var found = execParser( 174 t053heteroT4Lexer, 175 t053heteroT4Parser, 176 "a", 177 "a"); 178 assertEquals("a<V>", found); 179 } 180 181 function testTokenRootWithListLabel() { 182 var found = execParser( 183 t053heteroT5Lexer, 184 t053heteroT5Parser, 185 "a", 186 "a"); 187 assertEquals("a<V>", found); 188 } 189 190 function testString() { 191 var found = execParser( 192 t053heteroT6Lexer, 193 t053heteroT6Parser, 194 "a", 195 "begin"); 196 assertEquals("begin<V>", found); 197 } 198 199 function testStringRoot() { 200 var found = execParser( 201 t053heteroT7Lexer, 202 t053heteroT7Parser, 203 "a", 204 "begin"); 205 assertEquals("begin<V>", found); 206 } 207 208 // PARSERS -- REWRITE AST 209 210 function testRewriteToken() { 211 var found = execParser( 212 t053heteroT8Lexer, 213 t053heteroT8Parser, 214 "a", 215 "a"); 216 assertEquals("a<V>", found); 217 } 218 219 function testrewritetokenwithargs() { 220 var found = execParser( 221 t053heteroT9Lexer, 222 t053heteroT9Parser, 223 "a", 224 "a"); 225 assertEquals("<V>;421930 a<V>;9900", found); 226 } 227 228 function testRewriteTokenRoot() { 229 var found = execParser( 230 t053heteroT10Lexer, 231 t053heteroT10Parser, 232 "a", 233 "a 2"); 234 assertEquals("(a<V> 2)", found); 235 } 236 237 function testRewriteString() { 238 var found = execParser( 239 t053heteroT11Lexer, 240 t053heteroT11Parser, 241 "a", 242 "begin"); 243 assertEquals("begin<V>", found); 244 } 245 246 function testRewriteStringRoot() { 247 var found = execParser( 248 t053heteroT12Lexer, 249 t053heteroT12Parser, 250 "a", 251 "begin 2"); 252 assertEquals("(begin<V> 2)", found); 253 } 254 255 // TREE PARSERS -- REWRITE AST 256 257 function testTreeParserRewriteFlatList() { 258 var found = execTreeParser( 259 t053heteroT13Lexer, 260 t053heteroT13Parser, 261 "a", 262 t053heteroTP13Parser, 263 "a", 264 "abc 34"); 265 assertEquals("34<V> abc<W>", found); 266 } 267 268 function testTreeParserRewriteTree() { 269 var found = execTreeParser( 270 t053heteroT14Lexer, 271 t053heteroT14Parser, 272 "a", 273 t053heteroTP14Parser, 274 "a", 275 "abc 34"); 276 assertEquals("(34<V> abc<W>)", found); 277 } 278 279 function testTreeParserRewriteImaginary() { 280 var found = execTreeParser( 281 t053heteroT15Lexer, 282 t053heteroT15Parser, 283 "a", 284 t053heteroTP15Parser, 285 "a", 286 "abc"); 287 assertEquals("ROOT<V> abc", found); 288 } 289 290 function testTreeParserRewriteImaginaryWithArgs() { 291 var found = execTreeParser( 292 t053heteroT16Lexer, 293 t053heteroT16Parser, 294 "a", 295 t053heteroTP16Parser, 296 "a", 297 "abc"); 298 assertEquals("ROOT<V>;42 abc", found); 299 } 300 301 function testTreeParserRewriteImaginaryRoot() { 302 var found = execTreeParser( 303 t053heteroT17Lexer, 304 t053heteroT17Parser, 305 "a", 306 t053heteroTP17Parser, 307 "a", 308 "abc"); 309 assertEquals("(ROOT<V> abc)", found); 310 } 311 312 function testTreeParserRewriteImaginaryFromReal() { 313 var found = execTreeParser( 314 t053heteroT18Lexer, 315 t053heteroT18Parser, 316 "a", 317 t053heteroTP18Parser, 318 "a", 319 "abc"); 320 assertEquals("ROOT<V>@1", found); 321 } 322 323 function testTreeParserAutoHeteroAST() { 324 var found = execTreeParser( 325 t053heteroTLexer, 326 t053heteroTParser, 327 "a", 328 t053heteroTPParser, 329 "a", 330 "abc;"); 331 assertEquals("abc<V> ;<V>", found); 332 } 333 </script> 334 335 </head> 336 <body> 337 <h1>t053hetero</h1> 338 </body> 339 </html> 340