You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.9 KiB

3 years ago
  1. <?php declare(strict_types=1);
  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(string $message, $attributes = []) {
  15. $this->rawMessage = $message;
  16. if (is_array($attributes)) {
  17. $this->attributes = $attributes;
  18. } else {
  19. $this->attributes = ['startLine' => $attributes];
  20. }
  21. $this->updateMessage();
  22. }
  23. /**
  24. * Gets the error message
  25. *
  26. * @return string Error message
  27. */
  28. public function getRawMessage() : string {
  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() : int {
  37. return $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() : int {
  45. return $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() : array {
  53. return $this->attributes;
  54. }
  55. /**
  56. * Sets the attributes of the node/token the error occurred 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(string $message) {
  70. $this->rawMessage = $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(int $line) {
  79. $this->attributes['startLine'] = $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() : bool {
  90. return isset($this->attributes['startFilePos'], $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(string $code) : int {
  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(string $code) : int {
  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. /**
  117. * Formats message including line and column information.
  118. *
  119. * @param string $code Source code associated with the error, for calculation of the columns
  120. *
  121. * @return string Formatted message
  122. */
  123. public function getMessageWithColumnInfo(string $code) : string {
  124. return sprintf(
  125. '%s from %d:%d to %d:%d', $this->getRawMessage(),
  126. $this->getStartLine(), $this->getStartColumn($code),
  127. $this->getEndLine(), $this->getEndColumn($code)
  128. );
  129. }
  130. /**
  131. * Converts a file offset into a column.
  132. *
  133. * @param string $code Source code that $pos indexes into
  134. * @param int $pos 0-based position in $code
  135. *
  136. * @return int 1-based column (relative to start of line)
  137. */
  138. private function toColumn(string $code, int $pos) : int {
  139. if ($pos > strlen($code)) {
  140. throw new \RuntimeException('Invalid position information');
  141. }
  142. $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
  143. if (false === $lineStartPos) {
  144. $lineStartPos = -1;
  145. }
  146. return $pos - $lineStartPos;
  147. }
  148. /**
  149. * Updates the exception message after a change to rawMessage or rawLine.
  150. */
  151. protected function updateMessage() {
  152. $this->message = $this->rawMessage;
  153. if (-1 === $this->getStartLine()) {
  154. $this->message .= ' on unknown line';
  155. } else {
  156. $this->message .= ' on line ' . $this->getStartLine();
  157. }
  158. }
  159. }