-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredicateParser.php
More file actions
84 lines (72 loc) · 1.78 KB
/
Copy pathPredicateParser.php
File metadata and controls
84 lines (72 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace petitparser;
/**
* A parser for a literal satisfying a predicate.
*/
class PredicateParser extends Parser
{
/**
* @var int
*/
protected $_length;
/**
* @var callable
*/
protected $_predicate;
/**
* @var string
*/
protected $_message;
/**
* @param int $length
* @param callable $predicate
* @param string $message
*/
public function __construct($length, $predicate, $message)
{
$this->_length = $length;
$this->_predicate = $predicate;
$this->_message = $message;
}
/**
* @param Context $context
*
* @return Result
*/
public function parseOn(Context $context)
{
$start = $context->getPosition();
$stop = $start + $this->_length;
if ($stop <= length($context->getBuffer())) {
$result = $context->getBuffer()->slice($start, $stop)->getString();
if (call_user_func($this->_predicate, $result)) {
return $context->success($result, $stop);
}
}
return $context->failure($this->_message);
}
public function __toString()
{
return parent::__toString() . '[' . $this->_message . ']';
}
/**
* @return Parser
*/
public function copy()
{
return new PredicateParser($this->_length, $this->_predicate, $this->_message);
}
/**
* @param Parser $other
*
* @return bool
*/
public function hasEqualProperties(Parser $other)
{
return parent::hasEqualProperties($other)
&& $other instanceof self
&& $this->_length === $other->_length
&& $this->_predicate === $other->_predicate
&& $this->_message === $other->_message;
}
}