parser.js revision 1224:065b159bb922
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 * @test 26 * @bug 8068303 27 * @option -scripting 28 * @run 29 */ 30 31load(__DIR__ + "/../assert.js") 32 33var Parser = Java.type('jdk.nashorn.api.tree.Parser') 34var Nashorn = Java.type('jdk.nashorn.api.scripting.NashornScriptEngineFactory') 35var File = java.io.File 36var Reader = java.io.FileReader 37 38 39var test_dir = __DIR__ + "/parsertests" 40var files = new File(test_dir).listFiles() 41 42// File source 43for (var i in files) { 44 var parser = Parser.create("-scripting", "--const-as-var", "-doe") 45 try { 46 var tree = parser.parse(files[i], null); 47 Assert.assertNotNull(tree); 48 } 49 catch (e) { 50 fail("Parser failed with message :" + e) 51 } 52} 53 54// Reader source 55for (var i in files) { 56 var parser = Parser.create("-scripting", "--const-as-var", "-doe") 57 try { 58 var tree = parser.parse(files[i].getName(), new Reader(files[i]), null) 59 Assert.assertNotNull(tree); 60 } catch (e) { 61 fail("Parser failed with message :" + e) 62 } 63 64} 65 66// URL source 67for (var i in files) { 68 var parser = Parser.create("-scripting", "--const-as-var", "-doe") 69 try { 70 var tree = parser.parse(files[i].toURI().toURL(), null) 71 Assert.assertNotNull(tree); 72 } catch (e) { 73 fail("Parser failed with message :" + e) 74 } 75} 76 77// ScriptObjectMirror 78 79for (var i in files) { 80 var parser = Parser.create("-scripting", "--const-as-var", "-doe") 81 var engine = new Nashorn().getScriptEngine("-scripting", "--const-as-var", "-doe") 82 try { 83 engine.compile(new Reader(files[i])) 84 var mirror = engine.createBindings() 85 mirror['name'] = mirror['script'] = files[i].getName() 86 var tree = parser.parse(mirror, null) 87 Assert.assertNotNull(tree); 88 } catch (e) { 89 fail("Parser failed with message :" + e) 90 } 91} 92 93