No Description

PrettyPrinterAbstract.php 9.7KB

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