+ * for ( variable of expression ) + * statement + *+ * + * @since 9 + */ +public interface ForOfLoopTree 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 + */ + @Override + StatementTree getStatement(); +} + diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/ForOfLoopTreeImpl.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/ForOfLoopTreeImpl.java new file mode 100644 index 00000000000..e2306e51c1e --- /dev/null +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/ForOfLoopTreeImpl.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2016, 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 ForOfLoopTreeImpl extends StatementTreeImpl implements ForOfLoopTree { + private final ExpressionTree lhsExpr; + private final ExpressionTree expr; + private final StatementTree stat; + + ForOfLoopTreeImpl(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; + } + + @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
@@ -37,6 +37,12 @@ import java.util.List; * body ** + *
+ * function* name + * ( parameters ) + * body + *+ * * @since 9 */ public interface FunctionDeclarationTree extends StatementTree { @@ -45,7 +51,7 @@ public interface FunctionDeclarationTree extends StatementTree { * * @return name the function declared */ - String getName(); + IdentifierTree getName(); /** * Returns the parameters of this function. @@ -67,4 +73,11 @@ public interface FunctionDeclarationTree extends StatementTree { * @return true if this function is strict */ boolean isStrict(); + + /** + * Is this a generator function? + * + * @return true if this is a generator function + */ + boolean isGenerator(); } diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionDeclarationTreeImpl.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionDeclarationTreeImpl.java index dfd5af938f6..592e4dce9ea 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionDeclarationTreeImpl.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionDeclarationTreeImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 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 @@ -32,7 +32,7 @@ import jdk.nashorn.internal.ir.VarNode; final class FunctionDeclarationTreeImpl extends StatementTreeImpl implements FunctionDeclarationTree { private final FunctionNode funcNode; - private final String funcName; + private final IdentifierTree funcName; private final List extends ExpressionTree> params; private final BlockTree body; @@ -43,7 +43,7 @@ final class FunctionDeclarationTreeImpl extends StatementTreeImpl assert node.getInit() instanceof FunctionNode : "function expected"; funcNode = (FunctionNode)node.getInit(); assert funcNode.isDeclared() : "function declaration expected"; - funcName = funcNode.isAnonymous()? null : node.getName().getName(); + funcName = funcNode.isAnonymous()? null : new IdentifierTreeImpl(node.getName()); this.params = params; this.body = body; } @@ -54,7 +54,7 @@ final class FunctionDeclarationTreeImpl extends StatementTreeImpl } @Override - public String getName() { + public IdentifierTree getName() { return funcName; } @@ -73,6 +73,11 @@ final class FunctionDeclarationTreeImpl extends StatementTreeImpl return funcNode.isStrict(); } + @Override + public boolean isGenerator() { + return funcNode.getKind() == FunctionNode.Kind.GENERATOR; + } + @Override public
@@ -37,6 +37,10 @@ import java.util.List; * body ** + *
+ * var func = (x) => x+1 + *+ * * @since 9 */ public interface FunctionExpressionTree extends ExpressionTree { @@ -45,7 +49,7 @@ public interface FunctionExpressionTree extends ExpressionTree { * * @return name the function declared */ - String getName(); + IdentifierTree getName(); /** * Returns the parameters of this function. @@ -55,11 +59,13 @@ public interface FunctionExpressionTree extends ExpressionTree { List extends ExpressionTree> getParameters(); /** - * Returns the body of code of this function. + * Returns the body of this function. This may be a {@link BlockTree} when this + * function has a block body. This is an {@link ExpressionTree} when the function body + * is a concise expression as in an expression arrow, or in an expression closure. * - * @return the body of code + * @return the body of this function */ - BlockTree getBody(); + Tree getBody(); /** * Is this a strict function? @@ -67,4 +73,18 @@ public interface FunctionExpressionTree extends ExpressionTree { * @return true if this function is strict */ boolean isStrict(); + + /** + * Is this a arrow function? + * + * @return true if this is a arrow function + */ + boolean isArrow(); + + /** + * Is this a generator function? + * + * @return true if this is a generator function + */ + boolean isGenerator(); } diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionExpressionTreeImpl.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionExpressionTreeImpl.java index b39cfbdfeea..6681f258070 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionExpressionTreeImpl.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/FunctionExpressionTreeImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 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 @@ -31,9 +31,9 @@ import jdk.nashorn.internal.ir.FunctionNode; final class FunctionExpressionTreeImpl extends ExpressionTreeImpl implements FunctionExpressionTree { private final FunctionNode funcNode; - private final String funcName; + private final IdentifierTree funcName; private final List extends ExpressionTree> params; - private final BlockTree body; + private final Tree body; FunctionExpressionTreeImpl(final FunctionNode node, final List extends ExpressionTree> params, @@ -46,11 +46,17 @@ final class FunctionExpressionTreeImpl extends ExpressionTreeImpl if (node.isAnonymous() || kind == FunctionNode.Kind.GETTER || kind == FunctionNode.Kind.SETTER) { funcName = null; } else { - funcName = node.getIdent().getName(); + funcName = new IdentifierTreeImpl(node.getIdent()); } this.params = params; - this.body = body; + if (node.getFlag(FunctionNode.HAS_EXPRESSION_BODY)) { + StatementTree first = body.getStatements().get(0); + assert first instanceof ReturnTree : "consise func. expression should have a return statement"; + this.body = ((ReturnTree)first).getExpression(); + } else { + this.body = body; + } } @Override @@ -59,7 +65,7 @@ final class FunctionExpressionTreeImpl extends ExpressionTreeImpl } @Override - public String getName() { + public IdentifierTree getName() { return funcName; } @@ -69,7 +75,7 @@ final class FunctionExpressionTreeImpl extends ExpressionTreeImpl } @Override - public BlockTree getBody() { + public Tree getBody() { return body; } @@ -78,6 +84,16 @@ final class FunctionExpressionTreeImpl extends ExpressionTreeImpl return funcNode.isStrict(); } + @Override + public boolean isArrow() { + return funcNode.getKind() == FunctionNode.Kind.ARROW; + } + + @Override + public boolean isGenerator() { + return funcNode.getKind() == FunctionNode.Kind.GENERATOR; + } + @Override public
The visit methods corresponding to ES 6 language constructs walk the + * "components" of the given tree by calling accept method passing the + * current visitor and the additional parameter. + * + *
For constructs introduced in later versions, {@code visitUnknown} + * is called instead which throws {@link UnknownTreeException}. + * + *
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 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 SimpleTreeVisitorES6
+ * `This is a String with ${computed} values in it`
+ *
+ *
+ * @since 9
+ *
+ */
+public interface TemplateLiteralTree extends ExpressionTree {
+ /**
+ * Returns the list of expressions in this template string
+ *
+ * @return the list of expressions in this template string
+ */
+ List extends ExpressionTree> getExpressions();
+}
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/TemplateLiteralTreeImpl.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/TemplateLiteralTreeImpl.java
new file mode 100644
index 00000000000..e3be74fe929
--- /dev/null
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/TemplateLiteralTreeImpl.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016, 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.Expression;
+
+final class TemplateLiteralTreeImpl extends ExpressionTreeImpl
+ implements TemplateLiteralTree {
+
+ private final List extends ExpressionTree> expressions;
+
+ TemplateLiteralTreeImpl(Expression node, List extends ExpressionTree> expressions) {
+ super(node);
+ this.expressions = expressions;
+ }
+
+ @Override
+ public Kind getKind() {
+ return Kind.TEMPLATE_LITERAL;
+ }
+
+ @Override
+ public List extends ExpressionTree> getExpressions() {
+ return expressions;
+ }
+
+
+ @Override
+ public
- * var name initializer ;
+ * var name [ initializer ] ;
+ * var binding_pattern [ initializer ];
*
*
* @since 9
*/
public interface VariableTree extends StatementTree {
/**
- * Returns the name of this variable.
+ * Returns the binding of this declaration. This is an {@link IdentifierTree}
+ * for a binding identifier case (simple variable declaration).
+ * This is an {@link ObjectLiteralTree} or a {@link ArrayLiteralTree} for a
+ * destructuring declaration.
*
- * @return the name of this variable
+ * @return the binding expression of this declaration
*/
- String getName();
+ ExpressionTree getBinding();
/**
* Returns the initial value expression for this variable. This is
@@ -50,4 +54,18 @@ public interface VariableTree extends StatementTree {
* @return the initial value expression
*/
ExpressionTree getInitializer();
+
+ /**
+ * Is this a const declaration?
+ *
+ * @return true if this is a const declaration
+ */
+ boolean isConst();
+
+ /**
+ * Is this a let declaration?
+ *
+ * @return true if this is a let declaration
+ */
+ boolean isLet();
}
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/VariableTreeImpl.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/VariableTreeImpl.java
index ca56511f1f9..a1e6f6f7e89 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/VariableTreeImpl.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/VariableTreeImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 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
@@ -28,12 +28,12 @@ package jdk.nashorn.api.tree;
import jdk.nashorn.internal.ir.VarNode;
final class VariableTreeImpl extends StatementTreeImpl implements VariableTree {
- private final String name;
+ private final IdentifierTree ident;
private final ExpressionTree init;
- VariableTreeImpl(final VarNode node, final ExpressionTree init) {
+ VariableTreeImpl(final VarNode node, final IdentifierTree ident, final ExpressionTree init) {
super(node);
- this.name = node.getName().getName();
+ this.ident = ident;
this.init = init;
}
@@ -43,8 +43,8 @@ final class VariableTreeImpl extends StatementTreeImpl implements VariableTree {
}
@Override
- public String getName() {
- return name;
+ public ExpressionTree getBinding() {
+ return ident;
}
@Override
@@ -52,6 +52,16 @@ final class VariableTreeImpl extends StatementTreeImpl implements VariableTree {
return init;
}
+ @Override
+ public boolean isConst() {
+ return ((VarNode)node).isConst();
+ }
+
+ @Override
+ public boolean isLet() {
+ return ((VarNode)node).isLet();
+ }
+
@Override
public
+ * function* id(){
+ * var index = 0;
+ * while(index < 10)
+ * yield index++;
+ * }
+ *
+ *
+ * @since 9
+ */
+public interface YieldTree extends ExpressionTree {
+ /**
+ * Returns the expression that is yielded.
+ *
+ * @return The expression that is yielded.
+ */
+ ExpressionTree getExpression();
+
+ /**
+ * Is this a yield * expression in a generator function?
+ *
+ * For example:
+ *
+ * function* id(){
+ * yield 1;
+ * yield * anotherGeneratorFunc();
+ * yield 10;
+ * }
+ *
+ *
+ *
+ * @return true if this is a yield * expression
+ */
+ boolean isStar();
+}
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/YieldTreeImpl.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/YieldTreeImpl.java
new file mode 100644
index 00000000000..aea75d81a4c
--- /dev/null
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/YieldTreeImpl.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016, 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;
+import jdk.nashorn.internal.ir.UnaryNode;
+import jdk.nashorn.internal.parser.TokenType;
+
+final class YieldTreeImpl extends ExpressionTreeImpl
+ implements YieldTree {
+
+ private final ExpressionTree expr;
+
+ YieldTreeImpl(final Expression exprNode, final ExpressionTree expr) {
+ super(exprNode);
+ this.expr = expr;
+ }
+
+ @Override
+ public Kind getKind() {
+ return Kind.YIELD;
+ }
+
+ @Override
+ public ExpressionTree getExpression() {
+ return expr;
+ }
+
+ @Override
+ public boolean isStar() {
+ return ((UnaryNode) node).isTokenType(TokenType.YIELD_STAR);
+ }
+
+ @Override
+ public