fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / IdentifierIndexManager.java
index 2d84bec..86de9b5 100644 (file)
@@ -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 == TokenNameCOMMENT_BLOCK  || fToken == TokenNameCOMMENT_PHPDOC) {
+                                               getNextToken();
+                               }
+                       } catch (InvalidInputException e1) {
+                               // TODO Auto-generated catch block
+                               e1.printStackTrace();
+                       }
+               }
+               
                private void parseDeclarations(char[] parent, StringBuffer buf,
                                boolean goBack) {
                        char[] ident;
@@ -143,6 +149,7 @@ public class IdentifierIndexManager {
                        boolean hasModifiers = false;
                        int phpdocOffset = -1;
                        int phpdocLength = -1;
+
                        try {
                                while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
                                        phpdocOffset = -1;
@@ -166,7 +173,7 @@ public class IdentifierIndexManager {
                                                }
                                        }
                                        if (fToken == TokenNamefunction) {
-                                               getNextToken();
+                                               skipComments();
                                                if (fToken == TokenNameAND) {
                                                        getNextToken();
                                                }
@@ -188,40 +195,41 @@ public class IdentifierIndexManager {
                                                                                        phpdocOffset, phpdocLength);
                                                                }
                                                        }
-                                                       getNextToken();
+                                                       skipComments();
                                                        parseDeclarations(null, buf, true);
                                                }
                                        } else if (fToken == TokenNameclass
                                                        || fToken == TokenNameinterface) {
-                                               getNextToken();
+                                               skipComments();
                                                if (fToken == TokenNameIdentifier) {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        addIdentifierInformation('c', ident, buf,
                                                                        phpdocOffset, phpdocLength);
-                                                       getNextToken();
+                                                       skipComments();
                                                        if (fToken == TokenNameextends) {
-                                                               getNextToken();
+                                                               skipComments();
                                                                while (fToken == TokenNameIdentifier) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // extends ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
-                                                                       if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                       skipComments();
+                                                               if (fToken == TokenNameCOMMA) {
+                                                                       skipComments();
                                                                        }
                                                                }
                                                        }
                                                        if (fToken == TokenNameimplements) {
-                                                               getNextToken();
+                                                               skipComments();
                                                                while (fToken == TokenNameIdentifier) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // implements ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
+                                                                       skipComments();
                                                                        if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                               skipComments();
+//                                                                             getNextToken();
                                                                        }
                                                                }
                                                        }
