function __construct(Patchwork_PHP_Parser $parent, $inlineClass) { foreach ($inlineClass as $inlineClass) { $this->inlineClass[strtolower(strtr($inlineClass, '\\', '_'))] = 1; } Patchwork_PHP_Parser::__construct($parent); }
protected function getTokens($code) { if ($this->lfLineEndings && false !== strpos($code, "\r")) { $code = str_replace("\r\n", "\n", $code); $code = strtr($code, "\r", "\n"); } if ($this->checkUtf8 && !preg_match('//u', $code)) { $this->setError("File encoding is not valid UTF-8", E_USER_WARNING); } if ($this->stripUtf8Bom && 0 === strncmp($code, "", 3)) { // substr_replace() is for mbstring overloading resistance $code = substr_replace($code, '', 0, 3); $this->setError("Stripping UTF-8 Byte Order Mark", E_USER_NOTICE); } if ('' === $code) { return array(); } $code = parent::getTokens($code); // Ensure that the first token is always a T_OPEN_TAG if (T_INLINE_HTML === $code[0][0]) { $a = $code[0][1]; $a = "\r" === $a[0] ? isset($a[1]) && "\n" === $a[1] ? '\\r\\n' : '\\r' : ("\n" === $a[0] ? '\\n' : ''); if ($a) { array_unshift($code, array(T_OPEN_TAG, '<?php '), array(T_ECHO, 'echo'), array(T_ENCAPSED_AND_WHITESPACE, "\"{$a}\""), array(T_CLOSE_TAG, '?>')); } else { array_unshift($code, array(T_OPEN_TAG, '<?php '), array(T_CLOSE_TAG, '?>')); } } // Ensure that the last valid PHP code position is tagged with a T_ENDPHP $a = array_pop($code); $code[] = T_CLOSE_TAG === $a[0] ? ';' : $a; T_INLINE_HTML === $a[0] && ($code[] = array(T_OPEN_TAG, '<?php ')); $code[] = array(T_ENDPHP, ''); return $code; }
function __construct(parent $parent) { if (PHP_VERSION_ID < 50300) { $this->callbacks = array(); } parent::__construct($parent); }
function __construct(parent $parent, &$openToken, $curly, $open, $close) { $this->openToken =& $openToken; $this->curly = $curly; $this->open = $open; $this->close = $close; parent::__construct($parent); }
function __construct(parent $parent, $level, $topClass) { if (0 <= $level) { unset($this->callbacks['tagRequire']); } parent::__construct($parent); $this->level = $level; $this->topClass = $topClass; }
protected function getTokens($code) { if (PHP_VERSION_ID < 50400 && false !== ($i = stripos($code, '0b'))) { if ('0b' === strtolower(rtrim(substr($code, $i, 3), '01'))) { $this->register(array('catch0b' => T_LNUMBER)); } } return parent::getTokens($code); }
function __construct(parent $parent, $autoglobals) { foreach ((array) $autoglobals as $autoglobals) { if (!isset(${substr($autoglobals, 1)}) || '$autoglobals' === $autoglobals || '$parent' === $autoglobals) { $this->autoglobals[$autoglobals] = 1; } } parent::__construct($parent); }
protected function getTokens($code) { if (PHP_VERSION_ID < 50400 && false !== strpos($code, '<?=')) { $c = token_get_all('<?='); if (T_INLINE_HTML === $c[0][0]) { $this->tag = '<?php __echo_soe_' . mt_rand() . ' '; $code = str_replace('<?=', $this->tag, $code); $this->register(array('echoTag' => T_OPEN_TAG, 'removeTag' => array(T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_HALT_COMPILER_DATA))); } } return parent::getTokens($code); }
function __construct(parent $parent, $file, &$new_consts = array()) { if ($file) { $this->file = self::export($file); $this->dir = self::export(dirname($file)); } else { unset($this->callbacks['tagFileC'], $this->callbacks['tagLineC']); } $file = self::$staticConsts; self::loadConsts($new_consts); $this->constants = self::$staticConsts; self::$staticConsts = $file; $this->newConsts =& $new_consts; parent::__construct($parent); }
protected function getTokens($code) { foreach ($this->backports as $k => $i) { if (self::T_OFFSET >= $i || false === stripos($code, $k)) { unset($this->backports[$k]); } } $code = parent::getTokens($code); $i = 0; if ($this->backports) { while (isset($code[++$i])) { if (T_STRING === $code[$i][0] && isset($this->backports[strtolower($code[$i][1])])) { $code[$i][0] = $this->backports[strtolower($code[$i][1])]; } } } return $code; }
<?php // vi: set fenc=utf-8 ts=4 sw=4 et: /* * Copyright (C) 2012 Nicolas Grekas - p@tchwork.com * * This library is free software; you can redistribute it and/or modify it * under the terms of the (at your option): * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt). */ Patchwork_PHP_Parser::createToken('T_NAME_NS', 'T_NAME_CLASS', 'T_NAME_FUNCTION', 'T_NAME_CONST', 'T_USE_NS', 'T_USE_CLASS', 'T_USE_METHOD', 'T_USE_PROPERTY', 'T_USE_FUNCTION', 'T_USE_CONST', 'T_USE_CONSTANT', 'T_GOTO_LABEL', 'T_TYPE_HINT'); /** * The StringInfo parser analyses T_STRING tokens and gives them a secondary type to allow more specific semantics. * * This parser analyses tokens surrounding T_STRING tokens and is able to determine between many different semantics: * - T_NAME_NS: namespace declaration as in namespace FOO\BAR * - T_NAME_CLASS: class, interface or trait declaration as in class FOO {} * - T_NAME_FUNCTION: function or method declaration as in function FOO() * - T_NAME_CONST: class or namespaced const declaration as in const FOO * - T_USE_NS: namespace prefix or "use" aliasing as in FOO\bar * - T_USE_CLASS: class usage as in new foo\BAR or FOO:: * - T_USE_METHOD: method call as in $a->FOO() or a::BAR() * - T_USE_PROPERTY: property access as in $a->BAR * - T_USE_FUNCTION: function call as in foo\BAR() * - T_USE_CONST: class constant access as in foo::BAR * - T_USE_CONSTANT: global or namespaced constant access as in FOO or foo\BAR * - T_GOTO_LABEL: goto label as in goto FOO or BAR:{} * - T_TYPE_HINT: type hint as in instanceof foo\BAR or function(foo\BAR $a) * * It also exposes a property and a method:
function __construct(parent $parent) { parent::__construct($parent); $this->backports += array('goto' => T_GOTO, '__dir__' => T_DIR, 'namespace' => T_NAMESPACE, '__namespace__' => T_NS_C); }
function __construct(parent $parent, $className) { parent::__construct($parent); $this->className = $className; }
// vi: set fenc=utf-8 ts=4 sw=4 et: /* * Copyright (C) 2012 Nicolas Grekas - p@tchwork.com * * This library is free software; you can redistribute it and/or modify it * under the terms of the (at your option): * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt). */ define('T_SEMANTIC', 1); // Primary type for semantic tokens define('T_NON_SEMANTIC', 2); // Primary type for non-semantic tokens (whitespace and comment) Patchwork_PHP_Parser::createToken('T_CURLY_CLOSE', 'T_STR_STRING'); defined('T_NS_SEPARATOR') || Patchwork_PHP_Parser::createToken('T_NS_SEPARATOR'); /** * Patchwork PHP Parser is a highly extensible framework for building high-performance PHP code * parsers around PHP's tokenizer extension. * * It does nothing on its own but implement an expert knowledge of tokenizer's special cases as well * as a predictable plugin mechanism for registering and dispatching tokens to a chain of parsers, * while remaining as fast and memory efficient as possible. * * It can be used for example to: * - compute static code analysis, * - verify coding practices for QA, * - backport some language features, * - extend the PHP language, * - build a code preprocessor, * - build an aspect weaver,
function __construct(parent $parent = null) { parent::__construct($parent); $this->register($this->stateCallbacks[2]); $this->runtimeKey = mt_rand(1, mt_getrandmax()); }
function __construct(parent $parent, &$new_overrides = array()) { parent::__construct($parent); $this->overrides = self::$staticOverrides; $this->newOverrides =& $new_overrides; }
function __construct(parent $parent, $ns_code_loader = null) { $this->nsCodeLoader = $ns_code_loader ? $ns_code_loader : array($this, 'nsCodeLoader'); parent::__construct($parent); }
<?php // vi: set fenc=utf-8 ts=4 sw=4 et: /* * Copyright (C) 2012 Nicolas Grekas - p@tchwork.com * * This library is free software; you can redistribute it and/or modify it * under the terms of the (at your option): * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt). */ Patchwork_PHP_Parser::createToken('T_BRACKET_CLOSE'); /** * The BracketWatcher parser counts opening brackets and triggers callbacks on corresponding closing brackets. */ class Patchwork_PHP_Parser_BracketWatcher extends Patchwork_PHP_Parser { protected $brackets = array(), $callbacks = array('~pushBracket' => array('{', '[', '('), 'closeBracket' => array('}', ']', ')'), '~popBracket' => array('}', ']', ')')); protected function pushBracket(&$token) { $b =& $this->brackets[]; switch ($token[0]) { case '(': $b = ')'; break; case '[': $b = ']'; break; default: $b = '}'; break;
function __construct(parent $parent, $aliasAdd = false) { $this->aliasAdd = $aliasAdd; parent::__construct($parent); }
<?php // vi: set fenc=utf-8 ts=4 sw=4 et: /* * Copyright (C) 2012 Nicolas Grekas - p@tchwork.com * * This library is free software; you can redistribute it and/or modify it * under the terms of the (at your option): * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt). */ Patchwork_PHP_Parser::createToken('T_SCOPE_OPEN'); /** * The ScopeInfo parser exposes scopes to dependend parsers. * * Scopes are typed as T_OPEN_TAG, T_NAMESPACE, T_FUNCTION, T_CLASS, T_INTERFACE and T_TRAIT, each * of these corresponding to the type of the token who opened the scope. For each scope, this * parser exposes this type alongside with a reference to its opening token and its parent scope. * * T_SCOPE_OPEN can be registered by dependend parsers and is emitted on scope opening tokens. * When T_BRACKET_CLOSE is registered within a T_SCOPE_OPEN, it matches its corresponding scope closing token. * * ScopeInfo eventually inherits removeNsPrefix(), namespace, nsResolved, nsPrefix properties from NamespaceInfo. */ class Patchwork_PHP_Parser_ScopeInfo extends Patchwork_PHP_Parser { protected $scope = false, $nextScope = T_OPEN_TAG, $callbacks = array('~tagFirstScope' => array(T_OPEN_TAG, ';', '{'), 'tagScopeOpen' => '{', 'tagNamespace' => T_NAMESPACE, 'tagEndScope' => T_ENDPHP, 'tagFunction' => T_FUNCTION, 'tagClass' => array(T_CLASS, T_INTERFACE, T_TRAIT)), $namespace, $nsResolved, $nsPrefix, $dependencies = array('BracketWatcher', 'NamespaceInfo' => array('namespace', 'nsResolved', 'nsPrefix'), 'Normalizer'); function removeNsPrefix() { empty($this->nsPrefix) || $this->dependencies['NamespaceInfo']->removeNsPrefix(); }