home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / phpDocumentorTWordParser.inc < prev    next >
Encoding:
Text File  |  2004-03-24  |  9.9 KB  |  315 lines

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