@@ -247,7 +255,7 @@ public class IdentifierIndexManager {
                                                                || fToken == TokenNamepublic
                                                                || fToken == TokenNameprotected
                                                                || fToken == TokenNameprivate) {
-                                                       getNextToken();
+                                                       skipComments();
                                                }
                                                while (fToken == TokenNameVariable) {
                                                        ident = fScanner.getCurrentIdentifierSource();
@@ -256,9 +264,9 @@ public class IdentifierIndexManager {
                                                                        ident.length - 1);
                                                        addClassVariableInformation('v', classVariable,
                                                                        buf, phpdocOffset, phpdocLength);
-                                                       getNextToken();
+                                                       skipComments();
                                                        if (fToken == TokenNameCOMMA) {
-                                                               getNextToken();
+                                                               skipComments();
                                                        }
                                                }
                                        } else if (!hasModifiers && fToken == TokenNameIdentifier) {
@@ -318,6 +326,7 @@ public class IdentifierIndexManager {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
+                       
                }
 
                synchronized public void parseIdentifiers(char[] charArray,
@@ -330,13 +339,15 @@ public class IdentifierIndexManager {
                        fScanner.setSource(charArray);
                        fScanner.setPHPMode(false);
                        fToken = TokenNameEOF;
+                       
                        try {
                                getNextToken();
                                while (fToken != TokenNameEOF) { // && fToken !=
-                                                                                                       // TokenNameERROR) {
+                                       // TokenNameERROR) {
                                        phpdocOffset = -1;
                                        hasModifiers = false;
-                                       if (fToken == TokenNameCOMMENT_PHPDOC) {
+                                       switch (fToken) {
+                                       case TokenNameCOMMENT_PHPDOC:
                                                phpdocOffset = fScanner.getCurrentTokenStartPosition();
                                                phpdocLength = fScanner.getCurrentTokenEndPosition()
                                                                - fScanner.getCurrentTokenStartPosition() + 1;
@@ -353,9 +364,10 @@ public class IdentifierIndexManager {
                                                if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
                                                        break;
                                                }
-                                       }
-                                       if (fToken == TokenNamefunction) {
-                                               getNextToken();
+                                               break;
+                                               
+                                       case TokenNamefunction:
+                                               skipComments();
                                                if (fToken == TokenNameAND) {
                                                        getNextToken();
                                                }
@@ -363,40 +375,60 @@ public class IdentifierIndexManager {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        addIdentifierInformation('f', ident, buf,
                                                                        phpdocOffset, phpdocLength);
-                                                       getNextToken();
+                                                       skipComments();
+                                                       if (fToken == TokenNameLPAREN) {
+                                                               skipComments();
+                                                               do {
+                                                                       if (fToken == TokenNameVariable) {
+                                                                               ident = fScanner.getCurrentIdentifierSource();
+                                                                               addIdentifierInformation('v', ident, buf,
+                                                                                               phpdocOffset, phpdocLength);
+                                                                               skipComments();
+                                                                               if (fToken == TokenNameCOMMA) {
+                                                                                       skipComments();
+                                                                               }
+                                                                       }       
+/*                                                                     skipComments();
+                                                                       if (fToken == TokenNameCOMMA) {
+                                                                               skipComments();
+                                                                       }*/
+                                                               } while (fToken != TokenNameRPAREN );
+                                                       }
                                                        parseDeclarations(null, buf, true);
                                                }
-                                       } else if (fToken == TokenNameclass
-                                                       || fToken == TokenNameinterface) {
-                                               getNextToken();
+                                               break;
+                                               
+                                       case TokenNameclass:
+                                       case TokenNameinterface:
+                                               skipComments();
                                                if (fToken == TokenNameIdentifier) {
                                                        ident = fScanner.getCurrentIdentifierSource();
                                                        addIdentifierInformation('c', ident, buf,
                                                                        phpdocOffset, phpdocLength);
-                                                       getNextToken();
+                                                       skipComments();
                                                        if (fToken == TokenNameextends) {
-                                                               getNextToken();
+                                                               skipComments();
                                                                while (fToken == TokenNameIdentifier) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // extends ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
+                                                                       skipComments();
                                                                        if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                               skipComments();
                                                                        }
                                                                }
                                                        }
                                                        if (fToken == TokenNameimplements) {
-                                                               getNextToken();
+                                                               skipComments();
                                                                while (fToken == TokenNameIdentifier) {
                                                                        ident = fScanner
                                                                                        .getCurrentIdentifierSource();
                                                                        // implements ident
                                                                        addIdentifierInformation('e', ident, buf);
-                                                                       getNextToken();
+                                                                       skipComments();
                                                                        if (fToken == TokenNameCOMMA) {
-                                                                               getNextToken();
+                                                                               skipComments();
                                                                        }
                                                                }
                                                        }
@@ -410,39 +442,20 @@ public class IdentifierIndexManager {
                                                        }
                                                        parseDeclarations(ident, buf, true);
                                                }
-                                       } else if (fToken == TokenNameVariable) {
+                                               break;
+
+                                       case TokenNameVariable:
                                                // 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
@@ -452,7 +465,7 @@ public class IdentifierIndexManager {
                        }
                }
        }
-
+               
        class StringComparator implements Comparator {
                public int compare(Object o1, Object o2) {
                        String s1 = (String) o1;
@@ -524,12 +537,12 @@ public class IdentifierIndexManager {
         * @throws CoreException
         */
        public void addInputStream(InputStream stream, String filePath,
-                       LineCreator lineCreator , String charset) throws CoreException {
+                       LineCreator lineCreator, String charset) throws CoreException {
                try {
                        StringBuffer lineBuffer = new StringBuffer();
                        lineBuffer.append(filePath);
                        lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream,
-                                       -1, charset),  lineBuffer);
+                                       -1, charset), lineBuffer);
                        addLine(lineBuffer.toString());
                } catch (IOException e) {
                        e.printStackTrace();
@@ -539,7 +552,7 @@ public class IdentifierIndexManager {
                                        stream.close();
                                }
                        } catch (IOException e) {
-                               //do nothing
+                               // do nothing
                        }
                }
        }