Returns the filename for the given stream

@param \Twig_TokenStream $stream
@return string
761146
private function getStreamFilename(\Twig_TokenStream $stream)
    {
        if (method_exists($stream, 'getFilename')) {
            // Support for 1.x versions of Twig.
            // @codeCoverageIgnoreStart
            return $stream->getFilename();
            // @codeCoverageIgnoreEnd
        }

        return $stream->getSourceContext()->getName();
    }
829967
protected function _msgText($stream, $data)
    {
        if ($data instanceof Horde_Stream) {
            if ($stream) {
                $data->rewind();
                return $data->stream;
            }
            return strval($data);
        }

        if (is_resource($data)) {
            rewind($data);
            return $stream
                ? $data
                : stream_get_contents($data);
        }

        if (!$stream) {
            return strval($data);
        }

        $tmp = fopen('php://temp', 'w+');

        if (!is_null($data)) {
            fwrite($tmp, $data);
            rewind($tmp);
        }

        return $tmp;
    }
1273344
private function parseList(Stream $stream)
    {
        $list = [];

        do {
            $list[] = $this->parseValue($stream);
        } while ($stream->current()->equals(Token::TYPE_SYMBOL, ',') && $stream->expects(Token::TYPE_SYMBOL, ','));

        return $list;
    }
919227
public static function tokenName($tokenType)
    {
        switch ($tokenType) {
            case self::T_UNDEFINED:
                return 'T_UNDEFINED';
            case self::T_OPEN_PARENS:
                return 'T_OPEN_PARENS';
            case self::T_CLOSE_PARENS:
                return 'T_CLOSE_PARENS';
            case self::T_OR:
                return 'T_OR';
            case self::T_AND:
                return 'T_AND';
            case self::T_QUERY:
                return 'T_QUERY';
            case self::T_SPACE:
                return 'T_SPACE';
            case self::T_TRUE:
                return 'T_TRUE';
            case self::T_FALSE:
                return 'T_FALSE';
            default:
                return '"' . (string) $tokenType . '"';
        }
    }
974465
public function parse($stream)
    {
        $tokeniser = new StreamTokeniser($this->csvFormat, $stream);
        $parser = new Parser();
        return $this->parseIterator(
            $parser->parse($tokeniser->getTokens())
        );
    }
280439
private function parseExport(Stream $stream)
    {
        $stream->expects(Token::TYPE_KEYWORD, 'export');

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'type')) {
            $stream->expects(Token::TYPE_KEYWORD, 'type');

            $typeName = $this->parseIdentifier($stream);
            $alias = null;

            if ($stream->current()->equals(Token::TYPE_KEYWORD, 'as')) {
                $stream->expects(Token::TYPE_KEYWORD, 'as');

                $alias = $this->parseIdentifier($stream);
            }

            $stream->expects(Token::TYPE_SYMBOL, ';');

            return new MapperExportNode($typeName, $alias);
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'function')) {
            $stream->expects(Token::TYPE_KEYWORD, 'function');

            $functionName = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
            $alias = null;

            if ($stream->current()->equals(Token::TYPE_KEYWORD, 'as')) {
                $stream->expects(Token::TYPE_KEYWORD, 'as');

                $alias = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
            }

            $stream->expects(Token::TYPE_SYMBOL, ';');

            return new BlueprintExportNode($functionName, $alias);
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'template')) {
            $stream->expects(Token::TYPE_KEYWORD, 'template');

            $templateName = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
            $alias = null;

            if ($stream->current()->equals(Token::TYPE_KEYWORD, 'as')) {
                $stream->expects(Token::TYPE_KEYWORD, 'as');

                $alias = $stream->expects(Token::TYPE_IDENTIFIER)->getValue();
            }

            $stream->expects(Token::TYPE_SYMBOL, ';');

            return new BlueprintExportNode($templateName, $alias);
        }

        throw new ParserException(sprintf(
            'Can\'t parse the stream "%s". Expects the keyword "type", "function" or "template".',
            $stream->humanize()
        ));
    }
