This commit is contained in:
Lana Steuck 2015-03-12 21:14:04 -07:00
commit 74c0fd2529
203 changed files with 20134 additions and 502 deletions
nashorn
make
samples
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api
scripting
tree
ArrayAccessTree.javaArrayAccessTreeImpl.javaArrayLiteralTree.javaArrayLiteralTreeImpl.javaAssignmentTree.javaAssignmentTreeImpl.javaBinaryTree.javaBinaryTreeImpl.javaBlockTree.javaBlockTreeImpl.javaBreakTree.javaBreakTreeImpl.javaCaseTree.javaCaseTreeImpl.javaCatchTree.javaCatchTreeImpl.javaCompilationUnitTree.javaCompilationUnitTreeImpl.javaCompoundAssignmentTree.javaCompoundAssignmentTreeImpl.javaConditionalExpressionTree.javaConditionalExpressionTreeImpl.javaConditionalLoopTree.javaContinueTree.javaContinueTreeImpl.javaDebuggerTree.javaDebuggerTreeImpl.javaDiagnostic.javaDiagnosticImpl.javaDiagnosticListener.javaDoWhileLoopTree.javaDoWhileLoopTreeImpl.javaEmptyStatementTree.javaEmptyStatementTreeImpl.javaErroneousTree.javaErroneousTreeImpl.javaExpressionStatementTree.javaExpressionStatementTreeImpl.javaExpressionTree.javaExpressionTreeImpl.javaForInLoopTree.javaForInLoopTreeImpl.javaForLoopTree.javaForLoopTreeImpl.javaFunctionCallTree.javaFunctionCallTreeImpl.javaFunctionDeclarationTree.javaFunctionDeclarationTreeImpl.javaFunctionExpressionTree.javaFunctionExpressionTreeImpl.javaGotoTree.javaIRTranslator.javaIdentifierTree.javaIdentifierTreeImpl.javaIfTree.javaIfTreeImpl.javaInstanceOfTree.javaInstanceOfTreeImpl.javaLabeledStatementTree.javaLabeledStatementTreeImpl.javaLineMap.javaLineMapImpl.javaLiteralTree.javaLiteralTreeImpl.javaLoopTree.javaMemberSelectTree.javaMemberSelectTreeImpl.javaNewTree.javaNewTreeImpl.javaObjectLiteralTree.javaObjectLiteralTreeImpl.javaParenthesizedTree.javaParser.javaParserImpl.javaPropertyTree.javaPropertyTreeImpl.javaRegExpLiteralTree.javaRegExpLiteralTreeImpl.javaReturnTree.javaReturnTreeImpl.javaSimpleTreeVisitorES5_1.javaStatementTree.javaStatementTreeImpl.javaSwitchTree.java

@ -105,8 +105,8 @@ MANIFEST_ATTRIBUTES := Name: jdk/nashorn/\nImplementation-Title: Oracle Nashorn\
# Create nashorn.jar from the final classes dir
$(eval $(call SetupArchive,BUILD_NASHORN_JAR, \
$(NASGEN_RUN_FILE) \
$(VERSION_FILE), \
DEPENDENCIES := $(NASGEN_RUN_FILE) \
$(VERSION_FILE), \
SRCS := $(NASHORN_CLASSES_DIR), \
SUFFIXES := .class .js .properties Factory, \
MANIFEST := $(NASHORN_TOPDIR)/src/jdk.scripting.nashorn/share/classes/META-INF/MANIFEST.MF, \

@ -35,11 +35,10 @@
<classpath>
<pathelement location="${basedir}/jcov2/lib/jcov_j2se_rt.jar"/>
<pathelement location="${basedir}/buildtools/nasgen/dist/nasgen.jar"/>
<pathelement path="${basedir}/build/classes"/>
<pathelement location="${dist.dir}/nasgen.jar"/>
<pathelement path="${build.dir}/classes"/>
</classpath>
<jvmarg value="${boot.class.path}"/>
<jvmarg value="-Xbootclasspath/p:${basedir}/build/classes"/>
<arg value="${build.dir}/classes"/>
<arg value="jdk.nashorn.internal.objects"/>
<arg value="${build.dir}/classes"/>
@ -52,8 +51,8 @@
<java classname="jdk.nashorn.internal.tools.nasgen.Main" fork="true" failonerror="true">
<classpath>
<pathelement location="${basedir}/buildtools/nasgen/dist/nasgen.jar"/>
<pathelement path="${basedir}/build/eclipse"/>
</classpath>
<jvmarg value="-Xbootclasspath/p:${basedir}/build/eclipse"/>
<arg value="${basedir}/build/eclipse"/>
<arg value="jdk.nashorn.internal.objects"/>
<arg value="${basedir}/build/eclipse/.nasgentmp"/>

@ -98,7 +98,7 @@
<fail message="Unsupported Java version: ${ant.java.version}. Please use Java version 1.8 or greater." unless="jdk1.8+">
</fail>
</target>
<target name="init" depends="check-ant-version, check-java-version, init-conditions, init-cc">
<!-- extends jvm args -->
<property name="run.test.jvmargs" value="${run.test.jvmargs.main} ${run.test.cc.jvmargs} ${jfr.options}"/>
@ -209,7 +209,7 @@
</target>
<target name="javadoc" depends="jar">
<javadoc destdir="${dist.javadoc.dir}" use="yes" overview="${src.dir}/overview.html"
<javadoc destdir="${dist.javadoc.dir}" use="yes" overview="${src.dir}/overview.html"
extdirs="${nashorn.ext.path}" windowtitle="${nashorn.product.name} ${nashorn.version}"
additionalparam="-quiet" failonerror="true">
<classpath>
@ -227,7 +227,7 @@
<!-- generate javadoc only for nashorn extension api classes -->
<target name="javadocapi" depends="jar">
<javadoc destdir="${dist.javadoc.dir}" use="yes" extdirs="${nashorn.ext.path}"
<javadoc destdir="${dist.javadoc.dir}" use="yes" extdirs="${nashorn.ext.path}"
windowtitle="${nashorn.product.name}" additionalparam="-quiet" failonerror="true">
<classpath>
<pathelement location="${build.classes.dir}"/>
@ -314,6 +314,15 @@ grant codeBase "file:/${basedir}/${nashorn.internal.tests.jar}" {
permission java.security.AllPermission;
};
grant codeBase "file:/${basedir}/${nashorn.api.tests.jar}" {
permission java.util.PropertyPermission "parserapitest.*", "read";
permission java.util.PropertyPermission "test.*", "read";
permission java.util.PropertyPermission "test262.*", "read";
permission java.io.FilePermission "${basedir}/test/-","read";
permission java.io.FilePermission "$${user.dir}", "read";
permission java.util.PropertyPermission "user.dir", "read";
};
grant codeBase "file:/${basedir}/${file.reference.testng.jar}" {
permission java.security.AllPermission;
};
@ -395,6 +404,7 @@ grant codeBase "file:/${basedir}/test/script/markdown.js" {
<fileset id="test.classes" dir="${build.test.classes.dir}">
<include name="**/api/javaaccess/*Test.class"/>
<include name="**/api/scripting/*Test.class"/>
<include name="**/api/tree/*Test.class"/>
<include name="**/codegen/*Test.class"/>
<include name="**/parser/*Test.class"/>
<include name="**/runtime/*Test.class"/>
@ -440,7 +450,7 @@ grant codeBase "file:/${basedir}/test/script/markdown.js" {
<delete dir="${build.dir}/nashorn_code_cache"/>
<property name="debug.test.jvmargs" value=""/>
<testng outputdir="${build.test.results.dir}/${testResultsSubDir}" classfilesetref="test.classes"
verbose="${testng.verbose}" haltonfailure="true" useDefaultListeners="false" listeners="${testng.listeners}" workingDir="${basedir}">
verbose="${testng.verbose}" haltonfailure="true" useDefaultListeners="false" listeners="${testng.listeners}" workingDir="${basedir}">
<jvmarg line="${boot.class.path}"/>
<jvmarg line="${run.test.jvmargs} -Xmx${run.test.xmx} ${run.test.jvmsecurityargs} -Dbuild.dir=${build.dir}"/>
<jvmarg line="${debug.test.jvmargs}"/>
@ -466,7 +476,7 @@ grant codeBase "file:/${basedir}/test/script/markdown.js" {
<antcall target="-test-nosecurity" inheritRefs="true">
<param name="optimistic" value="true"/>
<param name="testResultsSubDir" value="optimistic"/>
</antcall>
</antcall>
<antcall target="-test-security" inheritRefs="true">
<param name="optimistic" value="true"/>
<param name="testResultsSubDir" value="optimistic"/>
@ -478,7 +488,7 @@ grant codeBase "file:/${basedir}/test/script/markdown.js" {
<antcall target="-test-nosecurity" inheritRefs="true">
<param name="optimistic" value="false"/>
<param name="testResultsSubDir" value="pessimistic"/>
</antcall>
</antcall>
<antcall target="-test-security" inheritRefs="true">
<param name="optimistic" value="false"/>
<param name="testResultsSubDir" value="pessimistic"/>

@ -2,21 +2,21 @@
<!--
Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
This code is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 only, as
published by the Free Software Foundation.
This code is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
version 2 for more details (a copy is included in the LICENSE file that
accompanied this code).
You should have received a copy of the GNU General Public License version
2 along with this work; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
or visit www.oracle.com if you need additional information or have any
questions.
@ -155,7 +155,7 @@
</view>
<subprojects/>
</general-data>
<java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/3">
<java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/4">
<compilation-unit>
<package-root>../test/src</package-root>
<unit-tests/>

@ -1,21 +1,21 @@
#
# Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
@ -81,8 +81,8 @@ run.test.jvmargs.jfr=-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:Fligh
file.reference.testng.jar=test/lib/testng.jar
# Set testng verbose level
# From TestNG docs: "the verbosity level (0 to 10 where 10 is most detailed)
# Actually, this is a lie: you can specify -1 and this will put TestNG in
# From TestNG docs: "the verbosity level (0 to 10 where 10 is most detailed)
# Actually, this is a lie: you can specify -1 and this will put TestNG in
# debug mode (no longer slicing off stack traces and all)."
testng.verbose=2
@ -134,6 +134,10 @@ test-sys-prop.test.js.roots=${test.basic.dir} ${test.maptests.dir} ${test.error.
test-sys-prop.test262.suite.dir=${test262.suite.dir}
test-sys-prop.es5conform.testcases.dir=${test.external.dir}/ES5Conform/TestCases
test-sys-prop.test.basic.dir=${test.basic.dir}
test-sys-prop.test.external.dir=${test.external.dir}
test-sys-prop.test.maptests.dir=${test.maptests.dir}
test-sys-prop.test.sandbox.dir=${test.sandbox.dir}
test-sys-prop.test.trusted.dir=${test.trusted.dir}
test-sys-prop-no-security.test.dir=${test.dir}
test-sys-prop-no-security.test.js.roots=${test.nosecurity.dir}
@ -147,9 +151,11 @@ test-sys-prop.parsertest.verbose=false
# turn on/off scripting mode for parser tests
test-sys-prop.parsertest.scripting=true
test-sys-prop.parserapitest.verbose=false
# turn on/off test262 scripts for parser tests
test-sys-prop.parsertest.test262=false
test-sys-prop.parserapitest.test262=false
# Control the verbosity of the CompilerTest
test-sys-prop.compilertest.verbose=false
@ -289,19 +295,19 @@ run.test.jvmargs.main=${run.test.jvmargs.common} -esa -ea
# Extra jvmargs that might be useful for debugging
# and performance improvements/monitoring
#
# -XX:+UnlockDiagnosticVMOptions
# -XX:+UnlockDiagnosticVMOptions
#
# turn off compressed class pointers in metaspace
# -XX:-UseCompressedKlassPointers
# -XX:-UseCompressedKlassPointers
#
# dump the heap after every GC
# -XX:+PrintHeapAtGC
#
# manually set a metaspace size for class data
# manually set a metaspace size for class data
# -XX:ClassMetaspaceSize=300M
#
# print out methods compiled
# -XX:+PrintCompilation
# -XX:+PrintCompilation
#
# print all compiled nmethods with oopmaps and lots of other info
# -XX:+PrintNMethods
@ -311,7 +317,7 @@ run.test.jvmargs.main=${run.test.jvmargs.common} -esa -ea
# feature in HotSpot - for example, the uncommon trap placement fix
# was hidden behind this flag before it became the default
#
# -XX:+UnlockDiagnosticVMOptions -XX:+UseNewCode
# -XX:+UnlockDiagnosticVMOptions -XX:+UseNewCode
#
# Crank up the type profile level to 222, which has some warmup
# penalties, but produces much better code for JavaScript, where better
@ -342,7 +348,7 @@ v8.shell=d8
octane.iterations=25
# List of octane tests to run, as properties prefixed with
# "octane.benchmark." mapping to the benchmark name in
# "octane.benchmark." mapping to the benchmark name in
# the test harness
#
# Octane tests that are disabled should have their entire line

@ -0,0 +1,129 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Find break or continue inside try..finally in all scripts
// in a given directory (recursively). If no directory is specified
// the current working directory is scanned for scripts.
if (arguments.length == 0) {
arguments[0] = ".";
}
var File = Java.type("java.io.File");
var file = new File(arguments[0]);
if (!file.exists()) {
print(arguments[0] + " is neither a directory nor a file");
exit(1);
}
var Files = Java.type("java.nio.file.Files");
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
var parser = Parser.create("-scripting", "--const-as-var");
function checkFile(file) {
// print("checking " + file);
var ast = parser.parse(file, print);
if (!ast) {
return;
}
// locate 'break/continue' inside try..finally
ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
lineMap: null,
inTryFinally: false,
printWarning: function(node, msg) {
var pos = node.startPosition;
var line = this.lineMap.getLineNumber(pos);
var column = this.lineMap.getColumnNumber(pos);
print(msg + " found in " + file + " @ " + line + ":" + column);
},
visitBreak: function(node, extra) {
if (this.inTryFinally) {
this.printWarning(node, "break");
}
},
visitContinue: function(node, extra) {
if (this.inTryFinally) {
this.printWarning(node, "continue");
}
},
visitCompilationUnit: function(node, extra) {
this.lineMap = node.lineMap;
Java.super(visitor).visitCompilationUnit(node, extra);
},
visitFunctionDeclaration: function(node, extra) {
var oldInTryFinally = this.inTryFinally;
this.inTryFinally = false;
try {
Java.super(visitor).visitFunctionDeclaration(node, extra);
} finally {
this.inTryFinally = oldInTryFinally;
}
},
visitFunctionExpression: function(node, extra) {
var oldInTryFinally = this.inTryFinally;
this.inTryFinally = false;
try {
Java.super(visitor).visitFunctionExpression(node, extra);
} finally {
this.inTryFinally = oldInTryFinally;
}
},
visitTry: function(node, extra) {
var oldInTryFinally = this.inTryFinally;
this.inTryFinally = (node.finallyBlock != null);
try {
Java.super(visitor).visitTry(node, extra);
} finally {
this.inTryFinally = oldInTryFinally;
}
}
}, null);
}
if (file.isDirectory()) {
Files.walk(file.toPath())
.filter(function(p) Files.isRegularFile(p))
.filter(function(p) p.toFile().name.endsWith('.js'))
.forEach(checkFile);
} else {
checkFile(file);
}

140
nashorn/samples/cricket.js Normal file

@ -0,0 +1,140 @@
#// Usage: jjs -scripting cricket.js
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Example that demonstrates reading XML Rss feed.
// XML DOM Document element is wrapped by script
// "proxy" (JSAdapter constructor)
// Java classes used
var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
var Node = Java.type("org.w3c.dom.Node");
// constants from Node class
var ELEMENT_NODE = Node.ELEMENT_NODE;
var TEXT_NODE = Node.TEXT_NODE;
// parse XML from uri and return Document
function parseXML(uri) {
var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
return docBuilder["parse(java.lang.String)"](uri);
}
// get child Elements of given name of the parent element given
function getChildElements(elem, name) {
var nodeList = elem.childNodes;
var childElems = [];
var len = nodeList.length;
for (var i = 0; i < len; i++) {
var node = nodeList.item(i);
if (node.nodeType == ELEMENT_NODE &&
node.tagName == name) {
childElems.push(wrapElement(node));
}
}
return childElems;
}
// get concatenated child text content of an Element
function getElemText(elem) {
var nodeList = elem.childNodes;
var len = nodeList.length;
var text = '';
for (var i = 0; i < len; i++) {
var node = nodeList.item(i);
if (node.nodeType == TEXT_NODE) {
text += node.nodeValue;
}
}
return text;
}
// Wrap DOM Element object as a convenient script object
// using JSAdapter. JSAdapter is like java.lang.reflect.Proxy
// in that it allows property access, method calls be trapped
// by 'magic' methods like __get__, __call__.
function wrapElement(elem) {
if (! elem) {
return elem;
}
return new JSAdapter() {
// getter to expose child elements and attributes by name
__get__: function(name) {
if (typeof name == 'string') {
if (name.startsWith('@')) {
var attr = elem.getAttributeNode(name.substring(1));
return !attr? undefined : attr.value;
}
var arr = getChildElements(elem, name);
if (arr.length == 1) {
// single child element, expose as single element
return arr[0];
} else {
// multiple children of given name, expose as array
return arr;
}
}
return undefined;
},
__call__: function(name) {
// toString override to get text content of this Element
if (name == 'toString' || name == 'valueOf') {
return getElemText(elem);
}
return undefined;
}
}
}
function printCricketScore() {
var doc = parseXML("http://static.cricinfo.com/rss/livescores.xml");
// wrap document root Element as script convenient object
var rss = wrapElement(doc.documentElement);
print("rss file version " + rss['@version']);
print(rss.channel.title);
print(rss.channel.description);
print(rss.channel.pubDate);
print("=====================");
var items = rss.channel.item;
for each (var i in items) {
print(i.description);
}
}
printCricketScore();

@ -0,0 +1,58 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Simple demo of Nashorn Parser API
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
var IdentifierTree = Java.type("jdk.nashorn.api.tree.IdentifierTree");
var parser = Parser.create();
var ast = parser.parse("t", "eval('hello');\n eval(2 + 3)", print);
// locate 'eval' calls in the script
ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
lineMap: null,
visitCompilationUnit: function(node, extra) {
this.lineMap = node.lineMap;
Java.super(visitor).visitCompilationUnit(node, extra);
},
visitFunctionCall: function(node, extra) {
var func = node.functionSelect;
if (func instanceof IdentifierTree && func.name == "eval") {
var pos = node.startPosition;
var line = this.lineMap.getLineNumber(pos);
var column = this.lineMap.getColumnNumber(pos);
print("eval call found @ " + line + ":" + column);
}
}
}, null);

@ -0,0 +1,113 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// __proto__ magic property is non-standard. Use Object.getPrototypeOf
// and Object.setPrototypeOf instead. This script finds __proto__
// usage in scripts a given directory (recursively). If no directory
// is specified the current working directory is scanned for scripts.
if (arguments.length == 0) {
arguments[0] = ".";
}
var File = Java.type("java.io.File");
var file = new File(arguments[0]);
if (!file.exists()) {
print(arguments[0] + " is neither a file nor a directory");
exit(1);
}
var Files = Java.type("java.nio.file.Files");
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
var parser = Parser.create("-scripting", "--const-as-var");
var protoFound = false;
function checkFile(file) {
// print("checking " + file);
var ast = parser.parse(file, print);
if (!ast) {
return;
}
// locate __proto__ usage and warn
ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
lineMap: null,
printWarning: function(node) {
var pos = node.startPosition;
var line = this.lineMap.getLineNumber(pos);
var column = this.lineMap.getColumnNumber(pos);
print("WARNING: __proto__ usage in " + file + " @ " + line + ":" + column);
},
checkProto: function(node, name) {
if (name == "__proto__") {
protoFound = true;
this.printWarning(node);
}
},
visitCompilationUnit: function(node, extra) {
this.lineMap = node.lineMap;
Java.super(visitor).visitCompilationUnit(node, extra);
},
visitIdentifier: function(node, extra) {
this.checkProto(node, node.name);
Java.super(visitor).visitIdentifier(node, extra);
},
visitMemberSelect: function(node, extra) {
this.checkProto(node, node.identifier);
Java.super(visitor).visitMemberSelect(node, extra);
},
visitProperty: function(node, extra) {
this.checkProto(node, node.key);
Java.super(visitor).visitProperty(node, extra);
}
}, null);
}
if (file.isDirectory()) {
Files.walk(file.toPath())
.filter(function(p) Files.isRegularFile(p))
.filter(function(p) p.toFile().name.endsWith('.js'))
.forEach(checkFile);
} else {
checkFile(file);
}
if (protoFound) {
print("__proto__ is non-standard. Use Object.get/setPrototypeOf instead");
}

@ -0,0 +1,97 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// delete of scope vars is reported as error in strict mode.
// This script finds scripts that such deletes. Scripts in
// the specified directory are scanned. If no directory is
// specified, the current directory is scanned.
if (arguments.length == 0) {
arguments[0] = ".";
}
var File = Java.type("java.io.File");
var file = new File(arguments[0]);
if (!file.exists()) {
print(arguments[0] + " is neither a directory nor a file");
exit(1);
}
var Files = Java.type("java.nio.file.Files");
var IdentifierTree = Java.type("jdk.nashorn.api.tree.IdentifierTree");
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
var Tree = Java.type("jdk.nashorn.api.tree.Tree");
var parser = Parser.create("-scripting", "--const-as-var");
function checkFile(file) {
// print("checking " + file);
var ast = parser.parse(file, print);
if (!ast) {
return;
}
// locate __proto__ usage and warn
ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
lineMap: null,
printWarning: function(node, varName) {
var pos = node.startPosition;
var line = this.lineMap.getLineNumber(pos);
var column = this.lineMap.getColumnNumber(pos);
print("WARNING: delete " + varName + " in " + file + " @ " + line + ":" + column);
},
visitCompilationUnit: function(node, extra) {
this.lineMap = node.lineMap;
Java.super(visitor).visitCompilationUnit(node, extra);
},
visitUnary: function(node, extra) {
if (node.kind == Tree.Kind.DELETE &&
node.expression instanceof IdentifierTree) {
this.printWarning(node, node.expression.name);
}
Java.super(visitor).visitUnary(node, extra);
},
}, null);
}
if (file.isDirectory()) {
Files.walk(file.toPath())
.filter(function(p) Files.isRegularFile(p))
.filter(function(p) p.toFile().name.endsWith('.js'))
.forEach(checkFile);
} else {
checkFile(file);
}

