improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / internal / compiler / ast / SwitchStatement.java
index ddc5514..82dc2b3 100644 (file)
@@ -10,7 +10,7 @@
  *******************************************************************************/
 package net.sourceforge.phpeclipse.internal.compiler.ast;
 
-import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
+import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
@@ -20,12 +20,12 @@ import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
 
 public class SwitchStatement extends Statement {
-       public Expression testExpression;
+       public Expression expression;
        public Statement[] statements;
        public BlockScope scope;
        public int explicitDeclarations;
        public Label breakLabel;
-       public Case[] cases;
+       public CaseStatement[] cases;
        public DefaultCase defaultCase;
        public int caseCount = 0;
 
@@ -42,7 +42,7 @@ public class SwitchStatement extends Statement {
                BlockScope currentScope,
                FlowContext flowContext,
                FlowInfo flowInfo) {
-               flowInfo = testExpression.analyseCode(currentScope, flowContext, flowInfo);
+               flowInfo = expression.analyseCode(currentScope, flowContext, flowInfo);
                SwitchFlowContext switchContext =
                        new SwitchFlowContext(flowContext, this, (breakLabel = new Label()));
 
@@ -92,8 +92,8 @@ public class SwitchStatement extends Statement {
        /**
         * Switch code generation
         *
-        * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
-        * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
+        * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
+        * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
         */
 //     public void generateCode(BlockScope currentScope, CodeStream codeStream) {
 //             int[] sortedIndexes = new int[caseCount];
@@ -207,15 +207,32 @@ public class SwitchStatement extends Statement {
                }
        }
 
+       public StringBuffer printStatement(int indent, StringBuffer output) {
+
+               printIndent(indent, output).append("switch ("); //$NON-NLS-1$
+               expression.printExpression(0, output).append(") {"); //$NON-NLS-1$
+               if (statements != null) {
+                       for (int i = 0; i < statements.length; i++) {
+                               output.append('\n');
+                               if (statements[i] instanceof CaseStatement) {
+                                       statements[i].printStatement(indent, output);
+                               } else {
+                                       statements[i].printStatement(indent+2, output);
+                               }
+                       }
+               }
+               output.append("\n"); //$NON-NLS-1$
+               return printIndent(indent, output).append('}');
+       }
        public void resolve(BlockScope upperScope) {
 
-               TypeBinding testType = testExpression.resolveType(upperScope);
+               TypeBinding testType = expression.resolveType(upperScope);
                if (testType == null)
                        return;
-               testExpression.implicitWidening(testType, testType);
-               if (!(testExpression.isConstantValueOfTypeAssignableToType(testType, IntBinding))) {
+               expression.implicitWidening(testType, testType);
+               if (!(expression.isConstantValueOfTypeAssignableToType(testType, IntBinding))) {
                        if (!testType.isCompatibleWith(IntBinding)) {
-                               upperScope.problemReporter().incorrectSwitchType(testExpression, testType);
+                               upperScope.problemReporter().incorrectSwitchType(expression, testType);
                                return;
                        }
                }
@@ -223,7 +240,7 @@ public class SwitchStatement extends Statement {
                        scope = explicitDeclarations == 0 ? upperScope : new BlockScope(upperScope);
                        int length;
                        // collection of cases is too big but we will only iterate until caseCount
-                       cases = new Case[length = statements.length];
+                       cases = new CaseStatement[length = statements.length];
                        int[] casesValues = new int[length];
                        int counter = 0;
                        for (int i = 0; i < length; i++) {
@@ -234,7 +251,7 @@ public class SwitchStatement extends Statement {
                                                int key = cst.intValue();
                                                for (int j = 0; j < counter; j++) {
                                                        if (casesValues[j] == key) {
-                                                               scope.problemReporter().duplicateCase((Case) statements[i], cst); //TODO: (philippe) could improve diagnosis to indicate colliding case
+                                                               scope.problemReporter().duplicateCase((CaseStatement) statements[i], cst); //TODO: (philippe) could improve diagnosis to indicate colliding case
                                                        }
                                                }
                                                casesValues[counter++] = key;
@@ -247,7 +264,7 @@ public class SwitchStatement extends Statement {
 
                String inFront, s = tabString(tab);
                inFront = s;
-               s = s + "switch (" + testExpression.toStringExpression() + ") "; //$NON-NLS-1$ //$NON-NLS-2$
+               s = s + "switch (" + expression.toStringExpression() + ") "; //$NON-NLS-1$ //$NON-NLS-2$
                if (statements == null) {
                        s = s + "{}"; //$NON-NLS-1$
                        return s;
@@ -267,17 +284,17 @@ public class SwitchStatement extends Statement {
                                //use instanceof in order not to polluate classes with behavior only needed for printing purpose.
                                if (statements[i] instanceof Expression)
                                        s = s + "\n" + inFront + tabulation; //$NON-NLS-1$
-                               if (statements[i] instanceof Break)
+                               if (statements[i] instanceof BreakStatement)
                                        s = s + statements[i].toString(0);
                                else
                                        s = s + "\n" + statements[i].toString(tab + 2); //$NON-NLS-1$
                                //============= 
-                               if ((statements[i] instanceof Case)
+                               if ((statements[i] instanceof CaseStatement)
                                        || (statements[i] instanceof DefaultCase)) {
                                        i++;
-                                       while (!((statements[i] instanceof Case)
+                                       while (!((statements[i] instanceof CaseStatement)
                                                || (statements[i] instanceof DefaultCase))) {
-                                               if ((statements[i] instanceof Expression) || (statements[i] instanceof Break))
+                                               if ((statements[i] instanceof Expression) || (statements[i] instanceof BreakStatement))
                                                        s = s + statements[i].toString(0) + " ; "; //$NON-NLS-1$
                                                else
                                                        s = s + "\n" + statements[i].toString(tab + 6) + " ; "; //$NON-NLS-1$ //$NON-NLS-2$
@@ -295,11 +312,11 @@ public class SwitchStatement extends Statement {
        }
 
        public void traverse(
-               IAbstractSyntaxTreeVisitor visitor,
+               ASTVisitor visitor,
                BlockScope blockScope) {
 
                if (visitor.visit(this, blockScope)) {
-                       testExpression.traverse(visitor, scope);
+                       expression.traverse(visitor, scope);
                        if (statements != null) {
                                int statementsLength = statements.length;
                                for (int i = 0; i < statementsLength; i++)