/plugins/PHPParser/tags/v1_0_3/src/net/sourceforge/phpdt/internal/compiler/ast/ForeachStatement.java

#
Java | 109 lines | 64 code | 10 blank | 35 comment | 21 complexity | d3187383345e64ac2a1e16057e64c427 MD5 | raw file

✨ Summary
  1. package net.sourceforge.phpdt.internal.compiler.ast;
  2. import java.util.List;
  3. /**
  4. * A Foreach statement.
  5. *
  6. * @author Matthieu Casanova
  7. */
  8. public final class ForeachStatement extends Statement {
  9. private final Expression expression;
  10. private final Expression variable;
  11. private final Statement statement;
  12. /**
  13. * Create a new Foreach statement.
  14. *
  15. * @param expression the Array that will be read. It could be null if there was a parse error
  16. * @param variable the value (it could be a value or a key => value, or null if there was a parse error)
  17. * @param statement the statement that will be executed
  18. * @param sourceStart the start of the foreach
  19. * @param sourceEnd the end of the foreach
  20. */
  21. public ForeachStatement(final Expression expression,
  22. final Expression variable,
  23. final Statement statement,
  24. final int sourceStart,
  25. final int sourceEnd) {
  26. super(sourceStart, sourceEnd);
  27. this.expression = expression;
  28. this.variable = variable;
  29. this.statement = statement;
  30. }
  31. /**
  32. * Return the object into String.
  33. *
  34. * @param tab how many tabs (not used here
  35. * @return a String
  36. */
  37. public String toString(final int tab) {
  38. final String expressionString;
  39. if (expression == null) {
  40. expressionString = "__EXPRESSION__";
  41. } else {
  42. expressionString = expression.toStringExpression();
  43. }
  44. final String variableString;
  45. if (variable == null) {
  46. variableString = "__VARIABLE__";
  47. } else {
  48. variableString = variable.toStringExpression();
  49. }
  50. final String statementString;
  51. if (statement== null) {
  52. statementString = "__STATEMENT__";
  53. } else {
  54. statementString = statement.toString(tab + 1);
  55. }
  56. final StringBuffer buff = new StringBuffer(tab +
  57. expressionString.length() +
  58. variableString.length() +
  59. statementString.length() + 18);
  60. buff.append(AstNode.tabString(tab));
  61. buff.append("foreach (");
  62. buff.append(expressionString);
  63. buff.append(" as ");
  64. buff.append(variableString);
  65. buff.append(" {\n");
  66. buff.append(statementString);
  67. buff.append("\n}");
  68. return buff.toString();
  69. }
  70. /**
  71. * Get the variables from outside (parameters, globals ...).
  72. *
  73. * @param list the list where we will put variables
  74. */
  75. public void getOutsideVariable(final List list) {
  76. if (expression != null) expression.getOutsideVariable(list);
  77. if (variable != null) variable.getOutsideVariable(list);
  78. if (statement!= null) statement.getOutsideVariable(list);
  79. }
  80. /**
  81. * get the modified variables.
  82. *
  83. * @param list the list where we will put variables
  84. */
  85. public void getModifiedVariable(final List list) {
  86. if (expression != null) expression.getModifiedVariable(list);
  87. if (variable != null) variable.getUsedVariable(list);
  88. if (statement!= null) statement.getModifiedVariable(list);
  89. }
  90. /**
  91. * Get the variables used.
  92. *
  93. * @param list the list where we will put variables
  94. */
  95. public void getUsedVariable(final List list) {
  96. if (expression != null) expression.getUsedVariable(list);
  97. if (statement!= null) statement.getUsedVariable(list);
  98. }
  99. }