/vendor/twig/twig/lib/Twig/TokenStream.php

https://gitlab.com/gideonmarked/PLCPortal · PHP · 192 lines · 91 code · 23 blank · 78 comment · 8 complexity · a0bf4c3c93cd6e82cf860e94e47509b2 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Represents a token stream.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_TokenStream
  17. {
  18. protected $tokens;
  19. protected $current = 0;
  20. protected $filename;
  21. private $source;
  22. /**
  23. * @param array $tokens An array of tokens
  24. * @param string|null $name The name of the template which tokens are associated with
  25. * @param string|null $source The source code associated with the tokens
  26. */
  27. public function __construct(array $tokens, $name = null, $source = null)
  28. {
  29. if (!$name instanceof Twig_Source) {
  30. if (null !== $name || null !== $source) {
  31. @trigger_error(sprintf('Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
  32. }
  33. $this->source = new Twig_Source($source, $name);
  34. } else {
  35. $this->source = $name;
  36. }
  37. $this->tokens = $tokens;
  38. // deprecated, not used anymore, to be removed in 2.0
  39. $this->filename = $this->source->getName();
  40. }
  41. public function __toString()
  42. {
  43. return implode("\n", $this->tokens);
  44. }
  45. public function injectTokens(array $tokens)
  46. {
  47. $this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
  48. }
  49. /**
  50. * Sets the pointer to the next token and returns the old one.
  51. *
  52. * @return Twig_Token
  53. */
  54. public function next()
  55. {
  56. if (!isset($this->tokens[++$this->current])) {
  57. throw new Twig_Error_Syntax('Unexpected end of template.', $this->tokens[$this->current - 1]->getLine(), $this->source->getName());
  58. }
  59. return $this->tokens[$this->current - 1];
  60. }
  61. /**
  62. * Tests a token, sets the pointer to the next one and returns it or throws a syntax error.
  63. *
  64. * @return Twig_Token|null The next token if the condition is true, null otherwise
  65. */
  66. public function nextIf($primary, $secondary = null)
  67. {
  68. if ($this->tokens[$this->current]->test($primary, $secondary)) {
  69. return $this->next();
  70. }
  71. }
  72. /**
  73. * Tests a token and returns it or throws a syntax error.
  74. *
  75. * @return Twig_Token
  76. */
  77. public function expect($type, $value = null, $message = null)
  78. {
  79. $token = $this->tokens[$this->current];
  80. if (!$token->test($type, $value)) {
  81. $line = $token->getLine();
  82. throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s).',
  83. $message ? $message.'. ' : '',
  84. Twig_Token::typeToEnglish($token->getType()), $token->getValue(),
  85. Twig_Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''),
  86. $line,
  87. $this->source->getName()
  88. );
  89. }
  90. $this->next();
  91. return $token;
  92. }
  93. /**
  94. * Looks at the next token.
  95. *
  96. * @param int $number
  97. *
  98. * @return Twig_Token
  99. */
  100. public function look($number = 1)
  101. {
  102. if (!isset($this->tokens[$this->current + $number])) {
  103. throw new Twig_Error_Syntax('Unexpected end of template.', $this->tokens[$this->current + $number - 1]->getLine(), $this->source->getName());
  104. }
  105. return $this->tokens[$this->current + $number];
  106. }
  107. /**
  108. * Tests the current token.
  109. *
  110. * @return bool
  111. */
  112. public function test($primary, $secondary = null)
  113. {
  114. return $this->tokens[$this->current]->test($primary, $secondary);
  115. }
  116. /**
  117. * Checks if end of stream was reached.
  118. *
  119. * @return bool
  120. */
  121. public function isEOF()
  122. {
  123. return $this->tokens[$this->current]->getType() === Twig_Token::EOF_TYPE;
  124. }
  125. /**
  126. * @return Twig_Token
  127. */
  128. public function getCurrent()
  129. {
  130. return $this->tokens[$this->current];
  131. }
  132. /**
  133. * Gets the name associated with this stream (null if not defined).
  134. *
  135. * @return string|null
  136. *
  137. * @deprecated since 1.27 (to be removed in 2.0)
  138. */
  139. public function getFilename()
  140. {
  141. @trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
  142. return $this->source->getName();
  143. }
  144. /**
  145. * Gets the source code associated with this stream.
  146. *
  147. * @return string
  148. *
  149. * @internal Don't use this as it might be empty depending on the environment configuration
  150. *
  151. * @deprecated since 1.27 (to be removed in 2.0)
  152. */
  153. public function getSource()
  154. {
  155. @trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
  156. return $this->source->getCode();
  157. }
  158. /**
  159. * Gets the source associated with this stream.
  160. *
  161. * @return Twig_Source
  162. *
  163. * @internal
  164. */
  165. public function getSourceContext()
  166. {
  167. return $this->source;
  168. }
  169. }