Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Source Code Highlighting
4
 *
5
 * The classes in this file are responsible for the dynamic @example, and
6
 * <programlisting role="tutorial"> tags output.  Using the
7
 * WordParser, the phpDocumentor_TutorialHighlightParser
8
 * retrieves PHP tokens one by one from the array generated by
9
 * {@link WordParser} source retrieval functions
10
 * and then highlights them individually.
11
 *
12
 * It accomplishes this highlighting through the assistance of methods in
13
 * the output Converter passed to its parse() method, and then returns the
14
 * fully highlighted source as a string
15
 *
16
 * phpDocumentor :: automatic documentation generator
17
 *
18
 * PHP versions 4 and 5
19
 *
20
 * Copyright (c) 2003-2007 Gregory Beaver
21
 *
22
 * LICENSE:
23
 *
24
 * This library is free software; you can redistribute it
25
 * and/or modify it under the terms of the GNU Lesser General
26
 * Public License as published by the Free Software Foundation;
27
 * either version 2.1 of the License, or (at your option) any
28
 * later version.
29
 *
30
 * This library is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33
 * Lesser General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU Lesser General Public
36
 * License along with this library; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38
 *
39
 * @category   ToolsAndUtilities
40
 * @package    phpDocumentor
41
 * @subpackage Parsers
42
 * @author     Gregory Beaver <cellog@php.net>
43
 * @copyright  2003-2007 Gregory Beaver
44
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
45
 * @version    CVS: $Id: TutorialHighlightParser.inc 246148 2007-11-14 01:57:04Z ashnazg $
46
 * @tutorial   tags.example.pkg, tags.filesource.pkg
47
 * @link       http://www.phpdoc.org
48
 * @link       http://pear.php.net/PhpDocumentor
49
 * @since      1.3.0
50
 * @todo       CS cleanup - change package to PhpDocumentor
51
 * @todo       CS cleanup - PHPCS needs to ignore CVS Id length
52
 */
53
 
54
/**
55
 * Highlights source code using {@link parse()}
56
 *
57
 * @category   ToolsAndUtilities
58
 * @package    phpDocumentor
59
 * @subpackage Parsers
60
 * @author     Gregory Beaver <cellog@php.net>
61
 * @copyright  2003-2007 Gregory Beaver
62
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
63
 * @version    Release: 1.4.3
64
 * @link       http://www.phpdoc.org
65
 * @link       http://pear.php.net/PhpDocumentor
66
 * @todo       CS cleanup - change package to PhpDocumentor
67
 * @todo       CS cleanup - change classname to PhpDocumentor_*
68
 */
