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.TokenConsumer; |
19 | import com.google.caja.parser.ParseTreeNode; |
20 | import com.google.caja.reporting.RenderContext; |
21 | import com.google.javascript.jscomp.jsonml.JsonML; |
22 | |
23 | import java.util.List; |
24 | |
25 | /** |
26 | * A labeled statement implementation that can apply to any statement. |
27 | * Javascript allows labels on statements other than loops. |
28 | * |
29 | * @author mikesamuel@gmail.com |
30 | */ |
31 | public final class LabeledStmtWrapper extends LabeledStatement { |
32 | // TODO(mikesamuel): Investigate whether use of continue to a non loop |
33 | // functions as goto, and whether that can introduces vulnerabilities. |
34 | // TODO(mikesamuel): Do we want to remove labelling of non-loop statements |
35 | // from Caja? |
36 | // TODO(mikesamuel): Erase the distinction between LabeledStmtWrapper and |
37 | // LabeledStatement. |
38 | private Statement body; |
39 | |
40 | @ReflectiveCtor |
41 | public LabeledStmtWrapper( |
42 | FilePosition pos, String value, List<? extends ParseTreeNode> children) { |
43 | this(pos, value, (Statement)children.get(0)); |
44 | } |
45 | |
46 | public LabeledStmtWrapper(FilePosition pos, String label, Statement body) { |
47 | super(pos, label, Statement.class); |
48 | appendChild(body); |
49 | } |
50 | |
51 | public Statement getBody() { return body; } |
52 | |
53 | @Override |
54 | public boolean isTargetForContinue() { return false; } |
55 | |
56 | @Override |
57 | protected void childrenChanged() { |
58 | super.childrenChanged(); |
59 | this.body = (Statement) children().get(0); |
60 | } |
61 | |
62 | public void render(RenderContext rc) { |
63 | TokenConsumer out = rc.getOut(); |
64 | out.mark(getFilePosition()); |
65 | String label = getRenderedLabel(rc); |
66 | if (null != label) { |
67 | out.consume(label); |
68 | out.consume(":"); |
69 | } |
70 | body.render(rc); |
71 | } |
72 | |
73 | @Override |
74 | public boolean isTerminal() { |
75 | return body.isTerminal(); |
76 | } |
77 | |
78 | public boolean hasHangingConditional() { |
79 | return body.hasHangingConditional(); |
80 | } |
81 | |
82 | public JsonML toJsonML() { |
83 | return wrapIfLabelled(body.toJsonML()); |
84 | } |
85 | } |