2021-02-15 21:29:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parser Exception
|
|
|
|
*/
|
|
|
|
class Less_Exception_Parser extends Exception {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current file
|
|
|
|
*
|
2023-07-07 22:33:10 +02:00
|
|
|
* @var array
|
2021-02-15 21:29:38 +01:00
|
|
|
*/
|
|
|
|
public $currentFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current parser index
|
|
|
|
*
|
2023-07-07 22:33:10 +02:00
|
|
|
* @var int
|
2021-02-15 21:29:38 +01:00
|
|
|
*/
|
|
|
|
public $index;
|
|
|
|
|
|
|
|
protected $input;
|
|
|
|
|
2023-07-07 22:33:10 +02:00
|
|
|
protected $details = [];
|
2021-02-15 21:29:38 +01:00
|
|
|
|
|
|
|
/**
|
2023-07-07 22:33:10 +02:00
|
|
|
* @param string|null $message
|
|
|
|
* @param Exception|null $previous Previous exception
|
|
|
|
* @param int|null $index The current parser index
|
|
|
|
* @param array|null $currentFile The file
|
|
|
|
* @param int $code The exception code
|
2021-02-15 21:29:38 +01:00
|
|
|
*/
|
|
|
|
public function __construct( $message = null, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) {
|
2023-07-07 22:33:10 +02:00
|
|
|
parent::__construct( $message, $code, $previous );
|
2021-02-15 21:29:38 +01:00
|
|
|
|
|
|
|
$this->currentFile = $currentFile;
|
|
|
|
$this->index = $index;
|
|
|
|
|
|
|
|
$this->genMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInput() {
|
|
|
|
if ( !$this->input && $this->currentFile && $this->currentFile['filename'] && file_exists( $this->currentFile['filename'] ) ) {
|
|
|
|
$this->input = file_get_contents( $this->currentFile['filename'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-07 22:33:10 +02:00
|
|
|
* Set a message based on the exception info
|
2021-02-15 21:29:38 +01:00
|
|
|
*/
|
|
|
|
public function genMessage() {
|
|
|
|
if ( $this->currentFile && $this->currentFile['filename'] ) {
|
2023-07-07 22:33:10 +02:00
|
|
|
$this->message .= ' in ' . basename( $this->currentFile['filename'] );
|
2021-02-15 21:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->index !== null ) {
|
|
|
|
$this->getInput();
|
|
|
|
if ( $this->input ) {
|
|
|
|
$line = self::getLineNumber();
|
2023-07-07 22:33:10 +02:00
|
|
|
$this->message .= ' on line ' . $line . ', column ' . self::getColumn();
|
2021-02-15 21:29:38 +01:00
|
|
|
|
|
|
|
$lines = explode( "\n", $this->input );
|
|
|
|
|
|
|
|
$count = count( $lines );
|
|
|
|
$start_line = max( 0, $line - 3 );
|
|
|
|
$last_line = min( $count, $start_line + 6 );
|
|
|
|
$num_len = strlen( $last_line );
|
|
|
|
for ( $i = $start_line; $i < $last_line; $i++ ) {
|
2023-07-07 22:33:10 +02:00
|
|
|
$this->message .= "\n" . str_pad( (string)( $i + 1 ), $num_len, '0', STR_PAD_LEFT ) . '| ' . $lines[$i];
|
2021-02-15 21:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the line number the error was encountered
|
|
|
|
*
|
2023-07-07 22:33:10 +02:00
|
|
|
* @return int
|
2021-02-15 21:29:38 +01:00
|
|
|
*/
|
|
|
|
public function getLineNumber() {
|
|
|
|
if ( $this->index ) {
|
|
|
|
// https://bugs.php.net/bug.php?id=49790
|
|
|
|
if ( ini_get( "mbstring.func_overload" ) ) {
|
|
|
|
return substr_count( substr( $this->input, 0, $this->index ), "\n" ) + 1;
|
|
|
|
} else {
|
|
|
|
return substr_count( $this->input, "\n", 0, $this->index ) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the column the error was encountered
|
|
|
|
*
|
2023-07-07 22:33:10 +02:00
|
|
|
* @return int
|
2021-02-15 21:29:38 +01:00
|
|
|
*/
|
|
|
|
public function getColumn() {
|
|
|
|
$part = substr( $this->input, 0, $this->index );
|
|
|
|
$pos = strrpos( $part, "\n" );
|
|
|
|
return $this->index - $pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|