133629
public function parse(TokenStream $stream)
    {
        if ($stream->match(Token::OPERATOR, array('!', '-'))) {
            $value = $stream->current()->value;
            $stream->next();
            $ret = new Op\Unary($value, $this->parse($stream));
        } elseif ($stream->match(Token::KEYWORD, array('true', 'false'))) {
            $ret = new Node\Expr\Literal($stream->current()->value === 'true');
            $stream->next();
        } elseif ($stream->match(Token::KEYWORD, 'null')) {
            $ret = new Node\Expr\Literal(null);
            $stream->next();
        } elseif ($stream->match(Token::IDENTIFIER)) {
            $name = $stream->current()->value;
            $stream->next();

            $ret = new Node\Expr\Variable($name);
        } elseif ($stream->match(Token::STRING)) {
            $ret = new Node\Expr\Str($stream->current()->value);
            $stream->next();
        } elseif ($stream->match(Token::NUMBER)) {
            $value = $stream->current()->value;
            if (strpos('.', $value) !== false) {
                $ret = new Node\Expr\Number((float)$value);
            } else {
                $ret = new Node\Expr\Number((int)$value);
            }
            $stream->next();
        } elseif ($stream->match(Token::OPERATOR, '(')) {
            $stream->next();
            $ret = new Node\Expr\Parens($this->parse($stream));
            $stream->expect(Token::OPERATOR, ')');
        } elseif ($stream->match(Token::OPERATOR, '[')) {
            $stream->next();
            $ret = new Node\Expr\ListNode();
            if (!$stream->match(Token::OPERATOR, ']')) {
                $ret->append($this->parse($stream));
                while ($stream->match(',')) {
                    $stream->next();
                    $ret->append($this->parse($stream));
                }
            }
            $stream->expect(Token::OPERATOR, ']');
        } else {
            $this->err($stream);
            return null;
        }

        if ($stream->valid()) {
            while ($stream->valid() && $stream->match(Token::OPERATOR, array('(', '.', '['))) {
                $type = $stream->current();

                $stream->next();
                if ($type->value === '(') {
                    $ret = new Node\Expr\Call($ret);
                    if (!$stream->match(Token::OPERATOR, ')')) {
                        do {
                            $arg = $this->parse($stream);
                            $ret->append($arg);

                            if ($stream->match(',')) {
                                $stream->next();
                            } else {
                                break;
                            }
                        } while (true);
                    }
                    $stream->expect(Token::OPERATOR, ')');
                } else {
                    $ret = new Node\Expr\Subscript($ret);

                    if ($type->value === '.') {
                        $token = $stream->expect(Token::IDENTIFIER);
                        $ret->append(new Node\Expr\Str($token->value));
                    } else {
                        $ret->append($this->parse($stream));
                    }

                    switch ($type->value) {
                        case '[':
                            $stream->expect(Token::OPERATOR, ']');
                            break;
                    }
                }
            }
        }

        if ($stream->valid()) {
            if ($stream->match(Token::OPERATOR, self::$INFIX_BINARY) || $stream->match(Token::KEYWORD, 'in')) {
                $value = $stream->current()->value;
                $stream->next();
                $ret = new Op\Binary($value, $ret, $this->parse($stream));
            } elseif ($stream->match(Token::OPERATOR, '?')) {
                $stream->next();
                if ($stream->match(Token::OPERATOR, ':')) {
                    $then = null;
                } else {
                    $then = $this->parse($stream);
                }

                if ($stream->valid() && $stream->match(Token::OPERATOR, ':')) {
                    $stream->next();
                    $else = $this->parse($stream);
                } else {
                    $else = null;
                }
                $ret = new Op\Ternary('?', $ret, $then, $else);
            }
        }

        // little syntactic sugar for function calls without parentheses:
        $allowInlineCallTokens = array(Token::IDENTIFIER, Token::STRING, Token::NUMBER);
        if ($stream->valid() && ($stream->match($allowInlineCallTokens))) {
            $ret = new Node\Expr\Call($ret);
            $ret->append($this->parse($stream));
        }

        return $ret;
    }
741716
private function parseType(Stream $stream)
    {
        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'export')) {
            $stream->expects(Token::TYPE_KEYWORD, 'export');

            $isExported = true;
        } else {
            $isExported = false;
        }

        $stream->expects(Token::TYPE_KEYWORD, 'type');

        $typeName = $this->parseTypeIdentifier($stream);

        if ($isExported) {
            $nodes = [new MapperExportNode($typeName)];
        } else {
            $nodes = [];
        }

        if ($stream->current()->equals(Token::TYPE_KEYWORD, 'extends')) {
            $stream->expects(Token::TYPE_KEYWORD, 'extends');

            $parentTypeName = $this->parseTypeIdentifier($stream);
        } else {
            $parentTypeName = null;
        }

        $stream->expects(Token::TYPE_SYMBOL, '{');

        $definition = $this->parseTypeDefinition($stream);

        $stream->expects(Token::TYPE_SYMBOL, '}');
        $stream->expects(Token::TYPE_SYMBOL, ';');

        if (isset($definition['mapping'])) {
            if ($parentTypeName !== null) {
                array_unshift($nodes, new ExtendCompositeTypeNode($parentTypeName, $typeName, $definition));
            } else {
                array_unshift($nodes, new SetCompositeTypeNode($typeName, $definition));
            }
        } else {
            if ($parentTypeName !== null) {
                array_unshift($nodes, new ExtendTypeNode($parentTypeName, $typeName, $definition));
            } else {
                array_unshift($nodes, new SetTypeNode($typeName, $definition));
            }
        }

        return $nodes;
    }
