First try for AST structure. A lot of things to change
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ClassDeclaration.java
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/ClassDeclaration.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/ast/ClassDeclaration.java
new file mode 100644 (file)
index 0000000..25fc716
--- /dev/null
@@ -0,0 +1,165 @@
+package net.sourceforge.phpdt.internal.compiler.ast;
+
+import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
+import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
+import net.sourceforge.phpdt.internal.ui.PHPUiImages;
+import org.eclipse.jface.resource.ImageDescriptor;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+
+
+/**
+ * This class is my ClassDeclaration declaration for php.
+ * It is similar to org.eclipse.jdt.internal.compiler.ast.TypeDeclaration
+ * It directly extends AstNode because a class cannot appear anywhere in php
+ * @author Matthieu Casanova
+ */
+public class ClassDeclaration extends Statement implements OutlineableWithChildren {
+
+  /** The name of the class. */
+  public char[] name;
+  /** The superclass. */
+  public char[] superclass;
+
+  public int declarationSourceStart;
+  public int declarationSourceEnd;
+  public int bodyStart;
+  public int bodyEnd;
+  /** The methods of the class. */
+  private ArrayList methods = new ArrayList();
+  /** The constructor of the class. */
+  public MethodDeclaration constructor;
+  /** The fields of the class. */
+  private ArrayList fields = new ArrayList();
+
+  private Object parent;
+  /** The outlineable children (those will be in the node array too. */
+  private ArrayList children = new ArrayList();
+
+  /**
+   * Create a class giving starting and ending offset
+   * @param sourceStart starting offset
+   * @param sourceEnd ending offset
+   */
+  public ClassDeclaration(Object parent,
+                          char[] name,
+                          char[] superclass,
+                          int sourceStart,
+                          int sourceEnd) {
+    super(sourceStart, sourceEnd);
+    this.parent = parent;
+    this.name = name;
+    this.superclass = superclass;
+  }
+
+    /**
+   * Create a class giving starting and ending offset
+   * @param sourceStart starting offset
+   * @param sourceEnd ending offset
+   */
+  public ClassDeclaration(Object parent,
+                          char[] name,
+                          int sourceStart,
+                          int sourceEnd) {
+    super(sourceStart, sourceEnd);
+    this.parent = parent;
+    this.name = name;
+  }
+
+  public void addMethod(MethodDeclaration method) {
+    method.add(method);
+    children.add(method);
+    if (method.name.equals(name)) {
+      constructor = method;
+    }
+  }
+
+  public void addVariable(FieldDeclaration var) {
+    for (int i = 0; i < var.vars.length; i++) {
+      VariableDeclaration c = var.vars[i];
+      children.add(c);
+    }
+    fields.add(var);
+  }
+
+  /**
+   * Tell if the class has a constructor.
+   * @return a boolean
+   */
+  public boolean hasConstructor() {
+    return constructor != null;
+  }
+
+  /**
+   * Return the class as String.
+   * @param tab how many tabs before the class
+   * @return the code of this class into String
+   */
+  public String toString(int tab) {
+    return tabString(tab) + toStringHeader() + toStringBody(tab);
+  }
+
+  /**
+   * Return the body of the class as String
+   * @param tab how many tabs before the body of the class
+   * @return the body as String
+   */
+  public String toStringBody(int tab) {
+    final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
+    if (fields != null) {
+      for (int i = 0; i < fields.size(); i++) {
+        FieldDeclaration field = (FieldDeclaration) fields.get(i);
+        buff.append("\n"); //$NON-NLS-1$
+        buff.append(field.toString(tab + 1));
+        buff.append(";");//$NON-NLS-1$
+      }
+    }
+    if (methods != null) {
+      for (int i = 0; i < methods.size(); i++) {
+        MethodDeclaration o = (MethodDeclaration) methods.get(i);
+        buff.append("\n");//$NON-NLS-1$
+        buff.append(o.toString(tab + 1));
+      }
+    }
+    buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
+    return buff.toString();
+  }
+
+  /**
+   * Return the header of the class as String.
+   * @return the header of the class
+   */
+  public String toStringHeader() {
+    final StringBuffer buff = new StringBuffer("class").append(name);//$NON-NLS-1$
+    if (superclass != null) {
+      buff.append(" extends "); //$NON-NLS-1$
+      buff.append(superclass);
+    }
+    return buff.toString();
+  }
+
+  /**
+   * Get the image of a class.
+   * @return the image that represents a php class
+   */
+  public ImageDescriptor getImage() {
+    return PHPUiImages.DESC_CLASS;
+  }
+
+  public Object getParent() {
+    return parent;
+  }
+
+  public boolean add(Outlineable o) {
+    return children.add(o);
+  }
+
+  public Outlineable get(int index) {
+    return (Outlineable) children.get(index);
+  }
+
+  public int size() {
+    return children.size();
+  }
+}