Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
TokenStream.php
001 <?php
002
003 /*
004 * This file is part of Twig.
005 *
006 * (c) Fabien Potencier
007 * (c) Armin Ronacher
008 *
009 * For the full copyright and license information, please view the LICENSE
010 * file that was distributed with this source code.
011 */
012
013 namespace Twig;
014
015 use Twig\Error\SyntaxError;
016
017 /**
018 * Represents a token stream.
019 *
020 * @author Fabien Potencier <fabien@symfony.com>
021 */
022 final class TokenStream
023 {
024 private $tokens;
025 private $current = 0;
026 private $source;
027
028 public function __construct(array $tokens, Source $source = null)
029 {
030 $this->tokens = $tokens;
031 $this->source = $source ?: new Source('', '');
032 }
033
034 public function __toString()
035 {
036 return implode("\n", $this->tokens);
037 }
038
039 public function injectTokens(array $tokens)
040 {
041 $this->tokens = array_merge(\array_slice($this->tokens, 0, $this->current), $tokens, \array_slice($this->tokens, $this->current));
042 }
043
044 /**
045 * Sets the pointer to the next token and returns the old one.
046 */
047 public function next(): Token
048 {
049 if (!isset($this->tokens[++$this->current])) {
050 throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current - 1]->getLine(), $this->source);
051 }
052
053 return $this->tokens[$this->current - 1];
054 }
055
056 /**
057 * Tests a token, sets the pointer to the next one and returns it or throws a syntax error.
058 *
059 * @return Token|null The next token if the condition is true, null otherwise
060 */
061 public function nextIf($primary, $secondary = null)
062 {
063 if ($this->tokens[$this->current]->test($primary, $secondary)) {
064 return $this->next();
065 }
066 }
067
068 /**
069 * Tests a token and returns it or throws a syntax error.
070 */
071 public function expect($type, $value = null, string $message = null): Token
072 {
073 $token = $this->tokens[$this->current];
074 if (!$token->test($type, $value)) {
075 $line = $token->getLine();
076 throw new SyntaxError(sprintf('%sUnexpected token "%s"%s ("%s" expected%s).',
077 $message ? $message.'. ' : '',
078 Token::typeToEnglish($token->getType()),
079 $token->getValue() ? sprintf(' of value "%s"', $token->getValue()) : '',
080 Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''),
081 $line,
082 $this->source
083 );
084 }
085 $this->next();
086
087 return $token;
088 }
089
090 /**
091 * Looks at the next token.
092 */
093 public function look(int $number = 1): Token
094 {
095 if (!isset($this->tokens[$this->current + $number])) {
096 throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current + $number - 1]->getLine(), $this->source);
097 }
098
099 return $this->tokens[$this->current + $number];
100 }
101
102 /**
103 * Tests the current token.
104 */
105 public function test($primary, $secondary = null): bool
106 {
107 return $this->tokens[$this->current]->test($primary, $secondary);
108 }
109
110 /**
111 * Checks if end of stream was reached.
112 */
113 public function isEOF(): bool
114 {
115 return /* Token::EOF_TYPE */ -1 === $this->tokens[$this->current]->getType();
116 }
117
118 public function getCurrent(): Token
119 {
120 return $this->tokens[$this->current];
121 }
122
123 /**
124 * Gets the source associated with this stream.
125 *
126 * @internal
127 */
128 public function getSourceContext(): Source
129 {
130 return $this->source;
131 }
132 }
133
134 class_alias('Twig\TokenStream', 'Twig_TokenStream');
135