xref: /aosp_15_r20/external/antlr/runtime/JavaScript/tests/functional/t047treeparser.html (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
1*16467b97STreehugger Robot<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2*16467b97STreehugger Robot<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3*16467b97STreehugger Robot<head>
4*16467b97STreehugger Robot<meta http-equiv="content-type" content="text/html;charset=utf-8" />
5*16467b97STreehugger Robot<title>t047treeparser</title>
6*16467b97STreehugger Robot
7*16467b97STreehugger Robot<!-- ANTLR includes -->
8*16467b97STreehugger Robot<script type="text/javascript" src="../../lib/antlr3-all.js"></script>
9*16467b97STreehugger Robot<script type="text/javascript" src="t047treeparserLexer.js"></script>
10*16467b97STreehugger Robot<script type="text/javascript" src="t047treeparserParser.js"></script>
11*16467b97STreehugger Robot<script type="text/javascript" src="t047treeparserWalker.js"></script>
12*16467b97STreehugger Robot
13*16467b97STreehugger Robot<!-- JsUnit include -->
14*16467b97STreehugger Robot<script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>
15*16467b97STreehugger Robot
16*16467b97STreehugger Robot<!-- Test Code -->
17*16467b97STreehugger Robot<script type="text/javascript">
18*16467b97STreehugger Robot    var TLexer = function() {
19*16467b97STreehugger Robot        TLexer.superclass.constructor.apply(this, arguments);
20*16467b97STreehugger Robot    };
21*16467b97STreehugger Robot    org.antlr.lang.extend(TLexer, t047treeparserLexer, {
22*16467b97STreehugger Robot        reportError: function(re) {
23*16467b97STreehugger Robot            throw re;
24*16467b97STreehugger Robot        }
25*16467b97STreehugger Robot    });
26*16467b97STreehugger Robot
27*16467b97STreehugger Robot    var TParser = function() {
28*16467b97STreehugger Robot        TParser.superclass.constructor.apply(this, arguments);
29*16467b97STreehugger Robot    };
30*16467b97STreehugger Robot    org.antlr.lang.extend(TParser, t047treeparserParser, {
31*16467b97STreehugger Robot        recover: function(re) {
32*16467b97STreehugger Robot        throw new PrivateError(re);
33*16467b97STreehugger Robot        }
34*16467b97STreehugger Robot    });
35*16467b97STreehugger Robot
36*16467b97STreehugger Robot    var TWalker = function() {
37*16467b97STreehugger Robot        TWalker.superclass.constructor.apply(this, arguments);
38*16467b97STreehugger Robot        this.traces = [];
39*16467b97STreehugger Robot    };
40*16467b97STreehugger Robot    org.antlr.lang.extend(TWalker, t047treeparserWalker, {
41*16467b97STreehugger Robot        recover: function(re) {
42*16467b97STreehugger Robot            throw new PrivateError(re);
43*16467b97STreehugger Robot        },
44*16467b97STreehugger Robot        traceIn: function(ruleName, ruleIndex) {
45*16467b97STreehugger Robot            this.traces.push(">"+ruleName);
46*16467b97STreehugger Robot        },
47*16467b97STreehugger Robot        traceOut: function(ruleName, ruleIndex) {
48*16467b97STreehugger Robot            this.traces.push("<"+ruleName);
49*16467b97STreehugger Robot        }
50*16467b97STreehugger Robot    });
51*16467b97STreehugger Robot
52*16467b97STreehugger Robot    function testWalker() {
53*16467b97STreehugger Robot        var xinput = [
54*16467b97STreehugger Robot                        "char c;",
55*16467b97STreehugger Robot                        "int x;",
56*16467b97STreehugger Robot                        "",
57*16467b97STreehugger Robot                        "void bar(int x);",
58*16467b97STreehugger Robot                        "",
59*16467b97STreehugger Robot                        "int foo(int y, char d) {",
60*16467b97STreehugger Robot                        "  int i;",
61*16467b97STreehugger Robot                        "  for (i=0; i<3; i=i+1) {",
62*16467b97STreehugger Robot                        "    x=3;",
63*16467b97STreehugger Robot                        "    y=5;",
64*16467b97STreehugger Robot                        "  }",
65*16467b97STreehugger Robot                        "}"
66*16467b97STreehugger Robot                     ].join("\n"),
67*16467b97STreehugger Robot            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*16467b97STreehugger Robot            traces = [
69*16467b97STreehugger Robot              '>program', '>declaration', '>variable', '>type', '<type',
70*16467b97STreehugger Robot              '>declarator', '<declarator', '<variable', '<declaration',
71*16467b97STreehugger Robot              '>declaration', '>variable', '>type', '<type', '>declarator',
72*16467b97STreehugger Robot              '<declarator', '<variable', '<declaration', '>declaration',
73*16467b97STreehugger Robot              '>functionHeader', '>type', '<type', '>formalParameter',
74*16467b97STreehugger Robot              '>type', '<type', '>declarator', '<declarator',
75*16467b97STreehugger Robot              '<formalParameter', '<functionHeader', '<declaration',
76*16467b97STreehugger Robot              '>declaration', '>functionHeader', '>type', '<type',
77*16467b97STreehugger Robot              '>formalParameter', '>type', '<type', '>declarator',
78*16467b97STreehugger Robot              '<declarator', '<formalParameter', '>formalParameter', '>type',
79*16467b97STreehugger Robot              '<type', '>declarator', '<declarator', '<formalParameter',
80*16467b97STreehugger Robot              '<functionHeader', '>block', '>variable', '>type', '<type',
81*16467b97STreehugger Robot              '>declarator', '<declarator', '<variable', '>stat', '>forStat',
82*16467b97STreehugger Robot              '>expr', '>expr', '>atom', '<atom', '<expr', '<expr', '>expr',
83*16467b97STreehugger Robot              '>expr', '>atom', '<atom', '<expr', '>expr', '>atom', '<atom',
84*16467b97STreehugger Robot              '<expr', '<expr', '>expr', '>expr', '>expr', '>atom', '<atom',
85*16467b97STreehugger Robot              '<expr', '>expr', '>atom', '<atom', '<expr', '<expr', '<expr',
86*16467b97STreehugger Robot              '>block', '>stat', '>expr', '>expr', '>atom', '<atom', '<expr',
87*16467b97STreehugger Robot              '<expr', '<stat', '>stat', '>expr', '>expr', '>atom', '<atom',
88*16467b97STreehugger Robot              '<expr', '<expr', '<stat', '<block', '<forStat', '<stat',
89*16467b97STreehugger Robot              '<block', '<declaration', '<program'
90*16467b97STreehugger Robot            ];
91*16467b97STreehugger Robot
92*16467b97STreehugger Robot
93*16467b97STreehugger Robot        var cstream = new org.antlr.runtime.ANTLRStringStream(xinput),
94*16467b97STreehugger Robot            lexer = new TLexer(cstream),
95*16467b97STreehugger Robot            tstream = new org.antlr.runtime.CommonTokenStream(lexer),
96*16467b97STreehugger Robot            parser = new TParser(tstream);
97*16467b97STreehugger Robot        var r = parser.program();
98*16467b97STreehugger Robot
99*16467b97STreehugger Robot        assertEquals(r.getTree().toStringTree(), xoutput);
100*16467b97STreehugger Robot
101*16467b97STreehugger Robot        var nodes = new org.antlr.runtime.tree.CommonTreeNodeStream(r.getTree());
102*16467b97STreehugger Robot        nodes.setTokenStream(tstream);
103*16467b97STreehugger Robot        var walker = new TWalker(nodes);
104*16467b97STreehugger Robot        walker.program();
105*16467b97STreehugger Robot
106*16467b97STreehugger Robot        assertArrayEquals(walker.traces, traces);
107*16467b97STreehugger Robot    }
108*16467b97STreehugger Robot
109*16467b97STreehugger Robot    function testRuleLabelPropertyRefText() {
110*16467b97STreehugger Robot        var cstream = new org.antlr.runtime.ANTLRStringStream("\nchar c;\n"),
111*16467b97STreehugger Robot            lexer = new TLexer(cstream),
112*16467b97STreehugger Robot            tstream = new org.antlr.runtime.CommonTokenStream(lexer),
113*16467b97STreehugger Robot            parser = new TParser(tstream),
114*16467b97STreehugger Robot            r = parser.variable();
115*16467b97STreehugger Robot
116*16467b97STreehugger Robot        var nodes = new org.antlr.runtime.tree.CommonTreeNodeStream(r.getTree());
117*16467b97STreehugger Robot        nodes.setTokenStream(tstream);
118*16467b97STreehugger Robot        var walker = new TWalker(nodes);
119*16467b97STreehugger Robot        r = walker.variable();
120*16467b97STreehugger Robot
121*16467b97STreehugger Robot        assertEquals(r, 'c');
122*16467b97STreehugger Robot    }
123*16467b97STreehugger Robot</script>
124*16467b97STreehugger Robot
125*16467b97STreehugger Robot</head>
126*16467b97STreehugger Robot<body>
127*16467b97STreehugger Robot    <h1>t047treeparser</h1>
128*16467b97STreehugger Robot</body>
129*16467b97STreehugger Robot</html>
130