summaryrefslogtreecommitdiff
path: root/src/ste/ParseCompileError.php
blob: 55a908aa152656223bd05071be5ab7de5d59e7b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

namespace r7r\ste;

/**
 * An exception thrown by the parser or compiler
 */
class ParseCompileError extends \Exception
{
    public $msg;
    public $tpl;
    public $off;

    public function __construct($msg, $tpl, $offset, $code = 0, $previous = null)
    {
        $this->msg = $msg;
        $this->tpl = $tpl;
        $this->off = $offset;

        parent::__construct("$msg (Template $tpl, Offset $offset)", $code, $previous);
    }

    /**
     * Update the message to include a human readable offset.
     * @param string $code
     */
    public function rewrite(string $code): void
    {
        $line = substr_count(str_replace("\r\n", "\n", substr($code, 0, $this->off)), "\n") + 1;
        $this->message = "{$this->msg} (Template {$this->tpl}, Line $line)";
    }
}