|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Utils; |
|
4
|
|
|
|
|
5
|
|
|
class Lexer |
|
6
|
|
|
{ |
|
7
|
|
|
const CLASS_UPPERCASE_UNAMBIGUOUS = 1; |
|
8
|
|
|
|
|
9
|
|
|
private $logger; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Tokeniser explodes input into components describing the properties expressed in the pattern. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $pattern |
|
15
|
|
|
* @param boolean $expand Augment token definition with the haystack of possible values. |
|
16
|
|
|
* |
|
17
|
|
|
* @return array |
|
18
|
|
|
*/ |
|
19
|
|
|
public function tokenise($pattern, $expand = false) |
|
20
|
|
|
{ |
|
21
|
|
|
preg_match_all(' |
|
22
|
|
|
/ |
|
23
|
|
|
(?<class_U_explicit>\\\U) |
|
24
|
|
|
\{ |
|
25
|
|
|
(?<class_U_repetition>[0-9]+) |
|
26
|
|
|
\} |
|
27
|
|
|
| |
|
28
|
|
|
(?<class_U_implicit>\\\U) |
|
29
|
|
|
| |
|
30
|
|
|
\[ |
|
31
|
|
|
(?<range_token_explicit>[^]]+) |
|
32
|
|
|
\] |
|
33
|
|
|
\{ |
|
34
|
|
|
(?<range_repetition>[0-9]+) |
|
35
|
|
|
\} |
|
36
|
|
|
| |
|
37
|
|
|
\[ |
|
38
|
|
|
(?<range_token_implicit>[^]]+) |
|
39
|
|
|
\] |
|
40
|
|
|
| |
|
41
|
|
|
(?<literal_string>[^\\\[]+) |
|
42
|
|
|
/x |
|
43
|
|
|
', $pattern, $matches, \PREG_SET_ORDER); |
|
44
|
|
|
$tokens = []; |
|
45
|
|
|
foreach ($matches as $match) { |
|
46
|
|
|
|
|
47
|
|
|
if (!empty($match['class_U_explicit'])) { |
|
48
|
|
|
$token = [ |
|
49
|
|
|
'type' => 'class', |
|
50
|
|
|
'class' => static::CLASS_UPPERCASE_UNAMBIGUOUS, |
|
51
|
|
|
'repetition' => (int)$match['class_U_repetition'], |
|
52
|
|
|
]; |
|
53
|
|
|
if ($expand) { |
|
54
|
|
|
$token['haystack'] = 'ABCDEFGHKMNOPRSTUVWXYZ23456789'; |
|
55
|
|
|
} |
|
56
|
|
|
$tokens[] = $token; |
|
57
|
|
|
} elseif (!empty($match['class_U_implicit'])) { |
|
58
|
|
|
$token = [ |
|
59
|
|
|
'type' => 'class', |
|
60
|
|
|
'class' => static::CLASS_UPPERCASE_UNAMBIGUOUS, |
|
61
|
|
|
'repetition' => 1, |
|
62
|
|
|
]; |
|
63
|
|
|
if ($expand) { |
|
64
|
|
|
$token['haystack'] = 'ABCDEFGHKMNOPRSTUVWXYZ23456789'; |
|
65
|
|
|
} |
|
66
|
|
|
$tokens[] = $token; |
|
67
|
|
|
} elseif (!empty($match['range_token_explicit'])) { |
|
68
|
|
|
$token = [ |
|
69
|
|
|
'type' => 'range', |
|
70
|
|
|
'token' => $match['range_token_explicit'], |
|
71
|
|
|
'repetition' => (int)$match['range_repetition'], |
|
72
|
|
|
]; |
|
73
|
|
|
if ($expand) { |
|
74
|
|
|
$token['haystack'] = static::expandRange($match['range_token_explicit']); |
|
75
|
|
|
} |
|
76
|
|
|
$tokens[] = $token; |
|
77
|
|
|
} elseif (!empty($match['range_token_implicit'])) { |
|
78
|
|
|
$token = [ |
|
79
|
|
|
'type' => 'range', |
|
80
|
|
|
'token' => $match['range_token_implicit'], |
|
81
|
|
|
'repetition' => 1, |
|
82
|
|
|
]; |
|
83
|
|
|
if ($expand) { |
|
84
|
|
|
$token['haystack'] = static::expandRange($match['range_token_implicit']); |
|
85
|
|
|
} |
|
86
|
|
|
$tokens[] = $token; |
|
87
|
|
|
} elseif (!empty($match['literal_string'])) { |
|
88
|
|
|
$tokens[] = [ |
|
89
|
|
|
'type' => 'literal', |
|
90
|
|
|
'string' => $match['literal_string'], |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $tokens; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $range_definition Set of characters defined individually, using range or both. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string All characters that fit in the range. |
|
102
|
|
|
*/ |
|
103
|
|
|
static public function expandRange($range_definition) |
|
104
|
|
|
{ |
|
105
|
|
|
$haystack = preg_replace_callback('/(?<from>.)\-(?<to>.)/', function ($e) { |
|
106
|
|
|
if (is_numeric($e['from']) || is_numeric($e['to'])) { |
|
107
|
|
|
$from = $e['from']; |
|
108
|
|
|
$to = $e['to']; |
|
109
|
|
|
if ($from > $to) { |
|
110
|
|
|
throw new Exception\LogicException('Invalid range definition. Start greater than end.'); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
$haystack = ''; |
|
113
|
|
|
for ($from; $from <= $to; $from++) { |
|
114
|
|
|
$haystack .= $from; |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
$from = ord($e['from']); |
|
118
|
|
|
$to = ord($e['to']); |
|
119
|
|
|
if ($from > $to) { |
|
120
|
|
|
throw new Exception\LogicException('Invalid range definition. Start greater than end.'); |
|
121
|
|
|
} |
|
122
|
|
|
$haystack = ''; |
|
123
|
|
|
for ($from; $from <= $to; $from++) { |
|
124
|
|
|
$haystack .= chr($from); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $haystack; |
|
129
|
|
|
}, $range_definition); |
|
130
|
|
|
$haystack = implode('', array_unique(str_split($haystack))); |
|
131
|
|
|
|
|
132
|
|
|
return $haystack; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|