|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This software is licensed under The MIT License. You should have received |
|
8
|
|
|
* a copy of the license along with the code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Definition; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A symbol in known to the parser. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Symbol { |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $regexp; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $binding_power; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* This defines what a token means when appearing in the initial position |
|
29
|
|
|
* of an expression. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \Closure |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $null_denotation = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* This defines what a token means when appearing inside an expression |
|
37
|
|
|
* to the left of the rest. |
|
38
|
|
|
* |
|
39
|
|
|
* @var \Closure |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $left_denotation = null; |
|
42
|
|
|
|
|
43
|
51 |
|
public function __construct($regexp, $binding_power) { |
|
44
|
51 |
|
assert('is_string($regexp)'); |
|
45
|
51 |
|
if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) { |
|
46
|
1 |
|
throw new \InvalidArgumentException("Invalid regexp: '%$regexp%'"); |
|
47
|
|
|
} |
|
48
|
50 |
|
assert('is_int($binding_power)'); |
|
49
|
50 |
|
assert('$binding_power >= 0'); |
|
50
|
50 |
|
$this->regexp = $regexp; |
|
51
|
50 |
|
$this->binding_power = $binding_power; |
|
52
|
50 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
43 |
|
public function regexp() { |
|
58
|
43 |
|
return $this->regexp; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return int |
|
63
|
|
|
*/ |
|
64
|
31 |
|
public function binding_power() { |
|
65
|
31 |
|
return $this->binding_power; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \Closure $led |
|
70
|
|
|
* @return self |
|
71
|
|
|
*/ |
|
72
|
38 |
|
public function null_denotation_is(\Closure $led) { |
|
73
|
38 |
|
assert('$this->null_denotation === null'); |
|
74
|
38 |
|
$this->null_denotation = $led; |
|
75
|
38 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $matches |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
33 |
|
public function null_denotation(array &$matches) { |
|
83
|
33 |
|
$denotation = $this->denotation("null", $matches); |
|
84
|
32 |
|
return $denotation($matches); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param \Closure $led |
|
89
|
|
|
* @return self |
|
90
|
|
|
*/ |
|
91
|
38 |
|
public function left_denotation_is(\Closure $led) { |
|
92
|
38 |
|
assert('$this->left_denotation === null'); |
|
93
|
38 |
|
$this->left_denotation = $led; |
|
94
|
38 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param mixed $left |
|
99
|
|
|
* @param array $matches |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
22 |
|
public function left_denotation($left, array &$matches) { |
|
103
|
22 |
|
$denotation = $this->denotation("left", $matches); |
|
104
|
21 |
|
return $denotation($left, $matches); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// HELPER |
|
108
|
|
|
|
|
109
|
35 |
|
protected function denotation($which, array &$matches) { |
|
110
|
35 |
|
$which = $which."_denotation"; |
|
111
|
35 |
|
$denotation = $this->$which; |
|
112
|
35 |
|
if ($this->$which === null) { |
|
113
|
2 |
|
$m = $matches[0]; |
|
114
|
2 |
|
throw new ParserException("Syntax Error: $m"); |
|
115
|
|
|
} |
|
116
|
33 |
|
return $denotation; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|