2011-04-18 19:02:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Lexer
|
|
|
|
{
|
|
|
|
protected $tokens;
|
|
|
|
protected $pos;
|
|
|
|
|
|
|
|
private static $tokenMap;
|
|
|
|
private static $dropTokens = array(
|
|
|
|
T_WHITESPACE => 1, T_COMMENT => 1, T_DOC_COMMENT => 1, T_OPEN_TAG => 1
|
|
|
|
);
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Creates a Lexer.
|
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
*/
|
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;
|
|
|
|
|
|
|
|
$this->tokens = @token_get_all($code);
|
2011-04-18 19:02:30 +02:00
|
|
|
$this->pos = -1;
|
2011-06-03 17:44:23 +02:00
|
|
|
|
|
|
|
$error = error_get_last();
|
|
|
|
|
|
|
|
if (preg_match(
|
|
|
|
'~^(Unterminated comment) starting line ([0-9]+)$~',
|
|
|
|
$error['message'],
|
|
|
|
$matches
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new ParseErrorException($matches[1], $matches[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match(
|
|
|
|
'~^(Unexpected character in input:\s+\'(.)\' \(ASCII=[0-9]+\))~s',
|
|
|
|
$error['message'],
|
|
|
|
$matches
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new ParseErrorException($matches[1]);
|
|
|
|
}
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Returns the next token id.
|
|
|
|
*
|
2011-06-01 22:37:10 +02:00
|
|
|
* @param mixed $lVal Variable to store token content in
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return int Token id
|
|
|
|
*/
|
|
|
|
public function lex(&$lVal) {
|
2011-04-18 19:02:30 +02:00
|
|
|
while (isset($this->tokens[++$this->pos])) {
|
|
|
|
$token = $this->tokens[$this->pos];
|
|
|
|
if (is_string($token)) {
|
2011-05-31 16:33:11 +02:00
|
|
|
$lVal = $token;
|
2011-04-18 19:02:30 +02:00
|
|
|
return ord($token);
|
|
|
|
} elseif (!isset(self::$dropTokens[$token[0]])) {
|
2011-05-31 16:33:11 +02:00
|
|
|
$lVal = $token[1];
|
2011-04-18 19:02:30 +02:00
|
|
|
return self::$tokenMap[$token[0]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the line the current token is in.
|
|
|
|
*
|
2011-06-01 22:37:10 +02:00
|
|
|
* @return int Line current token is in
|
2011-04-18 19:02:30 +02:00
|
|
|
*/
|
|
|
|
public function getLine() {
|
2011-05-27 18:20:44 +02:00
|
|
|
for ($i = $this->pos - 1; $i--;) {
|
|
|
|
if (is_array($this->tokens[$i])) {
|
|
|
|
return $this->tokens[$i][2];
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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-04-18 19:02:30 +02:00
|
|
|
private static function initTokenMap() {
|
|
|
|
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) {
|
|
|
|
self::$tokenMap[$i] = Parser::T_PAAMAYIM_NEKUDOTAYIM;
|
|
|
|
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
|
|
|
|
} elseif(T_OPEN_TAG_WITH_ECHO === $i) {
|
|
|
|
self::$tokenMap[$i] = Parser::T_ECHO;
|
|
|
|
// T_CLOSE_TAG is equivalent to ';'
|
|
|
|
} elseif(T_CLOSE_TAG === $i) {
|
|
|
|
self::$tokenMap[$i] = ord(';');
|
|
|
|
// and the others can be mapped directly
|
|
|
|
} elseif ('UNKNOWN' !== ($name = token_name($i))) {
|
|
|
|
self::$tokenMap[$i] = constant('Parser::' . $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|