PHP_Parser_DocblockParser
[ class tree: PHP_Parser_DocblockParser ] [ index: PHP_Parser_DocblockParser ] [ all elements ]

Source for file Tokenizer.php

Documentation is available at Tokenizer.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:  nobody <nobody@localhost>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Tokenizer.php 231255 2007-03-05 20:42:42Z cellog $
  20. //
  21.  
  22. require_once 'PHP/Parser/DocblockParser.php';
  23. /**
  24. * The tokenizer wrapper for parser - implements the 'standard?' yylex interface
  25. *
  26. * 2 main methods:
  27. *  <ul>
  28. *   <li>constructor, which takes the data to parse
  29. *     calls php's internal tokenizer, then tidies up the array
  30. *     a little (key=>value) rather than mixed type.</li>
  31. *   <li>advance, which returns true while tokens are available
  32. *       - sets {@link $value}
  33. *       - sets {@link $token}
  34. *   </li>
  35. *   <li>parseError, which returns a string to appear on parser error messages.
  36. *       (could also display some of the code that has an error)
  37. *   </li>
  38. *
  39. * uses a few flags like:
  40. *   - {@link $line} - current line number
  41. *   - {@link $pos} - current token id
  42. *   - {@link $N} - total no. of tokens
  43. @version    $Id: Tokenizer.php 231255 2007-03-05 20:42:42Z cellog $
  44. */
  45.  
  46. class PHP_Parser_DocblockParser_Tokenizer {
  47.     
  48.          
  49.     /**
  50.     * Debugging on/off
  51.     *
  52.     * @var boolean 
  53.     * @access public
  54.     */
  55.     var $debug = false;
  56.     /**
  57.     * Tokens - array of all the tokens.
  58.     *
  59.     * @var array 
  60.     * @access public
  61.     */
  62.     var $tokens;
  63.     /**
  64.     * Total Number of tokens.
  65.     *
  66.     * @var int 
  67.     * @access public
  68.     */
  69.     var $N = 0;
  70.     /**
  71.     * Current line.
  72.     *
  73.     * @var int 
  74.     * @access public
  75.     */
  76.     var $line;
  77.     /**
  78.     * Current token position.
  79.     *
  80.     * @var int 
  81.     * @access public
  82.     */
  83.     var $pos = -1;
  84.     /**
  85.     * The current token (either a ord(';') or token numer - see php tokenizer.
  86.     *
  87.     * @var int 
  88.     * @access public
  89.     */ 
  90.     
  91.     var $token;
  92.  
  93.     /**
  94.     * The value associated with a token - eg. for T_STRING it's the string
  95.     *
  96.     * @var string 
  97.     * @access public
  98.     */ 
  99.     
  100.     var $value;
  101.  
  102.     /**
  103.      * Tokenizing options
  104.      * @access private
  105.      */
  106.     var $_options;
  107.     
  108.     /**
  109.     * Constructor
  110.     *
  111.     * Load the tokenizer - with a string to tokenize.
  112.     * tidies up array, sets vars pos, line, N and tokens
  113.     * 
  114.     * @param   string PHP code to serialize
  115.     * 
  116.     *
  117.     * @return   none 
  118.     * @access   public
  119.     */    
  120.     function __construct($data$options = array()) 
  121.     {
  122.         $this->tokens = docblock_tokenize($datatrue);
  123.         $this->count($this->tokens);
  124.         $this->pos = -1;
  125.         $this->line = 1;
  126.     }
  127.  
  128.     /**
  129.     * The main advance call required by the parser
  130.     *
  131.     * return true if a token is available, false if no more are available.
  132.     * skips stuff that is not a valid token
  133.     * stores lastcomment, lastcommenttoken
  134.     * 
  135.     *
  136.     * @return   boolean - true = have tokens
  137.     * @access   public
  138.     */    
  139.     function advance(
  140.     {
  141.         $this->pos++;
  142.         while ($this->pos $this->N
  143.             
  144.             if ($this->debug{
  145.                 echo docblock_token_name($this->tokens[$this->pos][0])'(' .
  146.                                 (PHP_Parser_DocblockParser::tokenName(PHP_Parser_DocblockParser::$transTable[$this->tokens[$this->pos[0]]])) .
  147.                                 ')' ." : {$this->tokens[$this->pos][1]}\n";
  148.             }
  149.  
  150.             switch ($this->tokens[$this->pos][0]{
  151.                 case DOCBLOCK_NEWLINE:
  152.                     $this->line++;
  153.                 default:
  154.                     $this->token $this->tokens[$this->pos][0];
  155.                     $this->value $this->tokens[$this->pos][1];
  156.                     $this->token = PHP_Parser_DocblockParser::$transTable[$this->token];
  157.                     return true;
  158.             }
  159.         }
  160.         //echo "END OF FILE?";
  161.         return false;
  162.         
  163.     }
  164.  
  165.     function getValue()
  166.     {
  167.         return $this->value;
  168.     }
  169.  
  170.     /**
  171.     * return something useful, when a parse error occurs.
  172.     *
  173.     * used to build error messages if the parser fails, and needs to know the line number..
  174.     *
  175.     * @return   string 
  176.     * @access   public
  177.     */
  178.     function parseError(
  179.     {
  180.         return "Error at line {$this->line}";
  181.         
  182.     }
  183. }

Documentation generated on Mon, 11 Mar 2019 15:41:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.