author | dholmes |
Wed, 24 Aug 2016 19:54:03 -0400 | |
changeset 40669 | 252f9d8272af |
parent 39662 | e2b36a3779b9 |
permissions | -rw-r--r-- |
29407 | 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 |
|
33 |
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1"); |
|
34 |
var Parser = Java.type("jdk.nashorn.api.tree.Parser"); |
|
35 |
||
36 |
function 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 |
||
42 |
parse("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 |
||
50 |
parse("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 |
||
60 |
parse("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 |
||
68 |
function 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 |
||
78 |
binaryExpr("add.js", "3 + 4"); |
|
79 |
binaryExpr("sub.js", "3 - 4"); |
|
80 |
binaryExpr("mul.js", "3 * 4"); |
|
81 |
binaryExpr("div.js", "3 / 4"); |
|
82 |
binaryExpr("rem.js", "3 % 4"); |
|
83 |
binaryExpr("rshift.js", "3 >> 4"); |
|
84 |
binaryExpr("rshift.js", "3 >>> 4"); |
|
85 |
binaryExpr("lshift.js", "3 << 4"); |
|
86 |
binaryExpr("less.js", "3 < 4"); |
|
87 |
binaryExpr("lessOrEq.js", "3 <= 4"); |
|
88 |
binaryExpr("greater.js", "3 > 4"); |
|
89 |
binaryExpr("greaterOrEq.js", "3 >= 4"); |
|
90 |
binaryExpr("in.js", "3 in this"); |
|
91 |
binaryExpr("eq.js", "3 == 3"); |
|
92 |
binaryExpr("ne.js", "3 != 2"); |
|
93 |
binaryExpr("seq.js", "3 === 2"); |
|
94 |
binaryExpr("sne.js", "3 !== 2"); |
|
95 |
binaryExpr("and.js", "3 & 2"); |
|
96 |
binaryExpr("or.js", "3 | 2"); |
|
97 |
binaryExpr("xor.js", "3 ^ 2"); |
|
98 |
binaryExpr("cond_and.js", "3 && 2"); |
|
99 |
binaryExpr("cond_or.js", "3 || 2"); |
|
100 |
binaryExpr("comma", "3, 2"); |
|
101 |
||
102 |
parse("block.js", "{ print('hello'); }", |
|
103 |
new (Java.extend(SimpleTreeVisitor))() { |
|
104 |
visitBlock: function() { |
|
105 |
print("in visitBlock"); |
|
106 |
} |
|
107 |
}); |
|
108 |
||
109 |
||
110 |
parse("break.js", "while(true) { break; }", |
|
111 |
new (Java.extend(SimpleTreeVisitor))() { |
|
112 |
visitBreak: function() { |
|
113 |
print("in visitBreak"); |
|
114 |
} |
|
115 |
}); |
|
116 |
||
117 |
function 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 |
||
127 |
compAssignExpr("mult_assign.js", "x *= 3"); |
|
128 |
compAssignExpr("div_assign.js", "x /= 3"); |
|
129 |
compAssignExpr("rem_assign.js", "x %= 3"); |
|
130 |
compAssignExpr("add_assign.js", "x += 3"); |
|
131 |
compAssignExpr("sub_assign.js", "x -= 3"); |
|
132 |
compAssignExpr("lshift_assign.js", "x <<= 3"); |
|
133 |
compAssignExpr("rshift_assign.js", "x >>= 3"); |
|
134 |
compAssignExpr("urshift_assign.js", "x >>>= 3"); |
|
135 |
compAssignExpr("and_assign.js", "x &= 3"); |
|
136 |
compAssignExpr("xor_assign.js", "x ^= 3"); |
|
137 |
compAssignExpr("or_assign.js", "x |= 3"); |
|
138 |
||
139 |
parse("condexpr.js", "foo? x : y", |
|
140 |
new (Java.extend(SimpleTreeVisitor))() { |
|
141 |
visitConditionalExpression: function() { |
|
142 |
print("in visitConditionalExpression"); |
|
143 |
} |
|
144 |
}); |
|
145 |
||
146 |
parse("continue.js", "while(true) { continue; }", |
|
147 |
new (Java.extend(SimpleTreeVisitor))() { |
|
148 |
visitContinue: function() { |
|
149 |
print("in visitContinue"); |
|
150 |
} |
|
151 |
}); |
|
152 |
||
153 |
parse("debugger.js", "debugger;", |
|
154 |
new (Java.extend(SimpleTreeVisitor))() { |
|
155 |
visitDebugger: function() { |
|
156 |
print("in visitDebugger"); |
|
157 |
} |
|
158 |
}); |
|
159 |
||
160 |
parse("dowhile.js", "do {} while(true)", |
|
161 |
new (Java.extend(SimpleTreeVisitor))() { |
|
162 |
visitDoWhileLoop: function() { |
|
163 |
print("in visitDoWhileLoop"); |
|
164 |
} |
|
165 |
}); |
|
166 |
||
167 |
parse("empty.js", ";", |
|
168 |
new (Java.extend(SimpleTreeVisitor))() { |
|
169 |
visitEmptyStatement: function() { |
|
170 |
print("in visitEmptyStatement"); |
|
171 |
} |
|
172 |
}); |
|
173 |
||
174 |
parse("exprstat.js", "2+3;", |
|
175 |
new (Java.extend(SimpleTreeVisitor))() { |
|
176 |
visitExpressionStatement: function() { |
|
177 |
print("in visitExpressionStatement"); |
|
178 |
} |
|
179 |
}); |
|
180 |
||
181 |
parse("forin.js", "for(i in this) {}", |
|
182 |
new (Java.extend(SimpleTreeVisitor))() { |
|
183 |
visitForInLoop: function() { |
|
184 |
print("in visitForInLoop"); |
|
185 |
} |
|
186 |
}); |
|
187 |
||
188 |
parse("for.js", "for(;;) {}", |
|
189 |
new (Java.extend(SimpleTreeVisitor))() { |
|
190 |
visitForLoop: function() { |
|
191 |
print("in visitForLoop"); |
|
192 |
} |
|
193 |
}); |
|
194 |
||
195 |
parse("funccall.js", "func()", |
|
196 |
new (Java.extend(SimpleTreeVisitor))() { |
|
197 |
visitFunctionCall: function(fc) { |
|
198 |
print("in visitFunctionCall " + fc.functionSelect.name); |
|
199 |
} |
|
200 |
}); |
|
201 |
||
202 |
parse("funcdecl.js", "function func() {}", |
|
203 |
new (Java.extend(SimpleTreeVisitor))() { |
|
204 |
visitFunctionDeclaration: function(fd) { |
|
39662
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
29407
diff
changeset
|
205 |
print("in visitFunctionDeclaration " + fd.name.name); |
29407 | 206 |
} |
207 |
}); |
|
208 |
||
209 |
parse("funcexpr.js", "x = function() {}", |
|
210 |
new (Java.extend(SimpleTreeVisitor))() { |
|
211 |
visitFunctionExpression: function() { |
|
212 |
print("in visitFunctionExpression"); |
|
213 |
} |
|
214 |
}); |
|
215 |
||
216 |
parse("ident.js", "this", |
|
217 |
new (Java.extend(SimpleTreeVisitor))() { |
|
218 |
visitIdentifier: function(ident) { |
|
219 |
print("in visitIdentifier " + ident.name); |
|
220 |
} |
|
221 |
}); |
|
222 |
||
223 |
parse("if.js", "if (true) {}", |
|
224 |
new (Java.extend(SimpleTreeVisitor))() { |
|
225 |
visitIf: function() { |
|
226 |
print("in visitIf"); |
|
227 |
} |
|
228 |
}); |
|
229 |
||
230 |
parse("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 |
||
238 |
parse("instanceof.js", "this instanceof Object", |
|
239 |
new (Java.extend(SimpleTreeVisitor))() { |
|
240 |
visitInstanceOf: function() { |
|
241 |
print("in visitInstanceOf"); |
|
242 |
} |
|
243 |
}); |
|
244 |
||
245 |
parse("labeled.js", "foo: print('hello');", |
|
246 |
new (Java.extend(SimpleTreeVisitor))() { |
|
247 |
visitLabeledStatement: function() { |
|
248 |
print("in visitLabeledStatement"); |
|
249 |
} |
|
250 |
}); |
|
251 |
||
252 |
function 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 |
||
261 |
literalExpr("bool.js", "true"); |
|
262 |
literalExpr("num.js", "3.14"); |
|
263 |
literalExpr("str.js", "'hello'"); |
|
264 |
literalExpr("null.js", "null"); |
|
265 |
||
266 |
parse("memselect.js", "this.foo", |
|
267 |
new (Java.extend(SimpleTreeVisitor))() { |
|
268 |
visitMemberSelect: function(ms) { |
|
269 |
print("in visitMemberSelect " + ms.identifier); |
|
270 |
} |
|
271 |
}); |
|
272 |
||
273 |
parse("new.js", "new Object()", |
|
274 |
new (Java.extend(SimpleTreeVisitor))() { |
|
275 |
visitNew: function() { |
|
276 |
print("in visitNew"); |
|
277 |
} |
|
278 |
}); |
|
279 |
||
280 |
parse("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 |
||
292 |
parse("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 |
||
299 |
parse("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 |
||
306 |
parse("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 |
||
322 |
parse("throw.js", "throw 2", |
|
323 |
new (Java.extend(SimpleTreeVisitor))() { |
|
324 |
visitThrow: function(tn) { |
|
325 |
print("in visitThrow " + tn.expression.value); |
|
326 |
} |
|
327 |
}); |
|
328 |
||
329 |
parse("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 |
||
340 |
function 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 |
||
349 |
unaryExpr("postincr.js", "x++"); |
|
350 |
unaryExpr("postdecr.js", "x--"); |
|
351 |
unaryExpr("preincr.js", "++x"); |
|
352 |
unaryExpr("predecr.js", "--x"); |
|
353 |
unaryExpr("plus.js", "+x"); |
|
354 |
unaryExpr("minus.js", "-x"); |
|
355 |
unaryExpr("complement.js", "~x"); |
|
356 |
unaryExpr("logical_compl.js", "!x"); |
|
357 |
unaryExpr("delete.js", "delete x"); |
|
358 |
unaryExpr("typeof.js", "typeof x"); |
|
359 |
unaryExpr("void.js", "void x"); |
|
360 |
||
361 |
parse("var.js", "var x = 34;", |
|
362 |
new (Java.extend(SimpleTreeVisitor))() { |
|
363 |
visitVariable: function(vn) { |
|
39662
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
29407
diff
changeset
|
364 |
print("in visitVariable " + vn.binding.name + " = " + vn.initializer.value); |
29407 | 365 |
} |
366 |
}); |
|
367 |
||
368 |
parse("while.js", "while(true) {}", |
|
369 |
new (Java.extend(SimpleTreeVisitor))() { |
|
370 |
visitWhileLoop: function() { |
|
371 |
print("in visitWhileLoop"); |
|
372 |
} |
|
373 |
}); |
|
374 |
||
375 |
parse("with.js", "with({}) {}", |
|
376 |
new (Java.extend(SimpleTreeVisitor))() { |
|
377 |
visitWith: function() { |
|
378 |
print("in visitWith"); |
|
379 |
} |
|
380 |
}); |