parservisitor.js revision 1204:9597425b6b38
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * Nashorn parser API - Basic TreeVisitor tests.
26 *
27 * @test
28 * @option -scripting
29 * @run
30 */
31
32// Java types used
33var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
34var Parser = Java.type("jdk.nashorn.api.tree.Parser");
35
36function parse(name, script, visitor) {
37    var parser = Parser.create("--empty-statements");
38    var tree = parser.parse(name, script, null);
39    return tree.accept(visitor, print);
40}
41
42parse("arrayaccess.js", "this['eval']",
43    new (Java.extend(SimpleTreeVisitor))() {
44        visitArrayAccess: function(aa) {
45            print("in visitArrayAccess " +
46              aa.expression.name + " " + aa.index.value);
47        }
48    });
49
50parse("arrayliteral.js", "[2, 3, 22]",
51    new (Java.extend(SimpleTreeVisitor))() {
52        visitArrayLiteral: function(al) {
53            print("in visitArrayLiteral");
54            for each (var e in al.elements) {
55               print(e.value);
56            }
57        }
58    });
59
60parse("assign.js", "x = 33",
61    new (Java.extend(SimpleTreeVisitor))() {
62        visitAssignment: function(an) {
63            print("in visitAssignment " +
64                an.variable.name + " " + an.expression.value);
65        }
66    });
67
68function binaryExpr(name, code) {
69    parse(name, code,
70        new (Java.extend(SimpleTreeVisitor))() {
71            visitBinary: function(bn) {
72                print("in visitBinary " + bn.kind + " " +
73                    bn.leftOperand.value + ", " + bn.rightOperand.value);
74            }
75        });
76}
77
78binaryExpr("add.js", "3 + 4");
79binaryExpr("sub.js", "3 - 4");
80binaryExpr("mul.js", "3 * 4");
81binaryExpr("div.js", "3 / 4");
82binaryExpr("rem.js", "3 % 4");
83binaryExpr("rshift.js", "3 >> 4");
84binaryExpr("rshift.js", "3 >>> 4");
85binaryExpr("lshift.js", "3 << 4");
86binaryExpr("less.js", "3 < 4");
87binaryExpr("lessOrEq.js", "3 <= 4");
88binaryExpr("greater.js", "3 > 4");
89binaryExpr("greaterOrEq.js", "3 >= 4");
90binaryExpr("in.js", "3 in this");
91binaryExpr("eq.js", "3 == 3");
92binaryExpr("ne.js", "3 != 2");
93binaryExpr("seq.js", "3 === 2");
94binaryExpr("sne.js", "3 !== 2");
95binaryExpr("and.js", "3 & 2");
96binaryExpr("or.js", "3 | 2");
97binaryExpr("xor.js", "3 ^ 2");
98binaryExpr("cond_and.js", "3 && 2");
99binaryExpr("cond_or.js", "3 || 2");
100binaryExpr("comma", "3, 2");
101
102parse("block.js", "{ print('hello'); }",
103    new (Java.extend(SimpleTreeVisitor))() {
104        visitBlock: function() {
105            print("in visitBlock");
106        }
107    });
108
109
110parse("break.js", "while(true) { break; }",
111    new (Java.extend(SimpleTreeVisitor))() {
112        visitBreak: function() {
113            print("in visitBreak");
114        }
115    });
116
117function compAssignExpr(name, code) {
118    parse(name, code,
119        new (Java.extend(SimpleTreeVisitor))() {
120            visitCompoundAssignment: function(bn) {
121                print("in visitCompoundAssignment " + bn.kind + " " +
122                  bn.variable.name + " " + bn.expression.value);
123            }
124        });
125}
126
127compAssignExpr("mult_assign.js", "x *= 3");
128compAssignExpr("div_assign.js", "x /= 3");
129compAssignExpr("rem_assign.js", "x %= 3");
130compAssignExpr("add_assign.js", "x += 3");
131compAssignExpr("sub_assign.js", "x -= 3");
132compAssignExpr("lshift_assign.js", "x <<= 3");
133compAssignExpr("rshift_assign.js", "x >>= 3");
134compAssignExpr("urshift_assign.js", "x >>>= 3");
135compAssignExpr("and_assign.js", "x &= 3");
136compAssignExpr("xor_assign.js", "x ^= 3");
137compAssignExpr("or_assign.js", "x |= 3");
138
139parse("condexpr.js", "foo? x : y",
140    new (Java.extend(SimpleTreeVisitor))() {
141        visitConditionalExpression: function() {
142            print("in visitConditionalExpression");
143        }
144    });
145
146parse("continue.js", "while(true) { continue; }",
147    new (Java.extend(SimpleTreeVisitor))() {
148        visitContinue: function() {
149            print("in visitContinue");
150        }
151    });
152
153parse("debugger.js", "debugger;",
154    new (Java.extend(SimpleTreeVisitor))() {
155        visitDebugger: function() {
156            print("in visitDebugger");
157        }
158    });
159
160parse("dowhile.js", "do {} while(true)",
161    new (Java.extend(SimpleTreeVisitor))() {
162        visitDoWhileLoop: function() {
163            print("in visitDoWhileLoop");
164        }
165    });
166
167parse("empty.js", ";",
168    new (Java.extend(SimpleTreeVisitor))() {
169        visitEmptyStatement: function() {
170            print("in visitEmptyStatement");
171        }
172    });
173
174parse("exprstat.js", "2+3;",
175    new (Java.extend(SimpleTreeVisitor))() {
176        visitExpressionStatement: function() {
177            print("in visitExpressionStatement");
178        }
179    });
180
181parse("forin.js", "for(i in this) {}",
182    new (Java.extend(SimpleTreeVisitor))() {
183        visitForInLoop: function() {
184            print("in visitForInLoop");
185        }
186    });
187
188parse("for.js", "for(;;) {}",
189    new (Java.extend(SimpleTreeVisitor))() {
190        visitForLoop: function() {
191            print("in visitForLoop");
192        }
193    });
194
195parse("funccall.js", "func()",
196    new (Java.extend(SimpleTreeVisitor))() {
197        visitFunctionCall: function(fc) {
198            print("in visitFunctionCall " + fc.functionSelect.name);
199        }
200    });
201
202parse("funcdecl.js", "function func() {}",
203    new (Java.extend(SimpleTreeVisitor))() {
204        visitFunctionDeclaration: function(fd) {
205            print("in visitFunctionDeclaration " + fd.name);
206        }
207    });
208
209parse("funcexpr.js", "x = function() {}",
210    new (Java.extend(SimpleTreeVisitor))() {
211        visitFunctionExpression: function() {
212            print("in visitFunctionExpression");
213        }
214    });
215
216parse("ident.js", "this",
217    new (Java.extend(SimpleTreeVisitor))() {
218        visitIdentifier: function(ident) {
219            print("in visitIdentifier " + ident.name);
220        }
221    });
222
223parse("if.js", "if (true) {}",
224    new (Java.extend(SimpleTreeVisitor))() {
225        visitIf: function() {
226            print("in visitIf");
227        }
228    });
229
230parse("if2.js", "if (true) print('yes')",
231    new (visitor = Java.extend(SimpleTreeVisitor))() {
232        visitBlock: function(node, extra) {
233            print("ERROR: No block expected here!");
234            Error.dumpStack();
235        }
236    });
237
238parse("instanceof.js", "this instanceof Object",
239    new (Java.extend(SimpleTreeVisitor))() {
240        visitInstanceOf: function() {
241            print("in visitInstanceOf");
242        }
243    });
244
245parse("labeled.js", "foo: print('hello');",
246    new (Java.extend(SimpleTreeVisitor))() {
247        visitLabeledStatement: function() {
248            print("in visitLabeledStatement");
249        }
250    });
251
252function literalExpr(name, code) {
253    parse(name, code,
254        new (Java.extend(SimpleTreeVisitor))() {
255            visitLiteral: function(ln) {
256                print("in visitLiteral " + ln.kind + " " + ln.value);
257            }
258        });
259}
260
261literalExpr("bool.js", "true");
262literalExpr("num.js", "3.14");
263literalExpr("str.js", "'hello'");
264literalExpr("null.js", "null");
265
266parse("memselect.js", "this.foo",
267    new (Java.extend(SimpleTreeVisitor))() {
268        visitMemberSelect: function(ms) {
269            print("in visitMemberSelect " + ms.identifier);
270        }
271    });
272
273parse("new.js", "new Object()",
274    new (Java.extend(SimpleTreeVisitor))() {
275        visitNew: function() {
276            print("in visitNew");
277        }
278    });
279
280parse("obj_literal.js", "({ foo: 343 })",
281    visitor = new (Java.extend(SimpleTreeVisitor))() {
282        visitObjectLiteral: function(ol) {
283            print("in visitObjectLiteral");
284            Java.super(visitor).visitObjectLiteral(ol, null);
285        },
286
287        visitProperty: function(pn) {
288            print("in visitProperty " + pn.key.name);
289        }
290    });
291
292parse("regexp.js", "/[a-b]/i",
293    new (Java.extend(SimpleTreeVisitor))() {
294        visitRegExpLiteral: function(re) {
295            print("in visitRegExpLiteral " + re.pattern + " " + re.options);
296        }
297    });
298
299parse("ret.js", "function func() { return 33 }",
300    new (Java.extend(SimpleTreeVisitor))() {
301        visitReturn: function(ret) {
302            print("in visitReturn " + ret.expression.value);
303        }
304    });
305
306parse("switch.js", "switch(c) { case '1': break; default: }",
307    visitor = new (Java.extend(SimpleTreeVisitor))() {
308        visitSwitch: function(sn) {
309            print("in visitSwitch");
310            Java.super(visitor).visitSwitch(sn, null);
311        },
312
313        visitCase: function(cn) {
314            if (cn.expression) {
315                print("in visitCase");
316            } else {
317                print("in visitCase (default)");
318            }
319        }
320    });
321
322parse("throw.js", "throw 2",
323    new (Java.extend(SimpleTreeVisitor))() {
324        visitThrow: function(tn) {
325            print("in visitThrow " + tn.expression.value);
326        }
327    });
328
329parse("try.js", "try { func() } catch(e) {}",
330    visitor = new (Java.extend(SimpleTreeVisitor))() {
331        visitTry: function(tn) {
332            print("in visitTry");
333            Java.super(visitor).visitTry(tn, null);
334        },
335        visitCatch: function(cn) {
336            print("in visitCatch " + cn.parameter.name);
337        }
338    });
339
340function unaryExpr(name, code) {
341    parse(name, code,
342        new (Java.extend(SimpleTreeVisitor))() {
343            visitUnary: function(un) {
344                print("in visitUnary " + un.kind + " " + un.expression.name);
345            }
346        });
347}
348
349unaryExpr("postincr.js", "x++");
350unaryExpr("postdecr.js", "x--");
351unaryExpr("preincr.js", "++x");
352unaryExpr("predecr.js", "--x");
353unaryExpr("plus.js", "+x");
354unaryExpr("minus.js", "-x");
355unaryExpr("complement.js", "~x");
356unaryExpr("logical_compl.js", "!x");
357unaryExpr("delete.js", "delete x");
358unaryExpr("typeof.js", "typeof x");
359unaryExpr("void.js", "void x");
360
361parse("var.js", "var x = 34;",
362    new (Java.extend(SimpleTreeVisitor))() {
363        visitVariable: function(vn) {
364            print("in visitVariable " + vn.name + " = " + vn.initializer.value);
365        }
366    });
367
368parse("while.js", "while(true) {}",
369    new (Java.extend(SimpleTreeVisitor))() {
370        visitWhileLoop: function() {
371            print("in visitWhileLoop");
372        }
373    });
374
375parse("with.js", "with({}) {}",
376    new (Java.extend(SimpleTreeVisitor))() {
377        visitWith: function() {
378            print("in visitWith");
379        }
380    });
381