*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParser.jj
index 34ee7b7..bb3d533 100644 (file)
@@ -30,6 +30,7 @@ import org.eclipse.jface.preference.IPreferenceStore;
 
 import java.util.Hashtable;
 import java.io.StringReader;
+import java.io.*;
 import java.text.MessageFormat;
 
 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
@@ -50,6 +51,7 @@ import net.sourceforge.phpdt.internal.compiler.parser.PHPReqIncDeclaration;
  */
 public final class PHPParser extends PHPParserSuperclass {
 
+  /** The file that is parsed. */
   private static IFile fileToParse;
 
   /** The current segment */
@@ -58,9 +60,15 @@ public final class PHPParser extends PHPParserSuperclass {
   private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
   private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
   PHPOutlineInfo outlineInfo;
+
+  /** The error level of the current ParseException. */
   private static int errorLevel = ERROR;
+  /** The message of the current ParseException. If it's null it's because the parse exception wasn't handled */
   private static String errorMessage;
 
+  private static int errorStart = -1;
+  private static int errorEnd = -1;
+
   public PHPParser() {
   }
 
@@ -83,6 +91,21 @@ public final class PHPParser extends PHPParserSuperclass {
     phpTest();
   }
 
+  public static final void htmlParserTester(final File fileName) throws CoreException, ParseException {
+    try {
+      final Reader stream = new FileReader(fileName);
+      if (jj_input_stream == null) {
+        jj_input_stream = new SimpleCharStream(stream, 1, 1);
+      }
+      ReInit(stream);
+      phpFile();
+    } catch (FileNotFoundException e) {
+      e.printStackTrace();  //To change body of catch statement use Options | File Templates.
+    } catch (ParseException e) {
+      e.printStackTrace();  //To change body of catch statement use Options | File Templates.
+    }
+  }
+
   public static final void htmlParserTester(final String strEval) throws CoreException, ParseException {
     final StringReader stream = new StringReader(strEval);
     if (jj_input_stream == null) {
@@ -117,6 +140,8 @@ public final class PHPParser extends PHPParserSuperclass {
     if (errorMessage == null) {
       PHPeclipsePlugin.log(e);
       errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
+      errorStart = jj_input_stream.getPosition();
+      errorEnd   = errorStart + 1;
     }
     setMarker(e);
     errorMessage = null;
@@ -128,12 +153,23 @@ public final class PHPParser extends PHPParserSuperclass {
    */
   private static void setMarker(final ParseException e) {
     try {
-      setMarker(fileToParse,
-                errorMessage,
-                jj_input_stream.tokenBegin,
-                jj_input_stream.tokenBegin + e.currentToken.image.length(),
-                errorLevel,
-                "Line " + e.currentToken.beginLine);
+      if (errorStart == -1) {
+        setMarker(fileToParse,
+                  errorMessage,
+                  jj_input_stream.tokenBegin,
+                  jj_input_stream.tokenBegin + e.currentToken.image.length(),
+                  errorLevel,
+                  "Line " + e.currentToken.beginLine);
+      } else {
+        setMarker(fileToParse,
+                  errorMessage,
+                  errorStart,
+                  errorEnd,
+                  errorLevel,
+                  "Line " + e.currentToken.beginLine);
+        errorStart = -1;
+        errorEnd = -1;
+      }
     } catch (CoreException e2) {
       PHPeclipsePlugin.log(e2);
     }
@@ -249,9 +285,9 @@ PARSER_END(PHPParser)
 
 <DEFAULT> TOKEN :
 {
-  <PHPSTARTSHORT : "<?"> : PHPPARSING
+  <PHPSTARTSHORT : "<?">   : PHPPARSING
 | <PHPSTARTLONG : "<?php"> : PHPPARSING
-| <PHPECHOSTART : "<?=">      : PHPPARSING
+| <PHPECHOSTART : "<?=">   : PHPPARSING
 }
 
 <PHPPARSING> TOKEN :
@@ -282,6 +318,8 @@ PARSER_END(PHPParser)
 {
   "//" : IN_SINGLE_LINE_COMMENT
 |
+  "#"  : IN_SINGLE_LINE_COMMENT
+|
   <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
 |
   "/*" : IN_MULTI_LINE_COMMENT
@@ -344,6 +382,10 @@ MORE :
 | <ARRAYASSIGN        : "=>">
 }
 
+<PHPPARSING> TOKEN :
+{
+  <LIST   : "list">
+}
 /* RESERVED WORDS AND LITERALS */
 
 <PHPPARSING> TOKEN :
@@ -568,29 +610,32 @@ void phpFile() :
 
 void PhpBlock() :
 {
-  final int start = jj_input_stream.bufpos;
+  final int start = jj_input_stream.getPosition();
 }
 {
   <PHPECHOSTART> Expression() [ <SEMICOLON> ] <PHPEND>
 |
   [ <PHPSTARTLONG>
-  | <PHPSTARTSHORT>
-  {try {
-    setMarker(fileToParse,
-              "You should use '<?php' instead of '<?' it will avoid some problems with XML",
-              start,
-              jj_input_stream.bufpos,
-              INFO,
-              "Line " + token.beginLine);
-  } catch (CoreException e) {
-    PHPeclipsePlugin.log(e);
-  }}
-  ]Php()
+    | <PHPSTARTSHORT>
+    {try {
+      setMarker(fileToParse,
+                "You should use '<?php' instead of '<?' it will avoid some problems with XML",
+                start,
+                jj_input_stream.getPosition(),
+                INFO,
+                "Line " + token.beginLine);
+    } catch (CoreException e) {
+      PHPeclipsePlugin.log(e);
+    }}
+  ]
+  Php()
   try {
     <PHPEND>
   } catch (ParseException e) {
     errorMessage = "'?>' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -605,10 +650,32 @@ void ClassDeclaration() :
 {
   final PHPClassDeclaration classDeclaration;
   final Token className;
-  final int pos = jj_input_stream.bufpos;
+  final int pos;
 }
 {
-  <CLASS> className = <IDENTIFIER> [ <EXTENDS> <IDENTIFIER> ]
+  <CLASS>
+  try {
+    {pos = jj_input_stream.getPosition();}
+    className = <IDENTIFIER>
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  [
+    <EXTENDS>
+    try {
+      <IDENTIFIER>
+    } catch (ParseException e) {
+      errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
+      errorLevel   = ERROR;
+      errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+      errorEnd   = jj_input_stream.getPosition() + 1;
+      throw e;
+    }
+  ]
   {
     if (currentSegment != null) {
       classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos);
@@ -630,16 +697,20 @@ void ClassBody() :
   try {
     <LBRACE>
   } catch (ParseException e) {
-    errorMessage = "'{' expected";
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image + "', '{' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   ( ClassBodyDeclaration() )*
   try {
     <RBRACE>
   } catch (ParseException e) {
-    errorMessage = "'var', 'function' or '}' expected";
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', 'var', 'function' or '}' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -676,6 +747,8 @@ void FieldDeclaration() :
   } catch (ParseException e) {
     errorMessage = "';' expected after variable declaration";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -683,8 +756,8 @@ void FieldDeclaration() :
 PHPVarDeclaration VariableDeclarator() :
 {
   final String varName;
-  String varValue = null;
-  final int pos = jj_input_stream.bufpos;
+  final String varValue;
+  final int pos = jj_input_stream.getPosition();
 }
 {
   varName = VariableDeclaratorId()
@@ -696,6 +769,8 @@ PHPVarDeclaration VariableDeclarator() :
     } catch (ParseException e) {
       errorMessage = "Literal expression expected in variable initializer";
       errorLevel   = ERROR;
+      errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+      errorEnd   = jj_input_stream.getPosition() + 1;
       throw e;
     }
   ]
@@ -718,6 +793,8 @@ String VariableDeclaratorId() :
   } catch (ParseException e) {
     errorMessage = "'$' expected for variable identifier";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -760,13 +837,16 @@ final Token token;
   <DOLLAR> expr = VariableName()
   {return "$" + expr;}
 |
+  token = <DOLLAR_ID>
+  {return token.image + expr;}
+/*|      pas besoin ?
   token = <DOLLAR_ID> [expr = VariableName()]
   {
   if (expr == null) {
     return token.image;
   }
   return token.image + expr;
-  }
+  }*/
 }
 
 String VariableInitializer() :
@@ -827,7 +907,19 @@ void MethodDeclaration() :
   final PHPFunctionDeclaration functionDeclaration;
 }
 {
-  <FUNCTION> functionDeclaration = MethodDeclarator()
+  <FUNCTION>
+  try {
+    functionDeclaration = MethodDeclarator()
+  } catch (ParseException e) {
+    if (errorMessage != null) {
+      throw e;
+    }
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
   {
     if (currentSegment != null) {
       currentSegment.add(functionDeclaration);
@@ -842,12 +934,16 @@ void MethodDeclaration() :
   }
 }
 
+/**
+ * A MethodDeclarator contains [&] IDENTIFIER(parameters ...).
+ * @return a function description for the outline
+ */
 PHPFunctionDeclaration MethodDeclarator() :
 {
   final Token identifier;
   final StringBuffer methodDeclaration = new StringBuffer();
   final String formalParameters;
-  final int pos = jj_input_stream.bufpos;
+  final int pos = jj_input_stream.getPosition();
 }
 {
   [ <BIT_AND> {methodDeclaration.append("&");} ]
@@ -871,7 +967,9 @@ String FormalParameters() :
   } catch (ParseException e) {
     errorMessage = "Formal parameter expected after function identifier";
     errorLevel   = ERROR;
-    jj_consume_token(token.kind);
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
   }
             [ expr = FormalParameter()
               {buff.append(expr);}
@@ -885,6 +983,8 @@ String FormalParameters() :
   } catch (ParseException e) {
     errorMessage = "')' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
  {
@@ -947,6 +1047,9 @@ String Expression() :
   expr = PrintExpression()
   {return expr;}
 |
+  expr = ListExpression()
+  {return expr;}
+|
   expr = ConditionalExpression()
   [
     assignOperator = AssignmentOperator()
@@ -956,6 +1059,8 @@ String Expression() :
     } catch (ParseException e) {
       errorMessage = "expression expected";
       errorLevel   = ERROR;
+      errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+      errorEnd   = jj_input_stream.getPosition() + 1;
       throw e;
     }
   ]
@@ -1137,7 +1242,15 @@ String EqualityExpression() :
     | operator = <BANGDOUBLEEQUAL>
     | operator = <TRIPLEEQUAL>
   )
-  expr = RelationalExpression()
+  try {
+    expr = RelationalExpression()
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected after '"+operator.image+"'";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
   {
     buff.append(operator.image);
     buff.append(expr);
@@ -1206,7 +1319,15 @@ String MultiplicativeExpression() :
   Token operator;
   final StringBuffer buff = new StringBuffer();}
 {
-  expr = UnaryExpression()
+  try {
+    expr = UnaryExpression()
+  } catch (ParseException e) {
+    errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
   {buff.append(expr);}
   (
   ( operator = <STAR> | operator = <SLASH> | operator = <REM> ) expr = UnaryExpression()
@@ -1298,7 +1419,16 @@ String UnaryExpressionNotPlusMinus() :
   expr = Literal()
   {return expr;}
 |
-  <LPAREN> expr = Expression()<RPAREN>
+  <LPAREN> expr = Expression()
+  try {
+    <RPAREN>
+  } catch (ParseException e) {
+    errorMessage = "')' expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
   {return "("+expr+")";}
 }
 
@@ -1372,11 +1502,26 @@ String PrimaryPrefix() :
   {
     return "new " + expr;
   }
-|  
+|
   expr = VariableDeclaratorId()
   {return expr;}
 }
 
+String classInstantiation() :
+{
+  String expr;
+  final StringBuffer buff = new StringBuffer("new ");
+}
+{
+  <NEW> expr = ClassIdentifier()
+  {buff.append(expr);}
+  [
+    expr = PrimaryExpression()
+    {buff.append(expr);}
+  ]
+  {return buff.toString();}
+}
+
 String ClassIdentifier():
 {
   final String expr;
@@ -1407,7 +1552,16 @@ String VariableSuffix() :
   String expr = null;
 }
 {
-  <CLASSACCESS> expr = VariableName()
+  <CLASSACCESS>
+  try {
+    expr = VariableName()
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
   {return "->" + expr;}
 | 
   <LBRACKET> [ expr = Expression() ]
@@ -1416,6 +1570,8 @@ String VariableSuffix() :
   } catch (ParseException e) {
     errorMessage = "']' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   {
@@ -1474,8 +1630,10 @@ String expr = null;
   try {
     <RPAREN>
   } catch (ParseException e) {
-    errorMessage = "')' expected to close the argument list";
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   {
@@ -1500,6 +1658,8 @@ final StringBuffer buff = new StringBuffer();
       } catch (ParseException e) {
         errorMessage = "expression expected after a comma in argument list";
         errorLevel   = ERROR;
+        errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+        errorEnd   = jj_input_stream.getPosition() + 1;
         throw e;
       }
     {
@@ -1509,20 +1669,21 @@ final StringBuffer buff = new StringBuffer();
    {return buff.toString();}
 }
 
-/*
- * Statement syntax follows.
+/**
+ * A Statement without break
  */
-
-void Statement() :
+void StatementNoBreak() :
 {}
 {
   LOOKAHEAD(2)
   Expression()
   try {
-    (<SEMICOLON> | <PHPEND>)
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 |
@@ -1539,6 +1700,8 @@ void Statement() :
   } catch (ParseException e) {
     errorMessage = "';' expected after expression";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 |
@@ -1554,8 +1717,6 @@ void Statement() :
 |
   ForeachStatement()
 |
-  BreakStatement()
-|
   ContinueStatement()
 |
   ReturnStatement()
@@ -1569,10 +1730,21 @@ void Statement() :
   GlobalStatement()
 }
 
+/**
+ * A Normal statement
+ */
+void Statement() :
+{}
+{
+  StatementNoBreak()
+|
+  BreakStatement()
+}
+
 void IncludeStatement() :
 {
   final String expr;
-  final int pos = jj_input_stream.bufpos;
+  final int pos = jj_input_stream.getPosition();
 }
 {
   <REQUIRE>
@@ -1583,10 +1755,12 @@ void IncludeStatement() :
     }
   }
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 |
@@ -1598,10 +1772,12 @@ void IncludeStatement() :
     }
   }
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 |
@@ -1613,10 +1789,12 @@ void IncludeStatement() :
     }
   }
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 |
@@ -1628,10 +1806,12 @@ void IncludeStatement() :
     }
   }
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -1649,15 +1829,64 @@ String PrintExpression() :
   }
 }
 
+String ListExpression() :
+{
+  final StringBuffer buff = new StringBuffer("list(");
+  String expr;
+}
+{
+  <LIST>
+  try {
+    <LPAREN>
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  [
+    expr = VariableDeclaratorId()
+    {buff.append(expr);}
+  ]
+  [
+    try {
+      <COMMA>
+    } catch (ParseException e) {
+      errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
+      errorLevel   = ERROR;
+      errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+      errorEnd   = jj_input_stream.getPosition() + 1;
+      throw e;
+    }
+    expr = VariableDeclaratorId()
+    {buff.append(",").append(expr);}
+  ]
+  {buff.append(")");}
+  try {
+    <RPAREN>
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  [ <ASSIGN> expr = Expression() {buff.append("(").append(expr);}]
+  {return buff.toString();}
+}
+
 void EchoStatement() :
 {}
 {
   <ECHO> Expression() (<COMMA> Expression())*
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected after 'echo' statement";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -1667,10 +1896,12 @@ void GlobalStatement() :
 {
   <GLOBAL> VariableDeclaratorId() (<COMMA> VariableDeclaratorId())*
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -1680,10 +1911,12 @@ void StaticStatement() :
 {
   <STATIC> VariableDeclarator() (<COMMA> VariableDeclarator())*
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -1702,10 +1935,20 @@ void Block() :
   } catch (ParseException e) {
     errorMessage = "'{' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   ( BlockStatement() )*
-  <RBRACE>
+  try {
+    <RBRACE>
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
 }
 
 void BlockStatement() :
@@ -1718,6 +1961,19 @@ void BlockStatement() :
   MethodDeclaration()
 }
 
+/**
+ * A Block statement that will not contain any 'break'
+ */
+void BlockStatementNoBreak() :
+{}
+{
+  StatementNoBreak()
+|
+  ClassDeclaration()
+|
+  MethodDeclaration()
+}
+
 void LocalVariableDeclaration() :
 {}
 {
@@ -1754,25 +2010,131 @@ void StatementExpression() :
 }
 
 void SwitchStatement() :
-{}
 {
-  <SWITCH> <LPAREN> Expression() <RPAREN> <LBRACE>
-    ( SwitchLabel() ( BlockStatement() )* )*
-  <RBRACE>
+  Token breakToken = null;
+  int line;
+}
+{
+  <SWITCH>
+  try {
+    <LPAREN>
+  } catch (ParseException e) {
+    errorMessage = "'(' expected after 'switch'";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  Expression()
+  try {
+    <RPAREN>
+  } catch (ParseException e) {
+    errorMessage = "')' expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  try {
+  <LBRACE>
+  } catch (ParseException e) {
+    errorMessage = "'{' expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+    (
+      line = SwitchLabel()
+      ( BlockStatementNoBreak() )*
+      [ breakToken = BreakStatement() ]
+      {
+        try {
+          if (breakToken == null) {
+            setMarker(fileToParse,
+                      "You should use put a 'break' at the end of your statement",
+                      line,
+                      INFO,
+                      "Line " + line);
+          }
+        } catch (CoreException e) {
+          PHPeclipsePlugin.log(e);
+        }
+      }
+    )*
+  try {
+    <RBRACE>
+  } catch (ParseException e) {
+    errorMessage = "'}' expected";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+}
+
+Token BreakStatement() :
+{
+  final Token token;
+}
+{
+  token = <BREAK> [ Expression() ]
+  try {
+    <SEMICOLON>
+  } catch (ParseException e) {
+    errorMessage = "';' expected after 'break' keyword";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  {return token;}
 }
 
-void SwitchLabel() :
-{}
+int SwitchLabel() :
+{
+  final Token token;
+}
 {
-  <CASE> Expression() <COLON>
+  token = <CASE>
+  try {
+    Expression()
+  } catch (ParseException e) {
+    if (errorMessage != null) throw e;
+    errorMessage = "expression expected after 'case' keyword";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  try {
+    <COLON>
+  } catch (ParseException e) {
+    errorMessage = "':' expected after case expression";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  {return token.beginLine;}
 |
-  <_DEFAULT> <COLON>
+  token = <_DEFAULT>
+  try {
+    <COLON>
+  } catch (ParseException e) {
+    errorMessage = "':' expected after 'default' keyword";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
+  {return token.beginLine;}
 }
 
 void IfStatement() :
 {
   final Token token;
-  final int pos = jj_input_stream.bufpos;
+  final int pos = jj_input_stream.getPosition();
 }
 {
   token = <IF> Condition("if") IfStatement0(pos,pos+token.image.length())
@@ -1786,7 +2148,9 @@ void Condition(final String keyword) :
   } catch (ParseException e) {
     errorMessage = "'(' expected after " + keyword + " keyword";
     errorLevel   = ERROR;
-    throw e;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length();
+    errorEnd   = errorStart +1;
+    processParseException(e);
   }
   Expression()
   try {
@@ -1794,13 +2158,14 @@ void Condition(final String keyword) :
   } catch (ParseException e) {
     errorMessage = "')' expected after " + keyword + " keyword";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
 
 void IfStatement0(final int start,final int end) :
-{
-}
+{}
 {
   <COLON> (Statement())* (ElseIfStatementColon())* [ElseStatementColon()]
 
@@ -1819,13 +2184,17 @@ void IfStatement0(final int start,final int end) :
   } catch (ParseException e) {
     errorMessage = "'endif' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   try {
     <SEMICOLON>
   } catch (ParseException e) {
-    errorMessage = "';' expected 'endif' keyword";
+    errorMessage = "';' expected after 'endif' keyword";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 |
@@ -1853,7 +2222,7 @@ void ElseIfStatement() :
 void WhileStatement() :
 {
   final Token token;
-  final int pos = jj_input_stream.bufpos;
+  final int pos = jj_input_stream.getPosition();
 }
 {
   token = <WHILE> Condition("while") WhileStatement0(pos,pos + token.image.length())
@@ -1878,13 +2247,17 @@ void WhileStatement0(final int start, final int end) :
   } catch (ParseException e) {
     errorMessage = "'endwhile' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected after 'endwhile' keyword";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 |
@@ -1896,10 +2269,12 @@ void DoStatement() :
 {
   <DO> Statement() <WHILE> Condition("while")
   try {
-    (<SEMICOLON> | "?>")
+    (<SEMICOLON> | <PHPEND> {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);})
   } catch (ParseException e) {
     errorMessage = "';' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -1913,6 +2288,8 @@ void ForeachStatement() :
   } catch (ParseException e) {
     errorMessage = "'(' expected after 'foreach' keyword";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   try {
@@ -1920,6 +2297,8 @@ void ForeachStatement() :
   } catch (ParseException e) {
     errorMessage = "variable expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   [ VariableSuffix() ]
@@ -1928,6 +2307,8 @@ void ForeachStatement() :
   } catch (ParseException e) {
     errorMessage = "'as' expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   try {
@@ -1935,6 +2316,8 @@ void ForeachStatement() :
   } catch (ParseException e) {
     errorMessage = "variable expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   [ <ARRAYASSIGN> Expression() ]
@@ -1943,6 +2326,8 @@ void ForeachStatement() :
   } catch (ParseException e) {
     errorMessage = "')' expected after 'foreach' keyword";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
   try {
@@ -1951,6 +2336,8 @@ void ForeachStatement() :
     if (errorMessage != null) throw e;
     errorMessage = "statement expected";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
 }
@@ -1958,7 +2345,7 @@ void ForeachStatement() :
 void ForStatement() :
 {
 final Token token;
-final int pos = jj_input_stream.bufpos;
+final int pos = jj_input_stream.getPosition();
 }
 {
   token = <FOR>
@@ -1967,9 +2354,11 @@ final int pos = jj_input_stream.bufpos;
   } catch (ParseException e) {
     errorMessage = "'(' expected after 'for' keyword";
     errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
-     [ ForInit() ] <SEMICOLON> [ Expression() ] <SEMICOLON> [ ForUpdate() ] <RPAREN>
+     [ ForInit() ] <SEMICOLON> [ Expression() ] <SEMICOLON> [ StatementExpressionList() ] <RPAREN>
     (
       Statement()
     |
@@ -1991,13 +2380,17 @@ final int pos = jj_input_stream.bufpos;
       } catch (ParseException e) {
         errorMessage = "'endfor' expected";
         errorLevel   = ERROR;
+        errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+        errorEnd   = jj_input_stream.getPosition() + 1;
         throw e;
       }
       try {
         <SEMICOLON>
       } catch (ParseException e) {
-        errorMessage = "';' expected 'endfor' keyword";
+        errorMessage = "';' expected after 'endfor' keyword";
         errorLevel   = ERROR;
+        errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+        errorEnd   = jj_input_stream.getPosition() + 1;
         throw e;
       }
     )
@@ -2018,26 +2411,32 @@ void StatementExpressionList() :
   StatementExpression() ( <COMMA> StatementExpression() )*
 }
 
-void ForUpdate() :
-{}
-{
-  StatementExpressionList()
-}
-
-void BreakStatement() :
-{}
-{
-  <BREAK> [ <IDENTIFIER> ] <SEMICOLON>
-}
-
 void ContinueStatement() :
 {}
 {
-  <CONTINUE> [ <IDENTIFIER> ] <SEMICOLON>
+  <CONTINUE> [ <IDENTIFIER> ]
+  try {
+    <SEMICOLON>
+  } catch (ParseException e) {
+    errorMessage = "';' expected after 'continue' statement";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
 }
 
 void ReturnStatement() :
 {}
 {
-  <RETURN> [ Expression() ] <SEMICOLON>
+  <RETURN> [ Expression() ]
+  try {
+    <SEMICOLON>
+  } catch (ParseException e) {
+    errorMessage = "';' expected after 'return' statement";
+    errorLevel   = ERROR;
+    errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd   = jj_input_stream.getPosition() + 1;
+    throw e;
+  }
 }
\ No newline at end of file