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