Added "Task Tags" functionality (TODO,...)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / IdentifierIndexManager.java
index 2e76596..532a301 100644 (file)
@@ -1,5 +1,4 @@
 package net.sourceforge.phpeclipse.builder;
-
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.FileNotFoundException;
@@ -16,52 +15,46 @@ import java.util.List;
 import java.util.SortedMap;
 import java.util.StringTokenizer;
 import java.util.TreeMap;
-
 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
 import net.sourceforge.phpdt.internal.compiler.util.Util;
 import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
-
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
-
 /**
  * Manages the identifer index information for a specific project
- *
+ *  
  */
 public class IdentifierIndexManager {
-
   public class LineCreator implements ITerminalSymbols {
-
     private Scanner fScanner;
     private int fToken;
-
     public LineCreator() {
-      fScanner = new Scanner(true, false);
+      fScanner = new Scanner(true, false, false, false, true, null, null);
     }
-
     /**
      * Add the information of the current identifier to the line
      * 
-     * @param typeOfIdentifier the type of the identifier ('c'lass, 'd'efine, 'f'unction, 'm'ethod, 'v'ariable)
-     * @param identifier current identifier
-     * @param line Buffer for the current index line
-     * @param phpdocOffset the offset of the PHPdoc comment if available
-     * @param phpdocLength the length of the PHPdoc comment if available
+     * @param typeOfIdentifier
+     *            the type of the identifier ('c'lass, 'd'efine, 'f'unction,
+     *            'm'ethod, 'v'ariable)
+     * @param identifier
+     *            current identifier
+     * @param line
+     *            Buffer for the current index line
+     * @param phpdocOffset
+     *            the offset of the PHPdoc comment if available
+     * @param phpdocLength
+     *            the length of the PHPdoc comment if available
      */
-    private void addIdentifierInformation(
-      char typeOfIdentifier,
-      char[] identifier,
-      StringBuffer line,
-      int phpdocOffset,
-      int phpdocLength) {
-
+    private void addIdentifierInformation(char typeOfIdentifier,
+        char[] identifier, StringBuffer line, int phpdocOffset, int phpdocLength) {
       line.append('\t');
       line.append(typeOfIdentifier);
       line.append(identifier);
-      line.append("\to"); // Offset 
+      line.append("\to"); // Offset
       line.append(fScanner.getCurrentTokenStartPosition());
       if (phpdocOffset >= 0) {
         line.append("\tp"); // phpdoc offset
@@ -69,7 +62,6 @@ public class IdentifierIndexManager {
         line.append("\tl"); // phpdoc length
         line.append(phpdocLength);
       }
-
     }
     /**
      * Get the next token from input
@@ -80,42 +72,45 @@ public class IdentifierIndexManager {
         if (Scanner.DEBUG) {
           int currentEndPosition = fScanner.getCurrentTokenEndPosition();
           int currentStartPosition = fScanner.getCurrentTokenStartPosition();
-
-          System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
+          System.out.print(currentStartPosition + "," + currentEndPosition
+              + ": ");
           System.out.println(fScanner.toStringAction(fToken));
         }
         return;
       } catch (InvalidInputException e) {
         // ignore errors
+        e.printStackTrace();
       }
       fToken = TokenNameERROR;
     }
-
-    private void parseDeclarations(char[] parent, StringBuffer buf, boolean goBack) {
+    private void parseDeclarations(char[] parent, StringBuffer buf,
+        boolean goBack) {
       char[] ident;
       char[] classVariable;
       int counter = 0;
       int phpdocOffset = -1;
       int phpdocLength = -1;
-
       try {
         while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
           phpdocOffset = -1;
           if (fToken == TokenNameCOMMENT_PHPDOC) {
             phpdocOffset = fScanner.getCurrentTokenStartPosition();
-            phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
+            phpdocLength = fScanner.getCurrentTokenEndPosition()
+                - fScanner.getCurrentTokenStartPosition() + 1;
             getNextToken();
             if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
               break;
             }
           }
-          if (fToken == TokenNamevar) {
+          if (fToken == TokenNamevar || fToken == TokenNamepublic
+              || fToken == TokenNameprotected || fToken == TokenNameprivate) {
             getNextToken();
             if (fToken == TokenNameVariable) {
               ident = fScanner.getCurrentIdentifierSource();
               classVariable = new char[ident.length - 1];
               System.arraycopy(ident, 1, classVariable, 0, ident.length - 1);
-              addIdentifierInformation('v', classVariable, buf, phpdocOffset, phpdocLength);
+              addIdentifierInformation('v', classVariable, buf, phpdocOffset,
+                  phpdocLength);
               getNextToken();
             }
           } else if (fToken == TokenNamefunction) {
@@ -127,14 +122,17 @@ public class IdentifierIndexManager {
               ident = fScanner.getCurrentIdentifierSource();
               if (parent != null && equalCharArrays(parent, ident)) {
                 // constructor function
-                addIdentifierInformation('k', ident, buf, phpdocOffset, phpdocLength);
+                addIdentifierInformation('k', ident, buf, phpdocOffset,
+                    phpdocLength);
               } else {
                 if (parent != null) {
                   // class method function
-                  addIdentifierInformation('m', ident, buf, phpdocOffset, phpdocLength);
+                  addIdentifierInformation('m', ident, buf, phpdocOffset,
+                      phpdocLength);
                 } else {
                   // nested function ?!
-                  addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
+                  addIdentifierInformation('f', ident, buf, phpdocOffset,
+                      phpdocLength);
                 }
               }
               getNextToken();
@@ -144,26 +142,38 @@ public class IdentifierIndexManager {
             getNextToken();
             if (fToken == TokenNameIdentifier) {
               ident = fScanner.getCurrentIdentifierSource();
-              addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
+              addIdentifierInformation('c', ident, buf, phpdocOffset,
+                  phpdocLength);
               getNextToken();
-
-              //skip tokens for classname, extends and others until we have the opening '{'
-              while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
+              //skip tokens for classname, extends and others until we have
+              // the opening '{'
+              while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
+                  && fToken != TokenNameERROR) {
                 getNextToken();
               }
               parseDeclarations(ident, buf, true);
             }
-          } else if (fToken == TokenNamedefine) {
+          } else if (fToken == TokenNameIdentifier) {
+            ident = fScanner.getCurrentIdentifierSource();
             getNextToken();
-            if (fToken == TokenNameLPAREN) {
-              getNextToken();
-              if (fToken == TokenNameStringLiteral) {
-                ident = fScanner.getCurrentStringLiteralSource();
-                addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
+            if (ident.length==6 && 
+                ident[0]=='d' && 
+                ident[1]=='e' && 
+                ident[2]=='f' && 
+                ident[3]=='i' && 
+                ident[4]=='n' && 
+                ident[5]=='e') {
+              if (fToken == TokenNameLPAREN) {
                 getNextToken();
+                if (fToken == TokenNameStringLiteral) {
+                  ident = fScanner.getCurrentStringLiteralSource();
+                  addIdentifierInformation('d', ident, buf, phpdocOffset,
+                      phpdocLength);
+                  getNextToken();
+                }
               }
             }
-          } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) {
+          } else if (fToken == TokenNameLBRACE) {
             getNextToken();
             counter++;
           } else if (fToken == TokenNameRBRACE) {
@@ -181,25 +191,23 @@ public class IdentifierIndexManager {
         e.printStackTrace();
       }
     }
-
     public void parseIdentifiers(char[] charArray, StringBuffer buf) {
       char[] ident;
       String identifier;
       int counter = 0;
       int phpdocOffset = -1;
       int phpdocLength = -1;
-
       fScanner.setSource(charArray);
       fScanner.setPHPMode(false);
       fToken = TokenNameEOF;
       getNextToken();
-
       try {
-        while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
+        while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
           phpdocOffset = -1;
           if (fToken == TokenNameCOMMENT_PHPDOC) {
             phpdocOffset = fScanner.getCurrentTokenStartPosition();
-            phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
+            phpdocLength = fScanner.getCurrentTokenEndPosition()
+                - fScanner.getCurrentTokenStartPosition() + 1;
             getNextToken();
             if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
               break;
@@ -212,7 +220,8 @@ public class IdentifierIndexManager {
             }
             if (fToken == TokenNameIdentifier) {
               ident = fScanner.getCurrentIdentifierSource();
-              addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
+              addIdentifierInformation('f', ident, buf, phpdocOffset,
+                  phpdocLength);
               getNextToken();
               parseDeclarations(null, buf, true);
             }
@@ -220,25 +229,35 @@ public class IdentifierIndexManager {
             getNextToken();
             if (fToken == TokenNameIdentifier) {
               ident = fScanner.getCurrentIdentifierSource();
-              addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
+              addIdentifierInformation('c', ident, buf, phpdocOffset,
+                  phpdocLength);
               getNextToken();
-
-              //skip fTokens for classname, extends and others until we have the opening '{'
-              while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
+              //skip fTokens for classname, extends and others until we have
+              // the opening '{'
+              while (fToken != TokenNameLBRACE && fToken != TokenNameEOF
+                  && fToken != TokenNameERROR) {
                 getNextToken();
               }
-
               parseDeclarations(ident, buf, true);
-
             }
-          } else if (fToken == TokenNamedefine) {
+          } else if (fToken == TokenNameIdentifier) {
+            ident = fScanner.getCurrentIdentifierSource();
             getNextToken();
-            if (fToken == TokenNameLPAREN) {
-              getNextToken();
-              if (fToken == TokenNameStringLiteral) {
-                ident = fScanner.getCurrentStringLiteralSource();
-                addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
+            if (ident.length==6 && 
+                ident[0]=='d' && 
+                ident[1]=='e' && 
+                ident[2]=='f' && 
+                ident[3]=='i' && 
+                ident[4]=='n' && 
+                ident[5]=='e') {
+              if (fToken == TokenNameLPAREN) {
                 getNextToken();
+                if (fToken == TokenNameStringLiteral) {
+                  ident = fScanner.getCurrentStringLiteralSource();
+                  addIdentifierInformation('d', ident, buf, phpdocOffset,
+                      phpdocLength);
+                  getNextToken();
+                }
               }
             }
           } else {
@@ -251,30 +270,26 @@ public class IdentifierIndexManager {
       }
     }
   }
-
   class StringComparator implements Comparator {
     public int compare(Object o1, Object o2) {
       String s1 = (String) o1;
       String s2 = (String) o2;
       return s1.compareTo(s2);
-      //       return s1.toUpperCase().compareTo(s2.toUpperCase()); 
+      //       return s1.toUpperCase().compareTo(s2.toUpperCase());
     }
     public boolean equals(Object o) {
       String s = (String) o;
       return compare(this, o) == 0;
     }
   }
-
   private HashMap fFileMap;
   private String fFilename;
   private TreeMap fIndentifierMap;
-
   public IdentifierIndexManager(String filename) {
     fFilename = filename;
     initialize();
     readFile();
   }
-
   /**
    * Check if 2 char arrays are equal
    * 
@@ -293,14 +308,16 @@ public class IdentifierIndexManager {
     }
     return true;
   }
-
+  public LineCreator createLineCreator() {
+    return new LineCreator();
+  }
   /**
    * Add the information for a given IFile resource
-   *
+   *  
    */
   public void addFile(IFile fileToParse) {
     //    InputStream iStream;
-    LineCreator lineCreator = new LineCreator();
+    LineCreator lineCreator = createLineCreator();
     try {
       //      iStream = fileToParse.getContents();
       //
@@ -313,36 +330,46 @@ public class IdentifierIndexManager {
       //      } catch (IOException e) {
       //        return;
       //      }
-      InputStream stream = null;
+      addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getFullPath().toString(), lineCreator);
+    } catch (CoreException e1) {
+      // TODO Auto-generated catch block
+      e1.printStackTrace();
+    }
+  }
+  /**
+   * @param fileToParse
+   * @param lineCreator
+   * @throws CoreException
+   */
+  public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
+//    InputStream stream;
+    try {
+     // stream = new BufferedInputStream(fileToParse.getContents());
+      StringBuffer lineBuffer = new StringBuffer();
+      lineBuffer.append(filePath);
+      int lineLength = lineBuffer.length();
+      // lineCreator.parseIdentifiers(buf.toString().toCharArray(),
+      // lineBuffer);
+      lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
+          null), lineBuffer);
+      if (lineLength != lineBuffer.length()) {
+        addLine(lineBuffer.toString());
+      }
+    } catch (IOException e) {
+      e.printStackTrace();
+    } finally {
       try {
-        stream = new BufferedInputStream(fileToParse.getContents());
-
-        StringBuffer lineBuffer = new StringBuffer();
-        lineBuffer.append(fileToParse.getFullPath().toString());
-        int lineLength = lineBuffer.length();
-        // lineCreator.parseIdentifiers(buf.toString().toCharArray(), lineBuffer);
-        lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1, null), lineBuffer);
-        if (lineLength != lineBuffer.length()) {
-          addLine(lineBuffer.toString());
+        if (stream != null) {
+          stream.close();
         }
       } catch (IOException e) {
-        return;
-      } finally {
-        try {
-          if (stream != null) {
-            stream.close();
-          }
-        } catch (IOException e) {
-        }
       }
-    } catch (CoreException e1) {
-      // TODO Auto-generated catch block
-      e1.printStackTrace();
     }
   }
