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