First try for AST structure. A lot of things to change
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / MethodDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import net.sourceforge.phpdt.internal.compiler.ast.Block;
4 import net.sourceforge.phpdt.internal.compiler.ast.ArgumentDeclaration;
5 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
6 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
7 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
8 import org.eclipse.jface.resource.ImageDescriptor;
9
10 import java.util.Hashtable;
11 import java.util.Enumeration;
12 import java.util.ArrayList;
13
14 /**
15  * A Method declaration.
16  * @author Matthieu Casanova
17  */
18 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
19
20   public char[] name;
21   public Hashtable arguments;
22   public Statement[] statements;
23   public int bodyStart;
24   public int bodyEnd = -1;
25   public boolean isConstructor;
26   private Object parent;
27   /** The outlineable children (those will be in the node array too. */
28   private ArrayList children = new ArrayList();
29
30   public boolean reference;
31
32   public MethodDeclaration(Object parent,
33                            char[] name,
34                            Hashtable arguments,
35                            boolean reference,
36                            int sourceStart,
37                            int sourceEnd) {
38     super(sourceStart, sourceEnd);
39     this.name = name;
40     this.arguments = arguments;
41     this.parent = parent;
42     this.reference = reference;
43   }
44
45   /**
46    * Return method into String, with a number of tabs
47    * @param tab the number of tabs
48    * @return the String containing the method
49    */
50   public String toString(int tab) {
51     StringBuffer buff = new StringBuffer(tabString(tab));
52     buff.append("function ");//$NON-NLS-1$
53     if (reference) {
54       buff.append('&');//$NON-NLS-1$
55     }
56     buff.append(name).append("(");//$NON-NLS-1$
57
58     if (arguments != null) {
59       Enumeration values = arguments.elements();
60       int i = 0;
61       while (values.hasMoreElements()) {
62         ArgumentDeclaration o = (ArgumentDeclaration) values.nextElement();
63         buff.append(o.toString(0));
64         if (i != (arguments.size() - 1)) {
65           buff.append(", "); //$NON-NLS-1$
66         }
67         i++;
68       }
69     }
70     buff.append(")"); //$NON-NLS-1$
71
72    buff.append(toStringStatements(tab + 1));
73     return buff.toString();
74   }
75
76   /**
77    * Return the statements of the method into Strings
78    * @param tab the number of tabs
79    * @return the String containing the statements
80    */
81   public String toStringStatements(int tab) {
82     StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
83     if (statements != null) {
84       for (int i = 0; i < statements.length; i++) {
85         buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
86         if (!(statements[i] instanceof Block)) {
87           buff.append(";"); //$NON-NLS-1$
88         }
89       }
90     }
91     buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
92     return buff.toString();
93   }
94
95   /**
96    * Get the image of a class.
97    * @return the image that represents a php class
98    */
99   public ImageDescriptor getImage() {
100     return PHPUiImages.DESC_FUN;
101   }
102
103   public Object getParent() {
104     return parent;
105   }
106
107   public boolean add(Outlineable o) {
108     return children.add(o);
109   }
110
111   public Outlineable get(int index) {
112     return (Outlineable) children.get(index);
113   }
114
115   public int size() {
116     return children.size();
117   }
118 }