-
   /**
-   * Adds a line of the index file for function, class, class-method and class-variable names
+   * Adds a line of the index file for function, class, class-method and
+   * class-variable names
    * 
    * @param line
    */
@@ -355,7 +382,6 @@ public class IdentifierIndexManager {
     String offset = null;
     PHPIdentifierLocation phpIdentifier = null;
     boolean tokenExists = false;
-
     tokenizer = new StringTokenizer(line, "\t");
     // first token contains the filename:
     if (tokenizer.hasMoreTokens()) {
@@ -369,46 +395,61 @@ public class IdentifierIndexManager {
       token = tokenizer.nextToken();
       //System.out.println(token);
       switch (token.charAt(0)) {
-        case 'c' : // class name
+        case 'c' :
+          // class name
           identifier = token.substring(1);
           classname = identifier;
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.CLASS, phpFileName);
           break;
-        case 'd' : // define
+        case 'd' :
+          // define
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.DEFINE, phpFileName);
           break;
-        case 'f' : // function name
+        case 'f' :
+          // function name
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.FUNCTION, phpFileName);
           break;
-        case 'k' : // constructor function name
+        case 'k' :
+          // constructor function name
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.CONSTRUCTOR, phpFileName);
           break;
-        case 'm' : //method inside a class
+        case 'm' :
+          //method inside a class
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.METHOD, phpFileName, classname);
           break;