@ -0,0 +1,98 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// 'with' statement is not considered a good practive. In strict mode,
// with statements result in early error being thrown.
// This script finds 'with' usage in scripts a given directory (recursively).
// If no directory is specified the current working directory is scanned
// for scripts.
if (arguments.length == 0) {
arguments[0] = ".";
}
var File = Java.type("java.io.File");
var file = new File(arguments[0]);
if (!file.exists()) {
print(arguments[0] + " is neither a directory nor a file");
exit(1);
}
var Files = Java.type("java.nio.file.Files");
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
var parser = Parser.create("-scripting", "--const-as-var");
var protoFound = false;
function checkFile(file) {
// print("checking " + file);
var ast = parser.parse(file, print);
if (!ast) {
return;
}
// locate __proto__ usage and warn
ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
lineMap: null,
printWarning: function(node) {
var pos = node.startPosition;
var line = this.lineMap.getLineNumber(pos);
var column = this.lineMap.getColumnNumber(pos);
print("WARNING: 'with' usage in " + file + " @ " + line + ":" + column);
},
visitCompilationUnit: function(node, extra) {
this.lineMap = node.lineMap;
Java.super(visitor).visitCompilationUnit(node, extra);
},
visitWith: function(node, extra) {
this.printWarning(node);
Java.super(visitor).visitWith(node, extra);
}
}, null);
}
if (file.isDirectory()) {
Files.walk(file.toPath())
.filter(function(p) Files.isRegularFile(p))
.filter(function(p) p.toFile().name.endsWith('.js'))
.forEach(checkFile);
} else {
checkFile(file);
}
if (protoFound) {
print("__proto__ is non-standard. Use Object.get/setPrototypeOf instead");
}

@ -0,0 +1,113 @@
#// Usage: jjs -scripting -fx nashornastviewer.js -- <scriptfile>
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
# NOTE: This script requires JDK 9 build to run
if (!$OPTIONS._fx) {
print("Usage: jjs -scripting -fx nashornastviewer.js -- <.js file>");
exit(1);
}
// Using JavaFX from Nashorn. See also:
// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
// This example shows AST of a script file as a JavaFX
// tree view in a window. If no file is specified, AST of
// this script file is shown. This script demonstrates
// Nashorn Parser API too - http://openjdk.java.net/jeps/236
// JavaFX classes used
var StackPane = Java.type("javafx.scene.layout.StackPane");
var Scene = Java.type("javafx.scene.Scene");
var TreeItem = Java.type("javafx.scene.control.TreeItem");
var TreeView = Java.type("javafx.scene.control.TreeView");
// Java classes used
var Enum = Java.type("java.lang.Enum");
var File = Java.type("java.io.File");
var List = Java.type("java.util.List");
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var Tree = Java.type("jdk.nashorn.api.tree.Tree");
// Create a javafx TreeItem to view a AST node
function treeItemForASTNode(ast, name) {
var item = new TreeItem(name);
// make an iteratable script object from a Tree
ast = Object.bindProperties({}, ast);
for (var prop in ast) {
var node = ast[prop];
var type = typeof node;
if (node == null || type == "function") {
// skip nulls and Java methods
continue;
}
var subitem = null;
if (node instanceof Tree) {
subitem = treeItemForASTNode(node, prop);
} else if (node instanceof List) {
var len = node.size();
subitem = new TreeItem(prop);
for (var i = 0; i < len; i++) {
var li = treeItemForASTNode(node.get(i), String(i));
subitem.children.add(li);
}
} else if (node instanceof Enum || type != 'object') {
subitem = new TreeItem(prop + ": " + node);
}
if (subitem) {
item.children.add(subitem);
}
}
return item;
}
// do we have a script file passed? if not, use current script
var sourceName = arguments.length == 0? __FILE__ : arguments[0];
var parser = Parser.create("-scripting");
// parse script to get CompilationUnitTree of it
var ast = parser.parse(new File(sourceName), null);
// JavaFX start method
function start(stage) {
stage.title = "AST Viewer";
var rootItem = treeItemForASTNode(ast, sourceName);
var tree = new TreeView(rootItem);
var root = new StackPane();
root.children.add(tree);
stage.scene = new Scene(root, 300, 450);
stage.show();
}

