Menu

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

Download this file

410 lines (383 with data), 15.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
<?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 $inPHP = false;
private $phpStarters = array('<?php', '<?', '<?=');
private $phpFinisher = '?>';
private $inComment = false;
private $lastLineEndTokenType = "";
// 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
);
/*}}}*/
/*{{{ 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
$this->lineType = LINE_TYPE_NOEXEC;
$line = trim($line);
$parseLine = $line;
$artificialStart = false;
$artificialEnd = false;
// If we are not inside PHP opening tag
if(!$this->inPHP) {
$pos = -1;
// Confirm that the line does not have T_OPEN_TAG_WITH_ECHO (< ? =)
if(strpos($line, $this->phpStarters[2]) === false) {
// If the line has PHP start tag of the first kind
if(($pos = strpos($line, $this->phpStarters[0])) !== false) {
$pos = $pos + strlen($this->phpStarters[0]);
}
// if the line has PHP start tag of the second kind.
else if(($pos = strpos($line, $this->phpStarters[1])) !== false) {
$pos = $pos + strlen($this->phpStarters[1]);
}
// $pos now points to the character after opening tag
if($pos > 0) {
$this->inPHP = true;
//echo "Going in PHP\n";
// Remove the part of the line till the PHP opening
// tag and recurse
return $this->processLine(trim(substr($line, $pos)));
}
}
}
// If we are already in PHP
else if($this->inPHP) {
// If we are inside a multi-line comment, that is not ending
// on the same line
if((strpos($line, "/*") !== false &&
strpos($line, "*/") === false) ||
(strpos($line, "/*") > strpos($line, "*/"))) {
$this->inComment = true;
}
if($this->inComment) {
// Do we need to append an artificial comment start?
// (otherwise the tokenizer might throw error.
if(strpos($line, "/*") === false) {
$line = "/*" . $line;
$artificialStart = true;
}
// Do we need to append an artificial comment end?
if(strpos($line, "*/") === false) {
$line = $line . "*/";
$artificialEnd = true;
}
}
// Since we are inside php, append php opening and closing tags
// to prevent tokenizer from mis-interpreting the line
$parseLine = "<?php " . $line . " ?>";
}
// Tokenize
$tokens = @token_get_all($parseLine);
$this->logger->debug("inPHP? " . $this->inPHP . "\nLine:" . $parseLine,
__FILE__, __LINE__);
$this->logger->debug(print_r($tokens, true), __FILE__, __LINE__);
$seenEnough = false;
$seeMore = false;
$tokenCnt = 0; //tokens in this line
if($this->isContinuation($this->lastLineEndTokenType)) {
$this->lineType = LINE_TYPE_CONT;
$this->logger->debug("Continuation !", __FILE__, __LINE__);
}
foreach($tokens as $token) {
$tokenCnt ++;
if($this->inPHP) {
if($tokenCnt == 2) {
if($this->isContinuation($token)) {
$this->lineType = LINE_TYPE_CONT;
$this->logger->debug("Continuation! Token: $token",
__FILE__, __LINE__);
break;
}
}
}
if(is_string($token)) {
// FIXME: Add more cases, if needed
switch($token) {
// Any of these things, are non-executable.
case '{':
case '}':
case '(':
case ')':
case ';':
if($this->lineType != LINE_TYPE_EXEC) {
$this->lineType = LINE_TYPE_NOEXEC;
}
break;
// Everything else by default is executable.
default:
$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) {
// If it is a comment end or start, set the correct flag
// If we have put the start or end artificially, ignore!
case T_COMMENT:
case T_DOC_COMMENT:
if(strpos($text, "/*") !== false && !$artificialStart) {
$this->inComment = true;
}
if(strpos($text, "*/") !== false && !$artificialEnd) {
$this->inComment = false;
}
case T_WHITESPACE: // white space
case T_CLOSE_TAG: // ? >
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"
// Only if decision is not already made
// mark this non-executable.
if($this->lineType != LINE_TYPE_EXEC) {
$this->lineType = LINE_TYPE_NOEXEC;
}
break;
case T_PRIVATE: // private
case T_PUBLIC: // public
case T_PROTECTED: // protected
case T_VAR: // var
case T_FUNCTION: // function
case T_CLASS: // class
case T_REQUIRE: // require
case T_REQUIRE_ONCE: // require_once
case T_INCLUDE: // include
case T_INCLUDE_ONCE: // include_once
case T_ARRAY: // array
$this->lineType = LINE_TYPE_NOEXEC;
// No need to see any further
$seenEnough = true;
break;
case T_VARIABLE: // $foo
$seeMore = true;
$this->lineType = LINE_TYPE_EXEC;
break;
default:
$seeMore = false;
$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__);
}
break;
}
} // end foreach
$this->logger->debug("Line Type: " . $this->getLineTypeStr($this->lineType),
__FILE__, __LINE__);
if($this->inPHP) {
$this->lastLineEndTokenType = $this->getLastTokenType($tokens);
}
$this->logger->debug("Last End Token: " . $this->lastLineEndTokenType,
__FILE__, __LINE__);
if($this->inPHP) {
// Check if PHP block ends on this line
if(($pos = strpos($line, $this->phpFinisher)) !== false) {
$this->inPHP = false;
// If line is not executable so far, check for the
// remaining part
if($this->lineType != LINE_TYPE_EXEC) {
return $this->processLine(trim(substr($line, $pos+2)));
}
}
}
}
/*}}}*/
/*{{{ 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) {
if(is_string($token)) {
switch($token) {
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;
}
}
/*}}}*/
/*{{{*/
/**
* Return the type of last non-empty token in a line
*
* @param &$tokens Array of tokens for a line
* @return mixed Last non-empty token type (or token) if exists; false otherwise
* @access protected
*/
protected function getLastTokenType(&$tokens) {
for($i = count($tokens)-2; $i > 0; $i--) {
if(empty($tokens[$i])) {
continue;
}
if(is_string($tokens[$i])) {
return $tokens[$i];
}
else {
list($tokenType, $text) = $tokens[$i];
if($tokenType != T_WHITESPACE && $tokenType != T_EMPTY) {
return $tokenType;
}
}
}
return false;
}
/*}}}*/
/*
// 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.