69
class phpDocumentor_TutorialHighlightParser extends Parser
70
{
71
    /**#@+ @access private */
72
    /**
73
     * Highlighted source is built up in this string
74
     * @var string
75
     */
76
    var $_output;
77
    /**
78
     * contents of the current source code line as it is parsed
79
     * @var string
80
     */
81
    var $_line;
82
    /**
83
     * Used to retrieve highlighted tokens
84
     * @var Converter a descendant of Converter
85
     */
86
    var $_converter;
87
    /**
88
     * Path to file being highlighted, if this is from a @filesource tag
89
     * @var false|string full path
90
     */
91
    var $_filesourcepath;
92
    /**
93
     * @var array
94
     */
95
    var $eventHandlers = array(
96
        TUTORIAL_EVENT_NOEVENTS    => 'defaultHandler',
97
        TUTORIAL_EVENT_ITAG        => 'defaultHandler',
98
        TUTORIAL_EVENT_ATTRIBUTE   => 'attrHandler',
99
        TUTORIAL_EVENT_OPENTAG     => 'defaultHandler',
100
        TUTORIAL_EVENT_CLOSETAG    => 'defaultHandler',
101
        TUTORIAL_EVENT_ENTITY      => 'defaultHandler',
102
        TUTORIAL_EVENT_COMMENT     => 'defaultHandler',
103
        TUTORIAL_EVENT_SINGLEQUOTE => 'defaultHandler',
104
        TUTORIAL_EVENT_DOUBLEQUOTE => 'defaultHandler',
105
    );
106
    /**#@-*/
107
 
108
    /**
109
     * advances output to a new line
110
     *
111
     * @return void
112
     * @uses Converter::SourceLine() encloses {@link $_line} in a
113
     *                               converter-specific format
114
     */
115
    function newLineNum()
116
    {
117
        $this->_line   .= $this->_converter->flushHighlightCache();
118
        $this->_output .= $this->_converter->SourceLine($this->_pv_curline + 1,
119
            $this->_line, $this->_path);
120
        $this->_line    = '';
121
    }
122
 
123
    /**
124
     * Start the parsing at a certain line number
125
     *
126
     * @param int $num the line number
127
     *
128
     * @return void
129
     */
130
    function setLineNum($num)
131
    {
132
        $this->_wp->linenum = $num;
133
    }
134
 
135
    /**
136
     * Parse a new file
137
     *
138
     * The parse() method is a do...while() loop that retrieves tokens one by
139
     * one from the {@link $_event_stack}, and uses the token event array set up
140
     * by the class constructor to call event handlers.
141
     *
142
     * The event handlers each process the tokens passed to them, and use the
143
     * {@link _addoutput()} method to append the processed tokens to the
144
     * {@link $_line} variable.  The word parser calls {@link newLineNum()}
145
     * every time a line is reached.
146
     *
147
     * In addition, the event handlers use special linking functions
148
     * {@link _link()} and its cousins (_classlink(), etc.) to create in-code
149
     * hyperlinks to the documentation for source code elements that are in the
150
     * source code.
151
     *
152
     * @param string        $parse_data     blah
153
     * @param Converter     &$converter     blah
154
     * @param false|string  $filesourcepath full path to file with @filesource tag,
155
     *                                      if this is a @filesource parse
156
     * @param false|integer $linenum        starting line number from
157
     *                                      {@}source linenum}
158
     *
159
     * @staticvar integer used for recursion limiting if a handler for
160
     *                    an event is not found
161
     * @return    bool
162
     * @uses setupStates() initialize parser state variables
163
     * @uses configWordParser() pass $parse_data to prepare retrieval of tokens
164
     * @todo CS cleanup - unable to get function signature below 85char wide
165
     */
166
    function parse($parse_data, &$converter, $filesourcepath = false, $linenum = false)
167
    {
168
        static $endrecur  = 0;
169
        $parse_data       =
170
            str_replace(array("\r\n", "\t"), array("\n", '    '), $parse_data);
171
        $this->_converter = &$converter;
172
        $converter->startHighlight();
173
        $this->_path = $filesourcepath;
174
        $this->setupStates($parse_data);
175
 
176
        $this->configWordParser(TUTORIAL_EVENT_NOEVENTS);
177
        if ($linenum !== false) {
178
            $this->setLineNum($linenum);
179
        }
180
        // initialize variables so E_ALL error_reporting doesn't complain
181
        $pevent = 0;
182
        $word   = 0;
183
 
184
        do {
185
            $lpevent = $pevent;
186
            $pevent  = $this->_event_stack->getEvent();
187
            if ($lpevent != $pevent) {
188
                $this->_last_pevent = $lpevent;
189
                $this->configWordParser($pevent);
190
            }
191
            $this->_wp->setWhitespace(true);
192
 
193
            $dbg_linenum         = $this->_wp->linenum;
194
            $dbg_pos             = $this->_wp->getPos();
195
            $this->_pv_last_word = $word;
196
            $this->_pv_curline   = $this->_wp->linenum;
197
            $word                = $this->_wp->getWord();
198
 
199
            if (PHPDOCUMENTOR_DEBUG == true) {
200
                echo "LAST: ";
201
                echo "|" . $this->_pv_last_word;
202
                echo "|\n";
203
                echo "PEVENT: " . $this->getParserEventName($pevent) . "\n";
204
                echo "LASTPEVENT: " .
205
                    $this->getParserEventName($this->_last_pevent) . "\n";
206
                //DEBUG echo "LINE: " . $this->_line . "\n";
207
                //DEBUG echo "OUTPUT: " . $this->_output . "\n";
208
                echo $dbg_linenum.'-'.$dbg_pos . ": ";
209
                echo '|'.htmlspecialchars($word);
210
                echo "|\n";
211
                echo "-------------------\n\n\n";
212
                flush();
213
            }
214
            if (isset($this->eventHandlers[$pevent])) {
215
                $handle = $this->eventHandlers[$pevent];
216
                $this->$handle($word, $pevent);
217
            } else {
218
                debug('WARNING: possible error, no handler for event number '
219
                    . $pevent);
220
                if ($endrecur++ == 25) {
221
                    die("FATAL ERROR, recursion limit reached");
222
                }
223
            }
224
        } while (!($word === false));
225
        if (strlen($this->_line)) {
226
            $this->newLineNum();
227
        }
228
        return $this->_output;
229
    }
230
 
231
    /**#@+
232
     * Event Handlers
233
     *
234
     * All Event Handlers use {@link checkEventPush()} and
235
     * {@link checkEventPop()} to set up the event stack and parser state.
236
     *
237
     * @param string|array $word   token value
238
     * @param integer      $pevent parser event from {@link Parser.inc}
239
     *
240
     * @return void
241
     * @access private
242
     */
243
    /**
244
     * Most tokens only need highlighting, and this method handles them
245
     *
246
     * @todo CS cleanup - PHPCS needs to recognize docblock template tags
247
     */
248
    function defaultHandler($word, $pevent)
249
    {
250
        if ($word == "\n") {
251
            $this->newLineNum();
252
            return;
253
        }
254
        if ($this->checkEventPush($word, $pevent)) {
255
            $this->_wp->backupPos($word);
256
            return;
257
        }
258
        $this->_addoutput($word);
259
        $this->checkEventPop($word, $pevent);
260
    }
261
 
262
    /**
263
     * Most tokens only need highlighting, and this method handles them
264
     *
265
     * @todo CS cleanup - PHPCS needs to recognize docblock template tags
266
     */
267
    function attrHandler($word, $pevent)
268
    {
269
        if ($word == "\n") {
270
            $this->newLineNum();
271
            return;
272
        }
273
        if ($e = $this->checkEventPush($word, $pevent)) {
274
            if ($e == TUTORIAL_EVENT_SINGLEQUOTE
275
                || $e == TUTORIAL_EVENT_DOUBLEQUOTE
276
            ) {
277
                $this->_addoutput($word);
278
            }
279
            return;
280
        }
281
        if ($this->checkEventPop($word, $pevent)) {
282
            $this->_wp->backupPos($word);
283
            return;
284
        }
285
        $this->_addoutput($word);
286
    }
287
    /**#@-*/
288
 
289
    /**#@+
290
     * Output Methods
291
     * @access private
292
     */
293
    /**
294
     * This method adds output to {@link $_line}
295
     *
296
     * If a string with variables like "$test this" is present, then special
297
     * handling is used to allow processing of the variable in context.
298
     *
299
     * @param string $word         the output to add
300
     * @param bool   $preformatted whether or not its preformatted
301
     *
302
     * @return void
303
     * @see _flush_save()
304
     */
305
    function _addoutput($word, $preformatted = false)
306
    {
307
        $type = array(
308
            TUTORIAL_EVENT_ATTRIBUTE   => 'attribute',
309
            TUTORIAL_EVENT_SINGLEQUOTE => 'attributevalue',
310
            TUTORIAL_EVENT_DOUBLEQUOTE => 'attributevalue',
311
            TUTORIAL_EVENT_CLOSETAG    => 'closetag',
312
            TUTORIAL_EVENT_ENTITY      => 'entity',
313
            TUTORIAL_EVENT_ITAG        => 'itag',
314
            TUTORIAL_EVENT_OPENTAG     => 'opentag',
315
            TUTORIAL_EVENT_COMMENT     => 'comment',
316
        );
317
 
318
        $a = $this->_event_stack->getEvent();
319
        if (in_array($a, array_keys($type))) {
320
            $this->_line .=
321
                $this->_converter->highlightTutorialSource($type[$a], $word);
322
        } else {
323
            $this->_line .= $this->_converter->flushHighlightCache();
324
            $this->_line .= $this->_converter->postProcess($word);
325
        }
326
    }
327
    /**#@-*/
328
 
329
    /**
330
     * Tell the parser's WordParser {@link $wp} to set up tokens to parse words by.
331
     *
332
     * Tokens are word separators.  In English, a space or punctuation are
333
     * examples of tokens.  In PHP, a token can be a ;, a parenthesis, or
334
     * even the word "function"
335
     *
336
     * @param integer $e an event number
337
     *
338
     * @return void
339
     * @see WordParser
340
     */
341
    function configWordParser($e)
342
    {
343
        $this->_wp->setSeperator($this->tokens[($e + 100)]);
344
    }
345
 
346
    /**#@+
347
     * @param string|array $word   token value
348
     * @param integer      $pevent parser event from {@link Parser.inc}
349
     *
350
     * @return mixed returns false, or the event number
351
     */
352
    /**
353
     * This function checks whether parameter $word is a token
354
     * for pushing a new event onto the Event Stack.
355
     *
356
     * @todo CS cleanup - PHPCS needs to recognize docblock template tags
357
     */
358
    function checkEventPush($word, $pevent)
359
    {
360
        $e = false;
361
        if (isset($this->pushEvent[$pevent])) {
362
            if (isset($this->pushEvent[$pevent][strtolower($word)])) {
363
                $e = $this->pushEvent[$pevent][strtolower($word)];
364
            }
365
        }
366
        if ($e) {
367
            $this->_event_stack->pushEvent($e);
368
            return $e;
369
        } else {
370
            return false;
371
        }
372
    }
373
 
374
    /**
375
     * This function checks whether parameter $word is a token
376
     * for popping the current event off of the Event Stack.
377
     *
378
     * @todo CS cleanup - PHPCS needs to recognize docblock template tags
379
     */
380
    function checkEventPop($word, $pevent)
381
    {
382
        if (!isset($this->popEvent[$pevent])) {
383
            return false;
384
        }
385
        if (in_array(strtolower($word), $this->popEvent[$pevent])) {
386
            return $this->_event_stack->popEvent();
387
        } else {
388
            return false;
389
        }
390
    }
391
    /**#@-*/
392
 
393
    /**
394
     * Initialize all parser state variables
395
     *
396
     * @param bool|string $parsedata true if we are highlighting an inline {@}source}
397
     *                               tag's output, or the name of class we are going
398
     *                               to start from
399
     *
400
     * @return void
401
     * @uses $_wp sets to a new {@link phpDocumentor_HighlightWordParser}
402
     */
403
    function setupStates($parsedata)
404
    {
405
        $this->_output = '';
406
        $this->_line   = '';
407
 
408
        unset($this->_wp);
409
        $this->_wp = new WordParser;
410
        $this->_wp->setup($parsedata);
411
 
412
        $this->_event_stack = new EventStack;
413
        $this->_event_stack->popEvent();
414
        $this->_event_stack->pushEvent(TUTORIAL_EVENT_NOEVENTS);
415
 
416
        $this->_pv_linenum   = null;
417
        $this->_pv_next_word = false;
418
    }
419
 
420
    /**
421
     * Initialize the {@link $tokenpushEvent, $wordpushEvent} arrays
422
     */
423
    function phpDocumentor_TutorialHighlightParser()
424
    {
425
        $this->allowableInlineTags                =
426
            $GLOBALS['_phpDocumentor_inline_tutorial_tags_allowed']
427
        ;
428
        $this->inlineTagHandlers                  =
429
            array('*' => 'handleDefaultInlineTag')
430
        ;
431
        $this->tokens[STATE_TUTORIAL_NOEVENTS]    =
432
            array("\n",'{@', '<!--', '</', '<', '&');
433
        $this->tokens[STATE_TUTORIAL_ITAG]        = array("\n","}");
434
        $this->tokens[STATE_TUTORIAL_OPENTAG]     = array("\n","\t"," ", '>', '/>');
435
        $this->tokens[STATE_TUTORIAL_CLOSETAG]    = array("\n",'>');
436
        $this->tokens[STATE_TUTORIAL_COMMENT]     = array("\n",'-->');
437
        $this->tokens[STATE_TUTORIAL_ENTITY]      = array("\n",';');
438
        $this->tokens[STATE_TUTORIAL_ATTRIBUTE]   = array("\n",'"',"'",'>','/>');
439
        $this->tokens[STATE_TUTORIAL_DOUBLEQUOTE] = array("\n",'"','&','{@');
440
        $this->tokens[STATE_TUTORIAL_SINGLEQUOTE] = array("\n","'",'&','{@');
441
        /**************************************************************/
442
 
443
        $this->pushEvent[TUTORIAL_EVENT_NOEVENTS] = array(
444
            "{@"   => TUTORIAL_EVENT_ITAG,
445
            '<'    => TUTORIAL_EVENT_OPENTAG,
446
            '</'   => TUTORIAL_EVENT_CLOSETAG,
447
            '&'    => TUTORIAL_EVENT_ENTITY,
448
            '<!--' => TUTORIAL_EVENT_COMMENT,
449
        );
450
        /**************************************************************/
451
 
452
        $this->pushEvent[TUTORIAL_EVENT_OPENTAG] = array(
453
            " "  => TUTORIAL_EVENT_ATTRIBUTE,
454
            "\n" => TUTORIAL_EVENT_ATTRIBUTE,
455
        );
456
        /**************************************************************/
457
 
458
        $this->pushEvent[TUTORIAL_EVENT_ATTRIBUTE] = array(
459
            "'" => TUTORIAL_EVENT_SINGLEQUOTE,
460
            '"' => TUTORIAL_EVENT_DOUBLEQUOTE,
461
        );
462
        /**************************************************************/
463
 
464
        $this->pushEvent[TUTORIAL_EVENT_SINGLEQUOTE] = array(
465
            '&'  => TUTORIAL_EVENT_ENTITY,
466
            '{@' => TUTORIAL_EVENT_ITAG,
467
        );
468
        /**************************************************************/
469
 
470
        $this->pushEvent[TUTORIAL_EVENT_DOUBLEQUOTE] = array(
471
            '&'  => TUTORIAL_EVENT_ENTITY,
472
            '{@' => TUTORIAL_EVENT_ITAG,
473
        );
474
        /**************************************************************/
475
 
476
        $this->popEvent[TUTORIAL_EVENT_ENTITY] = array(';');
477
        /**************************************************************/
478
 
479
        $this->popEvent[TUTORIAL_EVENT_SINGLEQUOTE] = array("'");
480
        /**************************************************************/
481
 
482
        $this->popEvent[TUTORIAL_EVENT_DOUBLEQUOTE] = array('"');
483
        /**************************************************************/
484
 
485
        $this->popEvent[TUTORIAL_EVENT_OPENTAG] = array('>', '/>');
486
        /**************************************************************/
487
 
488
        $this->popEvent[TUTORIAL_EVENT_CLOSETAG] = array('>');
489
        /**************************************************************/
490
 
491
        $this->popEvent[TUTORIAL_EVENT_COMMENT] = array('-->');
492
        /**************************************************************/
493
 
494
        $this->popEvent[TUTORIAL_EVENT_ATTRIBUTE] = array('>','/>');
495
        /**************************************************************/
496
 
497
        $this->popEvent[TUTORIAL_EVENT_ITAG] = array('}');
498
        /**************************************************************/
499
    }
500
 
501
    /**
502
     * searches for a parser event name based on its number
503
     *
504
     * @param int $value the event number
505
     *
506
     * @return string|int the event name, or the original value
507
     */
508
    function getParserEventName ($value)
509
    {
510
        $lookup = array(
511
            TUTORIAL_EVENT_NOEVENTS    => "TUTORIAL_EVENT_NOEVENTS",
512
            TUTORIAL_EVENT_ITAG        => "TUTORIAL_EVENT_ITAG",
513
            TUTORIAL_EVENT_OPENTAG     => "TUTORIAL_EVENT_OPENTAG",
514
            TUTORIAL_EVENT_ATTRIBUTE   => "TUTORIAL_EVENT_ATTRIBUTE",
515
            TUTORIAL_EVENT_CLOSETAG    => "TUTORIAL_EVENT_CLOSETAG",
516
            TUTORIAL_EVENT_ENTITY      => "TUTORIAL_EVENT_ENTITY",
517
            TUTORIAL_EVENT_COMMENT     => "TUTORIAL_EVENT_COMMENT",
518
            TUTORIAL_EVENT_SINGLEQUOTE => "TUTORIAL_EVENT_SINGLEQUOTE",
519
            TUTORIAL_EVENT_DOUBLEQUOTE => "TUTORIAL_EVENT_DOUBLEQUOTE",
520
        );
521
        if (isset($lookup[$value])) {
522
            return $lookup[$value];
523
        } else {
524
            return $value;
525
        }
526
    }
527
}
528
 
