2011-04-18 19:02:30 +02:00
|
|
|
<?php
|
|
|
|
|
2011-06-05 18:40:04 +02:00
|
|
|
class PHPParser_Lexer
|
2011-04-18 19:02:30 +02:00
|
|
|
{
|
2011-06-03 22:02:02 +02:00
|
|
|
protected $code;
|
2011-04-18 19:02:30 +02:00
|
|
|
protected $tokens;
|
|
|
|
protected $pos;
|
2011-06-12 17:12:47 +02:00
|
|
|
protected $line;
|
2011-04-18 19:02:30 +02:00
|
|
|
|
2011-07-13 12:24:10 +02:00
|
|
|
protected static $tokenMap;
|
|
|
|
protected static $dropTokens = array(
|
2011-07-03 16:35:45 +02:00
|
|
|
T_WHITESPACE => 1, T_COMMENT => 1, T_OPEN_TAG => 1
|
2011-04-18 19:02:30 +02:00
|
|
|
);
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Creates a Lexer.
|
|
|
|
*
|
|
|
|
* @param string $code
|
2011-07-13 23:07:05 +02:00
|
|
|
*
|
|
|
|
* @throws PHPParser_Error on lexing errors (unterminated comment or unexpected character)
|
2011-05-31 16:33:11 +02:00
|
|
|
*/
|
2011-04-18 19:02:30 +02:00
|
|
|
public function __construct($code) {
|
|
|
|
self::initTokenMap();
|
|
|
|
|
2011-06-03 17:44:23 +02:00
|
|
|
// Reset the error message in error_get_last()
|
|
|
|
// Still hoping for a better solution to be found.
|
|
|
|
@$errorGetLastResetUndefinedVariable;
|
|
|
|
|
2011-06-03 22:02:02 +02:00
|
|
|
$this->code = $code;
|
2011-06-03 17:44:23 +02:00
|
|
|
$this->tokens = @token_get_all($code);
|
2011-04-18 19:02:30 +02:00
|
|
|
$this->pos = -1;
|
2011-07-13 13:27:14 +02:00
|
|
|
$this->line = 1;
|
2011-06-03 17:44:23 +02:00
|
|
|
|
|
|
|
$error = error_get_last();
|
|
|
|
|
|
|
|
if (preg_match(
|
2011-07-13 12:24:10 +02:00
|
|
|
'~^Unterminated comment starting line ([0-9]+)$~',
|
2011-06-03 17:44:23 +02:00
|
|
|
$error['message'],
|
|
|
|
$matches
|
|
|
|
)
|
|
|
|
) {
|
2011-07-13 12:24:10 +02:00
|
|
|
throw new PHPParser_Error('Unterminated comment', $matches[1]);
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match(
|
2011-07-13 12:24:10 +02:00
|
|
|
'~^Unexpected character in input: \'(.)\' \(ASCII=([0-9]+)\)~s',
|
2011-06-03 17:44:23 +02:00
|
|
|
$error['message'],
|
|
|
|
$matches
|
|
|
|
)
|
|
|
|
) {
|
2011-07-13 12:24:10 +02:00
|
|
|
throw new PHPParser_Error(sprintf(
|
|
|
|
'Unexpected character "%s" (ASCII %d)',
|
|
|
|
$matches[1], $matches[2]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// PHP cuts error message after null byte, so need special case
|
|
|
|
if (preg_match('~^Unexpected character in input: \'$~', $error['message'])) {
|
|
|
|
throw new PHPParser_Error('Unexpected null byte');
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Returns the next token id.
|
|
|
|
*
|
2011-07-03 16:35:45 +02:00
|
|
|
* @param mixed $value Variable to store token content in
|
|
|
|
* @param mixed $line Variable to store line in
|
|
|
|
* @param mixed $docComment Variable to store doc comment in
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return int Token id
|
|
|
|
*/
|
2011-07-13 12:24:10 +02:00
|
|
|
public function lex(&$value = null, &$line = null, &$docComment = null) {
|
2011-07-03 16:35:45 +02:00
|
|
|
$docComment = null;
|
|
|
|
|
2011-04-18 19:02:30 +02:00
|
|
|
while (isset($this->tokens[++$this->pos])) {
|
|
|
|
$token = $this->tokens[$this->pos];
|
2011-06-03 22:02:02 +02:00
|
|
|
|
2011-04-18 19:02:30 +02:00
|
|
|
if (is_string($token)) {
|
2011-06-12 17:12:47 +02:00
|
|
|
$value = $token;
|
|
|
|
$line = $this->line;
|
2011-04-18 19:02:30 +02:00
|
|
|
return ord($token);
|
2011-07-13 13:27:14 +02:00
|
|
|
} else {
|
|
|
|
$this->line += substr_count($token[1], "\n");
|
|
|
|
|
|
|
|
if (T_DOC_COMMENT === $token[0]) {
|
|
|
|
$docComment = $token[1];
|
|
|
|
} elseif (!isset(self::$dropTokens[$token[0]])) {
|
|
|
|
$value = $token[1];
|
|
|
|
$line = $token[2];
|
|
|
|
return self::$tokenMap[$token[0]];
|
|
|
|
}
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:02:02 +02:00
|
|
|
/**
|
|
|
|
* Handles __halt_compiler() by returning the text after it.
|
|
|
|
*
|
|
|
|
* @return string Remaining text
|
|
|
|
*/
|
|
|
|
public function handleHaltCompiler() {
|
|
|
|
// get the length of the text before the T_HALT_COMPILER token
|
|
|
|
$textBefore = '';
|
|
|
|
for ($i = 0; $i <= $this->pos; ++$i) {
|
|
|
|
if (is_string($this->tokens[$i])) {
|
|
|
|
$textBefore .= $this->tokens[$i];
|
|
|
|
} else {
|
|
|
|
$textBefore .= $this->tokens[$i][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// text after T_HALT_COMPILER, still including ();
|
|
|
|
$textAfter = substr($this->code, strlen($textBefore));
|
|
|
|
|
|
|
|
// ensure that it is followed by ();
|
|
|
|
// this simplifies the situation, by not allowing any comments
|
|
|
|
// in between of the tokens.
|
2011-10-18 18:03:28 +02:00
|
|
|
if (!preg_match('~\s*\(\s*\)\s*(?:;|\?>)~', $textAfter, $matches)) {
|
2011-06-05 18:52:41 +02:00
|
|
|
throw new PHPParser_Error('__halt_compiler must be followed by "();"');
|
2011-06-03 22:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// prevent the lexer from returning any further tokens
|
|
|
|
$this->pos = count($this->tokens);
|
|
|
|
|
|
|
|
// return with (); removed
|
|
|
|
return substr($textAfter, strlen($matches[0]));
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Initializes the token map.
|
|
|
|
*
|
|
|
|
* The token map maps the PHP internal token identifiers
|
|
|
|
* to the identifiers used by the Parser. Additionally it
|
|
|
|
* maps T_OPEN_TAG_WITH_ECHO to T_ECHO and T_CLOSE_TAG to ';'.
|
|
|
|
*/
|
2011-07-13 12:24:10 +02:00
|
|
|
protected static function initTokenMap() {
|
2011-04-18 19:02:30 +02:00
|
|
|
if (!self::$tokenMap) {
|
|
|
|
self::$tokenMap = array();
|
|
|
|
|
|
|
|
// 256 is the minimum possible token number, as everything below
|
|
|
|
// it is an ASCII value
|
|
|
|
for ($i = 256; $i < 1000; ++$i) {
|
|
|
|
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
|
|
|
|
if (T_DOUBLE_COLON === $i) {
|
2011-06-05 18:40:04 +02:00
|
|
|
self::$tokenMap[$i] = PHPParser_Parser::T_PAAMAYIM_NEKUDOTAYIM;
|
2011-04-18 19:02:30 +02:00
|
|
|
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
|
|
|
|
} elseif(T_OPEN_TAG_WITH_ECHO === $i) {
|
2011-06-05 18:40:04 +02:00
|
|
|
self::$tokenMap[$i] = PHPParser_Parser::T_ECHO;
|
2011-04-18 19:02:30 +02:00
|
|
|
// T_CLOSE_TAG is equivalent to ';'
|
|
|
|
} elseif(T_CLOSE_TAG === $i) {
|
|
|
|
self::$tokenMap[$i] = ord(';');
|
|
|
|
// and the others can be mapped directly
|
2011-07-04 21:09:20 +02:00
|
|
|
} elseif ('UNKNOWN' !== ($name = token_name($i))
|
|
|
|
&& defined($name = 'PHPParser_Parser::' . $name)
|
|
|
|
) {
|
|
|
|
self::$tokenMap[$i] = constant($name);
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|