1<?php
2
3namespace AST;
4use \Exception;
5
6class UnknownTokenException extends Exception {
7    private $_text = null;
8    private $_position = null;
9
10    public function __construct($text, $position, $code = 0, Exception $previous = null) {
11        $this->_text = $text;
12        $this->_position = $position;
13        $message = 'Unknown token "';
14        if (extension_loaded('mbstring') === true) {
15            $message .= mb_substr($text, $position, 4);
16        } else {
17            $message .= substr($text, $position, 4);
18        }
19        $message .= '" at position ' . $position;
20        parent::__construct($message, $code, $previous);
21    }
22
23    public function getText() { return $this->_text; }
24    public function getPosition() { return $this->_position; }
25
26    public function __toString() {
27        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
28    }
29}
30
31class MalformedExpressionException extends Exception {
32    private $_elementInstance = null;
33
34    public function __construct($elementInstance, $message, $code = 0, Exception $previous = null) {
35        $this->_elementInstance = $elementInstance;
36        parent::__construct($message, $code, $previous);
37    }
38
39    public function getElementInstance() { return $this->_elementInstance; }
40
41    public function __toString() {
42        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
43    }
44}
45
46class InvalidExpressionException extends Exception  {
47    private $_elementInstance = null;
48
49    public function __construct($elementInstance, $message, $code = 0, Exception $previous = null) {
50        $this->_elementInstance = $elementInstance;
51        parent::__construct($message, $code, $previous);
52    }
53
54    public function getElementInstance() { return $this->_elementInstance; }
55
56    public function __toString() {
57        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
58    }
59}
60
61class NotEnoughArgumentsException extends Exception {
62    private $_elementDefinition = null;
63
64    public function __construct($elementDefinition, $code = 0, Exception $previous = null) {
65        $this->_elementDefinition = $elementDefinition;
66        $message = 'Not enough arguments for operator ' . $elementDefinition->name() . '.';
67        if ($elementDefinition->arity() > 0) {
68            $message .= ' Expected ' . $elementDefinition->arity() . ' arguments.';
69        }
70        parent::__construct($message, $code, $previous);
71    }
72
73    public function getElementDefinition() { return $this->_elementDefinition; }
74
75    public function __toString() {
76        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
77    }
78}
79
80class StrayTokenException extends Exception {
81    private $_tokenInstance = null;
82
83    public function __construct($tokenInstance, $code = 0, Exception $previous = null) {
84        $this->_tokenInstance = $tokenInstance;
85        $message = 'Stray token encountered at position ' . $tokenInstance->position() . ', around "';
86        if (extension_loaded('mbstring') === true) {
87            $message .= mb_substr($tokenInstance->text(), max(0, $tokenInstance->position() - 3), $tokenInstance->length() + 3);
88        } else {
89            $message .= substr($tokenInstance->text(), max(0, $tokenInstance->position() - 3), $tokenInstance->length() + 3);
90        }
91        $message .= '".';
92        parent::__construct($message, $code, $previous);
93    }
94
95    public function getTokenInstance() { return $this->_tokenInstance; }
96
97    public function __toString() {
98        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
99    }
100}
101
102class UnmatchedWrapperException extends Exception {
103    private $_elementDefinition = null;
104    private $_firstTokenInstance = null;
105
106    public function __construct($elementDefinition, $firstTokenInstance, $code = 0, Exception $previous = null) {
107        $this->_elementDefinition = $elementDefinition;
108        $this->_firstTokenInstance = $firstTokenInstance;
109        $message = 'Unmatched opening token ' . $elementDefinition->tokenDefs()[0] . ' for wrapping operator '
110            . $elementDefinition->name() . ' encountered at position ' . $firstTokenInstance->position() . ', around "';
111        if (extension_loaded('mbstring') === true) {
112            $message .= mb_substr($firstTokenInstance->text(), max(0, $firstTokenInstance->position() - 3), $firstTokenInstance->length() + 3);
113        } else {
114            $message .= substr($firstTokenInstance->text(), max(0, $firstTokenInstance->position() - 3), $firstTokenInstance->length() + 3);
115        }
116        $message .= '". The missing closing token is ' . $elementDefinition->tokenDefs()[1] . '.';
117        parent::__construct($message, $code, $previous);
118    }
119
120    public function getFirstTokenInstance() { return $this->_firstTokenInstance; }
121    public function getElementDefinition() { return $this->_elementDefinition; }
122
123    public function __toString() {
124        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
125    }
126}
127
128?>