1*e6380ba3SAndreas Gohr<?php 2*e6380ba3SAndreas Gohr 3*e6380ba3SAndreas Gohrnamespace LesserPHP; 4*e6380ba3SAndreas Gohr 5*e6380ba3SAndreas Gohr/** 6*e6380ba3SAndreas Gohr * An exception signalling a problem in the LESS source 7*e6380ba3SAndreas Gohr */ 8*e6380ba3SAndreas Gohrclass ParserException extends \Exception 9*e6380ba3SAndreas Gohr{ 10*e6380ba3SAndreas Gohr protected string $error = ''; 11*e6380ba3SAndreas Gohr protected string $culprit = ''; 12*e6380ba3SAndreas Gohr protected string $sourceFile = ''; 13*e6380ba3SAndreas Gohr protected int $sourceLine = -1; 14*e6380ba3SAndreas Gohr 15*e6380ba3SAndreas Gohr public function __construct( 16*e6380ba3SAndreas Gohr string $message = '', 17*e6380ba3SAndreas Gohr ?string $culprit = '', 18*e6380ba3SAndreas Gohr ?string $sourceFile = '', 19*e6380ba3SAndreas Gohr ?int $sourceLine = -1, 20*e6380ba3SAndreas Gohr \Throwable $previous = null 21*e6380ba3SAndreas Gohr ) { 22*e6380ba3SAndreas Gohr $this->error = $message; 23*e6380ba3SAndreas Gohr 24*e6380ba3SAndreas Gohr if ($culprit) { 25*e6380ba3SAndreas Gohr $this->culprit = $culprit; 26*e6380ba3SAndreas Gohr $message .= " `$culprit`"; 27*e6380ba3SAndreas Gohr } 28*e6380ba3SAndreas Gohr if ($sourceFile) { 29*e6380ba3SAndreas Gohr $this->sourceFile = $sourceFile; 30*e6380ba3SAndreas Gohr $message .= " in $sourceFile"; 31*e6380ba3SAndreas Gohr } 32*e6380ba3SAndreas Gohr 33*e6380ba3SAndreas Gohr if ($sourceLine !== null && $sourceLine > -1) { 34*e6380ba3SAndreas Gohr $this->sourceLine = $sourceLine; 35*e6380ba3SAndreas Gohr $message .= " line: $sourceLine"; 36*e6380ba3SAndreas Gohr } 37*e6380ba3SAndreas Gohr 38*e6380ba3SAndreas Gohr parent::__construct($message, 0, $previous); 39*e6380ba3SAndreas Gohr } 40*e6380ba3SAndreas Gohr 41*e6380ba3SAndreas Gohr /** 42*e6380ba3SAndreas Gohr * This is the error message without any additional context 43*e6380ba3SAndreas Gohr */ 44*e6380ba3SAndreas Gohr public function getError(): string 45*e6380ba3SAndreas Gohr { 46*e6380ba3SAndreas Gohr return $this->error; 47*e6380ba3SAndreas Gohr } 48*e6380ba3SAndreas Gohr 49*e6380ba3SAndreas Gohr /** 50*e6380ba3SAndreas Gohr * The LESS code that triggered the error 51*e6380ba3SAndreas Gohr * 52*e6380ba3SAndreas Gohr * This is the line the parser choked on. Not always available. 53*e6380ba3SAndreas Gohr */ 54*e6380ba3SAndreas Gohr public function getCulprit(): string 55*e6380ba3SAndreas Gohr { 56*e6380ba3SAndreas Gohr return $this->culprit; 57*e6380ba3SAndreas Gohr } 58*e6380ba3SAndreas Gohr 59*e6380ba3SAndreas Gohr /** 60*e6380ba3SAndreas Gohr * The LESS source file where the error was triggered 61*e6380ba3SAndreas Gohr * 62*e6380ba3SAndreas Gohr * This is the file the parser was parsing, will usually only be available when 63*e6380ba3SAndreas Gohr * parsing an import or when compileFile() was used. 64*e6380ba3SAndreas Gohr */ 65*e6380ba3SAndreas Gohr public function getSourceFile(): string 66*e6380ba3SAndreas Gohr { 67*e6380ba3SAndreas Gohr return $this->sourceFile; 68*e6380ba3SAndreas Gohr } 69*e6380ba3SAndreas Gohr 70*e6380ba3SAndreas Gohr /** 71*e6380ba3SAndreas Gohr * The line number where the error was triggered 72*e6380ba3SAndreas Gohr */ 73*e6380ba3SAndreas Gohr public function getSourceLine(): int 74*e6380ba3SAndreas Gohr { 75*e6380ba3SAndreas Gohr return $this->sourceLine; 76*e6380ba3SAndreas Gohr } 77*e6380ba3SAndreas Gohr} 78