1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/astviewer.js Wed Apr 27 01:36:41 2016 +0800 1.3 @@ -0,0 +1,98 @@ 1.4 +#// Usage: jjs -scripting -fx astviewer.js -- <scriptfile> 1.5 + 1.6 +/* 1.7 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions 1.11 + * are met: 1.12 + * 1.13 + * - Redistributions of source code must retain the above copyright 1.14 + * notice, this list of conditions and the following disclaimer. 1.15 + * 1.16 + * - Redistributions in binary form must reproduce the above copyright 1.17 + * notice, this list of conditions and the following disclaimer in the 1.18 + * documentation and/or other materials provided with the distribution. 1.19 + * 1.20 + * - Neither the name of Oracle nor the names of its 1.21 + * contributors may be used to endorse or promote products derived 1.22 + * from this software without specific prior written permission. 1.23 + * 1.24 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.25 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.26 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.27 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.28 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.29 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.30 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.31 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.32 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.33 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.34 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.35 + */ 1.36 + 1.37 +if (!$OPTIONS._fx) { 1.38 + print("Usage: jjs -scripting -fx astviewer.js -- <.js file>"); 1.39 + exit(1); 1.40 +} 1.41 + 1.42 +// Using JavaFX from Nashorn. See also: 1.43 +// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html 1.44 + 1.45 +// This example shows AST of a script file as a JavaFX 1.46 +// tree view in a window. If no file is specified, AST of 1.47 +// this script file is shown. This script demonstrates 1.48 +// 'load' function, JavaFX support by -fx, readFully function 1.49 +// in scripting mode. 1.50 + 1.51 +// JavaFX classes used 1.52 +var StackPane = Java.type("javafx.scene.layout.StackPane"); 1.53 +var Scene = Java.type("javafx.scene.Scene"); 1.54 +var TreeItem = Java.type("javafx.scene.control.TreeItem"); 1.55 +var TreeView = Java.type("javafx.scene.control.TreeView"); 1.56 + 1.57 +// Create a javafx TreeItem to view a AST node 1.58 +function treeItemForASTNode(ast, name) { 1.59 + var item = new TreeItem(name); 1.60 + for (var prop in ast) { 1.61 + var node = ast[prop]; 1.62 + if (typeof node == 'object') { 1.63 + if (node == null) { 1.64 + // skip nulls 1.65 + continue; 1.66 + } 1.67 + 1.68 + if (Array.isArray(node) && node.length == 0) { 1.69 + // skip empty arrays 1.70 + continue; 1.71 + } 1.72 + 1.73 + var subitem = treeItemForASTNode(node, prop); 1.74 + } else { 1.75 + var subitem = new TreeItem(prop + ": " + node); 1.76 + } 1.77 + item.children.add(subitem); 1.78 + } 1.79 + return item; 1.80 +} 1.81 + 1.82 +// do we have a script file passed? if not, use current script 1.83 +var sourceName = arguments.length == 0? __FILE__ : arguments[0]; 1.84 + 1.85 +// load parser.js from nashorn resources 1.86 +load("nashorn:parser.js"); 1.87 + 1.88 +// read the full content of the file and parse it 1.89 +// to get AST of the script specified 1.90 +var ast = parse(readFully(sourceName)); 1.91 + 1.92 +// JavaFX start method 1.93 +function start(stage) { 1.94 + stage.title = "AST Viewer"; 1.95 + var rootItem = treeItemForASTNode(ast, sourceName); 1.96 + var tree = new TreeView(rootItem); 1.97 + var root = new StackPane(); 1.98 + root.children.add(tree); 1.99 + stage.scene = new Scene(root, 300, 450); 1.100 + stage.show(); 1.101 +}