24283
|
1 |
#// Usage: jjs -scripting -fx astviewer.js -- <scriptfile>
|
|
2 |
|
|
3 |
/*
|
|
4 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
|
5 |
*
|
|
6 |
* Redistribution and use in source and binary forms, with or without
|
|
7 |
* modification, are permitted provided that the following conditions
|
|
8 |
* are met:
|
|
9 |
*
|
|
10 |
* - Redistributions of source code must retain the above copyright
|
|
11 |
* notice, this list of conditions and the following disclaimer.
|
|
12 |
*
|
|
13 |
* - Redistributions in binary form must reproduce the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer in the
|
|
15 |
* documentation and/or other materials provided with the distribution.
|
|
16 |
*
|
|
17 |
* - Neither the name of Oracle nor the names of its
|
|
18 |
* contributors may be used to endorse or promote products derived
|
|
19 |
* from this software without specific prior written permission.
|
|
20 |
*
|
|
21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
22 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
23 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
26 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*/
|
|
33 |
|
|
34 |
if (!$OPTIONS._fx) {
|
|
35 |
print("Usage: jjs -scripting -fx astviewer.js -- <.js file>");
|
|
36 |
exit(1);
|
|
37 |
}
|
|
38 |
|
|
39 |
// Using JavaFX from Nashorn. See also:
|
|
40 |
// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
|
|
41 |
|
|
42 |
// This example shows AST of a script file as a JavaFX
|
|
43 |
// tree view in a window. If no file is specified, AST of
|
|
44 |
// this script file is shown. This script demonstrates
|
|
45 |
// 'load' function, JavaFX support by -fx, readFully function
|
|
46 |
// in scripting mode.
|
|
47 |
|
|
48 |
// JavaFX classes used
|
|
49 |
var StackPane = Java.type("javafx.scene.layout.StackPane");
|
|
50 |
var Scene = Java.type("javafx.scene.Scene");
|
|
51 |
var TreeItem = Java.type("javafx.scene.control.TreeItem");
|
|
52 |
var TreeView = Java.type("javafx.scene.control.TreeView");
|
|
53 |
|
|
54 |
// Create a javafx TreeItem to view a AST node
|
|
55 |
function treeItemForASTNode(ast, name) {
|
|
56 |
var item = new TreeItem(name);
|
|
57 |
for (var prop in ast) {
|
|
58 |
var node = ast[prop];
|
|
59 |
if (typeof node == 'object') {
|
|
60 |
if (node == null) {
|
|
61 |
// skip nulls
|
|
62 |
continue;
|
|
63 |
}
|
|
64 |
|
|
65 |
if (Array.isArray(node) && node.length == 0) {
|
|
66 |
// skip empty arrays
|
|
67 |
continue;
|
|
68 |
}
|
|
69 |
|
|
70 |
var subitem = treeItemForASTNode(node, prop);
|
|
71 |
} else {
|
|
72 |
var subitem = new TreeItem(prop + ": " + node);
|
|
73 |
}
|
|
74 |
item.children.add(subitem);
|
|
75 |
}
|
|
76 |
return item;
|
|
77 |
}
|
|
78 |
|
|
79 |
// do we have a script file passed? if not, use current script
|
|
80 |
var sourceName = arguments.length == 0? __FILE__ : arguments[0];
|
|
81 |
|
|
82 |
// load parser.js from nashorn resources
|
|
83 |
load("nashorn:parser.js");
|
|
84 |
|
|
85 |
// read the full content of the file and parse it
|
|
86 |
// to get AST of the script specified
|
|
87 |
var ast = parse(readFully(sourceName));
|
|
88 |
|
|
89 |
// JavaFX start method
|
|
90 |
function start(stage) {
|
|
91 |
stage.title = "AST Viewer";
|
|
92 |
var rootItem = treeItemForASTNode(ast, sourceName);
|
|
93 |
var tree = new TreeView(rootItem);
|
|
94 |
var root = new StackPane();
|
|
95 |
root.children.add(tree);
|
|
96 |
stage.scene = new Scene(root, 300, 450);
|
|
97 |
stage.show();
|
|
98 |
}
|