improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CompilationUnitStructureRequestor.java
index 7b672a1..a09a6c6 100644 (file)
  *******************************************************************************/
 package net.sourceforge.phpdt.internal.core;
 
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Stack;
 
+import net.sourceforge.phpdt.internal.core.ImportContainer;
+import net.sourceforge.phpdt.internal.core.ImportDeclaration;
+import net.sourceforge.phpdt.internal.core.ImportDeclarationElementInfo;
 import net.sourceforge.phpdt.internal.core.JavaElement;
-import net.sourceforge.phpdt.internal.core.SourceField;
-import net.sourceforge.phpdt.internal.core.SourceFieldElementInfo;
-import net.sourceforge.phpdt.internal.core.SourceMethod;
-import net.sourceforge.phpdt.internal.core.SourceTypeElementInfo;
+import net.sourceforge.phpdt.internal.core.JavaElementInfo;
 
+import net.sourceforge.phpdt.core.Flags;
 import net.sourceforge.phpdt.core.ICompilationUnit;
 import net.sourceforge.phpdt.core.IField;
 import net.sourceforge.phpdt.core.IJavaElement;
@@ -41,17 +44,17 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
   /**
    * The handle to the compilation unit being parsed
    */
-  protected ICompilationUnit fUnit;
+  protected ICompilationUnit unit;
 
   /**
    * The info object for the compilation unit being parsed
    */
-  protected CompilationUnitElementInfo fUnitInfo;
+  protected CompilationUnitElementInfo unitInfo;
 
   /**
    * The import container info - null until created
    */
-  protected JavaElementInfo fImportContainerInfo = null;
+  protected JavaElementInfo importContainerInfo = null;
 
   /**
    * Hashtable of children elements of the compilation unit.
@@ -59,7 +62,7 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
    * the parser. Keys are handles, values are corresponding
    * info objects.
    */
-  protected Map fNewElements;
+  protected Map newElements;
 
   /**
    * Stack of parent scope info objects. The info on the
@@ -67,14 +70,14 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
    * For example, when we locate a method, the parent info object
    * will be the type the method is contained in.
    */
-  protected Stack fInfoStack;
+  protected Stack infoStack;
 
   /**
    * Stack of parent handles, corresponding to the info stack. We
    * keep both, since info objects do not have back pointers to
    * handles.
    */
-  protected Stack fHandleStack;
+  protected Stack handleStack;
 
   /**
    * The name of the source file being parsed.
@@ -131,49 +134,58 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
 
   protected CompilationUnitStructureRequestor(ICompilationUnit unit, CompilationUnitElementInfo unitInfo, Map newElements)
     throws JavaModelException {
-    this.fUnit = unit;
-    this.fUnitInfo = unitInfo;
-    this.fNewElements = newElements;
+    this.unit = unit;
+    this.unitInfo = unitInfo;
+    this.newElements = newElements;
     this.fSourceFileName = unit.getElementName().toCharArray();
   }
   /**
    * @see ISourceElementRequestor
    */
