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