1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 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 /** 29 * Common interface for all nodes in an abstract syntax tree. 30 * 31 * <p><b>WARNING:</b> This interface and its sub-interfaces are 32 * subject to change as the ECMAScript programming language evolves. 33 * 34 * @since 1.9 35 */ 36 @jdk.Exported 37 public interface Tree { 38 39 /** 40 * Enumerates all kinds of trees. 41 */ 42 @jdk.Exported 43 public enum Kind { 44 /** 45 * Used for instances of {@link ArrayAccessTree}. 46 */ 47 ARRAY_ACCESS(ArrayAccessTree.class), 48 49 /** 50 * Used for instances of {@link ArrayLiteralTree}. 51 */ 52 ARRAY_LITERAL(ArrayLiteralTree.class), 53 54 /** 55 * Used for instances of {@link AssignmentTree}. 56 */ 57 ASSIGNMENT(AssignmentTree.class), 58 59 /** 60 * Used for instances of {@link BlockTree}. 61 */ 62 BLOCK(BlockTree.class), 63 64 /** 65 * Used for instances of {@link BreakTree}. 66 */ 67 BREAK(BreakTree.class), 68 69 /** 70 * Used for instances of {@link CaseTree}. 71 */ 72 CASE(CaseTree.class), 73 74 /** 75 * Used for instances of {@link CatchTree}. 76 */ 77 CATCH(CatchTree.class), 78 79 /** 80 * Used for instances of {@link CompilationUnitTree}. 81 */ 82 COMPILATION_UNIT(CompilationUnitTree.class), 83 84 /** 85 * Used for instances of {@link ConditionalExpressionTree}. 86 */ 87 CONDITIONAL_EXPRESSION(ConditionalExpressionTree.class), 88 89 /** 90 * Used for instances of {@link ContinueTree}. 91 */ 92 CONTINUE(ContinueTree.class), 93 94 /** 95 * Used for instances of {@link DoWhileLoopTree}. 96 */ 97 DO_WHILE_LOOP(DoWhileLoopTree.class), 98 99 /** 100 * Used for instances of {@link DebuggerTree}. 101 */ 102 DEBUGGER(DebuggerTree.class), 103 104 /** 105 * Used for instances of {@link ForInLoopTree}. 106 */ 107 FOR_IN_LOOP(ForInLoopTree.class), 108 109 /** 110 * Used for instances of {@link FunctionExpressionTree}. 111 */ 112 FUNCTION_EXPRESSION(FunctionExpressionTree.class), 113 114 /** 115 * Used for instances of {@link ErroneousTree}. 116 */ 117 ERROR(ErroneousTree.class), 118 119 /** 120 * Used for instances of {@link ExpressionStatementTree}. 121 */ 122 EXPRESSION_STATEMENT(ExpressionStatementTree.class), 123 124 /** 125 * Used for instances of {@link MemberSelectTree}. 126 */ 127 MEMBER_SELECT(MemberSelectTree.class), 128 129 /** 130 * Used for instances of {@link ForLoopTree}. 131 */ 132 FOR_LOOP(ForLoopTree.class), 133 134 /** 135 * Used for instances of {@link IdentifierTree}. 136 */ 137 IDENTIFIER(IdentifierTree.class), 138 139 /** 140 * Used for instances of {@link IfTree}. 141 */ 142 IF(IfTree.class), 143 144 /** 145 * Used for instances of {@link InstanceOfTree}. 146 */ 147 INSTANCE_OF(InstanceOfTree.class), 148 149 /** 150 * Used for instances of {@link LabeledStatementTree}. 151 */ 152 LABELED_STATEMENT(LabeledStatementTree.class), 153 154 /** 155 * Used for instances of {@link FunctionDeclarationTree}. 156 */ 157 FUNCTION(FunctionDeclarationTree.class), 158 159 /** 160 * Used for instances of {@link FunctionCallTree}. 161 */ 162 FUNCTION_INVOCATION(FunctionCallTree.class), 163 164 /** 165 * Used for instances of {@link NewTree}. 166 */ 167 NEW(NewTree.class), 168 169 /** 170 * Used for instances of {@link ObjectLiteralTree}. 171 */ 172 OBJECT_LITERAL(ObjectLiteralTree.class), 173 174 /** 175 * Used for instances of {@link ParenthesizedTree}. 176 */ 177 PARENTHESIZED(ParenthesizedTree.class), 178 179 /** 180 * Used for instances of {@link PropertyTree}. 181 */ 182 PROPERTY(PropertyTree.class), 183 184 /** 185 * Used for instances of {@link RegExpLiteralTree}. 186 */ 187 REGEXP_LITERAL(RegExpLiteralTree.class), 188 189 /** 190 * Used for instances of {@link ReturnTree}. 191 */ 192 RETURN(ReturnTree.class), 193 194 /** 195 * Used for instances of {@link EmptyStatementTree}. 196 */ 197 EMPTY_STATEMENT(EmptyStatementTree.class), 198 199 /** 200 * Used for instances of {@link SwitchTree}. 201 */ 202 SWITCH(SwitchTree.class), 203 204 /** 205 * Used for instances of {@link ThrowTree}. 206 */ 207 THROW(ThrowTree.class), 208 209 /** 210 * Used for instances of {@link TryTree}. 211 */ 212 TRY(TryTree.class), 213 214 /** 215 * Used for instances of {@link VariableTree}. 216 */ 217 VARIABLE(VariableTree.class), 218 219 /** 220 * Used for instances of {@link WhileLoopTree}. 221 */ 222 WHILE_LOOP(WhileLoopTree.class), 223 224 /** 225 * Used for instances of {@link WithTree}. 226 */ 227 WITH(WithTree.class), 228 229 /** 230 * Used for instances of {@link UnaryTree} representing postfix 231 * increment operator {@code ++}. 232 */ 233 POSTFIX_INCREMENT(UnaryTree.class), 234 235 /** 236 * Used for instances of {@link UnaryTree} representing postfix 237 * decrement operator {@code --}. 238 */ 239 POSTFIX_DECREMENT(UnaryTree.class), 240 241 /** 242 * Used for instances of {@link UnaryTree} representing prefix 243 * increment operator {@code ++}. 244 */ 245 PREFIX_INCREMENT(UnaryTree.class), 246 247 /** 248 * Used for instances of {@link UnaryTree} representing prefix 249 * decrement operator {@code --}. 250 */ 251 PREFIX_DECREMENT(UnaryTree.class), 252 253 /** 254 * Used for instances of {@link UnaryTree} representing unary plus 255 * operator {@code +}. 256 */ 257 UNARY_PLUS(UnaryTree.class), 258 259 /** 260 * Used for instances of {@link UnaryTree} representing unary minus 261 * operator {@code -}. 262 */ 263 UNARY_MINUS(UnaryTree.class), 264 265 /** 266 * Used for instances of {@link UnaryTree} representing bitwise 267 * complement operator {@code ~}. 268 */ 269 BITWISE_COMPLEMENT(UnaryTree.class), 270 271 /** 272 * Used for instances of {@link UnaryTree} representing logical 273 * complement operator {@code !}. 274 */ 275 LOGICAL_COMPLEMENT(UnaryTree.class), 276 277 /** 278 * Used for instances of {@link UnaryTree} representing logical 279 * delete operator {@code delete}. 280 */ 281 DELETE(UnaryTree.class), 282 283 /** 284 * Used for instances of {@link UnaryTree} representing logical 285 * typeof operator {@code typeof}. 286 */ 287 TYPEOF(UnaryTree.class), 288 289 /** 290 * Used for instances of {@link UnaryTree} representing logical 291 * void operator {@code typeof}. 292 */ 293 VOID(UnaryTree.class), 294 295 /** 296 * Used for instances of {@link BinaryTree} representing 297 * comma {@code ,}. 298 */ 299 COMMA(BinaryTree.class), 300 301 /** 302 * Used for instances of {@link BinaryTree} representing 303 * multiplication {@code *}. 304 */ 305 MULTIPLY(BinaryTree.class), 306 307 /** 308 * Used for instances of {@link BinaryTree} representing 309 * division {@code /}. 310 */ 311 DIVIDE(BinaryTree.class), 312 313 /** 314 * Used for instances of {@link BinaryTree} representing 315 * remainder {@code %}. 316 */ 317 REMAINDER(BinaryTree.class), 318 319 /** 320 * Used for instances of {@link BinaryTree} representing 321 * addition or string concatenation {@code +}. 322 */ 323 PLUS(BinaryTree.class), 324 325 /** 326 * Used for instances of {@link BinaryTree} representing 327 * subtraction {@code -}. 328 */ 329 MINUS(BinaryTree.class), 330 331 /** 332 * Used for instances of {@link BinaryTree} representing 333 * left shift {@code <<}. 334 */ 335 LEFT_SHIFT(BinaryTree.class), 336 337 /** 338 * Used for instances of {@link BinaryTree} representing 339 * right shift {@code >>}. 340 */ 341 RIGHT_SHIFT(BinaryTree.class), 342 343 /** 344 * Used for instances of {@link BinaryTree} representing 345 * unsigned right shift {@code >>>}. 346 */ 347 UNSIGNED_RIGHT_SHIFT(BinaryTree.class), 348 349 /** 350 * Used for instances of {@link BinaryTree} representing 351 * less-than {@code <}. 352 */ 353 LESS_THAN(BinaryTree.class), 354 355 /** 356 * Used for instances of {@link BinaryTree} representing 357 * greater-than {@code >}. 358 */ 359 GREATER_THAN(BinaryTree.class), 360 361 /** 362 * Used for instances of {@link BinaryTree} representing 363 * less-than-equal {@code <=}. 364 */ 365 LESS_THAN_EQUAL(BinaryTree.class), 366 367 /** 368 * Used for instances of {@link BinaryTree} representing 369 * greater-than-equal {@code >=}. 370 */ 371 GREATER_THAN_EQUAL(BinaryTree.class), 372 373 /** 374 * Used for instances of {@link BinaryTree} representing 375 * in operator {@code in}. 376 */ 377 IN(BinaryTree.class), 378 379 /** 380 * Used for instances of {@link BinaryTree} representing 381 * equal-to {@code ==}. 382 */ 383 EQUAL_TO(BinaryTree.class), 384 385 /** 386 * Used for instances of {@link BinaryTree} representing 387 * not-equal-to {@code !=}. 388 */ 389 NOT_EQUAL_TO(BinaryTree.class), 390 391 /** 392 * Used for instances of {@link BinaryTree} representing 393 * equal-to {@code ===}. 394 */ 395 STRICT_EQUAL_TO(BinaryTree.class), 396 397 /** 398 * Used for instances of {@link BinaryTree} representing 399 * not-equal-to {@code !==}. 400 */ 401 STRICT_NOT_EQUAL_TO(BinaryTree.class), 402 403 /** 404 * Used for instances of {@link BinaryTree} representing 405 * bitwise and logical "and" {@code &}. 406 */ 407 AND(BinaryTree.class), 408 409 /** 410 * Used for instances of {@link BinaryTree} representing 411 * bitwise and logical "xor" {@code ^}. 412 */ 413 XOR(BinaryTree.class), 414 415 /** 416 * Used for instances of {@link BinaryTree} representing 417 * bitwise and logical "or" {@code |}. 418 */ 419 OR(BinaryTree.class), 420 421 /** 422 * Used for instances of {@link BinaryTree} representing 423 * conditional-and {@code &&}. 424 */ 425 CONDITIONAL_AND(BinaryTree.class), 426 427 /** 428 * Used for instances of {@link BinaryTree} representing 429 * conditional-or {@code ||}. 430 */ 431 CONDITIONAL_OR(BinaryTree.class), 432 433 /** 434 * Used for instances of {@link CompoundAssignmentTree} representing 435 * multiplication assignment {@code *=}. 436 */ 437 MULTIPLY_ASSIGNMENT(CompoundAssignmentTree.class), 438 439 /** 440 * Used for instances of {@link CompoundAssignmentTree} representing 441 * division assignment {@code /=}. 442 */ 443 DIVIDE_ASSIGNMENT(CompoundAssignmentTree.class), 444 445 /** 446 * Used for instances of {@link CompoundAssignmentTree} representing 447 * remainder assignment {@code %=}. 448 */ 449 REMAINDER_ASSIGNMENT(CompoundAssignmentTree.class), 450 451 /** 452 * Used for instances of {@link CompoundAssignmentTree} representing 453 * addition or string concatenation assignment {@code +=}. 454 */ 455 PLUS_ASSIGNMENT(CompoundAssignmentTree.class), 456 457 /** 458 * Used for instances of {@link CompoundAssignmentTree} representing 459 * subtraction assignment {@code -=}. 460 */ 461 MINUS_ASSIGNMENT(CompoundAssignmentTree.class), 462 463 /** 464 * Used for instances of {@link CompoundAssignmentTree} representing 465 * left shift assignment {@code <<=}. 466 */ 467 LEFT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class), 468 469 /** 470 * Used for instances of {@link CompoundAssignmentTree} representing 471 * right shift assignment {@code >>=}. 472 */ 473 RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class), 474 475 /** 476 * Used for instances of {@link CompoundAssignmentTree} representing 477 * unsigned right shift assignment {@code >>>=}. 478 */ 479 UNSIGNED_RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class), 480 481 /** 482 * Used for instances of {@link CompoundAssignmentTree} representing 483 * bitwise and logical "and" assignment {@code &=}. 484 */ 485 AND_ASSIGNMENT(CompoundAssignmentTree.class), 486 487 /** 488 * Used for instances of {@link CompoundAssignmentTree} representing 489 * bitwise and logical "xor" assignment {@code ^=}. 490 */ 491 XOR_ASSIGNMENT(CompoundAssignmentTree.class), 492 493 /** 494 * Used for instances of {@link CompoundAssignmentTree} representing 495 * bitwise and logical "or" assignment {@code |=}. 496 */ 497 OR_ASSIGNMENT(CompoundAssignmentTree.class), 498 499 /** 500 * Used for instances of {@link LiteralTree} representing 501 * a number literal expression of type {@code double}. 502 */ 503 NUMBER_LITERAL(LiteralTree.class), 504 505 /** 506 * Used for instances of {@link LiteralTree} representing 507 * a boolean literal expression of type {@code boolean}. 508 */ 509 BOOLEAN_LITERAL(LiteralTree.class), 510 511 /** 512 * Used for instances of {@link LiteralTree} representing 513 * a string literal expression of type {@link String}. 514 */ 515 STRING_LITERAL(LiteralTree.class), 516 517 /** 518 * Used for instances of {@link LiteralTree} representing 519 * the use of {@code null}. 520 */ 521 NULL_LITERAL(LiteralTree.class), 522 523 /** 524 * An implementation-reserved node. This is the not the node 525 * you are looking for. 526 */ 527 OTHER(null); 528 529 Kind(Class<? extends Tree> intf) { 530 associatedInterface = intf; 531 } 532 533 public Class<? extends Tree> asInterface() { 534 return associatedInterface; 535 } 536 537 /** 538 * Returns if this is a literal tree kind or not. 539 * 540 * @return true if this is a literal tree kind, false otherwise 541 */ 542 public boolean isLiteral() { 543 return associatedInterface == LiteralTree.class; 544 } 545 546 /** 547 * Returns if this is an expression tree kind or not. 548 * 549 * @return true if this is an expression tree kind, false otherwise 550 */ 551 public boolean isExpression() { 552 return ExpressionTree.class.isAssignableFrom(associatedInterface); 553 } 554 555 /** 556 * Returns if this is a statement tree kind or not. 557 * 558 * @return true if this is a statement tree kind, false otherwise 559 */ 560 public boolean isStatement() { 561 return StatementTree.class.isAssignableFrom(associatedInterface); 562 } 563 564 private final Class<? extends Tree> associatedInterface; 565 } 566 567 /** 568 * Start character offset of this Tree within the source. 569 * 570 * @return the position 571 */ 572 long getStartPosition(); 573 574 /** 575 * End character offset of this Tree within the source. 576 * 577 * @return the position 578 */ 579 long getEndPosition(); 580 581 /** 582 * Gets the kind of this tree. 583 * 584 * @return the kind of this tree. 585 */ 586 Kind getKind(); 587 588 /** 589 * Accept method used to implement the visitor pattern. The 590 * visitor pattern is used to implement operations on trees. 591 * 592 * @param <R> result type of this operation. 593 * @param <D> type of additional data. 594 * @param visitor tree visitor 595 * @param data additional data passed to visitor methods 596 * @return the value from visitor's visit methods 597 */ 598 <R,D> R accept(TreeVisitor<R,D> visitor, D data); 599 }