improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / SourceElementParser.java
index 21df17c..fc27be9 100644 (file)
@@ -23,16 +23,15 @@ import net.sourceforge.phpdt.internal.compiler.lookup.ClassScope;
 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
 import net.sourceforge.phpdt.internal.core.util.CommentRecorderParser;
+import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode;
 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.AnonymousLocalTypeDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.Argument;
-import net.sourceforge.phpeclipse.internal.compiler.ast.AstNode;
 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.ConstructorDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.ExplicitConstructorCall;
 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.ImportReference;
-import net.sourceforge.phpeclipse.internal.compiler.ast.LocalTypeDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.MemberTypeDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.NameReference;
@@ -80,35 +79,27 @@ public class SourceElementParser extends CommentRecorderParser {//extends
   /**
    * An ast visitor that visits local type declarations.
    */
-  public class LocalDeclarationVisitor extends AbstractSyntaxTreeVisitorAdapter {
-    // public boolean visit(
-    //                 AnonymousLocalTypeDeclaration anonymousTypeDeclaration,
-    //                 BlockScope scope) {
-    //         notifySourceElementRequestor(anonymousTypeDeclaration, sourceType ==
-    // null);
-    //         return false; // don't visit members as this was done during
-    // notifySourceElementRequestor(...)
-    // }
-    public boolean visit(LocalTypeDeclaration typeDeclaration, BlockScope scope) {
-      notifySourceElementRequestor(typeDeclaration, sourceType == null);
-      return false; // don't visit members as this was done during
-                    // notifySourceElementRequestor(...)
-    }
-    public boolean visit(MemberTypeDeclaration typeDeclaration, ClassScope scope) {
-      notifySourceElementRequestor(typeDeclaration, sourceType == null);
-      return false; // don't visit members as this was done during
-                    // notifySourceElementRequestor(...)
-    }
-
+  public class LocalDeclarationVisitor extends ASTVisitor {
+       public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
+               notifySourceElementRequestor(typeDeclaration, sourceType == null);
+               return false; // don't visit members as this was done during notifySourceElementRequestor(...)
+       }
+       public boolean visit(TypeDeclaration typeDeclaration, ClassScope scope) {
+               notifySourceElementRequestor(typeDeclaration, sourceType == null);
+               return false; // don't visit members as this was done during notifySourceElementRequestor(...)
+       }
+       
   }
 
   public SourceElementParser(final ISourceElementRequestor requestor, IProblemFactory problemFactory, CompilerOptions options) {
     // we want to notify all syntax error with the acceptProblem API
     // To do so, we define the record method of the ProblemReporter
-    super(new ProblemReporter(DefaultErrorHandlingPolicies.exitAfterAllProblems(), options, problemFactory) {
+    super( new ProblemReporter(DefaultErrorHandlingPolicies.exitAfterAllProblems(), options, problemFactory) {
       public void record(IProblem problem, CompilationResult unitResult, ReferenceContext referenceContext) {
         unitResult.record(problem, referenceContext);
-        requestor.acceptProblem(problem);
+        if (requestor!=null) {
+          requestor.acceptProblem(problem);
+        }
       }
     });
     // true);
@@ -124,21 +115,21 @@ public class SourceElementParser extends CommentRecorderParser {//extends
    * @deprecated use SourceElementParser(ISourceElementRequestor,
    *             IProblemFactory, CompilerOptions)
    */
-  //public SourceElementParser(
-  //   final ISourceElementRequestor requestor,
-  //   IProblemFactory problemFactory) {
-  //           this(requestor, problemFactory, new CompilerOptions());
-  //}
-  //public SourceElementParser(
-  //   final ISourceElementRequestor requestor,
-  //   IProblemFactory problemFactory,
-  //   CompilerOptions options,
-  //   boolean reportLocalDeclarations) {
-  //           this(requestor, problemFactory, options);
-  //           if (reportLocalDeclarations) {
-  //                   this.localDeclarationVisitor = new LocalDeclarationVisitor();
-  //           }
-  //}
+  public SourceElementParser(
+       final ISourceElementRequestor requestor,
+       IProblemFactory problemFactory) {
+               this(requestor, problemFactory, new CompilerOptions());
+  }
+  public SourceElementParser(
+       final ISourceElementRequestor requestor,
+       IProblemFactory problemFactory,
+       CompilerOptions options,
+       boolean reportLocalDeclarations) {
+               this(requestor, problemFactory, options);
+               if (reportLocalDeclarations) {
+                       this.localDeclarationVisitor = new LocalDeclarationVisitor();
+               }
+  }
   //public void checkAnnotation() {
   //   int firstCommentIndex = scanner.commentPtr;
   //
@@ -514,7 +505,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
   //                   new SingleNameReference(
   //                           identifierStack[identifierPtr],
   //                           identifierPositionStack[identifierPtr--]);
-  //           ref.bits &= ~AstNode.RestrictiveFlagMASK;
+  //           ref.bits &= ~ASTNode.RestrictiveFlagMASK;
   //           ref.bits |= LOCAL | FIELD;
   //           if (reportReferenceInfo) {
   //                   this.addUnknownRef(ref);
@@ -541,7 +532,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
   //                   (int) (identifierPositionStack[identifierPtr + 1] >> 32),
   //   // sourceStart
   //    (int) identifierPositionStack[identifierPtr + length]); // sourceEnd
-  //   ref.bits &= ~AstNode.RestrictiveFlagMASK;
+  //   ref.bits &= ~ASTNode.RestrictiveFlagMASK;
   //   ref.bits |= LOCAL | FIELD;
   //   if (reportReferenceInfo) {
   //           this.addUnknownRef(ref);
@@ -584,7 +575,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
     // }
     // collect the top level ast nodes
     int length = 0;
-    AstNode[] nodes = null;
+    ASTNode[] nodes = null;
     if (sourceType == null) {
       if (isInRange) {
         requestor.enterCompilationUnit();
@@ -598,9 +589,9 @@ public class SourceElementParser extends CommentRecorderParser {//extends
         //                     (currentPackage == null ? 0 : 1)
         //                     + (imports == null ? 0 : imports.length)
         //                     + (types == null ? 0 : types.length);
-        //             nodes = new AstNode[length];
+        //             nodes = new ASTNode[length];
         length = (imports == null ? 0 : imports.length) + types.size();
-        nodes = new AstNode[length];
+        nodes = new ASTNode[length];
         int index = 0;
         //             if (currentPackage != null) {
         //                     nodes[index++] = currentPackage;
@@ -612,7 +603,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
         }
 
         for (int i = 0, max = types.size(); i < max; i++) {
-          nodes[index++] = (AstNode) types.get(i);
+          nodes[index++] = (ASTNode) types.get(i);
         }
       }
     } else {
@@ -620,9 +611,9 @@ public class SourceElementParser extends CommentRecorderParser {//extends
       ArrayList types = parsedUnit.types;
       if (types != null) {
         length = types.size();
-        nodes = new AstNode[length];
+        nodes = new ASTNode[length];
         for (int i = 0, max = types.size(); i < max; i++) {
-          nodes[i] = (AstNode) types.get(i);
+          nodes[i] = (ASTNode) types.get(i);
         }
       }
     }
@@ -631,7 +622,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
     if (nodes != null && length > 0) {
       quickSort(nodes, 0, length - 1);
       for (int i = 0; i < length; i++) {
-        AstNode node = nodes[i];
+        ASTNode node = nodes[i];
                                if (node instanceof ImportReference) {
                                        ImportReference importRef = (ImportReference)node;
 //                                     if (node == parsedUnit.currentPackage) {
@@ -910,7 +901,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
 //     }
   }
   public void notifySourceElementRequestor(TypeDeclaration typeDeclaration, boolean notifyTypePresence) {
-    //// public void notifySourceElementRequestor(AstNode typeDeclaration,
+    //// public void notifySourceElementRequestor(ASTNode typeDeclaration,
     // boolean notifyTypePresence) {
 
     // range check
@@ -919,7 +910,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
 
     FieldDeclaration[] fields = typeDeclaration.fields;
     AbstractMethodDeclaration[] methods = typeDeclaration.methods;
-    MemberTypeDeclaration[] memberTypes = typeDeclaration.memberTypes;
+    TypeDeclaration[] memberTypes = typeDeclaration.memberTypes;
     int fieldCount = fields == null ? 0 : fields.length;
     int methodCount = methods == null ? 0 : methods.length;
     int memberTypeCount = memberTypes == null ? 0 : memberTypes.length;
@@ -1115,6 +1106,38 @@ public class SourceElementParser extends CommentRecorderParser {//extends
     return null;
   }
 
+  public CompilationUnitDeclaration parseCompletionUnit(ICompilationUnit unit, boolean fullParse) {
+
+    //         boolean old = diet;
+    //         if (fullParse) {
+    //                 unknownRefs = new NameReference[10];
+    //                 unknownRefsCounter = 0;
+    //         }
+
+    try {
+      //                       diet = true;
+      this.reportReferenceInfo = fullParse;
+      CompilationResult compilationUnitResult = new CompilationResult(unit, 0, 0, this.options.maxProblemsPerUnit);
+      CompilationUnitDeclaration parsedUnit = parse(unit, compilationUnitResult, false);
+//      if (scanner.recordLineSeparator) {
+//        requestor.acceptLineSeparatorPositions(scanner.getLineEnds());
+//      }
+//      int initialStart = this.scanner.initialPosition;
+//      int initialEnd = this.scanner.eofPosition;
+//      //                     if (this.localDeclarationVisitor != null || fullParse){
+//      //                             diet = false;
+//      //                             this.getMethodBodies(parsedUnit);
+//      //                     }
+//      this.scanner.resetTo(initialStart, initialEnd);
+//      notifySourceElementRequestor(parsedUnit);
+      return parsedUnit;
+    } catch (AbortCompilation e) {
+      // ignore this exception
+    } finally {
+      //                       diet = old;
+    }
+    return null;
+  }
   //public void parseTypeMemberDeclarations(
   //   ISourceType sourceType,
   //   ICompilationUnit sourceUnit,
@@ -1207,10 +1230,10 @@ public class SourceElementParser extends CommentRecorderParser {//extends
   /**
    * Sort the given ast nodes by their positions.
    */
-  private static void quickSort(AstNode[] sortedCollection, int left, int right) {
+  private static void quickSort(ASTNode[] sortedCollection, int left, int right) {
     int original_left = left;
     int original_right = right;
-    AstNode mid = sortedCollection[(left + right) / 2];
+    ASTNode mid = sortedCollection[(left + right) / 2];
     do {
       while (sortedCollection[left].sourceStart < mid.sourceStart) {
         left++;
@@ -1219,7 +1242,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
         right--;
       }
       if (left <= right) {
-        AstNode tmp = sortedCollection[left];
+        ASTNode tmp = sortedCollection[left];
         sortedCollection[left] = sortedCollection[right];
         sortedCollection[right] = tmp;
         left++;
@@ -1263,7 +1286,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
   }
 
   private void visitIfNeeded(AbstractMethodDeclaration method) {
-    if (this.localDeclarationVisitor != null && (method.bits & AstNode.HasLocalTypeMASK) != 0) {
+    if (this.localDeclarationVisitor != null && (method.bits & ASTNode.HasLocalTypeMASK) != 0) {
       if (method.statements != null) {
         int statementsLength = method.statements.length;
         for (int i = 0; i < statementsLength; i++)
@@ -1274,7 +1297,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
 
   //private void visitIfNeeded(FieldDeclaration field) {
   //   if (this.localDeclarationVisitor != null
-  //           && (field.bits & AstNode.HasLocalTypeMASK) != 0) {
+  //           && (field.bits & ASTNode.HasLocalTypeMASK) != 0) {
   //                   if (field.initialization != null) {
   //                           field.initialization.traverse(this.localDeclarationVisitor, null);
   //                   }
@@ -1283,7 +1306,7 @@ public class SourceElementParser extends CommentRecorderParser {//extends
   //
   //private void visitIfNeeded(Initializer initializer) {
   //   if (this.localDeclarationVisitor != null
-  //           && (initializer.bits & AstNode.HasLocalTypeMASK) != 0) {
+  //           && (initializer.bits & ASTNode.HasLocalTypeMASK) != 0) {
   //                   if (initializer.block != null) {
   //                           initializer.block.traverse(this.localDeclarationVisitor, null);
   //                   }