home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / WordParser.inc < prev    next >
Encoding:
Text File  |  2004-03-24  |  10.0 KB  |  326 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    Joshua Eichorn <jeichorn@phpdoc.org>
  22.  * @version    $Id: WordParser.inc,v 1.27.2.1 2003/06/03 05:44:52 CelloG Exp $
  23.  * @package     phpDocumentor
  24.  * @subpackage WordParsers
  25.  */
  26. /**
  27.  * Retrieves tokens from source code for use by the Parser
  28.  * @see Parser
  29.  * @author    Joshua Eichorn <jeichorn@phpdoc.org>
  30.  * @version    $Id: WordParser.inc,v 1.27.2.1 2003/06/03 05:44:52 CelloG Exp $
  31.  * @package     phpDocumentor
  32.  * @subpackage WordParsers
  33.  */
  34. class WordParser
  35. {
  36.     /*
  37.     New lines around the world
  38.     Macintosh: \r 
  39.         Unix : \n 
  40.     Windows : \r\n 
  41.      */
  42.     
  43.     /**#@+
  44.      * @access private
  45.      */
  46.     /**
  47.      * List of text that separates tokens, used to retrieve tokens
  48.      * @var array
  49.      */
  50.     var $wordseperators = array();
  51.     
  52.     /**
  53.      * Position within input of the cursor pointing to the next text to be
  54.      * retrieved as a token
  55.      * @var integer
  56.      */
  57.     var $pos = 0;
  58.  
  59.     /**
  60.      * Size of the input source code
  61.      * @var integer
  62.      */
  63.     var $size;
  64.  
  65.     /**
  66.      * Source code
  67.      * @var string
  68.      */
  69.     var $data;
  70.  
  71.     var $cache;
  72.     /**
  73.      * Current line number
  74.      * @var integer
  75.      */
  76.     var $linenum = 0;
  77.     /**
  78.      * Position the cursor was at the last time line numbers were counted, used
  79.      * to guarantee that line numbers are incremented
  80.      * @var integer
  81.      */
  82.     var $linenumpos = 0;
  83.     
  84.     /**
  85.      * Used for {@}source} tag, contains currently parsed function source
  86.      * @var string
  87.      */
  88.     var $source = '';
  89.     /**
  90.      * flag, determines whether tokens are added to {@link $source}
  91.      * @var boolean
  92.      */
  93.     var $getsource = false;
  94.  
  95.     /**
  96.      * If true, then white space is returned as a part of tokens, otherwise
  97.      * tokens are trimmed
  98.      * @var boolean
  99.      */
  100.     var $returnWhiteSpace = false;
  101.     /**#@-*/
  102.  
  103.     /**
  104.      * Initialize the WordParser
  105.      * @param string source code
  106.      */
  107.     function setup(&$input)
  108.     {
  109.         $this->size = strlen($input);
  110.         $this->data = & $input;
  111.         $this->pos = 0;
  112.         $this->linenum = 0;
  113.         $this->linenumpos = 0;
  114.         $this->cache = array();
  115.         //$this->run = 0;
  116.         //$this->word = WORD_PARSER_RET_WORD;
  117.     }
  118.     
  119.     /**
  120.      * Retrieve source code for the last function/method
  121.      * @return string
  122.      */
  123.     function getSource()
  124.     {
  125.         $source = $this->source;
  126.         $this->source = '';
  127.         $this->getsource = false;
  128.         return $source;
  129.     }
  130.     
  131.     /**
  132.      * Used to tell the WordParser to start retrieving source code
  133.      * @access private
  134.      */
  135.     function retrievesource($word = '')
  136.     {
  137.         $this->source = $word;
  138.         $this->getsource = true;
  139.     }
  140.  
  141.     /**
  142.      * Retrieve a token from the token list
  143.      *
  144.      * The {@link Parser} class relies upon this method to retrieve the next
  145.      * token.  The {@link $wordseperators} array is a collection of strings
  146.      * that delineate tokens for the current parser state.  $wordseperators
  147.      * is set by the parser with a call to {@link Parser::configWordParser()}
  148.      * every time a new parser state is reached.
  149.      *
  150.      * For example, while parsing the source code for a class, the word
  151.      * <code>var</code> is a token, and <code>global</code> is not,
  152.      * but inside a function, the reverse is true.  The parser state
  153.      * {@link PARSER_STATE_CLASS} has a token list that includes whitespace,
  154.      * code delimiters like ; and {}, and comment/DocBlock indicators
  155.      *
  156.      * If the whitespace option has been turned off using
  157.      * {@link setWhitespace()}, then no whitespace is returned with tokens
  158.      *
  159.      * {@internal
  160.      * In the first segment of the function, the code attempts to find the next
  161.      * token.  A cache is used to speed repetitious tasks.  The $tpos variable
  162.      * is used to hold the position of the next token.  $npos is used to
  163.      * hold the end of the token, and so $npos - $tpos will give the length
  164.      * of the token.  This is used to allow tokens that contain whitespace,
  165.      * should that option be desired.
  166.      *
  167.      * {@link $data} is of course the string containing the PHP code to be
  168.      * parsed, and {@link $pos} is the cursor, or current location within the
  169.      * parsed data.
  170.      * }}
  171.      * @return string|false the next token, an empty string if there are no
  172.      *                      token separators in the $wordseperators array,
  173.      *                      or false if the end of input has been reached
  174.      */
  175.     function getWord()
  176.     {
  177.         //$st = $this->mtime();
  178.         if ($this->size == $this->pos)
  179.         {
  180.             return false;
  181.         }
  182.  
  183.         // assume, for starting, that the token is from $this->pos to the end
  184.         $npos = $this->size;
  185.         if (is_array($this->wordseperators))
  186.         {
  187.             //$this->wordseperators = array();
  188.             foreach($this->wordseperators as $sep)
  189.             {
  190.                 // cache is set if this separator has been tested
  191.                 if (isset($this->cache[$sep]))
  192.                 $tpos = $this->cache[$sep];
  193.                 else
  194.                 $tpos = false;
  195.                 if ($tpos < $this->pos || !is_int($tpos))
  196.                 {
  197.                     // find the position of the next token separator
  198.                     $tpos = strpos($this->data,$sep,$this->pos);
  199.                 }
  200.  
  201.                 // was a token separator found that is closer to the current
  202.                 // location?
  203.                 if ( ($tpos < $npos) && !($tpos === false))
  204.                 {
  205.                     //echo trim($sep) . "=$tpos\n";
  206.                     // set the length of the token to be from $this->pos to
  207.                     // the next token separator
  208.                     $npos = $tpos;
  209.                     $seplen = strlen($sep);
  210.                 } 
  211.                   else if (!($tpos === false))
  212.                 {
  213.                     $this->cache[$sep] = $tpos;
  214.                 }
  215.             }
  216.         } else {
  217.             // no token separators, tell the parser to choose a new state
  218.             return "";
  219.         }
  220.  
  221.         $len = $npos - $this->pos;
  222.         if ($len == 0)
  223.         {
  224.             $len = $seplen;
  225.         }
  226.  
  227.         //$st3 = $this->mtime();
  228.         $word = substr($this->data,$this->pos,$len);
  229.         
  230.         // Change random other os newlines to the unix one
  231.         if ($word == "\r" || $word == "\r\n")
  232.         {
  233.             $word = "\n";
  234.         }
  235.         
  236.         if ($this->linenumpos <= $this->pos)
  237.         {
  238.             $this->linenumpos = $this->pos + $len;
  239.             $this->linenum += count(explode("\n",$word)) - 1;
  240.         }
  241.  
  242.         if ($this->getsource)
  243.         {
  244.             $this->source .= $word;
  245.         }
  246.         $this->pos = $this->pos + $len;
  247.         //$this->word = WORD_PARSER_RET_SEP;
  248.  
  249.         // Things like // commenats rely on the newline to find their end so im going to have to return them
  250.         // never return worthless white space /t ' '
  251.         if ($this->returnWhiteSpace == false)
  252.         {
  253.             if (strlen(trim($word)) == 0 && $word != "\n") 
  254.             {
  255.                 $word = $this->getWord();
  256.             }
  257.         }
  258.         //$this->time3 = $this->time3 + ($this->mtime() - $st3);
  259.         //$this->time = $this->time + ($this->mtime() - $st);
  260.         return $word;
  261.     }
  262.     
  263.  
  264.     /**
  265.      * Returns the current pointer position, or 1 character after the end of the word
  266.      */
  267.     function getPos()
  268.     {
  269.         return $this->pos;
  270.     }
  271.  
  272.     /**
  273.      * Unused
  274.      *
  275.      * {@source}
  276.      * @param integer starting position
  277.      * @param integer length of block to retrieve
  278.      */
  279.     function getBlock($start,$len)
  280.     {
  281.         return substr($this->data,$start,$len);
  282.     }
  283.  
  284.     /**
  285.      * @uses $wordseperators
  286.      * @param array array of strings that separate tokens
  287.      */
  288.     function setSeperator(&$seps)
  289.     {
  290.         $this->wordseperators = &$seps;
  291.     }
  292.  
  293.     /**
  294.      * Set the internal cursor within the source code
  295.      * @param integer
  296.      */
  297.     function setPos($pos)
  298.     {
  299.         $this->pos = $pos;
  300.     }
  301.     
  302.     /**
  303.      * Backup to the previous token so that it can be retrieved again in a new
  304.      * context.
  305.      *
  306.      * Occasionally, a word will be passed to an event handler that should be
  307.      * handled by another event handler.  This method allows that to happen.
  308.      * @param string token to back up to
  309.      */
  310.     function backupPos($word)
  311.     {
  312.         if ($this->getsource) $this->source = substr($this->source,0,strlen($this->source) - 1);
  313.         $this->pos = $this->pos - strlen($word);
  314.     }
  315.  
  316.     /**
  317.      * set parser to return or strip whitespace
  318.      * @param boolean
  319.      */
  320.     function setWhitespace($val = false)
  321.     {
  322.         $this->returnWhiteSpace = $val;
  323.     }
  324. }
  325. ?>
  326.