Package com.google.caja.parser.js

Examples of com.google.caja.parser.js.CaseStmt


      }
    }
    // Eliminate unnecessary breaks
    if (!hasDefault) {
      for (int i = newChildren.size(); --i >= 1;) {
        CaseStmt cs = (CaseStmt) newChildren.get(i);
        Block body = cs.getBody();
        if (!isBlankBreak(body)) { continue; }
        if (cs.getCaseValue().simplifyForSideEffect() != null) { continue; }
        if (i != 1) {
          SwitchCase prev = (SwitchCase) newChildren.get(i - 1);
          if (!exits(prev.getBody())) {
            // Move the break into the preceding case.
            newChildren.set(
                i - 1, withBody(prev, combine(prev.getBody(), body)));
          }
        }
        newChildren.remove(i);
        changed = true;
      }
    }
    // Eliminate duplicate cases
    SwitchCase last = null;
    for (int i = 1; i < newChildren.size(); ++i) {
      SwitchCase cs = (SwitchCase) newChildren.get(i);
      if (last != null && !last.getBody().children().isEmpty()
          && ParseTreeNodes.deepEquals(last.getBody(), cs.getBody())) {
        newChildren.set(i - 1, withoutBody(last));
        changed = true;
      }
      last = cs;
    }
    while (newChildren.size() > 1) {
      int lastIndex = newChildren.size() - 1;
      last = (SwitchCase) newChildren.get(lastIndex);

      Block lastBody = last.getBody();
      boolean changedOne = false;
      if (!lastBody.children().isEmpty()) {
        // Eliminate trailing break statement.
        List<? extends Statement> stmts = lastBody.children();
        int n = stmts.size();
        if (n > 0 && isBlankBreak(stmts.get(n - 1))) {
          stmts = stmts.subList(0, n - 1);
          Block newBody = new Block(lastBody.getFilePosition(), stmts);
          newChildren.set(newChildren.size() - 1, withBody(last, newBody));
          changedOne = true;
        }
      } else if (isBlankBreak(lastBody)) {
        //    switch (...) { default: ... case foo: break; }
        // => switch (...) { default: ... case foo: }
        newChildren.set(lastIndex, withoutBody(last));
        changedOne = true;
      } else if (!hasDefault) {
        CaseStmt cs = (CaseStmt) last;  // OK since !hasDefault
        //    switch (...) { ... case 4: case 5: break; }
        // => switch (...) { ... }
        // We can eliminate cases entirely if there is no default since it
        // will not change the set of values that default matches.
        if (null == cs.getCaseValue().simplifyForSideEffect()) {
          newChildren.remove(lastIndex);
          changedOne = true;
        }
      }
      if (changedOne) {
View Full Code Here


  private static SwitchCase withBody(SwitchCase sc, Block body) {
    if (sc instanceof DefaultCaseStmt) {
      return new DefaultCaseStmt(sc.getFilePosition(), body);
    } else {
      CaseStmt cs = (CaseStmt) sc;
      return new CaseStmt(sc.getFilePosition(), cs.getCaseValue(), body);
    }
  }
View Full Code Here

      }
    }
    // Eliminate unnecessary breaks
    if (!hasDefault) {
      for (int i = newChildren.size(); --i >= 1;) {
        CaseStmt cs = (CaseStmt) newChildren.get(i);
        Block body = cs.getBody();
        if (!isBlankBreak(body)) { continue; }
        if (cs.getCaseValue().simplifyForSideEffect() != null) { continue; }
        if (i != 1) {
          SwitchCase prev = (SwitchCase) newChildren.get(i - 1);
          if (!exits(prev.getBody())) {
            // Move the break into the preceding case.
            newChildren.set(
                i - 1, withBody(prev, combine(prev.getBody(), body)));
          }
        }
        newChildren.remove(i);
        changed = true;
      }
    }
    // Eliminate duplicate cases
    SwitchCase last = null;
    for (int i = 1; i < newChildren.size(); ++i) {
      SwitchCase cs = (SwitchCase) newChildren.get(i);
      if (last != null && !last.getBody().children().isEmpty()
          && ParseTreeNodes.deepEquals(last.getBody(), cs.getBody())) {
        newChildren.set(i - 1, withoutBody(last));
        changed = true;
      }
      last = cs;
    }
    while (newChildren.size() > 1) {
      int lastIndex = newChildren.size() - 1;
      last = (SwitchCase) newChildren.get(lastIndex);

      Block lastBody = last.getBody();
      boolean changedOne = false;
      if (!lastBody.children().isEmpty()) {
        // Eliminate trailing break statement.
        List<? extends Statement> stmts = lastBody.children();
        int n = stmts.size();
        if (n > 0 && isBlankBreak(stmts.get(n - 1))) {
          stmts = stmts.subList(0, n - 1);
          Block newBody = new Block(lastBody.getFilePosition(), stmts);
          newChildren.set(newChildren.size() - 1, withBody(last, newBody));
          changedOne = true;
        }
      } else if (isBlankBreak(lastBody)) {
        //    switch (...) { default: ... case foo: break; }
        // => switch (...) { default: ... case foo: }
        newChildren.set(lastIndex, withoutBody(last));
        changedOne = true;
      } else if (!hasDefault) {
        CaseStmt cs = (CaseStmt) last;  // OK since !hasDefault
        //    switch (...) { ... case 4: case 5: break; }
        // => switch (...) { ... }
        // We can eliminate cases entirely if there is no default since it
        // will not change the set of values that default matches.
        if (null == cs.getCaseValue().simplifyForSideEffect()) {
          newChildren.remove(lastIndex);
          changedOne = true;
        }
      }
      if (changedOne) {
View Full Code Here

  private static SwitchCase withBody(SwitchCase sc, Block body) {
    if (sc instanceof DefaultCaseStmt) {
      return new DefaultCaseStmt(sc.getFilePosition(), body);
    } else {
      CaseStmt cs = (CaseStmt) sc;
      return new CaseStmt(sc.getFilePosition(), cs.getCaseValue(), body);
    }
  }
View Full Code Here

TOP

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

Copyright © 2018 www.massapicom. 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.