-        case 'v' : // variable inside a class
+        case 'v' :
+          // variable inside a class
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.VARIABLE, phpFileName, classname);
           break;
-        case 'o' : // offset information
+        case 'o' :
+          // offset information
           identifier = null;
           if (phpIdentifier != null) {
             offset = token.substring(1);
             phpIdentifier.setOffset(Integer.parseInt(offset));
           }
           break;
-        case 'p' : // PHPdoc offset information
+        case 'p' :
+          // PHPdoc offset information
           identifier = null;
           if (phpIdentifier != null) {
             offset = token.substring(1);
             phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
           }
           break;
-        case 'l' : // PHPdoc length information
+        case 'l' :
+          // PHPdoc length information
           identifier = null;
           if (phpIdentifier != null) {
             offset = token.substring(1);
@@ -445,18 +486,17 @@ public class IdentifierIndexManager {
       fFileMap.put(phpFileName, line);
     }
   }
-
   /**
    * Change the information for a given IFile resource
-   *
+   *  
    */
   public void changeFile(IFile fileToParse) {
     removeFile(fileToParse);
     addFile(fileToParse);
   }
-
   /**
-   * Get a list of all PHPIdentifierLocation object's associated with an identifier
+   * Get a list of all PHPIdentifierLocation object's associated with an
+   * identifier
    * 
    * @param identifier
    * @return
@@ -464,24 +504,19 @@ public class IdentifierIndexManager {
   public List getLocations(String identifier) {
     return (List) fIndentifierMap.get(identifier);
   }
-
   /**
    * Initialize (i.e. clear) the current index information
-   *
+   *  
    */
   public void initialize() {
     fIndentifierMap = new TreeMap(new StringComparator());
     fFileMap = new HashMap();
   }
