author | rrich |
Wed, 16 Oct 2019 17:03:40 +0200 | |
changeset 58662 | 5b7a967da646 |
parent 47216 | 71c04702a3d5 |
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 usage. |
|
26 |
* |
|
27 |
* @test |
|
28 |
* @option -scripting |
|
29 |
* @run |
|
30 |
*/ |
|
31 |
||
32 |
function Parser() { |
|
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
33 |
// create nashorn parser |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
34 |
this._parser = Parser.create(); |
29407 | 35 |
} |
36 |
||
37 |
// Java types used |
|
38 |
Parser.Diagnostic = Java.type("jdk.nashorn.api.tree.Diagnostic"); |
|
39 |
Parser.SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1"); |
|
40 |
Parser.Tree = Java.type("jdk.nashorn.api.tree.Tree"); |
|
41 |
Parser.List = Java.type("java.util.List"); |
|
42 |
Parser.Enum = Java.type("java.lang.Enum"); |
|
43 |
||
44 |
// function to parse the script and return script friendly object |
|
45 |
Parser.prototype.parse = function(name, script, listener) { |
|
46 |
var tree = this._parser.parse(name, script, listener); |
|
47 |
tree.accept(new Parser.SimpleTreeVisitor(), null); |
|
48 |
return this.convert(tree); |
|
49 |
} |
|
50 |
||
51 |
Parser.create = function() { |
|
52 |
return Java.type("jdk.nashorn.api.tree.Parser").create(); |
|
53 |
} |
|
54 |
||
55 |
// convert Nashorn parser Tree, Diagnostic as a script friendly object |
|
56 |
Parser.prototype.convert = function(tree) { |
|
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
57 |
if (!tree || typeof tree != 'object' || tree instanceof java.lang.Long) { |
29407 | 58 |
return tree; |
59 |
} |
|
60 |
||
61 |
var obj = Object.bindProperties({}, tree); |
|
62 |
var result = {}; |
|
63 |
for (var i in obj) { |
|
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
64 |
var val = obj[i]; |
39662
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
65 |
// skip these ES6 specific properties to reduce noise |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
66 |
// in the output - unless there were set to true |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
67 |
if (typeof(val) == 'boolean' && val == false) { |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
68 |
switch (i) { |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
69 |
case "computed": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
70 |
case "static": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
71 |
case "restParameter": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
72 |
case "this": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
73 |
case "super": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
74 |
case "star": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
75 |
case "default": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
76 |
case "starDefaultStar": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
77 |
case "arrow": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
78 |
case "generator": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
79 |
case "let": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
80 |
case "const": |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
81 |
continue; |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
82 |
} |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
83 |
} |
e2b36a3779b9
8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents:
34974
diff
changeset
|
84 |
|
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
85 |
if (val instanceof Parser.Tree) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
86 |
result[i] = this.convert(val); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
87 |
} else if (val instanceof Parser.List) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
88 |
var arr = new Array(val.size()); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
89 |
for (var j in val) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
90 |
arr[j] = this.convert(val[j]); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
91 |
} |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
92 |
|
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
93 |
result[i] = arr; |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
94 |
} else { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
95 |
switch (typeof val) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
96 |
case 'number': |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
97 |
case 'string': |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
98 |
case 'boolean': |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
99 |
result[i] = String(val); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
100 |
break; |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
101 |
default: |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
102 |
if (val instanceof java.lang.Long || val instanceof Parser.Enum) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
103 |
result[i] = String(val); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
104 |
} |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
105 |
} |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
106 |
} |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
107 |
} |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
108 |
return result; |
29407 | 109 |
} |
110 |
||
111 |
function processFiles(subdir) { |
|
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
112 |
var File = Java.type("java.io.File"); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
113 |
var files = new File(__DIR__ + subdir).listFiles(); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
114 |
java.util.Arrays.sort(files); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
115 |
for each (var file in files) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
116 |
if (file.name.endsWith(".js")) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
117 |
var script = readFully(file); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
118 |
var parser = new Parser(); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
119 |
var tree = parser.parse(subdir + "/" + file.name, script, |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
120 |
function(diagnostic) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
121 |
print(JSON.stringify(parser.convert(diagnostic), null, 2).replace(/\\r/g, '')); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
122 |
print(","); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
123 |
}); |
29407 | 124 |
|
34974
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
125 |
if (tree != null) { |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
126 |
print(JSON.stringify(tree, null, 2)); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
127 |
print(","); |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
128 |
} |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
129 |
} |
94a13629c390
8143896: java.lang.Long is implicitly converted to double
hannesw
parents:
29626
diff
changeset
|
130 |
} |
29407 | 131 |
} |
132 |
||
133 |
// parse files in parsertests directory |
|
134 |
function main() { |
|
135 |
print("["); |
|
136 |
||
137 |
processFiles("parsertests"); |
|
138 |
processFiles("parsernegativetests"); |
|
139 |
||
140 |
// parse this file first! |
|
141 |
var script = readFully(__FILE__); |
|
142 |
var tree = new Parser().parse("parserapi.js", script, null); |
|
143 |
print(JSON.stringify(tree, null, 2)); |
|
144 |
print("]"); |
|
145 |
} |
|
146 |
||
147 |
main(); |