Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/samples/astviewer.js
32281 views
#// Usage: jjs -scripting -fx astviewer.js -- <scriptfile>12/*3* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* - Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* - Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* - Neither the name of Oracle nor the names of its17* contributors may be used to endorse or promote products derived18* from this software without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS21* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,22* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR24* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,26* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR27* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF28* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING29* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS30* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233if (!$OPTIONS._fx) {34print("Usage: jjs -scripting -fx astviewer.js -- <.js file>");35exit(1);36}3738// Using JavaFX from Nashorn. See also:39// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html4041// This example shows AST of a script file as a JavaFX42// tree view in a window. If no file is specified, AST of43// this script file is shown. This script demonstrates44// 'load' function, JavaFX support by -fx, readFully function45// in scripting mode.4647// JavaFX classes used48var StackPane = Java.type("javafx.scene.layout.StackPane");49var Scene = Java.type("javafx.scene.Scene");50var TreeItem = Java.type("javafx.scene.control.TreeItem");51var TreeView = Java.type("javafx.scene.control.TreeView");5253// Create a javafx TreeItem to view a AST node54function treeItemForASTNode(ast, name) {55var item = new TreeItem(name);56for (var prop in ast) {57var node = ast[prop];58if (typeof node == 'object') {59if (node == null) {60// skip nulls61continue;62}6364if (Array.isArray(node) && node.length == 0) {65// skip empty arrays66continue;67}6869var subitem = treeItemForASTNode(node, prop);70} else {71var subitem = new TreeItem(prop + ": " + node);72}73item.children.add(subitem);74}75return item;76}7778// do we have a script file passed? if not, use current script79var sourceName = arguments.length == 0? __FILE__ : arguments[0];8081// load parser.js from nashorn resources82load("nashorn:parser.js");8384// read the full content of the file and parse it85// to get AST of the script specified86var ast = parse(readFully(sourceName));8788// JavaFX start method89function start(stage) {90stage.title = "AST Viewer";91var rootItem = treeItemForASTNode(ast, sourceName);92var tree = new TreeView(rootItem);93var root = new StackPane();94root.children.add(tree);95stage.scene = new Scene(root, 300, 450);96stage.show();97}9899100