PrettyPrinterAbstract.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. abstract class PHPParser_PrettyPrinterAbstract
  3. {
  4. protected $precedenceMap = array(
  5. // [precedence, associativity] where for the latter -1 is %left, 0 is %nonassoc and 1 is %right
  6. 'Expr_BitwiseNot' => array( 1, 1),
  7. 'Expr_PreInc' => array( 1, 1),
  8. 'Expr_PreDec' => array( 1, 1),
  9. 'Expr_PostInc' => array( 1, -1),
  10. 'Expr_PostDec' => array( 1, -1),
  11. 'Expr_UnaryPlus' => array( 1, 1),
  12. 'Expr_UnaryMinus' => array( 1, 1),
  13. 'Expr_Cast_Int' => array( 1, 1),
  14. 'Expr_Cast_Double' => array( 1, 1),
  15. 'Expr_Cast_String' => array( 1, 1),
  16. 'Expr_Cast_Array' => array( 1, 1),
  17. 'Expr_Cast_Object' => array( 1, 1),
  18. 'Expr_Cast_Bool' => array( 1, 1),
  19. 'Expr_Cast_Unset' => array( 1, 1),
  20. 'Expr_ErrorSuppress' => array( 1, 1),
  21. 'Expr_Instanceof' => array( 2, 0),
  22. 'Expr_BooleanNot' => array( 3, 1),
  23. 'Expr_Mul' => array( 4, -1),
  24. 'Expr_Div' => array( 4, -1),
  25. 'Expr_Mod' => array( 4, -1),
  26. 'Expr_Plus' => array( 5, -1),
  27. 'Expr_Minus' => array( 5, -1),
  28. 'Expr_Concat' => array( 5, -1),
  29. 'Expr_ShiftLeft' => array( 6, -1),
  30. 'Expr_ShiftRight' => array( 6, -1),
  31. 'Expr_Smaller' => array( 7, 0),
  32. 'Expr_SmallerOrEqual' => array( 7, 0),
  33. 'Expr_Greater' => array( 7, 0),
  34. 'Expr_GreaterOrEqual' => array( 7, 0),
  35. 'Expr_Equal' => array( 8, 0),
  36. 'Expr_NotEqual' => array( 8, 0),
  37. 'Expr_Identical' => array( 8, 0),
  38. 'Expr_NotIdentical' => array( 8, 0),
  39. 'Expr_BitwiseAnd' => array( 9, -1),
  40. 'Expr_BitwiseXor' => array(10, -1),
  41. 'Expr_BitwiseOr' => array(11, -1),
  42. 'Expr_BooleanAnd' => array(12, -1),
  43. 'Expr_BooleanOr' => array(13, -1),
  44. 'Expr_Ternary' => array(14, -1),
  45. // parser uses %left for assignments, but they really behave as %right
  46. 'Expr_Assign' => array(15, 1),
  47. 'Expr_AssignRef' => array(15, 1),
  48. 'Expr_AssignPlus' => array(15, 1),
  49. 'Expr_AssignMinus' => array(15, 1),
  50. 'Expr_AssignMul' => array(15, 1),
  51. 'Expr_AssignDiv' => array(15, 1),
  52. 'Expr_AssignConcat' => array(15, 1),
  53. 'Expr_AssignMod' => array(15, 1),
  54. 'Expr_AssignBitwiseAnd' => array(15, 1),
  55. 'Expr_AssignBitwiseOr' => array(15, 1),
  56. 'Expr_AssignBitwiseXor' => array(15, 1),
  57. 'Expr_AssignShiftLeft' => array(15, 1),
  58. 'Expr_AssignShiftRight' => array(15, 1),
  59. 'Expr_LogicalAnd' => array(16, -1),
  60. 'Expr_LogicalXor' => array(17, -1),
  61. 'Expr_LogicalOr' => array(18, -1),
  62. 'Expr_Include' => array(19, -1),
  63. );
  64. protected $noIndentToken;
  65. protected $canUseSemicolonNamespaces;
  66. public function __construct() {
  67. $this->noIndentToken = '_NO_INDENT_' . mt_rand();
  68. }
  69. /**
  70. * Pretty prints an array of statements.
  71. *
  72. * @param PHPParser_Node[] $stmts Array of statements
  73. *
  74. * @return string Pretty printed statements
  75. */
  76. public function prettyPrint(array $stmts) {
  77. $this->preprocessNodes($stmts);
  78. return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false));
  79. }
  80. /**
  81. * Pretty prints an expression.
  82. *
  83. * @param PHPParser_Node_Expr $node Expression node
  84. *
  85. * @return string Pretty printed node
  86. */
  87. public function prettyPrintExpr(PHPParser_Node_Expr $node) {
  88. return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
  89. }
  90. /**
  91. * Pretty prints a file of statements (includes the opening <?php tag if it is required).
  92. *
  93. * @param PHPParser_Node[] $stmts Array of statements
  94. *
  95. * @return string Pretty printed statements
  96. */
  97. public function prettyPrintFile(array $stmts) {
  98. $p = trim($this->prettyPrint($stmts));
  99. $p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
  100. $p = preg_replace('/<\?php$/', '', $p);
  101. if (!$count) {
  102. $p = "<?php\n\n" . $p;
  103. }
  104. return $p;
  105. }
  106. /**
  107. * Preprocesses the top-level nodes to initialize pretty printer state.
  108. *
  109. * @param PHPParser_Node[] $nodes Array of nodes
  110. */
  111. protected function preprocessNodes(array $nodes) {
  112. /* We can use semicolon-namespaces unless there is a global namespace declaration */
  113. $this->canUseSemicolonNamespaces = true;
  114. foreach ($nodes as $node) {
  115. if ($node instanceof PHPParser_Node_Stmt_Namespace && null === $node->name) {
  116. $this->canUseSemicolonNamespaces = false;
  117. }
  118. }
  119. }
  120. /**
  121. * Pretty prints an array of nodes (statements) and indents them optionally.
  122. *
  123. * @param PHPParser_Node[] $nodes Array of nodes
  124. * @param bool $indent Whether to indent the printed nodes
  125. *
  126. * @return string Pretty printed statements
  127. */
  128. protected function pStmts(array $nodes, $indent = true) {
  129. $pNodes = array();
  130. foreach ($nodes as $node) {
  131. $pNodes[] = $this->pComments($node->getAttribute('comments', array()))
  132. . $this->p($node)
  133. . ($node instanceof PHPParser_Node_Expr ? ';' : '');
  134. }
  135. if ($indent) {
  136. return ' ' . preg_replace(
  137. '~\n(?!$|' . $this->noIndentToken . ')~',
  138. "\n" . ' ',
  139. implode("\n", $pNodes)
  140. );
  141. } else {
  142. return implode("\n", $pNodes);
  143. }
  144. }
  145. /**
  146. * Pretty prints a node.
  147. *
  148. * @param PHPParser_Node $node Node to be pretty printed
  149. *
  150. * @return string Pretty printed node
  151. */
  152. protected function p(PHPParser_Node $node) {
  153. return $this->{'p' . $node->getType()}($node);
  154. }
  155. protected function pInfixOp($type, PHPParser_Node $leftNode, $operatorString, PHPParser_Node $rightNode) {
  156. list($precedence, $associativity) = $this->precedenceMap[$type];
  157. return $this->pPrec($leftNode, $precedence, $associativity, -1)
  158. . $operatorString
  159. . $this->pPrec($rightNode, $precedence, $associativity, 1);
  160. }
  161. protected function pPrefixOp($type, $operatorString, PHPParser_Node $node) {
  162. list($precedence, $associativity) = $this->precedenceMap[$type];
  163. return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
  164. }
  165. protected function pPostfixOp($type, PHPParser_Node $node, $operatorString) {
  166. list($precedence, $associativity) = $this->precedenceMap[$type];
  167. return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
  168. }
  169. /**
  170. * Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
  171. *
  172. * @param PHPParser_Node $node Node to pretty print
  173. * @param int $parentPrecedence Precedence of the parent operator
  174. * @param int $parentAssociativity Associativity of parent operator
  175. * (-1 is left, 0 is nonassoc, 1 is right)
  176. * @param int $childPosition Position of the node relative to the operator
  177. * (-1 is left, 1 is right)
  178. *
  179. * @return string The pretty printed node
  180. */
  181. protected function pPrec(PHPParser_Node $node, $parentPrecedence, $parentAssociativity, $childPosition) {
  182. $type = $node->getType();
  183. if (isset($this->precedenceMap[$type])) {
  184. $childPrecedence = $this->precedenceMap[$type][0];
  185. if ($childPrecedence > $parentPrecedence
  186. || ($parentPrecedence == $childPrecedence && $parentAssociativity != $childPosition)
  187. ) {
  188. return '(' . $this->{'p' . $type}($node) . ')';
  189. }
  190. }
  191. return $this->{'p' . $type}($node);
  192. }
  193. /**
  194. * Pretty prints an array of nodes and implodes the printed values.
  195. *
  196. * @param PHPParser_Node[] $nodes Array of Nodes to be printed
  197. * @param string $glue Character to implode with
  198. *
  199. * @return string Imploded pretty printed nodes
  200. */
  201. protected function pImplode(array $nodes, $glue = '') {
  202. $pNodes = array();
  203. foreach ($nodes as $node) {
  204. $pNodes[] = $this->p($node);
  205. }
  206. return implode($glue, $pNodes);
  207. }
  208. /**
  209. * Pretty prints an array of nodes and implodes the printed values with commas.
  210. *
  211. * @param PHPParser_Node[] $nodes Array of Nodes to be printed
  212. *
  213. * @return string Comma separated pretty printed nodes
  214. */
  215. protected function pCommaSeparated(array $nodes) {
  216. return $this->pImplode($nodes, ', ');
  217. }
  218. /**
  219. * Signals the pretty printer that a string shall not be indented.
  220. *
  221. * @param string $string Not to be indented string
  222. *
  223. * @return mixed String marked with $this->noIndentToken's.
  224. */
  225. protected function pNoIndent($string) {
  226. return str_replace("\n", "\n" . $this->noIndentToken, $string);
  227. }
  228. protected function pComments(array $comments) {
  229. $result = '';
  230. foreach ($comments as $comment) {
  231. $result .= $comment->getReformattedText() . "\n";
  232. }
  233. return $result;
  234. }
  235. }