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 | import com.google.javascript.jscomp.jsonml.TagType; |
23 | |
24 | import java.util.List; |
25 | |
26 | /** |
27 | * |
28 | * @author mikesamuel@gmail.com |
29 | */ |
30 | public class WhileLoop extends Loop { |
31 | private Expression condition; |
32 | private Statement body; |
33 | |
34 | @ReflectiveCtor |
35 | public WhileLoop( |
36 | FilePosition pos, String label, List<? extends ParseTreeNode> children) { |
37 | this(pos, label, (Expression) children.get(0), (Statement) children.get(1)); |
38 | } |
39 | |
40 | public WhileLoop( |
41 | FilePosition pos, String label, Expression condition, Statement body) { |
42 | super(pos, label, ParseTreeNode.class); |
43 | createMutation().appendChild(condition).appendChild(body).execute(); |
44 | } |
45 | |
46 | @Override |
47 | protected void childrenChanged() { |
48 | super.childrenChanged(); |
49 | this.condition = (Expression) children().get(0); |
50 | this.body = (Statement) children().get(1); |
51 | } |
52 | |
53 | @Override |
54 | public Expression getCondition() { return condition; } |
55 | @Override |
56 | public Statement getBody() { return body; } |
57 | |
58 | public void render(RenderContext rc) { |
59 | TokenConsumer out = rc.getOut(); |
60 | out.mark(getFilePosition()); |
61 | String label = getRenderedLabel(rc); |
62 | if (null != label) { |
63 | out.consume(label); |
64 | out.consume(":"); |
65 | } |
66 | out.consume("while"); |
67 | out.consume("("); |
68 | condition.render(rc); |
69 | out.consume(")"); |
70 | body.renderBlock(rc, false); |
71 | } |
72 | |
73 | public boolean hasHangingConditional() { |
74 | return body.hasHangingConditional(); |
75 | } |
76 | |
77 | public JsonML toJsonML() { |
78 | return wrapIfLabelled( |
79 | JsonMLBuilder.builder(TagType.WhileStmt, getFilePosition()) |
80 | .addChild(condition).addChild(body).build()); |
81 | } |
82 | } |