01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.js;
17:
18: import com.google.gwt.dev.js.ast.JsStatement;
19: import com.google.gwt.dev.js.ast.JsVisitable;
20: import com.google.gwt.dev.js.ast.JsVisitor;
21:
22: import java.util.ArrayList;
23: import java.util.Iterator;
24: import java.util.List;
25:
26: class FlatteningVisitor extends JsVisitor {
27:
28: public static TreeNode exec(List<JsStatement> statements) {
29: FlatteningVisitor visitor = new FlatteningVisitor();
30: visitor.acceptList(statements);
31: return visitor.root;
32: }
33:
34: public static class TreeNode {
35: public final JsVisitable<?> node;
36: public final List<TreeNode> children = new ArrayList<TreeNode>();
37:
38: public TreeNode(JsVisitable<?> node) {
39: this .node = node;
40: }
41: }
42:
43: private TreeNode root;
44:
45: private FlatteningVisitor() {
46: root = new TreeNode(null);
47: }
48:
49: protected <T extends JsVisitable<T>> T doAccept(T node) {
50: TreeNode oldRoot = root;
51: root = new TreeNode(node);
52: oldRoot.children.add(root);
53: super .doAccept(node);
54: root = oldRoot;
55: return node;
56: }
57:
58: protected <T extends JsVisitable<T>> void doAcceptList(
59: List<T> collection) {
60: for (Iterator<T> it = collection.iterator(); it.hasNext();) {
61: doAccept(it.next());
62: }
63: }
64: }
|