Menu

[r13]: / trunk / PhpCheckstyle / src / TokenUtils.php  Maximize  Restore  History

Download this file

449 lines (412 with data), 10.5 kB

  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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/*
* $Id: TokenUtils.php 28215 2005-07-28 02:53:05Z hkodungallur $
*
* Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Source License version 2.1
* (See http://www.spikesource.com/license.html)
*
* Lexical Analysis.
*/
if (!defined("T_ML_COMMENT")) {
define("T_ML_COMMENT", T_COMMENT);
}
define('T_NEW_LINE', -1);
/**
* Class that stores the tokens for a particular class and provide
* utility functions like getting the next/previous token,
* checking whether the token is of particular type etc.
*
* @see http://www.php.net/manual/en/tokens.php
* @author Hari Kodungallur <hkodungallur@spikesource.com>
*/
class TokenUtils {
// Variables
private $fileRef;
private $tokens;
private $totalNumTokens;
private $curTokenNumber;
/**
* Constructor
*/
public function TokenUtils() {
$this->reset();
}
/**
* Tokenizes the input php file and stores all the tokens in the
* $this->tokens variable.
*
* @param &$line the line where the token is found
* @return true if the token is found (and update the line value)
*/
public function tokenize($filename) {
$contents = "";
if (filesize($filename)) {
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);
}
$this->tokens = $this->_getAllTokens($contents);
$this->totalNumTokens = count($this->tokens);
return $this->totalNumTokens;
}
/**
* Gets the next token.
*
* In the process moves the index to the next position.
*
* @return the token found
*/
public function getNextToken() {
$ret = false;
if ($this->curTokenNumber < $this->totalNumTokens) {
$ret = $this->tokens[$this->curTokenNumber];
$this->curTokenNumber++;
}
return $ret;
}
/**
* Peeks the token at a given position.
*
* @param $position the position of the token
* @return the token found
*/
public function peekTokenAt($position) {
return $this->tokens[$position];
}
/**
* Gives the current position in the tokenizer.
*
* @return current position of the Tokenizer
*/
public function getCurrentPosition() {
return $this->curTokenNumber;
}
/**
* Peeks the next token, i.e., returns the next token without moving
* the index.
*
* @return true if the token is found (and update the line value)
*/
public function peekNextToken() {
$ret = false;
if ($this->curTokenNumber < $this->totalNumTokens) {
$ret = $this->tokens[$this->curTokenNumber];
}
return $ret;
}
/**
* Peeks at the previous token. That is it returns the previous token
* without moving the index
*
* @return true if the token is found (and update the line value)
*/
public function peekPrvsToken() {
$ret = false;
if ($this->curTokenNumber > 1) {
$ret = $this->tokens[$this->curTokenNumber - 2];
}
return $ret;
}
/**
* Peeks at the next valid token.
* A valid token is one that is neither a whitespace or a comment
*
* @param Integer $tokenNumber the start position for the search
* @param Boolean $stopOnNewLine Indicate if we need to stop when we meet a new line
* @return TokenInfo the info about the token found
*/
public function peekNextValidToken($tokenNumber = null, $stopOnNewLine = false) {
$ret = false;
$lineOffset = 0;
$tmpTokenNumber = $this->curTokenNumber;
if ($tokenNumber != null) {
$tmpTokenNumber = $tokenNumber;
}
while ($tmpTokenNumber < $this->totalNumTokens) {
$ret = $this->tokens[$tmpTokenNumber];
$tmpTokenNumber++;
if (is_array($ret)) {
list($k, $v) = $ret;
if ($k == T_WHITESPACE || $k == T_COMMENT || $k == T_ML_COMMENT || $k == T_DOC_COMMENT) {
continue;
} else if ($k == T_NEW_LINE) {
$lineOffset++; // increment the line number when a new line is found
if ($stopOnNewLine) {
break;
} else {
continue;
}
} else {
break;
}
} else {
break;
}
}
if ($ret) {
$result = new TokenInfo();
$result->token = $ret;
$result->position = $tmpTokenNumber;
$result->lineOffset = $lineOffset;
return $result;
} else {
return null;
}
}
/**
* Peeks at the previous valid token.
* A valid token is one that is neither a whitespace or a comment
*
* @return TokenInfo the info about the token found
*/
public function peekPrvsValidToken() {
$ret = false;
$tmpTokenNumber = $this->curTokenNumber - 2;
$lineOffset = 0;
while ($tmpTokenNumber > 0) {
$ret = $this->tokens[$tmpTokenNumber];
$tmpTokenNumber--;
if (is_array($ret)) {
list($k, $v) = $ret;
if ($k == T_WHITESPACE || $k == T_COMMENT || $k == T_ML_COMMENT || $k == T_DOC_COMMENT) {
continue;
} else if ($k == T_NEW_LINE) {
$lineOffset--; // decrement the line number when a new line is found
continue;
} else {
break;
}
} else {
break;
}
}
if ($ret) {
$result = new TokenInfo();
$result->token = $ret;
$result->position = $tmpTokenNumber;
$result->lineOffset = $lineOffset;
return $result;
} else {
return null;
}
}
/**
* Resets all local variables
*
* @return
* @access public
*/
public function reset() {
$this->fileRef = false;
$this->curTokenNumber = 0;
$this->totalNumTokens = 0;
$this->tokens = array();
}
/**
* Check if a token is equal to a given token ID
*
* @param $token the token to test
* @param $value the token ID we're looking for
* @param $text (optional) the text we're looking for
* @return true if the token correspond
*/
public function checkProvidedToken($token, $value, $text = false) {
$ret = false;
if (is_array($token)) {
list($k, $v) = $token;
if ($k == $value) {
if ($text) {
if ($v == $text) {
$ret = true;
}
} else {
$ret = true;
}
}
}
return $ret;
}
/**
* Check if the previous valid token (ignoring whitespace) correspond to the specified token.
*
* @param $value the token ID we're looking for
* @param $text (optional) the text we're looking for
* @return true if the token is found
*/
public function checkPreviousValidToken($value, $text = false) {
$ret = false;
$retInfo = $this->peekPrvsValidToken();
$token = $retInfo->token;
if (is_array($token)) {
list($k, $v) = $token;
if ($k == $value) {
if ($text) {
if ($v == $text) {
$ret = true;
}
} else {
$ret = true;
}
}
}
return $ret;
}
/**
* Check if a the next token exists (and if its value correspond to what is expected).
*
* @param String $text (optional) the text we're looking for
* @return true if the token is found
*/
public function checkNextToken($value, $text = false) {
$ret = false;
$token = $this->peekNextToken();
if (is_array($token)) { // Case of a real token
list($k, $v) = $token;
if ($k == $value) {
if ($text) {
if ($v == $text) {
$ret = true;
}
} else {
$ret = true;
}
}
}
return $ret;
}
/**
* Check if the next token (including whitespaces) correspond to the text.
*
* @param String $text the text we're looking for
* @return true if the token is found
*/
public function checkNextTextToken($text) {
$ret = false;
$token = $this->peekNextToken();
if (is_string($token)) {
if ($token == $text) {
$ret = true;
}
}
return $ret;
}
/**
* Check if the next valid token (ignoring whitespaces) correspond to the text.
*
* @param String $text the text we're looking for
* @return true if the token is found
*/
public function checkNextValidTextToken($text) {
$ret = false;
$retInfo = $this->peekNextValidToken();
$token = $retInfo->token;
if (is_string($token)) {
if ($token == $text) {
$ret = true;
}
}
return $ret;
}
/**
* Check if the next valid token (ignoring whitespaces) correspond to the specified token.
*
* @param Integer $value the value of the token we're looking for
* @param String $text the text we're looking for
* @return true if the token is found
*/
public function checkNextValidToken($value, $text = false) {
$ret = false;
$retInfo = $this->peekNextValidToken();
$token = $retInfo->token;
if (is_array($token)) {
list($k, $v) = $token;
if ($k == $value) {
if ($text) {
if ($v == $text) {
$ret = true;
}
} else {
$ret = true;
}
}
}
return $ret;
}
/**
* Check if the token correspond to a given text
*
* @param $token the token
* @param $text the text
* @return true if the token contains the text
*/
public function checkProvidedText($token, $text) {
$ret = false;
if (is_string($token)) {
if ($token == $text) {
$ret = true;
}
}
return $ret;
}
/**
* Extract the text contained in a token.
*
* @param $token the token
* @return the text content of the token
*/
public function extractTokenText($token) {
$ret = $token;
if (is_array($token)) {
$ret = $token[1];
}
return $ret;
}
/**
* Tokenize a string and separate the newline token.
*
* Found here : http://php.net/manual/function.token-get-all.php
*
* @param source The source code to analyse
* @return an array of tokens
*/
private function _getAllTokens($source) {
$newTokens = array();
// Get the tokens
$tokens = token_get_all($source);
// Split newlines into their own tokens
foreach ($tokens as $token) {
$tokenName = is_array($token) ? $token[0] : null;
$tokenData = is_array($token) ? $token[1] : $token;
// Do not split encapsed strings or multiline comments
if ($tokenName == T_CONSTANT_ENCAPSED_STRING || substr($tokenData, 0, 2) == '/*') {
$newTokens[] = array($tokenName, $tokenData);
continue;
}
// Split the data up by newlines
$splitData = preg_split('#(\r\n|\n)#', $tokenData, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ($splitData as $data) {
if ($data == "\r\n" || $data == "\n") {
// This is a new line token
$newTokens[] = array(T_NEW_LINE, $data);
} else {
// Add the token under the original token name
$newTokens[] = is_array($token) ? array($tokenName, $data) : $data;
}
}
}
return $newTokens;
}
/**
* Return the name of a token, including the NEW_LINE one.
*
* @param $token a token
* @return the name of the token
*/
public function getTokenName($token) {
if ($token === T_NEW_LINE) {
return 'T_NEW_LINE';
}
return token_name($token);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.