parserapi_nse.js revision 1204:9597425b6b38
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 -nse option test. 26 * 27 * @test 28 * @option -scripting 29 * @run 30 */ 31 32var Parser = Java.type("jdk.nashorn.api.tree.Parser"); 33var noExtParser = Parser.create("-nse"); 34var parser = Parser.create(); 35var scriptingParser = Parser.create("-scripting"); 36 37var condCatch = <<EOF 38 39try { 40 that(); 41} catch (e if e instanceof ReferenceError) {} 42 43EOF; 44 45noExtParser.parse("cond_catch.js", condCatch, print); 46parser.parse("cond_catch1.js", condCatch, print); 47 48var funcClosure = <<EOF 49 50function square(x) x*x; 51 52EOF; 53 54noExtParser.parse("func_closure.js", funcClosure, print); 55parser.parse("func_closure1.js", funcClosure, print); 56 57var forEach = <<EOF 58 59for each (arg in arguments) print(arg); 60 61EOF; 62 63noExtParser.parse("for_each.js", forEach, print); 64parser.parse("for_each1.js", forEach, print); 65 66var anonNew = <<EOF 67 68var r = new java.lang.Runnable() { 69 run: function() { print("hello") } 70}; 71 72EOF; 73 74noExtParser.parse("anon_new.js", anonNew, print); 75parser.parse("anon_new1.js", anonNew, print); 76 77var anonFuncStat = <<EOF 78 79function () { print("hello") } 80 81EOF; 82 83noExtParser.parse("anon_func_stat.js", anonFuncStat, print); 84parser.parse("anon_func_stat1.js", anonFuncStat, print); 85 86// These lexer (scripting) extensions should also be not parsed 87// by "no extensions parser" ( as well as "default" parser ) 88 89var backquote = "`ls`"; 90noExtParser.parse("backquote.js", backquote, print); 91parser.parse("backquote1.js", backquote, print); 92scriptingParser.parse("backquote2.js", backquote, print); 93 94var heredoc = "var str = <<EOF\nprint('hello')\nEOF\n"; 95noExtParser.parse("heredoc.js", heredoc, print); 96parser.parse("heredoc1.js", heredoc, print); 97scriptingParser.parse("heredoc2.js", heredoc, print); 98 99var hashComment = "#comment\nprint('hello')"; 100noExtParser.parse("hashcomment.js", hashComment, print); 101parser.parse("hashcomment1.js", hashComment, print); 102scriptingParser.parse("hashcomment2.js", hashComment, print); 103