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.reporting.RenderContext; |
18 | import com.google.caja.lexer.FilePosition; |
19 | import com.google.caja.lexer.TokenConsumer; |
20 | import com.google.caja.parser.ParseTreeNode; |
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.ArrayList; |
26 | import java.util.List; |
27 | import java.util.Map; |
28 | |
29 | /** |
30 | * |
31 | * @author mikesamuel@gmail.com |
32 | */ |
33 | public final class BreakStmt extends AbstractStatement { |
34 | private final String label; |
35 | |
36 | /** @param children unused. This ctor is provided for reflection. */ |
37 | @ReflectiveCtor |
38 | public BreakStmt( |
39 | FilePosition pos, String value, List<? extends ParseTreeNode> children) { |
40 | this(pos, value); |
41 | } |
42 | |
43 | public BreakStmt(FilePosition pos, String label) { |
44 | super(pos, Statement.class); |
45 | this.label = label; |
46 | } |
47 | |
48 | @Override |
49 | public void breaks(Map<String, List<BreakStmt>> breaksReaching) { |
50 | List<BreakStmt> breaks = breaksReaching.get(this.label); |
51 | if (null == breaks) { |
52 | breaksReaching.put(label, breaks = new ArrayList<BreakStmt>()); |
53 | } |
54 | breaks.add(this); |
55 | } |
56 | |
57 | @Override |
58 | public String getValue() { return label; } |
59 | |
60 | public String getLabel() { return label; } |
61 | |
62 | public void render(RenderContext rc) { |
63 | TokenConsumer out = rc.getOut(); |
64 | out.mark(getFilePosition()); |
65 | out.consume("break"); |
66 | if (null != label && !"".equals(label)) { |
67 | out.consume(label); |
68 | } |
69 | } |
70 | |
71 | public boolean hasHangingConditional() { return false; } |
72 | |
73 | public JsonML toJsonML() { |
74 | return JsonMLBuilder.builder(TagType.BreakStmt, getFilePosition()) |
75 | .setAttributeIfNotBlank(TagAttr.LABEL, label).build(); |
76 | } |
77 | } |