First try, not finished
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParser.jj
index e69a928..e411485 100644 (file)
@@ -29,7 +29,6 @@ import org.eclipse.ui.texteditor.MarkerUtilities;
 import org.eclipse.jface.preference.IPreferenceStore;
 
 import java.util.Hashtable;
-import java.util.ArrayList;
 import java.util.Enumeration;
 import java.io.StringReader;
 import java.io.*;
@@ -38,6 +37,7 @@ import java.text.MessageFormat;
 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpdt.internal.compiler.parser.*;
+import net.sourceforge.phpdt.internal.compiler.ast.*;
 
 /**
  * A new php parser.
@@ -51,7 +51,7 @@ public final class PHPParser extends PHPParserSuperclass {
   /** The file that is parsed. */
   private static IFile fileToParse;
 
-  /** The current segment */
+  /** The current segment. */
   private static PHPSegmentWithChildren currentSegment;
 
   private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
@@ -59,6 +59,7 @@ public final class PHPParser extends PHPParserSuperclass {
   PHPOutlineInfo outlineInfo;
 
   private static PHPFunctionDeclaration currentFunction;
+  private static boolean assigning;
 
   /** The error level of the current ParseException. */
   private static int errorLevel = ERROR;
@@ -68,13 +69,20 @@ public final class PHPParser extends PHPParserSuperclass {
   private static int errorStart = -1;
   private static int errorEnd = -1;
 
-  public PHPParser() {
-  }
+       //ast stack
+       private final static int AstStackIncrement = 100;
+       /** The stack of node. */
+  private static AstNode[] astStack;
+  /** The cursor in expression stack. */
+       private static int expressionPtr;
 
   public final void setFileToParse(final IFile fileToParse) {
     this.fileToParse = fileToParse;
   }
 
+  public PHPParser() {
+  }
+
   public PHPParser(final IFile fileToParse) {
     this(new StringReader(""));
     this.fileToParse = fileToParse;
@@ -87,6 +95,7 @@ public final class PHPParser extends PHPParserSuperclass {
       jj_input_stream = new SimpleCharStream(stream, 1, 1);
     }
     ReInit(new StringReader(strEval));
+    astStack = new AstNode[AstStackIncrement];
     phpTest();
   }
 
@@ -97,6 +106,7 @@ public final class PHPParser extends PHPParserSuperclass {
         jj_input_stream = new SimpleCharStream(stream, 1, 1);
       }
       ReInit(stream);
+      astStack = new AstNode[AstStackIncrement];
       phpFile();
     } catch (FileNotFoundException e) {
       e.printStackTrace();  //To change body of catch statement use Options | File Templates.
@@ -109,6 +119,7 @@ public final class PHPParser extends PHPParserSuperclass {
       jj_input_stream = new SimpleCharStream(stream, 1, 1);
     }
     ReInit(stream);
+    astStack = new AstNode[AstStackIncrement];
     phpFile();
   }
 
@@ -120,6 +131,7 @@ public final class PHPParser extends PHPParserSuperclass {
       jj_input_stream = new SimpleCharStream(stream, 1, 1);
     }
     ReInit(stream);
+    astStack = new AstNode[AstStackIncrement];
     try {
       parse();
     } catch (ParseException e) {
@@ -244,6 +256,7 @@ public final class PHPParser extends PHPParserSuperclass {
       jj_input_stream = new SimpleCharStream(stream, 1, 1);
     }
     ReInit(stream);
+    astStack = new AstNode[AstStackIncrement];
     try {
       parse();
     } catch (ParseException e) {
@@ -273,7 +286,7 @@ public final class PHPParser extends PHPParserSuperclass {
     }
   }
 
-  public static final void parse() throws ParseException {
+  private static final void parse() throws ParseException {
          phpFile();
   }
 }
@@ -778,7 +791,7 @@ PHPVarDeclaration VariableDeclarator() :
     <ASSIGN>
     try {
       varValue = VariableInitializer()
-      {return new PHPVarDeclaration(currentSegment,varName.substring(1),pos,varValue);}
+      {return new PHPVarDeclaration(currentSegment,varName,pos,varValue);}
     } catch (ParseException e) {
       errorMessage = "Literal expression expected in variable initializer";
       errorLevel   = ERROR;
@@ -820,20 +833,20 @@ String Variable():
 {
   token = <DOLLAR_ID> [<LBRACE> expr = Expression() <RBRACE>]
   {
-    if (expr == null) {
+    if (expr == null && !assigning) {
       if (currentFunction != null) {
         PHPVarDeclaration var = currentFunction.getParameter(token.image.substring(1));
         if (var != null) {
           var.getVariable().setUsed(true);
         }
       }
-      return token.image;
+      return token.image.substring(1);
     }
     return token + "{" + expr + "}";
   }
 |
   <DOLLAR> expr = VariableName()
-  {return "$" + expr;}
+  {return expr;}
 }
 
 String VariableName():
@@ -1075,7 +1088,7 @@ PHPVarDeclaration FormalParameter() :
   [token = <BIT_AND>] variableDeclaration = VariableDeclarator()
   {
     if (token != null) {
-      variableDeclaration.getVariable().setPrefix("@");
+      variableDeclaration.getVariable().setReference(true);
     }
     return variableDeclaration;
   }
@@ -1125,12 +1138,29 @@ String Expression() :
   expr = ListExpression()
   {return expr;}
 |
+  LOOKAHEAD(varAssignation())
+  expr = varAssignation()
+  {return expr;}
+|
   expr = ConditionalExpression()
-  [
-    assignOperator = AssignmentOperator()
+  {return expr;}
+}
+
+/**
+ * A Variable assignation.
+ * varName (an assign operator) any expression
+ */
+String varAssignation() :
+{
+  String varName,assignOperator,expr2;
+  PHPVarDeclaration variable;
+  final int pos = SimpleCharStream.getPosition();
+}
+{
+  varName = VariableDeclaratorId()
+  assignOperator = AssignmentOperator()
     try {
       expr2 = Expression()
-      {return expr + assignOperator + expr2;}
     } catch (ParseException e) {
       if (errorMessage != null) {
         throw e;
@@ -1141,8 +1171,7 @@ String Expression() :
       errorEnd   = jj_input_stream.getPosition() + 1;
       throw e;
     }
-  ]
-  {return expr;}
+    {return varName + assignOperator + expr2;}
 }
 
 String AssignmentOperator() :
@@ -2064,7 +2093,18 @@ void SwitchStatement() :
     errorEnd   = jj_input_stream.getPosition() + 1;
     throw e;
   }
-  Expression()
+  try {
+    Expression()
+  } catch (ParseException e) {
+    if (errorMessage != null) {
+      throw 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;
+  }
   try {
     <RPAREN>
   } catch (ParseException e) {