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