Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Compiler
  • Engine
  • Helpers
  • HtmlNode
  • MacroNode
  • MacroTokens
  • Parser
  • PhpHelpers
  • PhpWriter
  • Token
  • TokenIterator
  • Tokenizer

Interfaces

  • ILoader
  • IMacro

Traits

  • Strict

Exceptions

  • CompileException
  • RegexpException
  • RuntimeException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Latte (https://latte.nette.org)
  5:  * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Latte;
  9: 
 10: 
 11: /**
 12:  * Traversing helper.
 13:  * @internal
 14:  */
 15: class TokenIterator
 16: {
 17:     use Strict;
 18: 
 19:     /** @var array */
 20:     public $tokens;
 21: 
 22:     /** @var int */
 23:     public $position = -1;
 24: 
 25:     /** @var array */
 26:     public $ignored = [];
 27: 
 28: 
 29:     /**
 30:      * @param array[]
 31:      */
 32:     public function __construct(array $tokens)
 33:     {
 34:         $this->tokens = $tokens;
 35:     }
 36: 
 37: 
 38:     /**
 39:      * Returns current token.
 40:      * @return array|null
 41:      */
 42:     public function currentToken()
 43:     {
 44:         return isset($this->tokens[$this->position])
 45:             ? $this->tokens[$this->position]
 46:             : null;
 47:     }
 48: 
 49: 
 50:     /**
 51:      * Returns current token value.
 52:      * @return string|null
 53:      */
 54:     public function currentValue()
 55:     {
 56:         return isset($this->tokens[$this->position])
 57:             ? $this->tokens[$this->position][Tokenizer::VALUE]
 58:             : null;
 59:     }
 60: 
 61: 
 62:     /**
 63:      * Returns next token.
 64:      * @param  int|string  (optional) desired token type or value
 65:      * @return array|null
 66:      */
 67:     public function nextToken()
 68:     {
 69:         return $this->scan(func_get_args(), true, true); // onlyFirst, advance
 70:     }
 71: 
 72: 
 73:     /**
 74:      * Returns next token value.
 75:      * @param  int|string  (optional) desired token type or value
 76:      * @return string|null
 77:      */
 78:     public function nextValue()
 79:     {
 80:         return $this->scan(func_get_args(), true, true, true); // onlyFirst, advance, strings
 81:     }
 82: 
 83: 
 84:     /**
 85:      * Returns all next tokens.
 86:      * @param  int|string  (optional) desired token type or value
 87:      * @return array[]
 88:      */
 89:     public function nextAll()
 90:     {
 91:         return $this->scan(func_get_args(), false, true); // advance
 92:     }
 93: 
 94: 
 95:     /**
 96:      * Returns all next tokens until it sees a given token type or value.
 97:      * @param  int|string  token type or value to stop before
 98:      * @return array[]
 99:      */
100:     public function nextUntil($arg)
101:     {
102:         return $this->scan(func_get_args(), false, true, false, true); // advance, until
103:     }
104: 
105: 
106:     /**
107:      * Returns concatenation of all next token values.
108:      * @param  int|string  (optional) token type or value to be joined
109:      * @return string
110:      */
111:     public function joinAll()
112:     {
113:         return $this->scan(func_get_args(), false, true, true); // advance, strings
114:     }
115: 
116: 
117:     /**
118:      * Returns concatenation of all next tokens until it sees a given token type or value.
119:      * @param  int|string  token type or value to stop before
120:      * @return string
121:      */
122:     public function joinUntil($arg)
123:     {
124:         return $this->scan(func_get_args(), false, true, true, true); // advance, strings, until
125:     }
126: 
127: 
128:     /**
129:      * Checks the current token.
130:      * @param  int|string  token type or value
131:      * @return bool
132:      */
133:     public function isCurrent($arg)
134:     {
135:         if (!isset($this->tokens[$this->position])) {
136:             return false;
137:         }
138:         $args = func_get_args();
139:         $token = $this->tokens[$this->position];
140:         return in_array($token[Tokenizer::VALUE], $args, true)
141:             || (isset($token[Tokenizer::TYPE]) && in_array($token[Tokenizer::TYPE], $args, true));
142:     }
143: 
144: 
145:     /**
146:      * Checks the next token existence.
147:      * @param  int|string  (optional) token type or value
148:      * @return bool
149:      */
150:     public function isNext()
151:     {
152:         return (bool) $this->scan(func_get_args(), true, false); // onlyFirst
153:     }
154: 
155: 
156:     /**
157:      * Checks the previous token existence.
158:      * @param  int|string  (optional) token type or value
159:      * @return bool
160:      */
161:     public function isPrev()
162:     {
163:         return (bool) $this->scan(func_get_args(), true, false, false, false, true); // onlyFirst, prev
164:     }
165: 
166: 
167:     /**
168:      * Returns next expected token or throws exception.
169:      * @param  int|string  (optional) desired token type or value
170:      * @return string
171:      * @throws CompileException
172:      */
173:     public function expectNextValue()
174:     {
175:         if ($token = $this->scan(func_get_args(), true, true)) { // onlyFirst, advance
176:             return $token[Tokenizer::VALUE];
177:         }
178:         $pos = $this->position + 1;
179:         while (($next = isset($this->tokens[$pos]) ? $this->tokens[$pos] : null) && in_array($next[Tokenizer::TYPE], $this->ignored, true)) {
180:             $pos++;
181:         }
182:         throw new CompileException($next ? "Unexpected token '" . $next[Tokenizer::VALUE] . "'." : 'Unexpected end.');
183:     }
184: 
185: 
186:     /**
187:      * @return static
188:      */
189:     public function reset()
190:     {
191:         $this->position = -1;
192:         return $this;
193:     }
194: 
195: 
196:     /**
197:      * Moves cursor to next token.
198:      */
199:     protected function next()
200:     {
201:         $this->position++;
202:     }
203: 
204: 
205:     /**
206:      * Looks for (first) (not) wanted tokens.
207:      * @param  array of desired token types or values
208:      * @param  bool
209:      * @param  bool
210:      * @param  bool
211:      * @param  bool
212:      * @param  bool
213:      * @return mixed
214:      */
215:     protected function scan($wanted, $onlyFirst, $advance, $strings = false, $until = false, $prev = false)
216:     {
217:         $res = $onlyFirst ? null : ($strings ? '' : []);
218:         $pos = $this->position + ($prev ? -1 : 1);
219:         do {
220:             if (!isset($this->tokens[$pos])) {
221:                 return $res;
222:             }
223: 
224:             $token = $this->tokens[$pos];
225:             $type = isset($token[Tokenizer::TYPE]) ? $token[Tokenizer::TYPE] : null;
226:             if (!$wanted || (in_array($token[Tokenizer::VALUE], $wanted, true) || in_array($type, $wanted, true)) ^ $until) {
227:                 while ($advance && !$prev && $pos > $this->position) {
228:                     $this->next();
229:                 }
230: 
231:                 if ($onlyFirst) {
232:                     return $strings ? $token[Tokenizer::VALUE] : $token;
233:                 } elseif ($strings) {
234:                     $res .= $token[Tokenizer::VALUE];
235:                 } else {
236:                     $res[] = $token;
237:                 }
238: 
239:             } elseif ($until || !in_array($type, $this->ignored, true)) {
240:                 return $res;
241:             }
242:             $pos += $prev ? -1 : 1;
243:         } while (true);
244:     }
245: }
246: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0