|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vanderlee\Comprehend\Parser\Terminal; |
|
4
|
|
|
|
|
5
|
|
|
use Vanderlee\Comprehend\Core\Context; |
|
6
|
|
|
use Vanderlee\Comprehend\Parser\Parser; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Matches an integer within the specified range |
|
10
|
|
|
* |
|
11
|
|
|
* @author Martijn |
|
12
|
|
|
*/ |
|
13
|
|
|
class Integer extends Parser |
|
14
|
|
|
{ |
|
15
|
|
|
use CaseSensitiveTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* List of digits to use for the different bases (upto 36) |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $set = '0123456789abcdefghijklmnopqrstuvwxyz'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int|null |
|
25
|
|
|
*/ |
|
26
|
|
|
private $minimum; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $maximum; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $base; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct($minimum = 0, $maximum = null, $base = 10) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($minimum !== null && !is_int($minimum)) { |
|
41
|
|
|
throw new \Exception('Minimum must be integer or `null`'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($maximum !== null && !is_int($maximum)) { |
|
45
|
|
|
throw new \Exception('Maximum must be integer or `null`'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($minimum !== null && $maximum !== null && $minimum > $maximum) { |
|
49
|
|
|
throw new \Exception('Maximum must be greater than minimum'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->minimum = $minimum; |
|
53
|
|
|
$this->maximum = $maximum; |
|
54
|
|
|
|
|
55
|
|
|
$this->base = intval($base); |
|
56
|
|
|
if ($base < 2 || $base > strlen(self::$set)) { |
|
57
|
|
|
throw new \Exception('Invalid base'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function parse(&$input, $offset, Context $context) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->pushCaseSensitivityToContext($context); |
|
64
|
|
|
|
|
65
|
|
|
// Build pattern |
|
66
|
|
|
$set0 = substr(self::$set, 0, $this->base); |
|
67
|
|
|
$set1 = substr(self::$set, 1, $this->base - 1); |
|
68
|
|
|
$pattern = '/^(?:0|-?[' . $set1 . '][' . $set0 . ']*)/' . ($context->isCaseSensitive() ? '' : 'i'); |
|
69
|
|
|
|
|
70
|
|
|
$this->popCaseSensitivityFromContext($context); |
|
71
|
|
|
|
|
72
|
|
|
if (preg_match($pattern, $input, $match, 0, $offset) === 1) { |
|
73
|
|
|
do { |
|
74
|
|
|
$integer = intval($match[0], $this->base); |
|
75
|
|
|
if (($this->minimum === null || $integer >= $this->minimum) |
|
76
|
|
|
&& ($this->maximum === null || $integer <= $this->maximum) |
|
77
|
|
|
&& $match[0] !== '-') { |
|
78
|
|
|
return $this->success($input, $offset, mb_strlen($match[0])); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$match[0] = substr($match[0], 0, -1); // strip off last char |
|
82
|
|
|
} while ($match[0] !== ''); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->failure($input, $offset); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function __toString() |
|
89
|
|
|
{ |
|
90
|
|
|
return ($this->minimum === null ? '<-INF' : ('[' . $this->minimum)) . ',' . ($this->maximum === null ? 'INF>' : ($this->maximum . ']')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|