|
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
|
|
|
* Description of RangeParser |
|
10
|
|
|
* |
|
11
|
|
|
* @author Martijn |
|
12
|
|
|
*/ |
|
13
|
|
|
class Range extends Parser |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
use CaseSensitiveTrait; |
|
17
|
|
|
|
|
18
|
|
|
private $first = null; |
|
19
|
|
|
private $last = null; |
|
20
|
|
|
private $in = true; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($first, $last, $in = true) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($first === null && $last === null) { |
|
25
|
|
|
throw new \InvalidArgumentException('Empty arguments'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->first = $first === null ? null : self::parseCharacter($first); |
|
29
|
|
|
$this->last = $last === null ? null : self::parseCharacter($last); |
|
30
|
|
|
$this->in = (bool)$in; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function parse(&$input, $offset, Context $context) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($offset >= mb_strlen($input)) { |
|
36
|
|
|
return $this->failure($input, $offset); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->pushCaseSensitivityToContext($context); |
|
40
|
|
|
|
|
41
|
|
|
$first = ord($context->handleCase($this->first)); |
|
42
|
|
|
$last = ord($context->handleCase($this->last)); |
|
43
|
|
|
$ord = ord($context->handleCase($input[$offset])); |
|
44
|
|
|
if ($first <= $ord && ($this->last === null || $ord <= $last)) { |
|
45
|
|
|
$this->popCaseSensitivityFromContext($context); |
|
46
|
|
|
|
|
47
|
|
|
return $this->in ? $this->success($input, $offset, 1) : $this->failure($input, $offset); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->popCaseSensitivityFromContext($context); |
|
51
|
|
|
|
|
52
|
|
|
return $this->in ? $this->failure($input, $offset) : $this->success($input, $offset, 1); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function __toString() |
|
56
|
|
|
{ |
|
57
|
|
|
return sprintf('x%02x-x%02x', ord($this->first), ord($this->last)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|