@ -0,0 +1,38 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Simple demo of Nashorn Parser API
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var parser = Parser.create("-strict");
// you'll get diagnostic message for 'with'
parser.parse("t", "with({}){}", print);

@ -0,0 +1,103 @@
#jjs -fx xmlviewer.js [-- <url-of-xml-doc>]
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
if (! $OPTIONS._fx) {
print("Usage: jjs -fx xmlviewer.js [-- <url-of-xml-doc>]");
exit(1);
}
// Using JavaFX from Nashorn. See also:
// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
// Simple sample to view a XML document as a JavaFX tree.
// JavaFX classes used
var StackPane = Java.type("javafx.scene.layout.StackPane");
var Scene = Java.type("javafx.scene.Scene");
var TreeItem = Java.type("javafx.scene.control.TreeItem");
var TreeView = Java.type("javafx.scene.control.TreeView");
// XML DocumentBuilderFactory
var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
var Attr = Java.type("org.w3c.dom.Attr");
var Element = Java.type("org.w3c.dom.Element");
var Text = Java.type("org.w3c.dom.Text");
// parse XML from uri and return Document
function parseXML(uri) {
var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
docBuilder.validating = false;
return docBuilder["parse(java.lang.String)"](uri);
}
// Create a javafx TreeItem to view a XML element
function treeItemForObject(element, name) {
var item = new TreeItem(name);
item.expanded = true;
var attrs = element.attributes;
var numAttrs = attrs.length;
for (var a = 0; a < numAttrs; a++) {
var attr = attrs.item(a);
var subitem = new TreeItem(attr.name + " = " + attr.value);
item.children.add(subitem);
}
var childNodes = element.childNodes;
var numNodes = childNodes.length;
for (var n = 0; n < numNodes; n++) {
var node = childNodes.item(n);
if (node instanceof Element) {
var subitem = treeItemForObject(node, node.tagName);
item.children.add(subitem);
}
}
return item;
}
// Ofcourse, the best default URL is cricket score :)
var DEFAULT_URL = "http://synd.cricbuzz.com/j2me/1.0/livematches.xml";
var url = arguments.length == 0? DEFAULT_URL : arguments[0];
var element = parseXML(url).getDocumentElement();
// JavaFX start method
function start(stage) {
stage.title = "XML Viewer: " + url;
var rootItem = treeItemForObject(element, element.tagName);
var tree = new TreeView(rootItem);
var root = new StackPane();
root.children.add(tree);
stage.scene = new Scene(root, 300, 450);
stage.show();
}

