1 /*
2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.nashorn.api.tree;
27 
28 import jdk.nashorn.internal.ir.Node;
29 
30 import jdk.nashorn.internal.parser.TokenType;
31 
32 abstract class TreeImpl implements Tree {
33     protected final Node node;
34 
TreeImpl(final Node node)35     TreeImpl(final Node node) {
36         this.node = node;
37     }
38 
39     @Override
getStartPosition()40     public long getStartPosition() {
41         return node.getStart();
42     }
43 
44     @Override
getEndPosition()45     public long getEndPosition() {
46         return node.getFinish();
47     }
48 
49     @Override
accept(final TreeVisitor<R,D> visitor, final D data)50     public <R,D> R accept(final TreeVisitor<R,D> visitor, final D data) {
51         return visitor.visitUnknown(this, data);
52     }
53 
getOperator(final TokenType tt)54     static Kind getOperator(final TokenType tt) {
55         switch(tt) {
56             case NEW:
57                 return Kind.NEW;
58             case NOT:
59                 return Kind.LOGICAL_COMPLEMENT;
60             case NE:
61                 return Kind.NOT_EQUAL_TO;
62             case NE_STRICT:
63                 return Kind.STRICT_NOT_EQUAL_TO;
64             case MOD:
65                 return Kind.REMAINDER;
66             case ASSIGN_MOD:
67                 return Kind.REMAINDER_ASSIGNMENT;
68             case BIT_AND:
69                 return Kind.AND;
70             case AND:
71                 return Kind.CONDITIONAL_AND;
72             case ASSIGN_BIT_AND:
73                 return Kind.AND_ASSIGNMENT;
74             case MUL:
75                 return Kind.MULTIPLY;
76             case ASSIGN_MUL:
77                 return Kind.MULTIPLY_ASSIGNMENT;
78             case POS:
79                 return Kind.UNARY_PLUS;
80             case ADD:
81                 return Kind.PLUS;
82             case INCPREFIX:
83                 return Kind.PREFIX_INCREMENT;
84             case INCPOSTFIX:
85                 return Kind.POSTFIX_INCREMENT;
86             case ASSIGN_ADD:
87                 return Kind.PLUS_ASSIGNMENT;
88             case NEG:
89                 return Kind.UNARY_MINUS;
90             case SUB:
91                 return Kind.MINUS;
92             case DECPREFIX:
93                 return Kind.PREFIX_DECREMENT;
94             case DECPOSTFIX:
95                 return Kind.POSTFIX_DECREMENT;
96             case ASSIGN_SUB:
97                 return Kind.MINUS_ASSIGNMENT;
98             case DIV:
99                 return Kind.DIVIDE;
100             case ASSIGN_DIV:
101                 return Kind.DIVIDE_ASSIGNMENT;
102             case LT:
103                 return Kind.LESS_THAN;
104             case SHL:
105                 return Kind.LEFT_SHIFT;
106             case ASSIGN_SHL:
107                 return Kind.LEFT_SHIFT_ASSIGNMENT;
108             case LE:
109                 return Kind.LESS_THAN_EQUAL;
110             case ASSIGN:
111                 return Kind.ASSIGNMENT;
112             case EQ:
113                 return Kind.EQUAL_TO;
114             case EQ_STRICT:
115                 return Kind.STRICT_EQUAL_TO;
116             case GT:
117                 return Kind.GREATER_THAN;
118             case GE:
119                 return Kind.GREATER_THAN_EQUAL;
120             case SAR:
121                 return Kind.RIGHT_SHIFT;
122             case ASSIGN_SAR:
123                 return Kind.RIGHT_SHIFT_ASSIGNMENT;
124             case SHR:
125                 return Kind.UNSIGNED_RIGHT_SHIFT;
126             case ASSIGN_SHR:
127                 return Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
128             case TERNARY:
129                 return Kind.CONDITIONAL_EXPRESSION;
130             case BIT_XOR:
131                 return Kind.XOR;
132             case ASSIGN_BIT_XOR:
133                 return Kind.XOR_ASSIGNMENT;
134             case BIT_OR:
135                 return Kind.OR;
136             case ASSIGN_BIT_OR:
137                 return Kind.OR_ASSIGNMENT;
138             case OR:
139                 return Kind.CONDITIONAL_OR;
140             case BIT_NOT:
141                 return Kind.BITWISE_COMPLEMENT;
142             case DELETE:
143                 return Kind.DELETE;
144             case SPREAD_ARRAY:
145             case SPREAD_ARGUMENT:
146                 return Kind.SPREAD;
147             case TYPEOF:
148                 return Kind.TYPEOF;
149             case VOID:
150                 return Kind.VOID;
151             case YIELD:
152                 return Kind.YIELD;
153             case IN:
154                 return Kind.IN;
155             case INSTANCEOF:
156                 return Kind.INSTANCE_OF;
157             case COMMARIGHT:
158                 return Kind.COMMA;
159             default:
160                 throw new AssertionError("should not reach here: " + tt);
161         }
162     }
163 }
164