529
 
530
/**
531
 * starting state
532
 */
533
define("TUTORIAL_EVENT_NOEVENTS", 1);
534
 
535
/**
536
 * currently in starting state
537
 */
538
define("STATE_TUTORIAL_NOEVENTS", 101);
539
 
540
/**
541
 * used when an {@}inline tag} is found
542
 */
543
define("TUTORIAL_EVENT_ITAG", 2);
544
 
545
/**
546
 * currently parsing an {@}inline tag}
547
 */
548
define("STATE_TUTORIAL_ITAG", 102);
549
 
550
/**
551
 * used when an open <tag> is found
552
 */
553
define("TUTORIAL_EVENT_OPENTAG", 3);
554
 
555
/**
556
 * currently parsing an open <tag>
557
 */
558
define("STATE_TUTORIAL_OPENTAG", 103);
559
 
560
/**
561
 * used when a <tag attr="attribute"> is found
562
 */
563
define("TUTORIAL_EVENT_ATTRIBUTE", 4);
564
 
565
/**
566
 * currently parsing an open <tag>
567
 */
568
define("STATE_TUTORIAL_ATTRIBUTE", 104);
569
 
570
/**
571
 * used when a close </tag> is found
572
 */
573
define("TUTORIAL_EVENT_CLOSETAG", 5);
574
 
