src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/IRTranslator.java
author sundar
Mon, 22 Jan 2018 20:31:19 +0530
changeset 48653 89111a0e6355
parent 47216 71c04702a3d5
permissions -rw-r--r--
8195829: Parsing a nameless ES6 class results in a thrown NullPointerException. Reviewed-by: jlaskey, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     1
/*
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     4
 *
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    10
 *
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    15
 * accompanied this code).
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    16
 *
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    20
 *
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    23
 * questions.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    24
 */
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    25
package jdk.nashorn.api.tree;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    26
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    27
import java.util.ArrayList;
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
    28
import java.util.Comparator;
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    29
import java.util.List;
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
    30
import java.util.Map;
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    31
import jdk.nashorn.internal.ir.AccessNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    32
import jdk.nashorn.internal.ir.BinaryNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    33
import jdk.nashorn.internal.ir.Block;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    34
import jdk.nashorn.internal.ir.BlockStatement;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    35
import jdk.nashorn.internal.ir.BreakNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    36
import jdk.nashorn.internal.ir.CallNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    37
import jdk.nashorn.internal.ir.CaseNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    38
import jdk.nashorn.internal.ir.CatchNode;
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
    39
import jdk.nashorn.internal.ir.ClassNode;
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    40
import jdk.nashorn.internal.ir.ContinueNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    41
import jdk.nashorn.internal.ir.DebuggerNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    42
import jdk.nashorn.internal.ir.EmptyNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    43
import jdk.nashorn.internal.ir.ErrorNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    44
import jdk.nashorn.internal.ir.Expression;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    45
import jdk.nashorn.internal.ir.ExpressionStatement;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    46
import jdk.nashorn.internal.ir.ForNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    47
import jdk.nashorn.internal.ir.FunctionNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    48
import jdk.nashorn.internal.ir.IdentNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    49
import jdk.nashorn.internal.ir.IfNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    50
import jdk.nashorn.internal.ir.IndexNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    51
import jdk.nashorn.internal.ir.LabelNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    52
import jdk.nashorn.internal.ir.LiteralNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    53
import jdk.nashorn.internal.ir.Node;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    54
import jdk.nashorn.internal.ir.ObjectNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    55
import jdk.nashorn.internal.ir.PropertyNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    56
import jdk.nashorn.internal.ir.ReturnNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    57
import jdk.nashorn.internal.ir.RuntimeNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    58
import jdk.nashorn.internal.ir.SplitNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    59
import jdk.nashorn.internal.ir.Statement;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    60
import jdk.nashorn.internal.ir.SwitchNode;
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
    61
import jdk.nashorn.internal.ir.TemplateLiteral;
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    62
import jdk.nashorn.internal.ir.TernaryNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    63
import jdk.nashorn.internal.ir.ThrowNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    64
import jdk.nashorn.internal.ir.TryNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    65
import jdk.nashorn.internal.ir.UnaryNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    66
import jdk.nashorn.internal.ir.VarNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    67
import jdk.nashorn.internal.ir.WhileNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    68
import jdk.nashorn.internal.ir.WithNode;
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 31485
diff changeset
    69
import jdk.nashorn.internal.ir.visitor.SimpleNodeVisitor;
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    70
import jdk.nashorn.internal.parser.Lexer;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    71
import jdk.nashorn.internal.parser.TokenType;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    72
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    73
/**
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    74
 * This class translates from nashorn IR Node objects
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    75
 * to nashorn parser API Tree objects.
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    76
 */
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 31485
diff changeset
    77