1275718
protected function findTwigFile($path, $template = null)
    {
        if ($template == null) {
            $template = $this->template;
        }

        return '::' . self::TWIG_TEMPLATE_PATH . '/' . $template . '/' . $path;
    }
889067
public function toStream($stream)
    {
        $bytesWritten = 0;
        foreach ($this->getChunkCursor() as $chunk) {
            $bytesWritten += fwrite($stream, $chunk['data']->getData());
        }
        return $bytesWritten;
    }
228742
protected function initStream($stream)
    {
        expect_type(
            $stream,
            ['stream resource', 'string'],
            \TypeError::class,
            "Expected stream resource or uri (string), %s given"
        );

        if (is_string($stream)) {
            $stream = $this->openStream($stream);
        }

        $meta = stream_get_meta_data($stream);

        if ($meta['mode'] === 'r') {
            throw new \InvalidArgumentException("Stream \"{$meta['uri']}\" is not writable");
        }

        return $stream;
    }
272970
public function getFilename(string $name): ?string
    {
        if (!empty($file = $this->get($name)) && !$file->getError()) {
            return StreamWrapper::getFilename($file->getStream());
        }

        return null;
    }
644641
protected function seekTernaryElse(\Twig_TokenStream $tokens)
    {
        $i = 1;
        $depth = 0;
        $found = false;
        $token = null;

        while ($depth || !$found) {
            $token = $tokens->look($i);

            if (in_array($token->getType(), [\Twig_Token::BLOCK_END_TYPE, \Twig_Token::VAR_END_TYPE, \Twig_Token::INTERPOLATION_END_TYPE])) {
                return;
            }

            // End of hash value means end of short ternary (eg. "foo ? bar" syntax)
            if ($token->getType() === \Twig_Token::PUNCTUATION_TYPE && $token->getValue() === ',') {
                return;
            }

            if ($token->getType() === \Twig_Token::PUNCTUATION_TYPE && in_array($token->getValue(), ['(', '[', '{'])) {
                $depth++;
            }

            if ($depth && $token->getType() === \Twig_Token::PUNCTUATION_TYPE && in_array($token->getValue(), [')', ']', '}'])) {
                $depth--;
            }

            $found = $token->getType() === \Twig_Token::PUNCTUATION_TYPE && $token->getValue() === ':';
            $i++;
        }

        return $token;
    }
178831
protected function extractTableName($stream)
    {
        if ($stream->getNextExpected(T_WHITESPACE) === null) {
            return false;
        }

        if ($stream->getNextExpected(T_VARIABLE) === null) {
            return false;
        }

        if ($stream->getCurrentText() != '$table') {
            return false;
        }

        if ($stream->getNextExpectedTerminated(['=', T_WHITESPACE], [T_CONSTANT_ENCAPSED_STRING]) === null) {
            return null;
        }

        $tableName = $stream->getCurrentText();
        $tableName = trim($tableName, '\''); 
        $tableName = trim($tableName, '"'); 

        return $tableName;
    }
1341400
public function lexAll(\Jstewmc\Stream\Stream $stream) 
	{
		$tokens = [];
		
		// while tokens exist
		while (false !== ($token = $this->lexOne($stream))) {
			// append le token
			$tokens[] = $token;
		}
		
		return $tokens;
	}
1271535
private function getStream($stream)
    {
        if ($stream === 'php://input') {
            return new PhpInputStream();
        }

        if (! is_string($stream) && ! is_resource($stream) && ! $stream instanceof StreamInterface) {
            throw new InvalidArgumentException(
                'Stream must be a string stream resource identifier, '
                . 'an actual stream resource, '
                . 'or a Psr\Http\Message\StreamInterface implementation'
            );
        }

        if (! $stream instanceof StreamInterface) {
            return new Stream($stream, 'r');
        }

        return $stream;
    }
817495
public static function getUsableUri($stream)
    {
        $uri = static::getMetaDataKey($stream, 'uri');

        return isset($uri) && $uri !== '' && file_exists($uri) ? $uri : false;
    }