improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / UnitParser.java
index f8ec712..9785834 100644 (file)
@@ -1,12 +1,13 @@
 package net.sourceforge.phpdt.internal.compiler.parser;
 
+
 import net.sourceforge.phpdt.core.IJavaModelMarker;
 import net.sourceforge.phpdt.core.compiler.IProblem;
 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
-import net.sourceforge.phpdt.internal.compiler.util.Util;
+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.FieldDeclaration;
@@ -19,16 +20,15 @@ import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 
 /**
- * @author jsurfer
- *
+ * 
  * 
  */
 public class UnitParser extends Parser {
 
   public UnitParser(ProblemReporter problemReporter) { //, boolean optimizeStringLiterals, boolean assertMode) {
-    super();
+    super(problemReporter);
     nestedMethod = new int[30];
-    this.problemReporter = problemReporter;
+    
     //         this.optimizeStringLiterals = optimizeStringLiterals;
     //         this.assertMode = assertMode;
     //         this.initializeScanner();
@@ -98,7 +98,7 @@ public class UnitParser extends Parser {
 
     //         int astLength = astStack.length;
     //         if (noAstNodes.length < astLength){
-    //                 noAstNodes = new AstNode[astLength];
+    //                 noAstNodes = new ASTNode[astLength];
     //                 //System.out.println("Resized AST stacks : "+ astLength);
     //         
     //         }
@@ -141,7 +141,7 @@ public class UnitParser extends Parser {
       goForCompilationUnit();
 
       /* scanner initialization */
-      scanner.setSource(sourceUnit.getContents());
+      scanner.setSource(sourceUnit, sourceUnit.getContents());
 
       /* unit creation */
       referenceContext =
@@ -202,46 +202,12 @@ public class UnitParser extends Parser {
             new Integer(problem.getSourceStart()),
             new Integer(problem.getSourceEnd() + 1),
             new Integer(problem.getSourceLineNumber()),
-            net.sourceforge.phpdt.internal.core.Util.getProblemArgumentsForMarker(problem.getArguments())});
+            net.sourceforge.phpdt.internal.core.util.Util.getProblemArgumentsForMarker(problem.getArguments())});
       }
 
     }
   }
-  protected CompilationUnitDeclaration endParse(int act) {
 
-    this.lastAct = act;
-
-    if (currentElement != null) {
-      currentElement.topElement().updateParseTree();
-      if (VERBOSE_RECOVERY) {
-        System.out.print(Util.bind("parser.syntaxRecovery")); //$NON-NLS-1$
-        System.out.println("--------------------------"); //$NON-NLS-1$
-        System.out.println(compilationUnit);
-        System.out.println("----------------------------------"); //$NON-NLS-1$
-      }
-    } else {
-      if (diet & VERBOSE_RECOVERY) {
-        System.out.print(Util.bind("parser.regularParse")); //$NON-NLS-1$
-        System.out.println("--------------------------"); //$NON-NLS-1$
-        System.out.println(compilationUnit);
-        System.out.println("----------------------------------"); //$NON-NLS-1$
-      }
-    }
-    if (scanner.recordLineSeparator) {
-      compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
-    }
-    if (scanner.taskTags != null) {
-      for (int i = 0; i < scanner.foundTaskCount; i++) {
-        problemReporter().task(
-          new String(scanner.foundTaskTags[i]),
-          new String(scanner.foundTaskMessages[i]),
-          scanner.foundTaskPriorities[i] == null ? null : new String(scanner.foundTaskPriorities[i]),
-          scanner.foundTaskPositions[i][0],
-          scanner.foundTaskPositions[i][1]);
-      }
-    }
-    return compilationUnit;
-  }
 
   // A P I
 
