Menu

[r292]: / trunk / src / PHPCheckstyle / Tokenizer.php  Maximize  Restore  History

Download this file

676 lines (594 with data), 17.1 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
<?php
namespace PHPCheckstyle;
use \Exception;
/**
* Lexical Analysis.
*
* 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.
*
* Based on the internal PHP tokenizer but separate the NEW_LINE and the TAB tokens.
*
* @see http://www.php.net/manual/en/tokens.php
* @author Hari Kodungallur <hkodungallur@spikesource.com>
*
*/
class Tokenizer {
/**
* Indicate if the "short_open_tag" is off.
*
* @var Boolean
*/
private $shortOpenTagOff = false;
/**
* The file content.
*
* @var String
*/
public $content = null;
/**
* The array of tokens in a file.
*
* @var Array[TokenInfo]
*/
private $tokens;
/**
* The array of "new" tokens.
*
* @var Array
*/
private $newTokens = array();
/**
* Position of the index in the current file.
*
* @var Integer
*/
private $index = 0;
/**
* Number of lines in the file.
*
* @var Integer
*/
private $lineNumber = 1;
/**
* Number of tokens in the file.
*
* @var Integer
*/
private $tokenNumber = 0;
/**
* Constructor
*/
public function __construct() {
// Detect the php.ini settings
$this->shortOpenTagOff = (ini_get('short_open_tag') == false);
/*if ($this->shortOpenTagOff) {
echo "Warning : The short_open_tag value of your php.ini setting is off, you may want to activate it for correct code analysis";
}*/
$this->reset();
}
/**
* Tokenizes the input php file and stores all the tokens in the
* $this->tokens variable.
*
* @param String $filename
* the line where the token is found
*/
public function tokenize($filename) {
// Read the file
if (filesize($filename)) {
$fp = fopen($filename, "rb");
$this->content = fread($fp, filesize($filename));
fclose($fp);
}
$this->tokens = $this->_getAllTokens($this->content);
}
/**
* Dump the tokens of the file.
*
* @return String
*/
public function dumpTokens() {
$result = "";
foreach ($this->tokens as $token) {
$result .= $token->toString() . PHP_EOL;
}
return $result;
}
/**
* Gets the next token.
*
* In the process moves the index to the next position.
*
* @return TokenInfo
*/
public function getNextToken() {
if ($this->index < (count($this->tokens) - 1)) {
// Increment the index
$this->index ++;
// Return the new token
return $this->tokens[$this->index];
} else {
return false;
}
}
/**
* Return the number of tokens in the file.
*
* @return Integer
*/
public function getTokenNumber() {
return $this->tokenNumber;
}
/**
* Gets the current token.
*
* @return TokenInfo
*/
public function getCurrentToken() {
return $this->tokens[$this->index];
}
/**
* Get the token at a given position.
*
* @param Integer $position
* the position of the token
* @return TokenInfo the token found
*/
public function peekTokenAt($position) {
if ($position < count($this->tokens)) {
return $this->tokens[$position];
} else {
return null;
}
}
/**
* Gives the current position in the tokenizer.
*
* @return current position of the Tokenizer
*/
public function getCurrentPosition() {
return $this->index;
}
/**
* Returns the next token without moving
* the index.
*
* @return TokenInfo if the token is found (and update the line value)
*/
public function peekNextToken() {
if ($this->index < (count($this->tokens) - 1)) {
return $this->tokens[$this->index + 1];
} else {
return null;
}
}
/**
* Peeks at the previous token.
* That is it returns the previous token
* without moving the index.
*
* @return TokenInfo
*/
public function peekPrvsToken() {
if ($this->index > 0) {
return $this->tokens[$this->index - 1];
} else {
return null;
}
}
/**
* Peeks at the next valid token.
* A valid token is one that is neither a whitespace or a comment
*
* @param Integer $startPos
* the start position for the search (if the item on this position is valid, it will be returned)
* @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($startPos = null, $stopOnNewLine = false) {
// define the start position
$pos = $this->getCurrentPosition() + 1; // defaut position for the search
if ($startPos != null) {
$pos = $startPos; // if defined, set the start position
}
// search for the next valid token
$token = null;
$nbTokens = count($this->tokens);
while ($pos < $nbTokens) {
$token = $this->tokens[$pos];
$pos ++;
if ($token->id == T_WHITESPACE || $token->id == T_TAB || $token->id == T_COMMENT || $token->id == T_ML_COMMENT || $token->id == T_DOC_COMMENT) {
continue;
} else if ($token->id == T_NEW_LINE) {
if ($stopOnNewLine) {
break;
} else {
continue;
}
} else {
break;
}
}
return $token;
}
/**
* 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() {
$pos = $this->index - 1;
$token = null;
while ($pos > 0) {
$token = $this->tokens[$pos];
$pos --;
if ($token->id == T_WHITESPACE || $token->id == T_TAB || $token->id == T_COMMENT || $token->id == T_ML_COMMENT || $token->id == T_DOC_COMMENT) {
continue;
} else if ($token->id == T_NEW_LINE) {
continue;
} else {
break;
}
}
return $token;
}
/**
* Resets all local variables
*
* @return
*
* @access public
*/
public function reset() {
$this->index = 0;
$this->tokens = array();
$this->newTokens = array();
$this->tokenNumber = 0;
$this->lineNumber = 1;
}
/**
* Check if a token is equal to a given token ID
*
* @param TokenInfo $token
* the token to test
* @param Integer $id
* the token ID we're looking for
* @param String $text
* (optional) the text we're looking for
* @return Boolean true if the token correspond
*/
public function checkToken($token, $id, $text = false) {
$result = false;
if ($token->id == $id) {
if ($text) {
$result = $token->text == $text;
} else {
$result = true;
}
}
return $result;
}
/**
* Check if the previous valid token (ignoring whitespace) correspond to the specified token.
*
* @param Integer $id
* the token ID we're looking for
* @param String $text
* (optional) the text we're looking for
* @return Boolean true if the token is found
*/
public function checkPreviousValidToken($id, $text = false) {
$token = $this->peekPrvsValidToken();
return $this->checkToken($token, $id, $text);
}
/**
* Check if the previous valid token (ignoring whitespace) correspond to the specified token.
*
* @param Integer $id
* the token ID we're looking for
* @param String $text
* (optional) the text we're looking for
* @return Boolean true if the token is found
*/
public function checkPreviousToken($id, $text = false) {
$token = $this->peekPrvsToken();
return $this->checkToken($token, $id, $text);
}
/**
* Check if a the next token exists (and if its value correspond to what is expected).
*
* @param Integer $id
* the token we're looking for
* @param String $text
* (optional) the text we're looking for
* @return Boolean true if the token is found
*/
public function checkNextToken($id, $text = false) {
$token = $this->peekNextToken();
return $this->checkToken($token, $id, $text);
}
/**
* Check if a the next token exists (and if its value correspond to what is expected).
*
* @param Integer $id
* the token we're looking for
* @param String $text
* (optional) the text we're looking for
* @return Boolean true if the token is found
*/
public function checkCurrentToken($id, $text = false) {
$token = $this->getCurrentToken();
return $this->checkToken($token, $id, $text);
}
/**
* Find the next position of the string.
*
* @param String $text
* the text we're looking for
* @param Integer $apos
* the position to start from (by defaut will use current position)
* @return Integer the position, null if not found
*/
public function findNextStringPosition($text, $apos = null) {
if ($apos == null) {
$pos = $this->getCurrentPosition();
} else {
$pos = $apos;
}
$pos += 1; // Start from the token following the current position
$nbTokens = count($this->tokens);
while ($pos < $nbTokens) {
$token = $this->tokens[$pos];
if ($text == $token->text) {
return $pos;
}
$pos ++;
}
return null;
}
/**
* Check if the next valid token (ignoring whitespaces) correspond to the specified token.
*
* @param Integer $id
* the id of the token we're looking for
* @param String $text
* the text we're looking for
* @param Integer $startPos
* the start position
* @return true if the token is found
*/
public function checkNextValidToken($id, $text = false, $startPos = null) {
$tokenInfo = $this->peekNextValidToken($startPos);
if ($tokenInfo != null) {
return $this->checkToken($tokenInfo, $id, $text);
} else {
return false;
}
}
/**
* Identify the token and enventually split the new lines.
*
* @param String $tokenText
* The token text
* @param Integer $tokenText
* The token text
* @return an array of tokens
*/
private function _identifyTokens($tokenText, $tokenID) {
// Split the data up by newlines
// To correctly handle T_NEW_LINE inside comments and HTML
$splitData = preg_split('#(\r\n|\n|\r)#', $tokenText, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ($splitData as $data) {
$tokenInfo = new TokenInfo();
$tokenInfo->text = $data;
$tokenInfo->position = $this->tokenNumber;
$tokenInfo->line = $this->lineNumber;
if ($data == "\r\n" || $data == "\n" || $data == "\r") {
// This is a new line token
$tokenInfo->id = T_NEW_LINE;
$this->lineNumber ++;
} else if ($data == "\t") {
// This is a tab token
$tokenInfo->id = T_TAB;
} else {
// Any other token
$tokenInfo->id = $tokenID;
}
$this->tokenNumber ++;
// Added detections
if ($tokenInfo->id == T_UNKNOWN) {
switch ($tokenInfo->text) {
case ";":
$tokenInfo->id = T_SEMICOLON;
break;
case "{":
$tokenInfo->id = T_BRACES_OPEN;
break;
case "}":
$tokenInfo->id = T_BRACES_CLOSE;
break;
case "(":
$tokenInfo->id = T_PARENTHESIS_OPEN;
break;
case ")":
$tokenInfo->id = T_PARENTHESIS_CLOSE;
break;
case ",":
$tokenInfo->id = T_COMMA;
break;
case "=":
$tokenInfo->id = T_EQUAL;
break;
case ".":
$tokenInfo->id = T_CONCAT;
break;
case ":":
$tokenInfo->id = T_COLON;
break;
case "-":
$tokenInfo->id = T_MINUS;
break;
case "+":
$tokenInfo->id = T_PLUS;
break;
case ">":
$tokenInfo->id = T_IS_GREATER;
break;
case "<":
$tokenInfo->id = T_IS_SMALLER;
break;
case "*":
$tokenInfo->id = T_MULTIPLY;
break;
case "/":
$tokenInfo->id = T_DIVIDE;
break;
case "?":
$tokenInfo->id = T_QUESTION_MARK;
break;
case "%":
$tokenInfo->id = T_MODULO;
break;
case "!":
$tokenInfo->id = T_EXCLAMATION_MARK;
break;
case "&":
$tokenInfo->id = T_AMPERSAND;
break;
case "[":
$tokenInfo->id = T_SQUARE_BRACKET_OPEN;
break;
case "]":
$tokenInfo->id = T_SQUARE_BRACKET_CLOSE;
break;
case "@":
$tokenInfo->id = T_AROBAS;
break;
case '"':
$tokenInfo->id = T_QUOTE;
break;
default:
}
}
$this->newTokens[] = $tokenInfo;
}
return $this->newTokens;
}
/**
* Tokenize a string and separate the newline token.
*
* Found here : http://php.net/manual/function.token-get-all.php
*
* @param String $source
* The source code to analyse
* @return array
*/
private function _getAllTokens($source) {
$newTokens = array();
// Ugly trick
// Reset the error array by calling an undefined variable
set_error_handler('var_dump', 0);
@$errorGetLastResetUndefinedVariable;
restore_error_handler();
// Get the tokens
$tokens = @token_get_all($source);
// Check for parsing errors
$parsingErrors = error_get_last();
if (!empty($parsingErrors) && $parsingErrors["type"] == 128) {
throw new Exception($parsingErrors["message"]);
}
// Check each token and transform into an Object
foreach ($tokens as $token) {
$isTokenArray = is_array($token);
$tokenID = $isTokenArray ? $token[0] : T_UNKNOWN;
$tokenText = $isTokenArray ? $token[1] : $token;
// Manage T_OPEN_TAG when php.ini setting short_open_tag is Off.
if ($this->shortOpenTagOff && $tokenID == T_INLINE_HTML) {
$startPos = strpos($tokenText, SHORT_OPEN_TAG);
$endPos = strpos($tokenText, CLOSE_TAG, $startPos + strlen(SHORT_OPEN_TAG));
// Extract the content of the short_open_tag
while (strlen($tokenText) > 2 && $startPos !== false && $endPos !== false) {
// Parse the beginning of the text
$beforeText = substr($tokenText, 0, $startPos);
$this->_identifyTokens($beforeText, $tokenID);
// The open tag
$openTag = new TokenInfo();
$openTag->id = T_OPEN_TAG;
$openTag->text = SHORT_OPEN_TAG;
$this->tokenNumber++;
$openTag->position = $this->tokenNumber;
$openTag->line = $this->lineNumber;
$newTokens[] = $openTag;
// Tokenize the content
$inlineText = substr($tokenText, $startPos + strlen(SHORT_OPEN_TAG), $endPos - $startPos);
$inlineText = substr($inlineText, 0, -strlen(CLOSE_TAG));
$inline = $this->_getAllTokens(OPEN_TAG . " " . $inlineText);
array_shift($inline); // remove <?php
$newTokens = array_merge($newTokens, $inline);
// Add the close tag
$closeTag = new TokenInfo();
$closeTag->id = T_CLOSE_TAG;
$closeTag->text = CLOSE_TAG;
$this->tokenNumber++;
$closeTag->position = $this->tokenNumber;
$closeTag->line = $this->lineNumber;
$newTokens[] = $closeTag;
// text = the remaining text
$tokenText = substr($tokenText, $endPos + strlen(SHORT_OPEN_TAG));
$startPos = strpos($tokenText, SHORT_OPEN_TAG);
$endPos = strpos($tokenText, CLOSE_TAG, $startPos + strlen(SHORT_OPEN_TAG));
}
}
// Identify the tokens
$this->_identifyTokens($tokenText, $tokenID);
}
return $this->newTokens;
}
/**
* Find the position of the closing parenthesis corresponding to the current position opening parenthesis.
*
* @param Integer $startPos
* @return Integer $closing position
*/
public function findClosingParenthesisPosition($startPos) {
// Find the opening parenthesis after current position
$pos = $this->findNextStringPosition('(', $startPos);
$parenthesisCount = 1;
$pos += 1; // Skip the opening parenthesis
$nbTokens = count($this->tokens);
while ($parenthesisCount > 0 && $pos < $nbTokens) {
// Look for the next token
$token = $this->peekTokenAt($pos);
// Increment or decrement the parenthesis count
if ($token->id == T_PARENTHESIS_OPEN) {
$parenthesisCount += 1;
} else if ($token->id == T_PARENTHESIS_CLOSE) {
$parenthesisCount -= 1;
}
// Increment the position
$pos += 1;
}
return $pos - 1;
}
/**
* Checks if a token is in the type of token list.
*
* @param TokenInfo $tokenToCheck
* the token to check.
* @param Array[Integer] $tokenList
* an array of token ids, e.g. T_NEW_LINE, T_DOC_COMMENT, etc.
* @return Boolean true if the token is found, false if it is not.
*/
public function isTokenInList($tokenToCheck, $tokenList) {
foreach ($tokenList as $tokenInList) {
if ($this->checkToken($tokenToCheck, $tokenInList)) {
return true;
}
}
return false;
}
}
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.