575
/**
576
 * currently parsing a close </tag>
577
 */
578
define("STATE_TUTORIAL_CLOSETAG", 105);
579
 
580
/**
581
 * used when an &entity; is found
582
 */
583
define("TUTORIAL_EVENT_ENTITY", 6);
584
 
585
/**
586
 * currently parsing an &entity;
587
 */
588
define("STATE_TUTORIAL_ENTITY", 106);
589
 
590
/**
591
 * used when a <!-- comment --> is found
592
 */
593
define("TUTORIAL_EVENT_COMMENT", 7);
594
 
595
/**
596
 * currently parsing a <!-- comment -->
597
 */
598
define("STATE_TUTORIAL_COMMENT", 107);
599
 
600
/**
601
 * used when a <!-- comment --> is found
602
 */
603
define("TUTORIAL_EVENT_SINGLEQUOTE", 8);
604
 
605
/**
606
 * currently parsing a <!-- comment -->
607
 */
608
define("STATE_TUTORIAL_SINGLEQUOTE", 108);
609
 
610
/**
611
 * used when a <!-- comment --> is found
612
 */
613
define("TUTORIAL_EVENT_DOUBLEQUOTE", 9);
614
 
615
/**
616
 * currently parsing a <!-- comment -->
617
 */
618
define("STATE_TUTORIAL_DOUBLEQUOTE", 109);
619
 
620
?>