001 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
002 // for details. All rights reserved. Use of this source code is governed by a
003 // BSD-style license that can be found in the LICENSE file.
004
005 package com.google.dart.compiler.util;
006
007 import com.google.dart.compiler.backend.js.ast.*;
008 import com.intellij.util.SmartList;
009 import org.jetbrains.annotations.NotNull;
010 import org.jetbrains.annotations.Nullable;
011
012 import java.util.ArrayList;
013 import java.util.List;
014
015 public final class AstUtil {
016 private AstUtil() {
017 }
018
019 /**
020 * Returns a sequence of expressions (using the binary sequence operator).
021 *
022 * @param exprs - expressions to add to sequence
023 * @return a sequence of expressions.
024 */
025 public static JsBinaryOperation newSequence(JsExpression... exprs) {
026 if (exprs.length < 2) {
027 throw new RuntimeException("newSequence expects at least two arguments");
028 }
029 JsExpression result = exprs[exprs.length - 1];
030 for (int i = exprs.length - 2; i >= 0; i--) {
031 result = new JsBinaryOperation(JsBinaryOperator.COMMA, exprs[i], result);
032 }
033 return (JsBinaryOperation) result;
034 }
035
036 @Nullable
037 public static <T extends JsNode> T deepCopy(@Nullable T node) {
038 if (node == null) return null;
039
040 //noinspection unchecked
041 return (T) node.deepCopy();
042 }
043
044 @NotNull
045 public static <T extends JsNode> List<T> deepCopy(@Nullable List<T> nodes) {
046 if (nodes == null) return new SmartList<T>();
047
048 List<T> nodesCopy = new ArrayList<T>(nodes.size());
049 for (T node : nodes) {
050 nodesCopy.add(deepCopy(node));
051 }
052
053 return nodesCopy;
054 }
055 }