phpDocumentor phpDocumentor
WordParsers
[ class tree: phpDocumentor ] [ index: phpDocumentor ] [ all elements ]

Source for file phpDocumentorTWordParser.inc

Documentation is available at phpDocumentorTWordParser.inc

  1. <?php
  2. /**
  3.  * tokenizer extension-based lexer for PHP code
  4.  * 
  5.  * phpDocumentor :: automatic documentation generator
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * Copyright (c) 2002-2006 Gregory Beaver
  10.  * 
  11.  * LICENSE:
  12.  * 
  13.  * This library is free software; you can redistribute it
  14.  * and/or modify it under the terms of the GNU Lesser General
  15.  * Public License as published by the Free Software Foundation;
  16.  * either version 2.1 of the License, or (at your option) any
  17.  * later version.
  18.  * 
  19.  * This library is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22.  * Lesser General Public License for more details.
  23.  * 
  24.  * You should have received a copy of the GNU Lesser General Public
  25.  * License along with this library; if not, write to the Free Software
  26.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27.  *
  28.  * @package    phpDocumentor
  29.  * @subpackage Parsers
  30.  * @author     Gregory Beaver <[email protected]>
  31.  * @copyright  2002-2006 Gregory Beaver
  32.  * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
  33.  * @version    CVS: $Id: phpDocumentorTWordParser.inc,v 1.5 2006/04/30 22:18:14 cellog Exp $
  34.  * @link       http://www.phpdoc.org
  35.  * @link       http://pear.php.net/PhpDocumentor
  36.  * @since      1.2
  37.  */
  38. /**
  39.  * Like WordParser, but expects an array of tokens from the tokenizer instead
  40.  * of a string.
  41.  * @author Greg Beaver <[email protected]>
  42.  * @package phpDocumentor
  43.  * @subpackage WordParsers
  44.  * @since 1.2
  45.  */
  46. {
  47.     /**#@+
  48.      * @access private
  49.      */
  50.     /**
  51.      * tokenized array from {@link token_get_all()}
  52.      * @var array 
  53.      */
  54.     var $_all;
  55.     /**
  56.      * List of tokens that can contain a newline
  57.      * @var array 
  58.      */
  59.     var $_nl_check array(
  60.         T_WHITESPACE,
  61.         T_ENCAPSED_AND_WHITESPACE,
  62.         T_CONSTANT_ENCAPSED_STRING,
  63.         T_COMMENT,
  64.         T_DOC_COMMENT,
  65.         T_OPEN_TAG,
  66.         T_CLOSE_TAG,
  67.         T_INLINE_HTML);
  68.     /**
  69.      * @var array 
  70.      */
  71.     var $_global_search;
  72.     /**
  73.      * current source line number (relative)
  74.      * @var integer 
  75.      */
  76.     var $_sourceline;
  77.     /**
  78.      * Source of the entire file, parsed into arrays of tokens on each line
  79.      * @var array 
  80.      */
  81.     var $_file_source array();
  82.     /**
  83.      * Line number the last comment was on
  84.      * @var integer 
  85.      */
  86.     var $_docblock_linenum;
  87.     /**#@-*/
  88.     
  89.     /**
  90.      * Uses {@link token_get_all()} to tokenize the source code.
  91.      * {@internal 
  92.      * Also, it divides the source tokens into separate lines for use by
  93.      * the @filesource tag.
  94.      *
  95.      * {@source } }
  96.      * @var string source code
  97.      */
  98.     function setup(&$input)
  99.     {
  100.         $input rtrim(ltrim($input"\r\n"));
  101.         $this->data &$input;
  102.         // fix php warnings on invalid source code
  103.         $this->_all @token_get_all($input);
  104.         $this->_file_source array();
  105.         $this->addFileSource($this->_all);
  106.         $this->_sourceline 0;
  107.         $this->pos 0;
  108.         $this->linenum 0;
  109.     }
  110.     
  111.     /**
  112.      * @return array 
  113.      */
  114.     function getSource()
  115.     {
  116.         $source $this->source;
  117.         $this->source array();
  118.         $this->getsource false;
  119.         return $source;
  120.     }
  121.     
  122.     /**
  123.      * @return array source code tokens split up by line number
  124.      */
  125.     function getFileSource()
  126.     {
  127.         return $this->_file_source;
  128.     }
  129.     
  130.     /**
  131.      * Begin retrieving source code
  132.      * @access private
  133.      * @param string word to add the beginning of source code
  134.      */
  135.     function retrievesource($word '')
  136.     {
  137.         $this->source array(array($word));
  138.         $this->_sourceline 0;
  139.         $this->getsource true;
  140.     }
  141.  
  142.     /**
  143.      * Utility function to determine whether two tokens from the tokenizer are equal
  144.      * @static
  145.      */
  146.     function tokenEquals($a$b)
  147.     {
  148.         if (is_array($a)) $a $a[1];
  149.         if (is_array($b)) $b $b[1];
  150.         return $a == $b;
  151.     }
  152.     
  153.     /**
  154.      * Utility function to convert a series of tokens into a string
  155.      * @static
  156.      */
  157.     function concatTokens($a)
  158.     {
  159.         $b '';
  160.         foreach($a as $c)
  161.         {
  162.             if (is_array($c)) $c $c[1];
  163.             $b .= $c;
  164.         }
  165.         return $b;
  166.     }
  167.     
  168.     /**
  169.      * Retrieve a token for the phpDocumentorTParser
  170.      * {@internal 
  171.      * This method adds source code to the array for a function to be returned
  172.      * to a {@}source} tag, and will return the token unless it is T_WHITESPACE
  173.      * and {@link $returnWhiteSpace} is false.
  174.      *
  175.      * The global variable search is more complicated than it is in the
  176.      * WordParser, as we have to compare an array of tokens to each other, and
  177.      * that is what this code does}}}
  178.      * @return string|arraytoken from tokenizer
  179.      */
  180.     function getWord()
  181.     {
  182.         if (!isset($this->_all[$this->pos])) return false;
  183.         $oldlinenum $this->linenum;
  184.         $word $this->_all[$this->pos++];
  185.         // if we're looking for a global variable declaration, then this section
  186.         // will search the upcoming tokens to see if they match the tokens
  187.         // that define the global variable
  188.         if (isset($this->_global_search))
  189.         {
  190.             $pos $this->pos;
  191.             $gpos 0;
  192.             $found false;
  193.             if ($this->tokenEquals($word,$this->_global_search[$gpos++]))
  194.             {
  195.                 $found true;
  196.                 for(;$gpos<count($this->_global_search);$gpos++,$pos++)
  197.                 {
  198.                     if (!$this->tokenEquals($this->_global_search[$gpos],$this->_all[$pos])) $found false;
  199.                 }
  200.             }
  201.             if ($found)
  202.             {
  203.                 $a $this->concatTokens($this->_global_search);
  204.                 $this->pos += count($this->_global_search1;
  205.                 unset($this->_global_search);
  206.                 return $a;
  207.             }
  208.         }
  209.         if ($this->getsource)
  210.         {
  211.             $this->addSource($word);
  212.         }
  213.         if (is_array($word))
  214.         {
  215.             if (in_array($word[0],$this->_nl_check))
  216.             {
  217.                 $this->linenum += substr_count($word[1],"\n");
  218.             }
  219.             if ($word[0== T_WHITESPACE && !$this->returnWhiteSpacereturn $this->getWord();
  220.             // seeing if we can get line numbers out of the beast
  221.         }
  222.         if (is_array($word&& $word[0== T_COMMENT$this->_docblock_linenum $oldlinenum;
  223.         return $word;
  224.     }
  225.     
  226.     /**
  227.      * Wrapper for {@link addSource()} used to retrieve the entire source code
  228.      * organized by line number in setup()
  229.      * @param array full file source code
  230.      */
  231.     function addFileSource($word)
  232.     {
  233.         $this->_sourceline 0;
  234.         foreach($word as $token)
  235.         {
  236.             $this->addSource($tokentrue);
  237.         }
  238. //        var_dump($this->_file_source);
  239.     }
  240.     
  241.     /**
  242.      * Generate source token arrays organized by line number
  243.      *
  244.      * This code will split up tokens that contain "\n" and add them to the
  245.      * source code as separate tokens on different lines.
  246.      * @param array|stringtoken to add
  247.      * @param boolean true if this should be added to {@link $_file_source}
  248.      * @param array|stringnext token, for lookahead splitting
  249.      * @uses _set_sars()
  250.      */
  251.     function addSource($word$file false)
  252.     {
  253.         if (is_array($word))
  254.         {
  255.             $lines str_replace("\r"''explode("\n",$word[1]));
  256.             foreach($lines as $i => $line)
  257.             {
  258.                 $this->_set_sars($filearray($word[0],$line));
  259.                 if ($i count($lines1)
  260.                 {
  261.                     // increment sourceline
  262.                     $this->_sourceline++;
  263.                 }
  264.             }
  265.         else $this->_set_sars($file$word);
  266.     }
  267.     
  268.     /**
  269.      * Add tokens to source code
  270.      *
  271.      * {@source } 
  272.      * @access private
  273.      * @param boolean true if this is file source, otherwise it is function source
  274.      * @param string|arraytoken to add
  275.      */
  276.     function _set_sars($type,$word)
  277.     {
  278.         if ($type)
  279.         {
  280.             $this->_file_source[$this->_sourceline][$word;
  281.         else
  282.         {
  283.             $this->source[$this->_sourceline][$word;
  284.         }
  285.     }
  286.     
  287.     /**
  288.      * Tell the phpDocumentorTWordParser to return the entire global variable
  289.      * if it is found.
  290.      * @uses $_global_search
  291.      * @param array tokens that represent the global variable definition
  292.      */
  293.     function findGlobal($tokens)
  294.     {
  295.         if (!$tokens)
  296.         {
  297.             unset($this->_global_search);
  298.         else
  299.         $this->_global_search $tokens;
  300.     }
  301.     
  302.     function backupPos()
  303.     {
  304.         $this->pos--;
  305.         $word $this->_all[$this->pos];
  306.         if ($this->getsource)
  307.         {
  308.             unset($this->source[$this->_sourceline][count($this->source[$this->_sourceline]1]);
  309.             if (empty($this->source[$this->_sourceline])) unset($this->source[$this->_sourceline]);
  310.             else $this->source[$this->_sourcelinearray_values($this->source[$this->_sourceline]);
  311.         }
  312.         if (is_array($word))
  313.         {
  314.             if ($word[0== T_WHITESPACE && !$this->returnWhiteSpacereturn $this->getWord();
  315.             // seeing if we can get line numbers out of the beast
  316.             if (in_array($word[0],$this->_nl_check))
  317.             {
  318.                 $this->linenum -= substr_count($word[1],"\n");
  319.             }
  320.         }
  321.     }
  322. }
  323. ?>

Documentation generated on Tue, 24 Oct 2006 09:25:55 -0500 by phpDocumentor 1.3.1