1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / IdentifierIndexManager.java
index 499bd82..78a6e2d 100644 (file)
@@ -24,21 +24,21 @@ 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.PHPeclipsePlugin;
-import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
+//import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
 
 /**
- * Manages the identifer index information for a specific project
+ * Manages the identifier index information for a specific project
  * 
  */
 public class IdentifierIndexManager {
        public class LineCreator implements ITerminalSymbols {
                private Scanner fScanner;
 
-               private int fToken;
+               private TokenName fToken;
 
                public LineCreator() {
                        fScanner = new Scanner(true, false, false, false, true, null, null,
@@ -61,8 +61,6 @@ public class IdentifierIndexManager {
                        line.append('\t');
                        line.append(typeOfIdentifier);
                        line.append(identifier);
-                       // line.append("\to"); // Offset
-                       // line.append(fScanner.getCurrentTokenStartPosition());
                }
 
                /**
@@ -128,13 +126,21 @@ public class IdentifierIndexManager {
                                System.out.println(fScanner.toStringAction(fToken));
                        }
                        return;
-                       // } catch (InvalidInputException e) {
-                       // // ignore errors
-                       // // e.printStackTrace();
-                       // }
-                       // fToken = TokenNameERROR;
                }
 
+               private void skipComments()
+               {
+                       try {
+                               getNextToken();
+                               while (fToken == TokenName.COMMENT_BLOCK  || fToken == TokenName.COMMENT_PHPDOC) {
+                                               getNextToken();
+                               }
+                       } catch (InvalidInputException e1) {
+                               // TODO Auto-generated catch block
+                               e1.printStackTrace();
+                       }
+               }
+               
                private void parseDeclarations(char[] parent, StringBuffer buf,
                                boolean goBack) {
                        char[] ident;
@@ -144,33 +150,33 @@ public class IdentifierIndexManager {
                        int phpdocOffset = -1;
                        int phpdocLength = -1;
                        try {
-                               while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
+                               while (fToken != TokenName.EOF && fToken != TokenName.ERROR) {
                                        phpdocOffset = -1;
                                        hasModifiers = false;
-                                       if (fToken == TokenNameCOMMENT_PHPDOC) {
+                                       if (fToken == TokenName.COMMENT_PHPDOC) {
                                                phpdocOffset = fScanner.getCurrentTokenStartPosition();
                                                phpdocLength = fScanner.getCurrentTokenEndPosition()
                                                                - fScanner.getCurrentTokenStartPosition() + 1;
                                                getNextToken();
-                                               while (fToken == TokenNamestatic
-                                                               || fToken == TokenNamefinal
-                                                               || fToken == TokenNamepublic
-                                                               || fToken == TokenNameprotected
-                                                               || fToken == TokenNameprivate
-                                                               || fToken == TokenNameabstract) {
+                                               while (fToken == TokenName.STATIC
+                                                               || fToken == TokenName.FINAL
+                                                               || fToken == TokenName.PUBLIC
+                                                               || fToken == TokenName.PROTECTED
+                                                               || fToken == TokenName.PRIVATE
+                                                               || fToken == TokenName.ABSTRACT) {
                                                        hasModifiers = true;
                                                        getNextToken();
                                                }
-                                               if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
+                                               if (fToken == TokenName.EOF || fToken == TokenName.ERROR) {
                                                        break;
                                                }
                                        }
-                                       if (fToken == TokenNamefunction) {
-                                               getNextToken();
-                                               if (fToken == TokenNameAND) {
+                                       if (fToken == TokenName.FUNCTION) {
+                                               skipComments();
+                                               if (fToken == TokenName.OP_AND) {
                                                        getNextToken();
                                                }
-                                               if (fToken == TokenNameIdentifier) {
+                                               if (fToken == TokenName.IDENTIFIER) {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        if (parent != null
                                                                        && equalCharArrays(parent, ident)) {
@@ -188,95 +194,96 @@ public class IdentifierIndexManager {
                                                                                        phpdocOffset, phpdocLength);
                                                                }
                                                        }
-                                                       getNextToken();
+                                                       skipComments();
                                                        parseDeclarations(null, buf, true);
                                                }
-                                       } else if (fToken == TokenNameclass
-                                                       || fToken == TokenNameinterface) {
-                                               getNextToken();
-                                               if (fToken == TokenNameIdentifier) {
+                                       } else if (fToken == TokenName.CLASS
+                                                       || fToken == TokenName.INTERFACE) {
+                                               skipComments();
+                                               if (fToken == TokenName.IDENTIFIER) {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        addIdentifierInformation('c', ident, buf,
                                                                        phpdocOffset, phpdocLength);
-                                                       getNextToken();
-                                                       if (fToken == TokenNameextends) {
-                                                               getNextToken();
-                                                               while (fToken == TokenNameIdentifier) {
+                                                       skipComments();
+                                                       if (fToken == TokenName.EXTENDS) {
+                                                               skipComments();
+                                                               while (fToken == TokenName.IDENTIFIER) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // extends ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
-                                                                       if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                       skipComments();
+                                                               if (fToken == TokenName.COMMA) {
+                                                                       skipComments();
                                                                        }
                                                                }
                                                        }
-                                                       if (fToken == TokenNameimplements) {
-                                                               getNextToken();
-                                                               while (fToken == TokenNameIdentifier) {
+                                                       if (fToken == TokenName.IMPLEMENTS) {
+                                                               skipComments();
+                                                               while (fToken == TokenName.IDENTIFIER) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // implements ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
-                                                                       if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                       skipComments();
+                                                                       if (fToken == TokenName.COMMA) {
+                                                                               skipComments();
+//                                                                             getNextToken();
                                                                        }
                                                                }
                                                        }
                                                        // skip tokens for classname, extends and others
                                                        // until we have
                                                        // the opening '{'
-                                                       while (fToken != TokenNameLBRACE
-                                                                       && fToken != TokenNameEOF
-                                                                       && fToken != TokenNameERROR) {
+                                                       while (fToken != TokenName.LBRACE
+                                                                       && fToken != TokenName.EOF
+                                                                       && fToken != TokenName.ERROR) {
                                                                getNextToken();
                                                        }
                                                        parseDeclarations(ident, buf, true);
                                                }
-                                       } else if (fToken == TokenNamevar || hasModifiers
-                                                       || fToken == TokenNamestatic
-                                                       || fToken == TokenNamefinal
-                                                       || fToken == TokenNamepublic
-                                                       || fToken == TokenNameprotected
-                                                       || fToken == TokenNameprivate) {
-                                               while (fToken == TokenNamevar
-                                                               || fToken == TokenNamestatic
-                                                               || fToken == TokenNamefinal
-                                                               || fToken == TokenNamepublic
-                                                               || fToken == TokenNameprotected
-                                                               || fToken == TokenNameprivate) {
-                                                       getNextToken();
+                                       } else if (fToken == TokenName.VAR || hasModifiers
+                                                       || fToken == TokenName.STATIC
+                                                       || fToken == TokenName.FINAL
+                                                       || fToken == TokenName.PUBLIC
+                                                       || fToken == TokenName.PROTECTED
+                                                       || fToken == TokenName.PRIVATE) {
+                                               while (fToken == TokenName.VAR
+                                                               || fToken == TokenName.STATIC
+                                                               || fToken == TokenName.FINAL
+                                                               || fToken == TokenName.PUBLIC
+                                                               || fToken == TokenName.PROTECTED
+                                                               || fToken == TokenName.PRIVATE) {
+                                                       skipComments();
                                                }
-                                               while (fToken == TokenNameVariable) {
+                                               while (fToken == TokenName.VARIABLE) {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        classVariable = new char[ident.length - 1];
                                                        System.arraycopy(ident, 1, classVariable, 0,
                                                                        ident.length - 1);
                                                        addClassVariableInformation('v', classVariable,
                                                                        buf, phpdocOffset, phpdocLength);
-                                                       getNextToken();
-                                                       if (fToken == TokenNameCOMMA) {
-                                                               getNextToken();
+                                                       skipComments();
+                                                       if (fToken == TokenName.COMMA) {
+                                                               skipComments();
                                                        }
                                                }
-                                       } else if (!hasModifiers && fToken == TokenNameIdentifier) {
+                                       } else if (!hasModifiers && fToken == TokenName.IDENTIFIER) {
                                                ident = fScanner.getCurrentIdentifierSource();
                                                getNextToken();
                                                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) {
+                                                       if (fToken == TokenName.LPAREN) {
                                                                getNextToken();
-                                                               if (fToken == TokenNameStringDoubleQuote) {
+                                                               if (fToken == TokenName.STRINGDOUBLEQUOTE) {
                                                                        ident = fScanner
                                                                                        .getCurrentStringLiteralSource();
                                                                        addIdentifierInformation('d', ident, buf,
                                                                                        phpdocOffset, phpdocLength);
                                                                        getNextToken();
-                                                               } else if (fToken == TokenNameStringSingleQuote) {
+                                                               } else if (fToken == TokenName.STRINGSINGLEQUOTE) {
                                                                        ident = fScanner
                                                                                        .getCurrentStringLiteralSource();
                                                                        addIdentifierInformation('d', ident, buf,
@@ -285,24 +292,24 @@ public class IdentifierIndexManager {
                                                                }
                                                        }
                                                }
-                                       } else if (fToken == TokenNameglobal) {
+                                       } else if (fToken == TokenName.GLOBAL) {
                                                // global variable
-                                               while (fToken != TokenNameEOF
-                                                               && fToken != TokenNameERROR
-                                                               && fToken != TokenNameSEMICOLON
-                                                               && fToken != TokenNameLBRACE
-                                                               && fToken != TokenNameRBRACE) {
+                                               while (fToken != TokenName.EOF
+                                                               && fToken != TokenName.ERROR
+                                                               && fToken != TokenName.SEMICOLON
+                                                               && fToken != TokenName.LBRACE
+                                                               && fToken != TokenName.RBRACE) {
                                                        getNextToken();
-                                                       if (fToken == TokenNameVariable) {
+                                                       if (fToken == TokenName.VARIABLE) {
                                                                ident = fScanner.getCurrentIdentifierSource();
                                                                addIdentifierInformation('g', ident, buf,
                                                                                phpdocOffset, phpdocLength);
                                                        }
                                                }
-                                       } else if (fToken == TokenNameLBRACE) {
+                                       } else if (fToken == TokenName.LBRACE) {
                                                getNextToken();
                                                counter++;
-                                       } else if (fToken == TokenNameRBRACE) {
+                                       } else if (fToken == TokenName.RBRACE) {
                                                getNextToken();
                                                --counter;
                                                if (counter == 0 && goBack) {
@@ -324,126 +331,125 @@ public class IdentifierIndexManager {
                                StringBuffer buf) {
                        char[] ident;
                        String identifier;
-                       int counter = 0;
                        boolean hasModifiers = false;
                        int phpdocOffset = -1;
                        int phpdocLength = -1;
                        fScanner.setSource(charArray);
                        fScanner.setPHPMode(false);
-                       fToken = TokenNameEOF;
+                       fToken = TokenName.EOF;
                        try {
                                getNextToken();
-                               while (fToken != TokenNameEOF) { // && fToken !=
-                                                                                                       // TokenNameERROR) {
+                               while (fToken != TokenName.EOF) { // && fToken !=
+                                       // TokenName.ERROR) {
                                        phpdocOffset = -1;
                                        hasModifiers = false;
-                                       if (fToken == TokenNameCOMMENT_PHPDOC) {
+                                       switch (fToken) {
+                                       case COMMENT_PHPDOC:
                                                phpdocOffset = fScanner.getCurrentTokenStartPosition();
                                                phpdocLength = fScanner.getCurrentTokenEndPosition()
                                                                - fScanner.getCurrentTokenStartPosition() + 1;
                                                getNextToken();
-                                               while (fToken == TokenNamestatic
-                                                               || fToken == TokenNamefinal
-                                                               || fToken == TokenNamepublic
-                                                               || fToken == TokenNameprotected
-                                                               || fToken == TokenNameprivate
-                                                               || fToken == TokenNameabstract) {
+                                               while (fToken == TokenName.STATIC
+                                                               || fToken == TokenName.FINAL
+                                                               || fToken == TokenName.PUBLIC
+                                                               || fToken == TokenName.PROTECTED
+                                                               || fToken == TokenName.PRIVATE
+                                                               || fToken == TokenName.ABSTRACT) {
                                                        hasModifiers = true;
                                                        getNextToken();
                                                }
-                                               if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
+                                               if (fToken == TokenName.EOF || fToken == TokenName.ERROR) {
                                                        break;
                                                }
-                                       }
-                                       if (fToken == TokenNamefunction) {
-                                               getNextToken();
-                                               if (fToken == TokenNameAND) {
+                                               break;
+                                               
+                                       case FUNCTION:
+                                               skipComments();
+                                               if (fToken == TokenName.OP_AND) {
                                                        getNextToken();
                                                }
-                                               if (fToken == TokenNameIdentifier) {
+                                               if (fToken == TokenName.IDENTIFIER) {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        addIdentifierInformation('f', ident, buf,
                                                                        phpdocOffset, phpdocLength);
-                                                       getNextToken();
+                                                       skipComments();
+                                                       if (fToken == TokenName.LPAREN) {
+                                                               skipComments();
+                                                               do {
+                                                                       if (fToken == TokenName.VARIABLE) {
+                                                                               ident = fScanner.getCurrentIdentifierSource();
+                                                                               addIdentifierInformation('v', ident, buf,
+                                                                                               phpdocOffset, phpdocLength);
+                                                                               skipComments();
+                                                                               if (fToken == TokenName.COMMA) {
+                                                                                       skipComments();
+                                                                               }
+                                                                       }       
+                                                                       getNextToken ();
+                                                               } while ((fToken != TokenName.RPAREN) && (fToken.compareTo (TokenName.EOF) > 0));
+                                                       }
                                                        parseDeclarations(null, buf, true);
                                                }
-                                       } else if (fToken == TokenNameclass
-                                                       || fToken == TokenNameinterface) {
-                                               getNextToken();
-                                               if (fToken == TokenNameIdentifier) {
+                                               break;
+                                               
+                                       case CLASS:
+                                       case INTERFACE:
+                                               skipComments();
+                                               if (fToken == TokenName.IDENTIFIER) {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        addIdentifierInformation('c', ident, buf,
                                                                        phpdocOffset, phpdocLength);
-                                                       getNextToken();
-                                                       if (fToken == TokenNameextends) {
-                                                               getNextToken();
-                                                               while (fToken == TokenNameIdentifier) {
+                                                       skipComments();
+                                                       if (fToken == TokenName.EXTENDS) {
+                                                               skipComments();
+                                                               while (fToken == TokenName.IDENTIFIER) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // extends ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
-                                                                       if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                       skipComments();
+                                                                       if (fToken == TokenName.COMMA) {
+                                                                               skipComments();
                                                                        }
                                                                }
                                                        }
-                                                       if (fToken == TokenNameimplements) {
-                                                               getNextToken();
-                                                               while (fToken == TokenNameIdentifier) {
+                                                       if (fToken == TokenName.IMPLEMENTS) {
+                                                               skipComments();
+                                                               while (fToken == TokenName.IDENTIFIER) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // implements ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
-                                                                       if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                       skipComments();
+                                                                       if (fToken == TokenName.COMMA) {
+                                                                               skipComments();
                                                                        }
                                                                }
                                                        }
                                                        // skip fTokens for classname, extends and others
                                                        // until we have
                                                        // the opening '{'
-                                                       while (fToken != TokenNameLBRACE
-                                                                       && fToken != TokenNameEOF
-                                                                       && fToken != TokenNameERROR) {
+                                                       while (fToken != TokenName.LBRACE
+                                                                       && fToken != TokenName.EOF
+                                                                       && fToken != TokenName.ERROR) {
                                                                getNextToken();
                                                        }
                                                        parseDeclarations(ident, buf, true);
                                                }
-                                       } else if (fToken == TokenNameVariable) {
+                                               break;
+
+                                       case VARIABLE:
                                                // global variable
                                                ident = fScanner.getCurrentIdentifierSource();
                                                addIdentifierInformation('g', ident, buf, phpdocOffset,
                                                                phpdocLength);
                                                getNextToken();
-                                       } else if (!hasModifiers && fToken == TokenNameIdentifier) {
-                                               ident = fScanner.getCurrentIdentifierSource();
-                                               getNextToken();
-                                               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 == TokenNameStringDoubleQuote) {
-                                                                       ident = fScanner
-                                                                                       .getCurrentStringLiteralSource();
-                                                                       addIdentifierInformation('d', ident, buf,
-                                                                                       phpdocOffset, phpdocLength);
-                                                                       getNextToken();
-                                                               } else if (fToken == TokenNameStringSingleQuote) {
-                                                                       ident = fScanner
-                                                                                       .getCurrentStringLiteralSource();
-                                                                       addIdentifierInformation('d', ident, buf,
-                                                                                       phpdocOffset, phpdocLength);
-                                                                       getNextToken();
-                                                               }
-                                                       }
-                                               }
-                                       } else {
+                                               break;
+
+                                       default:
                                                getNextToken();
                                        }
+                                       
                                }
                        } catch (InvalidInputException e) {
                                // ignore errors
@@ -453,7 +459,7 @@ public class IdentifierIndexManager {
                        }
                }
        }
-
+               
        class StringComparator implements Comparator {
                public int compare(Object o1, Object o2) {
                        String s1 = (String) o1;
@@ -463,7 +469,7 @@ public class IdentifierIndexManager {
                }
 
                public boolean equals(Object o) {
-                       String s = (String) o;
+                       //String s = (String) o;
                        return compare(this, o) == 0;
                }
        }
@@ -508,12 +514,11 @@ public class IdentifierIndexManager {
         * 
         */
        public void addFile(IFile fileToParse) {
-               // InputStream iStream;
                LineCreator lineCreator = createLineCreator();
                try {
                        addInputStream(new BufferedInputStream(fileToParse.getContents()),
                                        fileToParse.getProjectRelativePath().toString(),
-                                       lineCreator);
+                                       lineCreator, fileToParse.getCharset());
                } catch (CoreException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
@@ -526,17 +531,13 @@ public class IdentifierIndexManager {
         * @throws CoreException
         */
        public void addInputStream(InputStream stream, String filePath,
-                       LineCreator lineCreator) throws CoreException {
+                       LineCreator lineCreator, String charset) throws CoreException {
                try {
                        StringBuffer lineBuffer = new StringBuffer();
                        lineBuffer.append(filePath);
-                       int lineLength = lineBuffer.length();
                        lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream,
-                                       -1, null), lineBuffer);
-                       // if (lineLength != lineBuffer.length()) {
-                       // always add the file for Open Include Action
+                                       -1, charset), lineBuffer);
                        addLine(lineBuffer.toString());
-                       // }
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
@@ -545,6 +546,7 @@ public class IdentifierIndexManager {
                                        stream.close();
                                }
                        } catch (IOException e) {
+                               // do nothing
                        }
                }
        }
@@ -575,7 +577,7 @@ public class IdentifierIndexManager {
                String line = (String) fFileMap.get(file.getProjectRelativePath()
                                .toString());
                if (line != null) {
-                       PHPIdentifierLocation ident;
+                       //PHPIdentifierLocation ident;
                        ArrayList allClassNames = new ArrayList();
                        addLine(treeMap, null, line, allClassNames);
                        int i = 0;
@@ -587,7 +589,7 @@ public class IdentifierIndexManager {
        }
 
        public void addIdentifiers(TreeMap treeMap, String startClazz) {
-               PHPIdentifierLocation ident;
+               //PHPIdentifierLocation ident;
                ArrayList allClassNames = new ArrayList();
                addClassName(treeMap, startClazz, allClassNames);
                int i = 0;