-
Notifications
You must be signed in to change notification settings - Fork 4
/
gan_tokenizer.php
567 lines (513 loc) · 16 KB
/
gan_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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?php
/**
* @author Niels A.D.
* @package Ganon
* @link http://code.google.com/p/ganon/
* @license http://dev.perl.org/licenses/artistic.html Artistic License
*/
/**
* Converts a document into tokens
*
* Can convert any string into tokens. The base class only supports
* identifier/whitespace tokens. For more tokens, the class can be
* easily extended.
*
* Use like:
* <code>
* <?php
* $a = new Tokenizer_Base('hello word');
* while ($a->next() !== $a::TOK_NULL) {
* echo $a->token, ': ',$a->getTokenString(), "<br>\n";
* }
* ?>
* </code>
*
* @internal The tokenizer works with a character map that connects a certain
* character to a certain function/token. This class is build with speed in mind.
*/
class Tokenizer_Base {
/**
* NULL Token, used at end of document (parsing should stop after this token)
*/
const TOK_NULL = 0;
/**
* Unknown token, used at unidentified character
*/
const TOK_UNKNOWN = 1;
/**
* Whitespace token, used with whitespace
*/
const TOK_WHITESPACE = 2;
/**
* Identifier token, used with identifiers
*/
const TOK_IDENTIFIER = 3;
/**
* The document that is being tokenized
* @var string
* @internal Public for faster access!
* @see setDoc()
* @see getDoc()
* @access private
*/
var $doc = '';
/**
* The size of the document (length of string)
* @var int
* @internal Public for faster access!
* @see $doc
* @access private
*/
var $size = 0;
/**
* Current (character) position in the document
* @var int
* @internal Public for faster access!
* @see setPos()
* @see getPos()
* @access private
*/
var $pos = 0;
/**
* Current (Line/Column) position in document
* @var array (Current_Line, Line_Starting_Pos)
* @internal Public for faster access!
* @see getLinePos()
* @access private
*/
var $line_pos = array(0, 0);
/**
* Current token
* @var int
* @internal Public for faster access!
* @see getToken()
* @access private
*/
var $token = self::TOK_NULL;
/**
* Startposition of token. If NULL, then current position is used.
* @var int
* @internal Public for faster access!
* @see getTokenString()
* @access private
*/
var $token_start = null;
/**
* List with all the character that can be considered as whitespace
* @var array|string
* @internal Variable is public + asscociated array for faster access!
* @internal array(' ' => true) will recognize space (' ') as whitespace
* @internal String will be converted to array in constructor
* @internal Result token will be {@link self::TOK_WHITESPACE};
* @see setWhitespace()
* @see getWhitespace()
* @access private
*/
var $whitespace = " \t\n\r\0\x0B";
/**
* List with all the character that can be considered as identifier
* @var array|string
* @internal Variable is public + asscociated array for faster access!
* @internal array('a' => true) will recognize 'a' as identifer
* @internal String will be converted to array in constructor
* @internal Result token will be {@link self::TOK_IDENTIFIER};
* @see setIdentifiers()
* @see getIdentifiers()
* @access private
*/
var $identifiers = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_';
/**
* All characters that should be mapped to a token/function that cannot be considered as whitespace or identifier
* @var array
* @internal Variable is public + asscociated array for faster access!
* @internal array('a' => 'parse_a') will call $this->parse_a() if it matches the character 'a'
* @internal array('a' => self::TOK_A) will set token to TOK_A if it matches the character 'a'
* @see mapChar()
* @see unmapChar()
* @access private
*/
var $custom_char_map = array();
/**
* Automaticly built character map. Built using {@link $identifiers}, {@link $whitespace} and {@link $custom_char_map}
* @var array
* @internal Public for faster access!
* @access private
*/
var $char_map = array();
/**
* All errors found while parsing the document
* @var array
* @see addError()
*/
var $errors = array();
/**
* Class constructor
* @param string $doc Document to be tokenized
* @param int $pos Position to start parsing
* @see setDoc()
* @see setPos()
*/
function __construct($doc = '', $pos = 0) {
$this->setWhitespace($this->whitespace);
$this->setIdentifiers($this->identifiers);
$this->setDoc($doc, $pos);
}
#php4 PHP4 class constructor compatibility
#function Tokenizer_Base($doc = '', $pos = 0) {return $this->__construct($doc, $pos);}
#php4e
/**
* Sets target document
* @param string $doc Document to be tokenized
* @param int $pos Position to start parsing
* @see getDoc()
* @see setPos()
*/
function setDoc($doc, $pos = 0) {
$this->doc = $doc;
$this->size = strlen($doc);
$this->setPos($pos);
}
/**
* Returns target document
* @return string
* @see setDoc()
*/
function getDoc() {
return $this->doc;
}
/**
* Sets position in document
* @param int $pos
* @see getPos()
*/
function setPos($pos = 0) {
$this->pos = $pos - 1;
$this->line_pos = array(0, 0);
$this->next();
}
/**
* Returns current position in document (Index)
* @return int
* @see setPos()
*/
function getPos() {
return $this->pos;
}
/**
* Returns current position in document (Line/Char)
* @return array array(Line, Column)
*/
function getLinePos() {
return array($this->line_pos[0], $this->pos - $this->line_pos[1]);
}
/**
* Returns current token
* @return int
* @see $token
*/
function getToken() {
return $this->token;
}
/**
* Returns current token as string
* @param int $start_offset Offset from token start
* @param int $end_offset Offset from token end
* @return string
*/
function getTokenString($start_offset = 0, $end_offset = 0) {
$token_start = ((is_int($this->token_start)) ? $this->token_start : $this->pos) + $start_offset;
$len = $this->pos - $token_start + 1 + $end_offset;
return (($len > 0) ? substr($this->doc, $token_start, $len) : '');
}
/**
* Sets characters to be recognized as whitespace
*
* Used like: setWhitespace('ab') or setWhitespace(array('a' => true, 'b', 'c'));
* @param string|array $ws
* @see getWhitespace();
*/
function setWhitespace($ws) {
if (is_array($ws)) {
$this->whitespace = array_fill_keys(array_values($ws), true);
$this->buildCharMap();
} else {
$this->setWhiteSpace(str_split($ws));
}
}
/**
* Returns whitespace characters as string/array
* @param bool $as_string Should the result be a string or an array?
* @return string|array
* @see setWhitespace()
*/
function getWhitespace($as_string = true) {
$ws = array_keys($this->whitespace);
return (($as_string) ? implode('', $ws) : $ws);
}
/**
* Sets characters to be recognized as identifier
*
* Used like: setIdentifiers('ab') or setIdentifiers(array('a' => true, 'b', 'c'));
* @param string|array $ident
* @see getIdentifiers();
*/
function setIdentifiers($ident) {
if (is_array($ident)) {
$this->identifiers = array_fill_keys(array_values($ident), true);
$this->buildCharMap();
} else {
$this->setIdentifiers(str_split($ident));
}
}
/**
* Returns identifier characters as string/array
* @param bool $as_string Should the result be a string or an array?
* @return string|array
* @see setIdentifiers()
*/
function getIdentifiers($as_string = true) {
$ident = array_keys($this->identifiers);
return (($as_string) ? implode('', $ident) : $ident);
}
/**
* Maps a custom character to a token/function
*
* Used like: mapChar('a', self::{@link TOK_IDENTIFIER}) or mapChar('a', 'parse_identifier');
* @param string $char Character that should be mapped. If set, it will be overriden
* @param int|string $map If function name, then $this->function will be called, otherwise token is set to $map
* @see unmapChar()
*/
function mapChar($char, $map) {
$this->custom_char_map[$char] = $map;
$this->buildCharMap();
}
/**
* Removes a char mapped with {@link mapChar()}
* @param string $char Character that should be unmapped
* @see mapChar()
*/
function unmapChar($char) {
unset($this->custom_char_map[$char]);
$this->buildCharMap();
}
/**
* Builds the {@link $map_char} array
* @internal Builds single array that maps all characters. Gets called if {@link $whitespace}, {@link $identifiers} or {@link $custom_char_map} get modified
*/
protected function buildCharMap() {
$this->char_map = $this->custom_char_map;
if (is_array($this->whitespace)) {
foreach($this->whitespace as $w => $v) {
$this->char_map[$w] = 'parse_whitespace';
}
}
if (is_array($this->identifiers)) {
foreach($this->identifiers as $i => $v) {
$this->char_map[$i] = 'parse_identifier';
}
}
}
/**
* Add error to the array and appends current position
* @param string $error
*/
function addError($error) {
$this->errors[] = htmlentities($error.' at '.($this->line_pos[0] + 1).', '.($this->pos - $this->line_pos[1] + 1).'!');
}
/**
* Parse whitespace
* @return int Token
* @internal Gets called with {@link $whitespace} characters
*/
protected function parse_whitespace() {
$this->token_start = $this->pos;
while(++$this->pos < $this->size) {
if (!isset($this->whitespace[$this->doc[$this->pos]])) {
break;
} elseif($this->doc[$this->pos] === "\r") {
++$this->line_pos[0];
if ($this->doc[$this->pos + 1] === "\n") {
++$this->pos;
}
$this->line_pos[1] = $this->pos;
} elseif($this->doc[$this->pos] === "\n") {
++$this->line_pos[0];
$this->line_pos[1] = $this->pos;
}
}
--$this->pos;
return self::TOK_WHITESPACE;
}
/**
* Parse identifiers
* @return int Token
* @internal Gets called with {@link $identifiers} characters
*/
protected function parse_identifier() {
$this->token_start = $this->pos;
while((++$this->pos < $this->size) && isset($this->identifiers[$this->doc[$this->pos]])) {}
--$this->pos;
return self::TOK_IDENTIFIER;
}
/**
* Continues to the next token
* @return int Next token ({@link TOK_NULL} if none)
*/
function next() {
$this->token_start = null;
if (++$this->pos < $this->size) {
if (isset($this->char_map[$this->doc[$this->pos]])) {
if (is_string($this->char_map[$this->doc[$this->pos]])) {
return ($this->token = $this->{$this->char_map[$this->doc[$this->pos]]}());
} else {
return ($this->token = $this->char_map[$this->doc[$this->pos]]);
}
} else {
return ($this->token = self::TOK_UNKNOWN);
}
} else {
return ($this->token = self::TOK_NULL);
}
}
/**
* Finds the next token, but skips whitespace
* @return int Next token ({@link TOK_NULL} if none)
*/
function next_no_whitespace() {
$this->token_start = null;
while (++$this->pos < $this->size) {
if (!isset($this->whitespace[$this->doc[$this->pos]])) {
if (isset($this->char_map[$this->doc[$this->pos]])) {
if (is_string($this->char_map[$this->doc[$this->pos]])) {
return ($this->token = $this->{$this->char_map[$this->doc[$this->pos]]}());
} else {
return ($this->token = $this->char_map[$this->doc[$this->pos]]);
}
} else {
return ($this->token = self::TOK_UNKNOWN);
}
} elseif($this->doc[$this->pos] === "\r") {
++$this->line_pos[0];
if ($this->doc[$this->pos + 1] === "\n") {
++$this->pos;
}
$this->line_pos[1] = $this->pos;
} elseif($this->doc[$this->pos] === "\n") {
++$this->line_pos[0];
$this->line_pos[1] = $this->pos;
}
}
return ($this->token = self::TOK_NULL);
}
/**
* Finds the next token using stopcharacters
*
* Used like: next_search('abc') or next_seach(array('a' => true, 'b' => true, 'c' => true));
* @param string|array $characters Characters to search for
* @param bool $callback Should the function check the charmap after finding a character?
* @return int Next token ({@link TOK_NULL} if none)
*/
function next_search($characters, $callback = true) {
$this->token_start = $this->pos;
if (!is_array($characters)) {
$characters = array_fill_keys(str_split($characters), true);
}
while(++$this->pos < $this->size) {
if (isset($characters[$this->doc[$this->pos]])) {
if ($callback && isset($this->char_map[$this->doc[$this->pos]])) {
if (is_string($this->char_map[$this->doc[$this->pos]])) {
return ($this->token = $this->{$this->char_map[$this->doc[$this->pos]]}());
} else {
return ($this->token = $this->char_map[$this->doc[$this->pos]]);
}
} else {
return ($this->token = self::TOK_UNKNOWN);
}
} elseif($this->doc[$this->pos] === "\r") {
++$this->line_pos[0];
if ($this->doc[$this->pos + 1] === "\n") {
++$this->pos;
}
$this->line_pos[1] = $this->pos;
} elseif($this->doc[$this->pos] === "\n") {
++$this->line_pos[0];
$this->line_pos[1] = $this->pos;
}
}
return ($this->token = self::TOK_NULL);
}
/**
* Finds the next token by searching for a string
* @param string $needle The needle that's being searched for
* @param bool $callback Should the function check the charmap after finding the needle?
* @return int Next token ({@link TOK_NULL} if none)
*/
function next_pos($needle, $callback = true) {
$this->token_start = $this->pos;
if (($this->pos < $this->size) && (($p = stripos($this->doc, $needle, $this->pos + 1)) !== false)) {
$len = $p - $this->pos - 1;
if ($len > 0) {
$str = substr($this->doc, $this->pos + 1, $len);
if (($l = strrpos($str, "\n")) !== false) {
++$this->line_pos[0];
$this->line_pos[1] = $l + $this->pos + 1;
$len -= $l;
if ($len > 0) {
$str = substr($str, 0, -$len);
$this->line_pos[0] += substr_count($str, "\n");
}
}
}
$this->pos = $p;
if ($callback && isset($this->char_map[$this->doc[$this->pos]])) {
if (is_string($this->char_map[$this->doc[$this->pos]])) {
return ($this->token = $this->{$this->char_map[$this->doc[$this->pos]]}());
} else {
return ($this->token = $this->char_map[$this->doc[$this->pos]]);
}
} else {
return ($this->token = self::TOK_UNKNOWN);
}
} else {
$this->pos = $this->size;
return ($this->token = self::TOK_NULL);
}
}
/**
* Expect a specific token or character. Adds error if token doesn't match.
* @param string|int $token Character or token to expect
* @param bool|int $do_next Go to next character before evaluating. 1 for next char, true to ignore whitespace
* @param bool|int $try_next Try next character if current doesn't match. 1 for next char, true to ignore whitespace
* @param bool|int $next_on_match Go to next character after evaluating. 1 for next char, true to ignore whitespace
* @return bool
*/
protected function expect($token, $do_next = true, $try_next = false, $next_on_match = 1) {
if ($do_next) {
if ($do_next === 1) {
$this->next();
} else {
$this->next_no_whitespace();
}
}
if (is_int($token)) {
if (($this->token !== $token) && ((!$try_next) || ((($try_next === 1) && ($this->next() !== $token)) || (($try_next === true) && ($this->next_no_whitespace() !== $token))))) {
$this->addError('Unexpected "'.$this->getTokenString().'"');
return false;
}
} else {
if (($this->doc[$this->pos] !== $token) && ((!$try_next) || (((($try_next === 1) && ($this->next() !== self::TOK_NULL)) || (($try_next === true) && ($this->next_no_whitespace() !== self::TOK_NULL))) && ($this->doc[$this->pos] !== $token)))) {
$this->addError('Expected "'.$token.'", but found "'.$this->getTokenString().'"');
return false;
}
}
if ($next_on_match) {
if ($next_on_match === 1) {
$this->next();
} else {
$this->next_no_whitespace();
}
}
return true;
}
}
?>