Fixed a bug in the String regex
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParser.jj
index 00cc6c9..8ee1085 100644 (file)
@@ -38,6 +38,7 @@ import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpdt.internal.compiler.ast.*;
 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
+import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
 
 /**
@@ -46,6 +47,7 @@ import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
  * given with JavaCC. You can get JavaCC at http://www.webgain.com
  * You can test the parser with the PHPParserTestCase2.java
  * @author Matthieu Casanova
+ * @version $Reference: 1.0$
  */
 public final class PHPParser extends PHPParserSuperclass {
 
@@ -107,12 +109,12 @@ public final class PHPParser extends PHPParserSuperclass {
    * Add an php node on the stack.
    * @param node the node that will be added to the stack
    */
-  private static final void pushOnAstNodes(AstNode node) {
+  private static final void pushOnAstNodes(final AstNode node) {
     try {
       nodes[++nodePtr] = node;
     } catch (IndexOutOfBoundsException e) {
-      int oldStackLength = nodes.length;
-      AstNode[] oldStack = nodes;
+      final int oldStackLength = nodes.length;
+      final AstNode[] oldStack = nodes;
       nodes = new AstNode[oldStackLength + AstStackIncrement];
       System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
       nodePtr = oldStackLength;
@@ -269,7 +271,7 @@ public final class PHPParser extends PHPParserSuperclass {
    */
   public static final void createNewHTMLCode() {
     final int currentPosition = SimpleCharStream.getPosition();
-    if (currentPosition == htmlStart) {
+    if (currentPosition == htmlStart || currentPosition > SimpleCharStream.currentBuffer.length()) {
       return;
     }
     final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
@@ -391,6 +393,7 @@ MORE :
 | <INCLUDE_ONCE       : "include_once">
 | <REQUIRE_ONCE       : "require_once">
 | <GLOBAL             : "global">
+| <DEFINE             : "define">
 | <STATIC             : "static">
 | <CLASSACCESS        : "->">
 | <STATICCLASSACCESS  : "::">
@@ -498,32 +501,9 @@ MORE :
   <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
 |
   <STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
-|    <STRING_1:
-      "\""
-      (
-          ~["\""]
-        | "\\\""
-        | "\\"
-      )*
-      "\""
-    >
-|    <STRING_2:
-      "'"
-      (
-         ~["'"]
-       | "\\'"
-      )*
-
-      "'"
-    >
-|   <STRING_3:
-      "`"
-      (
-        ~["`"]
-      | "\\`"
-      )*
-      "`"
-    >
+|   <STRING_1: "\"" ( ~["\"","\\"] | "\\" ( ["\"","\\"] ) )* "\"">
+|   <STRING_2: "'" ( ~["'","\\"] | "\\" ( ["'","\\"] ) )* "'">
+|   <STRING_3: "`" ( ~["`","\\"] | "\\" ( ["`","\\"] ) )* "`">
 }
 
 /* IDENTIFIERS */
@@ -657,7 +637,7 @@ PHPEchoBlock phpEchoBlock() :
 {
   final Expression expr;
   final int pos = SimpleCharStream.getPosition();
-  PHPEchoBlock echoBlock;
+  final PHPEchoBlock echoBlock;
 }
 {
   <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] <PHPEND>
@@ -676,8 +656,7 @@ void Php() :
 ClassDeclaration ClassDeclaration() :
 {
   final ClassDeclaration classDeclaration;
-  final Token className;
-  Token superclassName = null;
+  final Token className,superclassName;
   final int pos;
   char[] classNameImage = SYNTAX_ERROR_CHAR;
   char[] superclassNameImage = null;
@@ -732,7 +711,7 @@ ClassDeclaration ClassDeclaration() :
    return classDeclaration;}
 }
 
-void ClassBody(ClassDeclaration classDeclaration) :
+void ClassBody(final ClassDeclaration classDeclaration) :
 {}
 {
   try {
@@ -759,10 +738,10 @@ void ClassBody(ClassDeclaration classDeclaration) :
 /**
  * A class can contain only methods and fields.
  */
-void ClassBodyDeclaration(ClassDeclaration classDeclaration) :
+void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
 {
-  MethodDeclaration method;
-  FieldDeclaration field;
+  final MethodDeclaration method;
+  final FieldDeclaration field;
 }
 {
   method = MethodDeclaration() {classDeclaration.addMethod(method);}
@@ -775,7 +754,7 @@ void ClassBodyDeclaration(ClassDeclaration classDeclaration) :
 FieldDeclaration FieldDeclaration() :
 {
   VariableDeclaration variableDeclaration;
-  VariableDeclaration[] list;
+  final VariableDeclaration[] list;
   final ArrayList arrayList = new ArrayList();
   final int pos = SimpleCharStream.getPosition();
 }
@@ -845,9 +824,8 @@ VariableDeclaration VariableDeclarator() :
  */
 String VariableDeclaratorId() :
 {
-  String expr;
+  final String expr;
   Expression expression = null;
-  final StringBuffer buff = new StringBuffer();
   final int pos = SimpleCharStream.getPosition();
   ConstantIdentifier ex;
 }
@@ -910,7 +888,7 @@ String Variable():
 String VariableName():
 {
   final StringBuffer buff;
-  String expr = null;
+  final String expr;
   Expression expression = null;
   final Token token;
 }
@@ -976,7 +954,7 @@ Expression VariableInitializer() :
 
 ArrayVariableDeclaration ArrayVariable() :
 {
-Expression expr,expr2;
+final Expression expr,expr2;
 }
 {
   expr = Expression()
@@ -1001,7 +979,7 @@ ArrayVariableDeclaration[] ArrayInitializer() :
            [<COMMA> {list.add(null);}]
   <RPAREN>
   {
-  ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
+  final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
   list.toArray(vars);
   return vars;}
 }
@@ -1152,6 +1130,16 @@ ConstantIdentifier Type() :
 Expression Expression() :
 {
   final Expression expr;
+  final int pos = SimpleCharStream.getPosition();
+}
+{
+  <BANG> expr = Expression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
+| expr = ExpressionNoBang() {return expr;}
+}
+
+Expression ExpressionNoBang() :
+{
+  final Expression expr;
 }
 {
   expr = PrintExpression()       {return expr;}
@@ -1167,7 +1155,7 @@ Expression Expression() :
  */
 VarAssignation varAssignation() :
 {
-  String varName;
+  final String varName;
   final Expression initializer;
   final int assignOperator;
   final int pos = SimpleCharStream.getPosition();
@@ -1428,7 +1416,7 @@ Expression MultiplicativeExpression() :
  */
 Expression UnaryExpression() :
 {
-  Expression expr;
+  final Expression expr;
   final int pos = SimpleCharStream.getPosition();
 }
 {
@@ -1440,7 +1428,7 @@ Expression UnaryExpression() :
 
 Expression AtUnaryExpression() :
 {
-  Expression expr;
+  final Expression expr;
   final int pos = SimpleCharStream.getPosition();
 }
 {
@@ -1455,8 +1443,8 @@ Expression AtUnaryExpression() :
 
 Expression UnaryExpressionNoPrefix() :
 {
-  Expression expr;
-  int operator;
+  final Expression expr;
+  final int operator;
   final int pos = SimpleCharStream.getPosition();
 }
 {
@@ -1488,12 +1476,11 @@ final int operator;
 
 Expression UnaryExpressionNotPlusMinus() :
 {
-  Expression expr;
-  final int pos = SimpleCharStream.getPosition();
+  final Expression expr;
 }
 {
-  <BANG> expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
-| LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
+//  <BANG> expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
+  LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
   expr = CastExpression()         {return expr;}
 | expr = PostfixExpression()      {return expr;}
 | expr = Literal()                {return expr;}
@@ -1526,7 +1513,7 @@ final int pos = SimpleCharStream.getPosition();
 
 Expression PostfixExpression() :
 {
-  Expression expr;
+  final Expression expr;
   int operator = -1;
   final int pos = SimpleCharStream.getPosition();
 }
@@ -1556,11 +1543,15 @@ Expression PrimaryExpression() :
                                                  SimpleCharStream.getPosition()),
                           expr,
                           ClassAccess.STATIC);}
-  (expr = PrimarySuffix(expr))*
+  (
+  LOOKAHEAD(PrimarySuffix())
+  expr = PrimarySuffix(expr))*
   {return expr;}
 |
   expr = PrimaryPrefix()
-  (expr = PrimarySuffix(expr))*
+  (
+  LOOKAHEAD(PrimarySuffix())
+  expr = PrimarySuffix(expr))*
   {return expr;}
 |
   expr = ArrayDeclarator()
@@ -1638,7 +1629,7 @@ ConstantIdentifier ClassIdentifier():
                                                                SimpleCharStream.getPosition());}
 }
 
-AbstractSuffixExpression PrimarySuffix(Expression prefix) :
+AbstractSuffixExpression PrimarySuffix(final Expression prefix) :
 {
   final AbstractSuffixExpression expr;
 }
@@ -1647,7 +1638,7 @@ AbstractSuffixExpression PrimarySuffix(Expression prefix) :
 | expr = VariableSuffix(prefix) {return expr;}
 }
 
-AbstractSuffixExpression VariableSuffix(Expression prefix) :
+AbstractSuffixExpression VariableSuffix(final Expression prefix) :
 {
   String expr = null;
   final int pos = SimpleCharStream.getPosition();
@@ -1701,7 +1692,7 @@ Literal Literal() :
                                     return new NullLiteral(pos-4,pos);}
 }
 
-FunctionCall Arguments(Expression func) :
+FunctionCall Arguments(final Expression func) :
 {
 Expression[] args = null;
 }
@@ -1745,7 +1736,7 @@ final ArrayList list = new ArrayList();
       }
    )*
    {
-   Expression[] arguments = new Expression[list.size()];
+   final Expression[] arguments = new Expression[list.size()];
    list.toArray(arguments);
    return arguments;}
 }
@@ -1804,6 +1795,69 @@ Statement StatementNoBreak() :
   return statement;}
 | statement = StaticStatement()         {return statement;}
 | statement = GlobalStatement()         {return statement;}
+| statement = defineStatement()         {currentSegment.add((Outlineable)statement);return statement;}
+}
+
+Define defineStatement() :
+{
+  final int start = SimpleCharStream.getPosition();
+  Expression defineName,defineValue;
+}
+{
+  <DEFINE>
+  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;
+    processParseException(e);
+  }
+  try {
+    defineName = Expression()
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
+    errorLevel   = ERROR;
+    errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd     = SimpleCharStream.getPosition() + 1;
+    throw e;
+  }
+  try {
+    <COMMA>
+  } 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;
+    processParseException(e);
+  }
+  try {
+    (   defineValue = PrintExpression()
+      | LOOKAHEAD(varAssignation())
+        defineValue = varAssignation()
+      | defineValue = ConditionalExpression())
+  } catch (ParseException e) {
+    errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
+    errorLevel   = ERROR;
+    errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
+    errorEnd     = SimpleCharStream.getPosition() + 1;
+    throw e;
+  }
+  try {
+    <RPAREN>
+  } 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;
+    processParseException(e);
+  }
+  {return new Define(currentSegment,
+                     defineName,
+                     defineValue,
+                     start,
+                     SimpleCharStream.getPosition());}
 }
 
 /**
@@ -1824,8 +1878,8 @@ Statement Statement() :
 HTMLBlock htmlBlock() :
 {
   final int startIndex = nodePtr;
-  AstNode[] blockNodes;
-  int nbNodes;
+  final AstNode[] blockNodes;
+  final int nbNodes;
 }
 {
   <PHPEND> (phpEchoBlock())*
@@ -1903,8 +1957,8 @@ PrintExpression PrintExpression() :
 ListExpression ListExpression() :
 {
   String expr = null;
-  Expression expression = null;
-  ArrayList list = new ArrayList();
+  final Expression expression;
+  final ArrayList list = new ArrayList();
   final int pos = SimpleCharStream.getPosition();
 }
 {
@@ -1933,8 +1987,7 @@ ListExpression ListExpression() :
       errorEnd     = SimpleCharStream.getPosition() + 1;
       throw e;
     }
-    expr = VariableDeclaratorId()
-    {list.add(expr);}
+    [expr = VariableDeclaratorId() {list.add(expr);}]
   )*
   try {
     <RPAREN>
@@ -1947,7 +2000,7 @@ ListExpression ListExpression() :
   }
   [ <ASSIGN> expression = Expression()
     {
-    String[] strings = new String[list.size()];
+    final String[] strings = new String[list.size()];
     list.toArray(strings);
     return new ListExpression(strings,
                               expression,
@@ -1955,7 +2008,7 @@ ListExpression ListExpression() :
                               SimpleCharStream.getPosition());}
   ]
   {
-    String[] strings = new String[list.size()];
+    final String[] strings = new String[list.size()];
     list.toArray(strings);
     return new ListExpression(strings,pos,SimpleCharStream.getPosition());}
 }
@@ -1988,7 +2041,7 @@ EchoStatement EchoStatement() :
       throw e;
     }
   }
-  {Expression[] exprs = new Expression[expressions.size()];
+  {final Expression[] exprs = new Expression[expressions.size()];
    expressions.toArray(exprs);
    return new EchoStatement(exprs,pos);}
 }
@@ -1997,8 +2050,8 @@ GlobalStatement GlobalStatement() :
 {
    final int pos = SimpleCharStream.getPosition();
    String expr;
-   ArrayList vars = new ArrayList();
-   GlobalStatement global;
+   final ArrayList vars = new ArrayList();
+   final GlobalStatement global;
 }
 {
   <GLOBAL>
@@ -2011,7 +2064,7 @@ GlobalStatement GlobalStatement() :
   try {
     <SEMICOLON>
     {
-    String[] strings = new String[vars.size()];
+    final String[] strings = new String[vars.size()];
     vars.toArray(strings);
     global = new GlobalStatement(currentSegment,
                                  strings,
@@ -2040,7 +2093,7 @@ StaticStatement StaticStatement() :
   try {
     <SEMICOLON>
     {
-    String[] strings = new String[vars.size()];
+    final String[] strings = new String[vars.size()];
     vars.toArray(strings);
     return new StaticStatement(strings,
                                 pos,
@@ -2100,7 +2153,7 @@ Block Block() :
     throw e;
   }
   {
-  Statement[] statements = new Statement[list.size()];
+  final Statement[] statements = new Statement[list.size()];
   list.toArray(statements);
   return new Block(statements,pos,SimpleCharStream.getPosition());}
 }
@@ -2151,7 +2204,7 @@ VariableDeclaration[] LocalVariableDeclaration() :
   {list.add(var);}
   ( <COMMA> var = LocalVariableDeclarator() {list.add(var);})*
   {
-    VariableDeclaration[] vars = new VariableDeclaration[list.size()];
+    final VariableDeclaration[] vars = new VariableDeclaration[list.size()];
     list.toArray(vars);
   return vars;}
 }
@@ -2190,8 +2243,8 @@ EmptyStatement EmptyStatement() :
 
 Expression StatementExpression() :
 {
-  Expression expr,expr2;
-  int operator;
+  final Expression expr,expr2;
+  final int operator;
 }
 {
   expr = PreIncDecExpression() {return expr;}
@@ -2262,7 +2315,7 @@ AbstractCase[] switchStatementBrace() :
   try {
     <RBRACE>
     {
-    AbstractCase[] abcase = new AbstractCase[cases.size()];
+    final AbstractCase[] abcase = new AbstractCase[cases.size()];
     cases.toArray(abcase);
     return abcase;}
   } catch (ParseException e) {
@@ -2308,7 +2361,7 @@ AbstractCase[] switchStatementColon(final int start, final int end) :
   try {
     <SEMICOLON>
     {
-    AbstractCase[] abcase = new AbstractCase[cases.size()];
+    final AbstractCase[] abcase = new AbstractCase[cases.size()];
     cases.toArray(abcase);
     return abcase;}
   } catch (ParseException e) {
@@ -2333,7 +2386,7 @@ AbstractCase switchLabel0() :
   | statement = htmlBlock()             {stmts.add(statement);})*
   [ statement = BreakStatement()        {stmts.add(statement);}]
   {
-  Statement[] stmtsArray = new Statement[stmts.size()];
+  final Statement[] stmtsArray = new Statement[stmts.size()];
   stmts.toArray(stmtsArray);
   if (expr == null) {//it's a default
     return new DefaultCase(stmtsArray,pos,SimpleCharStream.getPosition());
@@ -2409,8 +2462,8 @@ Break BreakStatement() :
 IfStatement IfStatement() :
 {
   final int pos = SimpleCharStream.getPosition();
-  Expression condition;
-  IfStatement ifStatement;
+  final Expression condition;
+  final IfStatement ifStatement;
 }
 {
   <IF> condition = Condition("if") ifStatement = IfStatement0(condition, pos,pos+2)
@@ -2445,18 +2498,18 @@ Expression Condition(final String keyword) :
   {return condition;}
 }
 
-IfStatement IfStatement0(Expression condition, final int start,final int end) :
+IfStatement IfStatement0(final Expression condition, final int start,final int end) :
 {
   Statement statement;
-  Statement stmt;
+  final Statement stmt;
   final Statement[] statementsArray;
   ElseIf elseifStatement;
   Else elseStatement = null;
-  ArrayList stmts;
+  final ArrayList stmts;
   final ArrayList elseIfList = new ArrayList();
-  ElseIf[] elseIfs;
+  final ElseIf[] elseIfs;
   int pos = SimpleCharStream.getPosition();
-  int endStatements;
+  final int endStatements;
 }
 {
   <COLON>
@@ -2550,7 +2603,7 @@ IfStatement IfStatement0(Expression condition, final int start,final int end) :
 
 ElseIf ElseIfStatementColon() :
 {
-  Expression condition;
+  final Expression condition;
   Statement statement;
   final ArrayList list = new ArrayList();
   final int pos = SimpleCharStream.getPosition();
@@ -2560,7 +2613,7 @@ ElseIf ElseIfStatementColon() :
   <COLON> (  statement = Statement() {list.add(statement);}
            | statement = htmlBlock() {list.add(statement);})*
   {
-  Statement[] stmtsArray = new Statement[list.size()];
+  final Statement[] stmtsArray = new Statement[list.size()];
   list.toArray(stmtsArray);
   return new ElseIf(condition,stmtsArray ,pos,SimpleCharStream.getPosition());}
 }
@@ -2575,22 +2628,22 @@ Else ElseStatementColon() :
   <ELSE> <COLON> (  statement = Statement() {list.add(statement);}
                   | statement = htmlBlock() {list.add(statement);})*
   {
-  Statement[] stmtsArray = new Statement[list.size()];
+  final Statement[] stmtsArray = new Statement[list.size()];
   list.toArray(stmtsArray);
   return new Else(stmtsArray,pos,SimpleCharStream.getPosition());}
 }
 
 ElseIf ElseIfStatement() :
 {
-  Expression condition;
-  Statement statement;
+  final Expression condition;
+  final Statement statement;
   final ArrayList list = new ArrayList();
   final int pos = SimpleCharStream.getPosition();
 }
 {
   <ELSEIF> condition = Condition("elseif") statement = Statement() {list.add(statement);/*todo:do better*/}
   {
-  Statement[] stmtsArray = new Statement[list.size()];
+  final Statement[] stmtsArray = new Statement[list.size()];
   list.toArray(stmtsArray);
   return new ElseIf(condition,stmtsArray,pos,SimpleCharStream.getPosition());}
 }