@@ -257,7 +223,7 @@ public class UnitParser extends Parser {
 
     referenceContext = cd;
     compilationUnit = unit;
-
+    
     scanner.resetTo(cd.sourceEnd + 1, cd.declarationSourceEnd);
     try {
       parse();
@@ -319,7 +285,7 @@ public class UnitParser extends Parser {
 
     referenceContext = type;
     compilationUnit = unit;
-
+    
     scanner.setSource(initializationSource);
     scanner.resetTo(0, initializationSource.length - 1);
     try {
@@ -337,8 +303,8 @@ public class UnitParser extends Parser {
     // field.initialization = expressionStack[expressionPtr];
     // 
     // // mark field with local type if one was found during parsing
-    // if ((type.bits & AstNode.HasLocalTypeMASK) != 0) {
-    //         field.bits |= AstNode.HasLocalTypeMASK;
+    // if ((type.bits & ASTNode.HasLocalTypeMASK) != 0) {
+    //         field.bits |= ASTNode.HasLocalTypeMASK;
     // }       
   }
   // A P I
@@ -355,7 +321,7 @@ public class UnitParser extends Parser {
 
     referenceContext = type;
     compilationUnit = unit;
-
+    
     scanner.resetTo(ini.sourceStart, ini.sourceEnd); // just on the beginning {
     try {
       parse();
@@ -372,56 +338,60 @@ public class UnitParser extends Parser {
     // ini.block = ((Initializer) astStack[astPtr]).block;
     // 
     // // mark initializer with local type if one was found during parsing
-    // if ((type.bits & AstNode.HasLocalTypeMASK) != 0) {
-    //         ini.bits |= AstNode.HasLocalTypeMASK;
+    // if ((type.bits & ASTNode.HasLocalTypeMASK) != 0) {
+    //         ini.bits |= ASTNode.HasLocalTypeMASK;
     // }       
   }
   // A P I
 
   public void parse(MethodDeclaration md, CompilationUnitDeclaration unit) {
-    //only parse the method body of md
-    //fill out method statements
-
-    //convert bugs into parse error
-
-    if (md.isAbstract())
-      return;
-    // if (md.isNative())
-    //         return;
-    // if ((md.modifiers & AccSemicolonBody) != 0)
-    //         return;
-
-    initialize(false);
-    goForMethodBody();
-    nestedMethod[nestedType]++;
-
-    referenceContext = md;
-    compilationUnit = unit;
-
-    scanner.resetTo(md.sourceEnd + 1, md.declarationSourceEnd);
-    // reset the scanner to parser from { down to }
-    try {
-      parse();
-    } catch (AbortCompilation ex) {
-      lastAct = ERROR_ACTION;
-    } finally {
-      nestedMethod[nestedType]--;
-    }
-
-    // if (lastAct == ERROR_ACTION) {
-    //         return;
-    // }
-    //
-    // //refill statements
-    // md.explicitDeclarations = realBlockStack[realBlockPtr--];
-    // int length;
-    // if ((length = astLengthStack[astLengthPtr--]) != 0)
-    //         System.arraycopy(
-    //                 astStack, 
-    //                 (astPtr -= length) + 1, 
-    //                 md.statements = new Statement[length], 
-    //                 0, 
-    //                 length); 
+//  TODO jsurfer - make the parse process work on methods ?
+    return; 
+    
+//    //only parse the method body of md
+//    //fill out method statements
+//
+//    //convert bugs into parse error
+//
+//    if (md.isAbstract())
+//      return;
+//    //       if (md.isNative())
+//    //               return;
+//    //       if ((md.modifiers & AccSemicolonBody) != 0)
+//    //               return;
+//
+//    initialize(false);
+//    goForMethodBody();
+//    nestedMethod[nestedType]++;
+//
+//    referenceContext = md;
+//    compilationUnit = unit;
+//
+//    scanner.resetTo(md.sourceEnd + 1, md.declarationSourceEnd);
+//   
+//    // reset the scanner to parser from { down to }
+//    try {
+//      parse();
+//    } catch (AbortCompilation ex) {
+//      lastAct = ERROR_ACTION;
+//    } finally {
+//      nestedMethod[nestedType]--;
+//    }
+//
+//    //       if (lastAct == ERROR_ACTION) {
+//    //               return;
+//    //       }
+//    //
+//    //       //refill statements
+//    //       md.explicitDeclarations = realBlockStack[realBlockPtr--];
+//    //       int length;
+//    //       if ((length = astLengthStack[astLengthPtr--]) != 0)
+//    //               System.arraycopy(
+//    //                       astStack, 
+//    //                       (astPtr -= length) + 1, 
+//    //                       md.statements = new Statement[length], 
+//    //                       0, 
+//    //                       length); 
   }
 
   // A P I
@@ -436,11 +406,12 @@ public class UnitParser extends Parser {
       goForCompilationUnit();
 
       /* scanner initialization */
-      scanner.setSource(sourceUnit.getContents());
+      scanner.setSource(sourceUnit, sourceUnit.getContents());
       scanner.resetTo(start, end);
       /* unit creation */
       referenceContext =
         compilationUnit = new CompilationUnitDeclaration(problemReporter, compilationResult, scanner.source.length);
+      
       /* run automaton */
       parse();
     } catch (SyntaxError syntaxError) {
@@ -467,4 +438,48 @@ public class UnitParser extends Parser {
     }
     return parsedUnit;
   }
+  
+  public void getMethodBodies(CompilationUnitDeclaration unit) {
+       //fill the methods bodies in order for the code to be generated
+
+       if (unit == null) return;
+       
+       if (unit.ignoreMethodBodies) {
+               unit.ignoreFurtherInvestigation = true;
+               return;
+               // if initial diet parse did not work, no need to dig into method bodies.
+       }
+
+       if ((unit.bits & ASTNode.HasAllMethodBodies) != 0)
+               return; //work already done ...
+
+       //real parse of the method....
+       char[] contents = unit.compilationResult.compilationUnit.getContents();
+       this.scanner.setSource(contents);
+       
+       // save existing values to restore them at the end of the parsing process
+       // see bug 47079 for more details
+       int[] oldLineEnds = this.scanner.lineEnds;
+       int oldLinePtr = this.scanner.linePtr;
+
+       final int[] lineSeparatorPositions = unit.compilationResult.lineSeparatorPositions;
+       this.scanner.lineEnds = lineSeparatorPositions;
+       this.scanner.linePtr = lineSeparatorPositions.length - 1;
+
+//     if (this.javadocParser != null && this.javadocParser.checkDocComment) {
+//             this.javadocParser.scanner.setSource(contents);
+//     }
+       if (unit.types != null) {
+               for (int i = unit.types.size(); --i >= 0;)
+                       ((TypeDeclaration)unit.types.get(i)).parseMethod(this, unit);
+       }
+       
+       // tag unit has having read bodies
+       unit.bits |= ASTNode.HasAllMethodBodies;
+
+       // this is done to prevent any side effects on the compilation unit result
+       // line separator positions array.
+       this.scanner.lineEnds = oldLineEnds;
+       this.scanner.linePtr = oldLinePtr;
+}
 }