菜谱项目

Error.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace PhpParser;
  3. class Error extends \RuntimeException
  4. {
  5. protected $rawMessage;
  6. protected $attributes;
  7. /**
  8. * Creates an Exception signifying a parse error.
  9. *
  10. * @param string $message Error message
  11. * @param array|int $attributes Attributes of node/token where error occurred
  12. * (or start line of error -- deprecated)
  13. */
  14. public function __construct($message, $attributes = array()) {
  15. $this->rawMessage = (string) $message;
  16. if (is_array($attributes)) {
  17. $this->attributes = $attributes;
  18. } else {
  19. $this->attributes = array('startLine' => $attributes);
  20. }
  21. $this->updateMessage();
  22. }
  23. /**
  24. * Gets the error message
  25. *
  26. * @return string Error message
  27. */
  28. public function getRawMessage() {
  29. return $this->rawMessage;
  30. }
  31. /**
  32. * Gets the line the error starts in.
  33. *
  34. * @return int Error start line
  35. */
  36. public function getStartLine() {
  37. return isset($this->attributes['startLine']) ? $this->attributes['startLine'] : -1;
  38. }
  39. /**
  40. * Gets the line the error ends in.
  41. *
  42. * @return int Error end line
  43. */
  44. public function getEndLine() {
  45. return isset($this->attributes['endLine']) ? $this->attributes['endLine'] : -1;
  46. }
  47. /**
  48. * Gets the attributes of the node/token the error occurred at.
  49. *
  50. * @return array
  51. */
  52. public function getAttributes() {
  53. return $this->attributes;
  54. }
  55. /**
  56. * Sets the attributes of the node/token the error occured at.
  57. *
  58. * @param array $attributes
  59. */
  60. public function setAttributes(array $attributes) {
  61. $this->attributes = $attributes;
  62. $this->updateMessage();
  63. }
  64. /**
  65. * Sets the line of the PHP file the error occurred in.
  66. *
  67. * @param string $message Error message
  68. */
  69. public function setRawMessage($message) {
  70. $this->rawMessage = (string) $message;
  71. $this->updateMessage();
  72. }
  73. /**
  74. * Sets the line the error starts in.
  75. *
  76. * @param int $line Error start line
  77. */
  78. public function setStartLine($line) {
  79. $this->attributes['startLine'] = (int) $line;
  80. $this->updateMessage();
  81. }
  82. /**
  83. * Returns whether the error has start and end column information.
  84. *
  85. * For column information enable the startFilePos and endFilePos in the lexer options.
  86. *
  87. * @return bool
  88. */
  89. public function hasColumnInfo() {
  90. return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']);
  91. }
  92. /**
  93. * Gets the start column (1-based) into the line where the error started.
  94. *
  95. * @param string $code Source code of the file
  96. * @return int
  97. */
  98. public function getStartColumn($code) {
  99. if (!$this->hasColumnInfo()) {
  100. throw new \RuntimeException('Error does not have column information');
  101. }
  102. return $this->toColumn($code, $this->attributes['startFilePos']);
  103. }
  104. /**
  105. * Gets the end column (1-based) into the line where the error ended.
  106. *
  107. * @param string $code Source code of the file
  108. * @return int
  109. */
  110. public function getEndColumn($code) {
  111. if (!$this->hasColumnInfo()) {
  112. throw new \RuntimeException('Error does not have column information');
  113. }
  114. return $this->toColumn($code, $this->attributes['endFilePos']);
  115. }
  116. public function getMessageWithColumnInfo($code) {
  117. return sprintf(
  118. '%s from %d:%d to %d:%d', $this->getRawMessage(),
  119. $this->getStartLine(), $this->getStartColumn($code),
  120. $this->getEndLine(), $this->getEndColumn($code)
  121. );
  122. }
  123. private function toColumn($code, $pos) {
  124. if ($pos > strlen($code)) {
  125. throw new \RuntimeException('Invalid position information');
  126. }
  127. $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
  128. if (false === $lineStartPos) {
  129. $lineStartPos = -1;
  130. }
  131. return $pos - $lineStartPos;
  132. }
  133. /**
  134. * Updates the exception message after a change to rawMessage or rawLine.
  135. */
  136. protected function updateMessage() {
  137. $this->message = $this->rawMessage;
  138. if (-1 === $this->getStartLine()) {
  139. $this->message .= ' on unknown line';
  140. } else {
  141. $this->message .= ' on line ' . $this->getStartLine();
  142. }
  143. }
  144. }