-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlittle_lexer.php
206 lines (195 loc) · 6.5 KB
/
little_lexer.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Dcp\Lex;
/**
* Analyse a general filter string
* @package Dcp\Lex
*/
class GeneralFilter
{
const T_ESCAPE = "T_ESCAPE";
const T_QUOTE = "T_QUOTE";
const T_WHITESPACE = "T_WHITESPACE";
const T_TILDE = "T_TILDE";
const T_OPEN_PARENTHESIS = "T_OPEN_PARENTHESIS";
const T_CLOSE_PARENTHESIS = "T_CLOSE_PARENTHESIS";
const T_OR = "T_OR";
const T_AND = "T_AND";
const T_WORD = "T_WORD";
const T_PUNCTUATION = "T_PUNCTUATION";
const MODE_STRING = "string";
const MODE_WORD = "word";
const MODE_OPEN_PARENTHESIS = "open_parenthesis";
const MODE_PARTIAL = "partial";
const MODE_CLOSE_PARENTHESIS = "close_parenthesis";
const MODE_OR = "or";
const MODE_AND = "and";
/**
* List of tokens in priority order
* @var array
*/
protected static $_terminals = array(
'/^(\\\)/' => self::T_ESCAPE,
'/^(\")/' => self::T_QUOTE,
'/^(\s+)/' => self::T_WHITESPACE,
'/^(OR)/' => self::T_OR,
'/^(AND)/' => self::T_AND,
'/^(\()/' => self::T_OPEN_PARENTHESIS,
'/^(\))/' => self::T_CLOSE_PARENTHESIS,
'/^(~)/' => self::T_TILDE,
'/^([\p{L}\p{S}\p{N}]+)/u' => self::T_WORD,
'/^(\p{P})/u' => self::T_PUNCTUATION,
);
/**
* Analyze a general filter string
*
* @param string $source the filter
* @param bool $onlyToken use it if you only want the lexer token
*
* @return array
* array of filter elements ("mode" => word, string, partial, open_parenthesis, close_parenthesis, and, or, "word" => currentWord)
* or array of token elements ("token" => token type (see $_terminals), "match" => matched string)
*
* @throws LexException
*/
public static function analyze($source, $onlyToken = false)
{
$tokens = array();
$offset = 0;
while ($offset < strlen($source)) {
$result = static::_match($source, $offset);
if ($result === false) {
throw new LexException(sprintf(_("LEX_GENERAL_FILTER:Unable to parse %s"), $source));
}
$tokens[] = $result;
$offset += strlen($result['match']);
}
if ($onlyToken) {
return $tokens;
}else {
return static::_convertToken($tokens);
}
}
/**
* Analyze a fragment of source
*
* @param string $line current line
* @param int $offset offset of the line
* @return array|bool current fragment or false
*/
protected static function _match($line, $offset)
{
$string = substr($line, $offset);
foreach (static::$_terminals as $pattern => $name) {
if (preg_match($pattern, $string, $matches)) {
return array(
'match' => $matches[1],
'token' => $name
);
}
}
return false;
}
/**
* Convert the tokens in filter element
*
* @param $tokens array of token
* @return array array of filter elements
*/
protected static function _convertToken($tokens)
{
// Keys are stored in this array
$keys = array();
// Mode are word, partial, string, false
$currentMode = false;
$inEscape = false;
$currentWord = "";
foreach ($tokens as $value) {
if ($inEscape) {
$currentWord .= $value["match"];
$inEscape = false;
continue;
}
if ($value["token"] === self::T_ESCAPE) {
$inEscape = true;
continue;
}
if ($value["token"] === self::T_QUOTE) {
if ($currentMode === false) {
$currentMode = self::MODE_STRING;
continue;
} else if ($currentMode === self::MODE_STRING) {
$keys[] = array(
"word" => $currentWord,
"mode" => self::MODE_STRING
);
$currentWord = "";
$currentMode = false;
} else {
$currentWord .= $value["match"];
}
}
if ($currentMode === self::MODE_STRING) {
$currentWord .= $value["match"];
continue;
}
if ($value["token"] === self::T_WHITESPACE) {
if ($currentWord !== "") {
$keys[] = array(
"word" => $currentWord,
"mode" => $currentMode ? $currentMode : self::MODE_WORD
);
}
$currentWord = "";
$currentMode = false;
continue;
}
if ($value["token"] === self::T_TILDE) {
if ($currentMode !== self::MODE_PARTIAL) {
$currentMode = self::MODE_PARTIAL;
} else {
$currentWord .= $value["match"];
}
}
if ($value["token"] === self::T_OPEN_PARENTHESIS) {
$keys[] = array("mode" => self::MODE_OPEN_PARENTHESIS);
continue;
}
if ($value["token"] === self::T_CLOSE_PARENTHESIS) {
if ($currentWord !== "") {
$keys[] = array(
"word" => $currentWord,
"mode" => $currentMode ? $currentMode : self::MODE_WORD
);
}
$currentWord = "";
$keys[] = array("mode" => self::MODE_CLOSE_PARENTHESIS);
continue;
}
if ($value["token"] === self::T_OR) {
$keys[] = array("mode" => self::MODE_OR);
continue;
}
if ($value["token"] === self::T_AND) {
$keys[] = array("mode" => self::MODE_AND);
continue;
}
if ($value["token"] === self::T_WORD) {
$currentWord .= $value["match"];
}
if ($value["token"] === self::T_PUNCTUATION) {
$currentWord .= $value["match"];
}
}
if ($currentWord !== "") {
$keys[] = array(
"word" => $currentWord,
"mode" => $currentMode ? $currentMode : self::MODE_WORD
);
}
return $keys;
}
}
class LexException extends \Exception
{
}
var_dump(GeneralFilter::analyze('je suis "Dynacase Platform" un ~fram!!'));