1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Tokenizer;
9:
10:
11: 12: 13:
14: class Stream
15: {
16:
17: public $tokens;
18:
19:
20: public $position = -1;
21:
22:
23: public $ignored = [];
24:
25:
26: 27: 28:
29: public function __construct(array $tokens)
30: {
31: $this->tokens = $tokens;
32: }
33:
34:
35: 36: 37: 38:
39: public function currentToken()
40: {
41: return isset($this->tokens[$this->position])
42: ? $this->tokens[$this->position]
43: : null;
44: }
45:
46:
47: 48: 49: 50:
51: public function currentValue()
52: {
53: return isset($this->tokens[$this->position])
54: ? $this->tokens[$this->position]->value
55: : null;
56: }
57:
58:
59: 60: 61: 62: 63:
64: public function nextToken()
65: {
66: return $this->scan(func_get_args(), true, true);
67: }
68:
69:
70: 71: 72: 73: 74:
75: public function nextValue()
76: {
77: return $this->scan(func_get_args(), true, true, true);
78: }
79:
80:
81: 82: 83: 84: 85:
86: public function nextAll()
87: {
88: return $this->scan(func_get_args(), false, true);
89: }
90:
91:
92: 93: 94: 95: 96:
97: public function nextUntil($arg)
98: {
99: return $this->scan(func_get_args(), false, true, false, true);
100: }
101:
102:
103: 104: 105: 106: 107:
108: public function joinAll()
109: {
110: return $this->scan(func_get_args(), false, true, true);
111: }
112:
113:
114: 115: 116: 117: 118:
119: public function joinUntil($arg)
120: {
121: return $this->scan(func_get_args(), false, true, true, true);
122: }
123:
124:
125: 126: 127: 128: 129:
130: public function isCurrent($arg)
131: {
132: if (!isset($this->tokens[$this->position])) {
133: return false;
134: }
135: $args = func_get_args();
136: $token = $this->tokens[$this->position];
137: return in_array($token->value, $args, true)
138: || in_array($token->type, $args, true);
139: }
140:
141:
142: 143: 144: 145: 146:
147: public function isNext()
148: {
149: return (bool) $this->scan(func_get_args(), true, false);
150: }
151:
152:
153: 154: 155: 156: 157:
158: public function isPrev()
159: {
160: return (bool) $this->scan(func_get_args(), true, false, false, false, true);
161: }
162:
163:
164: 165: 166:
167: public function reset()
168: {
169: $this->position = -1;
170: return $this;
171: }
172:
173:
174: 175: 176:
177: protected function next()
178: {
179: $this->position++;
180: }
181:
182:
183: 184: 185: 186: 187: 188: 189: 190: 191: 192:
193: protected function scan($wanted, $onlyFirst, $advance, $strings = false, $until = false, $prev = false)
194: {
195: $res = $onlyFirst ? null : ($strings ? '' : []);
196: $pos = $this->position + ($prev ? -1 : 1);
197: do {
198: if (!isset($this->tokens[$pos])) {
199: if (!$wanted && $advance && !$prev && $pos <= count($this->tokens)) {
200: $this->next();
201: }
202: return $res;
203: }
204:
205: $token = $this->tokens[$pos];
206: if (!$wanted || (in_array($token->value, $wanted, true) || in_array($token->type, $wanted, true)) ^ $until) {
207: while ($advance && !$prev && $pos > $this->position) {
208: $this->next();
209: }
210:
211: if ($onlyFirst) {
212: return $strings ? $token->value : $token;
213: } elseif ($strings) {
214: $res .= $token->value;
215: } else {
216: $res[] = $token;
217: }
218:
219: } elseif ($until || !in_array($token->type, $this->ignored, true)) {
220: return $res;
221: }
222: $pos += $prev ? -1 : 1;
223: } while (true);
224: }
225: }
226: