aoqi@0: #// Usage: jjs -scripting -fx astviewer.js -- aoqi@0: aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * aoqi@0: * Redistribution and use in source and binary forms, with or without aoqi@0: * modification, are permitted provided that the following conditions aoqi@0: * are met: aoqi@0: * aoqi@0: * - Redistributions of source code must retain the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer. aoqi@0: * aoqi@0: * - Redistributions in binary form must reproduce the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer in the aoqi@0: * documentation and/or other materials provided with the distribution. aoqi@0: * aoqi@0: * - Neither the name of Oracle nor the names of its aoqi@0: * contributors may be used to endorse or promote products derived aoqi@0: * from this software without specific prior written permission. aoqi@0: * aoqi@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS aoqi@0: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, aoqi@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR aoqi@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR aoqi@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, aoqi@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, aoqi@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR aoqi@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF aoqi@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING aoqi@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS aoqi@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aoqi@0: */ aoqi@0: aoqi@0: if (!$OPTIONS._fx) { aoqi@0: print("Usage: jjs -scripting -fx astviewer.js -- <.js file>"); aoqi@0: exit(1); aoqi@0: } aoqi@0: aoqi@0: // Using JavaFX from Nashorn. See also: aoqi@0: // http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html aoqi@0: aoqi@0: // This example shows AST of a script file as a JavaFX aoqi@0: // tree view in a window. If no file is specified, AST of aoqi@0: // this script file is shown. This script demonstrates aoqi@0: // 'load' function, JavaFX support by -fx, readFully function aoqi@0: // in scripting mode. aoqi@0: aoqi@0: // JavaFX classes used aoqi@0: var StackPane = Java.type("javafx.scene.layout.StackPane"); aoqi@0: var Scene = Java.type("javafx.scene.Scene"); aoqi@0: var TreeItem = Java.type("javafx.scene.control.TreeItem"); aoqi@0: var TreeView = Java.type("javafx.scene.control.TreeView"); aoqi@0: aoqi@0: // Create a javafx TreeItem to view a AST node aoqi@0: function treeItemForASTNode(ast, name) { aoqi@0: var item = new TreeItem(name); aoqi@0: for (var prop in ast) { aoqi@0: var node = ast[prop]; aoqi@0: if (typeof node == 'object') { aoqi@0: if (node == null) { aoqi@0: // skip nulls aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: if (Array.isArray(node) && node.length == 0) { aoqi@0: // skip empty arrays aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: var subitem = treeItemForASTNode(node, prop); aoqi@0: } else { aoqi@0: var subitem = new TreeItem(prop + ": " + node); aoqi@0: } aoqi@0: item.children.add(subitem); aoqi@0: } aoqi@0: return item; aoqi@0: } aoqi@0: aoqi@0: // do we have a script file passed? if not, use current script aoqi@0: var sourceName = arguments.length == 0? __FILE__ : arguments[0]; aoqi@0: aoqi@0: // load parser.js from nashorn resources aoqi@0: load("nashorn:parser.js"); aoqi@0: aoqi@0: // read the full content of the file and parse it aoqi@0: // to get AST of the script specified aoqi@0: var ast = parse(readFully(sourceName)); aoqi@0: aoqi@0: // JavaFX start method aoqi@0: function start(stage) { aoqi@0: stage.title = "AST Viewer"; aoqi@0: var rootItem = treeItemForASTNode(ast, sourceName); aoqi@0: var tree = new TreeView(rootItem); aoqi@0: var root = new StackPane(); aoqi@0: root.children.add(tree); aoqi@0: stage.scene = new Scene(root, 300, 450); aoqi@0: stage.show(); aoqi@0: }