| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 10 /** 11 * The scanner tokenizes input content ({@link input()}) 12 * and provides a "scanner" interface which can go forward 13 * ({@link next()}) and backward (({@link back()}) ) 14 * along the tokens. It also correlates the toekens with line 15 * number ({@link line()}). 16 * 17 * Part of the credits for this scanner is due to phpDocumentor, 18 * from which I borrowed some ideas to make it work. 19 * 20 * @author Oak Nauhygon <ezpdo4php@gmail.com> 21 * @package core 22 * @version $Id$ 23 */ 24 class lmbPHPTokenizer 25 { 26 /** 27 * List of tokens that can contain a newline 28 * @var array 29 */ 30 static public $newline_tokens = array( 31 T_WHITESPACE, 32 T_ENCAPSED_AND_WHITESPACE, 33 T_COMMENT, 34 T_DOC_COMMENT, 35 T_OPEN_TAG, 36 T_CLOSE_TAG, 37 T_INLINE_HTML 38 ); 39 40 /** 41 * The input content 42 * @var string 43 */ 44 protected $input; 45 46 /** 47 * tokenized array from {@link token_get_all()} 48 * @var array 49 */ 50 protected $tokens; 51 52 /** 53 * current token position 54 * @var integer 55 */ 56 protected $pos = 0; 57 58 /** 59 * current source line number 60 * @var integer 61 */ 62 protected $line = 0; 63 64 /** 65 * Constructor 66 * @param string input content 67 */ 68 public function __construct($input = '') { 69 if (!empty($input)) { 70 $this->input($input); 71 } 72 } 73 74 /** 75 * get input if no param supplied or set input if param is a non-empty string 76 * @param string input content 77 * @return string|bool 78 */ 79 public function input($input = false) { 80 81 // if input is false, return 82 if ($input === false) { 83 return $this->input; 84 } 85 86 // trim the \r\n input content 87 $input = rtrim(ltrim($input, "\r\n")); 88 if (empty($input)) { 89 return false; 90 } 91 92 // use reference to save memory 93 $this->input = & $input; 94 95 // unset the tokens so when next() is called the frist 96 // time, it will call reset() 97 unset($this->tokens); 98 99 return true; 100 } 101 102 /** 103 * Tokenize input content and reset current 104 * token position and line number 105 * @return void 106 */ 107 public function reset() { 108 $this->tokens = @token_get_all($this->input); 109 $this->pos = 0; 110 $this->line = 0; 111 } 112 113 /** 114 * Fetch the next token 115 * @return string|array token from tokenizer 116 */ 117 function next() { 118 119 // check if we need to reset (tokenize input) 120 if (empty($this->tokens)) { 121 $this->reset(); 122 } 123 124 // check if token at the cur position set 125 if ( !isset($this->tokens[$this->pos]) ) { 126 return false; 127 } 128 129 // keep track of the old line 130 $oldline = $this->line; 131 132 // now get the current token 133 $word = $this->tokens[$this->pos++]; 134 135 // correlate line and token 136 if ( is_array($word) ) { 137 138 // count line num for special tokens ({@link $newline_tokens}) 139 if ( in_array($word[0], lmbPHPTokenizer::$newline_tokens) ) { 140 $this->line += substr_count($word[1], "\n"); 141 } 142 143 // always skip whitespace 144 if ( $word[0] == T_WHITESPACE ) { 145 return $this->next(); 146 } 147 } 148 149 return $word; 150 } 151 152 /** 153 * Go back one token (reverse of {@link next()}) 154 * @return false|string|array 155 */ 156 public function back() { 157 158 $this->pos --; 159 160 // check if it's the beginning 161 if ($this->pos < 0) { 162 $this->pos = 0; 163 return false; 164 } 165 166 $word = $this->tokens[$this->pos]; 167 168 if ( is_array($word) ) { 169 170 if ( $word[0] == T_WHITESPACE ) 171 return $this->next(); 172 173 if ( in_array($word[0], lmbPHPTokenizer::$newline_tokens) ) { 174 $this->line -= substr_count($word[1], "\n"); 175 } 176 } 177 } 178 179 /** 180 * Get the current line number 181 * @return integer 182 */ 183 public function line() { 184 return $this->line; 185 } 186 } 187 188 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Oct 15 04:31:08 2008 | Cross-referenced by PHPXref 0.7 |