Home | History | Annotate | Download | only in functional
      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>t047treeparser</title>
      6 
      7 <!-- ANTLR includes -->
      8 <script type="text/javascript" src="../../lib/antlr3-all.js"></script>
      9 <script type="text/javascript" src="t047treeparserLexer.js"></script>
     10 <script type="text/javascript" src="t047treeparserParser.js"></script>
     11 <script type="text/javascript" src="t047treeparserWalker.js"></script>
     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, t047treeparserLexer, {
     22         reportError: function(re) {
     23             throw re;
     24         }
     25     });
     26 
     27     var TParser = function() {
     28         TParser.superclass.constructor.apply(this, arguments);
     29     };
     30     org.antlr.lang.extend(TParser, t047treeparserParser, {
     31         recover: function(re) {
     32         throw new PrivateError(re);
     33         }
     34     });
     35 
     36     var TWalker = function() {
     37         TWalker.superclass.constructor.apply(this, arguments);
     38         this.traces = [];
     39     };
     40     org.antlr.lang.extend(TWalker, t047treeparserWalker, {
     41         recover: function(re) {
     42             throw new PrivateError(re);
     43         },
     44         traceIn: function(ruleName, ruleIndex) {
     45             this.traces.push(">"+ruleName);
     46         },
     47         traceOut: function(ruleName, ruleIndex) {
     48             this.traces.push("<"+ruleName);
     49         }
     50     });
     51 
     52     function testWalker() {
     53         var xinput = [
     54                         "char c;",
     55                         "int x;",
     56                         "",
     57                         "void bar(int x);",
     58                         "",
     59                         "int foo(int y, char d) {",
     60                         "  int i;",
     61                         "  for (i=0; i<3; i=i+1) {",
     62                         "    x=3;",
     63                         "    y=5;",
     64                         "  }",
     65                         "}"
     66                      ].join("\n"),
     67             xoutput = "(VAR_DEF char c) (VAR_DEF int x) (FUNC_DECL (FUNC_HDR void bar (ARG_DEF int x))) (FUNC_DEF (FUNC_HDR int foo (ARG_DEF int y) (ARG_DEF char d)) (BLOCK (VAR_DEF int i) (for (= i 0) (< i 3) (= i (+ i 1)) (BLOCK (= x 3) (= y 5)))))",
     68             traces = [
     69               '>program', '>declaration', '>variable', '>type', '<type',
     70               '>declarator', '<declarator', '<variable', '<declaration',
     71               '>declaration', '>variable', '>type', '<type', '>declarator',
     72               '<declarator', '<variable', '<declaration', '>declaration',
     73               '>functionHeader', '>type', '<type', '>formalParameter',
     74               '>type', '<type', '>declarator', '<declarator',
     75               '<formalParameter', '<functionHeader', '<declaration',
     76               '>declaration', '>functionHeader', '>type', '<type',
     77               '>formalParameter', '>type', '<type', '>declarator',
     78               '<declarator', '<formalParameter', '>formalParameter', '>type',
     79               '<type', '>declarator', '<declarator', '<formalParameter',
     80               '<functionHeader', '>block', '>variable', '>type', '<type',
     81               '>declarator', '<declarator', '<variable', '>stat', '>forStat',
     82               '>expr', '>expr', '>atom', '<atom', '<expr', '<expr', '>expr',
     83               '>expr', '>atom', '<atom', '<expr', '>expr', '>atom', '<atom',
     84               '<expr', '<expr', '>expr', '>expr', '>expr', '>atom', '<atom',
     85               '<expr', '>expr', '>atom', '<atom', '<expr', '<expr', '<expr',
     86               '>block', '>stat', '>expr', '>expr', '>atom', '<atom', '<expr',
     87               '<expr', '<stat', '>stat', '>expr', '>expr', '>atom', '<atom',
     88               '<expr', '<expr', '<stat', '<block', '<forStat', '<stat',
     89               '<block', '<declaration', '<program'
     90             ];
     91 
     92 
     93         var cstream = new org.antlr.runtime.ANTLRStringStream(xinput),
     94             lexer = new TLexer(cstream),
     95             tstream = new org.antlr.runtime.CommonTokenStream(lexer),
     96             parser = new TParser(tstream);
     97         var r = parser.program();
     98 
     99         assertEquals(r.getTree().toStringTree(), xoutput);
    100 
    101         var nodes = new org.antlr.runtime.tree.CommonTreeNodeStream(r.getTree());
    102         nodes.setTokenStream(tstream);
    103         var walker = new TWalker(nodes);
    104         walker.program();
    105 
    106         assertArrayEquals(walker.traces, traces);
    107     }
    108 
    109     function testRuleLabelPropertyRefText() {
    110         var cstream = new org.antlr.runtime.ANTLRStringStream("\nchar c;\n"),
    111             lexer = new TLexer(cstream),
    112             tstream = new org.antlr.runtime.CommonTokenStream(lexer),
    113             parser = new TParser(tstream),
    114             r = parser.variable();
    115 
    116         var nodes = new org.antlr.runtime.tree.CommonTreeNodeStream(r.getTree());
    117         nodes.setTokenStream(tstream);
    118         var walker = new TWalker(nodes);
    119         r = walker.variable();
    120 
    121         assertEquals(r, 'c');
    122     }
    123 </script>
    124 
    125 </head>
    126 <body>
    127     <h1>t047treeparser</h1>
    128 </body>
    129 </html>
    130