Debian Codegraph @

src/com/google/javascript/jscomp/CodeConsumer.java

raw symbols SCIP
1 /*
2 * Copyright 2004 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.javascript.rhino.Node;
20
21
22 /**
23 * Abstracted consumer of the CodeGenerator output.
24 *
25 * @see CodeGenerator
26 * @see CodePrinter
27 * @see InlineCostEstimator
28 */
29 abstract class CodeConsumer {
30 boolean statementNeedsEnded = false;
31 boolean statementStarted = false;
32 boolean sawFunction = false;
33
34 /**
35 * Starts the source mapping for the given
36 * node at the current position.
37 */
38 void startSourceMapping(Node node) {
39 }
40
41 /**
42 * Finishes the source mapping for the given
43 * node at the current position.
44 */
45 void endSourceMapping(Node node) {
46 }
47
48 /**
49 * Provides a means of interrupting the CodeGenerator. Derived classes should
50 * return false to stop further processing.
51 */
52 boolean continueProcessing() {
53 return true;
54 }
55
56 /**
57 * Retrieve the last character of the last string sent to append.
58 */
59 abstract char getLastChar();
60
61 void addIdentifier(String identifier) {
62 add(identifier);
63 }
64
65 /**
66 * Appends a string to the code, keeping track of the current line length.
67 *
68 * NOTE: the string must be a complete token--partial strings or
69 * partial regexes will run the risk of being split across lines.
70 *
71 * Do not directly append newlines with this method. Instead use
72 * {@link #startNewLine}.
73 */
74 abstract void append(String str);
75
76 void appendBlockStart() {
77 append("{");
78 }
79
80 void appendBlockEnd() {
81 append("}");
82 }
83
84 void startNewLine() {
85 }
86
87 void maybeLineBreak() {
88 maybeCutLine();
89 }
90
91 void maybeCutLine() {
92 }
93
94 void endLine() {
95 }
96
97 void notePreferredLineBreak() {
98 }
99
100 void beginBlock() {
101 if (statementNeedsEnded) {
102 append(";");
103 maybeLineBreak();
104 }
105 appendBlockStart();
106
107 endLine();
108 statementNeedsEnded = false;
109 }
110
111 void endBlock() {
112 endBlock(false);
113 }
114
115 void endBlock(boolean shouldEndLine) {
116 appendBlockEnd();
117 if (shouldEndLine) {
118 endLine();
119 }
120 statementNeedsEnded = false;
121 }
122
123 void listSeparator() {
124 add(",");
125 maybeLineBreak();
126 }
127
128 /**
129 * Indicates the end of a statement and a ';' may need to be added.
130 * But we don't add it now, in case we're at the end of a block (in which
131 * case we don't have to add the ';').
132 * See maybeEndStatement()
133 */
134 void endStatement() {
135 endStatement(false);
136 }
137
138 void endStatement(boolean needSemiColon) {
139 if (needSemiColon) {
140 append(";");
141 maybeLineBreak();
142 statementNeedsEnded = false;
143 } else if (statementStarted) {
144 statementNeedsEnded = true;
145 }
146 }
147
148 /**
149 * This is to be called when we're in a statement. If the prev statement
150 * needs to be ended, add a ';'.
151 */
152 void maybeEndStatement() {
153 // Add a ';' if we need to.
154 if (statementNeedsEnded) {
155 append(";");
156 maybeLineBreak();
157 endLine();
158 statementNeedsEnded = false;
159 }
160 statementStarted = true;
161 }
162
163 void endFunction() {
164 endFunction(false);
165 }
166
167 void endFunction(boolean statementContext) {
168 sawFunction = true;
169 if (statementContext) {
170 endLine();
171 }
172 }
173
174 void beginCaseBody() {
175 append(":");
176 }
177
178 void endCaseBody() {
179 }
180
181 void add(String newcode) {
182 maybeEndStatement();
183
184 if (newcode.length() == 0) {
185 return;
186 }
187
188 char c = newcode.charAt(0);
189 if ((isWordChar(c) || c == '\\') &&
190 isWordChar(getLastChar())) {
191 // need space to separate. This is not pretty printing.
192 // For example: "return foo;"
193 append(" ");
194 } else if (c == '/' && getLastChar() == '/') {
195 // Do not allow a forward slash to appear after a DIV.
196 // For example,
197 // REGEXP DIV REGEXP
198 // is valid and should print like
199 // / // / /
200 append(" ");
201 }
202
203 append(newcode);
204 }
205
206 void appendOp(String op, boolean binOp) {
207 append(op);
208 }
209
210 void addOp(String op, boolean binOp) {
211 maybeEndStatement();
212
213 char first = op.charAt(0);
214 char prev = getLastChar();
215
216 if ((first == '+' || first == '-') && prev == first) {
217 // This is not pretty printing. This is to prevent misparsing of
218 // things like "x + ++y" or "x++ + ++y"
219 append(" ");
220 } else if (Character.isLetter(first) &&
221 isWordChar(prev)) {
222 // Make sure there is a space after e.g. instanceof , typeof
223 append(" ");
224 } else if (prev == '-' && first == '>') {
225 // Make sure that we don't emit -->
226 append(" ");
227 }
228
229 // Allow formatting around the operator.
230 appendOp(op, binOp);
231
232 // Line breaking after an operator is always safe. Line breaking before an
233 // operator on the other hand is not. We only line break after a bin op
234 // because it looks strange.
235 if (binOp) {
236 maybeCutLine();
237 }
238 }
239
240 void addNumber(double x) {
241 // This is not pretty printing. This is to prevent misparsing of x- -4 as
242 // x--4 (which is a syntax error).
243 char prev = getLastChar();
244 boolean negativeZero = isNegativeZero(x);
245 if ((x < 0 || negativeZero) && prev == '-') {
246 add(" ");
247 }
248
249 if (negativeZero) {
250 addConstant("-0");
251 } else if ((long) x == x) {
252 long value = (long) x;
253 long mantissa = value;
254 int exp = 0;
255 if (Math.abs(x) >= 100) {
256 while (mantissa / 10 * Math.pow(10, exp + 1) == value) {
257 mantissa /= 10;
258 exp++;
259 }
260 }
261 if (exp > 2) {
262 addConstant(Long.toString(mantissa) + "E" + Integer.toString(exp));
263 } else {
264 long valueAbs = Math.abs(value);
265 if (Long.toHexString(valueAbs).length() + 2 <
266 Long.toString(valueAbs).length()) {
267 addConstant((value < 0 ? "-" : "") + "0x" +
268 Long.toHexString(valueAbs));
269 } else {
270 addConstant(Long.toString(value));
271 }
272 }
273 } else {
274 addConstant(String.valueOf(x).replace(".0E", "E"));
275 }
276 }
277
278 void addConstant(String newcode) {
279 add(newcode);
280 }
281
282 static boolean isNegativeZero(double x) {
283 return x == 0.0 && Math.copySign(1, x) == -1.0;
284 }
285
286 static boolean isWordChar(char ch) {
287 return (ch == '_' ||
288 ch == '$' ||
289 Character.isLetterOrDigit(ch));
290 }
291
292 /**
293 * If the body of a for loop or the then clause of an if statement has
294 * a single statement, should it be wrapped in a block? Doing so can
295 * help when pretty-printing the code, and permits putting a debugging
296 * breakpoint on the statement inside the condition.
297 *
298 * @return {@boolean true} if such expressions should be wrapped
299 */
300 boolean shouldPreserveExtraBlocks() {
301 return false;
302 }
303
304 /**
305 * @return Whether the a line break can be added after the specified BLOCK.
306 */
307 boolean breakAfterBlockFor(Node n, boolean statementContext) {
308 return statementContext;
309 }
310
311 /** Called when we're at the end of a file. */
312 void endFile() {}
313 }