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