PHP_ParserGenerator
[ class tree: PHP_ParserGenerator ] [ index: PHP_ParserGenerator ] [ 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 216607 2006-07-18 00:53:10Z cellog $
  20. //
  21.  
  22. require_once dirname(__FILE__'/PHP_Parser.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 216607 2006-07-18 00:53:10Z cellog $
  44. */
  45.  
  46. class PHP_Parser_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.     * The value associated with a token plus preceding whitespace, if any
  104.     *
  105.     * This is only filled if whitespace attachment is turned on, for performance reasons
  106.     * @var string 
  107.     * @access public
  108.     */ 
  109.     
  110.     var $valueWithWhitespace;
  111.      
  112.     /**
  113.     * ID of the last Comment Token
  114.     *
  115.     * @var int 
  116.     * @access public
  117.     */ 
  118.     
  119.     var $lastCommentToken
  120.      
  121.     /**
  122.     * ID of the last Comment Token
  123.     *
  124.     * @var int 
  125.     * @access public
  126.     */ 
  127.     
  128.     var $lastCommentLine
  129.     
  130.     /**
  131.     * The string of the last Comment Token
  132.     *
  133.     * @var string 
  134.     * @access public
  135.     */ 
  136.     
  137.     var $lastComment;
  138.     
  139.     /**
  140.      * String of global variable to search for
  141.      *
  142.      * phpDocumentor-specific usage, extracted from
  143.      * documentation's @global tag
  144.      * @var string 
  145.      * @access private
  146.      */
  147.     var $_globalSearch = false;
  148.     
  149.     /**
  150.      * Tokenizing options
  151.      * @access private
  152.      */
  153.     var $_options;
  154.     private $_whitespace;
  155.     private $_trackingWhitespace = 0;
  156.     
  157.     /**
  158.     * Constructor
  159.     *
  160.     * Load the tokenizer - with a string to tokenize.
  161.     * tidies up array, sets vars pos, line, N and tokens
  162.     * 
  163.     * @param   string PHP code to serialize
  164.     * 
  165.     *
  166.     * @return   none 
  167.     * @access   public
  168.     */    
  169.     function __construct($data$options = array()) 
  170.     {
  171.         $this->_options['documentationParser'=
  172.         $this->_options['documentationLexer'=
  173.         $this->_options['publishAllDocumentation'=
  174.         $this->_options['documentationContainer'=
  175.         $this->_options['publisher'=
  176.         $this->_options['publishMethod'=
  177.         $this->_options['publishMessageClass'=
  178.         $this->_options['publishDocumentation'=
  179.         $this->_options['publishDocumentationMessage'=
  180.         false;
  181.         $this->_options array_merge($this->_options$options);
  182.         if (!class_exists($this->_options['documentationContainer'])) {
  183.             $this->_options['documentationContainer'= false;
  184.         }
  185.         if (!is_object($this->_options['documentationParser'])) {
  186.             $this->_options['documentationParser'= false;
  187.             $this->_options['documentationLexer'= false;
  188.         else {
  189.             $this->_options['documentationParser'&$options['documentationParser'];
  190.             $this->_options['documentationLexer'&$options['documentationLexer'];
  191.             // make sure it's an exact match
  192.         }
  193.         if (!is_object($this->_options['publisher'])) {
  194.             $this->_options['publisher'= false;
  195.             $this->_options['publishAllDocumentation'= false;
  196.         else {
  197.             if (!method_exists($this->_options['publisher']$this->_options['publishMethod'])) {
  198.                 $this->_options['publishMethod'= false;
  199.                 if (!method_exists($this->_options['publisher']'publish')) {
  200.                     $this->_options['publisher'= false;
  201.                     $this->_options['publishAllDocumentation'= false;
  202.                 else {
  203.                     $this->_options['publishMethod''publish';
  204.                 }
  205.             else {
  206.                 if (!class_exists($this->_options['publishMessageClass'])) {
  207.                     $this->_options['publishMessageClass'= false;
  208.                 }
  209.             }
  210.         }
  211.         $this->tokens token_get_all($data);
  212.         $this->count($this->tokens);
  213.         for ($i=0;$i<$this->N;$i++{
  214.             if (!is_array($this->tokens[$i])) {
  215.                 $this->tokens[$i= array(ord($this->tokens[$i]),$this->tokens[$i]);
  216.             }
  217.         }
  218.         $this->pos = -1;
  219.         $this->line = 1;
  220.     }
  221.  
  222.     function haltLexing()
  223.     {
  224.         $this->pos $this->N;
  225.     }
  226.  
  227.     /**
  228.      * Return the whitespace (if any) that preceded the current token
  229.      *
  230.      * @return string 
  231.      */
  232.     function getWhitespace()
  233.     {
  234.         return $this->_whitespace;
  235.     }
  236.  
  237.     /**
  238.      * @param MsgServer_Msg 
  239.      */
  240.     function handleMessage($msg)
  241.     {
  242.         if ($msg->getMsg(== 'find global'{
  243.             $this->_setGlobalSearch($msg->getData());
  244.         }
  245.     }
  246.     
  247.     /**
  248.      * @param string 
  249.      * @access private
  250.      */
  251.     function _setGlobalSearch($var)
  252.     {
  253.         $this->_globalSearch $var;
  254.     }
  255.  
  256.     function trackWhitespace()
  257.     {
  258.         $this->_trackingWhitespace++;
  259.     }
  260.  
  261.     function stopTrackingWhitespace()
  262.     {
  263.         $this->_trackingWhitespace--;
  264.     }
  265.  
  266.     /**
  267.      * Compare global variable to search value, to see if we've
  268.      * found a variable that must be documented
  269.      * @param string global variable found in source code
  270.      */
  271.     function globalSearch($var)
  272.     {
  273.         if ($this->_globalSearch{
  274.             $ret $var == $this->_globalSearch;
  275.             if ($ret{
  276.                 $this->_globalSearch = false;
  277.             }
  278.             return $ret;
  279.         else {
  280.             return false;
  281.         }
  282.     }
  283.     
  284.     /**
  285.     * get the last comment block (and reset it)
  286.     *
  287.     * 
  288.     *
  289.     * @return   array  ($commmentstring and $tokenPosition)
  290.     * @access   public
  291.     */
  292.     
  293.     function getLastComment()
  294.     {
  295.         $com $this->lastComment;
  296.         $tok $this->lastCommentToken;
  297.         $line $this->lastCommentLine;
  298.         $this->lastComment '';
  299.         $this->lastCommentToken = -1;
  300.         $this->lastCommentLine = -1;
  301.        
  302.         return array($com$tok$line);
  303.     }
  304.     
  305.     /**
  306.      * Helper function for advance(), parses and publishes doc
  307.      * comments as necessary
  308.      * @access private
  309.      */
  310.     function _handleDocumentation()
  311.     {
  312.         $this->lastComment $this->tokens[$this->pos][1];
  313.         $this->lastCommentLine $this->line;
  314.         $this->lastCommentToken $this->pos;
  315.         if ($this->_options['documentationParser']{
  316.             $parser &$this->_options['documentationParser'];
  317.             $err $parser->parse(array('comment' => $this->lastComment,
  318.                                         'commentline' => $this->lastCommentLine,
  319.                                         'commenttoken' => $this->lastCommentToken,
  320.                                         'lexer' => $this->_options['documentationLexer'],
  321.                                                       ));
  322.             if (PEAR::isError($err)) {
  323.                 $this->lastComment = false;
  324.                 $this->lastCommentLine = -1;
  325.                 $this->lastCommentToken = -1;
  326.             else {
  327.                 $this->lastComment $err;
  328.             }
  329.         }
  330.         if ($this->_options['publishAllDocumentation']{
  331.             $publish $this->_options['publishMethod'];
  332.             $message 'documentation';
  333.             if ($this->_options['publishDocumentationMessage']{
  334.                 $message $this->_options['publishDocumentationMessage'];
  335.             }
  336.             if ($this->_options['publishMessageClass']{
  337.                 $pc $this->_options['publishMessageClass'];
  338.                 $publisher $this->_options['publisher'];
  339.                 $message = new $pc($message$this->lastComment);
  340.                 $publisher->$publish($pc);
  341.             else {
  342.                 $publisher $this->_options['publisher'];
  343.                 $publisher->$publish($message$this->lastComment);
  344.             }
  345.         }
  346.     }
  347.  
  348.  
  349.     /**
  350.     * The main advance call required by the parser
  351.     *
  352.     * return true if a token is available, false if no more are available.
  353.     * skips stuff that is not a valid token
  354.     * stores lastcomment, lastcommenttoken
  355.     * 
  356.     *
  357.     * @return   boolean - true = have tokens
  358.     * @access   public
  359.     */
  360.   
  361.     
  362.     function advance(
  363.     {
  364.         $this->pos++;
  365.         while ($this->pos $this->N
  366.             
  367.             if ($this->debug{
  368.                 echo token_name($this->tokens[$this->pos][0])'(' .
  369.                                 (PHPyyParser::tokenName(PHPyyParser::$transTable[$this->tokens[$this->pos[0]]])) .
  370.                                 ')' ." : {$this->tokens[$this->pos][1]}\n";
  371.             }
  372.             static $T_DOC_COMMENT = false;
  373.             
  374.             if (!$T_DOC_COMMENT{
  375.                 $T_DOC_COMMENT = defined('T_DOC_COMMENT'? constant('T_DOC_COMMENT': 10000;
  376.             }
  377.  
  378.             switch ($this->tokens[$this->pos][0]{
  379.             
  380.             
  381.                 // simple ignore tags.
  382.                
  383.                 case T_CLOSE_TAG:
  384.                 case T_OPEN_TAG_WITH_ECHO:
  385.                     $this->pos++;
  386.                     continue;
  387.                 
  388.                 // comments - store for phpdoc
  389.                 case $T_DOC_COMMENT;
  390.                 
  391.                 
  392.                 case T_COMMENT:
  393.                     $this->lastComment '';
  394.                     $this->lastCommentToken = -1;
  395.                     $this->lastCommentLine = -1;
  396.                     if (substr($this->tokens[$this->pos][1],0,2== '/*'{
  397.                         $this->_handleDocumentation();
  398.                     }
  399.                     $this->line += substr_count ($this->tokens[$this->pos][1]"\n");
  400.                     $this->pos++;
  401.                     continue;
  402.                     
  403.                     $this->_handleDocumentation();
  404.                 // ... continues into m/l skipeed tags..
  405.                 
  406.                 // large 
  407.                 case T_OPEN_TAG:
  408.                 case T_INLINE_HTML:
  409.                 case T_ENCAPSED_AND_WHITESPACE:
  410.                 case T_WHITESPACE:
  411.                     if ($this->tokens[$this->pos][0== T_WHITESPACE{
  412.                         $this->_whitespace $this->tokens[$this->pos][1];
  413.                     }
  414.                     $this->line += substr_count ($this->tokens[$this->pos][1]"\n");
  415.                     $this->pos++;
  416.                     continue;
  417.                 
  418.                 //--- begin returnable values--
  419.                 
  420.                 // end statement - clear any comment details.
  421.                 case 59; // ord(';'):
  422.                     $this->lastComment '';
  423.                     $this->lastCommentToken = -1;
  424.                     
  425.                 // everything else!
  426.                 default:
  427.                     $this->line += substr_count ($this->tokens[$this->pos][1]"\n");
  428.                     
  429.                     $this->token $this->tokens[$this->pos][0];
  430.                     $this->value $this->tokens[$this->pos][1];
  431.                     $this->token = PHPyyParser::$transTable[$this->token];
  432.                     $this->valueWithWhitespace $this->_whitespace $this->value;
  433.                     $this->_whitespace '';
  434.                     return true;
  435.             }
  436.         }
  437.         //echo "END OF FILE?";
  438.         return false;
  439.         
  440.     }
  441.  
  442.     function getValue()
  443.     {
  444.         if ($this->_trackingWhitespace{
  445.             return $this->valueWithWhitespace;
  446.         }
  447.         return $this->value;
  448.     }
  449.  
  450.     /**
  451.     * return something useful, when a parse error occurs.
  452.     *
  453.     * used to build error messages if the parser fails, and needs to know the line number..
  454.     *
  455.     * @return   string 
  456.     * @access   public
  457.     */
  458.     function parseError(
  459.     {
  460.         return "Error at line {$this->line}";
  461.         
  462.     }
  463. }
  464. ?>

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