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