Token::isLtOrGt()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Xls;
4
5
class Token
6
{
7
    const TOKEN_ADD = "+";
8
    const TOKEN_SUB = "-";
9
    const TOKEN_MUL = "*";
10
    const TOKEN_DIV = "/";
11
    const TOKEN_OPEN = "(";
12
    const TOKEN_CLOSE = ")";
13
    const TOKEN_COMA = ",";
14
    const TOKEN_SEMICOLON = ";";
15
    const TOKEN_GT = ">";
16
    const TOKEN_LT = "<";
17
    const TOKEN_LE = "<=";
18
    const TOKEN_GE = ">=";
19
    const TOKEN_EQ = "=";
20
    const TOKEN_NE = "<>";
21
    const TOKEN_CONCAT = "&";
22
    const TOKEN_ARG = "arg";
23
24
    protected static $ptgMap = array(
25
        self::TOKEN_MUL => 'ptgMul',
26
        self::TOKEN_DIV => 'ptgDiv',
27
        self::TOKEN_ADD => 'ptgAdd',
28
        self::TOKEN_SUB => 'ptgSub',
29
        self::TOKEN_LT => 'ptgLT',
30
        self::TOKEN_GT => 'ptgGT',
31
        self::TOKEN_LE => 'ptgLE',
32
        self::TOKEN_GE => 'ptgGE',
33
        self::TOKEN_EQ => 'ptgEQ',
34
        self::TOKEN_NE => 'ptgNE',
35
        self::TOKEN_CONCAT => 'ptgConcat',
36
    );
37
38
    protected static $deterministicMap = array(
39
        self::TOKEN_MUL => 1,
40
        self::TOKEN_DIV => 1,
41
        self::TOKEN_ADD => 1,
42
        self::TOKEN_SUB => 1,
43
        self::TOKEN_LE => 1,
44
        self::TOKEN_GE => 1,
45
        self::TOKEN_EQ => 1,
46
        self::TOKEN_NE => 1,
47
        self::TOKEN_CONCAT => 1,
48
        self::TOKEN_COMA => 1,
49
        self::TOKEN_SEMICOLON => 1,
50
        self::TOKEN_OPEN => 1,
51
        self::TOKEN_CLOSE => 1
52
    );
53
54
    protected static $lookaheadMap = array(
55
        self::TOKEN_GT => array('='),
56
        self::TOKEN_LT => array('=', '>'),
57
    );
58
59
    protected static $comparisonTokens = array(
60
        self::TOKEN_LT,
61
        self::TOKEN_GT,
62
        self::TOKEN_LE,
63
        self::TOKEN_GE,
64
        self::TOKEN_EQ,
65
        self::TOKEN_NE
66
    );
67
68
    /**
69
     * Reference A1 or $A$1
70
     * @param $token
71
     *
72
     * @return boolean
73
     */
74
    public static function isReference($token)
75
    {
76
        return preg_match('/^\$?[A-Za-z]+\$?[0-9]+$/', $token) === 1;
77
    }
78
79
    /**
80
     * External reference Sheet1!A1 or Sheet1:Sheet2!A1 or 'Sheet1'!A1 or 'Sheet1:Sheet2'!A1
81
     * @param $token
82
     *
83
     * @return boolean
84
     */
85
    public static function isExternalReference($token)
86
    {
87
        return preg_match("/^\w+(\:\w+)?\![A-za-z]+[0-9]+$/u", $token) === 1
88
            || preg_match("/^'[\w -]+(\:[\w -]+)?'\![A-za-z]+[0-9]+$/u", $token) === 1;
89
    }
90
91
    /**
92
     * @param $token
93
     *
94
     * @return boolean
95
     */
96
    public static function isAnyReference($token)
97
    {
98
        return self::isReference($token) || self::isExternalReference($token);
99
    }
100
101
    /**
102
     * @param $token
103
     *
104
     * @return boolean
105
     */
106
    public static function isAnyRange($token)
107
    {
108
        return self::isRange($token) || self::isExternalRange($token);
109
    }
110
111
    /**
112
     * Range A1:A2 or A1..A2
113
     * @param $token
114
     *
115
     * @return boolean
116
     */
117
    public static function isRange($token)
118
    {
119
        return self::isRangeWithColon($token) || self::isRangeWithDots($token);
120
    }
121
122
    /**
123
     * Range A1:A2 or $A$1:$A$2
124
     * @param $token
125
     *
126
     * @return boolean
127
     */
128
    public static function isRangeWithColon($token)
129
    {
130
        return preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/', $token) === 1;
131
    }
132
133
    /**
134
     * Range A1..A2 or $A$1..$A$2
135
     * @param $token
136
     *
137
     * @return boolean
138
     */
139
    public static function isRangeWithDots($token)
140
    {
141
        return preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/', $token) === 1;
142
    }
143
144
    /**
145
     * External range:
146
     * Sheet1!A1:B2 or Sheet1:Sheet2!A1:B2
147
     * 'Sheet1'!A1:B2 or 'Sheet1:Sheet2'!A1:B2
148
     *
149
     * @param $token
150
     *
151
     * @return boolean
152
     */
153
    public static function isExternalRange($token)
154
    {
155
        // A1:B2
156
        $cellsPattern = '([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+';
157
158
        // Sheet1!A1:B2 or Sheet1:Sheet2!A1:B2
159
        $unquotedSheetsPattern = "/^\w+(\:\w+)?\!{$cellsPattern}$/u";
160
161
        // 'Sheet1'!A1:B2 or 'Sheet1:Sheet2'!A1:B2
162
        $quotedSheetsPattern = "/^'[\w -]+(\:[\w -]+)?'\!{$cellsPattern}$/u";
163
164
        return preg_match($unquotedSheetsPattern, $token) === 1
165
            || preg_match($quotedSheetsPattern, $token) === 1;
166
    }
167
168
    /**
169
     * String
170
     * @param $token
171
     *
172
     * @return boolean
173
     */
174
    public static function isString($token)
175
    {
176
        return preg_match("/^\"[^\"]*\"$/", $token) === 1;
177
    }
178
179
    /**
180
     * @param $token
181
     *
182
     * @return boolean
183
     */
184
    public static function isFunctionCall($token)
185
    {
186
        if (self::isArg($token)) {
187
            return false;
188
        }
189
190
        return preg_match("/^[A-Za-z0-9\xc0-\xdc\.]+$/", $token) === 1;
191
    }
192
193
    /**
194
     * @param $token
195
     *
196
     * @return bool
197
     */
198
    public static function isMulOrDiv($token)
199
    {
200
        return $token == self::TOKEN_MUL || $token == self::TOKEN_DIV;
201
    }
202
203
    /**
204
     * @param $token
205
     *
206
     * @return bool
207
     */
208
    public static function isAddOrSub($token)
209
    {
210
        return $token == self::TOKEN_ADD || $token == self::TOKEN_SUB;
211
    }
212
213
    /**
214
     * @param $token
215
     *
216
     * @return bool
217
     */
218
    public static function isCommaOrSemicolon($token)
219
    {
220
        return $token == self::TOKEN_COMA || $token == self::TOKEN_SEMICOLON;
221
    }
222
223
    /**
224
     * @param $token
225
     *
226
     * @return bool
227
     */
228
    public static function isComparison($token)
229
    {
230
        return in_array($token, self::$comparisonTokens);
231
    }
232
233
    /**
234
     * @param $token
235
     *
236
     * @return bool
237
     */
238
    public static function isLtOrGt($token)
239
    {
240
        return $token == self::TOKEN_LT
241
        || $token == self::TOKEN_GT;
242
    }
243
244
    /**
245
     * @param $token
246
     *
247
     * @return bool
248
     */
249
    public static function isConcat($token)
250
    {
251
        return $token == self::TOKEN_CONCAT;
252
    }
253
254
    /**
255
     * @param $token
256
     *
257
     * @return string|null
258
     */
259
    public static function getPtg($token)
260
    {
261
        if (isset(self::$ptgMap[$token])) {
262
            return self::$ptgMap[$token];
263
        }
264
265
        return null;
266
    }
267
268
    /**
269
     * @param $token
270
     *
271
     * @return bool
272
     */
273
    public static function isDeterministic($token)
274
    {
275
        return isset(self::$deterministicMap[$token]);
276
    }
277
278
    /**
279
     * @param $token
280
     * @param $lookahead
281
     *
282
     * @return bool
283
     */
284
    public static function isPossibleLookahead($token, $lookahead)
285
    {
286
        if (!isset(self::$lookaheadMap[$token])) {
287
            return true;
288
        }
289
290
        return in_array($lookahead, self::$lookaheadMap[$token], true);
291
    }
292
293
    /**
294
     * @param $token
295
     *
296
     * @return bool
297
     */
298
    public static function isArg($token)
299
    {
300
        return $token == self::TOKEN_ARG;
301
    }
302
}
303