forked from jbowens/jBBCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenManager.php
113 lines (98 loc) · 2.32 KB
/
TokenManager.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
<?php
namespace JBBCode;
/**
* @author Jackson Owens
*
* The TokenManager is used when constructing the parse tree. Before parsing begins, the TokenManager separates the string into
* left and right brackets ("[", "]") and string to make parsing easier.
*
*/
class TokenManager {
protected $tokens;
protected $i = 0;
/**
* Tokenizes the inputted string
*
* @param string $str the string to tokenize
*/
public function __construct( $str ) {
$this->tokens = array();
foreach(preg_split('/([\[\]])/', $str, -1, PREG_SPLIT_DELIM_CAPTURE) as $s)
{
if( $s != "" )
array_push($this->tokens, $s);
}
$this->restart();
}
/**
* Returns true if there is another token after the current one.
*
* @return true if there is another token to be read
*/
public function hasNext() {
return count( $this->tokens ) > ($this->i + 1);
}
/**
* Returns true if there is a current token.
*
* @return true if there is a current token
*/
public function hasCurrent() {
return count( $this->tokens ) > $this->i;
}
/**
* Returns the current token.
*
* @return the current token
*/
public function current() {
return $this->tokens[$this->i];
}
/**
* Alias for getCurrent()
*
* @return the current token
*/
public function getCurrent() {
return $this->current();
}
/**
* Returns the next token.
*
* @return the next token
*/
public function next() {
if( $this->hasNext() )
return $this->tokens[$this->i+1];
else
return null;
}
/**
* Alias for next().
*
* @return the next token
*/
public function getNext() {
return $this->next();
}
/**
* Returns the array of all the tokens.
*
* @return all tokens
*/
public function getAllTokens() {
return $this->tokens;
}
/**
* Moves the pointer back to the first token.
*/
public function restart() {
$this->i = 0;
}
/**
* Advances the token pointer to the next token.
*/
public function advance() {
$this->i++;
}
}