1 | // Copyright (C) 2005 Google Inc. |
2 | // |
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 | |
15 | package com.google.caja.parser.js; |
16 | |
17 | import com.google.caja.lexer.FilePosition; |
18 | import com.google.caja.lexer.escaping.Escaping; |
19 | import com.google.caja.parser.ParseTreeNode; |
20 | import com.google.caja.reporting.RenderContext; |
21 | import com.google.javascript.jscomp.jsonml.JsonML; |
22 | import com.google.javascript.jscomp.jsonml.TagAttr; |
23 | import com.google.javascript.jscomp.jsonml.TagType; |
24 | |
25 | import java.util.List; |
26 | import java.util.Map; |
27 | |
28 | /** |
29 | * A statement with a label, such as |
30 | * <code>label: for (...) { ... }</code>. |
31 | * |
32 | * <p>Javascript bizarrely allows labels on any statements, but the labels are |
33 | * only useful when applied to loops or switches.</p> |
34 | * |
35 | * <p>The empty string is the default label, so <code>break;</code> breaks with |
36 | * label <code>""</code>.</p> |
37 | * |
38 | * @author mikesamuel@gmail.com |
39 | */ |
40 | public abstract class LabeledStatement extends AbstractStatement { |
41 | private final String label; |
42 | |
43 | protected LabeledStatement( |
44 | FilePosition pos, |
45 | String label, Class<? extends ParseTreeNode> childClass) { |
46 | super(pos, childClass); |
47 | assert label != null; |
48 | this.label = label; |
49 | } |
50 | |
51 | public String getLabel() { return label; } |
52 | |
53 | /** |
54 | * Is the statement a target for a {@link ContinueStmt continue}? |
55 | * For example, {@code switch} statements can be broken from, but since they |
56 | * are not loops, they cannot be continued to. |
57 | */ |
58 | public abstract boolean isTargetForContinue(); |
59 | |
60 | @Override |
61 | public void breaks(Map<String, List<BreakStmt>> labels) { |
62 | super.breaks(labels); |
63 | labels.remove(this.label); |
64 | } |
65 | |
66 | @Override |
67 | public void continues(Map<String, List<ContinueStmt>> labels) { |
68 | super.continues(labels); |
69 | labels.remove(this.label); |
70 | } |
71 | |
72 | @Override |
73 | public final Object getValue() { return this.label; } |
74 | |
75 | /** @return null if no label to render. */ |
76 | protected final String getRenderedLabel(RenderContext rc) { |
77 | if (label == null || "".equals(label)) { return null; } |
78 | StringBuilder escapedLabel = new StringBuilder(); |
79 | Escaping.escapeJsIdentifier(label, rc.isAsciiOnly(), escapedLabel); |
80 | return escapedLabel.toString(); |
81 | } |
82 | |
83 | protected JsonML wrapIfLabelled(JsonML unlabelled) { |
84 | if (label == null || "".equals(label)) { return unlabelled; } |
85 | return JsonMLBuilder.builder(TagType.LabelledStmt, getFilePosition()) |
86 | .setAttribute(TagAttr.LABEL, label) |
87 | .addChild(unlabelled) |
88 | .build(); |
89 | } |
90 | } |