author | sgehwolf |
Fri, 27 Mar 2015 16:12:40 +0100 | |
changeset 30126 | 89b8253271bd |
parent 29409 | 613987e37f79 |
permissions | -rw-r--r-- |
29409
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
1 |
/* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
3 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
4 |
* Redistribution and use in source and binary forms, with or without |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
5 |
* modification, are permitted provided that the following conditions |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
6 |
* are met: |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
7 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
8 |
* - Redistributions of source code must retain the above copyright |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
9 |
* notice, this list of conditions and the following disclaimer. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
10 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
12 |
* notice, this list of conditions and the following disclaimer in the |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
13 |
* documentation and/or other materials provided with the distribution. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
14 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
15 |
* - Neither the name of Oracle nor the names of its |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
16 |
* contributors may be used to endorse or promote products derived |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
17 |
* from this software without specific prior written permission. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
18 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
30 |
*/ |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
31 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
32 |
// Find break or continue inside try..finally in all scripts |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
33 |
// in a given directory (recursively). If no directory is specified |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
34 |
// the current working directory is scanned for scripts. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
35 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
36 |
if (arguments.length == 0) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
37 |
arguments[0] = "."; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
38 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
39 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
40 |
var File = Java.type("java.io.File"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
41 |
var file = new File(arguments[0]); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
42 |
if (!file.exists()) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
43 |
print(arguments[0] + " is neither a directory nor a file"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
44 |
exit(1); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
45 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
46 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
47 |
var Files = Java.type("java.nio.file.Files"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
48 |
var Parser = Java.type("jdk.nashorn.api.tree.Parser"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
49 |
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
50 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
51 |
var parser = Parser.create("-scripting", "--const-as-var"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
52 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
53 |
function checkFile(file) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
54 |
// print("checking " + file); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
55 |
var ast = parser.parse(file, print); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
56 |
if (!ast) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
57 |
return; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
58 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
59 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
60 |
// locate 'break/continue' inside try..finally |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
61 |
ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
62 |
lineMap: null, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
63 |
inTryFinally: false, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
64 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
65 |
printWarning: function(node, msg) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
66 |
var pos = node.startPosition; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
67 |
var line = this.lineMap.getLineNumber(pos); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
68 |
var column = this.lineMap.getColumnNumber(pos); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
69 |
print(msg + " found in " + file + " @ " + line + ":" + column); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
70 |
}, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
71 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
72 |
visitBreak: function(node, extra) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
73 |
if (this.inTryFinally) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
74 |
this.printWarning(node, "break"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
75 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
76 |
}, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
77 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
78 |
visitContinue: function(node, extra) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
79 |
if (this.inTryFinally) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
80 |
this.printWarning(node, "continue"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
81 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
82 |
}, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
83 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
84 |
visitCompilationUnit: function(node, extra) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
85 |
this.lineMap = node.lineMap; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
86 |
Java.super(visitor).visitCompilationUnit(node, extra); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
87 |
}, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
88 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
89 |
visitFunctionDeclaration: function(node, extra) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
90 |
var oldInTryFinally = this.inTryFinally; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
91 |
this.inTryFinally = false; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
92 |
try { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
93 |
Java.super(visitor).visitFunctionDeclaration(node, extra); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
94 |
} finally { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
95 |
this.inTryFinally = oldInTryFinally; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
96 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
97 |
}, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
98 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
99 |
visitFunctionExpression: function(node, extra) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
100 |
var oldInTryFinally = this.inTryFinally; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
101 |
this.inTryFinally = false; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
102 |
try { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
103 |
Java.super(visitor).visitFunctionExpression(node, extra); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
104 |
} finally { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
105 |
this.inTryFinally = oldInTryFinally; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
106 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
107 |
}, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
108 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
109 |
visitTry: function(node, extra) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
110 |
var oldInTryFinally = this.inTryFinally; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
111 |
this.inTryFinally = (node.finallyBlock != null); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
112 |
try { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
113 |
Java.super(visitor).visitTry(node, extra); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
114 |
} finally { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
115 |
this.inTryFinally = oldInTryFinally; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
116 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
117 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
118 |
}, null); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
119 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
120 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
121 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
122 |
if (file.isDirectory()) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
123 |
Files.walk(file.toPath()) |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
124 |
.filter(function(p) Files.isRegularFile(p)) |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
125 |
.filter(function(p) p.toFile().name.endsWith('.js')) |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
126 |
.forEach(checkFile); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
127 |
} else { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
128 |
checkFile(file); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
129 |
} |