First try for AST structure. A lot of things to change
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Block.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import net.sourceforge.phpdt.internal.compiler.ast.AstNode;
4
5
6 /**
7  * A Block is
8  * {
9  * statements
10  * }.
11  * @author Matthieu Casanova
12  */
13 public class Block extends Statement {
14
15   /** An array of statements inside the block. */
16   public Statement[] statements;
17
18   /**
19    * Create a block.
20    * @param statements the statements
21    * @param sourceStart starting offset
22    * @param sourceEnd ending offset
23    */
24   public Block(Statement[] statements,int sourceStart, int sourceEnd) {
25     super(sourceStart, sourceEnd);
26     this.statements = statements;
27   }
28
29   public boolean isEmptyBlock() {
30     return statements == null;
31   }
32
33   public String toString(int tab) {
34     final String s = AstNode.tabString(tab);
35     final StringBuffer buff = new StringBuffer(s);
36     if (this.statements == null) {
37       buff.append("{\n"); //$NON-NLS-1$
38       buff.append(s);
39       buff.append("}"); //$NON-NLS-1$
40       return s;
41     }
42     buff.append("{\n"); //$NON-NLS-1$
43     buff.append(this.toStringStatements(tab));
44     buff.append(s);
45     buff.append("}"); //$NON-NLS-1$
46     return s;
47   }
48
49   public String toStringStatements(int tab) {
50     if (this.statements == null)
51       return ""; //$NON-NLS-1$
52     StringBuffer buffer = new StringBuffer();
53     for (int i = 0; i < statements.length; i++) {
54       buffer.append(statements[i].toString(tab + 1));
55       if (statements[i] instanceof Block) {
56         buffer.append("\n"); //$NON-NLS-1$
57       } else {
58         buffer.append(";\n"); //$NON-NLS-1$
59       }
60     }
61     return buffer.toString();
62   }
63 }