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 |
View Code Duplication |
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 |
View Code Duplication |
public function null_denotation(array &$matches) { |
|
|
|
|
83
|
33 |
|
if ($this->null_denotation === null) { |
84
|
1 |
|
$m = $matches[0]; |
85
|
1 |
|
throw new ParserException("Syntax Error: $m"); |
86
|
|
|
} |
87
|
32 |
|
$led = $this->null_denotation; |
88
|
32 |
|
return $led($matches); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \Closure $led |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
38 |
|
public function left_denotation_is(\Closure $led) { |
96
|
38 |
|
assert('$this->left_denotation === null'); |
97
|
38 |
|
$this->left_denotation = $led; |
98
|
38 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param mixed $left |
103
|
|
|
* @param array $matches |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
22 |
View Code Duplication |
public function left_denotation($left, array &$matches) { |
|
|
|
|
107
|
22 |
|
if ($this->left_denotation === null) { |
108
|
1 |
|
$m = $matches[0]; |
109
|
1 |
|
throw new ParserException("Syntax Error: $m"); |
110
|
|
|
} |
111
|
21 |
|
$led = $this->left_denotation; |
112
|
21 |
|
return $led($left, $matches); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.