Package com.google.caja.parser.js

Source Code of com.google.caja.parser.js.MultiDeclaration

// Copyright (C) 2005 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.caja.parser.js;

import com.google.caja.SomethingWidgyHappenedError;
import com.google.caja.lexer.FilePosition;
import com.google.caja.lexer.TokenConsumer;
import com.google.caja.parser.ParseTreeNode;
import com.google.caja.reporting.RenderContext;
import com.google.javascript.jscomp.jsonml.JsonML;
import com.google.javascript.jscomp.jsonml.TagType;

import java.util.List;

/**
* A group of declarations as in
*   {@code var a, b, c;}
* or
*   {@code for (var a, b, c; ;)}
*
* @author mikesamuel@gmail.com
*/
public final class MultiDeclaration extends AbstractStatement {
  private static final long serialVersionUID = 4634282561782526243L;

  /** @param value unused.  This ctor is provided for reflection. */
  @ReflectiveCtor
  public MultiDeclaration(
      FilePosition pos, Void value, List<? extends Declaration> children) {
    this(pos, children);
  }

  public MultiDeclaration(FilePosition pos, List<? extends Declaration> decls) {
    super(pos, Declaration.class);
    createMutation().appendChildren(decls).execute();
  }

  @Override
  protected void childrenChanged() {
    super.childrenChanged();
    if (children().isEmpty()) { throw new IllegalStateException(); }
    for (ParseTreeNode child : children()) {
      if (!(child instanceof Declaration)) {
        throw new IllegalArgumentException("Child must be a declaration");
      }
    }
  }

  @Override
  public Object getValue() { return null; }

  @Override
  public List<? extends Declaration> children() {
    return childrenAs(Declaration.class);
  }

  public void render(RenderContext rc) {
    TokenConsumer out = rc.getOut();
    out.mark(getFilePosition());
    if (children().isEmpty()) {
      throw new SomethingWidgyHappenedError("Empty MultiDeclaration");
    }
    out.consume("var");
    boolean seen = false;
    for (Declaration decl : children()) {
      if (seen) {
        out.consume(",");
      } else {
        seen = true;
      }
      decl.renderShort(rc);
    }
  }

  public boolean hasHangingConditional() { return false; }

  public JsonML toJsonML() {
    JsonMLBuilder builder = JsonMLBuilder.builder(
        TagType.VarDecl, getFilePosition());
    for (Declaration d : children()) {
      builder.addChild(d.toInitOrIdPatt());
    }
    return builder.build();
  }
}
TOP

Related Classes of com.google.caja.parser.js.MultiDeclaration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.