1<?php
2require_once 'PHP/Parser/DocblockParser.php';
3require_once 'PHP/Parser/DocblockParser/Tokenizer.php';
4/**
5 * Basic PHP_Parser docblock parsing mechanism.
6 *
7 * This allows processing of @global tags to search
8 * for global variables, for instance, as well as
9 * parsing out the structure of doc comments
10 */
11class PHP_Parser_Docblock_Parser
12{
13    private $_lex;
14    private $_parser;
15    function __construct($data, $processInternal = false)
16    {
17        $this->_parser = new PHP_Parser_DocblockParser($this->_lex, $processInternal);
18    }
19
20    function parse($data, PHP_Parser_Tokenizer $tokenizer)
21    {
22        $this->_lex = new PHP_Parser_DocblockParser_Tokenizer($data);
23        while ($this->_lex->advance()) {
24            $this->_parser->doParse($this->_lex->token, $this->_lex->getValue());
25        }
26        $this->_parser->doParse(0, 0);
27        if (!count($this->_parser->data['tags'])) {
28            return $this->_parser->data;
29        }
30        if (!isset($this->_parser->data['tags']['global'])) {
31            return $this->_parser->data;
32        }
33        if (count($this->_parser->data['tags']['global']) > 1) {
34            // too many @global tags, this isn't valid
35            return $this->_parser->data;
36        }
37        if (count($this->_parser->data['tags'])) {
38            foreach ($this->_parser->data['tags']['global'] as $tag) {
39                if ($tag['text']) {
40                    if (!is_array($tag['text'])) {
41                        $info = preg_split('/[\t ]+/', trim($tag['text']), 2);
42                        if (count($info) != 2) {
43                            break;
44                        }
45                        if ($info[0][0] == '$') {
46                            // invalid
47                            break;
48                        }
49                        if ($info[1][0] != '$') {
50                            // function-level @global
51                            break;
52                        }
53                        $tokenizer->_setGlobalSearch(trim($info[1]));
54                        return $this->_parser->data;
55                    }
56                }
57            }
58        }
59        return $this->_parser->data;
60    }
61}
62?>