summaryrefslogtreecommitdiff
path: root/buildscripts/PhpDocumentor/phpDocumentor/phpDocumentorTWordParser.inc
blob: fb513df78870128347d70c12abf060a1797d2d9e (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
 * tokenizer extension-based lexer for PHP code
 * 
 * phpDocumentor :: automatic documentation generator
 * 
 * PHP versions 4 and 5
 *
 * Copyright (c) 2002-2006 Gregory Beaver
 * 
 * LICENSE:
 * 
 * This library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * @category   ToolsAndUtilities
 * @package    phpDocumentor
 * @subpackage Parsers
 * @author     Gregory Beaver <cellog@php.net>
 * @copyright  2002-2007 Gregory Beaver
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @version    CVS: $Id: phpDocumentorTWordParser.inc 287887 2009-08-30 06:10:55Z ashnazg $
 * @link       http://www.phpdoc.org
 * @link       http://pear.php.net/PhpDocumentor
 * @since      1.2
 * @todo       CS cleanup - change package to PhpDocumentor
 * @todo       CS cleanup - PHPCS needs to ignore CVS Id length
 */

/**
 * Like WordParser, but expects an array of tokens from the tokenizer 
 * instead of a string.
 *
 * @category   ToolsAndUtilities
 * @package    phpDocumentor
 * @subpackage WordParsers
 * @author     Gregory Beaver <cellog@php.net>
 * @copyright  2002-2007 Gregory Beaver
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @version    Release: 1.4.3
 * @link       http://www.phpdoc.org
 * @link       http://pear.php.net/PhpDocumentor
 * @since      1.2
 * @todo       CS cleanup - change package to PhpDocumentor
 * @todo       CS cleanup - change classname to PhpDocumentor_*
 */
class phpDocumentorTWordParser extends WordParser
{
    /**#@+
     * @access private
     */
    /**
     * tokenized array from {@link token_get_all()}
     * @var array
     */
    var $_all;
    /**
     * List of tokens that can contain a newline
     * @var array
     */
    var $_nl_check = array(
        T_WHITESPACE,
        T_ENCAPSED_AND_WHITESPACE,
        T_CONSTANT_ENCAPSED_STRING,
        T_COMMENT,
        T_DOC_COMMENT,
        T_OPEN_TAG,
        T_CLOSE_TAG,
        T_INLINE_HTML,
        T_START_HEREDOC,
    );
    /**
     * @var array
     */
    var $_global_search;
    /**
     * current source line number (relative)
     * @var integer
     */
    var $_sourceline;
    /**
     * Source of the entire file, parsed into arrays of tokens on each line
     * @var array
     */
    var $_file_source = array();
    /**
     * Line number the last comment was on
     * @var integer
     */
    var $_docblock_linenum;
    /**#@-*/
    
    /**
     * Uses {@link token_get_all()} to tokenize the source code.
     * {@internal
     * Also, it divides the source tokens into separate lines for use by
     * the @filesource tag.
     *
     * {@source}}}
     *
     * @param string &$input source code
     *
     * @return void
     */
    function setup(&$input)
    {
        $input      = rtrim(ltrim($input, "\r\n"));
        $this->data = &$input;
        // fix php warnings on invalid source code
        $this->_all         = @token_get_all($input);
        $this->_file_source = array();
        $this->addFileSource($this->_all);
        $this->_sourceline = 0;
        $this->pos         = 0;
        $this->linenum     = 0;
    }
    
    /**
     * loads up next set of source code
     *
     * @return array source code array
     */
    function getSource()
    {
        $source          = $this->source;
        $this->source    = array();
        $this->getsource = false;
        return $source;
    }
    
    /**
     * gets the source code tokens
     *
     * @return array source code tokens split up by line number
     */
    function getFileSource()
    {
        return $this->_file_source;
    }
    
    /**
     * Begin retrieving source code
     *
     * @param string $word word to add the beginning of source code
     *
     * @return void
     * @access private
     * @todo   CS cleanup - rename to retrieveSource for camelCase rule
     */
    function retrievesource($word = '')
    {
        $this->source      = array(array($word));
        $this->_sourceline = 0;
        $this->getsource   = true;
    }

    /**
     * Utility function to determine whether two tokens from the tokenizer are equal
     *
     * @param mixed $a first token
     * @param mixed $b second token
     *
     * @return bool whether or not the tokens are equal
     * @static
     */
    function tokenEquals($a, $b)
    {
        if (is_array($a)) $a = $a[1];
        if (is_array($b)) $b = $b[1];
        return $a == $b;
    }
    
    /**
     * Utility function to convert a series of tokens into a string
     *
     * @param array $a array of tokens
     *
     * @return string the resulting string
     * @static
     */
    function concatTokens($a)
    {
        $b = '';
        foreach ($a as $c) {
            if (is_array($c)) {
                $c = $c[1];
            }
            $b .= $c;
        }
        return $b;
    }
    
    /**
     * Retrieve a token for the phpDocumentorTParser
     * {@internal
     * This method adds source code to the array for a function to be returned
     * to a {@}source} tag, and will return the token unless it is T_WHITESPACE
     * and {@link $returnWhiteSpace} is false.
     *
     * The global variable search is more complicated than it is in the
     * WordParser, as we have to compare an array of tokens to each other, and
     * that is what this code does}}
     *
     * @return string|array token from tokenizer
     */
    function getWord()
    {
        if (!isset($this->_all[$this->pos])) {
            return false;
        }

        $oldlinenum = $this->linenum;
        $word       = $this->_all[$this->pos++];

        // if we're looking for a global variable declaration, then this section
        // will search the upcoming tokens to see if they match the tokens
        // that define the global variable
        if (isset($this->_global_search)) {
            $pos   = $this->pos;
            $gpos  = 0;
            $found = false;
            if ($this->tokenEquals($word, $this->_global_search[$gpos++])) {
                $found = true;
                for (;$gpos<count($this->_global_search);$gpos++, $pos++) {
                    if (isset($this->_all[$pos]) &&
                        !$this->tokenEquals($this->_global_search[$gpos], 
                            $this->_all[$pos])
                    ) {
                        $found = false;
                    }
                }
            }
            if ($found) {
                $a          = $this->concatTokens($this->_global_search);
                $this->pos += count($this->_global_search) - 1;
                unset($this->_global_search);
                return $a;
            }
        }
        if ($this->getsource) {
            $this->addSource($word);
        }
        if (is_array($word)) {
            if (in_array($word[0], $this->_nl_check)) {
                $this->linenum += substr_count($word[1], "\n");
            }
            if ($word[0] == T_WHITESPACE && !$this->returnWhiteSpace) {
                return $this->getWord();
            }
            // seeing if we can get line numbers out of the beast
        }
        if (is_array($word) && $word[0] == T_COMMENT) {
            $this->_docblock_linenum = $oldlinenum;
        }
        return $word;
    }
    
    /**
     * Wrapper for {@link addSource()} used to retrieve the entire source code
     * organized by line number in setup()
     *
     * @param array $word full file source code
     *
     * @return void
     */
    function addFileSource($word)
    {
        $this->_sourceline = 0;
        foreach ($word as $token) {
            $this->addSource($token, true);
        }
        // var_dump($this->_file_source);
    }
    
    /**
     * Generate source token arrays organized by line number
     *
     * This code will split up tokens that contain "\n" and add them to the
     * source code as separate tokens on different lines.
     *
     * @param array|string $word token to add
     * @param bool         $file true if this should be added 
     *                           to {@link $_file_source}
     *
     * @return void
     * @uses _set_sars()
     */
    function addSource($word, $file = false)
    {
        if (is_array($word)) {
            $lines = str_replace("\r", '', explode("\n", $word[1]));
            foreach ($lines as $i => $line) {
                $this->_set_sars($file, array($word[0], $line));
                if ($i < count($lines) - 1) {
                    // increment sourceline
                    $this->_sourceline++;
                }
            }
        } else {
            $this->_set_sars($file, $word);
        }
    }
    
    /**
     * Add tokens to source code
     *
     * {@source}
     *
     * @param bool         $type true if this is file source,
     *                           otherwise it is function source
     * @param string|array $word token to add
     *
     * @return void
     * @access private
     * @todo CS cleanup - rename to _setSars for camelCasing rule
     */
    function _set_sars($type, $word)
    {
        if ($type) {
            $this->_file_source[$this->_sourceline][] = $word;
        } else {
            $this->source[$this->_sourceline][] = $word;
        }
    }
    
    /**
     * Tell the phpDocumentorTWordParser to return the entire global variable
     * if it is found.
     *
     * @param array $tokens tokens that represent the global variable definition
     *
     * @return void
     * @uses $_global_search
     */
    function findGlobal($tokens)
    {
        if (!$tokens) {
            unset($this->_global_search);
        } else {
            $this->_global_search = $tokens;
        }
    }
    
    /**
     * backs the parser up to the previous position
     *
     * @return int|void can return a word
     */
    function backupPos()
    {
        $this->pos--;
        $word = $this->_all[$this->pos];
        if ($this->getsource) {
            unset($this->source[$this->_sourceline]
                [count($this->source[$this->_sourceline]) - 1]);
            if (empty($this->source[$this->_sourceline])) {
                unset($this->source[$this->_sourceline]);
            } else {
                $this->source[$this->_sourceline] 
                    = array_values($this->source[$this->_sourceline]);
            }
        }
        if (is_array($word)) {
            if ($word[0] == T_WHITESPACE && !$this->returnWhiteSpace) {
                return $this->getWord();
            }
            // seeing if we can get line numbers out of the beast
            if (in_array($word[0], $this->_nl_check)) {
                $this->linenum -= substr_count($word[1], "\n");
            }
        }
    }
}
?>