final class IRTranslator extends SimpleNodeVisitor {
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    78
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    79
    public IRTranslator() {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    80
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    81
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    82
    // currently translated Statement
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    83
    private StatementTreeImpl curStat;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    84
    // currently translated Expression
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    85
    private ExpressionTreeImpl curExpr;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    86
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    87
    // entry point for translator
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    88
    CompilationUnitTree translate(final FunctionNode node) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    89
        if (node == null) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    90
            return null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    91
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    92
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
    93
        assert node.getKind() == FunctionNode.Kind.SCRIPT ||
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
    94
                node.getKind() == FunctionNode.Kind.MODULE :
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
    95
                "script or module function expected";
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    96
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    97
        final Block body = node.getBody();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
    98
        return new CompilationUnitTreeImpl(node,
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
    99
                translateStats(body != null? getOrderedStatements(body.getStatements()) : null),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   100
                translateModule(node));
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   101
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   102
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   103
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   104
    public boolean enterAccessNode(final AccessNode accessNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   105
        curExpr = new MemberSelectTreeImpl(accessNode, translateExpr(accessNode.getBase()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   106
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   107
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   108
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   109
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   110
    public boolean enterBlock(final Block block) {
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   111
        return handleBlock(block, false);
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   112
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   113
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   114
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   115
    public boolean enterBinaryNode(final BinaryNode binaryNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   116
        if (binaryNode.isAssignment()) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   117
            final ExpressionTree srcTree = translateExpr(binaryNode.getAssignmentSource());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   118
            final ExpressionTree destTree = translateExpr(binaryNode.getAssignmentDest());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   119
30392
dc4a419b2982 8079362: Enforce best practices for Node token API usage
attila
parents: 29539
diff changeset
   120
            if (binaryNode.isTokenType(TokenType.ASSIGN)) {
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   121
                curExpr = new AssignmentTreeImpl(binaryNode, destTree, srcTree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   122
            } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   123
                curExpr = new CompoundAssignmentTreeImpl(binaryNode, destTree, srcTree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   124
            }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   125
        } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   126
            final ExpressionTree leftTree = translateExpr(binaryNode.lhs());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   127
            final ExpressionTree rightTree = translateExpr(binaryNode.rhs());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   128
30392
dc4a419b2982 8079362: Enforce best practices for Node token API usage
attila
parents: 29539
diff changeset
   129
            if (binaryNode.isTokenType(TokenType.INSTANCEOF)) {
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   130
                curExpr = new InstanceOfTreeImpl(binaryNode, leftTree, rightTree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   131
            } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   132
                curExpr = new BinaryTreeImpl(binaryNode, leftTree, rightTree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   133
            }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   134
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   135
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   136
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   137
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   138
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   139
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   140
    public boolean enterBreakNode(final BreakNode breakNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   141
        curStat = new BreakTreeImpl(breakNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   142
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   143
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   144
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   145
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   146
    public boolean enterCallNode(final CallNode callNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   147
        curExpr = null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   148
        callNode.getFunction().accept(this);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   149
        final ExpressionTree funcTree = curExpr;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   150
        final List<? extends ExpressionTree> argTrees = translateExprs(callNode.getArgs());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   151
        curExpr = new FunctionCallTreeImpl(callNode, funcTree, argTrees);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   152
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   153
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   154
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   155
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   156
    public boolean enterCaseNode(final CaseNode caseNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   157
        assert false : "should not reach here!";
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   158
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   159
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   160
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   161
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   162
    public boolean enterCatchNode(final CatchNode catchNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   163
        assert false : "should not reach here";
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   164
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   165
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   166
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   167
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   168
    public boolean enterContinueNode(final ContinueNode continueNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   169
        curStat = new ContinueTreeImpl(continueNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   170
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   171
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   172
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   173
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   174
    public boolean enterDebuggerNode(final DebuggerNode debuggerNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   175
        curStat = new DebuggerTreeImpl(debuggerNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   176
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   177
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   178
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   179
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   180
    public boolean enterEmptyNode(final EmptyNode emptyNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   181
        curStat = new EmptyStatementTreeImpl(emptyNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   182
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   183
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   184
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   185
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   186
    public boolean enterErrorNode(final ErrorNode errorNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   187
        curExpr = new ErroneousTreeImpl(errorNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   188
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   189
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   190
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   191
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   192
    public boolean enterExpressionStatement(final ExpressionStatement expressionStatement) {
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   193
        if (expressionStatement.destructuringDeclarationType() != null) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 39662
diff changeset
   194
            final ExpressionTree expr = translateExpr(expressionStatement.getExpression());
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   195
            assert expr instanceof AssignmentTree : "destructuring decl. statement does not have assignment";
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 39662
diff changeset
   196
            final AssignmentTree assign = (AssignmentTree)expr;
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   197
            curStat = new DestructuringDeclTreeImpl(expressionStatement, assign.getVariable(), assign.getExpression());
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   198
        } else {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   199
            curStat = new ExpressionStatementTreeImpl(expressionStatement,
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   200
                translateExpr(expressionStatement.getExpression()));
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   201
        }
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   202
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   203
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   204
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   205
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   206
    public boolean enterBlockStatement(final BlockStatement blockStatement) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   207
        final Block block = blockStatement.getBlock();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   208
        if (blockStatement.isSynthetic()) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   209
            assert block != null && block.getStatements() != null && block.getStatements().size() == 1;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   210
            curStat = translateStat(block.getStatements().get(0));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   211
        } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   212
            curStat = new BlockTreeImpl(blockStatement,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   213
                translateStats(block != null? block.getStatements() : null));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   214
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   215
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   216
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   217
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   218
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   219
    public boolean enterForNode(final ForNode forNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   220
        if (forNode.isForIn()) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   221
            curStat = new ForInLoopTreeImpl(forNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   222
                    translateExpr(forNode.getInit()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   223
                    translateExpr(forNode.getModify()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   224
                    translateBlock(forNode.getBody()));
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   225
        } else if (forNode.isForOf()) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   226
            curStat = new ForOfLoopTreeImpl(forNode,
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   227
                    translateExpr(forNode.getInit()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   228
                    translateExpr(forNode.getModify()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   229
                    translateBlock(forNode.getBody()));
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   230
        } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   231
            curStat = new ForLoopTreeImpl(forNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   232
                    translateExpr(forNode.getInit()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   233
                    translateExpr(forNode.getTest()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   234
                    translateExpr(forNode.getModify()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   235
                    translateBlock(forNode.getBody()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   236
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   237
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   238
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   239
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   240
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   241
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   242
    public boolean enterFunctionNode(final FunctionNode functionNode) {
31485
be1acaee8e9e 8114838: Anonymous functions escape to surrounding scope when defined under "with" statement
sundar
parents: 30392
diff changeset
   243
        assert !functionNode.isDeclared() || functionNode.isAnonymous() : "should not reach here for function declaration";
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   244
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   245
        final List<? extends ExpressionTree> paramTrees = translateParameters(functionNode);
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   246
        final BlockTree blockTree = (BlockTree) translateBlock(functionNode.getBody(), true);
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   247
        curExpr = new FunctionExpressionTreeImpl(functionNode, paramTrees, blockTree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   248
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   249
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   250
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   251
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   252
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   253
    public boolean enterIdentNode(final IdentNode identNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   254
        curExpr = new IdentifierTreeImpl(identNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   255
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   256
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   257
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   258
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   259
    public boolean enterIfNode(final IfNode ifNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   260
        curStat = new IfTreeImpl(ifNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   261
                translateExpr(ifNode.getTest()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   262
                translateBlock(ifNode.getPass()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   263
                translateBlock(ifNode.getFail()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   264
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   265
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   266
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   267
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   268
    public boolean enterIndexNode(final IndexNode indexNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   269
        curExpr = new ArrayAccessTreeImpl(indexNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   270
                translateExpr(indexNode.getBase()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   271
                translateExpr(indexNode.getIndex()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   272
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   273
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   274
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   275
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   276
    public boolean enterLabelNode(final LabelNode labelNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   277
        curStat = new LabeledStatementTreeImpl(labelNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   278
                translateBlock(labelNode.getBody()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   279
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   280
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   281
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   282
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   283
    public boolean enterLiteralNode(final LiteralNode<?> literalNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   284
        final Object value = literalNode.getValue();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   285
        if (value instanceof Lexer.RegexToken) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   286
            curExpr = new RegExpLiteralTreeImpl(literalNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   287
        } else if (literalNode.isArray()) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   288
            final List<Expression> exprNodes = literalNode.getElementExpressions();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   289
            final List<ExpressionTreeImpl> exprTrees = new ArrayList<>(exprNodes.size());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   290
            for (final Node node : exprNodes) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   291
                if (node == null) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   292
                    exprTrees.add(null);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   293
                } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   294
                    curExpr = null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   295
                    node.accept(this);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   296
                    assert curExpr != null : "null for " + node;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   297
                    exprTrees.add(curExpr);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   298
                }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   299
            }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   300
            curExpr = new ArrayLiteralTreeImpl(literalNode, exprTrees);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   301
        } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   302
            curExpr = new LiteralTreeImpl(literalNode);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   303
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   304
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   305
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   306
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   307
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   308
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   309
    public boolean enterObjectNode(final ObjectNode objectNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   310
        final List<PropertyNode> propNodes = objectNode.getElements();
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   311
        final List<? extends PropertyTree> propTrees = translateProperties(propNodes);
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   312
        curExpr = new ObjectLiteralTreeImpl(objectNode, propTrees);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   313
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   314
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   315
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   316
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   317
    public boolean enterPropertyNode(final PropertyNode propertyNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   318
        assert false : "should not reach here!";
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   319
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   320
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   321
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   322
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   323
    public boolean enterReturnNode(final ReturnNode returnNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   324
        curStat = new ReturnTreeImpl(returnNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   325
                translateExpr(returnNode.getExpression()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   326
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   327
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   328
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   329
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   330
    public boolean enterRuntimeNode(final RuntimeNode runtimeNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   331
        assert false : "should not reach here: RuntimeNode";
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   332
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   333
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   334
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   335
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   336
    public boolean enterSplitNode(final SplitNode splitNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   337
        assert false : "should not reach here!";
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   338
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   339
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   340
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   341
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   342
    public boolean enterSwitchNode(final SwitchNode switchNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   343
        final List<CaseNode> caseNodes = switchNode.getCases();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   344
        final List<CaseTreeImpl> caseTrees = new ArrayList<>(caseNodes.size());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   345
        for (final CaseNode caseNode : caseNodes) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   346
            final Block body = caseNode.getBody();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   347
            caseTrees.add(
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   348
                    new CaseTreeImpl(caseNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   349
                            translateExpr(caseNode.getTest()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   350
                            translateStats(body != null? body.getStatements() : null)));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   351
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   352
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   353
        curStat = new SwitchTreeImpl(switchNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   354
                translateExpr(switchNode.getExpression()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   355
                caseTrees);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   356
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   357
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   358
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   359
    @Override
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   360
    public boolean enterTemplateLiteral(final TemplateLiteral templateLiteral) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   361
        curExpr = new TemplateLiteralTreeImpl(templateLiteral, translateExprs(templateLiteral.getExpressions()));
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   362
        return false;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   363
    }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   364
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   365
    @Override
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   366
    public boolean enterTernaryNode(final TernaryNode ternaryNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   367
        curExpr = new ConditionalExpressionTreeImpl(ternaryNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   368
                translateExpr(ternaryNode.getTest()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   369
                translateExpr(ternaryNode.getTrueExpression()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   370
                translateExpr(ternaryNode.getFalseExpression()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   371
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   372
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   373
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   374
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   375
    public boolean enterThrowNode(final ThrowNode throwNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   376
        curStat = new ThrowTreeImpl(throwNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   377
                translateExpr(throwNode.getExpression()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   378
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   379
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   380
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   381
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   382
    public boolean enterTryNode(final TryNode tryNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   383
        final List<? extends CatchNode> catchNodes = tryNode.getCatches();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   384
        final List<CatchTreeImpl> catchTrees = new ArrayList<>(catchNodes.size());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   385
        for (final CatchNode catchNode : catchNodes) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   386
            catchTrees.add(new CatchTreeImpl(catchNode,
41983
eb674141ab03 8156615: Catch parameter can be a BindingPattern in ES6 mode
sdama
parents: 41422
diff changeset
   387
                    translateExpr(catchNode.getException()),
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   388
                    (BlockTree) translateBlock(catchNode.getBody()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   389
                    translateExpr(catchNode.getExceptionCondition())));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   390
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   391
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   392
        curStat = new TryTreeImpl(tryNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   393
                (BlockTree) translateBlock(tryNode.getBody()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   394
                catchTrees,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   395
                (BlockTree) translateBlock(tryNode.getFinallyBody()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   396
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   397
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   398
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   399
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   400
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   401
    public boolean enterUnaryNode(final UnaryNode unaryNode) {
30392
dc4a419b2982 8079362: Enforce best practices for Node token API usage
attila
parents: 29539
diff changeset
   402
        if (unaryNode.isTokenType(TokenType.NEW)) {
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   403
            curExpr = new NewTreeImpl(unaryNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   404
                    translateExpr(unaryNode.getExpression()));
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   405
        } else if (unaryNode.isTokenType(TokenType.YIELD) ||
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   406
                unaryNode.isTokenType(TokenType.YIELD_STAR)) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   407
            curExpr = new YieldTreeImpl(unaryNode,
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   408
                    translateExpr(unaryNode.getExpression()));
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   409
        } else if (unaryNode.isTokenType(TokenType.SPREAD_ARGUMENT) ||
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   410
                unaryNode.isTokenType(TokenType.SPREAD_ARRAY)) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   411
            curExpr = new SpreadTreeImpl(unaryNode,
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   412
                    translateExpr(unaryNode.getExpression()));
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   413
        } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   414
            curExpr = new UnaryTreeImpl(unaryNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   415
                    translateExpr(unaryNode.getExpression()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   416
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   417
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   418
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   419
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   420
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   421
    public boolean enterVarNode(final VarNode varNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   422
        final Expression initNode = varNode.getInit();
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   423
        if (initNode instanceof FunctionNode && ((FunctionNode)initNode).isDeclared()) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   424
            final FunctionNode funcNode = (FunctionNode) initNode;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   425
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   426
            final List<? extends ExpressionTree> paramTrees = translateParameters(funcNode);
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   427
            final BlockTree blockTree = (BlockTree) translateBlock(funcNode.getBody(), true);
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   428
            curStat = new FunctionDeclarationTreeImpl(varNode, paramTrees, blockTree);
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   429
        } else if (initNode instanceof ClassNode && ((ClassNode)initNode).isStatement()) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   430
            final ClassNode classNode = (ClassNode) initNode;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   431
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   432
            curStat = new ClassDeclarationTreeImpl(varNode,
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   433
                    translateIdent(classNode.getIdent()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   434
                    translateExpr(classNode.getClassHeritage()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   435
                    translateProperty(classNode.getConstructor()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   436
                    translateProperties(classNode.getClassElements()));
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   437
        } else {
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   438
            curStat = new VariableTreeImpl(varNode, translateIdent(varNode.getName()), translateExpr(initNode));
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   439
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   440
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   441
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   442
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   443
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   444
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   445
    public boolean enterWhileNode(final WhileNode whileNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   446
        final ExpressionTree condTree = translateExpr(whileNode.getTest());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   447
        final StatementTree statTree = translateBlock(whileNode.getBody());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   448
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   449
        if (whileNode.isDoWhile()) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   450
            curStat = new DoWhileLoopTreeImpl(whileNode, condTree, statTree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   451
        } else {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   452
            curStat = new WhileLoopTreeImpl(whileNode, condTree, statTree);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   453
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   454
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   455
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   456
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   457
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   458
    @Override
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   459
    public boolean enterWithNode(final WithNode withNode) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   460
        curStat = new WithTreeImpl(withNode,
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   461
                translateExpr(withNode.getExpression()),
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   462
                translateBlock(withNode.getBody()));
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   463
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   464
        return false;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   465
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   466
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   467
    /**
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   468
     * Callback for entering a ClassNode
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   469
     *
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   470
     * @param  classNode  the node
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   471
     * @return true if traversal should continue and node children be traversed, false otherwise
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   472
     */
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   473
    @Override
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   474
    public boolean enterClassNode(final ClassNode classNode) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   475
        assert !classNode.isStatement(): "should not reach here for class declaration";
48653
89111a0e6355 8195829: Parsing a nameless ES6 class results in a thrown NullPointerException.
sundar
parents: 47216
diff changeset
   476
        final IdentNode className = classNode.getIdent();
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   477
        curExpr = new ClassExpressionTreeImpl(classNode,
48653
89111a0e6355 8195829: Parsing a nameless ES6 class results in a thrown NullPointerException.
sundar
parents: 47216
diff changeset
   478
            className != null? translateIdent(className) : null,
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   479
            translateExpr(classNode.getClassHeritage()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   480
            translateProperty(classNode.getConstructor()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   481
            translateProperties(classNode.getClassElements()));
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   482
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   483
        return false;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   484
    }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   485
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   486
    private StatementTree translateBlock(final Block blockNode) {
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   487
        return translateBlock(blockNode, false);
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   488
    }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   489
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   490
    private StatementTree translateBlock(final Block blockNode, final boolean sortStats) {
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   491
        if (blockNode == null) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   492
            return null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   493
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   494
        curStat = null;
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   495
        handleBlock(blockNode, sortStats);
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   496
        return curStat;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   497
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   498
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   499
    private boolean handleBlock(final Block block, final boolean sortStats) {
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   500
        // FIXME: revisit this!
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   501
        if (block.isSynthetic()) {
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   502
            final int statCount = block.getStatementCount();
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   503
            switch (statCount) {
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   504
                case 0: {
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   505
                    final EmptyNode emptyNode = new EmptyNode(-1, block.getToken(), block.getFinish());
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   506
                    curStat = new EmptyStatementTreeImpl(emptyNode);
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   507
                    return false;
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   508
                }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   509
                case 1: {
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   510
                    curStat = translateStat(block.getStatements().get(0));
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   511
                    return false;
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   512
                }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   513
                default: {
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   514
                    // fall through
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   515
                    break;
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   516
                }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   517
            }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   518
        }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   519
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   520
        final List<? extends Statement> stats = block.getStatements();
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   521
        curStat = new BlockTreeImpl(block,
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   522
            translateStats(sortStats? getOrderedStatements(stats) : stats));
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   523
        return false;
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   524
    }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   525
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   526
    private List<? extends Statement> getOrderedStatements(final List<? extends Statement> stats) {
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   527
        final List<? extends Statement> statList = new ArrayList<>(stats);
29539
b2a8fb583979 8075448: nashorn parser API returns init variable tree object of a for loop after for loop statement tree object
sundar
parents: 29536
diff changeset
   528
        statList.sort(Comparator.comparingInt(Node::getSourceOrder));
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   529
        return statList;
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   530
    }
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   531
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   532
    private List<? extends StatementTree> translateStats(final List<? extends Statement> stats) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   533
        if (stats == null) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   534
            return null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   535
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   536
        final List<StatementTreeImpl> statTrees = new ArrayList<>(stats.size());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   537
        for (final Statement stat : stats) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   538
            curStat = null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   539
            stat.accept(this);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   540
            assert curStat != null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   541
            statTrees.add(curStat);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   542
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   543
        return statTrees;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   544
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   545
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   546
    private List<? extends ExpressionTree> translateParameters(final FunctionNode func) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 39662
diff changeset
   547
        final Map<IdentNode, Expression> paramExprs = func.getParameterExpressions();
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   548
        if (paramExprs != null) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 39662
diff changeset
   549
            final List<IdentNode> params = func.getParameters();
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   550
            final List<ExpressionTreeImpl> exprTrees = new ArrayList<>(params.size());
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   551
            for (final IdentNode ident : params) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 39662
diff changeset
   552
                final Expression expr = paramExprs.containsKey(ident)? paramExprs.get(ident) : ident;
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   553
                curExpr = null;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   554
                expr.accept(this);
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   555
                assert curExpr != null;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   556
                exprTrees.add(curExpr);
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   557
            }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   558
            return exprTrees;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   559
        } else {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   560
            return translateExprs(func.getParameters());
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   561
        }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   562
    }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   563
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   564
    private List<? extends ExpressionTree> translateExprs(final List<? extends Expression> exprs) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   565
        if (exprs == null) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   566
            return null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   567
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   568
        final List<ExpressionTreeImpl> exprTrees = new ArrayList<>(exprs.size());
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   569
        for (final Expression expr : exprs) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   570
            curExpr = null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   571
            expr.accept(this);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   572
            assert curExpr != null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   573
            exprTrees.add(curExpr);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   574
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   575
        return exprTrees;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   576
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   577
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   578
    private ExpressionTreeImpl translateExpr(final Expression expr) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   579
        if (expr == null) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   580
            return null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   581
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   582
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   583
        curExpr = null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   584
        expr.accept(this);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   585
        assert curExpr != null : "null for " + expr;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   586
        return curExpr;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   587
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   588
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   589
    private StatementTreeImpl translateStat(final Statement stat) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   590
        if (stat == null) {
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   591
            return null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   592
        }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   593
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   594
        curStat = null;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   595
        stat.accept(this);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   596
        assert curStat != null : "null for " + stat;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   597
        return curStat;
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   598
    }
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   599
29536
805894cd3c6b 8075207: Nashorn parser API returns StatementTree objects in out of order
sundar
parents: 29407
diff changeset
   600
    private static IdentifierTree translateIdent(final IdentNode ident) {
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   601
        return new IdentifierTreeImpl(ident);
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   602
    }
39662
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   603
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   604
    private List<? extends PropertyTree> translateProperties(final List<PropertyNode> propNodes) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   605
        final List<PropertyTree> propTrees = new ArrayList<>(propNodes.size());
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   606
        for (final PropertyNode propNode : propNodes) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   607
            propTrees.add(translateProperty(propNode));
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   608
        }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   609
        return propTrees;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   610
    }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   611
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   612
    private PropertyTree translateProperty(final PropertyNode propNode) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   613
        return new PropertyTreeImpl(propNode,
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   614
                    translateExpr(propNode.getKey()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   615
                    translateExpr(propNode.getValue()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   616
                    (FunctionExpressionTree) translateExpr(propNode.getGetter()),
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   617
                    (FunctionExpressionTree) translateExpr(propNode.getSetter()));
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   618
    }
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   619
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   620
    private ModuleTree translateModule(final FunctionNode func) {
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   621
        return func.getKind() == FunctionNode.Kind.MODULE?
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   622
            ModuleTreeImpl.create(func) : null;
e2b36a3779b9 8149929: Nashorn Parser API needs to be updated for ES6
sundar
parents: 32888
diff changeset
   623
    }
29407
3fd4ede1581e 8074671: Nashorn Parser API
sundar
parents:
diff changeset
   624
}