a lot of bugfixes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParser.jj
index 205b380..d45ca07 100644 (file)
@@ -1,3 +1,4 @@
+
 options {
   LOOKAHEAD = 1;
   CHOICE_AMBIGUITY_CHECK = 2;
@@ -52,6 +53,10 @@ import net.sourceforge.phpdt.internal.corext.Assert;
  */
 public final class PHPParser extends PHPParserSuperclass {
 
+//todo : fix the variables names bug
+//todo : handle tilde operator
+
+
   /** The current segment. */
   private static OutlineableWithChildren currentSegment;
 
@@ -82,7 +87,7 @@ public final class PHPParser extends PHPParserSuperclass {
   /** The cursor in expression stack. */
   private static int nodePtr;
 
-  private static final boolean PARSER_DEBUG = false;
+  private static final boolean PARSER_DEBUG = true;
 
   public final void setFileToParse(final IFile fileToParse) {
     PHPParser.fileToParse = fileToParse;
@@ -317,8 +322,9 @@ public final class PHPParser extends PHPParserSuperclass {
    * Put a new html block in the stack.
    */
   public static final void createNewHTMLCode() {
-    final int currentPosition = SimpleCharStream.getPosition();
-    if (currentPosition == htmlStart || currentPosition > SimpleCharStream.currentBuffer.length()) {
+    final int currentPosition = token.sourceStart;
+    if (currentPosition == htmlStart ||
+          currentPosition > SimpleCharStream.currentBuffer.length()) {
       return;
     }
     final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
@@ -327,19 +333,20 @@ public final class PHPParser extends PHPParserSuperclass {
 
   /** Create a new task. */
   public static final void createNewTask() {
-    final int currentPosition = SimpleCharStream.getPosition();
+    final int currentPosition = token.sourceStart;
     final String  todo = SimpleCharStream.currentBuffer.substring(currentPosition-3,
                                                                   SimpleCharStream.currentBuffer.indexOf("\n",
                                                                                                          currentPosition)-1);
-    PHPeclipsePlugin.log(1,SimpleCharStream.currentBuffer.toString());
-    try {
-      setMarker(fileToParse,
-                todo,
-                SimpleCharStream.getBeginLine(),
-                TASK,
-                "Line "+SimpleCharStream.getBeginLine());
-    } catch (CoreException e) {
-      PHPeclipsePlugin.log(e);
+    if (!PARSER_DEBUG) {
+      try {
+        setMarker(fileToParse,
+                  todo,
+                  SimpleCharStream.getBeginLine(),
+                  TASK,
+                  "Line "+SimpleCharStream.getBeginLine());
+      } catch (CoreException e) {
+        PHPeclipsePlugin.log(e);
+      }
     }
   }
 
@@ -371,7 +378,7 @@ TOKEN_MGR_DECLS:
 
 <PHPPARSING, IN_SINGLE_LINE_COMMENT> TOKEN :
 {
-  <PHPEND :"?>"> {PHPParser.htmlStart = SimpleCharStream.getPosition();} : DEFAULT
+  <PHPEND :"?>"> {PHPParser.htmlStart = PHPParser.token.sourceEnd;} : DEFAULT
 }
 
 /* Skip any character if we are not in php mode */
@@ -666,20 +673,20 @@ void phpFile() :
  */
 void PhpBlock() :
 {
-  final int start = SimpleCharStream.getPosition();
   final PHPEchoBlock phpEchoBlock;
+  final Token token;
 }
 {
   phpEchoBlock = phpEchoBlock()
   {pushOnAstNodes(phpEchoBlock);}
 |
   [   <PHPSTARTLONG>
-    | <PHPSTARTSHORT>
+    | token = <PHPSTARTSHORT>
     {try {
       setMarker(fileToParse,
                 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
-                start,
-                SimpleCharStream.getPosition(),
+                token.sourceStart,
+                token.sourceEnd,
                 INFO,
                 "Line " + token.beginLine);
     } catch (CoreException e) {
@@ -707,7 +714,7 @@ PHPEchoBlock phpEchoBlock() :
 {
   token = <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
   {
-  echoBlock = new PHPEchoBlock(expr,token.sourceStart,token.sourceEnd);
+  echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
   pushOnAstNodes(echoBlock);
   return echoBlock;}
 }
@@ -831,7 +838,6 @@ FieldDeclaration FieldDeclaration() :
   VariableDeclaration variableDeclaration;
   final VariableDeclaration[] list;
   final ArrayList arrayList = new ArrayList();
-  final int pos = SimpleCharStream.getPosition();
   final Token token;
   Token token2 = null;
 }
@@ -876,18 +882,19 @@ VariableDeclaration VariableDeclaratorNoSuffix() :
 {
   final Token varName;
   Expression initializer = null;
+  Token assignToken;
 }
 {
   varName = <DOLLAR_ID>
   [
-    <ASSIGN>
+    assignToken = <ASSIGN>
     try {
       initializer = VariableInitializer()
     } catch (ParseException e) {
       errorMessage = "Literal expression expected in variable initializer";
       errorLevel   = ERROR;
-      errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
-      errorEnd   = SimpleCharStream.getPosition() + 1;
+      errorStart = assignToken.sourceEnd +1;
+      errorEnd   = assignToken.sourceEnd +1;
       processParseExceptionDebug(e);
     }
   ]
@@ -895,18 +902,18 @@ VariableDeclaration VariableDeclaratorNoSuffix() :
   if (initializer == null) {
     return new VariableDeclaration(currentSegment,
                                    new Variable(varName.image.substring(1),
-                                                varName.sourceStart,
-                                                varName.sourceEnd),
-                                   varName.sourceStart,
-                                   varName.sourceEnd);
+                                                varName.sourceStart+1,
+                                                varName.sourceEnd+1),
+                                   varName.sourceStart+1,
+                                   varName.sourceEnd+1);
   }
   return new VariableDeclaration(currentSegment,
                                  new Variable(varName.image.substring(1),
-                                              varName.sourceStart,
-                                              varName.sourceEnd),
+                                              varName.sourceStart+1,
+                                              varName.sourceEnd+1),
                                  initializer,
                                  VariableDeclaration.EQUAL,
-                                 varName.sourceStart);
+                                 varName.sourceStart+1);
   }
 }
 
@@ -955,7 +962,6 @@ AbstractVariable VariableDeclaratorId() :
 {
   final Variable var;
   AbstractVariable expression = null;
-  final int pos = SimpleCharStream.getPosition();
 }
 {
   try {
@@ -997,8 +1003,8 @@ Variable Variable():
   {
     if (expression == null) {
       return new Variable(token.image.substring(1),
-                          token.sourceStart,
-                          token.sourceEnd);
+                          token.sourceStart+1,
+                          token.sourceEnd+1);
     }
     String s = expression.toStringExpression();
     buff = new StringBuffer(token.image.length()+s.length()+2);
@@ -1007,7 +1013,7 @@ Variable Variable():
     buff.append(s);
     buff.append("}");
     s = buff.toString();
-    return new Variable(s,token.sourceStart,token.sourceEnd);
+    return new Variable(s,token.sourceStart+1,token.sourceEnd+1);
   }
 |
   token = <DOLLAR>
@@ -1024,12 +1030,12 @@ Variable Variable() :
  token = <DOLLAR_ID> [variable = Var(token)]
   {
     if (variable == null) {
-      return new Variable(token.image.substring(1),token.sourceStart,token.sourceEnd);
+      return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
     }
     final StringBuffer buff = new StringBuffer();
     buff.append(token.image.substring(1));
     buff.append(variable.toStringExpression());
-    return new Variable(buff.toString(),token.sourceStart,variable.sourceEnd);
+    return new Variable(buff.toString(),token.sourceStart+1,variable.sourceEnd+1);
   }
 |
   token = <DOLLAR> variable = Var(token)
@@ -1047,7 +1053,7 @@ Variable Var(final Token dollar) :
 {
   token = <DOLLAR_ID> [variable = Var(token)]
   {if (variable == null) {
-     return new Variable(token.image.substring(1),token.sourceStart,token.sourceEnd);
+     return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
    }
    final StringBuffer buff = new StringBuffer();
    buff.append(token.image.substring(1));
@@ -1071,11 +1077,9 @@ ConstantIdentifier VariableName():
 {
   final StringBuffer buff;
   String expr;
-  final Variable var;
   Expression expression = null;
   final Token token;
   Token token2 = null;
-  int pos;
 }
 {
   token = <LBRACE> expression = Expression() token2 = <RBRACE>
@@ -1084,7 +1088,6 @@ ConstantIdentifier VariableName():
    buff.append("{");
    buff.append(expr);
    buff.append("}");
-   pos = SimpleCharStream.getPosition();
    expr = buff.toString();
    return new ConstantIdentifier(expr,
                                  token.sourceStart,
@@ -1123,8 +1126,8 @@ ConstantIdentifier VariableName():
   token = <DOLLAR_ID>
   {
   return new Variable(token.image,
-                      token.sourceStart,
-                      token.sourceEnd);
+                      token.sourceStart+1,
+                      token.sourceEnd+1);
   } */
 }
 
@@ -1686,7 +1689,6 @@ Expression AtNotUnaryExpression() :
   {return expr;}
 }
 
-
 Expression UnaryExpressionNoPrefix() :
 {
   final Expression expr;
@@ -1787,7 +1789,7 @@ Expression PostfixExpression() :
 
 Expression PrimaryExpression() :
 {
-  Expression expr = null;
+  Expression expr;
   Token token = null;
 }
 {
@@ -1800,11 +1802,9 @@ Expression PrimaryExpression() :
 
 Expression refPrimaryExpression(final Token reference) :
 {
-  Expression expr = null;
+  Expression expr;
   Expression expr2 = null;
-  int assignOperator = -1;
   final Token identifier;
-  final String var;
 }
 {
   identifier = <IDENTIFIER>
@@ -1873,7 +1873,6 @@ Expression ClassIdentifier():
 {
   final Expression expr;
   final Token token;
-  final ConstantIdentifier type;
 }
 {
   token = <IDENTIFIER>          {return new ConstantIdentifier(token);}
@@ -1886,20 +1885,19 @@ Expression ClassIdentifier():
  */
 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
 {
-  Variable expr = null;
-  final int pos = SimpleCharStream.getPosition();
   Expression expression = null;
+  final Token classAccessToken;
 }
 {
-  <CLASSACCESS>
+  classAccessToken = <CLASSACCESS>
   try {
-    expression = VariableName()
+    ( expression = VariableName() | expression = Variable() )
   } catch (ParseException e) {
     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
     errorLevel   = ERROR;
-    errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
-    errorEnd   = SimpleCharStream.getPosition() + 1;
-    throw e;
+    errorStart = classAccessToken.sourceEnd +1;
+    errorEnd   = classAccessToken.sourceEnd +1;
+    processParseExceptionDebug(e);
   }
   {return new ClassAccess(prefix,
                           expression,
@@ -2204,17 +2202,18 @@ ListExpression ListExpression() :
   final Expression expression;
   final ArrayList list = new ArrayList();
   final int pos = SimpleCharStream.getPosition();
+  final Token listToken;
 }
 {
-  <LIST>
+  listToken = <LIST>
   try {
     <LPAREN>
   } catch (ParseException e) {
     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
     errorLevel   = ERROR;
-    errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
-    errorEnd     = SimpleCharStream.getPosition() + 1;
-    throw e;
+    errorStart   = listToken.sourceEnd+1;
+    errorEnd     = listToken.sourceEnd+1;
+    processParseExceptionDebug(e);
   }
   [
     expr = VariableDeclaratorId()
@@ -2244,7 +2243,7 @@ ListExpression ListExpression() :
   }
   [ <ASSIGN> expression = Expression()
     {
-    final Variable[] vars = new Variable[list.size()];
+    final AbstractVariable[] vars = new AbstractVariable[list.size()];
     list.toArray(vars);
     return new ListExpression(vars,
                               expression,
@@ -2449,19 +2448,20 @@ Statement BlockStatementNoBreak() :
 /**
  * used only by ForInit()
  */
-VariableDeclaration[] LocalVariableDeclaration() :
+Expression[] LocalVariableDeclaration() :
 {
   final ArrayList list = new ArrayList();
-  VariableDeclaration var;
+  Expression var;
 }
 {
-  var = LocalVariableDeclarator()
+  var = Expression()
   {list.add(var);}
-  ( <COMMA> var = LocalVariableDeclarator() {list.add(var);})*
+  ( <COMMA> var = Expression() {list.add(var);})*
   {
-    final VariableDeclaration[] vars = new VariableDeclaration[list.size()];
+    final Expression[] vars = new Expression[list.size()];
     list.toArray(vars);
-  return vars;}
+    return vars;
+  }
 }
 
 /**
@@ -2503,8 +2503,7 @@ EmptyStatement EmptyStatement() :
  */
 Expression StatementExpression() :
 {
-  final Expression expr,expr2;
-  final int operator;
+  final Expression expr;
 }
 {
   expr = PreIncDecExpression() {return expr;}
@@ -2525,9 +2524,10 @@ SwitchStatement SwitchStatement() :
   final Expression variable;
   final AbstractCase[] cases;
   final int pos = SimpleCharStream.getPosition();
+  final Token switchToken;
 }
 {
-  <SWITCH>
+  switchToken = <SWITCH>
   try {
     <LPAREN>
   } catch (ParseException e) {
@@ -2558,8 +2558,8 @@ SwitchStatement SwitchStatement() :
     errorEnd   = SimpleCharStream.getPosition() + 1;
     throw e;
   }
-  (cases = switchStatementBrace() | cases = switchStatementColon(pos, pos + 6))
-  {return new SwitchStatement(variable,cases,pos,SimpleCharStream.getPosition());}
+  (cases = switchStatementBrace() | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
+  {return new SwitchStatement(variable,cases,switchToken.sourceStart,SimpleCharStream.getPosition());}
 }
 
 AbstractCase[] switchStatementBrace() :
@@ -2670,19 +2670,19 @@ Expression SwitchLabel() :
     if (errorMessage != null) throw e;
     errorMessage = "expression expected after 'case' keyword";
     errorLevel   = ERROR;
-    errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
-    errorEnd   = SimpleCharStream.getPosition() + 1;
+    errorStart = token.sourceEnd +1;
+    errorEnd   = token.sourceEnd +1;
     throw e;
   }
   try {
-    <COLON>
+    token = <COLON>
     {return expr;}
   } catch (ParseException e) {
     errorMessage = "':' expected after case expression";
     errorLevel   = ERROR;
-    errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
-    errorEnd   = SimpleCharStream.getPosition() + 1;
-    throw e;
+    errorStart = expr.sourceEnd+1;
+    errorEnd   = expr.sourceEnd+1;
+    processParseExceptionDebug(e);
   }
 |
   token = <_DEFAULT>
@@ -2692,9 +2692,9 @@ Expression SwitchLabel() :
   } catch (ParseException e) {
     errorMessage = "':' expected after 'default' keyword";
     errorLevel   = ERROR;
-    errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
-    errorEnd   = SimpleCharStream.getPosition() + 1;
-    throw e;
+    errorStart = token.sourceEnd+1;
+    errorEnd   = token.sourceEnd+1;
+    processParseExceptionDebug(e);
   }
 }
 
@@ -2719,14 +2719,13 @@ Break BreakStatement() :
 
 IfStatement IfStatement() :
 {
-  final int pos = SimpleCharStream.getPosition();
   final Expression condition;
   final IfStatement ifStatement;
   Token token;
 }
 {
-  token = <IF> condition = Condition("if") ifStatement = IfStatement0(condition,
-                                                                      token.sourceStart,token.sourceStart+2)
+  token = <IF> condition = Condition("if")
+  ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
   {return ifStatement;}
 }
 
@@ -3058,6 +3057,10 @@ ForeachStatement ForeachStatement() :
 
 }
 
+/**
+ * a for declaration.
+ * @return a node representing the for statement
+ */
 ForStatement ForStatement() :
 {
 final Token token,token2;
@@ -3067,7 +3070,6 @@ Expression condition = null;
 Expression[] increments = null;
 Statement action;
 final ArrayList list = new ArrayList();
-final int startBlock, endBlock;
 }
 {
   token = <FOR>
@@ -3076,9 +3078,9 @@ final int startBlock, endBlock;
   } catch (ParseException e) {
     errorMessage = "'(' expected after 'for' keyword";
     errorLevel   = ERROR;
-    errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
-    errorEnd   = SimpleCharStream.getPosition() + 1;
-    throw e;
+    errorStart = token.sourceEnd;
+    errorEnd   = token.sourceEnd +1;
+    processParseExceptionDebug(e);
   }
      [ initializations = ForInit() ] <SEMICOLON>
      [ condition = Expression() ] <SEMICOLON>
@@ -3093,21 +3095,19 @@ final int startBlock, endBlock;
                                action.sourceEnd);}
     |
       <COLON>
-      {startBlock = SimpleCharStream.getPosition();}
       (action = Statement() {list.add(action);})*
       {
         try {
         setMarker(fileToParse,
                   "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
-                  pos,
-                  pos+token.image.length(),
+                  token.sourceStart,
+                  token.sourceEnd,
                   INFO,
                   "Line " + token.beginLine);
         } catch (CoreException e) {
           PHPeclipsePlugin.log(e);
         }
       }
-      {endBlock = SimpleCharStream.getPosition();}
       try {
         <ENDFOR>
       } catch (ParseException e) {
@@ -3159,8 +3159,8 @@ Expression[] StatementExpressionList() :
   final Expression expr;
 }
 {
-  expr = StatementExpression()   {list.add(expr);}
-  (<COMMA> StatementExpression() {list.add(expr);})*
+  expr = Expression()   {list.add(expr);}
+  (<COMMA> Expression() {list.add(expr);})*
   {
     final Expression[] exprsArray = new Expression[list.size()];
     list.toArray(exprsArray);
@@ -3203,5 +3203,6 @@ ReturnStatement ReturnStatement() :
     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
     errorEnd   = SimpleCharStream.getPosition() + 1;
     throw e;
-  }
-}
\ No newline at end of file
+ }
+}
+