예제 #1
0
<?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).
 */
namespace Patchwork\PHP\Parser;

use Patchwork\PHP\Parser;
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 ScopeInfo extends 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()
예제 #2
0
<?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).
 */
namespace Patchwork\PHP\Parser;

use Patchwork\PHP\Parser;
Parser::createToken('T_BRACKET_CLOSE');
/**
 * The BracketWatcher parser counts opening brackets and triggers callbacks on corresponding closing brackets.
 */
class BracketWatcher extends Parser
{
    protected $brackets = array(), $bracketsCount = 0, $callbacks = array('~pushBracket' => array('{', '[', '('), 'closeBracket' => array('}', ']', ')'), '~popBracket' => array('}', ']', ')'));
    protected function pushBracket(&$token)
    {
        ++$this->bracketsCount;
        $b =& $this->brackets[];
        switch ($token[0]) {
            case '(':
                $b = ')';
                break;
            case '[':
                $b = ']';
예제 #3
0
<?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).
 */
namespace Patchwork\PHP\Parser;

use Patchwork\PHP\Parser;
Parser::createToken('T_ENDPHP');
/**
 * The Normalizer parser verifies and ensures basic guaranties on the parsed code and its token stream.
 *
 * On the raw code, it can verify its UTF-8 validity,
 * strip any UTF-8 BOM and force line endings to LF only.
 * On the token stream, it enforces the very first token to be a T_OPEN_TAG
 * and tags the last valid PHP code position as T_ENDPHP.
 */
class Normalizer extends Parser
{
    public $checkUtf8 = true, $stripUtf8Bom = true;
    protected $callbacks = array('fixVar' => T_VAR, 'tagOpenTag' => T_OPEN_TAG, 'tagCloseTag' => T_CLOSE_TAG, 'tagOpenEchoTag' => T_OPEN_TAG_WITH_ECHO, 'tagHaltCompiler' => T_HALT_COMPILER);
    protected function getTokens($code, $is_fragment)
    {
        if ($is_fragment) {
            return parent::getTokens($code, $is_fragment);
예제 #4
0
namespace Patchwork\PHP\Parser;

use Patchwork\PHP\Parser;
// PHP 5.3 tokens
defined('T_DIR') || Parser::createToken('T_DIR');
defined('T_GOTO') || Parser::createToken('T_GOTO');
defined('T_NS_C') || Parser::createToken('T_NS_C');
defined('T_NAMESPACE') || Parser::createToken('T_NAMESPACE');
// PHP 5.4 tokens
defined('T_TRAIT') || Parser::createToken('T_TRAIT');
defined('T_TRAIT_C') || Parser::createToken('T_TRAIT_C');
defined('T_CALLABLE') || Parser::createToken('T_CALLABLE');
defined('T_INSTEADOF') || Parser::createToken('T_INSTEADOF');
// PHP 5.5 tokens
defined('T_YIELD') || Parser::createToken('T_YIELD');
defined('T_FINALLY') || Parser::createToken('T_FINALLY');
/**
 * The BackportTokens parser backports tokens introduced since PHP 5.3
 *
 * @todo Backport nowdoc syntax, allow heredoc in static declarations.
 * @todo Work around https://bugs.php.net/60097
 */
class BackportTokens extends Parser
{
    protected $backports = array('goto' => T_GOTO, '__dir__' => T_DIR, 'namespace' => T_NAMESPACE, '__namespace__' => T_NS_C, 'trait' => T_TRAIT, 'callable' => T_CALLABLE, '__trait__' => T_TRAIT_C, 'insteadof' => T_INSTEADOF, 'yield' => T_YIELD, 'finally' => T_FINALLY);
    function __construct(parent $parent)
    {
        $b = $this->backports;
        foreach ($b as $k => $i) {
            if (self::T_OFFSET >= $i) {
                unset($b[$k]);
예제 #5
0
<?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).
 */
namespace Patchwork\PHP\Parser;

use Patchwork\PHP\Parser;
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:{}
defined('T_NS_C') || Parser::createToken('T_NS_C');
defined('T_NAMESPACE') || Parser::createToken('T_NAMESPACE');
// PHP 5.4 tokens
defined('T_TRAIT') || Parser::createToken('T_TRAIT');
defined('T_TRAIT_C') || Parser::createToken('T_TRAIT_C');
defined('T_CALLABLE') || Parser::createToken('T_CALLABLE');
defined('T_INSTEADOF') || Parser::createToken('T_INSTEADOF');
// PHP 5.5 tokens
defined('T_YIELD') || Parser::createToken('T_YIELD');
defined('T_FINALLY') || Parser::createToken('T_FINALLY');
// PHP 5.6 tokens
defined('T_POW_EQUAL') || Parser::createToken('T_POW_EQUAL');
defined('T_POW') || Parser::createToken('T_POW');
defined('T_ELLIPSIS') || Parser::createToken('T_ELLIPSIS');
// PHP 7 tokens
defined('T_COALESCE') || Parser::createToken('T_COALESCE');
/**
 * The BackportTokens parser backports tokens introduced since PHP 5.3
 *
 * @todo Backport nowdoc syntax, allow heredoc in static declarations.
 * @todo Work around https://bugs.php.net/60097
 */
class BackportTokens extends Parser
{
    protected $backports = array('goto' => T_GOTO, '__dir__' => T_DIR, 'namespace' => T_NAMESPACE, '__namespace__' => T_NS_C, 'trait' => T_TRAIT, 'callable' => T_CALLABLE, '__trait__' => T_TRAIT_C, 'insteadof' => T_INSTEADOF, 'yield' => T_YIELD, 'finally' => T_FINALLY);
    function __construct(parent $parent)
    {
        $b = $this->backports;
        foreach ($b as $k => $i) {
            if (self::T_OFFSET >= $i) {
                unset($b[$k]);