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

  • Stream
  • Token
  • Tokenizer

Exceptions

  • Exception
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Tokenizer;
  9: 
 10: 
 11: /**
 12:  * Stream of tokens.
 13:  */
 14: class Stream
 15: {
 16:     /** @var Token[] */
 17:     public $tokens;
 18: 
 19:     /** @var int */
 20:     public $position = -1;
 21: 
 22:     /** @var array */
 23:     public $ignored = [];
 24: 
 25: 
 26:     /**
 27:      * @param  Token[]
 28:      */
 29:     public function __construct(array $tokens)
 30:     {
 31:         $this->tokens = $tokens;
 32:     }
 33: 
 34: 
 35:     /**
 36:      * Returns current token.
 37:      * @return Token|null
 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:      * Returns current token value.
 49:      * @return string|null
 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:      * Returns next token.
 61:      * @param  int|string  (optional) desired token type or value
 62:      * @return Token|null
 63:      */
 64:     public function nextToken()
 65:     {
 66:         return $this->scan(func_get_args(), true, true); // onlyFirst, advance
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Returns next token value.
 72:      * @param  int|string  (optional) desired token type or value
 73:      * @return string|null
 74:      */
 75:     public function nextValue()
 76:     {
 77:         return $this->scan(func_get_args(), true, true, true); // onlyFirst, advance, strings
 78:     }
 79: 
 80: 
 81:     /**
 82:      * Returns all next tokens.
 83:      * @param  int|string  (optional) desired token type or value
 84:      * @return Token[]
 85:      */
 86:     public function nextAll()
 87:     {
 88:         return $this->scan(func_get_args(), false, true); // advance
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Returns all next tokens until it sees a given token type or value.
 94:      * @param  int|string  token type or value to stop before
 95:      * @return Token[]
 96:      */
 97:     public function nextUntil($arg)
 98:     {
 99:         return $this->scan(func_get_args(), false, true, false, true); // advance, until
100:     }
101: 
102: 
103:     /**
104:      * Returns concatenation of all next token values.
105:      * @param  int|string  (optional) token type or value to be joined
106:      * @return string
107:      */
108:     public function joinAll()
109:     {
110:         return $this->scan(func_get_args(), false, true, true); // advance, strings
111:     }
112: 
113: 
114:     /**
115:      * Returns concatenation of all next tokens until it sees a given token type or value.
116:      * @param  int|string  token type or value to stop before
117:      * @return string
118:      */
119:     public function joinUntil($arg)
120:     {
121:         return $this->scan(func_get_args(), false, true, true, true); // advance, strings, until
122:     }
123: 
124: 
125:     /**
126:      * Checks the current token.
127:      * @param  int|string  token type or value
128:      * @return bool
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:      * Checks the next token existence.
144:      * @param  int|string  (optional) token type or value
145:      * @return bool
146:      */
147:     public function isNext()
148:     {
149:         return (bool) $this->scan(func_get_args(), true, false); // onlyFirst
150:     }
151: 
152: 
153:     /**
154:      * Checks the previous token existence.
155:      * @param  int|string  (optional) token type or value
156:      * @return bool
157:      */
158:     public function isPrev()
159:     {
160:         return (bool) $this->scan(func_get_args(), true, false, false, false, true); // onlyFirst, prev
161:     }
162: 
163: 
164:     /**
165:      * @return static
166:      */
167:     public function reset()
168:     {
169:         $this->position = -1;
170:         return $this;
171:     }
172: 
173: 
174:     /**
175:      * Moves cursor to next token.
176:      */
177:     protected function next()
178:     {
179:         $this->position++;
180:     }
181: 
182: 
183:     /**
184:      * Looks for (first) (not) wanted tokens.
185:      * @param  array of desired token types or values
186:      * @param  bool
187:      * @param  bool
188:      * @param  bool
189:      * @param  bool
190:      * @param  bool
191:      * @return mixed
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: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0