-
   private void readFile() {
-
     FileReader fileReader;
     try {
       fileReader = new FileReader(fFilename);
-
       BufferedReader bufferedReader = new BufferedReader(fileReader);
-
       String line;
       while (bufferedReader.ready()) {
         // all entries for one file are in a line
@@ -489,7 +524,6 @@ public class IdentifierIndexManager {
         line = bufferedReader.readLine();
         addLine(line);
       }
-
       fileReader.close();
     } catch (FileNotFoundException e) {
       // ignore this
@@ -498,23 +532,22 @@ public class IdentifierIndexManager {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
-
   }
-
   /**
    * Remove the information for a given IFile resource
-   *
+   *  
    */
   public void removeFile(IFile fileToParse) {
-    //    String line = (String) fFileMap.get(fileToParse.getLocation().toString());
+    //    String line = (String)
+    // fFileMap.get(fileToParse.getLocation().toString());
     String line = (String) fFileMap.get(fileToParse.getFullPath().toString());
     if (line != null) {
       removeLine(line);
     }
   }
-
   /**
-   * Removes a line of the index file for function, class, class-method and class-variable names
+   * Removes a line of the index file for function, class, class-method and
+   * class-variable names
    * 
    * @param line
    */
@@ -526,7 +559,6 @@ public class IdentifierIndexManager {
     String classname = null;
     PHPIdentifier phpIdentifier = null;
     boolean tokenExists = false;
-
     tokenizer = new StringTokenizer(line, "\t");
     // first token contains the filename:
     if (tokenizer.hasMoreTokens()) {
@@ -540,30 +572,42 @@ public class IdentifierIndexManager {
       token = tokenizer.nextToken();
       //System.out.println(token);
       switch (token.charAt(0)) {
-        case 'c' : // class name
+        case 'c' :
+          // class name
           identifier = token.substring(1);
           classname = identifier;
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.CLASS, phpFileName);
           break;
-        case 'd' : // define
+        case 'd' :
+          // define
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.DEFINE, phpFileName);
           break;
-        case 'f' : // function name
+        case 'f' :
+          // function name
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.FUNCTION, phpFileName);
           break;
-        case 'k' : // constructor function name
+        case 'k' :
+          // constructor function name
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.CONSTRUCTOR, phpFileName);
           break;
-        case 'm' : //method inside a class
+        case 'm' :
+          //method inside a class
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.METHOD, phpFileName, classname);
           break;
-        case 'v' : // variable inside a class
+        case 'v' :
+          // variable inside a class
           identifier = token.substring(1);
-          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.VARIABLE, phpFileName, classname);
           break;
         default :
           identifier = null;
@@ -588,10 +632,9 @@ public class IdentifierIndexManager {
     }
     fFileMap.remove(phpFileName);
   }
-
   /**
    * Save the current index information in the projects index file
-   *
+   *  
    */
   public void writeFile() {
     FileWriter fileWriter;
@@ -612,7 +655,6 @@ public class IdentifierIndexManager {
       e.printStackTrace();
     }
   }
-
   /**
    * @param fromKey
    * @param toKey
@@ -621,5 +663,4 @@ public class IdentifierIndexManager {
   public SortedMap getIdentifierMap() {
     return fIndentifierMap;
   }
-
 }
\ No newline at end of file