parserapi.js revision 1593:8faab9cd4b95
1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * Nashorn parser API usage. 26 * 27 * @test 28 * @option -scripting 29 * @run 30 */ 31 32function Parser() { 33 // create nashorn parser 34 this._parser = Parser.create(); 35} 36 37// Java types used 38Parser.Diagnostic = Java.type("jdk.nashorn.api.tree.Diagnostic"); 39Parser.SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1"); 40Parser.Tree = Java.type("jdk.nashorn.api.tree.Tree"); 41Parser.List = Java.type("java.util.List"); 42Parser.Enum = Java.type("java.lang.Enum"); 43 44// function to parse the script and return script friendly object 45Parser.prototype.parse = function(name, script, listener) { 46 var tree = this._parser.parse(name, script, listener); 47 tree.accept(new Parser.SimpleTreeVisitor(), null); 48 return this.convert(tree); 49} 50 51Parser.create = function() { 52 return Java.type("jdk.nashorn.api.tree.Parser").create(); 53} 54 55// convert Nashorn parser Tree, Diagnostic as a script friendly object 56Parser.prototype.convert = function(tree) { 57 if (!tree || typeof tree != 'object' || tree instanceof java.lang.Long) { 58 return tree; 59 } 60 61 var obj = Object.bindProperties({}, tree); 62 var result = {}; 63 for (var i in obj) { 64 var val = obj[i]; 65 if (val instanceof Parser.Tree) { 66 result[i] = this.convert(val); 67 } else if (val instanceof Parser.List) { 68 var arr = new Array(val.size()); 69 for (var j in val) { 70 arr[j] = this.convert(val[j]); 71 } 72 73 result[i] = arr; 74 } else { 75 switch (typeof val) { 76 case 'number': 77 case 'string': 78 case 'boolean': 79 result[i] = String(val); 80 break; 81 default: 82 if (val instanceof java.lang.Long || val instanceof Parser.Enum) { 83 result[i] = String(val); 84 } 85 } 86 } 87 } 88 return result; 89} 90 91function processFiles(subdir) { 92 var File = Java.type("java.io.File"); 93 var files = new File(__DIR__ + subdir).listFiles(); 94 java.util.Arrays.sort(files); 95 for each (var file in files) { 96 if (file.name.endsWith(".js")) { 97 var script = readFully(file); 98 var parser = new Parser(); 99 var tree = parser.parse(subdir + "/" + file.name, script, 100 function(diagnostic) { 101 print(JSON.stringify(parser.convert(diagnostic), null, 2).replace(/\\r/g, '')); 102 print(","); 103 }); 104 105 if (tree != null) { 106 print(JSON.stringify(tree, null, 2)); 107 print(","); 108 } 109 } 110 } 111} 112 113// parse files in parsertests directory 114function main() { 115 print("["); 116 117 processFiles("parsertests"); 118 processFiles("parsernegativetests"); 119 120 // parse this file first! 121 var script = readFully(__FILE__); 122 var tree = new Parser().parse("parserapi.js", script, null); 123 print(JSON.stringify(tree, null, 2)); 124 print("]"); 125} 126 127main(); 128