blob: f14adfef7e73f7d3b66500db628c10d741ac9774 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.syntax;
16
17import com.google.common.annotations.VisibleForTesting;
18import com.google.devtools.build.lib.events.Location;
brandjone2ffd5d2017-06-27 18:14:54 +020019import java.io.IOException;
brandjone2ffd5d2017-06-27 18:14:54 +020020import java.util.List;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021
Googler4ace4652019-09-16 07:47:08 -070022/** An Node is a node in a Starlark syntax tree. */
23public abstract class Node {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024
25 private Location location;
26
Googler4ace4652019-09-16 07:47:08 -070027 protected Node() {}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028
Florian Weikert90a15962015-09-11 13:43:10 +000029 /**
30 * Returns whether this node represents a new scope, e.g. a function call.
31 */
32 protected boolean isNewScope() {
33 return false;
34 }
35
Janak Ramakrishnan3745c902016-05-11 08:12:10 +000036 /** Returns an exception which should be thrown instead of the original one. */
37 protected final EvalException maybeTransformException(EvalException original) {
Florian Weikert90a15962015-09-11 13:43:10 +000038 // If there is already a non-empty stack trace, we only add this node iff it describes a
39 // new scope (e.g. FuncallExpression).
Florian Weikert4b67d4f2015-09-14 13:35:34 +000040 if (original instanceof EvalExceptionWithStackTrace) {
Florian Weikert90a15962015-09-11 13:43:10 +000041 EvalExceptionWithStackTrace real = (EvalExceptionWithStackTrace) original;
Florian Weikert4b67d4f2015-09-14 13:35:34 +000042 if (isNewScope()) {
43 real.registerNode(this);
44 }
Florian Weikert90a15962015-09-11 13:43:10 +000045 return real;
46 }
47
Janak Ramakrishnan3745c902016-05-11 08:12:10 +000048 if (original.canBeAddedToStackTrace()) {
49 return new EvalExceptionWithStackTrace(original, this);
50 } else {
51 return original;
Florian Weikert90a15962015-09-11 13:43:10 +000052 }
Florian Weikert90a15962015-09-11 13:43:10 +000053 }
54
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 @VisibleForTesting // productionVisibility = Visibility.PACKAGE_PRIVATE
56 public void setLocation(Location location) {
57 this.location = location;
58 }
59
Francois-Rene Rideauedf7bdb2015-03-02 17:12:45 +000060 /** @return the same node with its location set, in a slightly more fluent style */
Googler4ace4652019-09-16 07:47:08 -070061 public static <NodeT extends Node> NodeT setLocation(Location location, NodeT node) {
Francois-Rene Rideauedf7bdb2015-03-02 17:12:45 +000062 node.setLocation(location);
63 return node;
64 }
65
Googler99cbc172018-11-08 07:43:00 -080066 public Location getLocation() {
67 return location;
68 }
69
brandjone2ffd5d2017-06-27 18:14:54 +020070 /** Number of spaces that each indentation level expands to when pretty-printing. */
71 public static final int INDENT_WIDTH = 2;
72
73 /** Writes out the indentation prefix for a line. */
74 protected void printIndent(Appendable buffer, int indentLevel) throws IOException {
75 for (int i = 0; i < indentLevel * INDENT_WIDTH; i++) {
76 buffer.append(' ');
77 }
78 }
79
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010080 /**
brandjone2ffd5d2017-06-27 18:14:54 +020081 * Writes out a suite of statements. The statements are indented one more level than given, i.e.,
82 * the {@code indentLevel} parameter should be the same as the parent node's.
83 *
84 * <p>This also prints out a {@code pass} line if the suite is empty.
85 */
86 protected void printSuite(Appendable buffer, List<Statement> statements, int parentIndentLevel)
87 throws IOException {
88 if (statements.isEmpty()) {
89 printIndent(buffer, parentIndentLevel + 1);
90 buffer.append("pass\n");
91 } else {
92 for (Statement stmt : statements) {
93 stmt.prettyPrint(buffer, parentIndentLevel + 1);
94 }
95 }
96 }
97
98 /**
99 * Writes a pretty-printed representation of this node to a buffer, assuming the given starting
100 * indentation level.
101 *
102 * <p>For expressions, the indentation level is ignored. For statements, the indentation is
103 * written, then the statement contents (which may include multiple lines with their own
104 * indentation), then a newline character.
105 *
106 * <p>Indentation expands to {@code INDENT_WIDTH} many spaces per indent.
107 *
108 * <p>Pretty printing returns the canonical source code corresponding to an AST. Generally, the
109 * output can be round-tripped: Pretty printing an AST and then parsing the result should give you
110 * back an equivalent AST.
111 *
112 * <p>Pretty printing can also be used as a proxy for comparing for equality between two ASTs.
113 * This can be very useful in tests. However, it is still possible for two different trees to have
114 * the same pretty printing. In particular, {@link BuildFileAST} includes import metadata and
115 * comment information that is not reflected in the string.
116 */
117 public abstract void prettyPrint(Appendable buffer, int indentLevel) throws IOException;
118
119 /** Same as {@link #prettyPrint(Appendable, int)}, except with no indent. */
120 public void prettyPrint(Appendable buffer) throws IOException {
121 prettyPrint(buffer, 0);
122 }
123
124 /** Returns a pretty-printed representation of this node. */
125 public String prettyPrint() {
126 StringBuilder builder = new StringBuilder();
127 try {
128 prettyPrint(builder);
129 } catch (IOException e) {
130 // Not possible for StringBuilder.
131 throw new AssertionError(e);
132 }
133 return builder.toString();
134 }
135
136 /**
137 * Print the syntax node in a form useful for debugging.
138 *
139 * <p>The output is not precisely specified; use {@link #prettyPrint()} if you need more stable
140 * and complete information. For instance, this function may omit child statements of compound
141 * statements, or parentheses around some expressions. It may also abbreviate large list literals.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100142 */
143 @Override
brandjone2ffd5d2017-06-27 18:14:54 +0200144 public String toString() {
145 return prettyPrint();
146 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100147
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100148 /**
Googler4ace4652019-09-16 07:47:08 -0700149 * Implements the double dispatch by calling into the node specific <code>visit</code> method of
150 * the {@link NodeVisitor}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100151 *
Googler4ace4652019-09-16 07:47:08 -0700152 * @param visitor the {@link NodeVisitor} instance to dispatch to.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100153 */
Googler4ace4652019-09-16 07:47:08 -0700154 public abstract void accept(NodeVisitor visitor);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100155}