forked from pear/PHP_ParserGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenizer.php
464 lines (412 loc) · 13.7 KB
/
Tokenizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: nobody <nobody@localhost> |
// +----------------------------------------------------------------------+
//
// $Id$
//
require_once dirname(__FILE__) . '/PHP_Parser.php';
/**
* The tokenizer wrapper for parser - implements the 'standard?' yylex interface
*
* 2 main methods:
* <ul>
* <li>constructor, which takes the data to parse
* calls php's internal tokenizer, then tidies up the array
* a little (key=>value) rather than mixed type.</li>
* <li>advance, which returns true while tokens are available
* - sets {@link $value}
* - sets {@link $token}
* </li>
* <li>parseError, which returns a string to appear on parser error messages.
* (could also display some of the code that has an error)
* </li>
*
* uses a few flags like:
* - {@link $line} - current line number
* - {@link $pos} - current token id
* - {@link $N} - total no. of tokens
* @version $Id$
*/
class PHP_Parser_Tokenizer {
/**
* Debugging on/off
*
* @var boolean
* @access public
*/
var $debug = false;
/**
* Tokens - array of all the tokens.
*
* @var array
* @access public
*/
var $tokens;
/**
* Total Number of tokens.
*
* @var int
* @access public
*/
var $N = 0;
/**
* Current line.
*
* @var int
* @access public
*/
var $line;
/**
* Current token position.
*
* @var int
* @access public
*/
var $pos = -1;
/**
* The current token (either a ord(';') or token numer - see php tokenizer.
*
* @var int
* @access public
*/
var $token;
/**
* The value associated with a token - eg. for T_STRING it's the string
*
* @var string
* @access public
*/
var $value;
/**
* The value associated with a token plus preceding whitespace, if any
*
* This is only filled if whitespace attachment is turned on, for performance reasons
* @var string
* @access public
*/
var $valueWithWhitespace;
/**
* ID of the last Comment Token
*
* @var int
* @access public
*/
var $lastCommentToken;
/**
* ID of the last Comment Token
*
* @var int
* @access public
*/
var $lastCommentLine;
/**
* The string of the last Comment Token
*
* @var string
* @access public
*/
var $lastComment;
/**
* String of global variable to search for
*
* phpDocumentor-specific usage, extracted from
* documentation's @global tag
* @var string
* @access private
*/
var $_globalSearch = false;
/**
* Tokenizing options
* @access private
*/
var $_options;
private $_whitespace;
private $_trackingWhitespace = 0;
/**
* Constructor
*
* Load the tokenizer - with a string to tokenize.
* tidies up array, sets vars pos, line, N and tokens
*
* @param string PHP code to serialize
*
*
* @return none
* @access public
*/
function __construct($data, $options = array())
{
$this->_options['documentationParser'] =
$this->_options['documentationLexer'] =
$this->_options['publishAllDocumentation'] =
$this->_options['documentationContainer'] =
$this->_options['publisher'] =
$this->_options['publishMethod'] =
$this->_options['publishMessageClass'] =
$this->_options['publishDocumentation'] =
$this->_options['publishDocumentationMessage'] =
false;
$this->_options = array_merge($this->_options, $options);
if (!class_exists($this->_options['documentationContainer'])) {
$this->_options['documentationContainer'] = false;
}
if (!is_object($this->_options['documentationParser'])) {
$this->_options['documentationParser'] = false;
$this->_options['documentationLexer'] = false;
} else {
$this->_options['documentationParser'] = &$options['documentationParser'];
$this->_options['documentationLexer'] = &$options['documentationLexer'];
// make sure it's an exact match
}
if (!is_object($this->_options['publisher'])) {
$this->_options['publisher'] = false;
$this->_options['publishAllDocumentation'] = false;
} else {
if (!method_exists($this->_options['publisher'], $this->_options['publishMethod'])) {
$this->_options['publishMethod'] = false;
if (!method_exists($this->_options['publisher'], 'publish')) {
$this->_options['publisher'] = false;
$this->_options['publishAllDocumentation'] = false;
} else {
$this->_options['publishMethod'] = 'publish';
}
} else {
if (!class_exists($this->_options['publishMessageClass'])) {
$this->_options['publishMessageClass'] = false;
}
}
}
$this->tokens = token_get_all($data);
$this->N = count($this->tokens);
for ($i=0;$i<$this->N;$i++) {
if (!is_array($this->tokens[$i])) {
$this->tokens[$i] = array(ord($this->tokens[$i]),$this->tokens[$i]);
}
}
$this->pos = -1;
$this->line = 1;
}
function haltLexing()
{
$this->pos = $this->N;
}
/**
* Return the whitespace (if any) that preceded the current token
*
* @return string
*/
function getWhitespace()
{
return $this->_whitespace;
}
/**
* @param MsgServer_Msg
*/
function handleMessage($msg)
{
if ($msg->getMsg() == 'find global') {
$this->_setGlobalSearch($msg->getData());
}
}
/**
* @param string
* @access private
*/
function _setGlobalSearch($var)
{
$this->_globalSearch = $var;
}
function trackWhitespace()
{
$this->_trackingWhitespace++;
}
function stopTrackingWhitespace()
{
$this->_trackingWhitespace--;
}
/**
* Compare global variable to search value, to see if we've
* found a variable that must be documented
* @param string global variable found in source code
*/
function globalSearch($var)
{
if ($this->_globalSearch) {
$ret = $var == $this->_globalSearch;
if ($ret) {
$this->_globalSearch = false;
}
return $ret;
} else {
return false;
}
}
/**
* get the last comment block (and reset it)
*
*
*
* @return array ($commmentstring and $tokenPosition)
* @access public
*/
function getLastComment()
{
$com = $this->lastComment;
$tok = $this->lastCommentToken;
$line = $this->lastCommentLine;
$this->lastComment = '';
$this->lastCommentToken = -1;
$this->lastCommentLine = -1;
return array($com, $tok, $line);
}
/**
* Helper function for advance(), parses and publishes doc
* comments as necessary
* @access private
*/
function _handleDocumentation()
{
$this->lastComment = $this->tokens[$this->pos][1];
$this->lastCommentLine = $this->line;
$this->lastCommentToken = $this->pos;
if ($this->_options['documentationParser']) {
$parser = &$this->_options['documentationParser'];
$err = $parser->parse(array('comment' => $this->lastComment,
'commentline' => $this->lastCommentLine,
'commenttoken' => $this->lastCommentToken,
'lexer' => $this->_options['documentationLexer'],
));
if (PEAR::isError($err)) {
$this->lastComment = false;
$this->lastCommentLine = -1;
$this->lastCommentToken = -1;
} else {
$this->lastComment = $err;
}
}
if ($this->_options['publishAllDocumentation']) {
$publish = $this->_options['publishMethod'];
$message = 'documentation';
if ($this->_options['publishDocumentationMessage']) {
$message = $this->_options['publishDocumentationMessage'];
}
if ($this->_options['publishMessageClass']) {
$pc = $this->_options['publishMessageClass'];
$publisher = $this->_options['publisher'];
$message = new $pc($message, $this->lastComment);
$publisher->$publish($pc);
} else {
$publisher = $this->_options['publisher'];
$publisher->$publish($message, $this->lastComment);
}
}
}
/**
* The main advance call required by the parser
*
* return true if a token is available, false if no more are available.
* skips stuff that is not a valid token
* stores lastcomment, lastcommenttoken
*
*
* @return boolean - true = have tokens
* @access public
*/
function advance()
{
$this->pos++;
while ($this->pos < $this->N) {
if ($this->debug) {
echo token_name($this->tokens[$this->pos][0]). '(' .
(PHPyyParser::tokenName(PHPyyParser::$transTable[$this->tokens[$this->pos[0]]])) .
')' ." : {$this->tokens[$this->pos][1]}\n";
}
static $T_DOC_COMMENT = false;
if (!$T_DOC_COMMENT) {
$T_DOC_COMMENT = defined('T_DOC_COMMENT') ? constant('T_DOC_COMMENT') : 10000;
}
switch ($this->tokens[$this->pos][0]) {
// simple ignore tags.
case T_CLOSE_TAG:
case T_OPEN_TAG_WITH_ECHO:
$this->pos++;
continue;
// comments - store for phpdoc
case $T_DOC_COMMENT;
case T_COMMENT:
$this->lastComment = '';
$this->lastCommentToken = -1;
$this->lastCommentLine = -1;
if (substr($this->tokens[$this->pos][1],0,2) == '/*') {
$this->_handleDocumentation();
}
$this->line += substr_count ($this->tokens[$this->pos][1], "\n");
$this->pos++;
continue;
$this->_handleDocumentation();
// ... continues into m/l skipeed tags..
// large
case T_OPEN_TAG:
case T_INLINE_HTML:
case T_ENCAPSED_AND_WHITESPACE:
case T_WHITESPACE:
if ($this->tokens[$this->pos][0] == T_WHITESPACE) {
$this->_whitespace = $this->tokens[$this->pos][1];
}
$this->line += substr_count ($this->tokens[$this->pos][1], "\n");
$this->pos++;
continue;
//--- begin returnable values--
// end statement - clear any comment details.
case 59; // ord(';'):
$this->lastComment = '';
$this->lastCommentToken = -1;
// everything else!
default:
$this->line += substr_count ($this->tokens[$this->pos][1], "\n");
$this->token = $this->tokens[$this->pos][0];
$this->value = $this->tokens[$this->pos][1];
$this->token = PHPyyParser::$transTable[$this->token];
$this->valueWithWhitespace = $this->_whitespace . $this->value;
$this->_whitespace = '';
return true;
}
}
//echo "END OF FILE?";
return false;
}
function getValue()
{
if ($this->_trackingWhitespace) {
return $this->valueWithWhitespace;
}
return $this->value;
}
/**
* return something useful, when a parse error occurs.
*
* used to build error messages if the parser fails, and needs to know the line number..
*
* @return string
* @access public
*/
function parseError()
{
return "Error at line {$this->line}";
}
}
?>