@@ -2638,7 +2691,7 @@ Statement WhileStatement0(final int start, final int end) :
   try {
     <SEMICOLON>
     {
-    Statement[] stmtsArray = new Statement[stmts.size()];
+    final Statement[] stmtsArray = new Statement[stmts.size()];
     stmts.toArray(stmtsArray);
     return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
   } catch (ParseException e) {
@@ -2802,7 +2855,7 @@ final int startBlock, endBlock;
       try {
         <SEMICOLON>
         {
-        Statement[] stmtsArray = new Statement[list.size()];
+        final Statement[] stmtsArray = new Statement[list.size()];
         list.toArray(stmtsArray);
         return new ForStatement(initializations,condition,increments,new Block(stmtsArray,startBlock,endBlock),pos,SimpleCharStream.getPosition());}
       } catch (ParseException e) {
@@ -2817,7 +2870,7 @@ final int startBlock, endBlock;
 
 Expression[] ForInit() :
 {
-  Expression[] exprs;
+  final Expression[] exprs;
 }
 {
   LOOKAHEAD(LocalVariableDeclaration())
@@ -2831,13 +2884,13 @@ Expression[] ForInit() :
 Expression[] StatementExpressionList() :
 {
   final ArrayList list = new ArrayList();
-  Expression expr;
+  final Expression expr;
 }
 {
   expr = StatementExpression()   {list.add(expr);}
   (<COMMA> StatementExpression() {list.add(expr);})*
   {
-  Expression[] exprsArray = new Expression[list.size()];
+  final Expression[] exprsArray = new Expression[list.size()];
   list.toArray(exprsArray);
   return exprsArray;}
 }