patch from http://www.cowbarn.org/phpeclipse/
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / Parser.java
index e99fb28..e7491b1 100644 (file)
@@ -19,6 +19,7 @@ import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
 import net.sourceforge.phpdt.internal.compiler.problem.ProblemSeverities;
 import net.sourceforge.phpdt.internal.compiler.util.Util;
+import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
 import net.sourceforge.phpeclipse.internal.compiler.ast.AstNode;
 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
@@ -69,12 +70,15 @@ public class Parser //extends PHPParserSuperclass
   //private boolean phpMode;
   protected int modifiers;
   protected int modifiersSourceStart;
+//  protected IdentifierIndexManager indexManager;
+  
   protected Parser(ProblemReporter problemReporter) {
     this.problemReporter = problemReporter;
     this.options = problemReporter.options;
     this.currentPHPString = 0;
     //         PHPParserSuperclass.fileToParse = fileToParse;
     this.phpList = null;
+//    this.indexManager = null;
     this.str = "";
     this.token = TokenNameEOF;
     //    this.chIndx = 0;
@@ -88,6 +92,7 @@ public class Parser //extends PHPParserSuperclass
     this.currentPHPString = 0;
     //    PHPParserSuperclass.fileToParse = fileToParse;
     this.phpList = null;
+//    this.indexManager = null;
     this.str = "";
     this.token = TokenNameEOF;
     this.phpEnd = false;
@@ -203,6 +208,13 @@ public class Parser //extends PHPParserSuperclass
       }
     } catch (InvalidInputException e) {
       token = TokenNameERROR;
+      String detailedMessage = e.getMessage();
+      
+      if (detailedMessage==Scanner.UNTERMINATED_STRING) {
+        throwSyntaxError("Unterminated string.");
+      } else if (detailedMessage==Scanner.UNTERMINATED_COMMENT) {
+        throwSyntaxError("Unterminated commment.");
+      }
     }
     return;
   }
@@ -219,9 +231,13 @@ public class Parser //extends PHPParserSuperclass
     scanner.setPHPMode(false);
   }
   protected void initialize(boolean phpMode) {
+    initialize(phpMode, null);
+  }
+  protected void initialize(boolean phpMode, IdentifierIndexManager indexManager) {
     compilationUnit = null;
     referenceContext = null;
     includesList = new ArrayList();
+//    this.indexManager = indexManager;
     this.str = "";
     this.token = TokenNameEOF;
     //    this.chIndx = 0;
@@ -851,6 +867,7 @@ public class Parser //extends PHPParserSuperclass
     } else if (token == TokenNamefunction) {
       MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
       methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
+      methodDecl.modifiers = AccDefault;
       getNextToken();
       functionDefinition(methodDecl);
       return;
@@ -1241,7 +1258,7 @@ public class Parser //extends PHPParserSuperclass
     //    '(' parameter_list ')' method_body
     initializeModifiers();
     int declarationSourceStart = scanner.getCurrentTokenStartPosition();
-    ;
+    
     if (token == TokenNamevar) {
       checkAndSetModifiers(AccPublic);
       problemReporter.phpVarDeprecatedWarning(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
@@ -1249,7 +1266,8 @@ public class Parser //extends PHPParserSuperclass
       getNextToken();
       class_variable_declaration(declarationSourceStart, list);
     } else if (token == TokenNameconst) {
-      class_constant_declaration();
+      checkAndSetModifiers(AccFinal|AccPublic);
+      class_constant_declaration(declarationSourceStart, list);
       if (token != TokenNameSEMICOLON) {
         throwSyntaxError("';' expected after class const declaration.");
       }
@@ -1272,21 +1290,8 @@ public class Parser //extends PHPParserSuperclass
         class_variable_declaration(declarationSourceStart, list);
       }
     }
-    //    if (token == TokenNamefunction) {
-    //      MethodDeclaration methodDecl = new MethodDeclaration(
-    //          this.compilationUnit.compilationResult);
-    //      methodDecl.declarationSourceStart = scanner
-    //          .getCurrentTokenStartPosition();
-    //      getNextToken();
-    //      functionDefinition(methodDecl);
-    //    } else if (token == TokenNamevar) {
-    //      getNextToken();
-    //      classProperty();
-    //    } else {
-    //      throwSyntaxError("'function' or 'var' expected.");
-    //    }
   }
