Package com.google.caja.parser.js

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

// 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.lexer.FilePosition;
import com.google.caja.lexer.TokenConsumer;
import com.google.caja.parser.ParseTreeNode;
import com.google.caja.reporting.RenderContext;
import com.google.caja.util.Pair;
import com.google.javascript.jscomp.jsonml.JsonML;
import com.google.javascript.jscomp.jsonml.TagType;

import java.util.List;

/**
* An if/else if/else statement.
*
* @author mikesamuel@gmail.com
*/
public final class Conditional extends AbstractStatement {
  private static final long serialVersionUID = 7726408694815849867L;

  /** @param value unused.  This ctor is provided for reflection. */
  @ReflectiveCtor
  public Conditional(
      FilePosition pos, Void value, List<? extends ParseTreeNode> children) {
    super(pos, ParseTreeNode.class);
    createMutation().appendChildren(children).execute();
  }

  public Conditional(
      FilePosition pos, List<Pair<Expression, Statement>> ifClauses,
      Statement elseClause) {
    super(pos, ParseTreeNode.class);
    if (ifClauses.isEmpty()) { throw new IllegalArgumentException(); }
    Mutation m = createMutation();
    for (Pair<Expression, Statement> p : ifClauses) {
      Expression condition = p.a;
      Statement then = p.b;
      if (null == condition || null == then) {
        throw new IllegalArgumentException();
      }
      m.appendChild(condition);
      m.appendChild(then);
    }
    if (null != elseClause) {
      m.appendChild(elseClause);
    }
    m.execute();
  }

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

  public void render(RenderContext rc) {
    TokenConsumer out = rc.getOut();
    out.mark(getFilePosition());
    List<? extends ParseTreeNode> children = children();
    int i = 0;
    int n = children.size();
    for (; i + 2 <= n; i += 2) {
      Expression condition = (Expression) children.get(i);
      Statement body = (Statement) children.get(i + 1);

      if (i != 0) {
        out.consume("else");
      }
      out.consume("if");
      out.consume("(");
      condition.render(rc);
      out.consume(")");
      boolean hanging = body.hasHangingConditional();
      if (hanging) { out.consume("{"); }
      body.renderBlock(rc, i + 2 < n);
      if (hanging) { out.consume("}"); }
    }
    if (i < n) {
      Statement body = (Statement) children.get(i);
      out.consume("else");
      body.renderBlock(rc, false);
    }
  }

  public boolean hasHangingConditional() {
    List<? extends ParseTreeNode> children = children();
    int n = children.size();
    // If there is no else clause, then an else following would change the
    // meaning.
    if ((n & 1) == 0) { return true; }
    // Otherwise if the else clause has a hanging conditional, then an else
    // would change the meaning.
    return ((Statement) children.get(n - 1)).hasHangingConditional();
  }

  public JsonML toJsonML() {
    FilePosition tailPos = FilePosition.endOf(getFilePosition());
    JsonML tail;
    List<? extends ParseTreeNode> parts = children();
    int k = parts.size();
    if ((k & 1) == 0) {
      tail = JsonMLBuilder.builder(TagType.EmptyStmt, tailPos).build();
    } else {
      tail = ((Statement) parts.get(--k)).toJsonML();
    }
    while (k != 0) {
      Expression cond = (Expression) parts.get(k - 2);
      Statement clause = (Statement) parts.get(k - 1);
      FilePosition pos = k == 2 ? getFilePosition()
          : FilePosition.span(cond.getFilePosition(), tailPos);
      tail = JsonMLBuilder.builder(TagType.IfStmt, pos)
          .addChild(cond)
          .addChild(clause)
          .addChild(tail)
          .build();
      k -= 2;
    }
    return tail;
  }
}
TOP

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

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.