-  //public void acceptImport(int declarationStart, int declarationEnd, char[] name, boolean onDemand) {
-  //   JavaElementInfo parentInfo = (JavaElementInfo) fInfoStack.peek();
-  //   JavaElement parentHandle= (JavaElement)fHandleStack.peek();
-  //   if (!(parentHandle.getElementType() == IJavaElement.COMPILATION_UNIT)) {
-  //           Assert.isTrue(false); // Should not happen
-  //   }
-  //
-  //   ICompilationUnit parentCU= (ICompilationUnit)parentHandle;
-  //   //create the import container and its info
-  //   IImportContainer importContainer= parentCU.getImportContainer();
-  //   if (fImportContainerInfo == null) {
-  //           fImportContainerInfo= new JavaElementInfo();
-  //           fImportContainerInfo.setIsStructureKnown(true);
-  //           parentInfo.addChild(importContainer);
-  //           fNewElements.put(importContainer, fImportContainerInfo);
-  //   }
-  //   
-  //   // tack on the '.*' if it is onDemand
-  //   String importName;
-  //   if (onDemand) {
-  //           importName= new String(name) + ".*"; //$NON-NLS-1$
-  //   } else {
-  //           importName= new String(name);
-  //   }
-  //   
-  //   ImportDeclaration handle = new ImportDeclaration(importContainer, importName);
-  //   resolveDuplicates(handle);
-  //   
-  //   SourceRefElementInfo info = new SourceRefElementInfo();
-  //   info.setSourceRangeStart(declarationStart);
-  //   info.setSourceRangeEnd(declarationEnd);
-  //
-  //   fImportContainerInfo.addChild(handle);
-  //   fNewElements.put(handle, info);
-  //}
+  public void acceptImport(int declarationStart, int declarationEnd, char[] name, boolean onDemand)  {
+    //, int modifiers) {
+  
+       JavaElementInfo parentInfo = (JavaElementInfo) this.infoStack.peek();
+       JavaElement parentHandle= (JavaElement) this.handleStack.peek();
+       if (!(parentHandle.getElementType() == IJavaElement.COMPILATION_UNIT)) {
+               Assert.isTrue(false); // Should not happen
+       }
+
+       ICompilationUnit parentCU= (ICompilationUnit)parentHandle;
+       //create the import container and its info
+       ImportContainer importContainer= (ImportContainer)parentCU.getImportContainer();
+       if (this.importContainerInfo == null) {
+               this.importContainerInfo= new JavaElementInfo();
+               this.importContainerInfo.setIsStructureKnown(true);
+               parentInfo.addChild(importContainer);
+               this.newElements.put(importContainer, this.importContainerInfo);
+       }
+       
+       // tack on the '.*' if it is onDemand
+       String importName;
+       if (onDemand) {
+               importName= new String(name) + ".*"; //$NON-NLS-1$
+       } else {
+               importName= new String(name);
+       }
+       
+       ImportDeclaration handle = new ImportDeclaration(importContainer, importName);
+       resolveDuplicates(handle);
+       
+       ImportDeclarationElementInfo info = new ImportDeclarationElementInfo();
+       info.setSourceRangeStart(declarationStart);
+       info.setSourceRangeEnd(declarationEnd);
+//     info.setFlags(modifiers);
+       info.setName(name); // no trailing * if onDemand
+       info.setOnDemand(onDemand);
+
+       this.importContainerInfo.addChild(handle);
+       this.newElements.put(handle, info);
+  }
+  /**
+   * @see ISourceElementRequestor
+   */
+  
   /*
    * Table of line separator position. This table is passed once at the end
    * of the parse action, so as to allow computation of normalized ranges.
@@ -250,10 +262,10 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
    * @see ISourceElementRequestor
    */
   public void enterCompilationUnit() {
-    fInfoStack = new Stack();
-    fHandleStack = new Stack();
-    fInfoStack.push(fUnitInfo);
-    fHandleStack.push(fUnit);
+    infoStack = new Stack();
+    handleStack = new Stack();
+    infoStack.push(unitInfo);
+    handleStack.push(unit);
   }
   /**
    * @see ISourceElementRequestor
@@ -286,8 +298,8 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
    */
   public void enterField(int declarationStart, int modifiers, char[] type, char[] name, int nameSourceStart, int nameSourceEnd) {
 
-    SourceTypeElementInfo parentInfo = (SourceTypeElementInfo) fInfoStack.peek();
-    JavaElement parentHandle = (JavaElement) fHandleStack.peek();
+    SourceTypeElementInfo parentInfo = (SourceTypeElementInfo) infoStack.peek();
+    JavaElement parentHandle = (JavaElement) handleStack.peek();
     IField handle = null;
 
     if (parentHandle.getElementType() == IJavaElement.TYPE) {
@@ -306,10 +318,10 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
     info.setTypeName(type);
 
     parentInfo.addChild(handle);
-    fNewElements.put(handle, info);
+    newElements.put(handle, info);
 
-    fInfoStack.push(info);
-    fHandleStack.push(handle);
+    infoStack.push(info);
+    handleStack.push(handle);
   }
   /**
    * @see ISourceElementRequestor
@@ -395,11 +407,11 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
     boolean isConstructor) {
     SourceTypeElementInfo parentInfo = null;
     try {
-      parentInfo = (SourceTypeElementInfo) fInfoStack.peek();
+      parentInfo = (SourceTypeElementInfo) infoStack.peek();
     } catch (ClassCastException e) {
       //                       parentInfo = null;
     }
-    JavaElement parentHandle = (JavaElement) fHandleStack.peek();
+    JavaElement parentHandle = (JavaElement) handleStack.peek();
     IMethod handle = null;
 
     // translate nulls to empty arrays
@@ -438,13 +450,13 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
     info.setExceptionTypeNames(exceptionTypes);
 
     if (parentInfo == null) {
-      fUnitInfo.addChild(handle);
+      unitInfo.addChild(handle);
     } else {
       parentInfo.addChild(handle);
     }
-    fNewElements.put(handle, info);
-    fInfoStack.push(info);
-    fHandleStack.push(handle);
+    newElements.put(handle, info);
+    infoStack.push(info);
+    handleStack.push(handle);
   }
   /**
    * Common processing for classes and interfaces.
@@ -461,8 +473,8 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
     char[] enclosingTypeName = null;
     char[] qualifiedName = null;
 
-    JavaElementInfo parentInfo = (JavaElementInfo) fInfoStack.peek();
-    JavaElement parentHandle = (JavaElement) fHandleStack.peek();
+    JavaElementInfo parentInfo = (JavaElementInfo) infoStack.peek();
+    JavaElement parentHandle = (JavaElement) handleStack.peek();
     IType handle = null;
     String nameString = new String(name);
 
@@ -502,10 +514,10 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
     // }
 
     parentInfo.addChild(handle);
-    fNewElements.put(handle, info);
+    newElements.put(handle, info);
 
-    fInfoStack.push(info);
-    fHandleStack.push(handle);
+    infoStack.push(info);
+    handleStack.push(handle);
 
   }
   /**
@@ -519,10 +531,10 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
    * @see ISourceElementRequestor
    */
   public void exitCompilationUnit(int declarationEnd) {
-    fUnitInfo.setSourceLength(declarationEnd + 1);
+    unitInfo.setSourceLength(declarationEnd + 1);
 
     // determine if there were any parsing errors
-    fUnitInfo.setIsStructureKnown(!this.hasSyntaxErrors);
+    unitInfo.setIsStructureKnown(!this.hasSyntaxErrors);
   }
   /**
    * @see ISourceElementRequestor
@@ -530,30 +542,30 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
   public void exitConstructor(int declarationEnd) {
     exitMember(declarationEnd);
   }
-  ///**
-  // * @see ISourceElementRequestor
-  // */
-  //public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
-  //   SourceFieldElementInfo info = (SourceFieldElementInfo) fInfoStack.pop();
-  //   info.setSourceRangeEnd(declarationSourceEnd);
-  //   
-  //   // remember initializer source if field is a constant
-  //   if (initializationStart != -1) {
-  //           int flags = info.flags;
-  //           Object typeInfo;
-  //           if (Flags.isStatic(flags) && Flags.isFinal(flags)
-  //                           || ((typeInfo = fInfoStack.peek()) instanceof SourceTypeElementInfo
-  //                                    && (Flags.isInterface(((SourceTypeElementInfo)typeInfo).flags)))) {
-  //                   int length = declarationEnd - initializationStart;
-  //                   if (length > 0) {
-  //                           char[] initializer = new char[length];
-  //                           System.arraycopy(this.parser.scanner.source, initializationStart, initializer, 0, length);
-  //                           info.initializationSource = initializer;
-  //                   }
-  //           }
-  //   }
-  //   fHandleStack.pop();
-  //}
+  /**
+   * @see ISourceElementRequestor
+   */
+  public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
+       SourceFieldElementInfo info = (SourceFieldElementInfo) infoStack.pop();
+       info.setSourceRangeEnd(declarationSourceEnd);
+       
+       // remember initializer source if field is a constant
+       if (initializationStart != -1) {
+               int flags = info.flags;
+               Object typeInfo;
+               if (Flags.isStatic(flags) && Flags.isFinal(flags)
+                               || ((typeInfo = infoStack.peek()) instanceof SourceTypeElementInfo
+                                        && (Flags.isInterface(((SourceTypeElementInfo)typeInfo).flags)))) {
+                       int length = declarationEnd - initializationStart;
+                       if (length > 0) {
+                               char[] initializer = new char[length];
+                               System.arraycopy(this.parser.scanner.source, initializationStart, initializer, 0, length);
+                               info.initializationSource = initializer;
+                       }
+               }
+       }
+       handleStack.pop();
+  }
   /**
    * @see ISourceElementRequestor
    */
@@ -570,9 +582,9 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
    * common processing for classes and interfaces
    */
   protected void exitMember(int declarationEnd) {
-    SourceRefElementInfo info = (SourceRefElementInfo) fInfoStack.pop();
+    SourceRefElementInfo info = (SourceRefElementInfo) infoStack.pop();
     info.setSourceRangeEnd(declarationEnd);
-    fHandleStack.pop();
+    handleStack.pop();
   }
   /**
    * @see ISourceElementRequestor
@@ -586,7 +598,7 @@ public class CompilationUnitStructureRequestor extends ReferenceInfoAdapter impl
    * of the handle being created until there is no conflict.
    */
   protected void resolveDuplicates(IJavaElement handle) {
-    while (fNewElements.containsKey(handle)) {
+    while (newElements.containsKey(handle)) {
       JavaElement h = (JavaElement) handle;
       h.setOccurrenceCount(h.getOccurrenceCount() + 1);
     }