/plugins/PHPParser/tags/PHPParser-1.3.2/src/net/sourceforge/phpdt/internal/compiler/ast/IfStatement.java

# · Java · 183 lines · 131 code · 12 blank · 40 comment · 34 complexity · 37ee5578ae85cdb538182b19b5822f60 MD5 · raw file

  1. package net.sourceforge.phpdt.internal.compiler.ast;
  2. import gatchan.phpparser.parser.PHPParser;
  3. import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
  4. import java.util.List;
  5. /**
  6. * This is a if statement.
  7. * if (condition)
  8. * statement
  9. * (elseif statement)*
  10. * else statement
  11. *
  12. * @author Matthieu Casanova
  13. */
  14. public class IfStatement extends Statement
  15. {
  16. private final Expression condition;
  17. private final Statement statement;
  18. private final ElseIf[] elseifs;
  19. private final Else els;
  20. /**
  21. * Create a new If statement.
  22. *
  23. * @param condition the condition
  24. * @param statement a statement or a block of statements
  25. * @param elseifs the elseifs
  26. * @param els the else (or null)
  27. * @param sourceStart the starting position
  28. * @param sourceEnd the ending offset
  29. */
  30. public IfStatement(Expression condition,
  31. Statement statement,
  32. ElseIf[] elseifs,
  33. Else els,
  34. int sourceStart,
  35. int sourceEnd,
  36. int beginLine,
  37. int endLine,
  38. int beginColumn,
  39. int endColumn)
  40. {
  41. super(sourceStart, sourceEnd, beginLine, endLine, beginColumn, endColumn);
  42. this.condition = condition;
  43. this.statement = statement;
  44. this.elseifs = elseifs;
  45. this.els = els;
  46. }
  47. /**
  48. * Return the object into String.
  49. *
  50. * @param tab how many tabs (not used here
  51. * @return a String
  52. */
  53. @Override
  54. public String toString(int tab)
  55. {
  56. StringBuilder buff = new StringBuilder(tabString(tab));
  57. buff.append("if (");
  58. buff.append(condition.toStringExpression()).append(") ");
  59. if (statement != null)
  60. {
  61. buff.append(statement.toString(tab + 1));
  62. }
  63. for (int i = 0; i < elseifs.length; i++)
  64. {
  65. buff.append(elseifs[i].toString(tab + 1));
  66. buff.append('\n');
  67. }
  68. if (els != null)
  69. {
  70. buff.append(els.toString(tab + 1));
  71. buff.append('\n');
  72. }
  73. return buff.toString();
  74. }
  75. /**
  76. * Get the variables from outside (parameters, globals ...)
  77. *
  78. * @param list the list where we will put variables
  79. */
  80. @Override
  81. public void getOutsideVariable(List<VariableUsage> list)
  82. {
  83. condition.getOutsideVariable(list); // todo: check if unuseful
  84. if (statement != null)
  85. {
  86. statement.getOutsideVariable(list);
  87. }
  88. for (int i = 0; i < elseifs.length; i++)
  89. {
  90. elseifs[i].getOutsideVariable(list);
  91. }
  92. if (els != null)
  93. {
  94. els.getOutsideVariable(list);
  95. }
  96. }
  97. /**
  98. * get the modified variables.
  99. *
  100. * @param list the list where we will put variables
  101. */
  102. @Override
  103. public void getModifiedVariable(List<VariableUsage> list)
  104. {
  105. condition.getModifiedVariable(list);
  106. if (statement != null)
  107. {
  108. statement.getModifiedVariable(list);
  109. }
  110. for (int i = 0; i < elseifs.length; i++)
  111. {
  112. elseifs[i].getModifiedVariable(list);
  113. }
  114. if (els != null)
  115. {
  116. els.getModifiedVariable(list);
  117. }
  118. }
  119. @Override
  120. public void analyzeCode(PHPParser parser)
  121. {
  122. condition.analyzeCode(parser);
  123. if (statement != null)
  124. {
  125. statement.analyzeCode(parser);
  126. }
  127. for (int i = 0; i < elseifs.length; i++)
  128. {
  129. elseifs[i].analyzeCode(parser);
  130. }
  131. if (els != null)
  132. {
  133. els.analyzeCode(parser);
  134. }
  135. }
  136. /**
  137. * Get the variables used.
  138. *
  139. * @param list the list where we will put variables
  140. */
  141. @Override
  142. public void getUsedVariable(List<VariableUsage> list)
  143. {
  144. condition.getUsedVariable(list);
  145. if (statement != null)
  146. {
  147. statement.getUsedVariable(list);
  148. }
  149. for (int i = 0; i < elseifs.length; i++)
  150. {
  151. elseifs[i].getUsedVariable(list);
  152. }
  153. if (els != null)
  154. {
  155. els.getUsedVariable(list);
  156. }
  157. }
  158. @Override
  159. public Expression expressionAt(int line, int column)
  160. {
  161. if (condition.isAt(line, column)) return condition;
  162. if (statement != null && statement.isAt(line, column)) return statement.expressionAt(line, column);
  163. for (int i = 0; i < elseifs.length; i++)
  164. {
  165. ElseIf elseif = elseifs[i];
  166. if (elseif.isAt(line, column)) return elseif.expressionAt(line, column);
  167. }
  168. if (els != null && els.isAt(line, column)) return els.expressionAt(line, column);
  169. return null;
  170. }
  171. }