@ -161,9 +161,8 @@ public abstract class AbstractJSObject implements JSObject {
* @return set of property names
*/
@Override
@SuppressWarnings("unchecked")
public Set<String> keySet() {
return Collections.EMPTY_SET;
return Collections.emptySet();
}
/**
@ -172,9 +171,8 @@ public abstract class AbstractJSObject implements JSObject {
* @return set of property values.
*/
@Override
@SuppressWarnings("unchecked")
public Collection<Object> values() {
return Collections.EMPTY_SET;
return Collections.emptySet();
}
// JavaScript instanceof check
@ -249,9 +247,27 @@ public abstract class AbstractJSObject implements JSObject {
* Returns this object's numeric value.
*
* @return this object's numeric value.
* @deprecated use {@link #getDefaultValue(Class)} with {@link Number} hint instead.
*/
@Override
@Override @Deprecated
public double toNumber() {
return Double.NaN;
}
/**
* When passed an {@link AbstractJSObject}, invokes its {@link #getDefaultValue(Class)} method. When passed any
* other {@link JSObject}, it will obtain its {@code [[DefaultValue]]} method as per ECMAScript 5.1 section
* 8.6.2.
*
* @param jsobj the {@link JSObject} whose {@code [[DefaultValue]]} is obtained.
* @param hint the type hint. Should be either {@code null}, {@code Number.class} or {@code String.class}.
* @return this object's default value.
* @throws UnsupportedOperationException if the conversion can't be performed. The engine will convert this
* exception into a JavaScript {@code TypeError}.
* @deprecated use {@link JSObject#getDefaultValue(Class)} instead.
*/
@Deprecated
public static Object getDefaultValue(final JSObject jsobj, final Class<?> hint) {
return jsobj.getDefaultValue(hint);
}
}

@ -813,7 +813,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
}
}
@Override
@Override @Deprecated
public double toNumber() {
return inGlobal(new Callable<Double>() {
@Override public Double call() {
@ -823,7 +823,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
}
@Override
public Object getDefaultValue(Class<?> hint) {
public Object getDefaultValue(final Class<?> hint) {
return inGlobal(new Callable<Object>() {
@Override public Object call() {
try {

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for an array access expression.
*
* For example:
* <pre>
* <em>expression</em> [ <em>index</em> ]
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ArrayAccessTree extends ExpressionTree {
/**
* Returns the array that is accessed.
*
* @return the array that is accessed
*/
ExpressionTree getExpression();
/**
* Returns the index of the array element accessed.
*
* @return the index expression
*/
ExpressionTree getIndex();
}

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.Expression;
final class ArrayAccessTreeImpl extends ExpressionTreeImpl implements ArrayAccessTree {
private final ExpressionTree base, index;
ArrayAccessTreeImpl(final Expression node, final ExpressionTree base, final ExpressionTree index) {
super(node);
this.base = base;
this.index = index;
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.ARRAY_ACCESS;
}
@Override
public ExpressionTree getExpression() {
return base;
}
@Override
public ExpressionTree getIndex() {
return index;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitArrayAccess(this, data);
}
}

@ -0,0 +1,43 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* Represents ECMAScript array literal expression.
*
* @since 1.9
*/
@jdk.Exported
public interface ArrayLiteralTree extends ExpressionTree {
/**
* Returns the list of Array element expressions.
*
* @return array element expressions
*/
public List<? extends ExpressionTree> getElements();
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.LiteralNode;
final class ArrayLiteralTreeImpl extends ExpressionTreeImpl
implements ArrayLiteralTree {
private final List<? extends ExpressionTree> elements;
ArrayLiteralTreeImpl(final LiteralNode<?> node, final List<? extends ExpressionTree> elements) {
super(node);
this.elements = elements;
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.ARRAY_LITERAL;
}
@Override
public List<? extends ExpressionTree> getElements() {
return elements;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitArrayLiteral(this, data);
}
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for an assignment expression.
*
* For example:
* <pre>
* <em>variable</em> = <em>expression</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface AssignmentTree extends ExpressionTree {
/**
* Returns the left hand side (LHS) of this assignment.
*
* @return left hand side (LHS) expression
*/
ExpressionTree getVariable();
/**
* Returns the right hand side (RHS) of this assignment.
*
* @return right hand side (RHS) expression
*/
ExpressionTree getExpression();
}

@ -0,0 +1,61 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.BinaryNode;
final class AssignmentTreeImpl extends ExpressionTreeImpl implements AssignmentTree {
private final Tree.Kind kind;
private final ExpressionTree var, expr;
AssignmentTreeImpl(final BinaryNode node, final ExpressionTree left, final ExpressionTree right) {
super(node);
assert node.isAssignment() : "assignment node expected";
this.var = left;
this.expr = right;
this.kind = getOperator(node.tokenType());
}
@Override
public Tree.Kind getKind() {
return kind;
}
@Override
public ExpressionTree getVariable() {
return var;
}
@Override
public ExpressionTree getExpression() {
return expr;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitAssignment(this, data);
}
}

@ -0,0 +1,54 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a binary expression.
* Use {@link #getKind getKind} to determine the kind of operator.
*
* For example:
* <pre>
* <em>leftOperand</em> <em>operator</em> <em>rightOperand</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface BinaryTree extends ExpressionTree {
/**
* Returns left hand side (LHS) of this binary expression.
*
* @return left hand side (LHS) of this binary expression
*/
ExpressionTree getLeftOperand();
/**
* Returns right hand side (RHS) of this binary expression.
*
* @return right hand side (RHS) of this binary expression
*/
ExpressionTree getRightOperand();
}

@ -0,0 +1,61 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.BinaryNode;
class BinaryTreeImpl extends ExpressionTreeImpl implements BinaryTree {
private final Tree.Kind kind;
private final ExpressionTree left, right;
BinaryTreeImpl(final BinaryNode node, final ExpressionTree left, final ExpressionTree right) {
super(node);
assert !node.isAssignment() : "assignment node";
this.left = left;
this.right = right;
this.kind = getOperator(node.tokenType());
}
@Override
public Tree.Kind getKind() {
return kind;
}
@Override
public ExpressionTree getLeftOperand() {
return left;
}
@Override
public ExpressionTree getRightOperand() {
return right;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitBinary(this, data);
}
}

@ -0,0 +1,50 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* A tree node for a statement block.
*
* For example:
* <pre>
* { }
*
* { <em>statements</em> }
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface BlockTree extends StatementTree {
/**
* Returns the list of statements in this block.
*
* @return the list of statements in this block
*/
List<? extends StatementTree> getStatements();
}

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.Block;
import jdk.nashorn.internal.ir.BlockStatement;
final class BlockTreeImpl extends StatementTreeImpl implements BlockTree {
private final List<? extends StatementTree> statements;
BlockTreeImpl(final BlockStatement node, final List<? extends StatementTree> statements) {
super(node);
this.statements = statements;
}
BlockTreeImpl(final Block node, final List<? extends StatementTree> statements) {
super(node);
this.statements = statements;
}
@Override
public Kind getKind() {
return Kind.BLOCK;
}
@Override
public List<? extends StatementTree> getStatements() {
return statements;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitBlock(this, data);
}
}

@ -0,0 +1,50 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a 'break' statement.
*
* For example:
* <pre>
* break;
*
* break <em>label</em> ;
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface BreakTree extends GotoTree {
/**
* Label associated with this break statement. This is null
* if there is no label associated with this break statement.
*
* @return label associated with this break statement.
*/
@Override
String getLabel();
}

@ -0,0 +1,52 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.BreakNode;
final class BreakTreeImpl extends StatementTreeImpl implements BreakTree {
private final String label;
BreakTreeImpl(final BreakNode node) {
super(node);
this.label = node.getLabelName();
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.BREAK;
}
@Override
public String getLabel() {
return label;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitBreak(this, data);
}
}

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* A tree node for a 'case' in a 'switch' statement.
*
* For example:
* <pre>
* case <em>expression</em> :
* <em>statements</em>
*
* default :
* <em>statements</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface CaseTree extends Tree {
/**
* Case expression of this 'case' statement.
*
* @return null if and only if this Case is {@code default:}
*/
ExpressionTree getExpression();
/**
* Return the list of statements for this 'case'.
*
* @return list of statements for this 'case'
*/
List<? extends StatementTree> getStatements();
}

@ -0,0 +1,62 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.CaseNode;
final class CaseTreeImpl extends TreeImpl implements CaseTree {
private final ExpressionTree expression;
private final List<? extends StatementTree> statements;
public CaseTreeImpl(final CaseNode node,
final ExpressionTree expression,
final List<? extends StatementTree> statements) {
super(node);
this.expression = expression;
this.statements = statements;
}
@Override
public Kind getKind() {
return Kind.CASE;
}
@Override
public ExpressionTree getExpression() {
return expression;
}
@Override
public List<? extends StatementTree> getStatements() {
return statements;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitCase(this, data);
}
}

@ -0,0 +1,62 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a 'catch' block in a 'try' statement.
*
* For example:
* <pre>
* catch ( <em>parameter</em> )
* <em>block</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface CatchTree extends Tree {
/**
* Returns the catch parameter identifier of the exception caught.
*
* @return the catch parameter identifier
*/
IdentifierTree getParameter();
/**
* Returns the code block of this catch block.
*
* @return the code block
*/
BlockTree getBlock();
/**
* Returns the optional catch condition expression. This is null
* if this is an unconditional catch statement.
*
* @return the optional catch condition expression.
*/
ExpressionTree getCondition();
}

@ -0,0 +1,69 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.CatchNode;
final class CatchTreeImpl extends TreeImpl implements CatchTree {
private final IdentifierTree param;
private final BlockTree block;
private final ExpressionTree condition;
CatchTreeImpl(final CatchNode node,
final IdentifierTree param,
final BlockTree block,
final ExpressionTree condition) {
super(node);
this.param = param;
this.block = block;
this.condition = condition;
}
@Override
public Kind getKind() {
return Kind.CATCH;
}
@Override
public IdentifierTree getParameter() {
return param;
}
@Override
public BlockTree getBlock() {
return block;
}
@Override
public ExpressionTree getCondition() {
return condition;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitCatch(this, data);
}
}

@ -0,0 +1,66 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* Represents the abstract syntax tree for compilation units (source
* files)
*
* @since 1.9
*/
@jdk.Exported
public interface CompilationUnitTree extends Tree {
/**
* Return the list of source elements in this compilation unit.
*
* @return the list of source elements in this compilation unit
*/
List<? extends Tree> getSourceElements();
/**
* Return the source name of this script compilation unit.
*
* @return the source name of this script compilation unit
*/
String getSourceName();
/**
* Returns if this is a ECMAScript "strict" compilation unit or not.
*
* @return true if this compilation unit is declared "strict"
*/
boolean isStrict();
/**
* Returns the line map for this compilation unit, if available.
* Returns null if the line map is not available.
*
* @return the line map for this compilation unit
*/
LineMap getLineMap();
}

@ -0,0 +1,73 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.FunctionNode;
final class CompilationUnitTreeImpl extends TreeImpl
implements CompilationUnitTree {
private final FunctionNode funcNode;
private final List<? extends Tree> elements;
CompilationUnitTreeImpl(final FunctionNode node,
final List<? extends Tree> elements) {
super(node);
this.funcNode = node;
assert funcNode.getKind() == FunctionNode.Kind.SCRIPT : "script function expected";
this.elements = elements;
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.COMPILATION_UNIT;
}
@Override
public List<? extends Tree> getSourceElements() {
return elements;
}
@Override
public String getSourceName() {
return funcNode.getSourceName();
}
@Override
public boolean isStrict() {
return funcNode.isStrict();
}
@Override
public LineMap getLineMap() {
return new LineMapImpl(funcNode.getSource());
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitCompilationUnit(this, data);
}
}

@ -0,0 +1,54 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for compound assignment operator.
* Use {@link #getKind getKind} to determine the kind of operator.
*
* For example:
* <pre>
* <em>variable</em> <em>operator</em> <em>expression</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface CompoundAssignmentTree extends ExpressionTree {
/**
* Returns the left hand side (LHS) of this assignment.
*
* @return left hand side (LHS) expression
*/
ExpressionTree getVariable();
/**
* Returns the right hand side (RHS) of this assignment.
*
* @return right hand side (RHS) expression
*/
ExpressionTree getExpression();
}

@ -0,0 +1,63 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.BinaryNode;
final class CompoundAssignmentTreeImpl extends ExpressionTreeImpl implements CompoundAssignmentTree {
private final ExpressionTree var, expr;
private final Kind kind;
CompoundAssignmentTreeImpl(final BinaryNode node, final ExpressionTree left, final ExpressionTree right) {
super(node);
assert node.isAssignment() : "not an assignment node";
this.var = left;
this.expr = right;
this.kind = getOperator(node.tokenType());
assert kind != Tree.Kind.ASSIGNMENT : "compound assignment expected";
}
@Override
public Kind getKind() {
return kind;
}
@Override
public ExpressionTree getVariable() {
return var;
}
@Override
public ExpressionTree getExpression() {
return expr;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitCompoundAssignment(this, data);
}
}

@ -0,0 +1,60 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for the conditional operator ? :.
*
* For example:
* <pre>
* <em>condition</em> ? <em>trueExpression</em> : <em>falseExpression</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ConditionalExpressionTree extends ExpressionTree {
/**
* Returns the condition expression of this ternary expression.
*
* @return the condition expression
*/
ExpressionTree getCondition();
/**
* Returns the true part of this ternary expression.
*
* @return the 'true' part expression
*/
ExpressionTree getTrueExpression();
/**
* Returns the false part of this ternary expression.
*
* @return the 'false' part expression
*/
ExpressionTree getFalseExpression();
}

@ -0,0 +1,67 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.TernaryNode;
final class ConditionalExpressionTreeImpl extends ExpressionTreeImpl implements ConditionalExpressionTree {
private final ExpressionTree condExpr, trueExpr, falseExpr;
ConditionalExpressionTreeImpl(final TernaryNode node,
final ExpressionTree condExpr, final ExpressionTree trueExpr,
final ExpressionTree falseExpr) {
super(node);
this.condExpr = condExpr;
this.trueExpr = trueExpr;
this.falseExpr = falseExpr;
}
@Override
public Kind getKind() {
return Kind.CONDITIONAL_EXPRESSION;
}
@Override
public ExpressionTree getCondition() {
return condExpr;
}
@Override
public ExpressionTree getTrueExpression() {
return trueExpr;
}
@Override
public ExpressionTree getFalseExpression() {
return falseExpr;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitConditionalExpression(this, data);
}
}

@ -0,0 +1,41 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A mixin for conditional "loop" statements.
*
* @since 1.9
*/
@jdk.Exported
public interface ConditionalLoopTree extends LoopTree {
/**
* Returns the condition expression of this 'loop' statement.
*
* @return the condition expression
*/
ExpressionTree getCondition();
}

@ -0,0 +1,49 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a 'continue' statement.
*
* For example:
* <pre>
* continue;
* continue <em>label</em> ;
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ContinueTree extends GotoTree {
/**
* Label associated with this continue statement. This is null
* if there is no label associated with this continue statement.
*
* @return label associated with this continue statement.
*/
@Override
String getLabel();
}

@ -0,0 +1,52 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.ContinueNode;
final class ContinueTreeImpl extends StatementTreeImpl implements ContinueTree {
private final String label;
ContinueTreeImpl(final ContinueNode node) {
super(node);
this.label = node.getLabelName();
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.CONTINUE;
}
@Override
public String getLabel() {
return label;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitContinue(this, data);
}
}

@ -0,0 +1,40 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a 'debugger' statement.
*
* For example:
* <pre>
* <em>debugger;</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface DebuggerTree extends StatementTree {
}

@ -0,0 +1,44 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.DebuggerNode;
final class DebuggerTreeImpl extends StatementTreeImpl implements DebuggerTree {
DebuggerTreeImpl(final DebuggerNode node) {
super(node);
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.DEBUGGER;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitDebugger(this, data);
}
}

@ -0,0 +1,151 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* Interface for diagnostics from tools. A diagnostic usually reports
* a problem at a specific position in a source file. However, not
* all diagnostics are associated with a position or a file.
*
* <p>A position is a zero-based character offset from the beginning of
* a file. Negative values (except {@link #NOPOS}) are not valid
* positions.
*
* <p>Line and column numbers begin at 1. Negative values (except
* {@link #NOPOS}) and 0 are not valid line or column numbers.
*
* <p>Line terminator is as defined in ECMAScript specification which is one
* of { &#92;u000A, &#92;u000B, &#92;u2028, &#92;u2029 }.
*
* @since 1.9
*/
@jdk.Exported
public interface Diagnostic {
/**
* Kinds of diagnostics, for example, error or warning.
*
* The kind of a diagnostic can be used to determine how the
* diagnostic should be presented to the user. For example,
* errors might be colored red or prefixed with the word "Error",
* while warnings might be colored yellow or prefixed with the
* word "Warning". There is no requirement that the Kind
* should imply any inherent semantic meaning to the message
* of the diagnostic: for example, a tool might provide an
* option to report all warnings as errors.
*/
enum Kind {
/**
* Problem which prevents the tool's normal completion.
*/
ERROR,
/**
* Problem which does not usually prevent the tool from
* completing normally.
*/
WARNING,
/**
* Problem similar to a warning, but is mandated by the tool's
* specification. For example, the Java&trade; Language
* Specification mandates warnings on certain
* unchecked operations and the use of deprecated methods.
*/
MANDATORY_WARNING,
/**
* Informative message from the tool.
*/
NOTE,
/**
* Diagnostic which does not fit within the other kinds.
*/
OTHER,
}
/**
* Used to signal that no position is available.
*/
public final static long NOPOS = -1;
/**
* Gets the kind of this diagnostic, for example, error or
* warning.
* @return the kind of this diagnostic
*/
Kind getKind();
/**
* Gets a character offset from the beginning of the source object
* associated with this diagnostic that indicates the location of
* the problem. In addition, the following must be true:
*
* <p>{@code getStartPostion() <= getPosition()}
* <p>{@code getPosition() <= getEndPosition()}
*
* @return character offset from beginning of source; {@link
* #NOPOS} if no location is suitable
*/
long getPosition();
/**
* Gets the source file name.
*
* @return the file name or null if not available
*/
String getFileName();
/**
* Gets the line number of the character offset returned by
* {@linkplain #getPosition()}.
*
* @return a line number or {@link #NOPOS} if and only if {@link
* #getPosition()} returns {@link #NOPOS}
*/
long getLineNumber();
/**
* Gets the column number of the character offset returned by
* {@linkplain #getPosition()}.
*
* @return a column number or {@link #NOPOS} if and only if {@link
* #getPosition()} returns {@link #NOPOS}
*/
long getColumnNumber();
/**
* Gets a diagnostic code indicating the type of diagnostic. The
* code is implementation-dependent and might be {@code null}.
*
* @return a diagnostic code
*/
String getCode();
/**
* Gets a message for this diagnostic.
*
* @return a message
*/
String getMessage();
}

@ -0,0 +1,80 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.parser.Token;
import jdk.nashorn.internal.runtime.ParserException;
final class DiagnosticImpl implements Diagnostic {
private final ParserException exp;
private final Kind kind;
DiagnosticImpl(final ParserException exp, final Kind kind) {
this.exp = exp;
this.kind = kind;
}
@Override
public Kind getKind() {
return kind;
}
@Override
public long getPosition() {
return exp.getPosition();
}
@Override
public String getFileName() {
return exp.getFileName();
}
@Override
public long getLineNumber() {
return exp.getLineNumber();
}
@Override
public long getColumnNumber() {
return exp.getColumnNumber();
}
@Override
public String getCode() {
final long token = exp.getToken();
return (token < 0)? null : Token.toString(null, token, true);
}
@Override
public String getMessage() {
return exp.getMessage();
}
@Override
public String toString() {
return getMessage();
}
}

@ -0,0 +1,42 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* Interface for receiving diagnostics from Nashorn parser.
*
* @since 1.9
*/
@jdk.Exported
@FunctionalInterface
public interface DiagnosticListener {
/**
* Invoked whenever a parsing problem is found.
*
* @param diagnostic additional information errors, warnings detected during parsing.
*/
void report(Diagnostic diagnostic);
}

@ -0,0 +1,55 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a 'do' statement.
*
* For example:
* <pre>
* do
* <em>statement</em>
* while ( <em>expression</em> );
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface DoWhileLoopTree extends ConditionalLoopTree {
/**
* Returns the condition expression of this do-while statement.
*
* @return the condition expression
*/
ExpressionTree getCondition();
/**
* The statement contained within this do-while statement.
*
* @return the statement
*/
StatementTree getStatement();
}

@ -0,0 +1,60 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.WhileNode;
final class DoWhileLoopTreeImpl extends StatementTreeImpl implements DoWhileLoopTree {
private final ExpressionTree cond;
private final StatementTree stat;
DoWhileLoopTreeImpl(final WhileNode node, final ExpressionTree cond, final StatementTree stat) {
super(node);
assert node.isDoWhile() : "do while expected";
this.cond = cond;
this.stat = stat;
}
@Override
public Kind getKind() {
return Kind.DO_WHILE_LOOP;
}
@Override
public ExpressionTree getCondition() {
return cond;
}
@Override
public StatementTree getStatement() {
return stat;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitDoWhileLoop(this, data);
}
}

@ -0,0 +1,39 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for an empty (skip) statement.
*
* For example:
* <pre>
* ;
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface EmptyStatementTree extends StatementTree {}

@ -0,0 +1,44 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.EmptyNode;
final class EmptyStatementTreeImpl extends StatementTreeImpl implements EmptyStatementTree {
EmptyStatementTreeImpl(final EmptyNode node) {
super(node);
}
@Override
public Kind getKind() {
return Kind.EMPTY_STATEMENT;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitEmptyStatement(this, data);
}
}

@ -0,0 +1,35 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node to stand in for a malformed expression.
*
* @since 1.9
*/
@jdk.Exported
public interface ErroneousTree extends ExpressionTree {
}

@ -0,0 +1,44 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.ErrorNode;
final class ErroneousTreeImpl extends ExpressionTreeImpl implements ErroneousTree {
ErroneousTreeImpl(final ErrorNode errorNode) {
super(errorNode);
}
@Override
public Kind getKind() {
return Kind.ERROR;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitErroneous(this, data);
}
}

@ -0,0 +1,46 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for an expression statement.
*
* For example:
* <pre>
* <em>expression</em> ;
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ExpressionStatementTree extends StatementTree {
/**
* Returns the expression of this expression statement.
*
* @return the expression
*/
ExpressionTree getExpression();
}

@ -0,0 +1,52 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.ExpressionStatement;
final class ExpressionStatementTreeImpl extends StatementTreeImpl implements ExpressionStatementTree {
private final ExpressionTree expr;
ExpressionStatementTreeImpl(final ExpressionStatement es, final ExpressionTree expr) {
super(es);
this.expr = expr;
}
@Override
public Kind getKind() {
return Kind.EXPRESSION_STATEMENT;
}
@Override
public ExpressionTree getExpression() {
return expr;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitExpressionStatement(this, data);
}
}

@ -0,0 +1,35 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node used as the base class for the different types of
* expressions.
*
* @since 1.9
*/
@jdk.Exported
public interface ExpressionTree extends Tree {}

@ -0,0 +1,34 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.Expression;
abstract class ExpressionTreeImpl extends TreeImpl implements ExpressionTree {
ExpressionTreeImpl(final Expression expr) {
super(expr);
}
}

@ -0,0 +1,68 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for for..in statement
*
* For example:
* <pre>
* for ( <em>variable</em> in <em>expression</em> )
* <em>statement</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ForInLoopTree extends LoopTree {
/**
* The for..in left hand side expression.
*
* @return the left hand side expression
*/
ExpressionTree getVariable();
/**
* The object or array being whose properties are iterated.
*
* @return the object or array expression being iterated
*/
ExpressionTree getExpression();
/**
* The statement contained in this for..in statement.
*
* @return the statement
*/
StatementTree getStatement();
/**
* Returns if this is a for..each..in statement or not.
*
* @return true if this is a for..each..in statement
*/
boolean isForEach();
}

@ -0,0 +1,77 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.ForNode;
final class ForInLoopTreeImpl extends StatementTreeImpl implements ForInLoopTree {
private final ExpressionTree lhsExpr;
private final ExpressionTree expr;
private final StatementTree stat;
private final boolean forEach;
ForInLoopTreeImpl(final ForNode node,
final ExpressionTree lhsExpr,
final ExpressionTree expr,
final StatementTree stat) {
super(node);
assert node.isForIn() : "for ..in expected";
this.lhsExpr = lhsExpr;
this.expr = expr;
this.stat = stat;
this.forEach = node.isForEach();
}
@Override
public Kind getKind() {
return Kind.FOR_IN_LOOP;
}
@Override
public ExpressionTree getVariable() {
return lhsExpr;
}
@Override
public ExpressionTree getExpression() {
return expr;
}
@Override
public StatementTree getStatement() {
return stat;
}
@Override
public boolean isForEach() {
return forEach;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitForInLoop(this, data);
}
}

@ -0,0 +1,70 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a basic 'for' loop statement.
*
* For example:
* <pre>
* for ( <em>initializer</em> ; <em>condition</em> ; <em>update</em> )
* <em>statement</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ForLoopTree extends ConditionalLoopTree {
/**
* Returns the initializer expression of this 'for' statement.
*
* @return the initializer expression
*/
ExpressionTree getInitializer();
/**
* Returns the condition expression of this 'for' statement.
*
* @return the condition expression
*/
@Override
ExpressionTree getCondition();
/**
* Returns the update expression of this 'for' statement.
*
* @return the update expression
*/
ExpressionTree getUpdate();
/**
* Returns the statement contained in this 'for' statement.
*
* @return the statement
*/
@Override
StatementTree getStatement();
}

@ -0,0 +1,78 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.ForNode;
final class ForLoopTreeImpl extends StatementTreeImpl implements ForLoopTree {
private final ExpressionTree init;
private final ExpressionTree cond;
private final ExpressionTree update;
private final StatementTree stat;
ForLoopTreeImpl(final ForNode node,
final ExpressionTree init,
final ExpressionTree cond,
final ExpressionTree update,
final StatementTree stat) {
super(node);
assert !node.isForIn() : "for statement expected";
this.init = init;
this.cond = cond;
this.update = update;
this.stat = stat;
}
@Override
public Kind getKind() {
return Kind.FOR_LOOP;
}
@Override
public ExpressionTree getInitializer() {
return init;
}
@Override
public ExpressionTree getCondition() {
return cond;
}
@Override
public ExpressionTree getUpdate() {
return update;
}
@Override
public StatementTree getStatement() {
return stat;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitForLoop(this, data);
}
}

@ -0,0 +1,57 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* A tree node for a function call expression.
*
* For example:
* <pre>
* <em>identifier</em> ( <em>arguments</em> )
*
* this . <em>identifier</em> ( <em>arguments</em> )
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface FunctionCallTree extends ExpressionTree {
/**
* Returns the function being called.
*
* @return the function being called
*/
ExpressionTree getFunctionSelect();
/**
* Returns the list of arguments being passed to this function call.
*
* @return the list of argument expressions
*/
List<? extends ExpressionTree> getArguments();
}

@ -0,0 +1,61 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.CallNode;
class FunctionCallTreeImpl extends ExpressionTreeImpl implements FunctionCallTree {
private final List<? extends ExpressionTree> arguments;
private final ExpressionTree function;
FunctionCallTreeImpl(final CallNode node,
final ExpressionTree function,
final List<? extends ExpressionTree> arguments) {
super(node);
this.function = function;
this.arguments = arguments;
}
@Override
public Kind getKind() {
return Kind.FUNCTION_INVOCATION;
}
@Override
public ExpressionTree getFunctionSelect() {
return function;
}
@Override
public List<? extends ExpressionTree> getArguments() {
return arguments;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitFunctionCall(this, data);
}
}

@ -0,0 +1,71 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* A tree node for a function declaration.
*
* For example:
* <pre>
* <em>function</em> <em>name</em>
* ( <em>parameters</em> )
* <em>body</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface FunctionDeclarationTree extends StatementTree {
/**
* Returns the name of the function being declared.
*
* @return name the function declared
*/
String getName();
/**
* Returns the parameters of this function.
*
* @return the list of parameters
*/
List<? extends ExpressionTree> getParameters();
/**
* Returns the body of code of this function.
*
* @return the body of code
*/
BlockTree getBody();
/**
* Is this a strict function?
*
* @return true if this function is strict
*/
boolean isStrict();
}

@ -0,0 +1,80 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.FunctionNode;
import jdk.nashorn.internal.ir.VarNode;
final class FunctionDeclarationTreeImpl extends StatementTreeImpl
implements FunctionDeclarationTree {
private final FunctionNode funcNode;
private final String funcName;
private final List<? extends ExpressionTree> params;
private final BlockTree body;
FunctionDeclarationTreeImpl(final VarNode node,
final List<? extends ExpressionTree> params,
final BlockTree body) {
super(node);
assert node.getInit() instanceof FunctionNode : "function expected";
funcNode = (FunctionNode)node.getInit();
assert funcNode.isDeclared() : "function declaration expected";
funcName = node.getName().getName();
this.params = params;
this.body = body;
}
@Override
public Kind getKind() {
return Kind.FUNCTION;
}
@Override
public String getName() {
return funcName;
}
@Override
public List<? extends ExpressionTree> getParameters() {
return params;
}
@Override
public BlockTree getBody() {
return body;
}
@Override
public boolean isStrict() {
return funcNode.isStrict();
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitFunctionDeclaration(this, data);
}
}

@ -0,0 +1,71 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* A tree node for a function expression.
*
* For example:
* <pre>
* <em>var</em> func = <em>function</em>
* ( <em>parameters</em> )
* <em>body</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface FunctionExpressionTree extends ExpressionTree {
/**
* Returns the name of the function being declared.
*
* @return name the function declared
*/
String getName();
/**
* Returns the parameters of this function.
*
* @return the list of parameters
*/
List<? extends ExpressionTree> getParameters();
/**
* Returns the body of code of this function.
*
* @return the body of code
*/
BlockTree getBody();
/**
* Is this a strict function?
*
* @return true if this function is strict
*/
boolean isStrict();
}

@ -0,0 +1,85 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.FunctionNode;
final class FunctionExpressionTreeImpl extends ExpressionTreeImpl
implements FunctionExpressionTree {
private final FunctionNode funcNode;
private final String funcName;
private final List<? extends ExpressionTree> params;
private final BlockTree body;
FunctionExpressionTreeImpl(final FunctionNode node,
final List<? extends ExpressionTree> params,
final BlockTree body) {
super(node);
funcNode = node;
assert !funcNode.isDeclared() : "function expression expected";
final FunctionNode.Kind kind = node.getKind();
if (node.isAnonymous() || kind == FunctionNode.Kind.GETTER || kind == FunctionNode.Kind.SETTER) {
funcName = null;
} else {
funcName = node.getIdent().getName();
}
this.params = params;
this.body = body;
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.FUNCTION_EXPRESSION;
}
@Override
public String getName() {
return funcName;
}
@Override
public List<? extends ExpressionTree> getParameters() {
return params;
}
@Override
public BlockTree getBody() {
return body;
}
@Override
public boolean isStrict() {
return funcNode.isStrict();
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitFunctionExpression(this, data);
}
}

@ -0,0 +1,45 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a statement that jumps to a target. Note that
* ECMAScript does not support a goto statement. But, this Tree
* type serves as a super interface for {@link BreakTree} and
* {@link ContinueTree}.
*
* @since 1.9
*/
@jdk.Exported
public interface GotoTree extends StatementTree {
/**
* Label associated with this goto statement. This is null
* if there is no label associated with this goto statement.
*
* @return label associated with this goto statement.
*/
String getLabel();
}

@ -0,0 +1,517 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.ArrayList;
import java.util.List;
import jdk.nashorn.internal.ir.AccessNode;
import jdk.nashorn.internal.ir.BinaryNode;
import jdk.nashorn.internal.ir.Block;
import jdk.nashorn.internal.ir.BlockStatement;
import jdk.nashorn.internal.ir.BreakNode;
import jdk.nashorn.internal.ir.CallNode;
import jdk.nashorn.internal.ir.CaseNode;
import jdk.nashorn.internal.ir.CatchNode;
import jdk.nashorn.internal.ir.ContinueNode;
import jdk.nashorn.internal.ir.DebuggerNode;
import jdk.nashorn.internal.ir.EmptyNode;
import jdk.nashorn.internal.ir.ErrorNode;
import jdk.nashorn.internal.ir.Expression;
import jdk.nashorn.internal.ir.ExpressionStatement;
import jdk.nashorn.internal.ir.ForNode;
import jdk.nashorn.internal.ir.FunctionNode;
import jdk.nashorn.internal.ir.IdentNode;
import jdk.nashorn.internal.ir.IfNode;
import jdk.nashorn.internal.ir.IndexNode;
import jdk.nashorn.internal.ir.LabelNode;
import jdk.nashorn.internal.ir.LexicalContext;
import jdk.nashorn.internal.ir.LiteralNode;
import jdk.nashorn.internal.ir.Node;
import jdk.nashorn.internal.ir.ObjectNode;
import jdk.nashorn.internal.ir.PropertyNode;
import jdk.nashorn.internal.ir.ReturnNode;
import jdk.nashorn.internal.ir.RuntimeNode;
import jdk.nashorn.internal.ir.SplitNode;
import jdk.nashorn.internal.ir.Statement;
import jdk.nashorn.internal.ir.SwitchNode;
import jdk.nashorn.internal.ir.TernaryNode;
import jdk.nashorn.internal.ir.ThrowNode;
import jdk.nashorn.internal.ir.TryNode;
import jdk.nashorn.internal.ir.UnaryNode;
import jdk.nashorn.internal.ir.VarNode;
import jdk.nashorn.internal.ir.WhileNode;
import jdk.nashorn.internal.ir.WithNode;
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
import jdk.nashorn.internal.parser.Lexer;
import jdk.nashorn.internal.parser.TokenType;
/**
* This class translates from nashorn IR Node objects
* to nashorn parser API Tree objects.
*/
final class IRTranslator extends NodeVisitor<LexicalContext> {
public IRTranslator() {
super(new LexicalContext());
}
// currently translated Statement
private StatementTreeImpl curStat;
// currently translated Expression
private ExpressionTreeImpl curExpr;
// entry point for translator
CompilationUnitTree translate(final FunctionNode node) {
if (node == null) {
return null;
}
assert (node.getKind() == FunctionNode.Kind.SCRIPT) : "script function expected";
final Block body = node.getBody();
return new CompilationUnitTreeImpl(node,
translateStats(body != null? body.getStatements() : null));
}
@Override
public boolean enterAccessNode(final AccessNode accessNode) {
curExpr = new MemberSelectTreeImpl(accessNode, translateExpr(accessNode.getBase()));
return false;
}
@Override
public boolean enterBlock(final Block block) {
// FIXME: revisit this!
if (block.isSynthetic()) {
final int statCount = block.getStatementCount();
switch (statCount) {
case 0: {
final EmptyNode emptyNode = new EmptyNode(-1, block.getToken(), block.getFinish());
curStat = new EmptyStatementTreeImpl(emptyNode);
return false;
}
case 1: {
curStat = translateStat(block.getStatements().get(0));
return false;
}
}
}
curStat = new BlockTreeImpl(block,
translateStats(block.getStatements()));
return false;
}
@Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
if (binaryNode.isAssignment()) {
final ExpressionTree srcTree = translateExpr(binaryNode.getAssignmentSource());
final ExpressionTree destTree = translateExpr(binaryNode.getAssignmentDest());
if (binaryNode.tokenType() == TokenType.ASSIGN) {
curExpr = new AssignmentTreeImpl(binaryNode, destTree, srcTree);
} else {
curExpr = new CompoundAssignmentTreeImpl(binaryNode, destTree, srcTree);
}
} else {
final ExpressionTree leftTree = translateExpr(binaryNode.lhs());
final ExpressionTree rightTree = translateExpr(binaryNode.rhs());
if (binaryNode.tokenType() == TokenType.INSTANCEOF) {
curExpr = new InstanceOfTreeImpl(binaryNode, leftTree, rightTree);
} else {
curExpr = new BinaryTreeImpl(binaryNode, leftTree, rightTree);
}
}
return false;
}
@Override
public boolean enterBreakNode(final BreakNode breakNode) {
curStat = new BreakTreeImpl(breakNode);
return false;
}
@Override
public boolean enterCallNode(final CallNode callNode) {
curExpr = null;
callNode.getFunction().accept(this);
final ExpressionTree funcTree = curExpr;
final List<? extends ExpressionTree> argTrees = translateExprs(callNode.getArgs());
curExpr = new FunctionCallTreeImpl(callNode, funcTree, argTrees);
return false;
}
@Override
public boolean enterCaseNode(final CaseNode caseNode) {
assert false : "should not reach here!";
return false;
}
@Override
public boolean enterCatchNode(final CatchNode catchNode) {
assert false : "should not reach here";
return false;
}
@Override
public boolean enterContinueNode(final ContinueNode continueNode) {
curStat = new ContinueTreeImpl(continueNode);
return false;
}
@Override
public boolean enterDebuggerNode(final DebuggerNode debuggerNode) {
curStat = new DebuggerTreeImpl(debuggerNode);
return false;
}
@Override
public boolean enterEmptyNode(final EmptyNode emptyNode) {
curStat = new EmptyStatementTreeImpl(emptyNode);
return false;
}
@Override
public boolean enterErrorNode(final ErrorNode errorNode) {
curExpr = new ErroneousTreeImpl(errorNode);
return false;
}
@Override
public boolean enterExpressionStatement(final ExpressionStatement expressionStatement) {
curStat = new ExpressionStatementTreeImpl(expressionStatement,
translateExpr(expressionStatement.getExpression()));
return false;
}
@Override
public boolean enterBlockStatement(final BlockStatement blockStatement) {
final Block block = blockStatement.getBlock();
if (blockStatement.isSynthetic()) {
assert block != null && block.getStatements() != null && block.getStatements().size() == 1;
curStat = translateStat(block.getStatements().get(0));
} else {
curStat = new BlockTreeImpl(blockStatement,
translateStats(block != null? block.getStatements() : null));
}
return false;
}
@Override
public boolean enterForNode(final ForNode forNode) {
if (forNode.isForIn()) {
curStat = new ForInLoopTreeImpl(forNode,
translateExpr(forNode.getInit()),
translateExpr(forNode.getModify()),
translateBlock(forNode.getBody()));
} else {
curStat = new ForLoopTreeImpl(forNode,
translateExpr(forNode.getInit()),
translateExpr(forNode.getTest()),
translateExpr(forNode.getModify()),
translateBlock(forNode.getBody()));
}
return false;
}
@Override
public boolean enterFunctionNode(final FunctionNode functionNode) {
assert !functionNode.isDeclared() : "should not reach here for function declaration";
final List<? extends ExpressionTree> paramTrees
= translateExprs(functionNode.getParameters());
final BlockTree blockTree = (BlockTree) translateBlock(functionNode.getBody());
curExpr = new FunctionExpressionTreeImpl(functionNode, paramTrees, blockTree);
return false;
}
@Override
public boolean enterIdentNode(final IdentNode identNode) {
curExpr = new IdentifierTreeImpl(identNode);
return false;
}
@Override
public boolean enterIfNode(final IfNode ifNode) {
curStat = new IfTreeImpl(ifNode,
translateExpr(ifNode.getTest()),
translateBlock(ifNode.getPass()),
translateBlock(ifNode.getFail()));
return false;
}
@Override
public boolean enterIndexNode(final IndexNode indexNode) {
curExpr = new ArrayAccessTreeImpl(indexNode,
translateExpr(indexNode.getBase()),
translateExpr(indexNode.getIndex()));
return false;
}
@Override
public boolean enterLabelNode(final LabelNode labelNode) {
curStat = new LabeledStatementTreeImpl(labelNode,
translateBlock(labelNode.getBody()));
return false;
}
@Override
public boolean enterLiteralNode(final LiteralNode<?> literalNode) {
final Object value = literalNode.getValue();
if (value instanceof Lexer.RegexToken) {
curExpr = new RegExpLiteralTreeImpl(literalNode);
} else if (literalNode.isArray()) {
final List<Expression> exprNodes = literalNode.getElementExpressions();
final List<ExpressionTreeImpl> exprTrees = new ArrayList<>(exprNodes.size());
for (final Node node : exprNodes) {
if (node == null) {
exprTrees.add(null);
} else {
curExpr = null;
node.accept(this);
assert curExpr != null : "null for " + node;
exprTrees.add(curExpr);
}
}
curExpr = new ArrayLiteralTreeImpl(literalNode, exprTrees);
} else {
curExpr = new LiteralTreeImpl(literalNode);
}
return false;
}
@Override
public boolean enterObjectNode(final ObjectNode objectNode) {
final List<PropertyNode> propNodes = objectNode.getElements();
final List<PropertyTreeImpl> propTrees = new ArrayList<>(propNodes.size());
for (final PropertyNode propNode : propNodes) {
propTrees.add(new PropertyTreeImpl(propNode,
translateExpr(propNode.getKey()),
translateExpr(propNode.getValue()),
(FunctionExpressionTree) translateExpr(propNode.getGetter()),
(FunctionExpressionTree) translateExpr(propNode.getSetter())));
}
curExpr = new ObjectLiteralTreeImpl(objectNode, propTrees);
return false;
}
@Override
public boolean enterPropertyNode(final PropertyNode propertyNode) {
assert false : "should not reach here!";
return false;
}
@Override
public boolean enterReturnNode(final ReturnNode returnNode) {
curStat = new ReturnTreeImpl(returnNode,
translateExpr(returnNode.getExpression()));
return false;
}
@Override
public boolean enterRuntimeNode(final RuntimeNode runtimeNode) {
assert false : "should not reach here: RuntimeNode";
return false;
}
@Override
public boolean enterSplitNode(final SplitNode splitNode) {
assert false : "should not reach here!";
return false;
}
@Override
public boolean enterSwitchNode(final SwitchNode switchNode) {
final List<CaseNode> caseNodes = switchNode.getCases();
final List<CaseTreeImpl> caseTrees = new ArrayList<>(caseNodes.size());
for (final CaseNode caseNode : caseNodes) {
final Block body = caseNode.getBody();
caseTrees.add(
new CaseTreeImpl(caseNode,
translateExpr(caseNode.getTest()),
translateStats(body != null? body.getStatements() : null)));
}
curStat = new SwitchTreeImpl(switchNode,
translateExpr(switchNode.getExpression()),
caseTrees);
return false;
}
@Override
public boolean enterTernaryNode(final TernaryNode ternaryNode) {
curExpr = new ConditionalExpressionTreeImpl(ternaryNode,
translateExpr(ternaryNode.getTest()),
translateExpr(ternaryNode.getTrueExpression()),
translateExpr(ternaryNode.getFalseExpression()));
return false;
}
@Override
public boolean enterThrowNode(final ThrowNode throwNode) {
curStat = new ThrowTreeImpl(throwNode,
translateExpr(throwNode.getExpression()));
return false;
}
@Override
public boolean enterTryNode(final TryNode tryNode) {
final List<? extends CatchNode> catchNodes = tryNode.getCatches();
final List<CatchTreeImpl> catchTrees = new ArrayList<>(catchNodes.size());
for (final CatchNode catchNode : catchNodes) {
catchTrees.add(new CatchTreeImpl(catchNode,
translateIdent(catchNode.getException()),
(BlockTree) translateBlock(catchNode.getBody()),
translateExpr(catchNode.getExceptionCondition())));
}
curStat = new TryTreeImpl(tryNode,
(BlockTree) translateBlock(tryNode.getBody()),
catchTrees,
(BlockTree) translateBlock(tryNode.getFinallyBody()));
return false;
}
@Override
public boolean enterUnaryNode(final UnaryNode unaryNode) {
if (unaryNode.tokenType() == TokenType.NEW) {
curExpr = new NewTreeImpl(unaryNode,
translateExpr(unaryNode.getExpression()));
} else {
curExpr = new UnaryTreeImpl(unaryNode,
translateExpr(unaryNode.getExpression()));
}
return false;
}
@Override
public boolean enterVarNode(final VarNode varNode) {
final Expression initNode = varNode.getInit();
if (initNode instanceof FunctionNode && ((FunctionNode)initNode).isDeclared()) {
final FunctionNode funcNode = (FunctionNode) initNode;
final List<? extends ExpressionTree> paramTrees
= translateExprs(funcNode.getParameters());
final BlockTree blockTree = (BlockTree) translateBlock(funcNode.getBody());
curStat = new FunctionDeclarationTreeImpl(varNode, paramTrees, blockTree);
} else {
curStat = new VariableTreeImpl(varNode, translateExpr(initNode));
}
return false;
}
@Override
public boolean enterWhileNode(final WhileNode whileNode) {
final ExpressionTree condTree = translateExpr(whileNode.getTest());
final StatementTree statTree = translateBlock(whileNode.getBody());
if (whileNode.isDoWhile()) {
curStat = new DoWhileLoopTreeImpl(whileNode, condTree, statTree);
} else {
curStat = new WhileLoopTreeImpl(whileNode, condTree, statTree);
}
return false;
}
@Override
public boolean enterWithNode(final WithNode withNode) {
curStat = new WithTreeImpl(withNode,
translateExpr(withNode.getExpression()),
translateBlock(withNode.getBody()));
return false;
}
private StatementTree translateBlock(final Block blockNode) {
if (blockNode == null) {
return null;
}
curStat = null;
blockNode.accept(this);
return curStat;
}
private List<? extends StatementTree> translateStats(final List<? extends Statement> stats) {
if (stats == null) {
return null;
}
final List<StatementTreeImpl> statTrees = new ArrayList<>(stats.size());
for (final Statement stat : stats) {
curStat = null;
stat.accept(this);
assert curStat != null;
statTrees.add(curStat);
}
return statTrees;
}
private List<? extends ExpressionTree> translateExprs(final List<? extends Expression> exprs) {
if (exprs == null) {
return null;
}
final List<ExpressionTreeImpl> exprTrees = new ArrayList<>(exprs.size());
for (final Expression expr : exprs) {
curExpr = null;
expr.accept(this);
assert curExpr != null;
exprTrees.add(curExpr);
}
return exprTrees;
}
private ExpressionTreeImpl translateExpr(final Expression expr) {
if (expr == null) {
return null;
}
curExpr = null;
expr.accept(this);
assert curExpr != null : "null for " + expr;
return curExpr;
}
private StatementTreeImpl translateStat(final Statement stat) {
if (stat == null) {
return null;
}
curStat = null;
stat.accept(this);
assert curStat != null : "null for " + stat;
return curStat;
}
private IdentifierTree translateIdent(final IdentNode ident) {
return new IdentifierTreeImpl(ident);
}
}

@ -0,0 +1,46 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for an identifier expression.
*
* For example:
* <pre>
* <em>name</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface IdentifierTree extends ExpressionTree {
/**
* Returns the name of this identifier.
*
* @return the name of this identifier
*/
String getName();
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.IdentNode;
final class IdentifierTreeImpl extends ExpressionTreeImpl implements IdentifierTree {
private final String name;
IdentifierTreeImpl(final IdentNode node) {
super(node);
this.name = node.getName();
}
@Override
public Kind getKind() {
return Kind.IDENTIFIER;
}
@Override
public String getName() {
return name;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitIdentifier(this, data);
}
}

@ -0,0 +1,67 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for an 'if' statement.
*
* For example:
* <pre>
* if ( <em>condition</em> )
* <em>thenStatement</em>
*
* if ( <em>condition</em> )
* <em>thenStatement</em>
* else
* <em>elseStatement</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface IfTree extends StatementTree {
/**
* Returns the condition expression of this 'if' statement.
*
* @return the condition expression
*/
ExpressionTree getCondition();
/**
* Returns the 'then' statement of this 'if' statement.
*
* @return the 'then' statement
*/
StatementTree getThenStatement();
/**
* Returns the then statement of this 'if' statement.
* null if this if statement has no else branch.
*
* @return the 'else' statement
*/
StatementTree getElseStatement();
}

@ -0,0 +1,69 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.IfNode;
final class IfTreeImpl extends StatementTreeImpl implements IfTree {
private final ExpressionTree cond;
private final StatementTree thenStat;
private final StatementTree elseStat;
IfTreeImpl(final IfNode node, final ExpressionTree cond,
final StatementTree thenStat, final StatementTree elseStat) {
super(node);
this.cond = cond;
this.thenStat = thenStat;
this.elseStat = elseStat;
}
@Override
public Kind getKind() {
return Kind.IF;
}
@Override
public ExpressionTree getCondition() {
return cond;
}
@Override
public StatementTree getThenStatement() {
return thenStat;
}
/**
* @return null if this if statement has no else branch.
*/
@Override
public StatementTree getElseStatement() {
return elseStat;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitIf(this, data);
}
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for an 'instanceof' expression.
*
* For example:
* <pre>
* <em>expression</em> instanceof <em>type</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface InstanceOfTree extends ExpressionTree {
/**
* Returns the expression whose type is being checked.
*
* @return the expression whose type is being checked
*/
ExpressionTree getExpression();
/**
* Returns the type expression.
*
* @return the type expression
*/
Tree getType();
}

@ -0,0 +1,58 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.BinaryNode;
import jdk.nashorn.internal.parser.TokenType;
final class InstanceOfTreeImpl extends BinaryTreeImpl implements InstanceOfTree {
InstanceOfTreeImpl(final BinaryNode node,
final ExpressionTree expr,
final ExpressionTree type) {
super(node, expr, type);
assert node.tokenType() == TokenType.INSTANCEOF : "instanceof expected";
}
@Override
public Kind getKind() {
return Kind.INSTANCE_OF;
}
@Override
public ExpressionTree getExpression() {
return getLeftOperand();
}
@Override
public Tree getType() {
return getRightOperand();
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitInstanceOf(this, data);
}
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a labeled statement.
*
* For example:
* <pre>
* <em>label</em> : <em>statement</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface LabeledStatementTree extends StatementTree {
/**
* Returns the label associated with this statement.
*
* @return the label
*/
String getLabel();
/**
* Returns the statement being labeled.
*
* @return the statement labeled
*/
StatementTree getStatement();
}

@ -0,0 +1,60 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.LabelNode;
final class LabeledStatementTreeImpl extends StatementTreeImpl
implements LabeledStatementTree {
private final String name;
private final StatementTree stat;
LabeledStatementTreeImpl(final LabelNode node, final StatementTree stat) {
super(node);
this.name = node.getLabelName();
this.stat = stat;
}
@Override
public Kind getKind() {
return Kind.LABELED_STATEMENT;
}
@Override
public String getLabel() {
return name;
}
@Override
public StatementTree getStatement() {
return stat;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitLabeledStatement(this, data);
}
}

@ -0,0 +1,54 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* Provides methods to convert between character positions and line numbers
* for a compilation unit.
*
* @since 1.9
*/
@jdk.Exported
public interface LineMap {
/**
* Find the line containing a position; a line termination
* character is on the line it terminates.
*
* @param pos character offset of the position
* @return the line number of pos (first line is 1)
*/
long getLineNumber(long pos);
/**
* Find the column for a character position.
* Tab characters preceding the position on the same line
* will be expanded when calculating the column number.
*
* @param pos character offset of the position
* @return the tab-expanded column number of pos (first column is 1)
*/
long getColumnNumber(long pos);
}

@ -0,0 +1,46 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.runtime.Source;
final class LineMapImpl implements LineMap {
private final Source source;
LineMapImpl(final Source source) {
this.source = source;
}
@Override
public long getLineNumber(long pos) {
return source.getLine((int)pos);
}
@Override
public long getColumnNumber(long pos) {
return source.getColumn((int)pos);
}
}

@ -0,0 +1,47 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a literal expression.
* Use {@link #getKind getKind} to determine the kind of literal.
*
* For example:
* <pre>
* <em>value</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface LiteralTree extends ExpressionTree {
/**
* Returns the value of this literal.
*
* @return the value of this literal
*/
Object getValue();
}

@ -0,0 +1,67 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.LiteralNode;
final class LiteralTreeImpl extends ExpressionTreeImpl implements LiteralTree {
private final Object value;
private final Kind kind;
LiteralTreeImpl(final LiteralNode<?> node) {
super(node);
this.kind = literalKind(node);
this.value = node.getValue();
}
@Override
public Kind getKind() {
return kind;
}
@Override
public Object getValue() {
return value;
}
private static Kind literalKind(final LiteralNode<?> node) {
if (node.isBoolean()) {
return Kind.BOOLEAN_LITERAL;
} else if (node.isNumeric()) {
return Kind.NUMBER_LITERAL;
} else if (node.isString()) {
return Kind.STRING_LITERAL;
} else if (node.isNull()) {
return Kind.NULL_LITERAL;
} else {
throw new AssertionError("should not reach here: " + node.getValue());
}
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitLiteral(this, data);
}
}

@ -0,0 +1,41 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A mixin for "loop" statements.
*
* @since 1.9
*/
@jdk.Exported
public interface LoopTree extends StatementTree {
/**
* Returns the statement contained in this 'loop' statement.
*
* @return the statement
*/
StatementTree getStatement();
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a member access expression.
*
* For example:
* <pre>
* <em>expression</em> . <em>identifier</em>
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface MemberSelectTree extends ExpressionTree {
/**
* The object expression whose member is being selected.
*
* @return the object whose member is selected
*/
ExpressionTree getExpression();
/**
* Returns the name of the property.
*
* @return the name of the property
*/
String getIdentifier();
}

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.AccessNode;
final class MemberSelectTreeImpl extends ExpressionTreeImpl
implements MemberSelectTree {
private final String ident;
private final ExpressionTree expr;
MemberSelectTreeImpl(final AccessNode node, final ExpressionTree expr) {
super(node);
this.ident = node.getProperty();
this.expr = expr;
}
@Override
public Kind getKind() {
return Kind.MEMBER_SELECT;
}
@Override
public ExpressionTree getExpression() {
return expr;
}
@Override
public String getIdentifier() {
return ident;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitMemberSelect(this, data);
}
}

@ -0,0 +1,48 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node to declare a new instance of a class.
*
* For example:
* <pre>
* new <em>identifier</em> ( )
*
* new <em>identifier</em> ( <em>arguments</em> )
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface NewTree extends ExpressionTree {
/**
* Returns the constructor expression of this 'new' expression.
*
* @return the constructor expression of this 'new' expression
*/
ExpressionTree getConstructorExpression();
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.UnaryNode;
import jdk.nashorn.internal.parser.TokenType;
final class NewTreeImpl extends ExpressionTreeImpl implements NewTree {
private final ExpressionTree constrExpr;
NewTreeImpl(final UnaryNode node, final ExpressionTree constrExpr) {
super(node);
assert (node.tokenType() == TokenType.NEW) : "new expected";
this.constrExpr = constrExpr;
}
@Override
public Kind getKind() {
return Kind.NEW;
}
@Override
public ExpressionTree getConstructorExpression() {
return constrExpr;
}
@Override
public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
return visitor.visitNew(this, data);
}
}

@ -0,0 +1,43 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* Represents ECMAScript object literal expression.
*
* @since 1.9
*/
@jdk.Exported
public interface ObjectLiteralTree extends ExpressionTree {
/**
* Returns the list of properties of this object literal.
*
* @return the list of properties of this object literal
*/
public List<? extends PropertyTree> getProperties();
}

@ -0,0 +1,53 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
import jdk.nashorn.internal.ir.ObjectNode;
final class ObjectLiteralTreeImpl extends ExpressionTreeImpl
implements ObjectLiteralTree {
private final List<? extends PropertyTree> props;
ObjectLiteralTreeImpl(final ObjectNode node, final List<? extends PropertyTree> props) {
super(node);
this.props = props;
}
@Override
public Kind getKind() {
return Kind.OBJECT_LITERAL;
}
@Override
public List<? extends PropertyTree> getProperties() {
return props;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitObjectLiteral(this, data);
}
}

@ -0,0 +1,47 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a parenthesized expression. Note: parentheses
* not be preserved by the parser.
*
* For example:
* <pre>
* ( <em>expression</em> )
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ParenthesizedTree extends ExpressionTree {
/**
* Returns the expression within the parenthesis.
*
* @return the expression within the parenthesis
*/
ExpressionTree getExpression();
}

@ -0,0 +1,160 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.nio.file.Path;
import jdk.nashorn.api.scripting.NashornException;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
/**
* Represents nashorn ECMAScript parser instance.
*
* @since 1.9
*/
@jdk.Exported
public interface Parser {
/**
* Parses the source file and returns compilation unit tree
*
* @param file source file to parse
* @param listener to receive diagnostic messages from the parser. This can be null.
* if null is passed, a NashornException is thrown on the first parse error.
* @return compilation unit tree
* @throws NullPointerException if file is null
* @throws IOException if parse source read fails
* @throws NashornException is thrown if no listener is supplied and parser encounters error
*/
public CompilationUnitTree parse(final File file, final DiagnosticListener listener) throws IOException, NashornException;
/**
* Parses the source Path and returns compilation unit tree
*
* @param path source Path to parse
* @param listener to receive diagnostic messages from the parser. This can be null.
* if null is passed, a NashornException is thrown on the first parse error.
* @return compilation unit tree
* @throws NullPointerException if path is null
* @throws IOException if parse source read fails
* @throws NashornException is thrown if no listener is supplied and parser encounters error
*/
public CompilationUnitTree parse(final Path path, final DiagnosticListener listener) throws IOException, NashornException;
/**
* Parses the source url and returns compilation unit tree
*
* @param url source file to parse
* @param listener to receive diagnostic messages from the parser. This can be null.
* if null is passed, a NashornException is thrown on the first parse error.
* @return compilation unit tree
* @throws NullPointerException if url is null
* @throws IOException if parse source read fails
* @throws NashornException is thrown if no listener is supplied and parser encounters error
*/
public CompilationUnitTree parse(final URL url, final DiagnosticListener listener) throws IOException, NashornException;
/**
* Parses the readerand returns compilation unit tree
*
* @param name name of the source file to parse
* @param reader from which source is read
* @param listener to receive diagnostic messages from the parser. This can be null.
* if null is passed, a NashornException is thrown on the first parse error.
* @return compilation unit tree
* @throws NullPointerException if name or reader is null
* @throws IOException if parse source read fails
* @throws NashornException is thrown if no listener is supplied and parser encounters error
*/
public CompilationUnitTree parse(final String name, Reader reader, final DiagnosticListener listener) throws IOException, NashornException;
/**
* Parses the string source and returns compilation unit tree
*
* @param name of the source
* @param code string source
* @param listener to receive diagnostic messages from the parser. This can be null.
* if null is passed, a NashornException is thrown on the first parse error.
* @return compilation unit tree
* @throws NullPointerException if name or code is null
* @throws NashornException is thrown if no listener is supplied and parser encounters error
*/
public CompilationUnitTree parse(final String name, String code, final DiagnosticListener listener) throws NashornException;
/**
* Parses the source from script object and returns compilation unit tree
*
* @param scriptObj script object whose script and name properties are used for script source
* @param listener to receive diagnostic messages from the parser. This can be null.
* if null is passed, a NashornException is thrown on the first parse error.
* @return compilation unit tree
* @throws NullPointerException if scriptObj is null
* @throws NashornException is thrown if no listener is supplied and parser encounters error
*/
public CompilationUnitTree parse(final ScriptObjectMirror scriptObj, final DiagnosticListener listener) throws NashornException;
/**
* Factory method to create a new instance of Parser.
*
* @param options configuration options to initialize the Parser.
* Currently the following options are supported:
*
* <dl>
* <dt>"--const-as-var"</dt><dd>treat "const" declaration as "var"</dd>
* <dt>"-dump-on-error" or "-doe"</dt><dd>dump stack trace on error</dd>
* <dt>"--empty-statements"</dt><dd>include empty statement nodes</dd>
* <dt>"--no-syntax-extensions" or "-nse"</dt><dd>disable ECMAScript syntax extensions</dd>
* <dt>"-scripting"</dt><dd>enable scripting mode extensions</dd>
* <dt>"-strict"</dt><dd>enable ECMAScript strict mode</dd>
* </dl>
*
* @throws NullPointerException if options arrry or any of it's element is null
* @throws IllegalArgumentException on unsupported option value.
* @return a new Parser instance.
*/
public static Parser create(final String... options) throws IllegalArgumentException {
options.getClass();
for (String opt : options) {
switch (opt) {
case "--const-as-var":
case "-dump-on-error":
case "-doe":
case "--empty-statements":
case "--no-syntax-extensions":
case "-nse":
case "-scripting":
case "-strict":
break;
default:
throw new IllegalArgumentException(opt);
}
}
return new ParserImpl(options);
}
}

@ -0,0 +1,145 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
import jdk.nashorn.api.scripting.NashornException;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.internal.ir.FunctionNode;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.ErrorManager;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.ParserException;
import jdk.nashorn.internal.runtime.ScriptEnvironment;
import jdk.nashorn.internal.runtime.Source;
import jdk.nashorn.internal.runtime.options.Options;
final class ParserImpl implements Parser {
private final ScriptEnvironment env;
ParserImpl(final String... args) throws IllegalArgumentException {
Objects.requireNonNull(args);
Options options = new Options("nashorn");
options.process(args);
this.env = new ScriptEnvironment(options,
new PrintWriter(System.out), new PrintWriter(System.err));
}
@Override
public CompilationUnitTree parse(final File file, final DiagnosticListener listener) throws IOException, NashornException {
Objects.requireNonNull(file);
final Source src = Source.sourceFor(file.getName(), file);
return translate(makeParser(src, listener).parse());
}
@Override
public CompilationUnitTree parse(final Path path, final DiagnosticListener listener) throws IOException, NashornException {
Objects.requireNonNull(path);
final Source src = Source.sourceFor(path.toString(), path);
return translate(makeParser(src, listener).parse());
}
@Override
public CompilationUnitTree parse(final URL url, final DiagnosticListener listener) throws IOException, NashornException {
final Source src = Source.sourceFor(url.toString(), url);
return translate(makeParser(src, listener).parse());
}
@Override
public CompilationUnitTree parse(final String name, final Reader reader, final DiagnosticListener listener) throws IOException, NashornException {
Objects.requireNonNull(name);
Objects.requireNonNull(reader);
final Source src = Source.sourceFor(name, reader);
return translate(makeParser(src, listener).parse());
}
@Override
public CompilationUnitTree parse(final String name, final String code, final DiagnosticListener listener) throws NashornException {
final Source src = Source.sourceFor(name, code);
return translate(makeParser(src, listener).parse());
}
@Override
public CompilationUnitTree parse(final ScriptObjectMirror scriptObj, final DiagnosticListener listener) throws NashornException {
Objects.requireNonNull(scriptObj);
final Map<?,?> map = scriptObj;
if (map.containsKey("script") && map.containsKey("name")) {
final String script = JSType.toString(map.get("script"));
final String name = JSType.toString(map.get("name"));
final Source src = Source.sourceFor(name, script);
return translate(makeParser(src, listener).parse());
} else {
throw new IllegalArgumentException("can't find 'script' and 'name' properties");
}
}
private jdk.nashorn.internal.parser.Parser makeParser(final Source source, final DiagnosticListener listener) {
final ErrorManager errMgr = listener != null? new ListenerErrorManager(listener) : new Context.ThrowErrorManager();
return new jdk.nashorn.internal.parser.Parser(env, source, errMgr);
}
private static class ListenerErrorManager extends ErrorManager {
private final DiagnosticListener listener;
ListenerErrorManager(final DiagnosticListener listener) {
// null check
listener.getClass();
this.listener = listener;
}
@Override
public void error(final String msg) {
error(new ParserException(msg));
}
@Override
public void error(final ParserException e) {
listener.report(new DiagnosticImpl(e, Diagnostic.Kind.ERROR));
}
@Override
public void warning(final String msg) {
warning(new ParserException(msg));
}
@Override
public void warning(final ParserException e) {
listener.report(new DiagnosticImpl(e, Diagnostic.Kind.WARNING));
}
}
private CompilationUnitTree translate(final FunctionNode node) {
return new IRTranslator().translate(node);
}
}

@ -0,0 +1,64 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* To represent property setting in an object literal tree.
*
* @since 1.9
*/
@jdk.Exported
public interface PropertyTree extends Tree {
/**
* Returns the name of this property.
*
* @return the name of the property
*/
public ExpressionTree getKey();
/**
* Returns the value of this property. This is null for accessor properties.
*
* @return the value of the property
*/
public ExpressionTree getValue();
/**
* Returns the setter function of this property if this
* is an accessor property. This is null for data properties.
*
* @return the setter function of the property
*/
public FunctionExpressionTree getGetter();
/**
* Returns the getter function of this property if this
* is an accessor property. This is null for data properties.
*
* @return the getter function of the property
*/
public FunctionExpressionTree getSetter();
}

@ -0,0 +1,76 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.PropertyNode;
final class PropertyTreeImpl extends TreeImpl implements PropertyTree {
private final ExpressionTree key;
private final ExpressionTree value;
private final FunctionExpressionTree getter;
private final FunctionExpressionTree setter;
PropertyTreeImpl(final PropertyNode node,
final ExpressionTree key,
final ExpressionTree value,
final FunctionExpressionTree getter,
final FunctionExpressionTree setter) {
super(node);
this.key = key;
this.value = value;
this.getter = getter;
this.setter = setter;
}
@Override
public Kind getKind() {
return Kind.PROPERTY;
}
@Override
public ExpressionTree getKey() {
return key;
}
@Override
public ExpressionTree getValue() {
return value;
}
@Override
public FunctionExpressionTree getGetter() {
return getter;
}
@Override
public FunctionExpressionTree getSetter() {
return setter;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitProperty(this, data);
}
}

@ -0,0 +1,48 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* Represents regular expression literal in the source code.
*
* @since 1.9
*/
@jdk.Exported
public interface RegExpLiteralTree extends Tree {
/**
* Regular expression pattern to match.
*
* @return regular expression patten
*/
public String getPattern();
/**
* Regular expression matching options.
*
* @return options like "i" for ignoreCase used
*/
public String getOptions();
}

@ -0,0 +1,62 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.LiteralNode;
import jdk.nashorn.internal.parser.Lexer;
final class RegExpLiteralTreeImpl extends ExpressionTreeImpl
implements RegExpLiteralTree {
private final String pattern;
private final String options;
RegExpLiteralTreeImpl(final LiteralNode<?> node) {
super(node);
assert node.getValue() instanceof Lexer.RegexToken : "regexp expected";
final Lexer.RegexToken regex = (Lexer.RegexToken) node.getValue();
this.pattern = regex.getExpression();
this.options = regex.getOptions();
}
@Override
public Kind getKind() {
return Kind.REGEXP_LITERAL;
}
@Override
public String getPattern() {
return pattern;
}
@Override
public String getOptions() {
return options;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitRegExpLiteral(this, data);
}
}

@ -0,0 +1,48 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node for a 'return' statement.
*
* For example:
* <pre>
* return;
* return <em>expression</em>;
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface ReturnTree extends StatementTree {
/**
* Returns the expression being returned. This is null if no value
* is being returned. i.e., empty return statement.
*
* @return the returned expression
*/
ExpressionTree getExpression();
}

@ -0,0 +1,52 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.ReturnNode;
final class ReturnTreeImpl extends StatementTreeImpl implements ReturnTree {
private final ExpressionTree expr;
ReturnTreeImpl(final ReturnNode returnNode, final ExpressionTree expr) {
super(returnNode);
this.expr = expr;
}
@Override
public Tree.Kind getKind() {
return Tree.Kind.RETURN;
}
@Override
public ExpressionTree getExpression() {
return expr;
}
@Override
public <R,D> R accept(TreeVisitor<R,D> visitor, D data) {
return visitor.visitReturn(this, data);
}
}

@ -0,0 +1,390 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A simple implementation of the TreeVisitor for ECMAScript edition 5.1.
*
* <p>The visit methods corresponding to ES 5.1 language constructs walk the
* "components" of the given tree by calling accept method passing the
* current visitor and the additional parameter.
*
* <p>For constructs introduced in later versions, {@code visitUnknown}
* is called instead which throws {@link UnknownTreeException}.
*
* <p> Methods in this class may be overridden subject to their
* general contract. Note that annotating methods in concrete
* subclasses with {@link java.lang.Override @Override} will help
* ensure that methods are overridden as intended.
*
* @param <R> the return type of this visitor's methods. Use {@link
* Void} for visitors that do not need to return results.
* @param <P> the type of the additional parameter to this visitor's
* methods. Use {@code Void} for visitors that do not need an
* additional parameter.
*/
public class SimpleTreeVisitorES5_1<R, P> implements TreeVisitor<R, P> {
@Override
public R visitAssignment(AssignmentTree node, P r) {
node.getVariable().accept(this, r);
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitCompoundAssignment(CompoundAssignmentTree node, P r) {
node.getVariable().accept(this, r);
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitBinary(BinaryTree node, P r) {
node.getLeftOperand().accept(this, r);
node.getRightOperand().accept(this, r);
return null;
}
@Override
public R visitBlock(BlockTree node, P r) {
node.getStatements().forEach((tree) -> {
tree.accept(this, r);
});
return null;
}
@Override
public R visitBreak(BreakTree node, P r) {
return null;
}
@Override
public R visitCase(CaseTree node, P r) {
final Tree caseVal = node.getExpression();
if (caseVal != null) {
caseVal.accept(this, r);
}
node.getStatements().forEach((tree) -> {
tree.accept(this, r);
});
return null;
}
@Override
public R visitCatch(CatchTree node, P r) {
final Tree cond = node.getCondition();
if (cond != null) {
cond.accept(this, r);
}
node.getParameter().accept(this, r);
node.getBlock().accept(this, r);
return null;
}
@Override
public R visitConditionalExpression(ConditionalExpressionTree node, P r) {
node.getCondition().accept(this, r);
node.getTrueExpression().accept(this, r);
node.getFalseExpression().accept(this, r);
return null;
}
@Override
public R visitContinue(ContinueTree node, P r) {
return null;
}
@Override
public R visitDebugger(DebuggerTree node, P r) {
return null;
}
@Override
public R visitDoWhileLoop(DoWhileLoopTree node, P r) {
node.getStatement().accept(this, r);
node.getCondition().accept(this, r);
return null;
}
@Override
public R visitErroneous(ErroneousTree node, P r) {
return null;
}
@Override
public R visitExpressionStatement(ExpressionStatementTree node, P r) {
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitForLoop(ForLoopTree node, P r) {
final Tree init = node.getInitializer();
if (init != null) {
init.accept(this, r);
}
final Tree cond = node.getCondition();
if (cond != null) {
cond.accept(this, r);
}
final Tree update = node.getUpdate();
if (update != null) {
update.accept(this, r);
}
node.getStatement().accept(this, r);
return null;
}
@Override
public R visitForInLoop(ForInLoopTree node, P r) {
node.getVariable().accept(this, r);
node.getExpression().accept(this, r);
final StatementTree stat = node.getStatement();
if (stat != null) {
stat.accept(this, r);
}
return null;
}
@Override
public R visitFunctionCall(FunctionCallTree node, P r) {
node.getFunctionSelect().accept(this, r);
node.getArguments().forEach((tree) -> {
tree.accept(this, r);
});
return null;
}
@Override
public R visitFunctionDeclaration(FunctionDeclarationTree node, P r) {
node.getParameters().forEach((tree) -> {
tree.accept(this, r);
});
node.getBody().accept(this, r);
return null;
}
@Override
public R visitFunctionExpression(FunctionExpressionTree node, P r) {
node.getParameters().forEach((tree) -> {
tree.accept(this, r);
});
node.getBody().accept(this, r);
return null;
}
@Override
public R visitIdentifier(IdentifierTree node, P r) {
return null;
}
@Override
public R visitIf(IfTree node, P r) {
node.getCondition().accept(this, r);
node.getThenStatement().accept(this, r);
final Tree elseStat = node.getElseStatement();
if (elseStat != null) {
elseStat.accept(this, r);
}
return null;
}
@Override
public R visitArrayAccess(ArrayAccessTree node, P r) {
node.getExpression().accept(this, r);
node.getIndex().accept(this, r);
return null;
}
@Override
public R visitArrayLiteral(ArrayLiteralTree node, P r) {
node.getElements().stream().filter((tree) -> (tree != null)).forEach((tree) -> {
tree.accept(this, r);
});
return null;
}
@Override
public R visitLabeledStatement(LabeledStatementTree node, P r) {
node.getStatement().accept(this, r);
return null;
}
@Override
public R visitLiteral(LiteralTree node, P r) {
return null;
}
@Override
public R visitParenthesized(ParenthesizedTree node, P r) {
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitReturn(ReturnTree node, P r) {
final Tree retExpr = node.getExpression();
if (retExpr != null) {
retExpr.accept(this, r);
}
return null;
}
@Override
public R visitMemberSelect(MemberSelectTree node, P r) {
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitNew(NewTree node, P r) {
node.getConstructorExpression().accept(this, r);
return null;
}
@Override
public R visitObjectLiteral(ObjectLiteralTree node, P r) {
node.getProperties().forEach((tree) -> {
tree.accept(this, r);
});
return null;
}
@Override
public R visitProperty(PropertyTree node, P r) {
FunctionExpressionTree getter = node.getGetter();
if (getter != null) {
getter.accept(this, r);
}
ExpressionTree key = node.getKey();
if (key != null) {
key.accept(this, r);
}
FunctionExpressionTree setter = node.getSetter();
if (setter != null) {
setter.accept(this, r);
}
ExpressionTree value = node.getValue();
if (value != null) {
value.accept(this, r);
}
return null;
}
@Override
public R visitRegExpLiteral(RegExpLiteralTree node, P r) {
return null;
}
@Override
public R visitEmptyStatement(EmptyStatementTree node, P r) {
return null;
}
@Override
public R visitSwitch(SwitchTree node, P r) {
node.getExpression().accept(this, r);
node.getCases().forEach((tree) -> {
tree.accept(this, r);
});
return null;
}
@Override
public R visitThrow(ThrowTree node, P r) {
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitCompilationUnit(CompilationUnitTree node, P r) {
node.getSourceElements().forEach((tree) -> {
tree.accept(this, r);
});
return null;
}
@Override
public R visitTry(TryTree node, P r) {
node.getBlock().accept(this, r);
node.getCatches().forEach((tree) -> {
tree.accept(this, r);
});
final Tree finallyBlock = node.getFinallyBlock();
if (finallyBlock != null) {
finallyBlock.accept(this, r);
}
return null;
}
@Override
public R visitInstanceOf(InstanceOfTree node, P r) {
node.getType().accept(this, r);
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitUnary(UnaryTree node, P r) {
node.getExpression().accept(this, r);
return null;
}
@Override
public R visitVariable(VariableTree node, P r) {
if (node.getInitializer() != null) {
node.getInitializer().accept(this, r);
}
return null;
}
@Override
public R visitWhileLoop(WhileLoopTree node, P r) {
node.getCondition().accept(this, r);
node.getStatement().accept(this, r);
return null;
}
@Override
public R visitWith(WithTree node, P r) {
node.getScope().accept(this, r);
node.getStatement().accept(this, r);
return null;
}
@Override
public R visitUnknown(Tree node, P r) {
// unknown in ECMAScript 5.1 edition
throw new UnknownTreeException(node, r);
}
}

@ -0,0 +1,36 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
/**
* A tree node used as the base class for the different kinds of
* statements.
*
* @since 1.9
*/
@jdk.Exported
public interface StatementTree extends Tree {
}

@ -0,0 +1,39 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.Block;
import jdk.nashorn.internal.ir.Statement;
abstract class StatementTreeImpl extends TreeImpl implements StatementTree {
StatementTreeImpl(final Statement stat) {
super(stat);
}
StatementTreeImpl(final Block stat) {
super(stat);
}
}

@ -0,0 +1,58 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.nashorn.api.tree;
import java.util.List;
/**
* A tree node for a 'switch' statement.
*
* For example:
* <pre>
* switch ( <em>expression</em> ) {
* <em>cases</em>
* }
* </pre>
*
* @since 1.9
*/
@jdk.Exported
public interface SwitchTree extends StatementTree {
/**
* Returns the expression on which this statement switches.
*
* @return the switch expression
*/
ExpressionTree getExpression();
/**
* Returns the list of 'case' statements.
*
* @return the 'case' statements
*/
List<? extends CaseTree> getCases();
}

Some files were not shown because too many files have changed in this diff Show More