Menu

[r21]: / trunk / src / parser / PHPParser.php  Maximize  Restore  History

Download this file

486 lines (462 with data), 18.6 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
<?php
/*
* $Id: PHPParser.php 14665 2005-03-23 19:37:50Z npac $
*
* Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Software License version 2.1
* (See http://www.spikesource.com/license.html)
*/
?>
<?php
if(!defined("__PHPCOVERAGE_HOME")) {
define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__)));
}
require_once __PHPCOVERAGE_HOME . "/parser/Parser.php";
/**
* Parser for PHP files
*
* @author Nimish Pachapurkar (npac@spikesource.com)
* @version $Revision: 14665 $
* @package SpikePHPCoverage_Parser
*/
class PHPParser extends Parser {
/*{{{ Members */
private $inHereDoc = false;
private $inFunction = false;
private $phpStarters = array('<?php', '<?', '<?=');
private $phpFinisher = '?>';
private $inComment = false;
private $lastLineEndTokenType = "";
private $fileToken = null;
private $currFileToken = 0;
private $lineNb = 0;
private $multiLineTok = null;
// If one of these tokens occur as the last token of a line
// then the next line can be treated as a continuation line
// depending on how it starts.
public static $contTypes = array(
"(",
",",
".",
"=",
"+",
"*",
"/",
"-",
")",
T_LOGICAL_XOR,
T_LOGICAL_AND,
T_LOGICAL_OR,
T_PLUS_EQUAL,
T_MINUS_EQUAL,
T_MUL_EQUAL,
T_DIV_EQUAL,
T_CONCAT_EQUAL,
T_MOD_EQUAL,
T_AND_EQUAL,
T_OR_EQUAL,
T_XOR_EQUAL,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
T_OBJECT_OPERATOR,
T_DOUBLE_ARROW,
"[",
"]",
T_LOGICAL_OR,
T_LOGICAL_XOR,
T_LOGICAL_AND,
T_STRING // not very useful, since getTokenType() will return the actual string for all T_STRING tokens
);
/*}}}*/
/*{{{ protected function openFileReadOnly() */
/**
* Opens the file to be parsed in Read-only mode.
* Overriden to tokenize the whole file.
*
* @return FALSE on failure.
* @access protected
*/
protected function openFileReadOnly() {
$this->fileToken = @token_get_all(file_get_contents($this->filename));
return parent::openFileReadOnly();
}
/*}}}*/
/*{{{ protected function getNextToken() */
/**
* Gets the next token.
*
* The same token can be returned several times for multi-line
* tokens.
*
* @param $line The current line read from the file.
* @param &$pos The position of the next token string in the line.
* @return the next token or null at the end of the line.
* @access protected
*/
protected function getNextToken($line, &$pos) {
$lineLen = strlen($line);
if($pos >= $lineLen) {
return null;
}
if($this->multiLineTok != null) {
list($tok, $lnb, $posnl) = $this->multiLineTok;
if(is_string($tok)) {
$str = $tok;
} else {
$str = $tok[1];
}
if($posnl >= strlen($str)) {
$this->multiLineTok = null;
} else {
if(substr($str, $posnl + 1, $lineLen) == $line) {
$pos += $lineLen;
$newPosnl = $posnl + $lineLen;
} else {
$newPosnl = strpos($str, "\n", $posnl + 1);
if($newPosnl === false) {
$newPosnl = strlen($str);
}
$len = $newPosnl - $posnl - 1;
//if(substr($str, $posnl + 1, $len) != substr($line, $pos, $len)) {
//}
$pos += $len;
}
$this->multiLineTok[1]++;
$this->multiLineTok[2] = $newPosnl;
return $tok;
}
}
if(!isset($this->fileToken[$this->currFileToken])) {
return null;
}
$tok = &$this->fileToken[$this->currFileToken];
if(is_string($tok)) {
$str = $tok;
} else {
$str = $tok[1];
}
$nbnl = substr_count($str, "\n");
if($nbnl > 0 && ($posnl = strpos($str, "\n")) != strlen($str) - 1) {
$this->multiLineTok = array($tok, 1, $posnl);
$str = substr($str, 0, $posnl + 1);
}
if(substr($line, $pos, strlen($str)) == $str) {
$this->currFileToken++;
$pos += strlen($str);
return $tok;
} else {
return null;
}
}
/*}}}*/
/*{{{ protected function isMultiLineCont() */
/**
* Determines if the beginning of current line is a
* continuation of an executable multi-line token.
*
* Called at the first token of a line.
*
* @return Boolean true if it is a continuation line
* @access protected
*/
protected function isMultiLineCont() {
if($this->multiLineTok == null
|| $this->multiLineTok[1] <= 1) {
return false;
}
switch ($this->getTokenType($this->multiLineTok[0])) {
case T_COMMENT:
case T_INLINE_HTML: // <br/><b>jhsk</b>
return false;
}
return true;
}
/*}}}*/
/*{{{ protected function processLine() */
/**
* Process a line read from the file and determine if it is an
* executable line or not.
*
* This is the work horse function that does most of the parsing.
* To parse PHP, get_all_tokens() tokenizer function is used.
*
* @param $line Line to be parsed.
* @access protected
*/
protected function processLine($line) {
// Default values
$prevLineType = $this->lineType;
$this->lineType = LINE_TYPE_NOEXEC;
$tokenCnt = 0;
$this->lineNb++;
$pos = 0;
$seeMore = false;
$seenEnough = false;
$lastToken = null;
while (($token = $this->getNextToken($line, $pos))) {
if (!is_string($token)) {
$stoken = token_name($token[0]) . ' "'
. str_replace(array("\\", "\"", "\n", "\r")
, array("\\\\", "\\\"", "\\n", "\\r")
, $token[1]) . '"';
if (isset($token[2])) {
$stoken .= '[' . $token[2] . ']';
if ($token[2] != $this->lineNb) {
$stoken .= ' != [' . $this->lineNb . ']';
}
}
} else {
$stoken = $token;
}
$this->logger->debug("Token $stoken", __FILE__, __LINE__);
if (!is_string($token) && $token[0] == T_WHITESPACE) {
continue;
}
$lastToken = $token;
$tokenCnt ++;
if ($this->inHereDoc) {
$this->lineType = LINE_TYPE_CONT;
$this->logger->debug("Here doc Continuation! Token: $token",
__FILE__, __LINE__);
if ($this->getTokenType($token) == T_END_HEREDOC) {
$this->inHereDoc = false;
}
continue;
}
if ($this->inFunction) {
$this->lineType = LINE_TYPE_NOEXEC;
$this->logger->debug("Function! Token: $token",
__FILE__, __LINE__);
if ($this->getTokenType($token) == '{') {
$this->inFunction = false;
}
continue;
}
if($tokenCnt == 1 && $prevLineType != LINE_TYPE_NOEXEC
&& ($this->isMultiLineCont()
|| $this->isContinuation($token))) {
$this->lineType = LINE_TYPE_CONT;
$this->logger->debug("Continuation! Token: ".print_r($token, true),
__FILE__, __LINE__);
$seenEnough = true;
continue;
}
if ($seenEnough) {
continue;
}
if(is_string($token)) {
// FIXME: Add more cases, if needed
switch($token) {
// Any of these things, are non-executable.
// And so do not change the status of the line
case '{':
case '}':
case '(':
case ')':
case ';':
break;
// Everything else by default is executable.
default:
$this->logger->debug("Other string: $token",
__FILE__, __LINE__);
if($this->lineType == LINE_TYPE_NOEXEC) {
$this->lineType = LINE_TYPE_EXEC;
}
break;
}
$this->logger->debug("Status: " . $this->getLineTypeStr($this->lineType) . "\t\tToken: $token",
__FILE__, __LINE__);
}
else {
// The token is an array
list($tokenType, $text) = $token;
switch($tokenType) {
case T_COMMENT:
case T_DOC_COMMENT:
case T_WHITESPACE: // white space
case T_OPEN_TAG: // < ?
case T_OPEN_TAG_WITH_ECHO: // < ? =
case T_CURLY_OPEN: //
case T_INLINE_HTML: // <br/><b>jhsk</b>
//case T_STRING: //
case T_EXTENDS: // extends
case T_STATIC: // static
case T_STRING_VARNAME: // string varname?
case T_CHARACTER: // character
case T_ELSE: // else
case T_CONSTANT_ENCAPSED_STRING: // "some str"
case T_ARRAY: // array
// Any of these things, are non-executable.
// And so do not change the status of the line
// as the line starts as non-executable.
break;
case T_START_HEREDOC:
$this->inHereDoc = true;
break;
case T_PRIVATE: // private
case T_PUBLIC: // public
case T_PROTECTED: // protected
case T_VAR: // var
case T_GLOBAL: // global
case T_CLASS: // class
case T_INTERFACE: // interface
case T_REQUIRE: // require
case T_REQUIRE_ONCE: // require_once
case T_INCLUDE: // include
case T_INCLUDE_ONCE: // include_once
case T_SWITCH: // switch
case T_CONST: // const
case T_TRY: // try
$this->lineType = LINE_TYPE_NOEXEC;
// No need to see any further
$seenEnough = true;
break;
case T_FUNCTION: // function
$this->lineType = LINE_TYPE_NOEXEC;
$this->inFunction = true;
// No need to see any further
$seenEnough = true;
break;
case T_VARIABLE: // $foo
$seeMore = true;
if($this->lineType == LINE_TYPE_NOEXEC) {
$this->lineType = LINE_TYPE_EXEC;
}
break;
case T_CLOSE_TAG:
if($this->lineType != LINE_TYPE_EXEC) {
$this->lineType = LINE_TYPE_NOEXEC;
}
break;
default:
$this->logger->debug("Other token: " . token_name($tokenType),
__FILE__, __LINE__);
$seeMore = false;
if($this->lineType == LINE_TYPE_NOEXEC) {
$this->lineType = LINE_TYPE_EXEC;
}
break;
}
$this->logger->debug("Status: " . $this->getLineTypeStr($this->lineType) . "\t\tToken type: $tokenType \tText: $text",
__FILE__, __LINE__);
}
if(($this->lineType == LINE_TYPE_EXEC && !$seeMore)
|| $seenEnough) {
$this->logger->debug("Made a decision! Exiting. Token Type: $tokenType & Text: $text",
__FILE__, __LINE__);
if($seenEnough) {
$this->logger->debug("Seen enough at Token Type: $tokenType & Text: $text",
__FILE__, __LINE__);
} else {
$seenEnough = true;
}
}
} // end while
$this->logger->debug("Line Type: " . $this->getLineTypeStr($this->lineType),
__FILE__, __LINE__);
$this->lastLineEndTokenType = $this->getTokenType($lastToken);
$this->logger->debug("Last End Token: " . $this->lastLineEndTokenType,
__FILE__, __LINE__);
}
/*}}}*/
/*{{{ public function getLineType() */
/**
* Returns the type of line just read
*
* @return Line type
* @access public
*/
public function getLineType() {
return $this->lineType;
}
/*}}}*/
/*{{{ protected function isContinuation() */
/**
* Check if a line is a continuation of the previous line
*
* @param &$token Second token in a line (after PHP start)
* @return Boolean True if the line is a continuation; false otherwise
* @access protected
*/
protected function isContinuation(&$token) {
//$this->logger->debug("isContinuation: " . print_r($token, true), __FILE__, __LINE__);
//$this->logger->debug("lastLineEndTokenType: " . print_r($this->lastLineEndTokenType, true), __FILE__, __LINE__);
if(is_string($token)) {
switch($token) {
case ".":
case ",";
case "]":
case "[":
case "(":
case ")":
case "=":
case "+":
case "-":
case "*":
case "/":
return true;
}
}
else {
list($tokenType, $text) = $token;
switch($tokenType) {
case T_CONSTANT_ENCAPSED_STRING:
case T_ARRAY:
case T_DOUBLE_ARROW:
case T_OBJECT_OPERATOR:
case T_LOGICAL_XOR:
case T_LOGICAL_AND:
case T_LOGICAL_OR:
case T_PLUS_EQUAL:
case T_MINUS_EQUAL:
case T_MUL_EQUAL:
case T_DIV_EQUAL:
case T_CONCAT_EQUAL:
case T_MOD_EQUAL:
case T_AND_EQUAL:
case T_OR_EQUAL:
case T_XOR_EQUAL:
case T_BOOLEAN_AND:
case T_BOOLEAN_OR:
case T_LNUMBER:
case T_DNUMBER:
return true;
case T_STRING:
case T_VARIABLE:
return in_array($this->lastLineEndTokenType, PHPParser::$contTypes);
}
}
return false;
}
/*}}}*/
/*{{{ protected function getTokenType() */
/**
* Get the token type of a token (if exists) or
* the token itself.
*
* @param $token Token
* @return Token type or token itself
* @access protected
*/
protected function getTokenType($token) {
if(is_string($token)) {
return $token;
}
else {
list($tokenType, $text) = $token;
return $tokenType;
}
}
/*}}}*/
/*
// Main
$obj = new PHPParser();
$obj->parse("test.php");
while(($line = $obj->getLine()) !== false) {
echo "#########################\n";
echo "[" . $line . "] Type: [" . $obj->getLineTypeStr($obj->getLineType()) . "]\n";
echo "#########################\n";
}
*/
}
?>
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.