示例#1
0
  /**
   * @param node The node we want to have wrapped.
   * @return The (new) node parent (the block probably)
   */
  private AstNode makeSureBlockExistsAround(AstNode node) {
    AstNode parent = node.getParent();

    if (parent instanceof IfStatement) {
      /* the parent is an if and there are no braces, so we should make a new block */
      IfStatement i = (IfStatement) parent;

      /* replace the if or the then, depending on what the current node is */
      if (i.getThenPart().equals(node)) {
        i.setThenPart(createBlockWithNode(node));
      } else if (i.getElsePart() != null) {
        if (i.getElsePart().equals(node)) i.setElsePart(createBlockWithNode(node));
      }

    } else if (parent instanceof WhileLoop) {
      /* the parent is a while and there are no braces, so we should make a new block */
      /* I don't think you can find this in the real world, but just to be sure */
      WhileLoop w = (WhileLoop) parent;
      if (w.getBody().equals(node)) w.setBody(createBlockWithNode(node));
    } else if (parent instanceof ForLoop) {
      /* the parent is a for and there are no braces, so we should make a new block */
      /* I don't think you can find this in the real world, but just to be sure */
      ForLoop f = (ForLoop) parent;
      if (f.getBody().equals(node)) f.setBody(createBlockWithNode(node));
    }

    return node.getParent();
  }
 @Override
 public AstNode forLoop(AstNode init, AstNode condition, AstNode update, AstNode body) {
   ForLoop loop = new ForLoop();
   loop.setInitializer(init);
   loop.setCondition(condition);
   loop.setIncrement(update);
   loop.setBody(body);
   return loop;
 }
示例#3
0
  public void testLinenoFor() {
    AstRoot root = parse("\nfor(\n" + ";\n" + ";\n" + ") {\n" + "}\n");

    ForLoop forLoop = (ForLoop) root.getFirstChild();
    AstNode initClause = forLoop.getInitializer();
    AstNode condClause = forLoop.getCondition();
    AstNode incrClause = forLoop.getIncrement();

    assertEquals(1, forLoop.getLineno());
    assertEquals(2, initClause.getLineno());
    assertEquals(3, condClause.getLineno());
    assertEquals(4, incrClause.getLineno());
  }