-  private void class_constant_declaration() {
+  private void class_constant_declaration(int declarationSourceStart, ArrayList list) {
     // class_constant_declaration ',' T_STRING '=' static_scalar
     // | T_CONST T_STRING '=' static_scalar
     if (token != TokenNameconst) {
@@ -1298,6 +1303,14 @@ public class Parser //extends PHPParserSuperclass
       if (token != TokenNameIdentifier) {
         throwSyntaxError("Identifier expected in class const declaration.");
       }
+      FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
+          .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
+      fieldDeclaration.modifiers = this.modifiers;
+      fieldDeclaration.declarationSourceStart = declarationSourceStart;
+      fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
+      fieldDeclaration.modifiersSourceStart = declarationSourceStart;
+      //        fieldDeclaration.type
+      list.add(fieldDeclaration);
       getNextToken();
       if (token != TokenNameEQUAL) {
         throwSyntaxError("'=' expected in class const declaration.");
@@ -1385,9 +1398,12 @@ public class Parser //extends PHPParserSuperclass
     // | class_variable_declaration ',' T_VARIABLE '=' static_scalar
     // | T_VARIABLE
     // | T_VARIABLE '=' static_scalar
+    char[] classVariable;
     do {
       if (token == TokenNameVariable) {
-        FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
+        classVariable = scanner.getCurrentIdentifierSource();
+      //  indexManager.addIdentifierInformation('v', classVariable, buf, -1, -1);
+        FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner
             .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
         fieldDeclaration.modifiers = this.modifiers;
         fieldDeclaration.declarationSourceStart = declarationSourceStart;
@@ -1456,10 +1472,16 @@ public class Parser //extends PHPParserSuperclass
     if (token == TokenNameAND) {
       getNextToken();
     }
-    if (token == TokenNameIdentifier) {
-      methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
-      methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
+    methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
+    methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
+    if (Scanner.isIdentifierOrKeyword(token)) {
       methodDecl.selector = scanner.getCurrentIdentifierSource();
+      if (token > TokenNameKEYWORD) {
+        problemReporter.phpKeywordWarning(new String[]{scanner.toStringAction(token)}, scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
+            referenceContext, compilationUnit.compilationResult);
+//        reportSyntaxWarning("Don't use keyword for function declaration [" + scanner.toStringAction(token) + "].",
+//          scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
+      }
       getNextToken();
       if (token == TokenNameLPAREN) {
         getNextToken();
@@ -1467,7 +1489,7 @@ public class Parser //extends PHPParserSuperclass
         throwSyntaxError("'(' expected in function declaration.");
       }
       if (token != TokenNameRPAREN) {
-        parameter_list();
+        parameter_list(); 
       }
       if (token != TokenNameRPAREN) {
         throwSyntaxError("')' expected in function declaration.");
@@ -1476,9 +1498,7 @@ public class Parser //extends PHPParserSuperclass
         getNextToken();
       }
     } else {
-      if (token > TokenNameKEYWORD) {
-        throwSyntaxError("Don't use keyword for function declaration [" + token + "].");
-      }
+      methodDecl.selector = "<undefined>".toCharArray();
       throwSyntaxError("Function name expected after keyword 'function'.");
     }
   }
@@ -2442,14 +2462,21 @@ public class Parser //extends PHPParserSuperclass
     //| class_constant '(' function_call_parameter_list ')'
     //| static_member '(' function_call_parameter_list ')'
     //| variable_without_objects '(' function_call_parameter_list ')'
+    char[] defineName = null;
+    int startPos=0;
+    int endPos=0;
     if (Scanner.TRACE) {
       System.out.println("TRACE: function_call()");
     }
     if (token == TokenNameIdentifier) {
+      defineName = scanner.getCurrentIdentifierSource();
+      startPos = scanner.getCurrentTokenStartPosition();
+      endPos = scanner.getCurrentTokenEndPosition();
       getNextToken();
       switch (token) {
         case TokenNamePAAMAYIM_NEKUDOTAYIM :
           // static member:
+          defineName = null;
           getNextToken();
           if (token == TokenNameIdentifier) {
             // class _constant
@@ -2464,6 +2491,45 @@ public class Parser //extends PHPParserSuperclass
       variable_without_objects();
     }
     if (token != TokenNameLPAREN) {
+      if (defineName!=null) {
+        // does this identifier contain only uppercase characters?
+        if (defineName.length==3) {
+          if (defineName[0]=='d' &&
+              defineName[1]=='i' &&
+              defineName[2]=='e' ) {
+            defineName=null;
+          } 
+        } else if (defineName.length==4) {
+          if (defineName[0]=='t' &&
+              defineName[1]=='r' &&
+              defineName[2]=='u' &&
+              defineName[3]=='e' ) {
+            defineName=null;
+          } else if (defineName[0]=='n' &&
+              defineName[1]=='u' &&
+              defineName[2]=='l' &&
+              defineName[3]=='l' ) {
+            defineName=null;
+          }
+        } else if (defineName.length==5) {
+          if (defineName[0]=='f' &&
+              defineName[1]=='a' &&
+              defineName[2]=='l' &&
+              defineName[3]=='s' &&
+              defineName[4]=='e' ) {
+            defineName=null;
+          }
+        }
+        if (defineName!=null) {
+          for (int i=0; i<defineName.length;i++) {
+            if (Character.isLowerCase(defineName[i])) {  
+              problemReporter.phpUppercaseIdentifierWarning(startPos, endPos,
+                referenceContext, compilationUnit.compilationResult);
+              break;
+            }
+          }
+        }
+      }
       // TODO is this ok ?
       return;
       //      throwSyntaxError("'(' expected in function call.");