TokenStream.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_TokenStream
  18. {
  19. protected $tokens;
  20. protected $current;
  21. protected $filename;
  22. /**
  23. * Constructor.
  24. *
  25. * @param array $tokens An array of tokens
  26. * @param string $filename The name of the filename which tokens are associated with
  27. */
  28. public function __construct(array $tokens, $filename = null)
  29. {
  30. $this->tokens = $tokens;
  31. $this->current = 0;
  32. $this->filename = $filename;
  33. }
  34. /**
  35. * Returns a string representation of the token stream.
  36. *
  37. * @return string
  38. */
  39. public function __toString()
  40. {
  41. return implode("\n", $this->tokens);
  42. }
  43. public function injectTokens(array $tokens)
  44. {
  45. $this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
  46. }
  47. /**
  48. * Sets the pointer to the next token and returns the old one.
  49. *
  50. * @return Twig_Token
  51. */
  52. public function next()
  53. {
  54. if (!isset($this->tokens[++$this->current])) {
  55. throw new Twig_Error_Syntax('Unexpected end of template', -1, $this->filename);
  56. }
  57. return $this->tokens[$this->current - 1];
  58. }
  59. /**
  60. * Tests a token and returns it or throws a syntax error.
  61. *
  62. * @return Twig_Token
  63. */
  64. public function expect($type, $value = null, $message = null)
  65. {
  66. $token = $this->tokens[$this->current];
  67. if (!$token->test($type, $value)) {
  68. $line = $token->getLine();
  69. throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)',
  70. $message ? $message.'. ' : '',
  71. Twig_Token::typeToEnglish($token->getType(), $line), $token->getValue(),
  72. Twig_Token::typeToEnglish($type, $line), $value ? sprintf(' with value "%s"', $value) : ''),
  73. $line,
  74. $this->filename
  75. );
  76. }
  77. $this->next();
  78. return $token;
  79. }
  80. /**
  81. * Looks at the next token.
  82. *
  83. * @param integer $number
  84. *
  85. * @return Twig_Token
  86. */
  87. public function look($number = 1)
  88. {
  89. if (!isset($this->tokens[$this->current + $number])) {
  90. throw new Twig_Error_Syntax('Unexpected end of template', -1, $this->filename);
  91. }
  92. return $this->tokens[$this->current + $number];
  93. }
  94. /**
  95. * Tests the current token
  96. *
  97. * @return bool
  98. */
  99. public function test($primary, $secondary = null)
  100. {
  101. return $this->tokens[$this->current]->test($primary, $secondary);
  102. }
  103. /**
  104. * Checks if end of stream was reached
  105. *
  106. * @return bool
  107. */
  108. public function isEOF()
  109. {
  110. return $this->tokens[$this->current]->getType() === Twig_Token::EOF_TYPE;
  111. }
  112. /**
  113. * Gets the current token
  114. *
  115. * @return Twig_Token
  116. */
  117. public function getCurrent()
  118. {
  119. return $this->tokens[$this->current];
  120. }
  121. /**
  122. * Gets the filename associated with this stream
  123. *
  124. * @return string
  125. */
  126. public function getFilename()
  127. {
  128. return $this->filename;
  129. }
  130. }