2011-12-18 13:04:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ATTENTION: This code is WRITE-ONLY. Do not try to read it.
|
|
|
|
*/
|
|
|
|
class PHPParser_Lexer_Emulative extends PHPParser_Lexer
|
|
|
|
{
|
2012-04-25 20:04:46 +02:00
|
|
|
protected $newKeywords;
|
2011-12-18 13:04:27 +01:00
|
|
|
protected $inObjectAccess;
|
|
|
|
|
2012-04-25 20:04:46 +02:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
2012-09-07 18:06:11 +02:00
|
|
|
$newKeywordsPerVersion = array(
|
|
|
|
'5.5.0-dev' => array(
|
|
|
|
'finally' => PHPParser_Parser::T_FINALLY,
|
2012-09-07 19:24:44 +02:00
|
|
|
'yield' => PHPParser_Parser::T_YIELD,
|
2012-09-07 18:06:11 +02:00
|
|
|
),
|
|
|
|
'5.4.0-dev' => array(
|
|
|
|
'callable' => PHPParser_Parser::T_CALLABLE,
|
|
|
|
'insteadof' => PHPParser_Parser::T_INSTEADOF,
|
|
|
|
'trait' => PHPParser_Parser::T_TRAIT,
|
|
|
|
'__trait__' => PHPParser_Parser::T_TRAIT_C,
|
|
|
|
),
|
|
|
|
'5.3.0-dev' => array(
|
|
|
|
'__dir__' => PHPParser_Parser::T_DIR,
|
|
|
|
'goto' => PHPParser_Parser::T_GOTO,
|
|
|
|
'namespace' => PHPParser_Parser::T_NAMESPACE,
|
|
|
|
'__namespace__' => PHPParser_Parser::T_NS_C,
|
|
|
|
),
|
2012-04-25 20:04:46 +02:00
|
|
|
);
|
|
|
|
|
2012-09-07 18:06:11 +02:00
|
|
|
$this->newKeywords = array();
|
|
|
|
foreach ($newKeywordsPerVersion as $version => $newKeywords) {
|
|
|
|
if (version_compare(PHP_VERSION, $version, '>=')) {
|
|
|
|
break;
|
|
|
|
}
|
2012-04-25 20:04:46 +02:00
|
|
|
|
2012-09-07 18:06:11 +02:00
|
|
|
$this->newKeywords += $newKeywords;
|
|
|
|
}
|
2012-04-25 20:04:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function startLexing($code) {
|
2011-12-18 13:04:27 +01:00
|
|
|
$this->inObjectAccess = false;
|
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
// on PHP 5.4 don't do anything
|
|
|
|
if (version_compare(PHP_VERSION, '5.4.0RC1', '>=')) {
|
2012-04-25 20:04:46 +02:00
|
|
|
parent::startLexing($code);
|
2012-02-21 17:56:07 +01:00
|
|
|
} else {
|
|
|
|
$code = $this->preprocessCode($code);
|
2012-04-25 20:04:46 +02:00
|
|
|
parent::startLexing($code);
|
2012-02-21 17:56:07 +01:00
|
|
|
$this->postprocessTokens();
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
2012-02-21 17:56:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replaces new features in the code by ~__EMU__{NAME}__{DATA}__~ sequences.
|
|
|
|
* ~LABEL~ is never valid PHP code, that's why we can (to some degree) safely
|
|
|
|
* use it here.
|
|
|
|
* Later when preprocessing the tokens these sequences will either be replaced
|
|
|
|
* by real tokens or replaced with their original content (e.g. if they occured
|
|
|
|
* inside a string, i.e. a place where they don't have a special meaning).
|
|
|
|
*/
|
|
|
|
protected function preprocessCode($code) {
|
|
|
|
// binary notation (0b010101101001...)
|
|
|
|
$code = preg_replace('(\b0b[01]+\b)', '~__EMU__BINARY__$0__~', $code);
|
2011-12-18 13:04:27 +01:00
|
|
|
|
|
|
|
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
|
2012-02-21 17:56:07 +01:00
|
|
|
// namespace separator (backslash not followed by some special characters,
|
|
|
|
// which are not valid after a NS separator, but would cause problems with
|
|
|
|
// escape sequence parsing if one would replace the backslash there)
|
2012-02-21 19:28:40 +01:00
|
|
|
$code = preg_replace('(\\\\(?!["\'`${\\\\]))', '~__EMU__NS__~', $code);
|
2011-12-18 13:04:27 +01:00
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
// nowdoc (<<<'ABC'\ncontent\nABC;)
|
2011-12-18 13:04:27 +01:00
|
|
|
$code = preg_replace_callback(
|
2012-01-15 14:37:53 +01:00
|
|
|
'((*BSR_ANYCRLF) # set \R to (?>\r\n|\r|\n)
|
2011-12-18 13:04:27 +01:00
|
|
|
(b?<<<[\t ]*\'([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\'\R) # opening token
|
2012-01-15 14:37:53 +01:00
|
|
|
((?:(?!\2;?\R).*\R)*) # content
|
|
|
|
(\2) # closing token
|
|
|
|
(?=;?\R) # must be followed by newline (with optional semicolon)
|
2011-12-18 13:04:27 +01:00
|
|
|
)x',
|
|
|
|
array($this, 'encodeNowdocCallback'),
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As nowdocs can have arbitrary content but LABELs can only contain a certain
|
|
|
|
* range of characters, the nowdoc content is encoded as hex and separated by
|
|
|
|
* 'x' tokens. So the result of the encoding will look like this:
|
|
|
|
* ~__EMU__NOWDOC__{HEX(START_TOKEN)}x{HEX(CONTENT)}x{HEX(END_TOKEN)}~
|
|
|
|
*/
|
|
|
|
public function encodeNowdocCallback(array $matches) {
|
|
|
|
return '~__EMU__NOWDOC__'
|
|
|
|
. bin2hex($matches[1]) . 'x' . bin2hex($matches[3]) . 'x' . bin2hex($matches[4])
|
|
|
|
. '__~';
|
|
|
|
}
|
2011-12-18 13:04:27 +01:00
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
/*
|
|
|
|
* Replaces the ~__EMU__...~ sequences with real tokens or their original
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
protected function postprocessTokens() {
|
|
|
|
// we need to manually iterate and manage a count because we'll change
|
|
|
|
// the tokens array on the way
|
2011-12-18 13:04:27 +01:00
|
|
|
for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) {
|
2012-02-21 17:56:07 +01:00
|
|
|
// first check that the following tokens are form ~LABEL~,
|
|
|
|
// then match the __EMU__... sequence.
|
2011-12-18 13:04:27 +01:00
|
|
|
if ('~' === $this->tokens[$i]
|
|
|
|
&& isset($this->tokens[$i + 2])
|
|
|
|
&& '~' === $this->tokens[$i + 2]
|
|
|
|
&& T_STRING === $this->tokens[$i + 1][0]
|
|
|
|
&& preg_match('(^__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?$)', $this->tokens[$i + 1][1], $matches)
|
|
|
|
) {
|
|
|
|
if ('BINARY' === $matches[1]) {
|
2012-02-21 17:56:07 +01:00
|
|
|
// the binary number can either be an integer or a double, so return a LNUMBER
|
|
|
|
// or DNUMBER respectively
|
2012-01-15 15:19:07 +01:00
|
|
|
$replace = array(
|
|
|
|
array(is_int(bindec($matches[2])) ? T_LNUMBER : T_DNUMBER, $matches[2], $this->tokens[$i + 1][2])
|
|
|
|
);
|
2011-12-18 13:04:27 +01:00
|
|
|
} elseif ('NS' === $matches[1]) {
|
2012-02-21 17:56:07 +01:00
|
|
|
// a \ single char token is returned here and replaced by a
|
2012-04-25 20:04:46 +02:00
|
|
|
// PHPParser_Parser::T_NS_SEPARATOR token in ->getNextToken(). This hacks around
|
|
|
|
// the limitations arising from T_NS_SEPARATOR not being defined on 5.3
|
2011-12-18 13:04:27 +01:00
|
|
|
$replace = array('\\');
|
|
|
|
} elseif ('NOWDOC' === $matches[1]) {
|
2012-02-21 17:56:07 +01:00
|
|
|
// decode the encoded nowdoc payload; pack('H*' is bin2hex( for 5.3
|
2011-12-18 13:04:27 +01:00
|
|
|
list($start, $content, $end) = explode('x', $matches[2]);
|
|
|
|
list($start, $content, $end) = array(pack('H*', $start), pack('H*', $content), pack('H*', $end));
|
|
|
|
|
|
|
|
$replace = array();
|
|
|
|
$replace[] = array(T_START_HEREDOC, $start, $this->tokens[$i + 1][2]);
|
|
|
|
if ('' !== $content) {
|
|
|
|
$replace[] = array(T_ENCAPSED_AND_WHITESPACE, $content, -1);
|
|
|
|
}
|
|
|
|
$replace[] = array(T_END_HEREDOC, $end, -1);
|
|
|
|
} else {
|
2012-02-21 17:56:07 +01:00
|
|
|
// just ignore all other __EMU__ sequences
|
2011-12-18 13:04:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_splice($this->tokens, $i, 3, $replace);
|
|
|
|
$c -= 3 - count($replace);
|
2012-02-21 17:56:07 +01:00
|
|
|
// for multichar tokens (e.g. strings) replace any ~__EMU__...~ sequences
|
|
|
|
// in their content with the original character sequence
|
2011-12-18 13:04:27 +01:00
|
|
|
} elseif (is_array($this->tokens[$i])
|
|
|
|
&& 0 !== strpos($this->tokens[$i][1], '__EMU__')
|
|
|
|
) {
|
|
|
|
$this->tokens[$i][1] = preg_replace_callback(
|
|
|
|
'(~__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?~)',
|
|
|
|
array($this, 'restoreContentCallback'),
|
|
|
|
$this->tokens[$i][1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
/*
|
|
|
|
* This method is a callback for restoring EMU sequences in
|
|
|
|
* multichar tokens (like strings) to their original value.
|
|
|
|
*/
|
2011-12-18 13:04:27 +01:00
|
|
|
public function restoreContentCallback(array $matches) {
|
|
|
|
if ('BINARY' === $matches[1]) {
|
|
|
|
return $matches[2];
|
|
|
|
} elseif ('NS' === $matches[1]) {
|
|
|
|
return '\\';
|
|
|
|
} elseif ('NOWDOC' === $matches[1]) {
|
|
|
|
list($start, $content, $end) = explode('x', $matches[2]);
|
|
|
|
return pack('H*', $start) . pack('H*', $content) . pack('H*', $end);
|
|
|
|
} else {
|
|
|
|
return $matches[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 17:34:27 +02:00
|
|
|
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
|
|
|
$token = parent::getNextToken($value, $startAttributes, $endAttributes);
|
2011-12-18 13:04:27 +01:00
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
// replace new keywords by their respective tokens. This is not done
|
|
|
|
// if we currently are in an object access (e.g. in $obj->namespace
|
|
|
|
// "namespace" stays a T_STRING tokens and isn't converted to T_NAMESPACE)
|
2011-12-18 13:04:27 +01:00
|
|
|
if (PHPParser_Parser::T_STRING === $token && !$this->inObjectAccess) {
|
2012-04-25 20:04:46 +02:00
|
|
|
if (isset($this->newKeywords[strtolower($value)])) {
|
|
|
|
return $this->newKeywords[strtolower($value)];
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
2012-02-21 17:56:07 +01:00
|
|
|
// backslashes are replaced by T_NS_SEPARATOR tokens
|
2011-12-18 13:04:27 +01:00
|
|
|
} elseif (92 === $token) { // ord('\\')
|
|
|
|
return PHPParser_Parser::T_NS_SEPARATOR;
|
2012-02-21 17:56:07 +01:00
|
|
|
// keep track of whether we currently are in an object access (after ->)
|
2011-12-18 13:04:27 +01:00
|
|
|
} elseif (PHPParser_Parser::T_OBJECT_OPERATOR === $token) {
|
|
|
|
$this->inObjectAccess = true;
|
|
|
|
} else {
|
|
|
|
$this->inObjectAccess = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
}
|