1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vanderlee\Comprehend\Parser\Structure; |
4
|
|
|
|
5
|
|
|
use Vanderlee\Comprehend\Core\ArgumentsTrait; |
6
|
|
|
use Vanderlee\Comprehend\Core\Context; |
7
|
|
|
use Vanderlee\Comprehend\Match\Failure; |
8
|
|
|
use Vanderlee\Comprehend\Match\Success; |
9
|
|
|
use Vanderlee\Comprehend\Parser\Parser; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Match the first parser but not the second. |
13
|
|
|
* Essentially the same as (A - B) = (A + !B). |
14
|
|
|
* |
15
|
|
|
* @author Martijn |
16
|
|
|
*/ |
17
|
|
|
class Except extends Parser |
18
|
|
|
{ |
19
|
|
|
use ArgumentsTrait; |
20
|
|
|
|
21
|
|
|
private $parserMatch; |
22
|
|
|
private $parserNot; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Parser|string $match |
26
|
|
|
* @param Parser|string $not |
27
|
|
|
*/ |
28
|
1 |
|
public function __construct($match, $not) |
29
|
|
|
{ |
30
|
1 |
|
$this->parserMatch = self::getArgument($match); |
31
|
1 |
|
$this->parserNot = self::getArgument($not); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
8 |
|
protected function parse(&$input, $offset, Context $context) |
35
|
|
|
{ |
36
|
8 |
|
$match = $this->parserMatch->parse($input, $offset, $context); |
37
|
8 |
|
$not = $this->parserNot->parse($input, $offset, $context); |
38
|
|
|
|
39
|
8 |
|
if (($match instanceof Success) && ($not instanceof Failure)) { |
40
|
3 |
|
return $this->success($input, $offset, $match->length, $match); |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
return $this->failure($input, $offset, min($match->length, $not->length)); |
44
|
|
|
} |
45
|
|
|
|
46
|
8 |
|
public function __toString() |
47
|
|
|
{ |
48
|
8 |
|
return '( ' . $this->parserMatch . ' - ' . $this->parserNot . ' )'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|