Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 313 lines (274 sloc) 8.818 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
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\analysis;

use lithium\util\Set;
use lithium\util\Collection;

/**
* The parser class uses PHP's tokenizer to provide methods and tools for performing static analysis
* on PHP code.
*/
class Parser extends \lithium\core\StaticObject {

/**
* Convenience method to get the token name of a PHP code string. If multiple tokens are
* present in the string, only the first is returned.
*
* @param string $string String of PHP code to get the token name of, i.e. `'=>'` or `'static'`.
* @param array $options
* @return mixed
*/
public static function token($string, array $options = array()) {
$defaults = array('id' => false);
$options += $defaults;

if (empty($string) && $string !== '0') {
return false;
}
list($token) = static::tokenize($string);
return $token[($options['id']) ? 'id' : 'name'];
}

/**
* Splits the provided `$code` into PHP language tokens.
*
* @param string $code Source code to be tokenized.
* @param array $options Options consists of:
* -'wrap': Boolean indicating whether or not to wrap the supplied
* code in PHP tags.
* -'ignore': An array containing PHP language tokens to ignore.
* -'include': If supplied, an array of the only language tokens
* to include in the output.
* @return array An array of tokens in the supplied source code.
*/
public static function tokenize($code, array $options = array()) {
$defaults = array('wrap' => true, 'ignore' => array(), 'include' => array());
$options += $defaults;
$tokens = array();
$line = 1;

if ($options['wrap']) {
$code = "<?php {$code}?>";
}
foreach (token_get_all($code) as $token) {
$token = (isset($token[1])) ? $token : array(null, $token, $line);
list($id, $content, $line) = $token;
$name = $id ? token_name($id) : $content;

if (!empty($options['include'])) {
if (!in_array($name, $options['include']) && !in_array($id, $options['include'])) {
continue;
}
}

if (!empty($options['ignore'])) {
if (in_array($name, $options['ignore']) || in_array($id, $options['ignore'])) {
continue;
}
}
$tokens[] = array('id' => $id, 'name' => $name, 'content' => $content, 'line' => $line);
}

if ($options['wrap'] && empty($options['include'])) {
$tokens = array_slice($tokens, 1, count($tokens) - 2);
}
return $tokens;
}

/**
* Finds a pattern in a block of code.
*
* @param string $code
* @param string $pattern
* @param array $options The list of options to be used when parsing / matching `$code`:
* - 'ignore': An array of token names to ignore while parsing, defaults to
* `array('T_WHITESPACE')`
* - 'lineBreaks': If true, all tokens in a single pattern match must appear on the
* same line of code, defaults to false
* - 'startOfLine': If true, the pattern must match starting with the beginning of
* the line of code to be matched, defaults to false
* @return array
*/
public static function find($code, $pattern, array $options = array()) {
$defaults = array(
'all' => true, 'capture' => array(), 'ignore' => array('T_WHITESPACE'),
'return' => true, 'lineBreaks' => false, 'startOfLine' => false
);
$options += $defaults;
$results = array();
$matches = array();
$patternMatch = array();
$ret = $options['return'];

$tokens = new Collection(array('data' => static::tokenize($code, $options)));
$pattern = new Collection(array('data' => static::tokenize($pattern, $options)));

$breaks = function($token) use (&$tokens, &$matches, &$patternMatch, $options) {
if (!$options['lineBreaks']) {
return true;
}
if (empty($patternMatch) && !$options['startOfLine']) {
return true;
}

if (empty($patternMatch)) {
$prev = $tokens->prev();
$tokens->next();
} else {
$prev = reset($patternMatch);
}

if (empty($patternMatch) && $options['startOfLine']) {
return ($token['line'] > $prev['line']);
}
return ($token['line'] == $prev['line']);
};

$capture = function($token) use (&$matches, &$patternMatch, $tokens, $breaks, $options) {
if (is_null($token)) {
$matches = $patternMatch = array();
return false;
}

if (empty($patternMatch)) {
$prev = $tokens->prev();
$tokens->next();
if ($options['startOfLine'] && $token['line'] == $prev['line']) {
$patternMatch = $matches = array();
return false;
}
}
$patternMatch[] = $token;

if (empty($options['capture']) || !in_array($token['name'], $options['capture'])) {
return true;
}
if (!$breaks($token)) {
$matches = array();
return true;
}
$matches[] = $token;
return true;
};

$executors = array(
'*' => function(&$tokens, &$pattern) use ($options, $capture) {
$closing = $pattern->next();
$tokens->prev();

while (($t = $tokens->next()) && !Parser::matchToken($closing, $t)) {
$capture($t);
}
$pattern->next();
}
);

$tokens->rewind();
$pattern->rewind();

while ($tokens->valid()) {
if (!$pattern->valid()) {
$pattern->rewind();

if (!empty($matches)) {
$results[] = array_map(
function($i) use ($ret) { return isset($i[$ret]) ? $i[$ret] : $i; },
$matches
);
}
$capture(null);
}

$p = $pattern->current();
$t = $tokens->current();

switch (true) {
case (static::matchToken($p, $t)):
$capture($t) ? $pattern->next() : $pattern->rewind();
break;
case (isset($executors[$p['name']])):
$exec = $executors[$p['name']];
$exec($tokens, $pattern);
break;
default:
$capture(null);
$pattern->rewind();
break;
}
$tokens->next();
}
return $results;
}

/**
* Token pattern matching.
*
* @param string $code Source code to be analyzed.
* @param string $parameters An array containing token patterns to be matched.
* @param array $options The list of options to be used when matching `$code`:
* - 'ignore': An array of language tokens to ignore.
* - 'return': If set to 'content' returns an array of matching tokens.
* @return array Array of matching tokens.
*/
public static function match($code, $parameters, array $options = array()) {
$defaults = array('ignore' => array('T_WHITESPACE'), 'return' => true);
$options += $defaults;
$parameters = static::_prepareMatchParams($parameters);

$tokens = is_array($code) ? $code : static::tokenize($code, $options);
$results = array();

foreach ($tokens as $i => $token) {
if (!array_key_exists($token['name'], $parameters)) {
if (!in_array('*', $parameters)) {
continue;
}
}
$param = $parameters[$token['name']];

if (isset($param['before']) && $i > 0) {
if (!in_array($tokens[$i - 1]['name'], (array) $param['before'])) {
continue;
}
}

if (isset($param['after']) && $i + 1 < count($tokens)) {
if (!in_array($tokens[$i + 1]['name'], (array) $param['after'])) {
continue;
}
}
$results[] = isset($token[$options['return']]) ? $token[$options['return']] : $token;
}
return $results;
}

/**
* Compares two PHP language tokens.
*
* @param array $pattern Pattern token.
* @param array $token Token to be compared.
* @return boolean Match result.
*/
public static function matchToken($pattern, $token) {
if ($pattern['name'] != $token['name']) {
return false;
}

if (!isset($pattern['content'])) {
return true;
}

$match = $pattern['content'];
$content = $token['content'];

if ($pattern['name'] == 'T_VARIABLE') {
$match = substr($match, 1);
$content = substr($content, 1);
}

switch (true) {
case ($match == '_' || $match == $content):
return true;
}
return false;
}

/**
* Helper function to normalize parameters for token matching.
*
* @see lithium\analysis\Parser::match()
* @param array $parameters Params to be normalized.
* @return array Normalized parameters.
*/
protected static function _prepareMatchParams($parameters) {
foreach (Set::normalize($parameters) as $token => $scope) {
if (strpos($token, 'T_') !== 0) {
unset($parameters[$token]);

foreach (array('before', 'after') as $key) {
if (!isset($scope[$key])) {
continue;
}
$items = array();

foreach ((array) $scope[$key] as $item) {
$items[] = (strpos($item, 'T_') !== 0) ? static::token($item) : $item;
}
$scope[$key] = $items;
}
$parameters[static::token($token)] = $scope;
}
}
return $parameters;
}
}

?>
Something went wrong with that request. Please try again.